[PATCH] D113105: [InstCombine] Extend stacksave/restore elimination

Itay Bookstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 8 23:35:26 PST 2021


nextsilicon-itay-bookstein updated this revision to Diff 385708.
nextsilicon-itay-bookstein marked an inline comment as done.
nextsilicon-itay-bookstein added a comment.

split from NFC refactor


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D113105/new/

https://reviews.llvm.org/D113105

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
  llvm/test/Transforms/InstCombine/stacksaverestore.ll


Index: llvm/test/Transforms/InstCombine/stacksaverestore.ll
===================================================================
--- llvm/test/Transforms/InstCombine/stacksaverestore.ll
+++ llvm/test/Transforms/InstCombine/stacksaverestore.ll
@@ -110,3 +110,27 @@
 ; CHECK: call void @llvm.stackrestore(i8* %save1)
 ; CHECK: br i1 %done, label %loop, label %return
 ; CHECK: ret void
+
+define i32 @test4(i32 %m, i32* nocapture readonly %a) {
+entry:
+  br label %for.body
+
+for.body:
+  %x.012 = phi i32 [ 0, %entry ], [ %add, %for.body ]
+  %i.011 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+  %0 = call i8* @llvm.stacksave()
+  %1 = load i32, i32* %a, align 4
+  %mul = mul nsw i32 %1, %m
+  %add = add nsw i32 %mul, %x.012
+  call void @llvm.stackrestore(i8* %0)
+  %inc = add nuw nsw i32 %i.011, 1
+  %exitcond.not = icmp eq i32 %inc, 100
+  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
+
+for.cond.cleanup:
+  ret i32 %add
+}
+
+; CHECK-LABEL: define i32 @test4(
+; CHECK-NOT: call void @llvm.stackrestore
+; CHECK: ret i32 %add
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1802,14 +1802,23 @@
       return ClassifyResult::None;
     };
 
-    // If the save is right next to the restore, remove the restore.  This can
-    // happen when variable allocas are DCE'd.
+    // If there is no intervening call/alloca between the save and the restore,
+    // remove the restore. This can happen when variable allocas are DCE'd.
     if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
-      if (SS->getIntrinsicID() == Intrinsic::stacksave) {
-        // Skip over debug info.
-        if (SS->getNextNonDebugInstruction() == II) {
-          return eraseInstFromFunction(CI);
+      if (SS->getIntrinsicID() == Intrinsic::stacksave &&
+          SS->getParent() == II->getParent()) {
+        BasicBlock::iterator BI(SS);
+        bool CannotRemove = false;
+        for (++BI; &*BI != II; ++BI) {
+          if (Classify(&*BI) == ClassifyResult::None)
+            continue;
+
+          CannotRemove = true;
+          break;
         }
+
+        if (!CannotRemove)
+          return eraseInstFromFunction(CI);
       }
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113105.385708.patch
Type: text/x-patch
Size: 2379 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211109/9334a375/attachment.bin>


More information about the llvm-commits mailing list