Fix instcombine to obey requested alignment when merging allocas.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@164117 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Richard Osborne 2012-09-18 09:31:44 +00:00
parent 97ecb83dff
commit d7cc8b839c
2 changed files with 31 additions and 3 deletions

View File

@ -246,12 +246,16 @@ Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) {
return &AI;
}
// If the alignment of the entry block alloca is 0 (unspecified),
// assign it the preferred alignment.
if (EntryAI->getAlignment() == 0)
EntryAI->setAlignment(
TD->getPrefTypeAlignment(EntryAI->getAllocatedType()));
// Replace this zero-sized alloca with the one at the start of the entry
// block after ensuring that the address will be aligned enough for both
// types.
unsigned MaxAlign =
std::max(TD->getPrefTypeAlignment(EntryAI->getAllocatedType()),
TD->getPrefTypeAlignment(AI.getAllocatedType()));
unsigned MaxAlign = std::max(EntryAI->getAlignment(),
AI.getAlignment());
EntryAI->setAlignment(MaxAlign);
if (AI.getType() != EntryAI->getType())
return new BitCastInst(EntryAI, AI.getType());

View File

@ -0,0 +1,24 @@
; RUN: opt -S -instcombine < %s | FileCheck %s
; When merging zero sized alloca check that requested alignments of the allocas
; are obeyed.
@x = global i8* null, align 8
@y = global i8* null, align 8
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.8.0"
; CHECK: @f
; CHECK-NEXT: alloca [0 x i8], align 1024
; CHECK-NOT: alloca
; CHECK: ret void
define void @f() {
%1 = alloca [0 x i8], align 1
%2 = alloca [0 x i8], align 1024
%3 = getelementptr inbounds [0 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [0 x i8]* %2, i64 0, i64 0
store i8* %3, i8** @x, align 8
store i8* %4, i8** @y, align 8
ret void
}