Index: lib/Analysis/ScalarEvolution.cpp |
diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp |
index f876748af3dc1cab39a166d3be2861cf815cb134..6ea915fdb0b7690cbb8efdc07aececde3e5efe42 100644 |
--- a/lib/Analysis/ScalarEvolution.cpp |
+++ b/lib/Analysis/ScalarEvolution.cpp |
@@ -3937,19 +3937,10 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) { |
/// before taking the branch. For loops with multiple exits, it may not be the |
/// number times that the loop header executes because the loop may exit |
/// prematurely via another branch. |
-/// |
-/// FIXME: We conservatively call getBackedgeTakenCount(L) instead of |
-/// getExitCount(L, ExitingBlock) to compute a safe trip count considering all |
-/// loop exits. getExitCount() may return an exact count for this branch |
-/// assuming no-signed-wrap. The number of well-defined iterations may actually |
-/// be higher than this trip count if this exit test is skipped and the loop |
-/// exits via a different branch. Ideally, getExitCount() would know whether it |
-/// depends on a NSW assumption, and we would only fall back to a conservative |
-/// trip count in that case. |
unsigned ScalarEvolution:: |
-getSmallConstantTripCount(Loop *L, BasicBlock */*ExitingBlock*/) { |
+getSmallConstantTripCount(Loop *L, BasicBlock *ExitingBlock) { |
const SCEVConstant *ExitCount = |
- dyn_cast<SCEVConstant>(getBackedgeTakenCount(L)); |
+ dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock)); |
if (!ExitCount) |
return 0; |
@@ -3976,8 +3967,8 @@ getSmallConstantTripCount(Loop *L, BasicBlock */*ExitingBlock*/) { |
/// As explained in the comments for getSmallConstantTripCount, this assumes |
/// that control exits the loop via ExitingBlock. |
unsigned ScalarEvolution:: |
-getSmallConstantTripMultiple(Loop *L, BasicBlock */*ExitingBlock*/) { |
- const SCEV *ExitCount = getBackedgeTakenCount(L); |
+getSmallConstantTripMultiple(Loop *L, BasicBlock *ExitingBlock) { |
+ const SCEV *ExitCount = getExitCount(L, ExitingBlock); |
if (ExitCount == getCouldNotCompute()) |
return 1; |
@@ -4006,7 +3997,7 @@ getSmallConstantTripMultiple(Loop *L, BasicBlock */*ExitingBlock*/) { |
} |
// getExitCount - Get the expression for the number of loop iterations for which |
-// this loop is guaranteed not to exit via ExitingBlock. Otherwise return |
+// this loop is guaranteed not to exit via ExitintBlock. Otherwise return |
// SCEVCouldNotCompute. |
const SCEV *ScalarEvolution::getExitCount(Loop *L, BasicBlock *ExitingBlock) { |
return getBackedgeTakenInfo(L).getExact(ExitingBlock, this); |
@@ -4391,36 +4382,26 @@ ScalarEvolution::ComputeExitLimit(const Loop *L, BasicBlock *ExitingBlock) { |
// Proceed to the next level to examine the exit condition expression. |
return ComputeExitLimitFromCond(L, ExitBr->getCondition(), |
ExitBr->getSuccessor(0), |
- ExitBr->getSuccessor(1), |
- /*IsSubExpr=*/false); |
+ ExitBr->getSuccessor(1)); |
} |
/// ComputeExitLimitFromCond - Compute the number of times the |
/// backedge of the specified loop will execute if its exit condition |
/// were a conditional branch of ExitCond, TBB, and FBB. |
-/// |
-/// @param IsSubExpr is true if ExitCond does not directly control the exit |
-/// branch. In this case, we cannot assume that the loop only exits when the |
-/// condition is true and cannot infer that failing to meet the condition prior |
-/// to integer wraparound results in undefined behavior. |
ScalarEvolution::ExitLimit |
ScalarEvolution::ComputeExitLimitFromCond(const Loop *L, |
Value *ExitCond, |
BasicBlock *TBB, |
- BasicBlock *FBB, |
- bool IsSubExpr) { |
+ BasicBlock *FBB) { |
// Check if the controlling expression for this loop is an And or Or. |
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
if (BO->getOpcode() == Instruction::And) { |
// Recurse on the operands of the and. |
- bool EitherMayExit = L->contains(TBB); |
- ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB, |
- IsSubExpr || EitherMayExit); |
- ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB, |
- IsSubExpr || EitherMayExit); |
+ ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB); |
+ ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB); |
const SCEV *BECount = getCouldNotCompute(); |
const SCEV *MaxBECount = getCouldNotCompute(); |
- if (EitherMayExit) { |
+ if (L->contains(TBB)) { |
// Both conditions must be true for the loop to continue executing. |
// Choose the less conservative count. |
if (EL0.Exact == getCouldNotCompute() || |
@@ -4448,14 +4429,11 @@ ScalarEvolution::ComputeExitLimitFromCond(const Loop *L, |
} |
if (BO->getOpcode() == Instruction::Or) { |
// Recurse on the operands of the or. |
- bool EitherMayExit = L->contains(FBB); |
- ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB, |
- IsSubExpr || EitherMayExit); |
- ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB, |
- IsSubExpr || EitherMayExit); |
+ ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB); |
+ ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB); |
const SCEV *BECount = getCouldNotCompute(); |
const SCEV *MaxBECount = getCouldNotCompute(); |
- if (EitherMayExit) { |
+ if (L->contains(FBB)) { |
// Both conditions must be false for the loop to continue executing. |
// Choose the less conservative count. |
if (EL0.Exact == getCouldNotCompute() || |
@@ -4486,7 +4464,7 @@ ScalarEvolution::ComputeExitLimitFromCond(const Loop *L, |
// With an icmp, it may be feasible to compute an exact backedge-taken count. |
// Proceed to the next level to examine the icmp. |
if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
- return ComputeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB, IsSubExpr); |
+ return ComputeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB); |
// Check for a constant condition. These are normally stripped out by |
// SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to |
@@ -4512,8 +4490,7 @@ ScalarEvolution::ExitLimit |
ScalarEvolution::ComputeExitLimitFromICmp(const Loop *L, |
ICmpInst *ExitCond, |
BasicBlock *TBB, |
- BasicBlock *FBB, |
- bool IsSubExpr) { |
+ BasicBlock *FBB) { |
// If the condition was exit on true, convert the condition to exit on false |
ICmpInst::Predicate Cond; |
@@ -4565,7 +4542,7 @@ ScalarEvolution::ComputeExitLimitFromICmp(const Loop *L, |
switch (Cond) { |
case ICmpInst::ICMP_NE: { // while (X != Y) |
// Convert to: while (X-Y != 0) |
- ExitLimit EL = HowFarToZero(getMinusSCEV(LHS, RHS), L, IsSubExpr); |
+ ExitLimit EL = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
if (EL.hasAnyInfo()) return EL; |
break; |
} |
@@ -4576,24 +4553,24 @@ ScalarEvolution::ComputeExitLimitFromICmp(const Loop *L, |
break; |
} |
case ICmpInst::ICMP_SLT: { |
- ExitLimit EL = HowManyLessThans(LHS, RHS, L, true, IsSubExpr); |
+ ExitLimit EL = HowManyLessThans(LHS, RHS, L, true); |
if (EL.hasAnyInfo()) return EL; |
break; |
} |
case ICmpInst::ICMP_SGT: { |
ExitLimit EL = HowManyLessThans(getNotSCEV(LHS), |
- getNotSCEV(RHS), L, true, IsSubExpr); |
+ getNotSCEV(RHS), L, true); |
if (EL.hasAnyInfo()) return EL; |
break; |
} |
case ICmpInst::ICMP_ULT: { |
- ExitLimit EL = HowManyLessThans(LHS, RHS, L, false, IsSubExpr); |
+ ExitLimit EL = HowManyLessThans(LHS, RHS, L, false); |
if (EL.hasAnyInfo()) return EL; |
break; |
} |
case ICmpInst::ICMP_UGT: { |
ExitLimit EL = HowManyLessThans(getNotSCEV(LHS), |
- getNotSCEV(RHS), L, false, IsSubExpr); |
+ getNotSCEV(RHS), L, false); |
if (EL.hasAnyInfo()) return EL; |
break; |
} |
@@ -5462,7 +5439,7 @@ SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
/// effectively V != 0. We know and take advantage of the fact that this |
/// expression only being used in a comparison by zero context. |
ScalarEvolution::ExitLimit |
-ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L, bool IsSubExpr) { |
+ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
// If the value is a constant |
if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
// If the value is already zero, the branch will execute zero times. |
@@ -5560,20 +5537,19 @@ ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L, bool IsSubExpr) { |
} |
// If the recurrence is known not to wraparound, unsigned divide computes the |
- // back edge count. (Ideally we would have an "isexact" bit for udiv). We know |
- // that the value will either become zero (and thus the loop terminates), that |
- // the loop will terminate through some other exit condition first, or that |
- // the loop has undefined behavior. This means we can't "miss" the exit |
- // value, even with nonunit stride. |
+ // back edge count. We know that the value will either become zero (and thus |
+ // the loop terminates), that the loop will terminate through some other exit |
+ // condition first, or that the loop has undefined behavior. This means |
+ // we can't "miss" the exit value, even with nonunit stride. |
// |
- // This is only valid for expressions that directly compute the loop exit. It |
- // is invalid for subexpressions in which the loop may exit through this |
- // branch even if this subexpression is false. In that case, the trip count |
- // computed by this udiv could be smaller than the number of well-defined |
- // iterations. |
- if (!IsSubExpr && AddRec->getNoWrapFlags(SCEV::FlagNW)) |
+ // FIXME: Prove that loops always exhibits *acceptable* undefined |
+ // behavior. Loops must exhibit defined behavior until a wrapped value is |
+ // actually used. So the trip count computed by udiv could be smaller than the |
+ // number of well-defined iterations. |
+ if (AddRec->getNoWrapFlags(SCEV::FlagNW)) { |
+ // FIXME: We really want an "isexact" bit for udiv. |
return getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step); |
- |
+ } |
// Then, try to solve the above equation provided that Start is constant. |
if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
@@ -6339,14 +6315,9 @@ const SCEV *ScalarEvolution::getBECount(const SCEV *Start, |
/// HowManyLessThans - Return the number of times a backedge containing the |
/// specified less-than comparison will execute. If not computable, return |
/// CouldNotCompute. |
-/// |
-/// @param IsSubExpr is true when the LHS < RHS condition does not directly |
-/// control the branch. In this case, we can only compute an iteration count for |
-/// a subexpression that cannot overflow before evaluating true. |
ScalarEvolution::ExitLimit |
ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
- const Loop *L, bool isSigned, |
- bool IsSubExpr) { |
+ const Loop *L, bool isSigned) { |
// Only handle: "ADDREC < LoopInvariant". |
if (!isLoopInvariant(RHS, L)) return getCouldNotCompute(); |
@@ -6355,12 +6326,10 @@ ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
return getCouldNotCompute(); |
// Check to see if we have a flag which makes analysis easy. |
- bool NoWrap = false; |
- if (!IsSubExpr) { |
- NoWrap = AddRec->getNoWrapFlags( |
- (SCEV::NoWrapFlags)(((isSigned ? SCEV::FlagNSW : SCEV::FlagNUW)) |
- | SCEV::FlagNW)); |
- } |
+ bool NoWrap = isSigned ? |
+ AddRec->getNoWrapFlags((SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNW)) : |
+ AddRec->getNoWrapFlags((SCEV::NoWrapFlags)(SCEV::FlagNUW | SCEV::FlagNW)); |
+ |
if (AddRec->isAffine()) { |
unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
const SCEV *Step = AddRec->getStepRecurrence(*this); |