[llvm-commits] [llvm] r68071 - in /llvm/trunk: lib/Transforms/Scalar/LoopIndexSplit.cpp test/Transforms/LoopIndexSplit/2009-03-30-undef.ll

Devang Patel dpatel at apple.com
Mon Mar 30 15:24:10 PDT 2009


Author: dpatel
Date: Mon Mar 30 17:24:10 2009
New Revision: 68071

URL: http://llvm.org/viewvc/llvm-project?rev=68071&view=rev
Log:
Loop Index Split can eliminate a loop if it can determin if loop body is executed only once. There was a bug in determining IV based value of the iteration for which the loop body is executed. Fix it.

Added:
    llvm/trunk/test/Transforms/LoopIndexSplit/2009-03-30-undef.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/LoopIndexSplit.cpp

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

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopIndexSplit.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopIndexSplit.cpp Mon Mar 30 17:24:10 2009
@@ -48,6 +48,7 @@
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Cloning.h"
+#include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/Statistic.h"
@@ -345,10 +346,25 @@
   if (!L->isLoopInvariant(SplitValue))
     return false;
   Instruction *OPI = dyn_cast<Instruction>(OPV);
-  if (!OPI) return false;
+  if (!OPI) 
+    return false;
   if (OPI->getParent() != Header || isUsedOutsideLoop(OPI, L))
     return false;
-  
+  Value *StartValue = IVStartValue;
+  Value *ExitValue = IVExitValue;;
+
+  if (OPV != IndVar) {
+    // If BR operand is IV based then use this operand to calculate
+    // effective conditions for loop body.
+    BinaryOperator *BOPV = dyn_cast<BinaryOperator>(OPV);
+    if (!BOPV) 
+      return false;
+    if (BOPV->getOpcode() != Instruction::Add) 
+      return false;
+    StartValue = BinaryOperator::CreateAdd(OPV, StartValue, "" , BR);
+    ExitValue = BinaryOperator::CreateAdd(OPV, ExitValue, "" , BR);
+  }
+
   if (!cleanBlock(Header))
     return false;
 
@@ -399,13 +415,13 @@
   //      and i32 c1, c2 
   Instruction *C1 = new ICmpInst(ExitCondition->isSignedPredicate() ? 
                                  ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
-                                 SplitValue, IVStartValue, "lisplit", BR);
+                                 SplitValue, StartValue, "lisplit", BR);
 
   CmpInst::Predicate C2P  = ExitCondition->getPredicate();
   BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
   if (LatchBR->getOperand(0) != Header)
     C2P = CmpInst::getInversePredicate(C2P);
-  Instruction *C2 = new ICmpInst(C2P, SplitValue, IVExitValue, "lisplit", BR);
+  Instruction *C2 = new ICmpInst(C2P, SplitValue, ExitValue, "lisplit", BR);
   Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", BR);
 
   SplitCondition->replaceAllUsesWith(NSplitCond);
@@ -419,11 +435,11 @@
     if (Header != *SI)
       LatchSucc = *SI;
   }
-  LatchBR->setUnconditionalDest(LatchSucc);
 
-  // Remove IVIncrement
-  IVIncrement->replaceAllUsesWith(UndefValue::get(IVIncrement->getType()));
-  IVIncrement->eraseFromParent();
+  // Clean up latch block.
+  Value *LatchBRCond = LatchBR->getCondition();
+  LatchBR->setUnconditionalDest(LatchSucc);
+  RecursivelyDeleteTriviallyDeadInstructions(LatchBRCond);
   
   LPM->deleteLoopFromQueue(L);
 

Added: llvm/trunk/test/Transforms/LoopIndexSplit/2009-03-30-undef.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopIndexSplit/2009-03-30-undef.ll?rev=68071&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/LoopIndexSplit/2009-03-30-undef.ll (added)
+++ llvm/trunk/test/Transforms/LoopIndexSplit/2009-03-30-undef.ll Mon Mar 30 17:24:10 2009
@@ -0,0 +1,24 @@
+; RUN: llvm-as < %s | opt -loop-index-split | llvm-dis | not grep undef
+define i32 @main() {
+entry:
+	br label %header
+
+header:
+	%r = phi i32 [ 0, %entry ], [ %r3, %skip ]
+	%i = phi i32 [ 0, %entry ], [ %i1, %skip ]
+        %i99 = add i32 %i, 99
+	%cond = icmp eq i32 %i99, 3
+        br i1 %cond, label %body, label %skip
+
+body:
+        br label %skip
+
+skip:
+        %r3 = phi i32 [ %r, %header ], [ 3, %body ]
+        %i1 = add i32 %i, 1
+        %exitcond = icmp eq i32 %i1, 10
+        br i1 %exitcond, label %exit, label %header
+
+exit:
+        ret i32 %r3
+}





More information about the llvm-commits mailing list