1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
5 ; (trunc (iSrc x l>> Q) to iDst) l>> K
6 ; we should rewrite it as
7 ; (trunc (iSrc x l>> (Q+K)) to iDst)
8 ; iff (Q+K) is bitwidth(iSrc)-1
9 ; THIS FOLD DOES *NOT* REQUIRE ANY 'nuw'/`nsw` FLAGS!
13 define i16 @t0(i32 %x, i16 %y) {
15 ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 31
16 ; CHECK-NEXT: [[T5:%.*]] = trunc nuw nsw i32 [[TMP1]] to i16
17 ; CHECK-NEXT: ret i16 [[T5]]
20 %t1 = zext i16 %t0 to i32
21 %t2 = lshr i32 %x, %t1
22 %t3 = trunc i32 %t2 to i16
24 %t5 = lshr i16 %t3, %t4
30 define <2 x i16> @t1_vec_splat(<2 x i32> %x, <2 x i16> %y) {
31 ; CHECK-LABEL: @t1_vec_splat(
32 ; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 31, i32 31>
33 ; CHECK-NEXT: [[T5:%.*]] = trunc nuw nsw <2 x i32> [[TMP1]] to <2 x i16>
34 ; CHECK-NEXT: ret <2 x i16> [[T5]]
36 %t0 = sub <2 x i16> <i16 32, i16 32>, %y
37 %t1 = zext <2 x i16> %t0 to <2 x i32>
38 %t2 = lshr <2 x i32> %x, %t1
39 %t3 = trunc <2 x i32> %t2 to <2 x i16>
40 %t4 = add <2 x i16> %y, <i16 -1, i16 -1>
41 %t5 = lshr <2 x i16> %t3, %t4
45 define <3 x i16> @t3_vec_nonsplat_poison0(<3 x i32> %x, <3 x i16> %y) {
46 ; CHECK-LABEL: @t3_vec_nonsplat_poison0(
47 ; CHECK-NEXT: [[TMP1:%.*]] = lshr <3 x i32> [[X:%.*]], <i32 31, i32 poison, i32 31>
48 ; CHECK-NEXT: [[T5:%.*]] = trunc nuw nsw <3 x i32> [[TMP1]] to <3 x i16>
49 ; CHECK-NEXT: ret <3 x i16> [[T5]]
51 %t0 = sub <3 x i16> <i16 32, i16 poison, i16 32>, %y
52 %t1 = zext <3 x i16> %t0 to <3 x i32>
53 %t2 = lshr <3 x i32> %x, %t1
54 %t3 = trunc <3 x i32> %t2 to <3 x i16>
55 %t4 = add <3 x i16> %y, <i16 -1, i16 -1, i16 -1>
56 %t5 = lshr <3 x i16> %t3, %t4
60 define <3 x i16> @t4_vec_nonsplat_poison1(<3 x i32> %x, <3 x i16> %y) {
61 ; CHECK-LABEL: @t4_vec_nonsplat_poison1(
62 ; CHECK-NEXT: [[TMP1:%.*]] = lshr <3 x i32> [[X:%.*]], <i32 31, i32 poison, i32 31>
63 ; CHECK-NEXT: [[T5:%.*]] = trunc nuw nsw <3 x i32> [[TMP1]] to <3 x i16>
64 ; CHECK-NEXT: ret <3 x i16> [[T5]]
66 %t0 = sub <3 x i16> <i16 32, i16 32, i16 32>, %y
67 %t1 = zext <3 x i16> %t0 to <3 x i32>
68 %t2 = lshr <3 x i32> %x, %t1
69 %t3 = trunc <3 x i32> %t2 to <3 x i16>
70 %t4 = add <3 x i16> %y, <i16 -1, i16 poison, i16 -1>
71 %t5 = lshr <3 x i16> %t3, %t4
75 define <3 x i16> @t5_vec_nonsplat_poison1(<3 x i32> %x, <3 x i16> %y) {
76 ; CHECK-LABEL: @t5_vec_nonsplat_poison1(
77 ; CHECK-NEXT: [[TMP1:%.*]] = lshr <3 x i32> [[X:%.*]], <i32 31, i32 poison, i32 31>
78 ; CHECK-NEXT: [[T5:%.*]] = trunc nuw nsw <3 x i32> [[TMP1]] to <3 x i16>
79 ; CHECK-NEXT: ret <3 x i16> [[T5]]
81 %t0 = sub <3 x i16> <i16 32, i16 poison, i16 32>, %y
82 %t1 = zext <3 x i16> %t0 to <3 x i32>
83 %t2 = lshr <3 x i32> %x, %t1
84 %t3 = trunc <3 x i32> %t2 to <3 x i16>
85 %t4 = add <3 x i16> %y, <i16 -1, i16 poison, i16 -1>
86 %t5 = lshr <3 x i16> %t3, %t4
92 declare void @use16(i16)
93 declare void @use32(i32)
95 define i16 @t6_extrause0(i32 %x, i16 %y) {
96 ; CHECK-LABEL: @t6_extrause0(
97 ; CHECK-NEXT: [[T0:%.*]] = sub i16 32, [[Y:%.*]]
98 ; CHECK-NEXT: [[T1:%.*]] = zext nneg i16 [[T0]] to i32
99 ; CHECK-NEXT: [[T2:%.*]] = lshr i32 [[X:%.*]], [[T1]]
100 ; CHECK-NEXT: [[T3:%.*]] = trunc i32 [[T2]] to i16
101 ; CHECK-NEXT: call void @use16(i16 [[T3]])
102 ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X]], 31
103 ; CHECK-NEXT: [[T5:%.*]] = trunc nuw nsw i32 [[TMP1]] to i16
104 ; CHECK-NEXT: ret i16 [[T5]]
107 %t1 = zext i16 %t0 to i32
108 %t2 = lshr i32 %x, %t1
109 %t3 = trunc i32 %t2 to i16
111 call void @use16(i16 %t3)
112 %t5 = lshr i16 %t3, %t4
116 define i16 @t7_extrause1(i32 %x, i16 %y) {
117 ; CHECK-LABEL: @t7_extrause1(
118 ; CHECK-NEXT: [[T4:%.*]] = add i16 [[Y:%.*]], -1
119 ; CHECK-NEXT: call void @use16(i16 [[T4]])
120 ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 31
121 ; CHECK-NEXT: [[T5:%.*]] = trunc nuw nsw i32 [[TMP1]] to i16
122 ; CHECK-NEXT: ret i16 [[T5]]
125 %t1 = zext i16 %t0 to i32
126 %t2 = lshr i32 %x, %t1
127 %t3 = trunc i32 %t2 to i16
129 call void @use16(i16 %t4)
130 %t5 = lshr i16 %t3, %t4
134 define i16 @t8_extrause2(i32 %x, i16 %y) {
135 ; CHECK-LABEL: @t8_extrause2(
136 ; CHECK-NEXT: [[T0:%.*]] = sub i16 32, [[Y:%.*]]
137 ; CHECK-NEXT: [[T1:%.*]] = zext nneg i16 [[T0]] to i32
138 ; CHECK-NEXT: [[T2:%.*]] = lshr i32 [[X:%.*]], [[T1]]
139 ; CHECK-NEXT: [[T3:%.*]] = trunc i32 [[T2]] to i16
140 ; CHECK-NEXT: [[T4:%.*]] = add i16 [[Y]], -1
141 ; CHECK-NEXT: call void @use16(i16 [[T3]])
142 ; CHECK-NEXT: call void @use16(i16 [[T4]])
143 ; CHECK-NEXT: [[T5:%.*]] = lshr i16 [[T3]], [[T4]]
144 ; CHECK-NEXT: ret i16 [[T5]]
147 %t1 = zext i16 %t0 to i32
148 %t2 = lshr i32 %x, %t1
149 %t3 = trunc i32 %t2 to i16
151 call void @use16(i16 %t3)
152 call void @use16(i16 %t4)
153 %t5 = lshr i16 %t3, %t4
157 ; No 'nuw'/'nsw' flags are to be propagated!
158 ; But we can't test that, such IR does not reach that code.
162 ; Can only fold if we are extracting the sign bit.
163 define i16 @t9_lshr(i32 %x, i16 %y) {
164 ; CHECK-LABEL: @t9_lshr(
165 ; CHECK-NEXT: [[T0:%.*]] = sub i16 32, [[Y:%.*]]
166 ; CHECK-NEXT: [[T1:%.*]] = zext nneg i16 [[T0]] to i32
167 ; CHECK-NEXT: [[T2:%.*]] = lshr i32 [[X:%.*]], [[T1]]
168 ; CHECK-NEXT: [[T3:%.*]] = trunc i32 [[T2]] to i16
169 ; CHECK-NEXT: [[T4:%.*]] = add i16 [[Y]], -2
170 ; CHECK-NEXT: [[T5:%.*]] = lshr i16 [[T3]], [[T4]]
171 ; CHECK-NEXT: ret i16 [[T5]]
174 %t1 = zext i16 %t0 to i32
175 %t2 = lshr i32 %x, %t1
176 %t3 = trunc i32 %t2 to i16
178 %t5 = lshr i16 %t3, %t4
182 ; If we have different right-shifts, in general, we can't do anything with it.
183 define i16 @n10_ashr_lshr(i32 %x, i16 %y) {
184 ; CHECK-LABEL: @n10_ashr_lshr(
185 ; CHECK-NEXT: [[T0:%.*]] = sub i16 32, [[Y:%.*]]
186 ; CHECK-NEXT: [[T1:%.*]] = zext nneg i16 [[T0]] to i32
187 ; CHECK-NEXT: [[T2:%.*]] = ashr i32 [[X:%.*]], [[T1]]
188 ; CHECK-NEXT: [[T3:%.*]] = trunc i32 [[T2]] to i16
189 ; CHECK-NEXT: [[T4:%.*]] = add i16 [[Y]], -1
190 ; CHECK-NEXT: [[T5:%.*]] = lshr i16 [[T3]], [[T4]]
191 ; CHECK-NEXT: ret i16 [[T5]]
194 %t1 = zext i16 %t0 to i32
195 %t2 = ashr i32 %x, %t1
196 %t3 = trunc i32 %t2 to i16
198 %t5 = lshr i16 %t3, %t4