llvm.org GIT mirror llvm / edf1700
Fix bug 22641 The bug was a result of getPreStartForExtend interpreting nsw/nuw flags on an add recurrence more strongly than is legal. {S,+,X}<nsw> implies S+X is nsw only if the backedge of the loop is taken at least once. NOTE: I had accidentally committed an unrelated change with the commit message of this change in r230275 (r230275 was reverted in r230279). This is the correct change for this commit message. Differential Revision: http://reviews.llvm.org/D7808 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230291 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 5 years ago
4 changed file(s) with 37 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
12731273 const SCEVAddRecExpr *PreAR = dyn_cast(
12741274 SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap));
12751275
1276 // WARNING: FIXME: the optimization below assumes that a sign/zero-overflowing
1277 // nsw/nuw operation is undefined behavior. This is strictly more aggressive
1278 // than the interpretation of nsw in other parts of LLVM (for instance, they
1279 // may unconditionally hoist nsw/nuw arithmetic through control flow). This
1280 // logic needs to be revisited once we have a consistent semantics for poison
1281 // values.
1276 // "{S,+,X} is /" and "the backedge is taken at least once" implies
1277 // "S+X does not sign/unsign-overflow".
12821278 //
1283 // "{S,+,X} is /" and "{S,+,X} is evaluated at least once" implies
1284 // "S+X does not sign/unsign-overflow" (we'd have undefined behavior if it
1285 // did). If `L->getExitingBlock() == L->getLoopLatch()` then `PreAR` (=
1286 // {S,+,X}/) is evaluated every-time `AR` (= {S+X,+,X}) is
1287 // evaluated, and hence within `AR` we are safe to assume that "S+X" will not
1288 // sign/unsign-overflow.
1289 //
1290
1291 BasicBlock *ExitingBlock = L->getExitingBlock();
1292 BasicBlock *LatchBlock = L->getLoopLatch();
1293 if (PreAR && PreAR->getNoWrapFlags(WrapType) && ExitingBlock != nullptr &&
1294 ExitingBlock == LatchBlock)
1279
1280 const SCEV *BECount = SE->getBackedgeTakenCount(L);
1281 if (PreAR && PreAR->getNoWrapFlags(WrapType) &&
1282 !isa(BECount) && SE->isKnownPositive(BECount))
12951283 return PreStart;
12961284
12971285 // 2. Direct overflow check on the step operation's expression.
55 br label %loop
66
77 loop:
8 %counter = phi i32 [ 0, %entry ], [ %counter.inc, %loop ]
89 %idx = phi i32 [ %start, %entry ], [ %idx.inc, %loop ]
910 %idx.inc = add nsw i32 %idx, 1
1011 %idx.inc.sext = sext i32 %idx.inc to i64
1112 ; CHECK: %idx.inc.sext = sext i32 %idx.inc to i64
1213 ; CHECK-NEXT: --> {(1 + (sext i32 %start to i64)),+,1}<%loop>
13 %condition = load volatile i1* %c
14 %condition = icmp eq i32 %counter, 1
15 %counter.inc = add i32 %counter, 1
1416 br i1 %condition, label %exit, label %loop
1517
1618 exit:
2325 br label %loop
2426
2527 loop:
28 %counter = phi i32 [ 0, %entry ], [ %counter.inc, %loop ]
2629 %idx = phi i32 [ %start, %entry ], [ %idx.inc, %loop ]
2730 %idx.inc = add nuw i32 %idx, 1
2831 %idx.inc.sext = zext i32 %idx.inc to i64
2932 ; CHECK: %idx.inc.sext = zext i32 %idx.inc to i64
3033 ; CHECK-NEXT: --> {(1 + (zext i32 %start to i64)),+,1}<%loop>
31 %condition = load volatile i1* %c
34 %condition = icmp eq i32 %counter, 1
35 %counter.inc = add i32 %counter, 1
3236 br i1 %condition, label %exit, label %loop
3337
3438 exit:
0 ; RUN: opt -analyze -scalar-evolution < %s | FileCheck %s
1
2 define i1 @main(i16 %a) {
3 ; CHECK-LABEL: Classifying expressions for: @main
4 entry:
5 br label %body
6
7 body:
8 %dec2 = phi i16 [ %a, %entry ], [ %dec, %cond ]
9 %dec = add i16 %dec2, -1
10 %conv2 = zext i16 %dec2 to i32
11 %conv = zext i16 %dec to i32
12 ; CHECK: %conv = zext i16 %dec to i32
13 ; CHECK-NEXT: --> {(zext i16 (-1 + %a) to i32),+,65535}<%body>
14 ; CHECK-NOT: --> {(65535 + (zext i16 %a to i32)),+,65535}<%body>
15
16 br label %cond
17
18 cond:
19 br i1 false, label %body, label %exit
20
21 exit:
22 %ret = icmp ne i32 %conv, 0
23 ret i1 %ret
24 }
7979 early.exit:
8080 ret i64 %postinc.sext
8181 }
82
83
84 ; WARNING: FIXME: it is safe to make the inference demonstrated here
85 ; only if we assume `add nsw` has undefined behavior if the result
86 ; sign-overflows; and this interpretation is stronger than what most
87 ; of LLVM assumes. This test here only serves as a documentation of
88 ; current behavior and will need to be revisited once we've decided
89 ; upon a consistent semantics for nsw (and nuw) arithetic operations.
90 ;
91 define i64 @good(i32 %start, i32 %low.limit, i32 %high.limit) {
92 ; CHECK-LABEL: Classifying expressions for: @good
93 entry:
94 %postinc.start = add i32 %start, 1
95 br label %loop
96
97 loop:
98 %idx = phi i32 [ %start, %entry ], [ %idx.inc, %loop ]
99 %postinc = phi i32 [ %postinc.start, %entry ], [ %postinc.inc, %loop ]
100 %postinc.inc = add nsw i32 %postinc, 1
101 %postinc.sext = sext i32 %postinc to i64
102 ; CHECK: %postinc.sext = sext i32 %postinc to i64
103 ; CHECK-NEXT: --> {(1 + (sext i32 %start to i64)),+,1}<%loop>
104
105 %break.early = icmp slt i32 %postinc, %low.limit
106 %idx.inc = add nsw i32 %idx, 1
107 %cmp = icmp slt i32 %idx.inc, %high.limit
108 br i1 %cmp, label %loop, label %exit
109
110 exit:
111 ret i64 0
112
113 early.exit:
114 ret i64 %postinc.sext
115 }