1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -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:%.*]], <i8 1, 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>
42 define <3 x i8> @t3_vec_undef0(<3 x i8> %x) {
43 ; CHECK-LABEL: @t3_vec_undef0(
44 ; CHECK-NEXT: [[TMP1:%.*]] = and <3 x i8> [[X:%.*]], <i8 1, i8 undef, i8 1>
45 ; CHECK-NEXT: [[R:%.*]] = sub <3 x i8> zeroinitializer, [[TMP1]]
46 ; CHECK-NEXT: ret <3 x i8> [[R]]
48 %i0 = shl <3 x i8> %x, <i8 7, i8 undef, i8 7>
49 %r = ashr <3 x i8> %i0, <i8 7, i8 7, i8 7>
52 define <3 x i8> @t4_vec_undef1(<3 x i8> %x) {
53 ; CHECK-LABEL: @t4_vec_undef1(
54 ; CHECK-NEXT: [[TMP1:%.*]] = and <3 x i8> [[X:%.*]], <i8 1, i8 undef, i8 1>
55 ; CHECK-NEXT: [[R:%.*]] = sub <3 x i8> zeroinitializer, [[TMP1]]
56 ; CHECK-NEXT: ret <3 x i8> [[R]]
58 %i0 = shl <3 x i8> %x, <i8 7, i8 7, i8 7>
59 %r = ashr <3 x i8> %i0, <i8 7, i8 undef, i8 7>
62 define <3 x i8> @t5_vec_undef2(<3 x i8> %x) {
63 ; CHECK-LABEL: @t5_vec_undef2(
64 ; CHECK-NEXT: [[TMP1:%.*]] = and <3 x i8> [[X:%.*]], <i8 1, i8 undef, i8 1>
65 ; CHECK-NEXT: [[R:%.*]] = sub <3 x i8> zeroinitializer, [[TMP1]]
66 ; CHECK-NEXT: ret <3 x i8> [[R]]
68 %i0 = shl <3 x i8> %x, <i8 7, i8 undef, i8 7>
69 %r = ashr <3 x i8> %i0, <i8 7, i8 undef, i8 7>
73 ; In general, the `shl` needs to go away.
74 define i8 @n6_extrause(i8 %x) {
75 ; CHECK-LABEL: @n6_extrause(
76 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 7
77 ; CHECK-NEXT: call void @use8(i8 [[I0]])
78 ; CHECK-NEXT: [[R:%.*]] = ashr exact i8 [[I0]], 7
79 ; CHECK-NEXT: ret i8 [[R]]
82 call void @use8(i8 %i0)
87 ; But, if the input to the shift is already masked, then we're fine.
88 define i8 @t7_already_masked(i8 %x) {
89 ; CHECK-LABEL: @t7_already_masked(
90 ; CHECK-NEXT: [[I0:%.*]] = and i8 [[X:%.*]], 1
91 ; CHECK-NEXT: call void @use8(i8 [[I0]])
92 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], 1
93 ; CHECK-NEXT: [[R:%.*]] = sub nsw i8 0, [[TMP1]]
94 ; CHECK-NEXT: ret i8 [[R]]
97 call void @use8(i8 %i0)
102 ; FIXME: we should fold this
103 define i8 @t8_already_masked_extrause(i8 %x) {
104 ; CHECK-LABEL: @t8_already_masked_extrause(
105 ; CHECK-NEXT: [[I0:%.*]] = and i8 [[X:%.*]], 1
106 ; CHECK-NEXT: call void @use8(i8 [[I0]])
107 ; CHECK-NEXT: [[I1:%.*]] = shl i8 [[X]], 7
108 ; CHECK-NEXT: call void @use8(i8 [[I1]])
109 ; CHECK-NEXT: [[R:%.*]] = ashr exact i8 [[I1]], 7
110 ; CHECK-NEXT: ret i8 [[R]]
113 call void @use8(i8 %i0)
115 call void @use8(i8 %i1)
119 define i8 @n9_wrongly_masked_extrause(i8 %x) {
120 ; CHECK-LABEL: @n9_wrongly_masked_extrause(
121 ; CHECK-NEXT: [[I0:%.*]] = and i8 [[X:%.*]], 3
122 ; CHECK-NEXT: call void @use8(i8 [[I0]])
123 ; CHECK-NEXT: [[I1:%.*]] = shl i8 [[X]], 7
124 ; CHECK-NEXT: call void @use8(i8 [[I1]])
125 ; CHECK-NEXT: [[R:%.*]] = ashr exact i8 [[I1]], 7
126 ; CHECK-NEXT: ret i8 [[R]]
129 call void @use8(i8 %i0)
131 call void @use8(i8 %i1)
136 ; Wrong shift amounts
137 define i8 @n10(i8 %x) {
139 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 6
140 ; CHECK-NEXT: [[R:%.*]] = ashr i8 [[I0]], 7
141 ; CHECK-NEXT: ret i8 [[R]]
143 %i0 = shl i8 %x, 6 ; not 7
147 define i8 @n11(i8 %x) {
149 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 7
150 ; CHECK-NEXT: [[R:%.*]] = ashr exact i8 [[I0]], 6
151 ; CHECK-NEXT: ret i8 [[R]]
154 %r = ashr i8 %i0, 6 ; not 7
157 define i8 @n12(i8 %x) {
159 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 6
160 ; CHECK-NEXT: [[R:%.*]] = ashr exact i8 [[I0]], 6
161 ; CHECK-NEXT: ret i8 [[R]]
163 %i0 = shl i8 %x, 6 ; not 7
164 %r = ashr i8 %i0, 6 ; not 7