1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 ; PR51305: prefer `-(x & 1)` over `(x << (bitwidth(x)-1)) a>> (bitwidth(x)-1)`
5 ; as the pattern to splat the lowest bit.
9 ; Basic positive scalar tests
10 define i8 @t0(i8 %x) {
12 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], 1
13 ; CHECK-NEXT: [[R:%.*]] = sub nsw i8 0, [[TMP1]]
14 ; CHECK-NEXT: ret i8 [[R]]
20 define i16 @t1_otherbitwidth(i16 %x) {
21 ; CHECK-LABEL: @t1_otherbitwidth(
22 ; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[X:%.*]], 1
23 ; CHECK-NEXT: [[R:%.*]] = sub nsw i16 0, [[TMP1]]
24 ; CHECK-NEXT: ret i16 [[R]]
31 ; Basic positive vector tests
32 define <2 x i8> @t2_vec(<2 x i8> %x) {
33 ; CHECK-LABEL: @t2_vec(
34 ; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], splat (i8 1)
35 ; CHECK-NEXT: [[R:%.*]] = sub nsw <2 x i8> zeroinitializer, [[TMP1]]
36 ; CHECK-NEXT: ret <2 x i8> [[R]]
38 %i0 = shl <2 x i8> %x, <i8 7, i8 7>
39 %r = ashr <2 x i8> %i0, <i8 7, i8 7>
43 ; TODO: The result constants should contain poison instead of undef.
45 define <3 x i8> @t3_vec_poison0(<3 x i8> %x) {
46 ; CHECK-LABEL: @t3_vec_poison0(
47 ; CHECK-NEXT: [[TMP1:%.*]] = and <3 x i8> [[X:%.*]], <i8 1, i8 undef, i8 1>
48 ; CHECK-NEXT: [[R:%.*]] = sub <3 x i8> zeroinitializer, [[TMP1]]
49 ; CHECK-NEXT: ret <3 x i8> [[R]]
51 %i0 = shl <3 x i8> %x, <i8 7, i8 poison, i8 7>
52 %r = ashr <3 x i8> %i0, <i8 7, i8 7, i8 7>
55 define <3 x i8> @t4_vec_poison1(<3 x i8> %x) {
56 ; CHECK-LABEL: @t4_vec_poison1(
57 ; CHECK-NEXT: [[TMP1:%.*]] = and <3 x i8> [[X:%.*]], <i8 1, i8 undef, i8 1>
58 ; CHECK-NEXT: [[R:%.*]] = sub <3 x i8> zeroinitializer, [[TMP1]]
59 ; CHECK-NEXT: ret <3 x i8> [[R]]
61 %i0 = shl <3 x i8> %x, <i8 7, i8 7, i8 7>
62 %r = ashr <3 x i8> %i0, <i8 7, i8 poison, i8 7>
65 define <3 x i8> @t5_vec_poison2(<3 x i8> %x) {
66 ; CHECK-LABEL: @t5_vec_poison2(
67 ; CHECK-NEXT: [[TMP1:%.*]] = and <3 x i8> [[X:%.*]], <i8 1, i8 undef, i8 1>
68 ; CHECK-NEXT: [[R:%.*]] = sub <3 x i8> zeroinitializer, [[TMP1]]
69 ; CHECK-NEXT: ret <3 x i8> [[R]]
71 %i0 = shl <3 x i8> %x, <i8 7, i8 poison, i8 7>
72 %r = ashr <3 x i8> %i0, <i8 7, i8 poison, i8 7>
76 ; In general, the `shl` needs to go away.
77 define i8 @n6_extrause(i8 %x) {
78 ; CHECK-LABEL: @n6_extrause(
79 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 7
80 ; CHECK-NEXT: call void @use8(i8 [[I0]])
81 ; CHECK-NEXT: [[R:%.*]] = ashr exact i8 [[I0]], 7
82 ; CHECK-NEXT: ret i8 [[R]]
85 call void @use8(i8 %i0)
90 ; But, if the input to the shift is already masked, then we're fine.
91 define i8 @t7_already_masked(i8 %x) {
92 ; CHECK-LABEL: @t7_already_masked(
93 ; CHECK-NEXT: [[I0:%.*]] = and i8 [[X:%.*]], 1
94 ; CHECK-NEXT: call void @use8(i8 [[I0]])
95 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], 1
96 ; CHECK-NEXT: [[R:%.*]] = sub nsw i8 0, [[TMP1]]
97 ; CHECK-NEXT: ret i8 [[R]]
100 call void @use8(i8 %i0)
105 ; FIXME: we should fold this
106 define i8 @t8_already_masked_extrause(i8 %x) {
107 ; CHECK-LABEL: @t8_already_masked_extrause(
108 ; CHECK-NEXT: [[I0:%.*]] = and i8 [[X:%.*]], 1
109 ; CHECK-NEXT: call void @use8(i8 [[I0]])
110 ; CHECK-NEXT: [[I1:%.*]] = shl i8 [[X]], 7
111 ; CHECK-NEXT: call void @use8(i8 [[I1]])
112 ; CHECK-NEXT: [[R:%.*]] = ashr exact i8 [[I1]], 7
113 ; CHECK-NEXT: ret i8 [[R]]
116 call void @use8(i8 %i0)
118 call void @use8(i8 %i1)
122 define i8 @n9_wrongly_masked_extrause(i8 %x) {
123 ; CHECK-LABEL: @n9_wrongly_masked_extrause(
124 ; CHECK-NEXT: [[I0:%.*]] = and i8 [[X:%.*]], 3
125 ; CHECK-NEXT: call void @use8(i8 [[I0]])
126 ; CHECK-NEXT: [[I1:%.*]] = shl i8 [[X]], 7
127 ; CHECK-NEXT: call void @use8(i8 [[I1]])
128 ; CHECK-NEXT: [[R:%.*]] = ashr exact i8 [[I1]], 7
129 ; CHECK-NEXT: ret i8 [[R]]
132 call void @use8(i8 %i0)
134 call void @use8(i8 %i1)
139 ; Wrong shift amounts
140 define i8 @n10(i8 %x) {
142 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 6
143 ; CHECK-NEXT: [[R:%.*]] = ashr i8 [[I0]], 7
144 ; CHECK-NEXT: ret i8 [[R]]
146 %i0 = shl i8 %x, 6 ; not 7
150 define i8 @n11(i8 %x) {
152 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 7
153 ; CHECK-NEXT: [[R:%.*]] = ashr exact i8 [[I0]], 6
154 ; CHECK-NEXT: ret i8 [[R]]
157 %r = ashr i8 %i0, 6 ; not 7
160 define i8 @n12(i8 %x) {
162 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 6
163 ; CHECK-NEXT: [[R:%.*]] = ashr exact i8 [[I0]], 6
164 ; CHECK-NEXT: ret i8 [[R]]
166 %i0 = shl i8 %x, 6 ; not 7
167 %r = ashr i8 %i0, 6 ; not 7