1 ; RUN: opt -analyze -scalar-evolution < %s | FileCheck %s
3 ; An example run where SCEV(%postinc)->getStart() may overflow:
6 ; %low.limit = INT_SMIN
7 ; %high.limit = < not used >
10 ; %postinc.start = INT_SMIN
15 ; %postinc.inc = INT_SMIN + 1
16 ; %postinc.sext = sext(INT_SMIN) = i64 INT32_SMIN
17 ; %break.early = INT_SMIN `slt` INT_SMIN = false
18 ; br i1 false, ___, %early.exit
24 define i64 @bad.0(i32 %start, i32 %low.limit, i32 %high.limit) {
25 ; CHECK-LABEL: Classifying expressions for: @bad.0
27 %postinc.start = add i32 %start, 1
31 %idx = phi i32 [ %start, %entry ], [ %idx.inc, %continue ]
32 %postinc = phi i32 [ %postinc.start, %entry ], [ %postinc.inc, %continue ]
33 %postinc.inc = add nsw i32 %postinc, 1
34 %postinc.sext = sext i32 %postinc to i64
35 ; CHECK: %postinc.sext = sext i32 %postinc to i64
36 ; CHECK-NEXT: --> {(sext i32 (1 + %start) to i64),+,1}<nsw><%loop>
37 %break.early = icmp slt i32 %postinc, %low.limit
38 br i1 %break.early, label %continue, label %early.exit
41 %idx.inc = add nsw i32 %idx, 1
42 %cmp = icmp slt i32 %idx.inc, %high.limit
43 br i1 %cmp, label %loop, label %exit
52 define i64 @bad.1(i32 %start, i32 %low.limit, i32 %high.limit, i1* %unknown) {
53 ; CHECK-LABEL: Classifying expressions for: @bad.1
55 %postinc.start = add i32 %start, 1
59 %idx = phi i32 [ %start, %entry ], [ %idx.inc, %continue ], [ %idx.inc, %continue.1 ]
60 %postinc = phi i32 [ %postinc.start, %entry ], [ %postinc.inc, %continue ], [ %postinc.inc, %continue.1 ]
61 %postinc.inc = add nsw i32 %postinc, 1
62 %postinc.sext = sext i32 %postinc to i64
63 ; CHECK: %postinc.sext = sext i32 %postinc to i64
64 ; CHECK-NEXT: --> {(sext i32 (1 + %start) to i64),+,1}<nsw><%loop>
65 %break.early = icmp slt i32 %postinc, %low.limit
66 br i1 %break.early, label %continue.1, label %early.exit
69 %cond = load volatile i1* %unknown
70 %idx.inc = add nsw i32 %idx, 1
71 br i1 %cond, label %loop, label %continue
74 %cmp = icmp slt i32 %idx.inc, %high.limit
75 br i1 %cmp, label %loop, label %exit
85 ; WARNING: FIXME: it is safe to make the inference demonstrated here
86 ; only if we assume `add nsw` has undefined behavior if the result
87 ; sign-overflows; and this interpretation is stronger than what most
88 ; of LLVM assumes. This test here only serves as a documentation of
89 ; current behavior and will need to be revisited once we've decided
90 ; upon a consistent semantics for nsw (and nuw) arithetic operations.
92 define i64 @good(i32 %start, i32 %low.limit, i32 %high.limit) {
93 ; CHECK-LABEL: Classifying expressions for: @good
95 %postinc.start = add i32 %start, 1
99 %idx = phi i32 [ %start, %entry ], [ %idx.inc, %loop ]
100 %postinc = phi i32 [ %postinc.start, %entry ], [ %postinc.inc, %loop ]
101 %postinc.inc = add nsw i32 %postinc, 1
102 %postinc.sext = sext i32 %postinc to i64
103 ; CHECK: %postinc.sext = sext i32 %postinc to i64
104 ; CHECK-NEXT: --> {(1 + (sext i32 %start to i64)),+,1}<nsw><%loop>
106 %break.early = icmp slt i32 %postinc, %low.limit
107 %idx.inc = add nsw i32 %idx, 1
108 %cmp = icmp slt i32 %idx.inc, %high.limit
109 br i1 %cmp, label %loop, label %exit
115 ret i64 %postinc.sext