Use DL preferred alignment for alloca in Value::getPointerAlignment

Teach Value::getPointerAlignment that allocas with no explicit alignment are aligned to preferred alignment of the allocated type.

Reviewed By: hfinkel

Differential Revision: http://reviews.llvm.org/D17569

llvm-svn: 267689
diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp
index cc20c48..08b4936 100644
--- a/llvm/lib/IR/Value.cpp
+++ b/llvm/lib/IR/Value.cpp
@@ -554,9 +554,14 @@
       if (EltTy->isSized())
         Align = DL.getABITypeAlignment(EltTy);
     }
-  } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this))
+  } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {
     Align = AI->getAlignment();
-  else if (auto CS = ImmutableCallSite(this))
+    if (Align == 0) {
+      Type *AllocatedType = AI->getAllocatedType();
+      if (AllocatedType->isSized())
+        Align = DL.getPrefTypeAlignment(AllocatedType);
+    }
+  } else if (auto CS = ImmutableCallSite(this))
     Align = CS.getAttributes().getParamAlignment(AttributeSet::ReturnIndex);
   else if (const LoadInst *LI = dyn_cast<LoadInst>(this))
     if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {