1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; Check simplification of
5 ; (icmp sgt x, -1) & (icmp sgt/sge n, x) --> icmp ugt/uge n, x
7 define i1 @test_and1(i32 %x, i32 %n) {
8 ; CHECK-LABEL: @test_and1(
9 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
10 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[NN]], [[X:%.*]]
11 ; CHECK-NEXT: ret i1 [[TMP1]]
13 %nn = and i32 %n, 2147483647
14 %a = icmp sge i32 %x, 0
15 %b = icmp slt i32 %x, %nn
20 define i1 @test_and1_logical(i32 %x, i32 %n) {
21 ; CHECK-LABEL: @test_and1_logical(
22 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
23 ; CHECK-NEXT: [[A:%.*]] = icmp sgt i32 [[X:%.*]], -1
24 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[NN]], [[X]]
25 ; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], i1 [[B]], i1 false
26 ; CHECK-NEXT: ret i1 [[C]]
28 %nn = and i32 %n, 2147483647
29 %a = icmp sge i32 %x, 0
30 %b = icmp slt i32 %x, %nn
31 %c = select i1 %a, i1 %b, i1 false
35 define i1 @test_and2(i32 %x, i32 %n) {
36 ; CHECK-LABEL: @test_and2(
37 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
38 ; CHECK-NEXT: [[TMP1:%.*]] = icmp uge i32 [[NN]], [[X:%.*]]
39 ; CHECK-NEXT: ret i1 [[TMP1]]
41 %nn = and i32 %n, 2147483647
42 %a = icmp sgt i32 %x, -1
43 %b = icmp sle i32 %x, %nn
48 define i1 @test_and2_logical(i32 %x, i32 %n) {
49 ; CHECK-LABEL: @test_and2_logical(
50 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
51 ; CHECK-NEXT: [[A:%.*]] = icmp sgt i32 [[X:%.*]], -1
52 ; CHECK-NEXT: [[B:%.*]] = icmp sge i32 [[NN]], [[X]]
53 ; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], i1 [[B]], i1 false
54 ; CHECK-NEXT: ret i1 [[C]]
56 %nn = and i32 %n, 2147483647
57 %a = icmp sgt i32 %x, -1
58 %b = icmp sle i32 %x, %nn
59 %c = select i1 %a, i1 %b, i1 false
63 define i1 @test_and3(i32 %x, i32 %n) {
64 ; CHECK-LABEL: @test_and3(
65 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
66 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[NN]], [[X:%.*]]
67 ; CHECK-NEXT: ret i1 [[TMP1]]
69 %nn = and i32 %n, 2147483647
70 %a = icmp sgt i32 %nn, %x
71 %b = icmp sge i32 %x, 0
76 define i1 @test_and3_logical(i32 %x, i32 %n) {
77 ; CHECK-LABEL: @test_and3_logical(
78 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
79 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[NN]], [[X:%.*]]
80 ; CHECK-NEXT: ret i1 [[TMP1]]
82 %nn = and i32 %n, 2147483647
83 %a = icmp sgt i32 %nn, %x
84 %b = icmp sge i32 %x, 0
85 %c = select i1 %a, i1 %b, i1 false
89 define i1 @test_and4(i32 %x, i32 %n) {
90 ; CHECK-LABEL: @test_and4(
91 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
92 ; CHECK-NEXT: [[TMP1:%.*]] = icmp uge i32 [[NN]], [[X:%.*]]
93 ; CHECK-NEXT: ret i1 [[TMP1]]
95 %nn = and i32 %n, 2147483647
96 %a = icmp sge i32 %nn, %x
97 %b = icmp sge i32 %x, 0
102 define i1 @test_and4_logical(i32 %x, i32 %n) {
103 ; CHECK-LABEL: @test_and4_logical(
104 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
105 ; CHECK-NEXT: [[TMP1:%.*]] = icmp uge i32 [[NN]], [[X:%.*]]
106 ; CHECK-NEXT: ret i1 [[TMP1]]
108 %nn = and i32 %n, 2147483647
109 %a = icmp sge i32 %nn, %x
110 %b = icmp sge i32 %x, 0
111 %c = select i1 %a, i1 %b, i1 false
115 define i1 @test_or1(i32 %x, i32 %n) {
116 ; CHECK-LABEL: @test_or1(
117 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
118 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i32 [[NN]], [[X:%.*]]
119 ; CHECK-NEXT: ret i1 [[TMP1]]
121 %nn = and i32 %n, 2147483647
122 %a = icmp slt i32 %x, 0
123 %b = icmp sge i32 %x, %nn
128 define i1 @test_or1_logical(i32 %x, i32 %n) {
129 ; CHECK-LABEL: @test_or1_logical(
130 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
131 ; CHECK-NEXT: [[A:%.*]] = icmp slt i32 [[X:%.*]], 0
132 ; CHECK-NEXT: [[B:%.*]] = icmp sle i32 [[NN]], [[X]]
133 ; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], i1 true, i1 [[B]]
134 ; CHECK-NEXT: ret i1 [[C]]
136 %nn = and i32 %n, 2147483647
137 %a = icmp slt i32 %x, 0
138 %b = icmp sge i32 %x, %nn
139 %c = select i1 %a, i1 true, i1 %b
143 define i1 @test_or2(i32 %x, i32 %n) {
144 ; CHECK-LABEL: @test_or2(
145 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
146 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[NN]], [[X:%.*]]
147 ; CHECK-NEXT: ret i1 [[TMP1]]
149 %nn = and i32 %n, 2147483647
150 %a = icmp sle i32 %x, -1
151 %b = icmp sgt i32 %x, %nn
156 define i1 @test_or2_logical(i32 %x, i32 %n) {
157 ; CHECK-LABEL: @test_or2_logical(
158 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
159 ; CHECK-NEXT: [[A:%.*]] = icmp slt i32 [[X:%.*]], 0
160 ; CHECK-NEXT: [[B:%.*]] = icmp slt i32 [[NN]], [[X]]
161 ; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], i1 true, i1 [[B]]
162 ; CHECK-NEXT: ret i1 [[C]]
164 %nn = and i32 %n, 2147483647
165 %a = icmp sle i32 %x, -1
166 %b = icmp sgt i32 %x, %nn
167 %c = select i1 %a, i1 true, i1 %b
171 define i1 @test_or3(i32 %x, i32 %n) {
172 ; CHECK-LABEL: @test_or3(
173 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
174 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i32 [[NN]], [[X:%.*]]
175 ; CHECK-NEXT: ret i1 [[TMP1]]
177 %nn = and i32 %n, 2147483647
178 %a = icmp sle i32 %nn, %x
179 %b = icmp slt i32 %x, 0
184 define i1 @test_or3_logical(i32 %x, i32 %n) {
185 ; CHECK-LABEL: @test_or3_logical(
186 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
187 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ule i32 [[NN]], [[X:%.*]]
188 ; CHECK-NEXT: ret i1 [[TMP1]]
190 %nn = and i32 %n, 2147483647
191 %a = icmp sle i32 %nn, %x
192 %b = icmp slt i32 %x, 0
193 %c = select i1 %a, i1 true, i1 %b
197 define i1 @test_or4(i32 %x, i32 %n) {
198 ; CHECK-LABEL: @test_or4(
199 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
200 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[NN]], [[X:%.*]]
201 ; CHECK-NEXT: ret i1 [[TMP1]]
203 %nn = and i32 %n, 2147483647
204 %a = icmp slt i32 %nn, %x
205 %b = icmp slt i32 %x, 0
210 define i1 @test_or4_logical(i32 %x, i32 %n) {
211 ; CHECK-LABEL: @test_or4_logical(
212 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
213 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[NN]], [[X:%.*]]
214 ; CHECK-NEXT: ret i1 [[TMP1]]
216 %nn = and i32 %n, 2147483647
217 %a = icmp slt i32 %nn, %x
218 %b = icmp slt i32 %x, 0
219 %c = select i1 %a, i1 true, i1 %b
225 define i1 @negative1(i32 %x, i32 %n) {
226 ; CHECK-LABEL: @negative1(
227 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
228 ; CHECK-NEXT: [[A:%.*]] = icmp sgt i32 [[NN]], [[X:%.*]]
229 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[X]], 0
230 ; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]]
231 ; CHECK-NEXT: ret i1 [[C]]
233 %nn = and i32 %n, 2147483647
234 %a = icmp slt i32 %x, %nn
235 %b = icmp sgt i32 %x, 0 ; should be: icmp sge
240 define i1 @negative1_logical(i32 %x, i32 %n) {
241 ; CHECK-LABEL: @negative1_logical(
242 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
243 ; CHECK-NEXT: [[A:%.*]] = icmp sgt i32 [[NN]], [[X:%.*]]
244 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[X]], 0
245 ; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]]
246 ; CHECK-NEXT: ret i1 [[C]]
248 %nn = and i32 %n, 2147483647
249 %a = icmp slt i32 %x, %nn
250 %b = icmp sgt i32 %x, 0 ; should be: icmp sge
251 %c = select i1 %a, i1 %b, i1 false
255 define i1 @negative2(i32 %x, i32 %n) {
256 ; CHECK-LABEL: @negative2(
257 ; CHECK-NEXT: [[A:%.*]] = icmp slt i32 [[X:%.*]], [[N:%.*]]
258 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[X]], -1
259 ; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]]
260 ; CHECK-NEXT: ret i1 [[C]]
262 %a = icmp slt i32 %x, %n ; n can be negative
263 %b = icmp sge i32 %x, 0
268 define i1 @negative2_logical(i32 %x, i32 %n) {
269 ; CHECK-LABEL: @negative2_logical(
270 ; CHECK-NEXT: [[A:%.*]] = icmp slt i32 [[X:%.*]], [[N:%.*]]
271 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[X]], -1
272 ; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]]
273 ; CHECK-NEXT: ret i1 [[C]]
275 %a = icmp slt i32 %x, %n ; n can be negative
276 %b = icmp sge i32 %x, 0
277 %c = select i1 %a, i1 %b, i1 false
281 define i1 @negative3(i32 %x, i32 %y, i32 %n) {
282 ; CHECK-LABEL: @negative3(
283 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
284 ; CHECK-NEXT: [[A:%.*]] = icmp sgt i32 [[NN]], [[X:%.*]]
285 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[Y:%.*]], -1
286 ; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]]
287 ; CHECK-NEXT: ret i1 [[C]]
289 %nn = and i32 %n, 2147483647
290 %a = icmp slt i32 %x, %nn
291 %b = icmp sge i32 %y, 0 ; should compare %x and not %y
296 define i1 @negative3_logical(i32 %x, i32 %y, i32 %n) {
297 ; CHECK-LABEL: @negative3_logical(
298 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
299 ; CHECK-NEXT: [[A:%.*]] = icmp sgt i32 [[NN]], [[X:%.*]]
300 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[Y:%.*]], -1
301 ; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], i1 [[B]], i1 false
302 ; CHECK-NEXT: ret i1 [[C]]
304 %nn = and i32 %n, 2147483647
305 %a = icmp slt i32 %x, %nn
306 %b = icmp sge i32 %y, 0 ; should compare %x and not %y
307 %c = select i1 %a, i1 %b, i1 false
311 define i1 @negative4(i32 %x, i32 %n) {
312 ; CHECK-LABEL: @negative4(
313 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
314 ; CHECK-NEXT: [[A:%.*]] = icmp ne i32 [[NN]], [[X:%.*]]
315 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[X]], -1
316 ; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]]
317 ; CHECK-NEXT: ret i1 [[C]]
319 %nn = and i32 %n, 2147483647
320 %a = icmp ne i32 %x, %nn ; should be: icmp slt/sle
321 %b = icmp sge i32 %x, 0
326 define i1 @negative4_logical(i32 %x, i32 %n) {
327 ; CHECK-LABEL: @negative4_logical(
328 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
329 ; CHECK-NEXT: [[A:%.*]] = icmp ne i32 [[NN]], [[X:%.*]]
330 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[X]], -1
331 ; CHECK-NEXT: [[C:%.*]] = and i1 [[A]], [[B]]
332 ; CHECK-NEXT: ret i1 [[C]]
334 %nn = and i32 %n, 2147483647
335 %a = icmp ne i32 %x, %nn ; should be: icmp slt/sle
336 %b = icmp sge i32 %x, 0
337 %c = select i1 %a, i1 %b, i1 false
341 define i1 @negative5(i32 %x, i32 %n) {
342 ; CHECK-LABEL: @negative5(
343 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
344 ; CHECK-NEXT: [[A:%.*]] = icmp sgt i32 [[NN]], [[X:%.*]]
345 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[X]], -1
346 ; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]]
347 ; CHECK-NEXT: ret i1 [[C]]
349 %nn = and i32 %n, 2147483647
350 %a = icmp slt i32 %x, %nn
351 %b = icmp sge i32 %x, 0
352 %c = or i1 %a, %b ; should be: and
356 define i1 @negative5_logical(i32 %x, i32 %n) {
357 ; CHECK-LABEL: @negative5_logical(
358 ; CHECK-NEXT: [[NN:%.*]] = and i32 [[N:%.*]], 2147483647
359 ; CHECK-NEXT: [[A:%.*]] = icmp sgt i32 [[NN]], [[X:%.*]]
360 ; CHECK-NEXT: [[B:%.*]] = icmp sgt i32 [[X]], -1
361 ; CHECK-NEXT: [[C:%.*]] = or i1 [[A]], [[B]]
362 ; CHECK-NEXT: ret i1 [[C]]
364 %nn = and i32 %n, 2147483647
365 %a = icmp slt i32 %x, %nn
366 %b = icmp sge i32 %x, 0
367 %c = select i1 %a, i1 true, i1 %b ; should be: and