1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 target datalayout = "n8:16:32:64"
8 define i32 @sextinreg(i32 %x) {
9 ; CHECK-LABEL: @sextinreg(
10 ; CHECK-NEXT: [[SEXT:%.*]] = shl i32 [[X:%.*]], 16
11 ; CHECK-NEXT: [[T3:%.*]] = ashr exact i32 [[SEXT]], 16
12 ; CHECK-NEXT: ret i32 [[T3]]
14 %t1 = and i32 %x, 65535
15 %t2 = xor i32 %t1, -32768
16 %t3 = add i32 %t2, 32768
20 define i32 @sextinreg_extra_use(i32 %x) {
21 ; CHECK-LABEL: @sextinreg_extra_use(
22 ; CHECK-NEXT: [[T1:%.*]] = and i32 [[X:%.*]], 65535
23 ; CHECK-NEXT: [[T2:%.*]] = xor i32 [[T1]], -32768
24 ; CHECK-NEXT: call void @use(i32 [[T2]])
25 ; CHECK-NEXT: [[T3:%.*]] = add nsw i32 [[T2]], 32768
26 ; CHECK-NEXT: ret i32 [[T3]]
28 %t1 = and i32 %x, 65535
29 %t2 = xor i32 %t1, -32768
30 call void @use(i32 %t2)
31 %t3 = add i32 %t2, 32768
35 define <2 x i32> @sextinreg_splat(<2 x i32> %x) {
36 ; CHECK-LABEL: @sextinreg_splat(
37 ; CHECK-NEXT: [[SEXT:%.*]] = shl <2 x i32> [[X:%.*]], <i32 16, i32 16>
38 ; CHECK-NEXT: [[T3:%.*]] = ashr exact <2 x i32> [[SEXT]], <i32 16, i32 16>
39 ; CHECK-NEXT: ret <2 x i32> [[T3]]
41 %t1 = and <2 x i32> %x, <i32 65535, i32 65535>
42 %t2 = xor <2 x i32> %t1, <i32 -32768, i32 -32768>
43 %t3 = add <2 x i32> %t2, <i32 32768, i32 32768>
47 define i32 @sextinreg_alt(i32 %x) {
48 ; CHECK-LABEL: @sextinreg_alt(
49 ; CHECK-NEXT: [[SEXT:%.*]] = shl i32 [[X:%.*]], 16
50 ; CHECK-NEXT: [[T3:%.*]] = ashr exact i32 [[SEXT]], 16
51 ; CHECK-NEXT: ret i32 [[T3]]
53 %t1 = and i32 %x, 65535
54 %t2 = xor i32 %t1, 32768
55 %t3 = add i32 %t2, -32768
59 define <2 x i32> @sextinreg_alt_splat(<2 x i32> %x) {
60 ; CHECK-LABEL: @sextinreg_alt_splat(
61 ; CHECK-NEXT: [[SEXT:%.*]] = shl <2 x i32> [[X:%.*]], <i32 16, i32 16>
62 ; CHECK-NEXT: [[T3:%.*]] = ashr exact <2 x i32> [[SEXT]], <i32 16, i32 16>
63 ; CHECK-NEXT: ret <2 x i32> [[T3]]
65 %t1 = and <2 x i32> %x, <i32 65535, i32 65535>
66 %t2 = xor <2 x i32> %t1, <i32 32768, i32 32768>
67 %t3 = add <2 x i32> %t2, <i32 -32768, i32 -32768>
71 define i32 @sext(i16 %P) {
73 ; CHECK-NEXT: [[T5:%.*]] = sext i16 [[P:%.*]] to i32
74 ; CHECK-NEXT: ret i32 [[T5]]
76 %t1 = zext i16 %P to i32
77 %t4 = xor i32 %t1, 32768
78 %t5 = add i32 %t4, -32768
82 define i32 @sext_extra_use(i16 %P) {
83 ; CHECK-LABEL: @sext_extra_use(
84 ; CHECK-NEXT: [[TMP1:%.*]] = xor i16 [[P:%.*]], -32768
85 ; CHECK-NEXT: [[T4:%.*]] = zext i16 [[TMP1]] to i32
86 ; CHECK-NEXT: call void @use(i32 [[T4]])
87 ; CHECK-NEXT: [[T5:%.*]] = sext i16 [[P]] to i32
88 ; CHECK-NEXT: ret i32 [[T5]]
90 %t1 = zext i16 %P to i32
91 %t4 = xor i32 %t1, 32768
92 call void @use(i32 %t4)
93 %t5 = add i32 %t4, -32768
97 define <2 x i32> @sext_splat(<2 x i16> %P) {
98 ; CHECK-LABEL: @sext_splat(
99 ; CHECK-NEXT: [[T5:%.*]] = sext <2 x i16> [[P:%.*]] to <2 x i32>
100 ; CHECK-NEXT: ret <2 x i32> [[T5]]
102 %t1 = zext <2 x i16> %P to <2 x i32>
103 %t4 = xor <2 x i32> %t1, <i32 32768, i32 32768>
104 %t5 = add <2 x i32> %t4, <i32 -32768, i32 -32768>
108 define i32 @sextinreg2(i32 %x) {
109 ; CHECK-LABEL: @sextinreg2(
110 ; CHECK-NEXT: [[SEXT:%.*]] = shl i32 [[X:%.*]], 24
111 ; CHECK-NEXT: [[T3:%.*]] = ashr exact i32 [[SEXT]], 24
112 ; CHECK-NEXT: ret i32 [[T3]]
114 %t1 = and i32 %x, 255
115 %t2 = xor i32 %t1, 128
116 %t3 = add i32 %t2, -128
120 define <2 x i32> @sextinreg2_splat(<2 x i32> %x) {
121 ; CHECK-LABEL: @sextinreg2_splat(
122 ; CHECK-NEXT: [[SEXT:%.*]] = shl <2 x i32> [[X:%.*]], <i32 24, i32 24>
123 ; CHECK-NEXT: [[T3:%.*]] = ashr exact <2 x i32> [[SEXT]], <i32 24, i32 24>
124 ; CHECK-NEXT: ret <2 x i32> [[T3]]
126 %t1 = and <2 x i32> %x, <i32 255, i32 255>
127 %t2 = xor <2 x i32> %t1, <i32 128, i32 128>
128 %t3 = add <2 x i32> %t2, <i32 -128, i32 -128>
132 define i32 @test5(i32 %x) {
133 ; CHECK-LABEL: @test5(
134 ; CHECK-NEXT: [[T2:%.*]] = shl i32 [[X:%.*]], 16
135 ; CHECK-NEXT: [[T4:%.*]] = ashr exact i32 [[T2]], 16
136 ; CHECK-NEXT: ret i32 [[T4]]
139 %t4 = ashr i32 %t2, 16
143 ; If the shift amount equals the difference in width of the destination
144 ; and source scalar types:
145 ; ashr (shl (zext X), C), C --> sext X
147 define i32 @test6(i16 %P) {
148 ; CHECK-LABEL: @test6(
149 ; CHECK-NEXT: [[T5:%.*]] = sext i16 [[P:%.*]] to i32
150 ; CHECK-NEXT: ret i32 [[T5]]
152 %t1 = zext i16 %P to i32
153 %sext1 = shl i32 %t1, 16
154 %t5 = ashr i32 %sext1, 16
158 ; Vectors should get the same fold as above.
160 define <2 x i32> @test6_splat_vec(<2 x i12> %P) {
161 ; CHECK-LABEL: @test6_splat_vec(
162 ; CHECK-NEXT: [[ASHR:%.*]] = sext <2 x i12> [[P:%.*]] to <2 x i32>
163 ; CHECK-NEXT: ret <2 x i32> [[ASHR]]
165 %z = zext <2 x i12> %P to <2 x i32>
166 %shl = shl <2 x i32> %z, <i32 20, i32 20>
167 %ashr = ashr <2 x i32> %shl, <i32 20, i32 20>
171 define i32 @ashr(i32 %x) {
172 ; CHECK-LABEL: @ashr(
173 ; CHECK-NEXT: [[SUB:%.*]] = ashr i32 [[X:%.*]], 5
174 ; CHECK-NEXT: ret i32 [[SUB]]
176 %shr = lshr i32 %x, 5
177 %xor = xor i32 %shr, 67108864
178 %sub = add i32 %xor, -67108864
182 define <2 x i32> @ashr_splat(<2 x i32> %x) {
183 ; CHECK-LABEL: @ashr_splat(
184 ; CHECK-NEXT: [[SUB:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 5, i32 5>
185 ; CHECK-NEXT: ret <2 x i32> [[SUB]]
187 %shr = lshr <2 x i32> %x, <i32 5, i32 5>
188 %xor = xor <2 x i32> %shr, <i32 67108864, i32 67108864>
189 %sub = add <2 x i32> %xor, <i32 -67108864, i32 -67108864>