1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 declare i64 @llvm.cttz.i64(i64, i1)
6 declare i64 @llvm.ctlz.i64(i64, i1)
8 declare <8 x i64> @llvm.cttz.v8i64(<8 x i64>, i1)
10 define i32 @test0(i64 %x) {
11 ; CHECK-LABEL: @test0(
13 ; CHECK-NEXT: [[C:%.*]] = icmp eq i64 [[X:%.*]], 0
14 ; CHECK-NEXT: br i1 [[C]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
16 ; CHECK-NEXT: [[CTZ:%.*]] = call range(i64 0, 65) i64 @llvm.cttz.i64(i64 [[X]], i1 true)
17 ; CHECK-NEXT: [[CTZ32:%.*]] = trunc nuw nsw i64 [[CTZ]] to i32
18 ; CHECK-NEXT: br label [[EXIT]]
20 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[CTZ32]], [[NON_ZERO]] ], [ 0, [[START:%.*]] ]
21 ; CHECK-NEXT: ret i32 [[RES]]
24 %c = icmp eq i64 %x, 0
25 br i1 %c, label %exit, label %non_zero
28 %ctz = call i64 @llvm.cttz.i64(i64 %x, i1 false)
29 %ctz32 = trunc i64 %ctz to i32
33 %res = phi i32 [ %ctz32, %non_zero ], [ 0, %start ]
37 define i32 @test1(i64 %x) {
38 ; CHECK-LABEL: @test1(
40 ; CHECK-NEXT: [[C:%.*]] = icmp eq i64 [[X:%.*]], 0
41 ; CHECK-NEXT: br i1 [[C]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
43 ; CHECK-NEXT: [[CTZ:%.*]] = call range(i64 0, 65) i64 @llvm.ctlz.i64(i64 [[X]], i1 true)
44 ; CHECK-NEXT: [[CTZ32:%.*]] = trunc nuw nsw i64 [[CTZ]] to i32
45 ; CHECK-NEXT: br label [[EXIT]]
47 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ [[CTZ32]], [[NON_ZERO]] ], [ 0, [[START:%.*]] ]
48 ; CHECK-NEXT: ret i32 [[RES]]
51 %c = icmp eq i64 %x, 0
52 br i1 %c, label %exit, label %non_zero
55 %ctz = call i64 @llvm.ctlz.i64(i64 %x, i1 false)
56 %ctz32 = trunc i64 %ctz to i32
60 %res = phi i32 [ %ctz32, %non_zero ], [ 0, %start ]
64 define <8 x i64> @test2(<8 x i64> %x) {
65 ; CHECK-LABEL: @test2(
67 ; CHECK-NEXT: [[A:%.*]] = icmp eq <8 x i64> [[X:%.*]], zeroinitializer
68 ; CHECK-NEXT: [[B:%.*]] = bitcast <8 x i1> [[A]] to i8
69 ; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[B]], 0
70 ; CHECK-NEXT: br i1 [[C]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
72 ; CHECK-NEXT: [[CTZ:%.*]] = call range(i64 0, 65) <8 x i64> @llvm.cttz.v8i64(<8 x i64> [[X]], i1 false)
73 ; CHECK-NEXT: br label [[EXIT]]
75 ; CHECK-NEXT: [[RES:%.*]] = phi <8 x i64> [ [[CTZ]], [[NON_ZERO]] ], [ zeroinitializer, [[START:%.*]] ]
76 ; CHECK-NEXT: ret <8 x i64> [[RES]]
79 %a = icmp eq <8 x i64> %x, zeroinitializer
80 %b = bitcast <8 x i1> %a to i8
82 br i1 %c, label %exit, label %non_zero
85 ; NB: We cannot determine that vectors are known to be zero based
86 ; on the dominating condition
87 %ctz = call <8 x i64> @llvm.cttz.v8i64(<8 x i64> %x, i1 false)
91 %res = phi <8 x i64> [ %ctz, %non_zero ], [ zeroinitializer, %start ]
95 ; Test that exposed a bug in the PHI handling after D60846.
96 ; Check that folding happens correctly.
97 define void @D60846_miscompile(ptr %p) {
98 ; CHECK-LABEL: @D60846_miscompile(
100 ; CHECK-NEXT: br label [[LOOP:%.*]]
102 ; CHECK-NEXT: [[I:%.*]] = phi i16 [ 0, [[ENTRY:%.*]] ], [ [[I_INC:%.*]], [[COMMON:%.*]] ]
103 ; CHECK-NEXT: [[IS_ZERO:%.*]] = icmp eq i16 [[I]], 0
104 ; CHECK-NEXT: br i1 [[IS_ZERO]], label [[COMMON]], label [[NON_ZERO:%.*]]
106 ; CHECK-NEXT: store i1 true, ptr [[P:%.*]], align 1
107 ; CHECK-NEXT: br label [[COMMON]]
109 ; CHECK-NEXT: [[I_INC]] = add nuw nsw i16 [[I]], 1
110 ; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp eq i16 [[I]], 0
111 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
113 ; CHECK-NEXT: ret void
118 loop: ; preds = %common, %entry
119 %i = phi i16 [ 0, %entry ], [ %i.inc, %common ]
120 %is_zero = icmp eq i16 %i, 0
121 br i1 %is_zero, label %common, label %non_zero
123 non_zero: ; preds = %loop
124 %is_one = icmp eq i16 %i, 1
125 store i1 %is_one, ptr %p
128 common: ; preds = %non_zero, %loop
129 %i.inc = add i16 %i, 1
130 %loop_cond = icmp ult i16 %i.inc, 2
131 br i1 %loop_cond, label %loop, label %exit
133 exit: ; preds = %common
137 define i64 @test_sgt_zero(i64 %x) {
138 ; CHECK-LABEL: @test_sgt_zero(
140 ; CHECK-NEXT: [[C:%.*]] = icmp sgt i64 [[X:%.*]], 0
141 ; CHECK-NEXT: br i1 [[C]], label [[NON_ZERO:%.*]], label [[EXIT:%.*]]
143 ; CHECK-NEXT: [[CTZ:%.*]] = call range(i64 1, 65) i64 @llvm.ctlz.i64(i64 [[X]], i1 true)
144 ; CHECK-NEXT: ret i64 [[CTZ]]
146 ; CHECK-NEXT: ret i64 -1
149 %c = icmp sgt i64 %x, 0
150 br i1 %c, label %non_zero, label %exit
153 %ctz = call i64 @llvm.ctlz.i64(i64 %x, i1 false)
160 define i64 @test_slt_neg_ten(i64 %x) {
161 ; CHECK-LABEL: @test_slt_neg_ten(
163 ; CHECK-NEXT: [[C:%.*]] = icmp slt i64 [[X:%.*]], -10
164 ; CHECK-NEXT: br i1 [[C]], label [[NON_ZERO:%.*]], label [[EXIT:%.*]]
166 ; CHECK-NEXT: ret i64 0
168 ; CHECK-NEXT: ret i64 -1
171 %c = icmp slt i64 %x, -10
172 br i1 %c, label %non_zero, label %exit
175 %ctz = call i64 @llvm.ctlz.i64(i64 %x, i1 false)
182 define i64 @test_slt_ten(i64 %x) {
183 ; CHECK-LABEL: @test_slt_ten(
185 ; CHECK-NEXT: [[C:%.*]] = icmp slt i64 [[X:%.*]], 10
186 ; CHECK-NEXT: br i1 [[C]], label [[MAYBE_ZERO:%.*]], label [[EXIT:%.*]]
188 ; CHECK-NEXT: [[CTZ:%.*]] = call range(i64 0, 65) i64 @llvm.ctlz.i64(i64 [[X]], i1 false)
189 ; CHECK-NEXT: ret i64 [[CTZ]]
191 ; CHECK-NEXT: ret i64 -1
194 %c = icmp slt i64 %x, 10
195 br i1 %c, label %maybe_zero, label %exit
198 %ctz = call i64 @llvm.ctlz.i64(i64 %x, i1 false)
205 define i64 @test_ugt_unknown(i64 %x, i64 %y) {
206 ; CHECK-LABEL: @test_ugt_unknown(
208 ; CHECK-NEXT: [[C:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
209 ; CHECK-NEXT: br i1 [[C]], label [[NON_ZERO:%.*]], label [[EXIT:%.*]]
211 ; CHECK-NEXT: [[CTZ:%.*]] = call range(i64 0, 65) i64 @llvm.ctlz.i64(i64 [[X]], i1 true)
212 ; CHECK-NEXT: ret i64 [[CTZ]]
214 ; CHECK-NEXT: ret i64 -1
217 %c = icmp ugt i64 %x, %y
218 br i1 %c, label %non_zero, label %exit
221 %ctz = call i64 @llvm.ctlz.i64(i64 %x, i1 false)
228 define i64 @test_sle_zero(i64 %x) {
229 ; CHECK-LABEL: @test_sle_zero(
231 ; CHECK-NEXT: [[C:%.*]] = icmp slt i64 [[X:%.*]], 1
232 ; CHECK-NEXT: br i1 [[C]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
234 ; CHECK-NEXT: [[CTZ:%.*]] = call range(i64 1, 65) i64 @llvm.ctlz.i64(i64 [[X]], i1 true)
235 ; CHECK-NEXT: ret i64 [[CTZ]]
237 ; CHECK-NEXT: ret i64 -1
240 %c = icmp sle i64 %x, 0
241 br i1 %c, label %exit, label %non_zero
244 %ctz = call i64 @llvm.ctlz.i64(i64 %x, i1 false)
251 define i64 @test_sge_neg_ten(i64 %x) {
252 ; CHECK-LABEL: @test_sge_neg_ten(
254 ; CHECK-NEXT: [[C:%.*]] = icmp sgt i64 [[X:%.*]], -11
255 ; CHECK-NEXT: br i1 [[C]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
257 ; CHECK-NEXT: ret i64 0
259 ; CHECK-NEXT: ret i64 -1
262 %c = icmp sge i64 %x, -10
263 br i1 %c, label %exit, label %non_zero
266 %ctz = call i64 @llvm.ctlz.i64(i64 %x, i1 false)
273 define i64 @test_sge_ten(i64 %x) {
274 ; CHECK-LABEL: @test_sge_ten(
276 ; CHECK-NEXT: [[C:%.*]] = icmp sgt i64 [[X:%.*]], 9
277 ; CHECK-NEXT: br i1 [[C]], label [[EXIT:%.*]], label [[MAYBE_ZERO:%.*]]
279 ; CHECK-NEXT: [[CTZ:%.*]] = call range(i64 0, 65) i64 @llvm.ctlz.i64(i64 [[X]], i1 false)
280 ; CHECK-NEXT: ret i64 [[CTZ]]
282 ; CHECK-NEXT: ret i64 -1
285 %c = icmp sge i64 %x, 10
286 br i1 %c, label %exit, label %maybe_zero
289 %ctz = call i64 @llvm.ctlz.i64(i64 %x, i1 false)
296 define i64 @test_ule_unknown(i64 %x, i64 %y) {
297 ; CHECK-LABEL: @test_ule_unknown(
299 ; CHECK-NEXT: [[C_NOT:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
300 ; CHECK-NEXT: br i1 [[C_NOT]], label [[NON_ZERO:%.*]], label [[EXIT:%.*]]
302 ; CHECK-NEXT: [[CTZ:%.*]] = call range(i64 0, 65) i64 @llvm.ctlz.i64(i64 [[X]], i1 true)
303 ; CHECK-NEXT: ret i64 [[CTZ]]
305 ; CHECK-NEXT: ret i64 -1
308 %c = icmp ule i64 %x, %y
309 br i1 %c, label %exit, label %non_zero
312 %ctz = call i64 @llvm.ctlz.i64(i64 %x, i1 false)