llvm.org GIT mirror llvm / c54c561
Add new SCEV, SCEVSMax. This allows LLVM to analyze do-while loops. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44319 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 12 years ago
8 changed file(s) with 204 addition(s) and 85 deletion(s). Raw diff Collapse all Expand all
234234 std::vector NewOp(Operands);
235235 return getAddRecExpr(NewOp, L);
236236 }
237 SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
238 SCEVHandle getSMaxExpr(std::vector Operands);
237239 SCEVHandle getUnknown(Value *V);
238240
239241 /// getNegativeSCEV - Return the SCEV object corresponding to -V.
133133
134134 Value *visitAddRecExpr(SCEVAddRecExpr *S);
135135
136 Value *visitSMaxExpr(SCEVSMaxExpr *S);
137
136138 Value *visitUnknown(SCEVUnknown *S) {
137139 return S->getValue();
138140 }
2424 // These should be ordered in terms of increasing complexity to make the
2525 // folders simpler.
2626 scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
27 scSDivExpr, scAddRecExpr, scUnknown, scCouldNotCompute
27 scSDivExpr, scAddRecExpr, scSMaxExpr, scUnknown, scCouldNotCompute
2828 };
2929
3030 //===--------------------------------------------------------------------===//
273273 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
274274 static inline bool classof(const SCEV *S) {
275275 return S->getSCEVType() == scAddExpr ||
276 S->getSCEVType() == scMulExpr;
276 S->getSCEVType() == scMulExpr ||
277 S->getSCEVType() == scSMaxExpr;
277278 }
278279 };
279280
457458 return S->getSCEVType() == scAddRecExpr;
458459 }
459460 };
461
462
463 //===--------------------------------------------------------------------===//
464 /// SCEVSMaxExpr - This class represents a signed maximum selection.
465 ///
466 class SCEVSMaxExpr : public SCEVCommutativeExpr {
467 friend class ScalarEvolution;
468
469 explicit SCEVSMaxExpr(const std::vector &ops)
470 : SCEVCommutativeExpr(scSMaxExpr, ops) {
471 }
472
473 public:
474 virtual const char *getOperationStr() const { return " smax "; }
475
476 /// Methods for support type inquiry through isa, cast, and dyn_cast:
477 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
478 static inline bool classof(const SCEV *S) {
479 return S->getSCEVType() == scSMaxExpr;
480 }
481 };
482
460483
461484 //===--------------------------------------------------------------------===//
462485 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
520543 return ((SC*)this)->visitSDivExpr((SCEVSDivExpr*)S);
521544 case scAddRecExpr:
522545 return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
546 case scSMaxExpr:
547 return ((SC*)this)->visitSMaxExpr((SCEVSMaxExpr*)S);
523548 case scUnknown:
524549 return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
525550 case scCouldNotCompute:
317317 return SE.getAddExpr(NewOps);
318318 else if (isa(this))
319319 return SE.getMulExpr(NewOps);
320 else if (isa(this))
321 return SE.getSMaxExpr(NewOps);
320322 else
321323 assert(0 && "Unknown commutative expr!");
322324 }
10941096 return Result;
10951097 }
10961098
1099 SCEVHandle ScalarEvolution::getSMaxExpr(const SCEVHandle &LHS,
1100 const SCEVHandle &RHS) {
1101 std::vector Ops;
1102 Ops.push_back(LHS);
1103 Ops.push_back(RHS);
1104 return getSMaxExpr(Ops);
1105 }
1106
1107 SCEVHandle ScalarEvolution::getSMaxExpr(std::vector Ops) {
1108 assert(!Ops.empty() && "Cannot get empty smax!");
1109 if (Ops.size() == 1) return Ops[0];
1110
1111 // Sort by complexity, this groups all similar expression types together.
1112 GroupByComplexity(Ops);
1113
1114 // If there are any constants, fold them together.
1115 unsigned Idx = 0;
1116 if (SCEVConstant *LHSC = dyn_cast(Ops[0])) {
1117 ++Idx;
1118 assert(Idx < Ops.size());
1119 while (SCEVConstant *RHSC = dyn_cast(Ops[Idx])) {
1120 // We found two constants, fold them together!
1121 Constant *Fold = ConstantInt::get(
1122 APIntOps::smax(LHSC->getValue()->getValue(),
1123 RHSC->getValue()->getValue()));
1124 if (ConstantInt *CI = dyn_cast(Fold)) {
1125 Ops[0] = getConstant(CI);
1126 Ops.erase(Ops.begin()+1); // Erase the folded element
1127 if (Ops.size() == 1) return Ops[0];
1128 LHSC = cast(Ops[0]);
1129 } else {
1130 // If we couldn't fold the expression, move to the next constant. Note
1131 // that this is impossible to happen in practice because we always
1132 // constant fold constant ints to constant ints.
1133 ++Idx;
1134 }
1135 }
1136
1137 // If we are left with a constant -inf, strip it off.
1138 if (cast(Ops[0])->getValue()->isMinValue(true)) {
1139 Ops.erase(Ops.begin());
1140 --Idx;
1141 }
1142 }
1143
1144 if (Ops.size() == 1) return Ops[0];
1145
1146 // Find the first SMax
1147 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
1148 ++Idx;
1149
1150 // Check to see if one of the operands is an SMax. If so, expand its operands
1151 // onto our operand list, and recurse to simplify.
1152 if (Idx < Ops.size()) {
1153 bool DeletedSMax = false;
1154 while (SCEVSMaxExpr *SMax = dyn_cast(Ops[Idx])) {
1155 Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end());
1156 Ops.erase(Ops.begin()+Idx);
1157 DeletedSMax = true;
1158 }
1159
1160 if (DeletedSMax)
1161 return getSMaxExpr(Ops);
1162 }
1163
1164 // Okay, check to see if the same value occurs in the operand list twice. If
1165 // so, delete one. Since we sorted the list, these values are required to
1166 // be adjacent.
1167 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1168 if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y
1169 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
1170 --i; --e;
1171 }
1172
1173 if (Ops.size() == 1) return Ops[0];
1174
1175 assert(!Ops.empty() && "Reduced smax down to nothing!");
1176
1177 // Okay, it looks like we really DO need an add expr. Check to see if we
1178 // already have one, otherwise create a new one.
1179 std::vector SCEVOps(Ops.begin(), Ops.end());
1180 SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scSMaxExpr,
1181 SCEVOps)];
1182 if (Result == 0) Result = new SCEVSMaxExpr(Ops);
1183 return Result;
1184 }
1185
10971186 SCEVHandle ScalarEvolution::getUnknown(Value *V) {
10981187 if (ConstantInt *CI = dyn_cast(V))
10991188 return getConstant(CI);
14541543 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
14551544 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
14561545 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
1546 return MinOpRes;
1547 }
1548
1549 if (SCEVSMaxExpr *M = dyn_cast(S)) {
1550 // The result is the min of all operands results.
1551 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
1552 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
1553 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
14571554 return MinOpRes;
14581555 }
14591556
15361633 case Instruction::PHI:
15371634 return createNodeForPHI(cast(I));
15381635
1636 case Instruction::Select:
1637 // This could be an SCEVSMax that was lowered earlier. Try to recover it.
1638 if (ICmpInst *ICI = dyn_cast(I->getOperand(0))) {
1639 Value *LHS = ICI->getOperand(0);
1640 Value *RHS = ICI->getOperand(1);
1641 switch (ICI->getPredicate()) {
1642 case ICmpInst::ICMP_SLT:
1643 case ICmpInst::ICMP_SLE:
1644 std::swap(LHS, RHS);
1645 // fall through
1646 case ICmpInst::ICMP_SGT:
1647 case ICmpInst::ICMP_SGE:
1648 if (LHS == I->getOperand(1) && RHS == I->getOperand(2))
1649 return SE.getSMaxExpr(getSCEV(LHS), getSCEV(RHS));
1650 default:
1651 break;
1652 }
1653 }
1654
15391655 default: // We cannot analyze this expression.
15401656 break;
15411657 }
21242240 }
21252241 if (isa(Comm))
21262242 return SE.getAddExpr(NewOps);
2127 assert(isa(Comm) && "Only know about add and mul!");
2128 return SE.getMulExpr(NewOps);
2243 if (isa(Comm))
2244 return SE.getMulExpr(NewOps);
2245 if (isa(Comm))
2246 return SE.getSMaxExpr(NewOps);
2247 assert(0 && "Unknown commutative SCEV type!");
21292248 }
21302249 }
21312250 // If we got here, all operands are loop invariant.
23422461 return UnknownValue;
23432462
23442463 if (AddRec->isAffine()) {
2464 // The number of iterations for "{n,+,1} < m", is m-n. However, we don't
2465 // know that m is >= n on input to the loop. If it is, the condition
2466 // returns true zero times. To handle both cases, we return SMAX(0, m-n).
2467
23452468 // FORNOW: We only support unit strides.
2346 SCEVHandle Zero = SE.getIntegerSCEV(0, RHS->getType());
23472469 SCEVHandle One = SE.getIntegerSCEV(1, RHS->getType());
23482470 if (AddRec->getOperand(1) != One)
23492471 return UnknownValue;
23502472
2351 // The number of iterations for "{n,+,1} < m", is m-n. However, we don't
2352 // know that m is >= n on input to the loop. If it is, the condition return
2353 // true zero times. What we really should return, for full generality, is
2354 // SMAX(0, m-n). Since we cannot check this, we will instead check for a
2355 // canonical loop form: most do-loops will have a check that dominates the
2356 // loop, that only enters the loop if (n-1)
2357 // we know that the SMAX will evaluate to m-n, because we know that m >= n.
2358
2359 // Search for the check.
2360 BasicBlock *Preheader = L->getLoopPreheader();
2361 BasicBlock *PreheaderDest = L->getHeader();
2362 if (Preheader == 0) return UnknownValue;
2363
2364 BranchInst *LoopEntryPredicate =
2365 dyn_cast(Preheader->getTerminator());
2366 if (!LoopEntryPredicate) return UnknownValue;
2367
2368 // This might be a critical edge broken out. If the loop preheader ends in
2369 // an unconditional branch to the loop, check to see if the preheader has a
2370 // single predecessor, and if so, look for its terminator.
2371 while (LoopEntryPredicate->isUnconditional()) {
2372 PreheaderDest = Preheader;
2373 Preheader = Preheader->getSinglePredecessor();
2374 if (!Preheader) return UnknownValue; // Multiple preds.
2375
2376 LoopEntryPredicate =
2377 dyn_cast(Preheader->getTerminator());
2378 if (!LoopEntryPredicate) return UnknownValue;
2379 }
2380
2381 // Now that we found a conditional branch that dominates the loop, check to
2382 // see if it is the comparison we are looking for.
2383 if (ICmpInst *ICI = dyn_cast(LoopEntryPredicate->getCondition())){
2384 Value *PreCondLHS = ICI->getOperand(0);
2385 Value *PreCondRHS = ICI->getOperand(1);
2386 ICmpInst::Predicate Cond;
2387 if (LoopEntryPredicate->getSuccessor(0) == PreheaderDest)
2388 Cond = ICI->getPredicate();
2389 else
2390 Cond = ICI->getInversePredicate();
2391
2392 switch (Cond) {
2393 case ICmpInst::ICMP_UGT:
2394 if (isSigned) return UnknownValue;
2395 std::swap(PreCondLHS, PreCondRHS);
2396 Cond = ICmpInst::ICMP_ULT;
2397 break;
2398 case ICmpInst::ICMP_SGT:
2399 if (!isSigned) return UnknownValue;
2400 std::swap(PreCondLHS, PreCondRHS);
2401 Cond = ICmpInst::ICMP_SLT;
2402 break;
2403 case ICmpInst::ICMP_ULT:
2404 if (isSigned) return UnknownValue;
2405 break;
2406 case ICmpInst::ICMP_SLT:
2407 if (!isSigned) return UnknownValue;
2408 break;
2409 default:
2410 return UnknownValue;
2411 }
2412
2413 if (PreCondLHS->getType()->isInteger()) {
2414 if (RHS != getSCEV(PreCondRHS))
2415 return UnknownValue; // Not a comparison against 'm'.
2416
2417 if (SE.getMinusSCEV(AddRec->getOperand(0), One)
2418 != getSCEV(PreCondLHS))
2419 return UnknownValue; // Not a comparison against 'n-1'.
2420 }
2421 else return UnknownValue;
2422
2423 // cerr << "Computed Loop Trip Count as: "
2424 // << // *SE.getMinusSCEV(RHS, AddRec->getOperand(0)) << "\n";
2425 return SE.getMinusSCEV(RHS, AddRec->getOperand(0));
2426 }
2427 else
2428 return UnknownValue;
2473 SCEVHandle Iters = SE.getMinusSCEV(RHS, AddRec->getOperand(0));
2474
2475 if (isSigned)
2476 return SE.getSMaxExpr(SE.getIntegerSCEV(0, RHS->getType()), Iters);
2477 else
2478 return Iters;
24292479 }
24302480
24312481 return UnknownValue;
207207 return expand(V);
208208 }
209209
210 Value *SCEVExpander::visitSMaxExpr(SCEVSMaxExpr *S) {
211 Value *LHS = expand(S->getOperand(0));
212 for (unsigned i = 1; i < S->getNumOperands(); ++i) {
213 Value *RHS = expand(S->getOperand(i));
214 Value *ICmp = new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt);
215 LHS = new SelectInst(ICmp, LHS, RHS, "smax", InsertPt);
216 }
217 return LHS;
218 }
219
210220 Value *SCEVExpander::expand(SCEV *S) {
211221 // Check to see if we already expanded this.
212222 std::map::iterator I = InsertedExpressions.find(S);
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep smax
1 ; PR1614
2
3 define i32 @f(i32 %x, i32 %y) {
4 entry:
5 br label %bb
6
7 bb: ; preds = %bb, %entry
8 %indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb ] ; [#uses=2]
9 %x_addr.0 = add i32 %indvar, %x ; [#uses=1]
10 %tmp2 = add i32 %x_addr.0, 1 ; [#uses=2]
11 %tmp5 = icmp slt i32 %tmp2, %y ; [#uses=1]
12 %indvar.next = add i32 %indvar, 1 ; [#uses=1]
13 br i1 %tmp5, label %bb, label %bb7
14
15 bb7: ; preds = %bb
16 ret i32 %tmp2
17 }
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep smax | count 2
1 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep \
2 ; RUN: "%. smax %. smax %."
3 ; PR1614
4
5 define i32 @x(i32 %a, i32 %b, i32 %c) {
6 %A = icmp sgt i32 %a, %b
7 %B = select i1 %A, i32 %a, i32 %b
8 %C = icmp sle i32 %c, %B
9 %D = select i1 %C, i32 %B, i32 %c
10 ret i32 %D
11 }
None ; RUN: llvm-as < %s | opt -indvars -adce -simplifycfg | llvm-dis | grep select
1 ; RUN: llvm-as < %s | opt -indvars -adce -simplifycfg | llvm-dis | not grep br
0 ; RUN: llvm-as < %s | opt -indvars -adce -simplifycfg | opt \
1 ; RUN: -analyze -loops | not grep "^Loop Containing"
22 ; PR1179
33
44 define i32 @ltst(i32 %x) {