llvm.org GIT mirror llvm / 7b5b768
Fix PR2002. Suppose n is the initial value for the induction variable (with step 1) and m is its final value. Then, the correct trip count is SMAX(m,n)-n. Previously, we used SMAX(0,m-n), but m-n may overflow and can't in general be interpreted as signed. Patch by Nick Lewycky. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47007 91177308-0d34-0410-b5e6-96231b3b80d8 Wojciech Matyjewicz 12 years ago
2 changed file(s) with 21 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
25262526 if (AddRec->isAffine()) {
25272527 // The number of iterations for "{n,+,1} < m", is m-n. However, we don't
25282528 // know that m is >= n on input to the loop. If it is, the condition
2529 // returns true zero times. To handle both cases, we return SMAX(0, m-n).
2529 // returns true zero times. To handle both cases, we return SMAX(m, n)-n.
25302530
25312531 // FORNOW: We only support unit strides.
25322532 SCEVHandle One = SE.getIntegerSCEV(1, RHS->getType());
25332533 if (AddRec->getOperand(1) != One)
25342534 return UnknownValue;
25352535
2536 SCEVHandle Iters = SE.getMinusSCEV(RHS, AddRec->getOperand(0));
2537
2538 if (isSigned)
2539 return SE.getSMaxExpr(SE.getIntegerSCEV(0, RHS->getType()), Iters);
2540 else
2541 return Iters;
2536 SCEVHandle Start = AddRec->getOperand(0);
2537 SCEVHandle End = isSigned ? SE.getSMaxExpr(RHS, Start) : (SCEVHandle)RHS;
2538
2539 return SE.getMinusSCEV(End, Start);
25422540 }
25432541
25442542 return UnknownValue;
0 ; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop loop: ( 100 + ( -100 smax %n)) iterations!}
1 ; PR2002
2
3 define void @foo(i8 %n) {
4 entry:
5 br label %loop
6 loop:
7 %i = phi i8 [ -100, %entry ], [ %i.inc, %next ]
8 %cond = icmp slt i8 %i, %n
9 br i1 %cond, label %next, label %return
10 next:
11 %i.inc = add i8 %i, 1
12 br label %loop
13 return:
14 ret void
15 }