[PATCH] Sink: Don't sink allocas

Tom Stellard tom at stellard.net
Mon Dec 16 07:17:34 PST 2013


From: Tom Stellard <thomas.stellard at amd.com>

CodeGen treats allocas outside the entry block as dynamically sized
stack objects.
---
 lib/Transforms/Scalar/Sink.cpp |  6 ++++++
 test/Transforms/Sink/basic.ll  | 23 +++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/lib/Transforms/Scalar/Sink.cpp b/lib/Transforms/Scalar/Sink.cpp
index d4595bb..c7f9e97 100644
--- a/lib/Transforms/Scalar/Sink.cpp
+++ b/lib/Transforms/Scalar/Sink.cpp
@@ -218,6 +218,12 @@ 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 alloca instructions.  CodeGen assumes allocas outside the
+  // entry block are dynamically sized stack objects.
+  if (dyn_cast<AllocaInst>(Inst))
+    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 85ab376..740e5a8 100644
--- a/test/Transforms/Sink/basic.ll
+++ b/test/Transforms/Sink/basic.ll
@@ -62,3 +62,26 @@ X:                                     ; preds = %5, %3
   ret i32 %R
 }
 
+; We shouldn't sink allocas, since CodeGen interprets allocas outside the
+; entry block as dynamically sized stack objects.
+
+; CHECK-LABEL: @alloca_sink
+; CHECK: entry:
+; CHECK-NEXT: alloca
+define i32 @alloca_sink(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
+}
-- 
1.8.1.5




More information about the llvm-commits mailing list