[llvm] r204473 - Sink: Don't sink static allocas from the entry block

Tom Stellard thomas.stellard at amd.com
Fri Mar 21 08:51:51 PDT 2014


Author: tstellar
Date: Fri Mar 21 10:51:51 2014
New Revision: 204473

URL: http://llvm.org/viewvc/llvm-project?rev=204473&view=rev
Log:
Sink: Don't sink static allocas from the entry block

CodeGen treats allocas outside the entry block as dynamically sized
stack objects.

Modified:
    llvm/trunk/lib/Transforms/Scalar/Sink.cpp
    llvm/trunk/test/Transforms/Sink/basic.ll

Modified: llvm/trunk/lib/Transforms/Scalar/Sink.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/Sink.cpp?rev=204473&r1=204472&r2=204473&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/Sink.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/Sink.cpp Fri Mar 21 10:51:51 2014
@@ -216,6 +216,13 @@ bool Sinking::IsAcceptableTarget(Instruc
 /// 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;

Modified: llvm/trunk/test/Transforms/Sink/basic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Sink/basic.ll?rev=204473&r1=204472&r2=204473&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/Sink/basic.ll (original)
+++ llvm/trunk/test/Transforms/Sink/basic.ll Fri Mar 21 10:51:51 2014
@@ -62,3 +62,82 @@ X:                                     ;
   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
+}





More information about the llvm-commits mailing list