Change SCEVExpander's expandCodeFor to provide more flexibility
authorDan Gohman <gohman@apple.com>
Thu, 23 Apr 2009 15:16:49 +0000 (15:16 +0000)
committerDan Gohman <gohman@apple.com>
Thu, 23 Apr 2009 15:16:49 +0000 (15:16 +0000)
with the persistent insertion point, and change IndVars to make
use of it. This fixes a bug where IndVars was holding on to a
stale insertion point and forcing the SCEVExpander to continue to
use it.

This fixes PR4038.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69892 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/ScalarEvolutionExpander.h
lib/Analysis/ScalarEvolutionExpander.cpp
lib/Transforms/Scalar/IndVarSimplify.cpp
test/Transforms/IndVarSimplify/casted-argument.ll

index 6e2db29f013623a6566fe7cc50398270af4a317c..b6b45788ea7cff43e1f028f982252aa7200be87a 100644 (file)
@@ -71,13 +71,23 @@ namespace llvm {
       InsertedInstructions.insert(I);
     }
 
+    void setInsertionPoint(BasicBlock::iterator NewIP) { InsertPt = NewIP; }
+
     BasicBlock::iterator getInsertionPoint() const { return InsertPt; }
 
+    /// expandCodeFor - Insert code to directly compute the specified SCEV
+    /// expression into the program.  The inserted code is inserted into the
+    /// SCEVExpander's current insertion point.
+    Value *expandCodeFor(SCEVHandle SH, const Type *Ty);
+
     /// expandCodeFor - Insert code to directly compute the specified SCEV
     /// expression into the program.  The inserted code is inserted into the
     /// specified block.
     Value *expandCodeFor(SCEVHandle SH, const Type *Ty,
-                         BasicBlock::iterator IP);
+                         BasicBlock::iterator IP) {
+      setInsertionPoint(IP);
+      return expandCodeFor(SH, Ty);
+    }
 
     /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
     /// we can to share the casts.
index 80b47d89240a7919d5a25f090033e10fd9895497..9676ea20af5b3b9c3ae13a5a4d4b8eafc83af8d3 100644 (file)
@@ -332,12 +332,10 @@ Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
   return LHS;
 }
 
-Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty,
-                                   BasicBlock::iterator IP) {
+Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty) {
   // Expand the code for this SCEV.
   assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
          "non-trivial casts should be done with the SCEVs directly!");
-  InsertPt = IP;
   Value *V = expand(SH);
   return InsertNoopCastOfTo(V, Ty);
 }
index 63ef021eb312c960b13e5d3d344578db75ce7b90..cc3919da846e2f1e049a0b56f51c7db9466f1e1c 100644 (file)
@@ -543,8 +543,7 @@ static Value *getSignExtendedTruncVar(const SCEVAddRecExpr *AR,
                                       ScalarEvolution *SE,
                                       const Type *LargestType, Loop *L, 
                                       const Type *myType,
-                                      SCEVExpander &Rewriter, 
-                                      BasicBlock::iterator InsertPt) {
+                                      SCEVExpander &Rewriter) {
   SCEVHandle ExtendedStart =
     SE->getSignExtendExpr(AR->getStart(), LargestType);
   SCEVHandle ExtendedStep =
@@ -553,15 +552,14 @@ static Value *getSignExtendedTruncVar(const SCEVAddRecExpr *AR,
     SE->getAddRecExpr(ExtendedStart, ExtendedStep, L);
   if (LargestType != myType)
     ExtendedAddRec = SE->getTruncateExpr(ExtendedAddRec, myType);
-  return Rewriter.expandCodeFor(ExtendedAddRec, myType, InsertPt);
+  return Rewriter.expandCodeFor(ExtendedAddRec, myType);
 }
 
 static Value *getZeroExtendedTruncVar(const SCEVAddRecExpr *AR,
                                       ScalarEvolution *SE,
                                       const Type *LargestType, Loop *L, 
                                       const Type *myType,
-                                      SCEVExpander &Rewriter, 
-                                      BasicBlock::iterator InsertPt) {
+                                      SCEVExpander &Rewriter) {
   SCEVHandle ExtendedStart =
     SE->getZeroExtendExpr(AR->getStart(), LargestType);
   SCEVHandle ExtendedStep =
@@ -570,7 +568,7 @@ static Value *getZeroExtendedTruncVar(const SCEVAddRecExpr *AR,
     SE->getAddRecExpr(ExtendedStart, ExtendedStep, L);
   if (LargestType != myType)
     ExtendedAddRec = SE->getTruncateExpr(ExtendedAddRec, myType);
-  return Rewriter.expandCodeFor(ExtendedAddRec, myType, InsertPt);
+  return Rewriter.expandCodeFor(ExtendedAddRec, myType);
 }
 
 /// allUsesAreSameTyped - See whether all Uses of I are instructions
@@ -699,6 +697,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
   // recurrences in terms of the induction variable.  Start with the auxillary
   // induction variables, and recursively rewrite any of their uses.
   BasicBlock::iterator InsertPt = Header->getFirstNonPHI();
+  Rewriter.setInsertionPoint(InsertPt);
 
   // If there were induction variables of other sizes, cast the primary
   // induction variable to the right size for them, avoiding the need for the
@@ -718,7 +717,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
   while (!IndVars.empty()) {
     PHINode *PN = IndVars.back().first;
     const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(IndVars.back().second);
-    Value *NewVal = Rewriter.expandCodeFor(AR, PN->getType(), InsertPt);
+    Value *NewVal = Rewriter.expandCodeFor(AR, PN->getType());
     DOUT << "INDVARS: Rewrote IV '" << *AR << "' " << *PN
          << "   into = " << *NewVal << "\n";
     NewVal->takeName(PN);
@@ -732,7 +731,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
         Instruction *UInst = dyn_cast<Instruction>(*UI);
         if (UInst && isa<SExtInst>(UInst) && NoSignedWrap) {
           Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType, L, 
-                                         UInst->getType(), Rewriter, InsertPt);
+                                         UInst->getType(), Rewriter);
           UInst->replaceAllUsesWith(TruncIndVar);
           DeadInsts.insert(UInst);
         }
@@ -753,8 +752,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
               SExtInst* oldSext = dyn_cast<SExtInst>(UInst->use_begin());
               uint64_t truncSize = oldSext->getType()->getPrimitiveSizeInBits();
               Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
-                                                L, oldSext->getType(), Rewriter,
-                                                InsertPt);
+                                                L, oldSext->getType(), Rewriter);
               APInt APnewAddRHS = APInt(AddRHS->getValue()).sext(newBitSize);
               if (newBitSize > truncSize)
                 APnewAddRHS = APnewAddRHS.trunc(truncSize);
