1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; https://bugs.llvm.org/show_bug.cgi?id=38123
8 ; Should be transformed into:
10 ; Iff: isPowerOf2(C + 1)
12 ; ============================================================================ ;
13 ; Basic positive tests
14 ; ============================================================================ ;
16 define i1 @p0(i8 %x) {
18 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i8 [[X:%.*]], 3
19 ; CHECK-NEXT: ret i1 [[TMP1]]
22 %ret = icmp slt i8 %tmp0, %x
26 ; ============================================================================ ;
28 ; ============================================================================ ;
30 define <2 x i1> @p1_vec_splat(<2 x i8> %x) {
31 ; CHECK-LABEL: @p1_vec_splat(
32 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 3, i8 3>
33 ; CHECK-NEXT: ret <2 x i1> [[TMP1]]
35 %tmp0 = and <2 x i8> %x, <i8 3, i8 3>
36 %ret = icmp slt <2 x i8> %tmp0, %x
40 define <2 x i1> @p2_vec_nonsplat(<2 x i8> %x) {
41 ; CHECK-LABEL: @p2_vec_nonsplat(
42 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 3, i8 15>
43 ; CHECK-NEXT: ret <2 x i1> [[TMP1]]
45 %tmp0 = and <2 x i8> %x, <i8 3, i8 15> ; doesn't have to be splat.
46 %ret = icmp slt <2 x i8> %tmp0, %x
50 define <3 x i1> @p3_vec_splat_undef(<3 x i8> %x) {
51 ; CHECK-LABEL: @p3_vec_splat_undef(
52 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <3 x i8> [[X:%.*]], <i8 3, i8 undef, i8 3>
53 ; CHECK-NEXT: ret <3 x i1> [[TMP1]]
55 %tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 3>
56 %ret = icmp slt <3 x i8> %tmp0, %x
60 ; ============================================================================ ;
61 ; One-use tests. We don't care about multi-uses here.
62 ; ============================================================================ ;
64 declare void @use8(i8)
66 define i1 @oneuse0(i8 %x) {
67 ; CHECK-LABEL: @oneuse0(
68 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X:%.*]], 3
69 ; CHECK-NEXT: call void @use8(i8 [[TMP0]])
70 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i8 [[X]], 3
71 ; CHECK-NEXT: ret i1 [[TMP1]]
74 call void @use8(i8 %tmp0)
75 %ret = icmp slt i8 %tmp0, %x
79 ; ============================================================================ ;
81 ; ============================================================================ ;
83 ; ============================================================================ ;
84 ; Commutativity tests.
85 ; ============================================================================ ;
91 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
92 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X]], 3
93 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[X]], [[TMP0]]
94 ; CHECK-NEXT: ret i1 [[RET]]
98 %ret = icmp slt i8 %x, %tmp0 ; swapped order
102 ; ============================================================================ ;
103 ; Commutativity tests with variable
104 ; ============================================================================ ;
106 ; Ok, this one should fold. We only testing commutativity of 'and'.
107 define i1 @cv0(i8 %y) {
109 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
110 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
111 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], [[TMP0]]
112 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[TMP1]], [[X]]
113 ; CHECK-NEXT: ret i1 [[RET]]
116 %tmp0 = lshr i8 -1, %y
117 %tmp1 = and i8 %x, %tmp0 ; swapped order
118 %ret = icmp slt i8 %tmp1, %x
122 define i1 @cv1(i8 %y) {
124 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
125 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
126 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X]]
127 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[X]], [[TMP1]]
128 ; CHECK-NEXT: ret i1 [[RET]]
131 %tmp0 = lshr i8 -1, %y
132 %tmp1 = and i8 %tmp0, %x
133 %ret = icmp slt i8 %x, %tmp1 ; swapped order
137 define i1 @cv2(i8 %y) {
139 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
140 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
141 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], [[TMP0]]
142 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[X]], [[TMP1]]
143 ; CHECK-NEXT: ret i1 [[RET]]
146 %tmp0 = lshr i8 -1, %y
147 %tmp1 = and i8 %x, %tmp0 ; swapped order
148 %ret = icmp slt i8 %x, %tmp1 ; swapped order
152 ; ============================================================================ ;
153 ; Normal negative tests
154 ; ============================================================================ ;
156 define i1 @n0(i8 %x) {
158 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X:%.*]], 4
159 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[TMP0]], [[X]]
160 ; CHECK-NEXT: ret i1 [[RET]]
162 %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
163 %ret = icmp slt i8 %tmp0, %x
167 define i1 @n1(i8 %x, i8 %y, i8 %notx) {
169 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X:%.*]], 3
170 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[TMP0]], [[NOTX:%.*]]
171 ; CHECK-NEXT: ret i1 [[RET]]
174 %ret = icmp slt i8 %tmp0, %notx ; not %x
178 define <2 x i1> @n2(<2 x i8> %x) {
180 ; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X:%.*]], <i8 3, i8 16>
181 ; CHECK-NEXT: [[RET:%.*]] = icmp slt <2 x i8> [[TMP0]], [[X]]
182 ; CHECK-NEXT: ret <2 x i1> [[RET]]
184 %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.
185 %ret = icmp slt <2 x i8> %tmp0, %x
189 ; ============================================================================ ;
190 ; Potential miscompiles.
191 ; ============================================================================ ;
193 define i1 @nv(i8 %x, i8 %y) {
195 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
196 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X:%.*]]
197 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[TMP1]], [[X]]
198 ; CHECK-NEXT: ret i1 [[RET]]
200 %tmp0 = lshr i8 -1, %y
201 %tmp1 = and i8 %tmp0, %x
202 %ret = icmp slt i8 %tmp1, %x
206 define <2 x i1> @n3(<2 x i8> %x) {
208 ; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X:%.*]], <i8 3, i8 -1>
209 ; CHECK-NEXT: [[RET:%.*]] = icmp slt <2 x i8> [[TMP0]], [[X]]
210 ; CHECK-NEXT: ret <2 x i1> [[RET]]
212 %tmp0 = and <2 x i8> %x, <i8 3, i8 -1>
213 %ret = icmp slt <2 x i8> %tmp0, %x
217 define <3 x i1> @n4(<3 x i8> %x) {
219 ; CHECK-NEXT: [[TMP0:%.*]] = and <3 x i8> [[X:%.*]], <i8 3, i8 undef, i8 -1>
220 ; CHECK-NEXT: [[RET:%.*]] = icmp slt <3 x i8> [[TMP0]], [[X]]
221 ; CHECK-NEXT: ret <3 x i1> [[RET]]
223 %tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 -1>
224 %ret = icmp slt <3 x i8> %tmp0, %x