[PATCH] Reassociate: reprocess RedoInsts after each instruction

Mehdi AMINI mehdi.amini at apple.com
Thu Jan 15 21:35:47 PST 2015


Taken comments into account: clang-format and CHECK line in the test.


http://reviews.llvm.org/D6995

Files:
  lib/Transforms/Scalar/Reassociate.cpp
  test/Transforms/Reassociate/crash3.ll

Index: lib/Transforms/Scalar/Reassociate.cpp
===================================================================
--- lib/Transforms/Scalar/Reassociate.cpp
+++ lib/Transforms/Scalar/Reassociate.cpp
@@ -2264,22 +2264,26 @@
   MadeChange = false;
   for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
     // Optimize every instruction in the basic block.
-    for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; )
+    for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; ) {
       if (isInstructionTriviallyDead(II)) {
         EraseInst(II++);
       } else {
         OptimizeInst(II);
         assert(II->getParent() == BI && "Moved to a different block!");
         ++II;
       }
 
-    // If this produced extra instructions to optimize, handle them now.
-    while (!RedoInsts.empty()) {
-      Instruction *I = RedoInsts.pop_back_val();
-      if (isInstructionTriviallyDead(I))
-        EraseInst(I);
-      else
-        OptimizeInst(I);
+      // If this produced extra instructions to optimize, handle them now.
+      while (!RedoInsts.empty()) {
+        Instruction *I = RedoInsts.pop_back_val();
+        if (I == II)
+          // Will be processed next iteration on the basic block
+          continue;
+        if (isInstructionTriviallyDead(I))
+          EraseInst(I);
+        else
+          OptimizeInst(I);
+      }
     }
   }
 
Index: test/Transforms/Reassociate/crash3.ll
===================================================================
--- /dev/null
+++ test/Transforms/Reassociate/crash3.ll
@@ -0,0 +1,22 @@
+; RUN: opt -reassociate -o - -S %s | FileCheck %s
+; ModuleID = 'bugpoint-reduced-simplified.bc'
+
+
+define i32 @foo() {
+; CHECK-LABEL: @foo
+; CHECK: %factor = mul i32 %3, -4
+wrapper_entry:
+  %0 = udiv i32 1, undef
+  %1 = mul i32 undef, %0
+  %2 = add i32 %1, undef
+  %3 = add i32 %2, 1
+  %4 = add i32 %3, undef
+  %5 = add i32 %3, 1
+  %6 = mul i32 %4, -2
+  %7 = add i32 %5, %6
+  %8 = add i32 %6, %7
+  ; this instruction is intentionally dead
+  %9 = add i32 %8, 1
+  %10 = add i32 undef, %8
+  ret i32 %10
+}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D6995.18290.patch
Type: text/x-patch
Size: 2120 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150116/16fd0050/attachment.bin>


More information about the llvm-commits mailing list