[llvm-commits] [llvm] r47258 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/stacksaverestore.ll

Chris Lattner sabre at nondot.org
Sun Feb 17 22:12:38 PST 2008


Author: lattner
Date: Mon Feb 18 00:12:38 2008
New Revision: 47258

URL: http://llvm.org/viewvc/llvm-project?rev=47258&view=rev
Log:
optimize away stackrestore calls that have no intervening alloca or call.

Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
    llvm/trunk/test/Transforms/InstCombine/stacksaverestore.ll

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=47258&r1=47257&r2=47258&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Mon Feb 18 00:12:38 2008
@@ -8199,22 +8199,30 @@
         }
       }
       
-      // If the stack restore is in a return/unwind block and if there are no
-      // allocas or calls between the restore and the return, nuke the restore.
+      // Scan down this block to see if there is another stack restore in the
+      // same block without an intervening call/alloca.
+      BasicBlock::iterator BI = II;
       TerminatorInst *TI = II->getParent()->getTerminator();
-      if (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)) {
-        BasicBlock::iterator BI = II;
-        bool CannotRemove = false;
-        for (++BI; &*BI != TI; ++BI) {
-          if (isa<AllocaInst>(BI) ||
-              (isa<CallInst>(BI) && !isa<IntrinsicInst>(BI))) {
+      bool CannotRemove = false;
+      for (++BI; &*BI != TI; ++BI) {
+        if (isa<AllocaInst>(BI)) {
+          CannotRemove = true;
+          break;
+        }
+        if (isa<CallInst>(BI)) {
+          if (!isa<IntrinsicInst>(BI)) {
             CannotRemove = true;
             break;
           }
-        }
-        if (!CannotRemove)
+          // If there is a stackrestore below this one, remove this one.
           return EraseInstFromFunction(CI);
+        }
       }
+      
+      // If the stack restore is in a return/unwind block and if there are no
+      // allocas or calls between the restore and the return, nuke the restore.
+      if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)))
+        return EraseInstFromFunction(CI);
       break;
     }
     }

Modified: llvm/trunk/test/Transforms/InstCombine/stacksaverestore.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/stacksaverestore.ll?rev=47258&r1=47257&r2=47258&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/stacksaverestore.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/stacksaverestore.ll Mon Feb 18 00:12:38 2008
@@ -1,4 +1,7 @@
-; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep {call.*stackrestore}
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {call.*stackrestore} | count 1
+
+declare i8* @llvm.stacksave()
+declare void @llvm.stackrestore(i8*)
 
 ;; Test that llvm.stackrestore is removed when possible.
 define i32* @test1(i32 %P) {
@@ -13,7 +16,41 @@
 	ret void
 }
 
-declare i8* @llvm.stacksave()
+define void @foo(i32 %size) nounwind  {
+entry:
+	%tmp118124 = icmp sgt i32 %size, 0		; <i1> [#uses=1]
+	br i1 %tmp118124, label %bb.preheader, label %return
 
-declare void @llvm.stackrestore(i8*)
+bb.preheader:		; preds = %entry
+	%tmp25 = add i32 %size, -1		; <i32> [#uses=1]
+	%tmp125 = icmp slt i32 %size, 1		; <i1> [#uses=1]
+	%smax = select i1 %tmp125, i32 1, i32 %size		; <i32> [#uses=1]
+	br label %bb
+
+bb:		; preds = %bb, %bb.preheader
+	%i.0.reg2mem.0 = phi i32 [ 0, %bb.preheader ], [ %indvar.next, %bb ]		; <i32> [#uses=2]
+	%tmp = call i8* @llvm.stacksave( )		; <i8*> [#uses=1]
+	%tmp23 = alloca i8, i32 %size		; <i8*> [#uses=2]
+	%tmp27 = getelementptr i8* %tmp23, i32 %tmp25		; <i8*> [#uses=1]
+	store i8 0, i8* %tmp27, align 1
+	%tmp28 = call i8* @llvm.stacksave( )		; <i8*> [#uses=1]
+	%tmp52 = alloca i8, i32 %size		; <i8*> [#uses=1]
+	%tmp53 = call i8* @llvm.stacksave( )		; <i8*> [#uses=1]
+	%tmp77 = alloca i8, i32 %size		; <i8*> [#uses=1]
+	%tmp78 = call i8* @llvm.stacksave( )		; <i8*> [#uses=1]
+	%tmp102 = alloca i8, i32 %size		; <i8*> [#uses=1]
+	call void @bar( i32 %i.0.reg2mem.0, i8* %tmp23, i8* %tmp52, i8* %tmp77, i8* %tmp102, i32 %size ) nounwind 
+	call void @llvm.stackrestore( i8* %tmp78 )
+	call void @llvm.stackrestore( i8* %tmp53 )
+	call void @llvm.stackrestore( i8* %tmp28 )
+	call void @llvm.stackrestore( i8* %tmp )
+	%indvar.next = add i32 %i.0.reg2mem.0, 1		; <i32> [#uses=2]
+	%exitcond = icmp eq i32 %indvar.next, %smax		; <i1> [#uses=1]
+	br i1 %exitcond, label %return, label %bb
+
+return:		; preds = %bb, %entry
+	ret void
+}
+
+declare void @bar(i32, i8*, i8*, i8*, i8*, i32)
 





More information about the llvm-commits mailing list