Back out the change in 64918 that used sign-extensions when promoting
authorDan Gohman <gohman@apple.com>
Mon, 23 Feb 2009 23:20:35 +0000 (23:20 +0000)
committerDan Gohman <gohman@apple.com>
Mon, 23 Feb 2009 23:20:35 +0000 (23:20 +0000)
trip counts that use signed comparisons. It's not obviously the best
approach for preserving trip count information, and at any rate there
isn't anything in the tree right now that makes use of that, so for
now always using zero-extensions is preferable.

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

lib/Transforms/Scalar/IndVarSimplify.cpp
test/Transforms/IndVarSimplify/preserve-signed-wrap.ll
test/Transforms/IndVarSimplify/promote-iv-to-eliminate-casts.ll
test/Transforms/IndVarSimplify/signed-trip-count.ll

index 1207dbf8e341e355021be5c3c326e9e198dd9f66..6b52ed7b84da363ef20ee2ac614c01fcf455d2ca 100644 (file)
@@ -97,8 +97,7 @@ namespace {
                                    Value *IndVar,
                                    BasicBlock *ExitingBlock,
                                    BranchInst *BI,
-                                   SCEVExpander &Rewriter,
-                                   bool SignExtendTripCount);
+                                   SCEVExpander &Rewriter);
     void RewriteLoopExitValues(Loop *L, SCEV *IterationCount);
 
     void DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts);
@@ -237,8 +236,7 @@ void IndVarSimplify::LinearFunctionTestReplace(Loop *L,
                                    Value *IndVar,
                                    BasicBlock *ExitingBlock,
                                    BranchInst *BI,
-                                   SCEVExpander &Rewriter,
-                                   bool SignExtendTripCount) {
+                                   SCEVExpander &Rewriter) {
   // If the exiting block is not the same as the backedge block, we must compare
   // against the preincremented value, otherwise we prefer to compare against
   // the post-incremented value.
@@ -256,18 +254,11 @@ void IndVarSimplify::LinearFunctionTestReplace(Loop *L,
     if ((isa<SCEVConstant>(N) && !N->isZero()) ||
         SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
       // No overflow. Cast the sum.
-      if (SignExtendTripCount)
-        IterationCount = SE->getTruncateOrSignExtend(N, IndVar->getType());
-      else
-        IterationCount = SE->getTruncateOrZeroExtend(N, IndVar->getType());
+      IterationCount = SE->getTruncateOrZeroExtend(N, IndVar->getType());
     } else {
       // Potential overflow. Cast before doing the add.
-      if (SignExtendTripCount)
-        IterationCount = SE->getTruncateOrSignExtend(IterationCount,
-                                                     IndVar->getType());
-      else
-        IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
-                                                     IndVar->getType());
+      IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
+                                                   IndVar->getType());
       IterationCount =
         SE->getAddExpr(IterationCount,
                        SE->getIntegerSCEV(1, IndVar->getType()));
@@ -279,12 +270,8 @@ void IndVarSimplify::LinearFunctionTestReplace(Loop *L,
     CmpIndVar = L->getCanonicalInductionVariableIncrement();
   } else {
     // We have to use the preincremented value...
-    if (SignExtendTripCount)
-      IterationCount = SE->getTruncateOrSignExtend(IterationCount,
-                                                   IndVar->getType());
-    else
-      IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
-                                                   IndVar->getType());
+    IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
+                                                 IndVar->getType());
     CmpIndVar = IndVar;
   }
 
