[CodeGen] Provide an appropriate alignment for dynamic allocas

GCC documents __builtin_alloca as aligning the storage to at least
__BIGGEST_ALIGNMENT__.

MSVC documents essentially the same for the x64 ABI:
https://msdn.microsoft.com/en-us/library/x9sx5da1.aspx

The 32-bit ABI follows the same rule: it emits a call to _alloca_probe_16

Differential Revision: https://reviews.llvm.org/D24378

llvm-svn: 285316
This commit is contained in:
David Majnemer 2016-10-27 17:18:24 +00:00
parent 07c915e1d5
commit 1878da43ea
2 changed files with 8 additions and 2 deletions

View File

@ -1139,7 +1139,13 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
case Builtin::BI_alloca:
case Builtin::BI__builtin_alloca: {
Value *Size = EmitScalarExpr(E->getArg(0));
return RValue::get(Builder.CreateAlloca(Builder.getInt8Ty(), Size));
const TargetInfo &TI = getContext().getTargetInfo();
// The alignment of the alloca should correspond to __BIGGEST_ALIGNMENT__.
unsigned SuitableAlignmentInBytes =
TI.getSuitableAlign() / TI.getCharWidth();
AllocaInst *AI = Builder.CreateAlloca(Builder.getInt8Ty(), Size);
AI->setAlignment(SuitableAlignmentInBytes);
return RValue::get(AI);
}
case Builtin::BIbzero:
case Builtin::BI__builtin_bzero: {

View File

@ -4,6 +4,6 @@
void capture(void *);
void test_alloca(int n) {
capture(_alloca(n));
// CHECK: %[[arg:.*]] = alloca i8, i32 %
// CHECK: %[[arg:.*]] = alloca i8, i32 %{{.*}}, align 16
// CHECK: call void @capture(i8* %[[arg]])
}