1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 define i64 @test_lshr(i1 %c) {
5 ; CHECK-LABEL: @test_lshr(
7 ; CHECK-NEXT: br label [[LOOP:%.*]]
9 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
11 ; CHECK-NEXT: ret i64 1023
16 %iv.lshr = phi i64 [1023, %entry], [%iv.lshr.next, %loop]
17 %iv.lshr.next = lshr i64 %iv.lshr, 1
18 br i1 %c, label %exit, label %loop
20 %res = or i64 %iv.lshr, 1023
24 define i64 @test_add(i1 %c) {
25 ; CHECK-LABEL: @test_add(
27 ; CHECK-NEXT: br label [[LOOP:%.*]]
29 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
31 ; CHECK-NEXT: ret i64 0
36 %iv = phi i64 [8, %entry], [%iv.next, %loop]
37 %iv.next = add nuw i64 %iv, 4
38 br i1 %c, label %exit, label %loop
44 define i64 @test_sub(i1 %c) {
45 ; CHECK-LABEL: @test_sub(
47 ; CHECK-NEXT: br label [[LOOP:%.*]]
49 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
51 ; CHECK-NEXT: ret i64 0
56 %iv = phi i64 [8, %entry], [%iv.next, %loop]
57 %iv.next = sub nuw i64 %iv, 4
58 br i1 %c, label %exit, label %loop
64 define i64 @test_mul(i1 %c) {
65 ; CHECK-LABEL: @test_mul(
67 ; CHECK-NEXT: br label [[LOOP:%.*]]
69 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
71 ; CHECK-NEXT: ret i64 0
76 %iv = phi i64 [8, %entry], [%iv.next, %loop]
77 %iv.next = mul i64 %iv, 2
78 br i1 %c, label %exit, label %loop
84 define i64 @test_udiv(i1 %c) {
85 ; CHECK-LABEL: @test_udiv(
87 ; CHECK-NEXT: br label [[LOOP:%.*]]
89 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
91 ; CHECK-NEXT: ret i64 0
96 %iv = phi i64 [9, %entry], [%iv.next, %loop]
97 %iv.next = udiv i64 %iv, 3
98 br i1 %c, label %exit, label %loop
100 %res = and i64 %iv, 16
104 define i64 @test_udiv_neg(i1 %c) {
105 ; CHECK-LABEL: @test_udiv_neg(
107 ; CHECK-NEXT: br label [[LOOP:%.*]]
109 ; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 2, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
110 ; CHECK-NEXT: [[IV_NEXT]] = udiv i64 9, [[IV]]
111 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
113 ; CHECK-NEXT: [[RES:%.*]] = and i64 [[IV]], 4
114 ; CHECK-NEXT: ret i64 [[RES]]
119 %iv = phi i64 [2, %entry], [%iv.next, %loop]
120 %iv.next = udiv i64 9, %iv
121 br i1 %c, label %exit, label %loop
123 %res = and i64 %iv, 4
127 define i64 @test_urem(i1 %c) {
128 ; CHECK-LABEL: @test_urem(
130 ; CHECK-NEXT: br label [[LOOP:%.*]]
132 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
134 ; CHECK-NEXT: ret i64 0
139 %iv = phi i64 [3, %entry], [%iv.next, %loop]
140 %iv.next = urem i64 9, %iv
141 br i1 %c, label %exit, label %loop
143 %res = and i64 %iv, 4
147 define i64 @test_and(i1 %c) {
148 ; CHECK-LABEL: @test_and(
150 ; CHECK-NEXT: br label [[LOOP:%.*]]
152 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
154 ; CHECK-NEXT: ret i64 2047
159 %iv = phi i64 [1025, %entry], [%iv.next, %loop]
160 %iv.next = and i64 %iv, 1024
161 br i1 %c, label %exit, label %loop
163 %res = or i64 %iv, 1023
167 define i64 @test_or(i1 %c) {
168 ; CHECK-LABEL: @test_or(
170 ; CHECK-NEXT: br label [[LOOP:%.*]]
172 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
174 ; CHECK-NEXT: ret i64 2047
179 %iv = phi i64 [1025, %entry], [%iv.next, %loop]
180 %iv.next = or i64 %iv, 1024
181 br i1 %c, label %exit, label %loop
183 %res = or i64 %iv, 1023
187 define i64 @test_ashr_zeros(i1 %c) {
188 ; CHECK-LABEL: @test_ashr_zeros(
190 ; CHECK-NEXT: br label [[LOOP:%.*]]
192 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
194 ; CHECK-NEXT: ret i64 1023
199 %iv.ashr = phi i64 [1023, %entry], [%iv.ashr.next, %loop]
200 %iv.ashr.next = ashr i64 %iv.ashr, 1
201 br i1 %c, label %exit, label %loop
203 %res = or i64 %iv.ashr, 1023
207 define i64 @test_ashr_ones(i1 %c) {
208 ; CHECK-LABEL: @test_ashr_ones(
210 ; CHECK-NEXT: br label [[LOOP:%.*]]
212 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
214 ; CHECK-NEXT: ret i64 -1
219 %iv.ashr = phi i64 [-1023, %entry], [%iv.ashr.next, %loop]
220 %iv.ashr.next = ashr i64 %iv.ashr, 1
221 br i1 %c, label %exit, label %loop
223 %res = or i64 %iv.ashr, 1023
227 ; Same as previous, but swapped operands to phi
228 define i64 @test_ashr_ones2(i1 %c) {
229 ; CHECK-LABEL: @test_ashr_ones2(
231 ; CHECK-NEXT: br label [[LOOP:%.*]]
233 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
235 ; CHECK-NEXT: ret i64 -1
240 %iv.ashr = phi i64 [%iv.ashr.next, %loop], [-1023, %entry]
241 %iv.ashr.next = ashr i64 %iv.ashr, 1
242 br i1 %c, label %exit, label %loop
244 %res = or i64 %iv.ashr, 1023
249 ; negative case for when start is unknown
250 define i64 @test_ashr_unknown(i1 %c, i64 %start) {
251 ; CHECK-LABEL: @test_ashr_unknown(
253 ; CHECK-NEXT: br label [[LOOP:%.*]]
255 ; CHECK-NEXT: [[IV_ASHR:%.*]] = phi i64 [ [[START:%.*]], [[ENTRY:%.*]] ], [ [[IV_ASHR_NEXT:%.*]], [[LOOP]] ]
256 ; CHECK-NEXT: [[IV_ASHR_NEXT]] = ashr i64 [[IV_ASHR]], 1
257 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
259 ; CHECK-NEXT: [[RES:%.*]] = or i64 [[IV_ASHR]], 1023
260 ; CHECK-NEXT: ret i64 [[RES]]
265 %iv.ashr = phi i64 [%start, %entry], [%iv.ashr.next, %loop]
266 %iv.ashr.next = ashr i64 %iv.ashr, 1
267 br i1 %c, label %exit, label %loop
269 %res = or i64 %iv.ashr, 1023
273 ; Negative case where we don't have a (shift) recurrence because the operands
274 ; of the ashr are swapped. (This does end up being a divide recurrence.)
275 define i64 @test_ashr_wrong_op(i1 %c, i64 %start) {
276 ; CHECK-LABEL: @test_ashr_wrong_op(
278 ; CHECK-NEXT: br label [[LOOP:%.*]]
280 ; CHECK-NEXT: [[IV_ASHR:%.*]] = phi i64 [ [[START:%.*]], [[ENTRY:%.*]] ], [ [[IV_ASHR_NEXT:%.*]], [[LOOP]] ]
281 ; CHECK-NEXT: [[IV_ASHR_NEXT]] = lshr i64 1, [[IV_ASHR]]
282 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
284 ; CHECK-NEXT: [[RES:%.*]] = or i64 [[IV_ASHR]], 1023
285 ; CHECK-NEXT: ret i64 [[RES]]
290 %iv.ashr = phi i64 [%start, %entry], [%iv.ashr.next, %loop]
291 %iv.ashr.next = ashr i64 1, %iv.ashr
292 br i1 %c, label %exit, label %loop
294 %res = or i64 %iv.ashr, 1023
299 define i64 @test_shl(i1 %c) {
300 ; CHECK-LABEL: @test_shl(
302 ; CHECK-NEXT: br label [[LOOP:%.*]]
304 ; CHECK-NEXT: br i1 [[C:%.*]], label [[EXIT:%.*]], label [[LOOP]]
306 ; CHECK-NEXT: ret i64 0
311 %iv.shl = phi i64 [8, %entry], [%iv.shl.next, %loop]
312 %iv.shl.next = shl i64 %iv.shl, 1
313 br i1 %c, label %exit, label %loop
315 %res = and i64 %iv.shl, 7