1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=instcombine < %s | FileCheck %s
5 ; Tests to show cases where computeKnownBits should be able to determine
6 ; the known bits of a phi based on limited recursion.
9 declare i64 @llvm.ctpop.i64(i64)
12 define i32 @single_entry_phi(i64 %x, i1 %c) {
13 ; CHECK-LABEL: @single_entry_phi(
15 ; CHECK-NEXT: br label [[BODY:%.*]]
17 ; CHECK-NEXT: br i1 [[C:%.*]], label [[END:%.*]], label [[BODY]]
19 ; CHECK-NEXT: [[Y:%.*]] = call range(i64 0, 65) i64 @llvm.ctpop.i64(i64 [[X:%.*]])
20 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw nsw i64 [[Y]] to i32
21 ; CHECK-NEXT: ret i32 [[TRUNC]]
24 %y = call i64 @llvm.ctpop.i64(i64 %x)
25 %trunc = trunc i64 %y to i32
28 br i1 %c, label %end, label %body
30 %phi = phi i32 [ %trunc, %body ]
31 %res = and i32 %phi, 127
36 define i32 @two_entry_phi_with_constant(i64 %x, i1 %c) {
37 ; CHECK-LABEL: @two_entry_phi_with_constant(
39 ; CHECK-NEXT: [[Y:%.*]] = call range(i64 0, 65) i64 @llvm.ctpop.i64(i64 [[X:%.*]])
40 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw nsw i64 [[Y]] to i32
41 ; CHECK-NEXT: br i1 [[C:%.*]], label [[END:%.*]], label [[BODY:%.*]]
43 ; CHECK-NEXT: br label [[END]]
45 ; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[TRUNC]], [[ENTRY:%.*]] ], [ 255, [[BODY]] ]
46 ; CHECK-NEXT: [[RES:%.*]] = and i32 [[PHI]], 255
47 ; CHECK-NEXT: ret i32 [[RES]]
50 %y = call i64 @llvm.ctpop.i64(i64 %x)
51 %trunc = trunc i64 %y to i32
52 br i1 %c, label %end, label %body
56 %phi = phi i32 [ %trunc, %entry ], [ 255, %body ]
57 %res = and i32 %phi, 255
61 define i32 @two_entry_phi_non_constant(i64 %x, i64 %x2, i1 %c) {
62 ; CHECK-LABEL: @two_entry_phi_non_constant(
64 ; CHECK-NEXT: [[Y:%.*]] = call range(i64 0, 65) i64 @llvm.ctpop.i64(i64 [[X:%.*]])
65 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw nsw i64 [[Y]] to i32
66 ; CHECK-NEXT: br i1 [[C:%.*]], label [[END:%.*]], label [[BODY:%.*]]
68 ; CHECK-NEXT: [[Y2:%.*]] = call range(i64 0, 65) i64 @llvm.ctpop.i64(i64 [[X2:%.*]])
69 ; CHECK-NEXT: [[TRUNC2:%.*]] = trunc nuw nsw i64 [[Y2]] to i32
70 ; CHECK-NEXT: br label [[END]]
72 ; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[TRUNC]], [[ENTRY:%.*]] ], [ [[TRUNC2]], [[BODY]] ]
73 ; CHECK-NEXT: [[RES:%.*]] = and i32 [[PHI]], 255
74 ; CHECK-NEXT: ret i32 [[RES]]
77 %y = call i64 @llvm.ctpop.i64(i64 %x)
78 %trunc = trunc i64 %y to i32
79 br i1 %c, label %end, label %body
81 %y2 = call i64 @llvm.ctpop.i64(i64 %x2)
82 %trunc2 = trunc i64 %y2 to i32
85 %phi = phi i32 [ %trunc, %entry ], [ %trunc2, %body ]
86 %res = and i32 %phi, 255
90 define i32 @neg_many_branches(i64 %x) {
91 ; CHECK-LABEL: @neg_many_branches(
93 ; CHECK-NEXT: [[Y:%.*]] = call range(i64 0, 65) i64 @llvm.ctpop.i64(i64 [[X:%.*]])
94 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw nsw i64 [[Y]] to i32
95 ; CHECK-NEXT: switch i32 [[TRUNC]], label [[END:%.*]] [
96 ; CHECK-NEXT: i32 1, label [[ONE:%.*]]
97 ; CHECK-NEXT: i32 2, label [[TWO:%.*]]
98 ; CHECK-NEXT: i32 3, label [[THREE:%.*]]
99 ; CHECK-NEXT: i32 4, label [[FOUR:%.*]]
102 ; CHECK-NEXT: [[A:%.*]] = add nuw nsw i32 [[TRUNC]], 1
103 ; CHECK-NEXT: br label [[END]]
105 ; CHECK-NEXT: [[B:%.*]] = add nuw nsw i32 [[TRUNC]], 2
106 ; CHECK-NEXT: br label [[END]]
108 ; CHECK-NEXT: [[C:%.*]] = add nuw nsw i32 [[TRUNC]], 3
109 ; CHECK-NEXT: br label [[END]]
111 ; CHECK-NEXT: [[D:%.*]] = add nuw nsw i32 [[TRUNC]], 4
112 ; CHECK-NEXT: br label [[END]]
114 ; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[TRUNC]], [[ENTRY:%.*]] ], [ [[A]], [[ONE]] ], [ [[B]], [[TWO]] ], [ [[C]], [[THREE]] ], [ [[D]], [[FOUR]] ]
115 ; CHECK-NEXT: [[RES:%.*]] = and i32 [[PHI]], 255
116 ; CHECK-NEXT: ret i32 [[RES]]
119 %y = call i64 @llvm.ctpop.i64(i64 %x)
120 %trunc = trunc i64 %y to i32
121 switch i32 %trunc, label %end [
128 %a = add i32 %trunc, 1
131 %b = add i32 %trunc, 2
134 %c = add i32 %trunc, 3
137 %d = add i32 %trunc, 4
140 %phi = phi i32 [ %trunc, %entry ], [ %a, %one ], [ %b, %two ], [ %c, %three ], [ %d, %four ]
141 %res = and i32 %phi, 255
145 define i32 @knownbits_phi_select_test1(ptr %p1, ptr %p2, i8 %x) {
146 ; CHECK-LABEL: @knownbits_phi_select_test1(
148 ; CHECK-NEXT: br label [[LOOP:%.*]]
150 ; CHECK-NEXT: [[INDVAR1:%.*]] = phi i8 [ [[LOAD2:%.*]], [[BB2:%.*]] ], [ [[X:%.*]], [[ENTRY:%.*]] ]
151 ; CHECK-NEXT: [[INDVAR3:%.*]] = phi ptr [ [[INDVAR3_NEXT:%.*]], [[BB2]] ], [ [[P1:%.*]], [[ENTRY]] ]
152 ; CHECK-NEXT: [[INDVAR4:%.*]] = phi i32 [ [[INDVAR4_NEXT:%.*]], [[BB2]] ], [ 0, [[ENTRY]] ]
153 ; CHECK-NEXT: [[INDVAR5:%.*]] = phi i32 [ [[INDVAR5_NEXT:%.*]], [[BB2]] ], [ 0, [[ENTRY]] ]
154 ; CHECK-NEXT: switch i8 [[INDVAR1]], label [[DEFAULT:%.*]] [
155 ; CHECK-NEXT: i8 0, label [[EXIT:%.*]]
156 ; CHECK-NEXT: i8 59, label [[BB1:%.*]]
157 ; CHECK-NEXT: i8 35, label [[BB1]]
160 ; CHECK-NEXT: [[EXT:%.*]] = sext i8 [[INDVAR1]] to i64
161 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i16, ptr [[P2:%.*]], i64 [[EXT]]
162 ; CHECK-NEXT: [[LOAD1:%.*]] = load i16, ptr [[GEP1]], align 2
163 ; CHECK-NEXT: [[MASK:%.*]] = and i16 [[LOAD1]], 8192
164 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i16 [[MASK]], 0
165 ; CHECK-NEXT: br i1 [[CMP1]], label [[BB2]], label [[BB1]]
167 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[INDVAR4]], 0
168 ; CHECK-NEXT: [[CMP3:%.*]] = icmp ne i32 [[INDVAR5]], 0
169 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[CMP2]], i1 true, i1 [[CMP3]]
170 ; CHECK-NEXT: br i1 [[OR_COND]], label [[BB2]], label [[EXIT]]
172 ; CHECK-NEXT: [[CMP4:%.*]] = icmp eq i8 [[INDVAR1]], 39
173 ; CHECK-NEXT: [[EXT2:%.*]] = zext i1 [[CMP4]] to i32
174 ; CHECK-NEXT: [[INDVAR4_NEXT]] = xor i32 [[INDVAR4]], [[EXT2]]
175 ; CHECK-NEXT: [[CMP6:%.*]] = icmp eq i8 [[INDVAR1]], 34
176 ; CHECK-NEXT: [[EXT3:%.*]] = zext i1 [[CMP6]] to i32
177 ; CHECK-NEXT: [[INDVAR5_NEXT]] = xor i32 [[INDVAR5]], [[EXT3]]
178 ; CHECK-NEXT: [[INDVAR3_NEXT]] = getelementptr inbounds i8, ptr [[INDVAR3]], i64 1
179 ; CHECK-NEXT: [[LOAD2]] = load i8, ptr [[INDVAR3_NEXT]], align 1
180 ; CHECK-NEXT: br label [[LOOP]]
182 ; CHECK-NEXT: ret i32 [[INDVAR5]]
188 %indvar1 = phi i8 [ %load2, %bb2 ], [ %x, %entry ]
189 %indvar2 = phi i64 [ %indvar2.next, %bb2 ], [ 0, %entry ]
190 %indvar3 = phi ptr [ %indvar3.next, %bb2 ], [ %p1, %entry ]
191 %indvar4 = phi i32 [ %indvar4.next, %bb2 ], [ 0, %entry ]
192 %indvar5 = phi i32 [ %indvar5.next, %bb2 ], [ 0, %entry ]
193 switch i8 %indvar1, label %default [
200 %ext = sext i8 %indvar1 to i64
201 %gep1 = getelementptr inbounds i16, ptr %p2, i64 %ext
202 %load1 = load i16, ptr %gep1, align 2
203 %mask = and i16 %load1, 8192
204 %cmp1 = icmp eq i16 %mask, 0
205 br i1 %cmp1, label %bb2, label %bb1
208 %cmp2 = icmp ne i32 %indvar4, 0
209 %cmp3 = icmp ne i32 %indvar5, 0
210 %or.cond = select i1 %cmp2, i1 true, i1 %cmp3
211 br i1 %or.cond, label %bb2, label %exit
214 %cmp4 = icmp eq i8 %indvar1, 39
215 %cmp5 = icmp eq i32 %indvar4, 0
216 %ext2 = zext i1 %cmp5 to i32
217 %indvar4.next = select i1 %cmp4, i32 %ext2, i32 %indvar4
218 %cmp6 = icmp eq i8 %indvar1, 34
219 %cmp7 = icmp eq i32 %indvar5, 0
220 %ext3 = zext i1 %cmp7 to i32
221 %indvar5.next = select i1 %cmp6, i32 %ext3, i32 %indvar5
222 %indvar3.next = getelementptr inbounds i8, ptr %indvar3, i64 1
223 %indvar2.next = add i64 %indvar2, 1
224 %load2 = load i8, ptr %indvar3.next, align 1
231 define i8 @knownbits_phi_select_test2() {
232 ; CHECK-LABEL: @knownbits_phi_select_test2(
234 ; CHECK-NEXT: br label [[LOOP:%.*]]
236 ; CHECK-NEXT: [[INDVAR:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[CONTAIN:%.*]], [[LOOP]] ]
237 ; CHECK-NEXT: [[COND0:%.*]] = call i1 @cond()
238 ; CHECK-NEXT: [[CONTAIN]] = select i1 [[COND0]], i8 1, i8 [[INDVAR]]
239 ; CHECK-NEXT: [[COND1:%.*]] = call i1 @cond()
240 ; CHECK-NEXT: br i1 [[COND1]], label [[EXIT:%.*]], label [[LOOP]]
242 ; CHECK-NEXT: ret i8 [[CONTAIN]]
248 %indvar = phi i8 [ 0, %entry ], [ %contain, %loop ]
249 %cond0 = call i1 @cond()
250 %contain = select i1 %cond0, i8 1, i8 %indvar
251 %cond1 = call i1 @cond()
252 br i1 %cond1, label %exit, label %loop
255 %bool = and i8 %contain, 1