Bugfix: ScalarEvolution incorrectly assumes that the start of certain
[oota-llvm.git] / test / Analysis / ScalarEvolution / scev-prestart-nowrap.ll
1 ; RUN: opt -analyze -scalar-evolution < %s | FileCheck %s
2
3 ; An example run where SCEV(%postinc)->getStart() may overflow:
4 ;
5 ; %start = INT_SMAX
6 ; %low.limit = INT_SMIN
7 ; %high.limit = < not used >
8 ;
9 ; >> entry:
10 ;  %postinc.start = INT_SMIN
11 ;
12 ; >> loop:
13 ;  %idx = %start 
14 ;  %postinc = 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
19 ;
20 ; >> early.exit:
21 ;  ret i64 INT32_SMIN
22
23
24 define i64 @bad.0(i32 %start, i32 %low.limit, i32 %high.limit) {
25 ; CHECK-LABEL: Classifying expressions for: @bad.0
26  entry:
27   %postinc.start = add i32 %start, 1
28   br label %loop
29
30  loop:
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
39
40  continue:
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
44
45  exit:
46   ret i64 0
47
48  early.exit:
49   ret i64 %postinc.sext
50 }
51
52 define i64 @bad.1(i32 %start, i32 %low.limit, i32 %high.limit, i1* %unknown) {
53 ; CHECK-LABEL: Classifying expressions for: @bad.1
54  entry:
55   %postinc.start = add i32 %start, 1
56   br label %loop
57
58  loop:
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
67
68  continue.1:
69   %cond = load volatile i1* %unknown
70   %idx.inc = add nsw i32 %idx, 1
71   br i1 %cond, label %loop, label %continue
72
73  continue:
74   %cmp = icmp slt i32 %idx.inc, %high.limit
75   br i1 %cmp, label %loop, label %exit
76
77  exit:
78   ret i64 0
79
80  early.exit:
81   ret i64 %postinc.sext
82 }
83
84
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.
91 ;
92 define i64 @good(i32 %start, i32 %low.limit, i32 %high.limit) {
93 ; CHECK-LABEL: Classifying expressions for: @good
94  entry:
95   %postinc.start = add i32 %start, 1
96   br label %loop
97
98  loop:
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>
105
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
110
111  exit:
112   ret i64 0
113
114  early.exit:
115   ret i64 %postinc.sext
116 }