[PATCH] Reassociate: reprocess RedoInsts after each instruction

Mehdi AMINI mehdi.amini at apple.com
Thu Jan 15 03:36:55 PST 2015


Previously the RedoInsts was processed at the end of the block.
However it was possible that it left behind some instructions that
were not canonicalized.
This should guarantee that any previous instruction in the basic
block is canonicalized before we process a new instruction.

http://reviews.llvm.org/D6995

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

Index: lib/Transforms/Scalar/Reassociate.cpp
===================================================================
--- lib/Transforms/Scalar/Reassociate.cpp
+++ lib/Transforms/Scalar/Reassociate.cpp
@@ -2261,22 +2261,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/crash2.ll
===================================================================
--- /dev/null
+++ test/Transforms/Reassociate/crash2.ll
@@ -0,0 +1,19 @@
+; RUN: opt -reassociate -disable-output < %s
+; ModuleID = 'bugpoint-reduced-simplified.bc'
+
+define i32 @foo() {
+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.18219.patch
Type: text/x-patch
Size: 2054 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150115/b62302ea/attachment.bin>


More information about the llvm-commits mailing list