xref: /llvm-project/llvm/test/Transforms/SCCP/ip-constant-ranges.ll (revision b7e51b4f139ec18c498c818c6bcaa5a842cea83c)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature
2; RUN: opt < %s -passes=ipsccp -S | FileCheck %s
3
4; Constant range for %a is [1, 48) and for %b is [301, 1000)
5define internal i32 @f1(i32 %a, i32 %b) {
6; CHECK-LABEL: define {{[^@]+}}@f1
7; CHECK-SAME: (i32 range(i32 1, 48) [[A:%.*]], i32 range(i32 301, 1000) [[B:%.*]]) {
8; CHECK-NEXT:  entry:
9; CHECK-NEXT:    ret i32 poison
10;
11entry:
12  %cmp.a = icmp sgt i32 %a, 300
13  %cmp.b = icmp sgt i32 %b, 300
14  %cmp.a2 = icmp ugt i32 %a, 300
15  %cmp.b2 = icmp ugt i32 %b, 300
16
17  %a.1 = select i1 %cmp.a, i32 1, i32 2
18  %b.1 = select i1 %cmp.b, i32 1, i32 2
19  %a.2 = select i1 %cmp.a2, i32 1, i32 2
20  %b.2 = select i1 %cmp.b2, i32 1, i32 2
21  %res1 = add i32 %a.1, %b.1
22  %res2 = add i32 %a.2, %b.2
23  %res3 = add i32 %res1, %res2
24  ret i32 %res3
25}
26
27; Constant range for %x is [47, 302)
28define internal i32 @f2(i32 %x) {
29; CHECK-LABEL: define {{[^@]+}}@f2
30; CHECK-SAME: (i32 range(i32 47, 302) [[X:%.*]]) {
31; CHECK-NEXT:  entry:
32; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[X]], 300
33; CHECK-NEXT:    [[CMP4:%.*]] = icmp ugt i32 [[X]], 300
34; CHECK-NEXT:    [[RES1:%.*]] = select i1 [[CMP]], i32 1, i32 2
35; CHECK-NEXT:    [[RES4:%.*]] = select i1 [[CMP4]], i32 3, i32 4
36; CHECK-NEXT:    [[RES6:%.*]] = add nuw nsw i32 [[RES1]], 3
37; CHECK-NEXT:    [[RES7:%.*]] = add nuw nsw i32 5, [[RES4]]
38; CHECK-NEXT:    [[RES:%.*]] = add nuw nsw i32 [[RES6]], 5
39; CHECK-NEXT:    ret i32 [[RES]]
40;
41entry:
42  %cmp = icmp sgt i32 %x, 300
43  %cmp2 = icmp ne i32 %x, 10
44  %cmp3 = icmp sge i32 %x, 47
45  %cmp4 = icmp ugt i32 %x, 300
46  %cmp5 = icmp uge i32 %x, 47
47  %res1 = select i1 %cmp, i32 1, i32 2
48  %res2 = select i1 %cmp2, i32 3, i32 4
49  %res3 = select i1 %cmp3, i32 5, i32 6
50  %res4 = select i1 %cmp4, i32 3, i32 4
51  %res5 = select i1 %cmp5, i32 5, i32 6
52
53  %res6 = add i32 %res1, %res2
54  %res7 = add i32 %res3, %res4
55  %res = add i32 %res6, %res5
56  ret i32 %res
57}
58
59define i32 @caller1() {
60; CHECK-LABEL: define {{[^@]+}}@caller1() {
61; CHECK-NEXT:  entry:
62; CHECK-NEXT:    [[CALL1:%.*]] = tail call i32 @f1(i32 1, i32 301)
63; CHECK-NEXT:    [[CALL2:%.*]] = tail call i32 @f1(i32 47, i32 999)
64; CHECK-NEXT:    [[CALL3:%.*]] = tail call i32 @f2(i32 47)
65; CHECK-NEXT:    [[CALL4:%.*]] = tail call i32 @f2(i32 301)
66; CHECK-NEXT:    [[RES_1:%.*]] = add nuw nsw i32 12, [[CALL3]]
67; CHECK-NEXT:    [[RES_2:%.*]] = add nuw nsw i32 [[RES_1]], [[CALL4]]
68; CHECK-NEXT:    ret i32 [[RES_2]]
69;
70entry:
71  %call1 = tail call i32 @f1(i32 1, i32 301)
72  %call2 = tail call i32 @f1(i32 47, i32 999)
73  %call3 = tail call i32 @f2(i32 47)
74  %call4 = tail call i32 @f2(i32 301)
75  %res.1 = add nsw i32 12, %call3
76  %res.2 = add nsw i32 %res.1, %call4
77  ret i32 %res.2
78}
79
80define internal i32 @f3(i32 %x) {
81; CHECK-LABEL: define {{[^@]+}}@f3
82; CHECK-SAME: (i32 range(i32 0, 2) [[X:%.*]]) {
83; CHECK-NEXT:  entry:
84; CHECK-NEXT:    ret i32 poison
85;
86entry:
87  %cmp = icmp sgt i32 %x, 300
88  %res = select i1 %cmp, i32 1, i32 2
89  ret i32 %res
90}
91
92; The phi node could be converted in a ConstantRange.
93define i32 @caller2(i1 %cmp) {
94; CHECK-LABEL: define {{[^@]+}}@caller2
95; CHECK-SAME: (i1 [[CMP:%.*]]) {
96; CHECK-NEXT:  entry:
97; CHECK-NEXT:    br i1 [[CMP]], label [[IF_TRUE:%.*]], label [[END:%.*]]
98; CHECK:       if.true:
99; CHECK-NEXT:    br label [[END]]
100; CHECK:       end:
101; CHECK-NEXT:    [[RES:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 1, [[IF_TRUE]] ]
102; CHECK-NEXT:    [[CALL1:%.*]] = tail call i32 @f3(i32 [[RES]])
103; CHECK-NEXT:    ret i32 2
104;
105entry:
106  br i1 %cmp, label %if.true, label %end
107
108if.true:
109  br label %end
110
111end:
112  %res = phi i32 [ 0, %entry], [ 1, %if.true ]
113  %call1 = tail call i32 @f3(i32 %res)
114  ret i32 2
115}
116
117define internal i32 @f4(i32 %x) {
118; CHECK-LABEL: define {{[^@]+}}@f4
119; CHECK-SAME: (i32 range(i32 301, -2147483648) [[X:%.*]]) {
120; CHECK-NEXT:  entry:
121; CHECK-NEXT:    ret i32 poison
122;
123entry:
124  %cmp = icmp sgt i32 %x, 300
125  %res = select i1 %cmp, i32 1, i32 2
126  ret i32 %res
127}
128
129; ICmp introduces bounds on ConstantRanges.
130define i32 @caller3(i32 %x) {
131; CHECK-LABEL: define {{[^@]+}}@caller3
132; CHECK-SAME: (i32 [[X:%.*]]) {
133; CHECK-NEXT:  entry:
134; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[X]], 300
135; CHECK-NEXT:    br i1 [[CMP]], label [[IF_TRUE:%.*]], label [[END:%.*]]
136; CHECK:       if.true:
137; CHECK-NEXT:    [[X_1:%.*]] = tail call i32 @f4(i32 [[X]])
138; CHECK-NEXT:    br label [[END]]
139; CHECK:       end:
140; CHECK-NEXT:    [[RES:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 1, [[IF_TRUE]] ]
141; CHECK-NEXT:    ret i32 [[RES]]
142;
143entry:
144  %cmp = icmp sgt i32 %x, 300
145  br i1 %cmp, label %if.true, label %end
146
147if.true:
148  %x.1 = tail call i32 @f4(i32 %x)
149  br label %end
150
151end:
152  %res = phi i32 [ 0, %entry], [ %x.1, %if.true ]
153  ret i32 %res
154}
155
156; Check to make sure we do not attempt to access lattice values in unreachable
157; blocks.
158define i32 @test_unreachable() {
159; CHECK-LABEL: define {{[^@]+}}@test_unreachable() {
160; CHECK-NEXT:  entry:
161; CHECK-NEXT:    [[TMP0:%.*]] = call i1 @test_unreachable_callee(i32 1)
162; CHECK-NEXT:    [[TMP1:%.*]] = call i1 @test_unreachable_callee(i32 2)
163; CHECK-NEXT:    ret i32 1
164;
165entry:
166  call i1 @test_unreachable_callee(i32 1)
167  call i1 @test_unreachable_callee(i32 2)
168  ret i32 1
169}
170
171define internal i1 @test_unreachable_callee(i32 %a) {
172; CHECK-LABEL: define {{[^@]+}}@test_unreachable_callee
173; CHECK-SAME: (i32 range(i32 1, 3) [[A:%.*]]) {
174; CHECK-NEXT:  entry:
175; CHECK-NEXT:    ret i1 poison
176;
177entry:
178  ret i1 true
179
180unreachablebb:
181  %cmp = icmp eq i32 undef, %a
182  unreachable
183}
184
185; Check that we do not attempt to get range info for non-integer types and
186; crash.
187define double @test_struct({ double, double } %test) {
188; CHECK-LABEL: define {{[^@]+}}@test_struct
189; CHECK-SAME: ({ double, double } [[TEST:%.*]]) {
190; CHECK-NEXT:    [[V:%.*]] = extractvalue { double, double } [[TEST]], 0
191; CHECK-NEXT:    [[R:%.*]] = fmul double [[V]], [[V]]
192; CHECK-NEXT:    ret double [[R]]
193;
194  %v = extractvalue { double, double } %test, 0
195  %r = fmul double %v, %v
196  ret double %r
197}
198
199; Constant range for %x is [47, 302)
200define internal i32 @f5(i32 %x) {
201; CHECK-LABEL: define {{[^@]+}}@f5
202; CHECK-SAME: (i32 range(i32 47, 302) [[X:%.*]]) {
203; CHECK-NEXT:  entry:
204; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[X]], undef
205; CHECK-NEXT:    [[CMP2:%.*]] = icmp ne i32 undef, [[X]]
206; CHECK-NEXT:    [[RES1:%.*]] = select i1 [[CMP]], i32 1, i32 2
207; CHECK-NEXT:    [[RES2:%.*]] = select i1 [[CMP2]], i32 3, i32 4
208; CHECK-NEXT:    [[RES:%.*]] = add i32 [[RES1]], [[RES2]]
209; CHECK-NEXT:    ret i32 [[RES]]
210;
211entry:
212  %cmp = icmp sgt i32 %x, undef
213  %cmp2 = icmp ne i32 undef, %x
214  %res1 = select i1 %cmp, i32 1, i32 2
215  %res2 = select i1 %cmp2, i32 3, i32 4
216
217  %res = add i32 %res1, %res2
218  ret i32 %res
219}
220
221define i32 @caller4() {
222; CHECK-LABEL: define {{[^@]+}}@caller4() {
223; CHECK-NEXT:  entry:
224; CHECK-NEXT:    [[CALL1:%.*]] = tail call i32 @f5(i32 47)
225; CHECK-NEXT:    [[CALL2:%.*]] = tail call i32 @f5(i32 301)
226; CHECK-NEXT:    [[RES:%.*]] = add nsw i32 [[CALL1]], [[CALL2]]
227; CHECK-NEXT:    ret i32 [[RES]]
228;
229entry:
230  %call1 = tail call i32 @f5(i32 47)
231  %call2 = tail call i32 @f5(i32 301)
232  %res = add nsw i32 %call1, %call2
233  ret i32 %res
234}
235
236; Make sure we do re-evaluate the function after ParamState changes.
237define internal i32 @recursive_f(i32 %i) {
238; CHECK-LABEL: define {{[^@]+}}@recursive_f
239; CHECK-SAME: (i32 [[I:%.*]]) {
240; CHECK-NEXT:  entry:
241; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[I]], 0
242; CHECK-NEXT:    br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
243; CHECK:       if.then:
244; CHECK-NEXT:    br label [[RETURN:%.*]]
245; CHECK:       if.else:
246; CHECK-NEXT:    [[SUB:%.*]] = sub nuw nsw i32 [[I]], 1
247; CHECK-NEXT:    [[CALL:%.*]] = call i32 @recursive_f(i32 [[SUB]])
248; CHECK-NEXT:    [[ADD:%.*]] = add i32 [[I]], [[CALL]]
249; CHECK-NEXT:    br label [[RETURN]]
250; CHECK:       return:
251; CHECK-NEXT:    [[RETVAL_0:%.*]] = phi i32 [ 0, [[IF_THEN]] ], [ [[ADD]], [[IF_ELSE]] ]
252; CHECK-NEXT:    ret i32 [[RETVAL_0]]
253;
254entry:
255  %cmp = icmp eq i32 %i, 0
256  br i1 %cmp, label %if.then, label %if.else
257
258if.then:                                          ; preds = %entry
259  br label %return
260
261if.else:                                          ; preds = %entry
262  %sub = sub nsw i32 %i, 1
263  %call = call i32 @recursive_f(i32 %sub)
264  %add = add i32 %i, %call
265  br label %return
266
267return:                                           ; preds = %if.else, %if.then
268  %retval.0 = phi i32 [ 0, %if.then ], [ %add, %if.else ]
269  ret i32 %retval.0
270}
271
272define i32 @caller5() {
273; CHECK-LABEL: define {{[^@]+}}@caller5() {
274; CHECK-NEXT:  entry:
275; CHECK-NEXT:    [[CALL:%.*]] = call i32 @recursive_f(i32 42)
276; CHECK-NEXT:    ret i32 [[CALL]]
277;
278entry:
279  %call = call i32 @recursive_f(i32 42)
280  ret i32 %call
281}
282
283define internal i32 @callee6.1(i32 %i) {
284; CHECK-LABEL: define {{[^@]+}}@callee6.1
285; CHECK-SAME: (i32 range(i32 30, 44) [[I:%.*]]) {
286; CHECK-NEXT:    [[RES:%.*]] = call i32 @callee6.2(i32 [[I]])
287; CHECK-NEXT:    ret i32 poison
288;
289  %res = call i32 @callee6.2(i32 %i)
290  ret i32 %res
291}
292
293define internal i32 @callee6.2(i32 %i) {
294; CHECK-LABEL: define {{[^@]+}}@callee6.2
295; CHECK-SAME: (i32 range(i32 30, 44) [[I:%.*]]) {
296; CHECK-NEXT:    br label [[IF_THEN:%.*]]
297; CHECK:       if.then:
298; CHECK-NEXT:    ret i32 poison
299;
300
301  %cmp = icmp ne i32 %i, 0
302  br i1 %cmp, label %if.then, label %if.else
303
304if.then:                                          ; preds = %entry
305  ret i32 1
306
307if.else:                                          ; preds = %entry
308  ret i32 2
309}
310
311define i32 @caller6() {
312; CHECK-LABEL: define {{[^@]+}}@caller6() {
313; CHECK-NEXT:    [[CALL_1:%.*]] = call i32 @callee6.1(i32 30)
314; CHECK-NEXT:    [[CALL_2:%.*]] = call i32 @callee6.1(i32 43)
315; CHECK-NEXT:    ret i32 2
316;
317  %call.1 = call i32 @callee6.1(i32 30)
318  %call.2 = call i32 @callee6.1(i32 43)
319  %res = add i32 %call.1, %call.2
320  ret i32 %res
321}
322