@@ -482,9 +469,8 @@ static const Type *getEffectiveIndvarType(const PHINode *Phi) {
 /// whether an induction variable in the same type that starts
 /// at 0 would undergo signed overflow.
 ///
-/// In addition to setting the NoSignedWrap, NoUnsignedWrap, and
-/// SignExtendTripCount variables, return the PHI for this induction
-/// variable.
+/// In addition to setting the NoSignedWrap, and NoUnsignedWrap,
+/// variables, return the PHI for this induction variable.
 ///
 /// TODO: This duplicates a fair amount of ScalarEvolution logic.
 /// Perhaps this can be merged with ScalarEvolution::getIterationCount
@@ -494,8 +480,7 @@ static const PHINode *TestOrigIVForWrap(const Loop *L,
                                         const BranchInst *BI,
                                         const Instruction *OrigCond,
                                         bool &NoSignedWrap,
-                                        bool &NoUnsignedWrap,
-                                        bool &SignExtendTripCount) {
+                                        bool &NoUnsignedWrap) {
   // Verify that the loop is sane and find the exit condition.
   const ICmpInst *Cmp = dyn_cast<ICmpInst>(OrigCond);
   if (!Cmp) return 0;
@@ -610,10 +595,6 @@ static const PHINode *TestOrigIVForWrap(const Loop *L,
   // less than some value in the same type. As such, it will never wrap.
   if (isSigned && !InitialVal->getValue().isMaxSignedValue()) {
     NoSignedWrap = true;
-    // If the original induction variable starts at zero or greater,
-    // the trip count can be considered signed.
-    if (InitialVal->getValue().isNonNegative())
-      SignExtendTripCount = true;
   } else if (!isSigned && !InitialVal->getValue().isMaxValue())
     NoUnsignedWrap = true;
   return PN;
@@ -700,7 +681,6 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
   // using it.  We can currently only handle loops with a single exit.
   bool NoSignedWrap = false;
   bool NoUnsignedWrap = false;
-  bool SignExtendTripCount = false;
   const PHINode *OrigControllingPHI = 0;
   if (!isa<SCEVCouldNotCompute>(IterationCount) && ExitingBlock)
     // Can't rewrite non-branch yet.
@@ -709,16 +689,14 @@ bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
         // Determine if the OrigIV will ever undergo overflow.
         OrigControllingPHI =
           TestOrigIVForWrap(L, BI, OrigCond,
-                            NoSignedWrap, NoUnsignedWrap,
-                            SignExtendTripCount);
+                            NoSignedWrap, NoUnsignedWrap);
 
         // We'll be replacing the original condition, so it'll be dead.
         DeadInsts.insert(OrigCond);
       }
 
       LinearFunctionTestReplace(L, IterationCount, IndVar,
-                                ExitingBlock, BI, Rewriter,
-                                SignExtendTripCount);
+                                ExitingBlock, BI, Rewriter);
     }
 
   // Now that we have a canonical induction variable, we can rewrite any
index 930721a85b480482bf1743320335bcf3a2d7fde8..0a91ec88064f4a67fae3063d3a67ac4f12151b10 100644 (file)
@@ -1,6 +1,5 @@
 ; RUN: llvm-as < %s | opt -indvars | llvm-dis > %t
-; RUN: grep sext %t | count 2
-; RUN: grep { = sext i32 %n to i64} %t
+; RUN: grep sext %t | count 1
 ; RUN: grep phi %t | count 1
 ; RUN: grep {phi i64} %t
 
index 9588bd30f2f628a0a47d4b27900dedcba24e1993..48c060c48d22138a1b6e6a1ca93788d5ebbfd311 100644 (file)
@@ -1,7 +1,5 @@
 ; RUN: llvm-as < %s | opt -indvars | llvm-dis > %t
-; RUN: grep sext %t | count 2
-; RUN: grep { = sext i16 %N to i64} %t
-; RUN: grep { = sext i32 %count to i64} %t
+; RUN: not grep sext %t
 
 define i64 @test(i64* nocapture %first, i32 %count) nounwind readonly {
 entry:
index cea9f8224551e78ccf1d72300bc4458e3090d93d..cd61d11394eebeae852276ab0a614c25e3d8c214 100644 (file)
@@ -1,7 +1,6 @@
 ; RUN: llvm-as < %s | opt -indvars | llvm-dis > %t
-; RUN: grep { = sext i32 %n} %t
+; RUN: not grep sext %t
 ; RUN: grep phi %t | count 1
-; RUN: not grep zext %t
 
 define void @foo(i64* nocapture %x, i32 %n) nounwind {
 entry: