1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2 ; RUN: opt < %s -passes=correlated-propagation -S | FileCheck %s
4 declare void @use(i8, i8)
6 define i8 @simple_phi(i1 %c, i8 %a, i8 %b) {
7 ; CHECK-LABEL: define i8 @simple_phi
8 ; CHECK-SAME: (i1 [[C:%.*]], i8 [[A:%.*]], i8 [[B:%.*]]) {
10 ; CHECK-NEXT: br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
12 ; CHECK-NEXT: ret i8 [[A]]
14 ; CHECK-NEXT: ret i8 [[B]]
17 %s = select i1 %c, i8 %a, i8 %b
18 br i1 %c, label %then, label %else
21 %phi1 = phi i8 [ %s, %entry ]
25 %phi2 = phi i8 [ %s, %entry ]
29 define i8 @phi_other_edge(i1 %c, i8 %a, i8 %b, i32 %sw) {
30 ; CHECK-LABEL: define i8 @phi_other_edge
31 ; CHECK-SAME: (i1 [[C:%.*]], i8 [[A:%.*]], i8 [[B:%.*]], i32 [[SW:%.*]]) {
33 ; CHECK-NEXT: switch i32 [[SW]], label [[TEST:%.*]] [
34 ; CHECK-NEXT: i32 0, label [[THEN:%.*]]
35 ; CHECK-NEXT: i32 1, label [[ELSE:%.*]]
38 ; CHECK-NEXT: br i1 [[C]], label [[THEN]], label [[ELSE]]
40 ; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ [[A]], [[TEST]] ], [ 1, [[ENTRY:%.*]] ]
41 ; CHECK-NEXT: ret i8 [[PHI1]]
43 ; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ [[B]], [[TEST]] ], [ 2, [[ENTRY]] ]
44 ; CHECK-NEXT: ret i8 [[PHI2]]
47 switch i32 %sw, label %test [
53 %s = select i1 %c, i8 %a, i8 %b
54 br i1 %c, label %then, label %else
57 %phi1 = phi i8 [ %s, %test ], [ 1, %entry ]
61 %phi2 = phi i8 [ %s, %test ], [ 2, %entry ]
65 define void @phi_loop(i32) {
66 ; CHECK-LABEL: define void @phi_loop
67 ; CHECK-SAME: (i32 [[TMP0:%.*]]) {
69 ; CHECK-NEXT: br label [[LOOP:%.*]]
71 ; CHECK-NEXT: [[IDX:%.*]] = phi i32 [ [[TMP0]], [[ENTRY:%.*]] ], [ [[TMP2:%.*]], [[LOOP]] ]
72 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[IDX]], 0
73 ; CHECK-NEXT: [[TMP2]] = add i32 [[IDX]], -1
74 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP2]]
75 ; CHECK-NEXT: br i1 [[TMP1]], label [[OUT:%.*]], label [[LOOP]]
77 ; CHECK-NEXT: ret void
83 %idx = phi i32 [ %0, %entry ], [ %sel, %loop ]
84 %1 = icmp eq i32 %idx, 0
86 %sel = select i1 %1, i32 0, i32 %2
87 br i1 %1, label %out, label %loop
93 define i8 @simple_non_phi(i1 %c, i8 %a, i8 %b) {
94 ; CHECK-LABEL: define i8 @simple_non_phi
95 ; CHECK-SAME: (i1 [[C:%.*]], i8 [[A:%.*]], i8 [[B:%.*]]) {
97 ; CHECK-NEXT: br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
99 ; CHECK-NEXT: ret i8 [[A]]
101 ; CHECK-NEXT: ret i8 [[B]]
104 %s = select i1 %c, i8 %a, i8 %b
105 br i1 %c, label %then, label %else
115 define void @simple_multiple_uses(i1 %c, i8 %a, i8 %b) {
116 ; CHECK-LABEL: define void @simple_multiple_uses
117 ; CHECK-SAME: (i1 [[C:%.*]], i8 [[A:%.*]], i8 [[B:%.*]]) {
119 ; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], i8 [[A]], i8 [[B]]
120 ; CHECK-NEXT: call void @use(i8 [[S]], i8 [[S]])
121 ; CHECK-NEXT: br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
123 ; CHECK-NEXT: call void @use(i8 [[A]], i8 [[A]])
124 ; CHECK-NEXT: ret void
126 ; CHECK-NEXT: call void @use(i8 [[B]], i8 [[B]])
127 ; CHECK-NEXT: ret void
130 %s = select i1 %c, i8 %a, i8 %b
131 call void @use(i8 %s, i8 %s)
132 br i1 %c, label %then, label %else
135 call void @use(i8 %s, i8 %s)
139 call void @use(i8 %s, i8 %s)
143 define i8 @not_correlated(i1, i1) {
144 ; CHECK-LABEL: define i8 @not_correlated
145 ; CHECK-SAME: (i1 [[TMP0:%.*]], i1 [[TMP1:%.*]]) {
147 ; CHECK-NEXT: [[S:%.*]] = select i1 [[TMP0]], i8 0, i8 1
148 ; CHECK-NEXT: br i1 [[TMP1]], label [[THEN:%.*]], label [[ELSE:%.*]]
150 ; CHECK-NEXT: ret i8 [[S]]
152 ; CHECK-NEXT: ret i8 [[S]]
155 %s = select i1 %0, i8 0, i8 1
156 br i1 %1, label %then, label %else
159 %a = phi i8 [ %s, %entry ]
163 %b = phi i8 [ %s, %entry ]
167 @c = global i32 0, align 4
168 @b = global i32 0, align 4
170 define i32 @PR23752() {
171 ; CHECK-LABEL: define i32 @PR23752() {
173 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
175 ; CHECK-NEXT: [[SEL:%.*]] = select i1 icmp sgt (ptr @b, ptr @c), i32 0, i32 1
176 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[SEL]], 1
177 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[IF_END:%.*]]
179 ; CHECK-NEXT: ret i32 1
185 %phi = phi i32 [ 0, %entry ], [ %sel, %for.body ]
186 %sel = select i1 icmp sgt (ptr @b, ptr @c), i32 %phi, i32 1
187 %cmp = icmp ne i32 %sel, 1
188 br i1 %cmp, label %for.body, label %if.end
195 define i1 @test1(ptr %p, i1 %unknown) {
196 ; CHECK-LABEL: define i1 @test1
197 ; CHECK-SAME: (ptr [[P:%.*]], i1 [[UNKNOWN:%.*]]) {
198 ; CHECK-NEXT: [[PVAL:%.*]] = load i32, ptr [[P]], align 4
199 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[PVAL]], 255
200 ; CHECK-NEXT: br i1 [[CMP1]], label [[NEXT:%.*]], label [[EXIT:%.*]]
202 ; CHECK-NEXT: [[MIN:%.*]] = select i1 [[UNKNOWN]], i32 [[PVAL]], i32 5
203 ; CHECK-NEXT: ret i1 false
205 ; CHECK-NEXT: ret i1 true
207 %pval = load i32, i32* %p
208 %cmp1 = icmp slt i32 %pval, 255
209 br i1 %cmp1, label %next, label %exit
212 %min = select i1 %unknown, i32 %pval, i32 5
213 %res = icmp eq i32 %min, 255
220 ; Check that we take a conservative meet
221 define i1 @test2(ptr %p, i32 %qval, i1 %unknown) {
222 ; CHECK-LABEL: define i1 @test2
223 ; CHECK-SAME: (ptr [[P:%.*]], i32 [[QVAL:%.*]], i1 [[UNKNOWN:%.*]]) {
224 ; CHECK-NEXT: [[PVAL:%.*]] = load i32, ptr [[P]], align 4
225 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[PVAL]], 255
226 ; CHECK-NEXT: br i1 [[CMP1]], label [[NEXT:%.*]], label [[EXIT:%.*]]
228 ; CHECK-NEXT: [[MIN:%.*]] = select i1 [[UNKNOWN]], i32 [[PVAL]], i32 [[QVAL]]
229 ; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[MIN]], 255
230 ; CHECK-NEXT: ret i1 [[RES]]
232 ; CHECK-NEXT: ret i1 true
234 %pval = load i32, i32* %p
235 %cmp1 = icmp slt i32 %pval, 255
236 br i1 %cmp1, label %next, label %exit
239 %min = select i1 %unknown, i32 %pval, i32 %qval
240 %res = icmp eq i32 %min, 255
247 ; Same as @test2, but for the opposite select input
248 define i1 @test3(ptr %p, i32 %qval, i1 %unknown) {
249 ; CHECK-LABEL: define i1 @test3
250 ; CHECK-SAME: (ptr [[P:%.*]], i32 [[QVAL:%.*]], i1 [[UNKNOWN:%.*]]) {
251 ; CHECK-NEXT: [[PVAL:%.*]] = load i32, ptr [[P]], align 4
252 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[PVAL]], 255
253 ; CHECK-NEXT: br i1 [[CMP1]], label [[NEXT:%.*]], label [[EXIT:%.*]]
255 ; CHECK-NEXT: [[MIN:%.*]] = select i1 [[UNKNOWN]], i32 [[QVAL]], i32 [[PVAL]]
256 ; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[MIN]], 255
257 ; CHECK-NEXT: ret i1 [[RES]]
259 ; CHECK-NEXT: ret i1 true
261 %pval = load i32, i32* %p
262 %cmp1 = icmp slt i32 %pval, 255
263 br i1 %cmp1, label %next, label %exit
266 %min = select i1 %unknown, i32 %qval, i32 %pval
267 %res = icmp eq i32 %min, 255
274 ; Conflicting constants (i.e. isOverdefined result)
275 ; NOTE: Using doubles in this version is a bit of a hack. This
276 ; is to get around the fact that all integers (including constants
277 ; and non-constants) are actually represented as constant-ranges.
278 define i1 @test4(ptr %p, i32 %qval, i1 %unknown) {
279 ; CHECK-LABEL: define i1 @test4
280 ; CHECK-SAME: (ptr [[P:%.*]], i32 [[QVAL:%.*]], i1 [[UNKNOWN:%.*]]) {
281 ; CHECK-NEXT: [[PVAL:%.*]] = load i32, ptr [[P]], align 4
282 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[PVAL]], 255
283 ; CHECK-NEXT: br i1 [[CMP1]], label [[NEXT:%.*]], label [[EXIT:%.*]]
285 ; CHECK-NEXT: [[MIN:%.*]] = select i1 [[UNKNOWN]], double 1.000000e+00, double 0.000000e+00
286 ; CHECK-NEXT: [[RES:%.*]] = fcmp oeq double [[MIN]], 3.000000e+02
287 ; CHECK-NEXT: ret i1 [[RES]]
289 ; CHECK-NEXT: ret i1 true
291 %pval = load i32, i32* %p
292 %cmp1 = icmp slt i32 %pval, 255
293 br i1 %cmp1, label %next, label %exit
296 %min = select i1 %unknown, double 1.0, double 0.0
297 %res = fcmp oeq double %min, 300.0
304 ;; Using the condition to clamp the result
307 define i1 @test5(ptr %p, i1 %unknown) {
308 ; CHECK-LABEL: define i1 @test5
309 ; CHECK-SAME: (ptr [[P:%.*]], i1 [[UNKNOWN:%.*]]) {
310 ; CHECK-NEXT: [[PVAL:%.*]] = load i32, ptr [[P]], align 4, !noundef [[META0:![0-9]+]]
311 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[PVAL]], 255
312 ; CHECK-NEXT: br i1 [[CMP1]], label [[NEXT:%.*]], label [[EXIT:%.*]]
314 ; CHECK-NEXT: [[COND:%.*]] = icmp sgt i32 [[PVAL]], 0
315 ; CHECK-NEXT: [[MIN:%.*]] = select i1 [[COND]], i32 [[PVAL]], i32 5
316 ; CHECK-NEXT: ret i1 false
318 ; CHECK-NEXT: ret i1 true
320 %pval = load i32, i32* %p, !noundef !0
321 %cmp1 = icmp slt i32 %pval, 255
322 br i1 %cmp1, label %next, label %exit
325 %cond = icmp sgt i32 %pval, 0
326 %min = select i1 %cond, i32 %pval, i32 5
327 %res = icmp eq i32 %min, -1
334 define i1 @test6(ptr %p, i1 %unknown) {
335 ; CHECK-LABEL: define i1 @test6
336 ; CHECK-SAME: (ptr [[P:%.*]], i1 [[UNKNOWN:%.*]]) {
337 ; CHECK-NEXT: [[PVAL:%.*]] = load i32, ptr [[P]], align 4, !noundef [[META0]]
338 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i32 [[PVAL]], 255
339 ; CHECK-NEXT: br i1 [[CMP1]], label [[NEXT:%.*]], label [[EXIT:%.*]]
341 ; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[PVAL]], 254
342 ; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i32 [[PVAL]], i32 1
343 ; CHECK-NEXT: ret i1 true
345 ; CHECK-NEXT: ret i1 true
347 %pval = load i32, i32* %p, !noundef !0
348 %cmp1 = icmp ult i32 %pval, 255
349 br i1 %cmp1, label %next, label %exit
352 %cond = icmp ne i32 %pval, 254
353 %sel = select i1 %cond, i32 %pval, i32 1
354 %res = icmp slt i32 %sel, 254
361 define i64 @select_cond_may_undef(i32 %a) {
362 ; CHECK-LABEL: define i64 @select_cond_may_undef
363 ; CHECK-SAME: (i32 [[A:%.*]]) {
364 ; CHECK-NEXT: [[IS_A_NONNEGATIVE:%.*]] = icmp sgt i32 [[A]], 1
365 ; CHECK-NEXT: [[NARROW:%.*]] = select i1 [[IS_A_NONNEGATIVE]], i32 [[A]], i32 0
366 ; CHECK-NEXT: [[MAX:%.*]] = sext i32 [[NARROW]] to i64
367 ; CHECK-NEXT: ret i64 [[MAX]]
369 %is_a_nonnegative = icmp sgt i32 %a, 1
370 %narrow = select i1 %is_a_nonnegative, i32 %a, i32 0
371 %max = sext i32 %narrow to i64