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