[llvm-commits] [llvm] r123060 - in /llvm/trunk: lib/Transforms/Scalar/LoopRotation.cpp test/Transforms/LoopRotate/phi-duplicate.ll

Chris Lattner sabre at nondot.org
Sat Jan 8 00:24:46 PST 2011


Author: lattner
Date: Sat Jan  8 02:24:46 2011
New Revision: 123060

URL: http://llvm.org/viewvc/llvm-project?rev=123060&view=rev
Log:
Have loop-rotate simplify instructions (yay instsimplify!) as it clones
them into the loop preheader, eliminating silly instructions like
"icmp i32 0, 100" in fixed tripcount loops.  This also better exposes the 
bigger problem with loop rotate that I'd like to fix: once this has been
folded, the duplicated conditional branch *often* turns into an uncond branch.

Not aggressively handling this is pessimizing later loop optimizations 
somethin' fierce by making "dominates all exit blocks" checks fail.

Modified:
    llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp
    llvm/trunk/test/Transforms/LoopRotate/phi-duplicate.ll

Modified: llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp?rev=123060&r1=123059&r2=123060&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopRotation.cpp Sat Jan  8 02:24:46 2011
@@ -14,9 +14,10 @@
 #define DEBUG_TYPE "loop-rotate"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Function.h"
-#include "llvm/Analysis/LoopPass.h"
-#include "llvm/Analysis/DominanceFrontier.h"
 #include "llvm/Analysis/CodeMetrics.h"
+#include "llvm/Analysis/DominanceFrontier.h"
+#include "llvm/Analysis/LoopPass.h"
+#include "llvm/Analysis/InstructionSimplify.h"
 #include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -205,9 +206,24 @@
     // Otherwise, create a duplicate of the instruction.
     Instruction *C = Inst->clone();
     
-    C->setName(Inst->getName());
-    C->insertBefore(LoopEntryBranch);
-    ValueMap[Inst] = C;
+    // Eagerly remap the operands of the instruction.
+    RemapInstruction(C, ValueMap,
+                     RF_NoModuleLevelChanges|RF_IgnoreMissingEntries);
+    
+    // With the operands remapped, see if the instruction constant folds or is
+    // otherwise simplifyable.  This commonly occurs because the entry from PHI
+    // nodes allows icmps and other instructions to fold.
+    if (Value *V = SimplifyInstruction(C)) {
+      // If so, then delete the temporary instruction and stick the folded value
+      // in the map.
+      delete C;
+      ValueMap[Inst] = V;
+    } else {
+      // Otherwise, stick the new instruction into the new block!
+      C->setName(Inst->getName());
+      C->insertBefore(LoopEntryBranch);
+      ValueMap[Inst] = C;
+    }
   }
 
   // Along with all the other instructions, we just cloned OrigHeader's

Modified: llvm/trunk/test/Transforms/LoopRotate/phi-duplicate.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopRotate/phi-duplicate.ll?rev=123060&r1=123059&r2=123060&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LoopRotate/phi-duplicate.ll (original)
+++ llvm/trunk/test/Transforms/LoopRotate/phi-duplicate.ll Sat Jan  8 02:24:46 2011
@@ -34,14 +34,13 @@
 
 ; CHECK:      define void @test
 ; CHECK-NEXT: entry:
-; CHECK-NEXT:   icmp slt i64
-; CHECK-NEXT:   br i1
+; CHECK-NEXT:   br i1 true, label %bb.nph, label %for.end
 ; CHECK-NOT:  :
 ; CHECK:      bb.nph:
 ; CHECK-NEXT:   br label %for.body
 ; CHECK-NOT:  :
 ; CHECK:      for.body:
-; CHECK-NEXT:   %j.02 = phi i64
+; CHECK-NEXT:   %j.01 = phi i64
 ; CHECK-NOT:    phi
 ; CHECK:        ret void
 ; CHECK-NEXT: }





More information about the llvm-commits mailing list