abebea7d74c3db6053104baebcade99ce0db589f
[oota-llvm.git] / test / Analysis / ScalarEvolution / max-trip-count.ll
1 ; RUN: opt < %s -analyze -scalar-evolution | FileCheck %s
2
3 ; ScalarEvolution should be able to understand the loop and eliminate the casts.
4
5 ; CHECK: {%d,+,sizeof(i32)}
6
7 define void @foo(i32* nocapture %d, i32 %n) nounwind {
8 entry:
9         %0 = icmp sgt i32 %n, 0         ; <i1> [#uses=1]
10         br i1 %0, label %bb.nph, label %return
11
12 bb.nph:         ; preds = %entry
13         br label %bb
14
15 bb:             ; preds = %bb1, %bb.nph
16         %i.02 = phi i32 [ %5, %bb1 ], [ 0, %bb.nph ]            ; <i32> [#uses=2]
17         %p.01 = phi i8 [ %4, %bb1 ], [ -1, %bb.nph ]            ; <i8> [#uses=2]
18         %1 = sext i8 %p.01 to i32               ; <i32> [#uses=1]
19         %2 = sext i32 %i.02 to i64              ; <i64> [#uses=1]
20         %3 = getelementptr i32, i32* %d, i64 %2         ; <i32*> [#uses=1]
21         store i32 %1, i32* %3, align 4
22         %4 = add i8 %p.01, 1            ; <i8> [#uses=1]
23         %5 = add i32 %i.02, 1           ; <i32> [#uses=2]
24         br label %bb1
25
26 bb1:            ; preds = %bb
27         %6 = icmp slt i32 %5, %n                ; <i1> [#uses=1]
28         br i1 %6, label %bb, label %bb1.return_crit_edge
29
30 bb1.return_crit_edge:           ; preds = %bb1
31         br label %return
32
33 return:         ; preds = %bb1.return_crit_edge, %entry
34         ret void
35 }
36
37 ; ScalarEvolution should be able to find the maximum tripcount
38 ; of this multiple-exit loop, and if it doesn't know the exact
39 ; count, it should say so.
40
41 ; PR7845
42 ; CHECK: Loop %for.cond: <multiple exits> Unpredictable backedge-taken count. 
43 ; CHECK: Loop %for.cond: max backedge-taken count is 5
44
45 @.str = private constant [4 x i8] c"%d\0A\00"     ; <[4 x i8]*> [#uses=2]
46
47 define i32 @main() nounwind {
48 entry:
49   br label %for.cond
50
51 for.cond:                                         ; preds = %for.inc, %entry
52   %g_4.0 = phi i32 [ 0, %entry ], [ %add, %for.inc ] ; <i32> [#uses=5]
53   %cmp = icmp slt i32 %g_4.0, 5                   ; <i1> [#uses=1]
54   br i1 %cmp, label %for.body, label %for.end
55
56 for.body:                                         ; preds = %for.cond
57   %conv = trunc i32 %g_4.0 to i16                 ; <i16> [#uses=1]
58   %tobool.not = icmp eq i16 %conv, 0              ; <i1> [#uses=1]
59   %tobool3 = icmp ne i32 %g_4.0, 0                ; <i1> [#uses=1]
60   %or.cond = and i1 %tobool.not, %tobool3         ; <i1> [#uses=1]
61   br i1 %or.cond, label %for.end, label %for.inc
62
63 for.inc:                                          ; preds = %for.body
64   %add = add nsw i32 %g_4.0, 1                    ; <i32> [#uses=1]
65   br label %for.cond
66
67 for.end:                                          ; preds = %for.body, %for.cond
68   %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.str, i64 0, i64 0), i32 %g_4.0) nounwind ; <i32> [#uses=0]
69   ret i32 0
70 }
71
72 declare i32 @printf(i8*, ...)
73
74 define void @test(i8* %a, i32 %n) nounwind {
75 entry:
76   %cmp1 = icmp sgt i32 %n, 0
77   br i1 %cmp1, label %for.body.lr.ph, label %for.end
78
79 for.body.lr.ph:                                   ; preds = %entry
80   %tmp = zext i32 %n to i64
81   br label %for.body
82
83 for.body:                                         ; preds = %for.body, %for.body.lr.ph
84   %indvar = phi i64 [ %indvar.next, %for.body ], [ 0, %for.body.lr.ph ]
85   %arrayidx = getelementptr i8, i8* %a, i64 %indvar
86   store i8 0, i8* %arrayidx, align 1
87   %indvar.next = add i64 %indvar, 1
88   %exitcond = icmp ne i64 %indvar.next, %tmp
89   br i1 %exitcond, label %for.body, label %for.cond.for.end_crit_edge
90
91 for.cond.for.end_crit_edge:                       ; preds = %for.body
92   br label %for.end
93
94 for.end:                                          ; preds = %for.cond.for.end_crit_edge, %entry
95   ret void
96 }
97
98 ; CHECK: Determining loop execution counts for: @test
99 ; CHECK-NEXT: backedge-taken count is
100 ; CHECK-NEXT: max backedge-taken count is -1
101
102 ; PR19799: Indvars miscompile due to an incorrect max backedge taken count from SCEV.
103 ; CHECK-LABEL: @pr19799
104 ; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count. 
105 ; CHECK: Loop %for.body.i: max backedge-taken count is 1
106 @a = common global i32 0, align 4
107
108 define i32 @pr19799() {
109 entry:
110   store i32 -1, i32* @a, align 4
111   br label %for.body.i
112
113 for.body.i:                                       ; preds = %for.cond.i, %entry
114   %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
115   %tobool.i = icmp eq i32 %storemerge1.i, 0
116   %add.i.i = add nsw i32 %storemerge1.i, 2
117   br i1 %tobool.i, label %bar.exit, label %for.cond.i
118
119 for.cond.i:                                       ; preds = %for.body.i
120   store i32 %add.i.i, i32* @a, align 4
121   %cmp.i = icmp slt i32 %storemerge1.i, 0
122   br i1 %cmp.i, label %for.body.i, label %bar.exit
123
124 bar.exit:                                         ; preds = %for.cond.i, %for.body.i
125   ret i32 0
126 }
127
128 ; PR18886: Indvars miscompile due to an incorrect max backedge taken count from SCEV.
129 ; CHECK-LABEL: @pr18886
130 ; CHECK: Loop %for.body: <multiple exits> Unpredictable backedge-taken count. 
131 ; CHECK: Loop %for.body: max backedge-taken count is 3
132 @aa = global i64 0, align 8
133
134 define i32 @pr18886() {
135 entry:
136   store i64 -21, i64* @aa, align 8
137   br label %for.body
138
139 for.body:
140   %storemerge1 = phi i64 [ -21, %entry ], [ %add, %for.cond ]
141   %tobool = icmp eq i64 %storemerge1, 0
142   %add = add nsw i64 %storemerge1, 8
143   br i1 %tobool, label %return, label %for.cond
144
145 for.cond:
146   store i64 %add, i64* @aa, align 8
147   %cmp = icmp slt i64 %add, 9
148   br i1 %cmp, label %for.body, label %return
149
150 return:
151   %retval.0 = phi i32 [ 1, %for.body ], [ 0, %for.cond ]
152   ret i32 %retval.0
153 }
154
155 ; Here we have a must-exit loop latch that is not computable and a
156 ; may-exit early exit that can only have one non-exiting iteration
157 ; before the check is forever skipped.
158 ;
159 ; CHECK-LABEL: @cannot_compute_mustexit
160 ; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count. 
161 ; CHECK: Loop %for.body.i: Unpredictable max backedge-taken count. 
162 @b = common global i32 0, align 4
163
164 define i32 @cannot_compute_mustexit() {
165 entry:
166   store i32 -1, i32* @a, align 4
167   br label %for.body.i
168
169 for.body.i:                                       ; preds = %for.cond.i, %entry
170   %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
171   %tobool.i = icmp eq i32 %storemerge1.i, 0
172   %add.i.i = add nsw i32 %storemerge1.i, 2
173   br i1 %tobool.i, label %bar.exit, label %for.cond.i
174
175 for.cond.i:                                       ; preds = %for.body.i
176   store i32 %add.i.i, i32* @a, align 4
177   %ld = load volatile i32, i32* @b
178   %cmp.i = icmp ne i32 %ld, 0
179   br i1 %cmp.i, label %for.body.i, label %bar.exit
180
181 bar.exit:                                         ; preds = %for.cond.i, %for.body.i
182   ret i32 0
183 }
184
185 ; This loop has two must-exits, both of which dominate the latch. The
186 ; MaxBECount should be the minimum of them.
187 ;
188 ; CHECK-LABEL: @two_mustexit
189 ; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count. 
190 ; CHECK: Loop %for.body.i: max backedge-taken count is 1
191 define i32 @two_mustexit() {
192 entry:
193   store i32 -1, i32* @a, align 4
194   br label %for.body.i
195
196 for.body.i:                                       ; preds = %for.cond.i, %entry
197   %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
198   %tobool.i = icmp sgt i32 %storemerge1.i, 0
199   %add.i.i = add nsw i32 %storemerge1.i, 2
200   br i1 %tobool.i, label %bar.exit, label %for.cond.i
201
202 for.cond.i:                                       ; preds = %for.body.i
203   store i32 %add.i.i, i32* @a, align 4
204   %cmp.i = icmp slt i32 %storemerge1.i, 3
205   br i1 %cmp.i, label %for.body.i, label %bar.exit
206
207 bar.exit:                                         ; preds = %for.cond.i, %for.body.i
208   ret i32 0
209 }