@@ -784,8 +782,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
             SExtInst* oldSext = dyn_cast<SExtInst>(UInst->use_begin());
             uint64_t truncSize = oldSext->getType()->getPrimitiveSizeInBits();
             Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
-                                              L, oldSext->getType(), Rewriter,
-                                              InsertPt);
+                                              L, oldSext->getType(), Rewriter);
             APInt APnewOrRHS = APInt(RHS->getValue()).sext(newBitSize);
             if (newBitSize > truncSize)
               APnewOrRHS = APnewOrRHS.trunc(truncSize);
@@ -805,7 +802,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
         // A zext of a signed variable known not to overflow is still safe.
         if (UInst && isa<ZExtInst>(UInst) && (NoUnsignedWrap || NoSignedWrap)) {
           Value *TruncIndVar = getZeroExtendedTruncVar(AR, SE, LargestType, L, 
-                                         UInst->getType(), Rewriter, InsertPt);
+                                         UInst->getType(), Rewriter);
           UInst->replaceAllUsesWith(TruncIndVar);
           DeadInsts.insert(UInst);
         }
@@ -822,7 +819,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
           ZExtInst* oldZext = dyn_cast<ZExtInst>(UInst->use_begin());
           uint64_t truncSize = oldZext->getType()->getPrimitiveSizeInBits();
           Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
-                                  L, oldZext->getType(), Rewriter, InsertPt);
+                                  L, oldZext->getType(), Rewriter);
           APInt APnewAndRHS = APInt(AndRHS->getValue()).zext(newBitSize);
           if (newBitSize > truncSize)
             APnewAndRHS = APnewAndRHS.trunc(truncSize);
@@ -858,7 +855,7 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
             ZExtInst* oldZext = dyn_cast<ZExtInst>(UInst2->use_begin());
             uint64_t truncSize = oldZext->getType()->getPrimitiveSizeInBits();
             Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
-                                    L, oldZext->getType(), Rewriter, InsertPt);
+                                    L, oldZext->getType(), Rewriter);
             ConstantInt* AndRHS = dyn_cast<ConstantInt>(UInst2->getOperand(1));
             APInt APnewAddRHS = APInt(AddRHS->getValue()).zext(newBitSize);
             if (newBitSize > truncSize)
index ae41e3a8b7c5bb3256ccc85c210cfd92b030dc0b..0a14c3e787d1eeabbfdc10216b52ede74fa7ad0f 100644 (file)
@@ -1,5 +1,6 @@
 ; RUN: llvm-as < %s | opt -indvars -disable-output
 ; PR4009
+; PR4038
 
 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"
 target triple = "i386-pc-linux-gnu"
@@ -21,4 +22,27 @@ loop:                ; preds = %loop, %if.else
        br label %loop
 }
 
+define void @safe_bcopy_4038(i8* %from, i8* %to, i32 %size) nounwind {
+entry:
+       br i1 false, label %if.else, label %if.then12
+
+if.then12:             ; preds = %entry
+       ret void
+
+if.else:               ; preds = %entry
+       %sub.ptr.rhs.cast40 = ptrtoint i8* %from to i32         ; <i32> [#uses=1]
+       br label %if.end54
+
+if.end54:              ; preds = %if.end54, %if.else
+       %sub.ptr4912.pn = phi i8* [ %sub.ptr4912, %if.end54 ], [ null, %if.else ]               ; <i8*> [#uses=1]
+       %sub.ptr7 = phi i8* [ %sub.ptr, %if.end54 ], [ null, %if.else ]         ; <i8*> [#uses=2]
+       %sub.ptr.rhs.cast46.pn = ptrtoint i8* %from to i32              ; <i32> [#uses=1]
+       %sub.ptr.lhs.cast45.pn = ptrtoint i8* %to to i32                ; <i32> [#uses=1]
+       %sub.ptr.sub47.pn = sub i32 %sub.ptr.rhs.cast46.pn, %sub.ptr.lhs.cast45.pn              ; <i32> [#uses=1]
+       %sub.ptr4912 = getelementptr i8* %sub.ptr4912.pn, i32 %sub.ptr.sub47.pn         ; <i8*> [#uses=2]
+       tail call void @bcopy(i8* %sub.ptr4912, i8* %sub.ptr7, i32 0) nounwind
+       %sub.ptr = getelementptr i8* %sub.ptr7, i32 %sub.ptr.rhs.cast40         ; <i8*> [#uses=1]
+       br label %if.end54
+}
+
 declare void @bcopy(i8* nocapture) nounwind