]> granicus.if.org Git - clang/commitdiff
Calculate alignment for local variables.
authorEli Friedman <eli.friedman@gmail.com>
Sat, 31 May 2008 21:20:41 +0000 (21:20 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sat, 31 May 2008 21:20:41 +0000 (21:20 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51826 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGDecl.cpp
test/CodeGen/align-local.c [new file with mode: 0644]

index 4541fc4caa349f354636aebf021e9a19b338884f..8723a668721ab588fdaca35d95d7de16b0a59d62 100644 (file)
@@ -129,8 +129,12 @@ void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
     if (!Target.useGlobalsForAutomaticVariables()) {
       // A normal fixed sized variable becomes an alloca in the entry block.
       const llvm::Type *LTy = ConvertType(Ty);
-      // TODO: Alignment
-      DeclPtr = CreateTempAlloca(LTy, D.getName());
+      llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
+      unsigned align = getContext().getTypeAlign(Ty);
+      if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
+        align = std::max(align, AA->getAlignment());
+      Alloc->setAlignment(align >> 3);
+      DeclPtr = Alloc;
     } else {
       // Targets that don't support recursion emit locals as globals.
       const char *Class =
diff --git a/test/CodeGen/align-local.c b/test/CodeGen/align-local.c
new file mode 100644 (file)
index 0000000..46f83fa
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: clang -emit-llvm < %s | grep "align 16" | count 2
+
+typedef struct __attribute((aligned(16))) {int x[4];} ff;
+
+int a() {
+  ff a;
+  struct {int x[4];} b __attribute((aligned(16)));
+}