summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Stellard <thomas.stellard@amd.com>2014-03-21 15:51:51 +0000
committerTom Stellard <thomas.stellard@amd.com>2014-03-21 15:51:51 +0000
commit1b05bb2900d8bb69818ab13e0851a5b43642d2c7 (patch)
treede8903f7a79cda6e284195df8dbde405b82ce433
parent20115c69f50e9fd787ad7d0aae1d1b0d17a20e3d (diff)
downloadllvm-1b05bb2900d8bb69818ab13e0851a5b43642d2c7.tar.gz
llvm-1b05bb2900d8bb69818ab13e0851a5b43642d2c7.tar.bz2
llvm-1b05bb2900d8bb69818ab13e0851a5b43642d2c7.tar.xz
Sink: Don't sink static allocas from the entry block
CodeGen treats allocas outside the entry block as dynamically sized stack objects. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204473 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Transforms/Scalar/Sink.cpp7
-rw-r--r--test/Transforms/Sink/basic.ll79
2 files changed, 86 insertions, 0 deletions
diff --git a/lib/Transforms/Scalar/Sink.cpp b/lib/Transforms/Scalar/Sink.cpp
index 5e1e4564bb..41073749ab 100644
--- a/lib/Transforms/Scalar/Sink.cpp
+++ b/lib/Transforms/Scalar/Sink.cpp
@@ -216,6 +216,13 @@ bool Sinking::IsAcceptableTarget(Instruction *Inst,
/// instruction out of its current block into a successor.
bool Sinking::SinkInstruction(Instruction *Inst,
SmallPtrSet<Instruction *, 8> &Stores) {
+
+ // Don't sink static alloca instructions. CodeGen assumes allocas outside the
+ // entry block are dynamically sized stack objects.
+ if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
+ if (AI->isStaticAlloca())
+ return false;
+
// Check if it's safe to move the instruction.
if (!isSafeToMove(Inst, AA, Stores))
return false;
diff --git a/test/Transforms/Sink/basic.ll b/test/Transforms/Sink/basic.ll
index 85ab376600..4aac6d613a 100644
--- a/test/Transforms/Sink/basic.ll
+++ b/test/Transforms/Sink/basic.ll
@@ -62,3 +62,82 @@ X: ; preds = %5, %3
ret i32 %R
}
+; We shouldn't sink constant sized allocas from the entry block, since CodeGen
+; interprets allocas outside the entry block as dynamically sized stack objects.
+
+; CHECK-LABEL: @alloca_nosink
+; CHECK: entry:
+; CHECK-NEXT: alloca
+define i32 @alloca_nosink(i32 %a, i32 %b) {
+entry:
+ %0 = alloca i32
+ %1 = icmp ne i32 %a, 0
+ br i1 %1, label %if, label %endif
+
+if:
+ %2 = getelementptr i32* %0, i32 1
+ store i32 0, i32* %0
+ store i32 1, i32* %2
+ %3 = getelementptr i32* %0, i32 %b
+ %4 = load i32* %3
+ ret i32 %4
+
+endif:
+ ret i32 0
+}
+
+; Make sure we sink dynamic sized allocas
+
+; CHECK-LABEL: @alloca_sink_dynamic
+; CHECK: entry:
+; CHECK-NOT: alloca
+; CHECK: if:
+; CHECK-NEXT: alloca
+define i32 @alloca_sink_dynamic(i32 %a, i32 %b, i32 %size) {
+entry:
+ %0 = alloca i32, i32 %size
+ %1 = icmp ne i32 %a, 0
+ br i1 %1, label %if, label %endif
+
+if:
+ %2 = getelementptr i32* %0, i32 1
+ store i32 0, i32* %0
+ store i32 1, i32* %2
+ %3 = getelementptr i32* %0, i32 %b
+ %4 = load i32* %3
+ ret i32 %4
+
+endif:
+ ret i32 0
+}
+
+; We also want to sink allocas that are not in the entry block. These
+; will already be considered as dynamically sized stack objects, so sinking
+; them does no further damage.
+
+; CHECK-LABEL: @alloca_sink_nonentry
+; CHECK: if0:
+; CHECK-NOT: alloca
+; CHECK: if:
+; CHECK-NEXT: alloca
+define i32 @alloca_sink_nonentry(i32 %a, i32 %b, i32 %c) {
+entry:
+ %cmp = icmp ne i32 %c, 0
+ br i1 %cmp, label %endif, label %if0
+
+if0:
+ %0 = alloca i32
+ %1 = icmp ne i32 %a, 0
+ br i1 %1, label %if, label %endif
+
+if:
+ %2 = getelementptr i32* %0, i32 1
+ store i32 0, i32* %0
+ store i32 1, i32* %2
+ %3 = getelementptr i32* %0, i32 %b
+ %4 = load i32* %3
+ ret i32 %4
+
+endif:
+ ret i32 0
+}