From 15cab2817b8f63fec0148609278bc2c1e05abb50 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 23 Feb 2009 23:20:35 +0000 Subject: [PATCH] Back out the change in 64918 that used sign-extensions when promoting 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 | 46 +++++-------------- .../IndVarSimplify/preserve-signed-wrap.ll | 3 +- .../promote-iv-to-eliminate-casts.ll | 4 +- .../IndVarSimplify/signed-trip-count.ll | 3 +- 4 files changed, 15 insertions(+), 41 deletions(-) diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index 1207dbf8e34..6b52ed7b84d 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -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 &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(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(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(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 diff --git a/test/Transforms/IndVarSimplify/preserve-signed-wrap.ll b/test/Transforms/IndVarSimplify/preserve-signed-wrap.ll index 930721a85b4..0a91ec88064 100644 --- a/test/Transforms/IndVarSimplify/preserve-signed-wrap.ll +++ b/test/Transforms/IndVarSimplify/preserve-signed-wrap.ll @@ -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 diff --git a/test/Transforms/IndVarSimplify/promote-iv-to-eliminate-casts.ll b/test/Transforms/IndVarSimplify/promote-iv-to-eliminate-casts.ll index 9588bd30f2f..48c060c48d2 100644 --- a/test/Transforms/IndVarSimplify/promote-iv-to-eliminate-casts.ll +++ b/test/Transforms/IndVarSimplify/promote-iv-to-eliminate-casts.ll @@ -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: diff --git a/test/Transforms/IndVarSimplify/signed-trip-count.ll b/test/Transforms/IndVarSimplify/signed-trip-count.ll index cea9f822455..cd61d11394e 100644 --- a/test/Transforms/IndVarSimplify/signed-trip-count.ll +++ b/test/Transforms/IndVarSimplify/signed-trip-count.ll @@ -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: