[InstCombine] Signed saturation patterns
[llvm-core.git] / test / Transforms / InstCombine / canonicalize-constant-low-bit-mask-and-icmp-ule-to-icmp-ule.ll
blob6f267206dbe82a8118465fe47ce6958534257688
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
6 ; Pattern:
7 ;   x u<= x & C
8 ; Should be transformed into:
9 ;   x u<= C
10 ; Iff: isPowerOf2(C + 1)
11 ; C can be 0 and -1.
13 declare i8 @gen8()
14 declare <2 x i8> @gen2x8()
15 declare <3 x i8> @gen3x8()
17 ; ============================================================================ ;
18 ; Basic positive tests
19 ; ============================================================================ ;
21 define i1 @p0() {
22 ; CHECK-LABEL: @p0(
23 ; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
24 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult i8 [[X]], 4
25 ; CHECK-NEXT:    ret i1 [[TMP1]]
27   %x = call i8 @gen8()
28   %tmp0 = and i8 %x, 3
29   %ret = icmp ule i8 %x, %tmp0
30   ret i1 %ret
33 define i1 @pv(i8 %y) {
34 ; CHECK-LABEL: @pv(
35 ; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
36 ; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
37 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule i8 [[X]], [[TMP0]]
38 ; CHECK-NEXT:    ret i1 [[TMP1]]
40   %x = call i8 @gen8()
41   %tmp0 = lshr i8 -1, %y
42   %tmp1 = and i8 %tmp0, %x
43   %ret = icmp ule i8 %x, %tmp1
44   ret i1 %ret
47 ; ============================================================================ ;
48 ; Vector tests
49 ; ============================================================================ ;
51 define <2 x i1> @p1_vec_splat() {
52 ; CHECK-LABEL: @p1_vec_splat(
53 ; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
54 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult <2 x i8> [[X]], <i8 4, i8 4>
55 ; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
57   %x = call <2 x i8> @gen2x8()
58   %tmp0 = and <2 x i8> %x, <i8 3, i8 3>
59   %ret = icmp ule <2 x i8> %x, %tmp0
60   ret <2 x i1> %ret
63 define <2 x i1> @p2_vec_nonsplat() {
64 ; CHECK-LABEL: @p2_vec_nonsplat(
65 ; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
66 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult <2 x i8> [[X]], <i8 4, i8 16>
67 ; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
69   %x = call <2 x i8> @gen2x8()
70   %tmp0 = and <2 x i8> %x, <i8 3, i8 15> ; doesn't have to be splat.
71   %ret = icmp ule <2 x i8> %x, %tmp0
72   ret <2 x i1> %ret
75 define <2 x i1> @p2_vec_nonsplat_edgecase0() {
76 ; CHECK-LABEL: @p2_vec_nonsplat_edgecase0(
77 ; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
78 ; CHECK-NEXT:    [[TMP0:%.*]] = and <2 x i8> [[X]], <i8 3, i8 0>
79 ; CHECK-NEXT:    [[RET:%.*]] = icmp ule <2 x i8> [[X]], [[TMP0]]
80 ; CHECK-NEXT:    ret <2 x i1> [[RET]]
82   %x = call <2 x i8> @gen2x8()
83   %tmp0 = and <2 x i8> %x, <i8 3, i8 0>
84   %ret = icmp ule <2 x i8> %x, %tmp0
85   ret <2 x i1> %ret
87 define <2 x i1> @p2_vec_nonsplat_edgecase1() {
88 ; CHECK-LABEL: @p2_vec_nonsplat_edgecase1(
89 ; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
90 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule <2 x i8> [[X]], <i8 3, i8 -1>
91 ; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
93   %x = call <2 x i8> @gen2x8()
94   %tmp0 = and <2 x i8> %x, <i8 3, i8 -1>
95   %ret = icmp ule <2 x i8> %x, %tmp0
96   ret <2 x i1> %ret
99 define <3 x i1> @p3_vec_splat_undef() {
100 ; CHECK-LABEL: @p3_vec_splat_undef(
101 ; CHECK-NEXT:    [[X:%.*]] = call <3 x i8> @gen3x8()
102 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult <3 x i8> [[X]], <i8 4, i8 undef, i8 4>
103 ; CHECK-NEXT:    ret <3 x i1> [[TMP1]]
105   %x = call <3 x i8> @gen3x8()
106   %tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 3>
107   %ret = icmp ule <3 x i8> %x, %tmp0
108   ret <3 x i1> %ret
111 ; ============================================================================ ;
112 ; Commutativity tests.
113 ; ============================================================================ ;
115 define i1 @c0(i8 %x) {
116 ; CHECK-LABEL: @c0(
117 ; CHECK-NEXT:    ret i1 true
119   %tmp0 = and i8 %x, 3
120   %ret = icmp ule i8 %tmp0, %x ; swapped order
121   ret i1 %ret
124 ; ============================================================================ ;
125 ; Commutativity tests with variable
126 ; ============================================================================ ;
128 define i1 @cv0(i8 %y) {
129 ; CHECK-LABEL: @cv0(
130 ; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
131 ; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
132 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule i8 [[X]], [[TMP0]]
133 ; CHECK-NEXT:    ret i1 [[TMP1]]
135   %x = call i8 @gen8()
136   %tmp0 = lshr i8 -1, %y
137   %tmp1 = and i8 %tmp0, %x ; swapped order
138   %ret = icmp ule i8 %x, %tmp1
139   ret i1 %ret
142 define i1 @cv1(i8 %y) {
143 ; CHECK-LABEL: @cv1(
144 ; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
145 ; CHECK-NEXT:    ret i1 true
147   %x = call i8 @gen8()
148   %tmp0 = lshr i8 -1, %y
149   %tmp1 = and i8 %x, %tmp0
150   %ret = icmp ule i8 %tmp1, %x ; swapped order
151   ret i1 %ret
154 define i1 @cv2(i8 %x, i8 %y) {
155 ; CHECK-LABEL: @cv2(
156 ; CHECK-NEXT:    ret i1 true
158   %tmp0 = lshr i8 -1, %y
159   %tmp1 = and i8 %tmp0, %x ; swapped order
160   %ret = icmp ule i8 %tmp1, %x ; swapped order
161   ret i1 %ret
164 ; ============================================================================ ;
165 ; One-use tests. We don't care about multi-uses here.
166 ; ============================================================================ ;
168 declare void @use8(i8)
170 define i1 @oneuse0() {
171 ; CHECK-LABEL: @oneuse0(
172 ; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
173 ; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 3
174 ; CHECK-NEXT:    call void @use8(i8 [[TMP0]])
175 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult i8 [[X]], 4
176 ; CHECK-NEXT:    ret i1 [[TMP1]]
178   %x = call i8 @gen8()
179   %tmp0 = and i8 %x, 3
180   call void @use8(i8 %tmp0)
181   %ret = icmp ule i8 %x, %tmp0
182   ret i1 %ret
185 ; ============================================================================ ;
186 ; Negative tests
187 ; ============================================================================ ;
189 define i1 @n0() {
190 ; CHECK-LABEL: @n0(
191 ; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
192 ; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 4
193 ; CHECK-NEXT:    [[RET:%.*]] = icmp ule i8 [[X]], [[TMP0]]
194 ; CHECK-NEXT:    ret i1 [[RET]]
196   %x = call i8 @gen8()
197   %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
198   %ret = icmp ule i8 %x, %tmp0
199   ret i1 %ret
202 define i1 @n1(i8 %y, i8 %notx) {
203 ; CHECK-LABEL: @n1(
204 ; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
205 ; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 3
206 ; CHECK-NEXT:    [[RET:%.*]] = icmp ule i8 [[TMP0]], [[NOTX:%.*]]
207 ; CHECK-NEXT:    ret i1 [[RET]]
209   %x = call i8 @gen8()
210   %tmp0 = and i8 %x, 3
211   %ret = icmp ule i8 %tmp0, %notx ; not %x
212   ret i1 %ret
215 define <2 x i1> @n2() {
216 ; CHECK-LABEL: @n2(
217 ; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
218 ; CHECK-NEXT:    [[TMP0:%.*]] = and <2 x i8> [[X]], <i8 3, i8 16>
219 ; CHECK-NEXT:    [[RET:%.*]] = icmp ule <2 x i8> [[X]], [[TMP0]]
220 ; CHECK-NEXT:    ret <2 x i1> [[RET]]
222   %x = call <2 x i8> @gen2x8()
223   %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.
224   %ret = icmp ule <2 x i8> %x, %tmp0
225   ret <2 x i1> %ret