1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4; https://bugs.llvm.org/show_bug.cgi?id=38708
5
6; Pattern:
7;   (1 << bits) u<= val
8; Should be transformed into:
9;   (val l>> bits) != 0
10
11; ============================================================================ ;
12; Basic positive tests
13; ============================================================================ ;
14
15define i1 @p0(i8 %val, i8 %bits) {
16; CHECK-LABEL: @p0(
17; CHECK-NEXT:    [[VAL_HIGHBITS:%.*]] = lshr i8 [[VAL:%.*]], [[BITS:%.*]]
18; CHECK-NEXT:    [[R:%.*]] = icmp ne i8 [[VAL_HIGHBITS]], 0
19; CHECK-NEXT:    ret i1 [[R]]
20;
21  %t0 = shl i8 1, %bits
22  %r = icmp ule i8 %t0, %val
23  ret i1 %r
24}
25
26; ============================================================================ ;
27; Vector tests
28; ============================================================================ ;
29
30define <2 x i1> @p1_vec(<2 x i8> %val, <2 x i8> %bits) {
31; CHECK-LABEL: @p1_vec(
32; CHECK-NEXT:    [[VAL_HIGHBITS:%.*]] = lshr <2 x i8> [[VAL:%.*]], [[BITS:%.*]]
33; CHECK-NEXT:    [[R:%.*]] = icmp ne <2 x i8> [[VAL_HIGHBITS]], zeroinitializer
34; CHECK-NEXT:    ret <2 x i1> [[R]]
35;
36  %t0 = shl <2 x i8> <i8 1, i8 1>, %bits
37  %r = icmp ule <2 x i8> %t0, %val
38  ret <2 x i1> %r
39}
40
41define <3 x i1> @p2_vec_undef(<3 x i8> %val, <3 x i8> %bits) {
42; CHECK-LABEL: @p2_vec_undef(
43; CHECK-NEXT:    [[VAL_HIGHBITS:%.*]] = lshr <3 x i8> [[VAL:%.*]], [[BITS:%.*]]
44; CHECK-NEXT:    [[R:%.*]] = icmp ne <3 x i8> [[VAL_HIGHBITS]], zeroinitializer
45; CHECK-NEXT:    ret <3 x i1> [[R]]
46;
47  %t0 = shl <3 x i8> <i8 1, i8 undef, i8 1>, %bits
48  %r = icmp ule <3 x i8> %t0, %val
49  ret <3 x i1> %r
50}
51
52; ============================================================================ ;
53; Commutativity tests.
54; ============================================================================ ;
55
56declare i8 @gen8()
57
58define i1 @c0(i8 %bits) {
59; CHECK-LABEL: @c0(
60; CHECK-NEXT:    [[VAL:%.*]] = call i8 @gen8()
61; CHECK-NEXT:    [[VAL_HIGHBITS:%.*]] = lshr i8 [[VAL]], [[BITS:%.*]]
62; CHECK-NEXT:    [[R:%.*]] = icmp ne i8 [[VAL_HIGHBITS]], 0
63; CHECK-NEXT:    ret i1 [[R]]
64;
65  %t0 = shl i8 1, %bits
66  %val = call i8 @gen8()
67  %r = icmp uge i8 %val, %t0 ; swapped order and predicate
68  ret i1 %r
69}
70
71; What if we have the same pattern on both sides?
72define i1 @both(i8 %bits0, i8 %bits1) {
73; CHECK-LABEL: @both(
74; CHECK-NEXT:    [[T1:%.*]] = shl i8 1, [[BITS1:%.*]]
75; CHECK-NEXT:    [[T1_HIGHBITS:%.*]] = lshr i8 [[T1]], [[BITS0:%.*]]
76; CHECK-NEXT:    [[R:%.*]] = icmp ne i8 [[T1_HIGHBITS]], 0
77; CHECK-NEXT:    ret i1 [[R]]
78;
79  %t0 = shl i8 1, %bits0
80  %t1 = shl i8 1, %bits1
81  %r = icmp ule i8 %t0, %t1
82  ret i1 %r
83}
84
85; ============================================================================ ;
86; One-use tests.
87; ============================================================================ ;
88
89declare void @use8(i8)
90
91define i1 @oneuse0(i8 %val, i8 %bits) {
92; CHECK-LABEL: @oneuse0(
93; CHECK-NEXT:    [[T0:%.*]] = shl i8 1, [[BITS:%.*]]
94; CHECK-NEXT:    call void @use8(i8 [[T0]])
95; CHECK-NEXT:    [[R:%.*]] = icmp ule i8 [[T0]], [[VAL:%.*]]
96; CHECK-NEXT:    ret i1 [[R]]
97;
98  %t0 = shl i8 1, %bits
99  call void @use8(i8 %t0)
100  %r = icmp ule i8 %t0, %val
101  ret i1 %r
102}
103
104; ============================================================================ ;
105; Negative tests
106; ============================================================================ ;
107
108define i1 @n0(i8 %val, i8 %bits) {
109; CHECK-LABEL: @n0(
110; CHECK-NEXT:    [[T0:%.*]] = shl i8 2, [[BITS:%.*]]
111; CHECK-NEXT:    [[R:%.*]] = icmp ule i8 [[T0]], [[VAL:%.*]]
112; CHECK-NEXT:    ret i1 [[R]]
113;
114  %t0 = shl i8 2, %bits ; constant is not 1
115  %r = icmp ule i8 %t0, %val
116  ret i1 %r
117}
118
119define <2 x i1> @n1_vec_nonsplat(<2 x i8> %val, <2 x i8> %bits) {
120; CHECK-LABEL: @n1_vec_nonsplat(
121; CHECK-NEXT:    [[T0:%.*]] = shl <2 x i8> <i8 1, i8 2>, [[BITS:%.*]]
122; CHECK-NEXT:    [[R:%.*]] = icmp ule <2 x i8> [[T0]], [[VAL:%.*]]
123; CHECK-NEXT:    ret <2 x i1> [[R]]
124;
125  %t0 = shl <2 x i8> <i8 1, i8 2>, %bits ; again, wrong constant
126  %r = icmp ule <2 x i8> %t0, %val
127  ret <2 x i1> %r
128}
129
130define i1 @n2(i8 %val, i8 %bits) {
131; CHECK-LABEL: @n2(
132; CHECK-NEXT:    [[T0:%.*]] = shl i8 1, [[BITS:%.*]]
133; CHECK-NEXT:    [[R:%.*]] = icmp ult i8 [[T0]], [[VAL:%.*]]
134; CHECK-NEXT:    ret i1 [[R]]
135;
136  %t0 = shl i8 1, %bits
137  %r = icmp ult i8 %t0, %val ; wrong predicate
138  ret i1 %r
139}
140
141define i1 @n3(i8 %bits) {
142; CHECK-LABEL: @n3(
143; CHECK-NEXT:    [[T0:%.*]] = shl i8 1, [[BITS:%.*]]
144; CHECK-NEXT:    [[VAL:%.*]] = call i8 @gen8()
145; CHECK-NEXT:    [[R:%.*]] = icmp ugt i8 [[VAL]], [[T0]]
146; CHECK-NEXT:    ret i1 [[R]]
147;
148  %t0 = shl i8 1, %bits
149  %val = call i8 @gen8()
150  %r = icmp ugt i8 %val, %t0 ; swapped order and [wrong] predicate
151  ret i1 %r
152}
153