1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
5 define i32 @test1(i32 %X, i8 %A) {
7 ; CHECK-NEXT: [[SHIFT_UPGRD_1:%.*]] = zext i8 %A to i32
8 ; CHECK-NEXT: [[Y1:%.*]] = lshr i32 %X, [[SHIFT_UPGRD_1]]
9 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[Y1]], 1
10 ; CHECK-NEXT: ret i32 [[Z]]
12 %shift.upgrd.1 = zext i8 %A to i32
13 ; can be logical shift.
14 %Y = ashr i32 %X, %shift.upgrd.1
19 define i32 @test2(i8 %tmp) {
20 ; CHECK-LABEL: @test2(
21 ; CHECK-NEXT: [[TMP3:%.*]] = zext i8 %tmp to i32
22 ; CHECK-NEXT: [[TMP4:%.*]] = add nuw nsw i32 [[TMP3]], 7
23 ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[TMP4]], 3
24 ; CHECK-NEXT: ret i32 [[TMP1]]
26 %tmp3 = zext i8 %tmp to i32
27 %tmp4 = add i32 %tmp3, 7
28 %tmp5 = ashr i32 %tmp4, 3
32 define i64 @test3(i1 %X, i64 %Y, i1 %Cond) {
33 ; CHECK-LABEL: @test3(
34 ; CHECK-NEXT: br i1 %Cond, label %T, label %F
36 ; CHECK-NEXT: [[X2:%.*]] = sext i1 %X to i64
37 ; CHECK-NEXT: br label %C
39 ; CHECK-NEXT: [[Y2:%.*]] = ashr i64 %Y, 63
40 ; CHECK-NEXT: br label %C
42 ; CHECK-NEXT: [[P:%.*]] = phi i64 [ [[X2]], %T ], [ [[Y2]], %F ]
43 ; CHECK-NEXT: ret i64 [[P]]
45 br i1 %Cond, label %T, label %F
47 %X2 = sext i1 %X to i64
53 %P = phi i64 [%X2, %T], [%Y2, %F]
58 define i64 @test4(i1 %X, i64 %Y, i1 %Cond) {
59 ; CHECK-LABEL: @test4(
60 ; CHECK-NEXT: br i1 %Cond, label %T, label %F
62 ; CHECK-NEXT: [[X2:%.*]] = sext i1 %X to i64
63 ; CHECK-NEXT: br label %C
65 ; CHECK-NEXT: [[Y2:%.*]] = ashr i64 %Y, 63
66 ; CHECK-NEXT: br label %C
68 ; CHECK-NEXT: [[P:%.*]] = phi i64 [ [[X2]], %T ], [ [[Y2]], %F ]
69 ; CHECK-NEXT: ret i64 [[P]]
71 br i1 %Cond, label %T, label %F
73 %X2 = sext i1 %X to i64
79 %P = phi i64 [%X2, %T], [%Y2, %F]
86 define i32 @test5(i32 %Y) {
87 ; CHECK-LABEL: @test5(
88 ; CHECK-NEXT: br i1 undef, label %A, label %C
90 ; CHECK-NEXT: br i1 undef, label %B, label %D
92 ; CHECK-NEXT: br label %D
94 ; CHECK-NEXT: br i1 undef, label %D, label %E
96 ; CHECK-NEXT: [[P:%.*]] = phi i32 [ 0, %A ], [ 0, %B ], [ %Y, %C ]
97 ; CHECK-NEXT: [[S:%.*]] = ashr i32 [[P]], 16
98 ; CHECK-NEXT: ret i32 [[S]]
100 ; CHECK-NEXT: ret i32 0
102 br i1 undef, label %A, label %C
104 br i1 undef, label %B, label %D
108 br i1 undef, label %D, label %E
110 %P = phi i32 [0, %A], [0, %B], [%Y, %C]
117 ; (X >>s C1) >>s C2 --> X >>s (C1 + C2)
119 define i32 @ashr_ashr(i32 %x) {
120 ; CHECK-LABEL: @ashr_ashr(
121 ; CHECK-NEXT: [[SH2:%.*]] = ashr i32 %x, 12
122 ; CHECK-NEXT: ret i32 [[SH2]]
124 %sh1 = ashr i32 %x, 5
125 %sh2 = ashr i32 %sh1, 7
130 ; (X >>s C1) >>s C2 --> X >>s (Bitwidth - 1)
132 define i32 @ashr_overshift(i32 %x) {
133 ; CHECK-LABEL: @ashr_overshift(
134 ; CHECK-NEXT: [[SH2:%.*]] = ashr i32 %x, 31
135 ; CHECK-NEXT: ret i32 [[SH2]]
137 %sh1 = ashr i32 %x, 15
138 %sh2 = ashr i32 %sh1, 17
142 ; (X >>s C1) >>s C2 --> X >>s (C1 + C2)
144 define <2 x i32> @ashr_ashr_splat_vec(<2 x i32> %x) {
145 ; CHECK-LABEL: @ashr_ashr_splat_vec(
146 ; CHECK-NEXT: [[SH2:%.*]] = ashr <2 x i32> %x, <i32 12, i32 12>
147 ; CHECK-NEXT: ret <2 x i32> [[SH2]]
149 %sh1 = ashr <2 x i32> %x, <i32 5, i32 5>
150 %sh2 = ashr <2 x i32> %sh1, <i32 7, i32 7>
154 ; (X >>s C1) >>s C2 --> X >>s (Bitwidth - 1)
156 define <2 x i32> @ashr_overshift_splat_vec(<2 x i32> %x) {
157 ; CHECK-LABEL: @ashr_overshift_splat_vec(
158 ; CHECK-NEXT: [[SH2:%.*]] = ashr <2 x i32> %x, <i32 31, i32 31>
159 ; CHECK-NEXT: ret <2 x i32> [[SH2]]
161 %sh1 = ashr <2 x i32> %x, <i32 15, i32 15>
162 %sh2 = ashr <2 x i32> %sh1, <i32 17, i32 17>
166 ; ashr (sext X), C --> sext (ashr X, C')
168 define i32 @hoist_ashr_ahead_of_sext_1(i8 %x) {
169 ; CHECK-LABEL: @hoist_ashr_ahead_of_sext_1(
170 ; CHECK-NEXT: [[TMP1:%.*]] = ashr i8 %x, 3
171 ; CHECK-NEXT: [[R:%.*]] = sext i8 [[TMP1]] to i32
172 ; CHECK-NEXT: ret i32 [[R]]
174 %sext = sext i8 %x to i32
175 %r = ashr i32 %sext, 3
179 ; ashr (sext X), C --> sext (ashr X, C')
181 define <2 x i32> @hoist_ashr_ahead_of_sext_1_splat(<2 x i8> %x) {
182 ; CHECK-LABEL: @hoist_ashr_ahead_of_sext_1_splat(
183 ; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i8> %x, <i8 3, i8 3>
184 ; CHECK-NEXT: [[R:%.*]] = sext <2 x i8> [[TMP1]] to <2 x i32>
185 ; CHECK-NEXT: ret <2 x i32> [[R]]
187 %sext = sext <2 x i8> %x to <2 x i32>
188 %r = ashr <2 x i32> %sext, <i32 3, i32 3>
192 ; ashr (sext X), C --> sext (ashr X, C') -- the shift amount must be clamped
194 define i32 @hoist_ashr_ahead_of_sext_2(i8 %x) {
195 ; CHECK-LABEL: @hoist_ashr_ahead_of_sext_2(
196 ; CHECK-NEXT: [[TMP1:%.*]] = ashr i8 %x, 7
197 ; CHECK-NEXT: [[R:%.*]] = sext i8 [[TMP1]] to i32
198 ; CHECK-NEXT: ret i32 [[R]]
200 %sext = sext i8 %x to i32
201 %r = ashr i32 %sext, 8
205 ; ashr (sext X), C --> sext (ashr X, C') -- the shift amount must be clamped
207 define <2 x i32> @hoist_ashr_ahead_of_sext_2_splat(<2 x i8> %x) {
208 ; CHECK-LABEL: @hoist_ashr_ahead_of_sext_2_splat(
209 ; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i8> %x, <i8 7, i8 7>
210 ; CHECK-NEXT: [[R:%.*]] = sext <2 x i8> [[TMP1]] to <2 x i32>
211 ; CHECK-NEXT: ret <2 x i32> [[R]]
213 %sext = sext <2 x i8> %x to <2 x i32>
214 %r = ashr <2 x i32> %sext, <i32 8, i32 8>