xref: /llvm-project/llvm/test/Analysis/ScalarEvolution/trip-count.ll (revision 1a37147af5a91635e3982a9cbbdfe1d7492af84c)
1; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
2; RUN: opt < %s -disable-output "-passes=print<scalar-evolution>" -scalar-evolution-max-iterations=0  -scalar-evolution-classify-expressions=0  2>&1 | FileCheck %s
3
4target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
5target triple = "x86_64-unknown-linux-gnu"
6
7@A = weak global [1000 x i32] zeroinitializer, align 32
8
9define void @PR1101(i32 %N) {
10; CHECK-LABEL: 'PR1101'
11; CHECK-NEXT:  Determining loop execution counts for: @PR1101
12; CHECK-NEXT:  Loop %bb3: backedge-taken count is i32 10000
13; CHECK-NEXT:  Loop %bb3: constant max backedge-taken count is i32 10000
14; CHECK-NEXT:  Loop %bb3: symbolic max backedge-taken count is i32 10000
15; CHECK-NEXT:  Loop %bb3: Trip multiple is 10001
16;
17entry:
18  br label %bb3
19
20bb:             ; preds = %bb3
21  %tmp = getelementptr [1000 x i32], ptr @A, i32 0, i32 %i.0          ; <ptr> [#uses=1]
22  store i32 123, ptr %tmp
23  %tmp2 = add i32 %i.0, 1         ; <i32> [#uses=1]
24  br label %bb3
25
26bb3:            ; preds = %bb, %entry
27  %i.0 = phi i32 [ 0, %entry ], [ %tmp2, %bb ]            ; <i32> [#uses=3]
28  %tmp3 = icmp sle i32 %i.0, 9999          ; <i1> [#uses=1]
29  br i1 %tmp3, label %bb, label %bb5
30
31bb5:            ; preds = %bb3
32  br label %return
33
34return:         ; preds = %bb5
35  ret void
36}
37
38define i32 @PR22795() {
39; CHECK-LABEL: 'PR22795'
40; CHECK-NEXT:  Determining loop execution counts for: @PR22795
41; CHECK-NEXT:  Loop %preheader: backedge-taken count is i64 7
42; CHECK-NEXT:  Loop %preheader: constant max backedge-taken count is i64 7
43; CHECK-NEXT:  Loop %preheader: symbolic max backedge-taken count is i64 7
44; CHECK-NEXT:  Loop %preheader: Trip multiple is 8
45;
46entry:
47  %bins = alloca [16 x i64], align 16
48  call void @llvm.memset.p0.i64(ptr align 16 %bins, i8 0, i64 128, i1 false)
49  br label %preheader
50
51preheader:                                        ; preds = %for.inc.1, %entry
52  %v11 = phi i64 [ 0, %entry ], [ %next12.1, %for.inc.1 ]
53  %iv = phi i32 [ -1, %entry ], [ %next.1, %for.inc.1 ]
54  %cmp = icmp sgt i64 %v11, 0
55  br i1 %cmp, label %for.body, label %for.inc
56
57for.body:                                         ; preds = %preheader
58  %zext = zext i32 %iv to i64
59  %arrayidx = getelementptr [16 x i64], ptr %bins, i64 0, i64 %v11
60  %loaded = load i64, ptr %arrayidx, align 8
61  %add = add i64 %loaded, 1
62  %add2 = add i64 %add, %zext
63  store i64 %add2, ptr %arrayidx, align 8
64  br label %for.inc
65
66for.inc:                                          ; preds = %for.body, %preheader
67  %next12 = add nuw nsw i64 %v11, 1
68  %next = add nsw i32 %iv, 1
69  br i1 true, label %for.body.1, label %for.inc.1
70
71end:                                              ; preds = %for.inc.1
72  %arrayidx8 = getelementptr [16 x i64], ptr %bins, i64 0, i64 2
73  %load = load i64, ptr %arrayidx8, align 16
74  %shr4 = lshr i64 %load, 32
75  %conv = trunc i64 %shr4 to i32
76  ret i32 %conv
77
78for.body.1:                                       ; preds = %for.inc
79  %zext.1 = zext i32 %next to i64
80  %arrayidx.1 = getelementptr [16 x i64], ptr %bins, i64 0, i64 %next12
81  %loaded.1 = load i64, ptr %arrayidx.1, align 8
82  %add.1 = add i64 %loaded.1, 1
83  %add2.1 = add i64 %add.1, %zext.1
84  store i64 %add2.1, ptr %arrayidx.1, align 8
85  br label %for.inc.1
86
87for.inc.1:                                        ; preds = %for.body.1, %for.inc
88  %next12.1 = add nuw nsw i64 %next12, 1
89  %next.1 = add nuw nsw i32 %next, 1
90  %exitcond.1 = icmp eq i64 %next12.1, 16
91  br i1 %exitcond.1, label %end, label %preheader
92}
93
94; Function Attrs: nounwind
95declare void @llvm.memset.p0.i64(ptr nocapture, i8, i64, i1) #0
96
97declare void @may_exit() nounwind
98
99define void @pr28012(i32 %n) {
100; CHECK-LABEL: 'pr28012'
101; CHECK-NEXT:  Determining loop execution counts for: @pr28012
102; CHECK-NEXT:  Loop %loop: backedge-taken count is i32 -1431655751
103; CHECK-NEXT:  Loop %loop: constant max backedge-taken count is i32 -1431655751
104; CHECK-NEXT:  Loop %loop: symbolic max backedge-taken count is i32 -1431655751
105; CHECK-NEXT:  Loop %loop: Trip multiple is 2863311546
106;
107entry:
108  br label %loop
109
110loop:
111  %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
112  %iv.inc = add nsw i32 %iv, 3
113  call void @may_exit()
114  %becond = icmp ne i32 %iv.inc, 46
115  br i1 %becond, label %loop, label %leave
116
117leave:
118  ret void
119}
120
121define void @non_zero_from_loop_guard(i16 %n) {
122; CHECK-LABEL: 'non_zero_from_loop_guard'
123; CHECK-NEXT:  Determining loop execution counts for: @non_zero_from_loop_guard
124; CHECK-NEXT:  Loop %loop: backedge-taken count is (-1 + (%n /u 2))<nsw>
125; CHECK-NEXT:  Loop %loop: constant max backedge-taken count is i16 32766
126; CHECK-NEXT:  Loop %loop: symbolic max backedge-taken count is (-1 + (%n /u 2))<nsw>
127; CHECK-NEXT:  Loop %loop: Trip multiple is 1
128;
129entry:
130  %shr = lshr i16 %n, 1
131  %precond = icmp ult i16 %n, 2
132  br i1 %precond, label %exit, label %loop
133
134loop:
135  %iv = phi i16 [ %inc, %loop ], [ 0, %entry ]
136  %inc = add nuw nsw i16 %iv, 1
137  %cmp = icmp ult i16 %inc, %shr
138  br i1 %cmp, label %loop, label %exit
139
140exit:
141  ret void
142}
143
144define void @dual_sext_ne_with_slt_guard(i8 %s, i8 %n) {
145; CHECK-LABEL: 'dual_sext_ne_with_slt_guard'
146; CHECK-NEXT:  Determining loop execution counts for: @dual_sext_ne_with_slt_guard
147; CHECK-NEXT:  Loop %for.body: backedge-taken count is (-1 + (sext i8 %n to i64) + (-1 * (sext i8 %s to i64))<nsw>)
148; CHECK-NEXT:  Loop %for.body: constant max backedge-taken count is i64 -2
149; CHECK-NEXT:  Loop %for.body: symbolic max backedge-taken count is (-1 + (sext i8 %n to i64) + (-1 * (sext i8 %s to i64))<nsw>)
150; CHECK-NEXT:  Loop %for.body: Trip multiple is 1
151;
152entry:
153  %cmp4 = icmp slt i8 %s, %n
154  br i1 %cmp4, label %for.body.preheader, label %exit
155
156for.body.preheader:
157  %0 = sext i8 %s to i64
158  %wide.trip.count = sext i8 %n to i64
159  br label %for.body
160
161for.body:
162  %iv = phi i64 [ %0, %for.body.preheader ], [ %iv.next, %for.body ]
163  %iv.next = add nsw i64 %iv, 1
164  %exitcond.not = icmp eq i64 %iv.next, %wide.trip.count
165  br i1 %exitcond.not, label %exit, label %for.body
166
167exit:
168  ret void
169}
170
171define void @dual_sext_ne_with_nsw_inc(i8 %s, i64 %n) {
172; CHECK-LABEL: 'dual_sext_ne_with_nsw_inc'
173; CHECK-NEXT:  Determining loop execution counts for: @dual_sext_ne_with_nsw_inc
174; CHECK-NEXT:  Loop %for.body: backedge-taken count is (-1 + (-1 * (sext i8 %s to i64))<nsw> + %n)
175; CHECK-NEXT:  Loop %for.body: constant max backedge-taken count is i64 -1
176; CHECK-NEXT:  Loop %for.body: symbolic max backedge-taken count is (-1 + (-1 * (sext i8 %s to i64))<nsw> + %n)
177; CHECK-NEXT:  Loop %for.body: Trip multiple is 1
178;
179entry:
180  %0 = sext i8 %s to i64
181  br label %for.body
182
183for.body:
184  %iv = phi i64 [ %0, %entry ], [ %iv.next, %for.body ]
185  %iv.next = add nsw i64 %iv, 1
186  %exitcond.not = icmp eq i64 %iv.next, %n
187  br i1 %exitcond.not, label %exit, label %for.body
188
189exit:
190  ret void
191}
192
193define void @dual_sext_ne_with_nuw_inc(i8 %s, i64 %n) {
194; CHECK-LABEL: 'dual_sext_ne_with_nuw_inc'
195; CHECK-NEXT:  Determining loop execution counts for: @dual_sext_ne_with_nuw_inc
196; CHECK-NEXT:  Loop %for.body: backedge-taken count is (-1 + (-1 * (sext i8 %s to i64))<nsw> + %n)
197; CHECK-NEXT:  Loop %for.body: constant max backedge-taken count is i64 -1
198; CHECK-NEXT:  Loop %for.body: symbolic max backedge-taken count is (-1 + (-1 * (sext i8 %s to i64))<nsw> + %n)
199; CHECK-NEXT:  Loop %for.body: Trip multiple is 1
200;
201entry:
202  %0 = sext i8 %s to i64
203  br label %for.body
204
205for.body:
206  %iv = phi i64 [ %0, %entry ], [ %iv.next, %for.body ]
207  %iv.next = add nuw i64 %iv, 1
208  %exitcond.not = icmp eq i64 %iv.next, %n
209  br i1 %exitcond.not, label %exit, label %for.body
210
211exit:
212  ret void
213}
214