1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S -data-layout="e" | FileCheck %s --check-prefixes=ANY,LE
3 ; RUN: opt < %s -passes=instcombine -S -data-layout="E" | FileCheck %s --check-prefixes=ANY,BE
5 define i32 @shrinkExtractElt_i64_to_i32_0(<3 x i64> %x) {
6 ; LE-LABEL: @shrinkExtractElt_i64_to_i32_0(
7 ; LE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <6 x i32>
8 ; LE-NEXT: [[T:%.*]] = extractelement <6 x i32> [[TMP1]], i64 0
9 ; LE-NEXT: ret i32 [[T]]
11 ; BE-LABEL: @shrinkExtractElt_i64_to_i32_0(
12 ; BE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <6 x i32>
13 ; BE-NEXT: [[T:%.*]] = extractelement <6 x i32> [[TMP1]], i64 1
14 ; BE-NEXT: ret i32 [[T]]
16 %e = extractelement <3 x i64> %x, i32 0
17 %t = trunc i64 %e to i32
21 define i32 @vscale_shrinkExtractElt_i64_to_i32_0(<vscale x 3 x i64> %x) {
22 ; LE-LABEL: @vscale_shrinkExtractElt_i64_to_i32_0(
23 ; LE-NEXT: [[TMP1:%.*]] = bitcast <vscale x 3 x i64> [[X:%.*]] to <vscale x 6 x i32>
24 ; LE-NEXT: [[T:%.*]] = extractelement <vscale x 6 x i32> [[TMP1]], i64 0
25 ; LE-NEXT: ret i32 [[T]]
27 ; BE-LABEL: @vscale_shrinkExtractElt_i64_to_i32_0(
28 ; BE-NEXT: [[TMP1:%.*]] = bitcast <vscale x 3 x i64> [[X:%.*]] to <vscale x 6 x i32>
29 ; BE-NEXT: [[T:%.*]] = extractelement <vscale x 6 x i32> [[TMP1]], i64 1
30 ; BE-NEXT: ret i32 [[T]]
32 %e = extractelement <vscale x 3 x i64> %x, i32 0
33 %t = trunc i64 %e to i32
38 define i32 @shrinkExtractElt_i64_to_i32_1(<3 x i64> %x) {
39 ; LE-LABEL: @shrinkExtractElt_i64_to_i32_1(
40 ; LE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <6 x i32>
41 ; LE-NEXT: [[T:%.*]] = extractelement <6 x i32> [[TMP1]], i64 2
42 ; LE-NEXT: ret i32 [[T]]
44 ; BE-LABEL: @shrinkExtractElt_i64_to_i32_1(
45 ; BE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <6 x i32>
46 ; BE-NEXT: [[T:%.*]] = extractelement <6 x i32> [[TMP1]], i64 3
47 ; BE-NEXT: ret i32 [[T]]
49 %e = extractelement <3 x i64> %x, i32 1
50 %t = trunc i64 %e to i32
54 define i32 @shrinkExtractElt_i64_to_i32_2(<3 x i64> %x) {
55 ; LE-LABEL: @shrinkExtractElt_i64_to_i32_2(
56 ; LE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <6 x i32>
57 ; LE-NEXT: [[T:%.*]] = extractelement <6 x i32> [[TMP1]], i64 4
58 ; LE-NEXT: ret i32 [[T]]
60 ; BE-LABEL: @shrinkExtractElt_i64_to_i32_2(
61 ; BE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <6 x i32>
62 ; BE-NEXT: [[T:%.*]] = extractelement <6 x i32> [[TMP1]], i64 5
63 ; BE-NEXT: ret i32 [[T]]
65 %e = extractelement <3 x i64> %x, i32 2
66 %t = trunc i64 %e to i32
70 define i16 @shrinkExtractElt_i64_to_i16_0(<3 x i64> %x) {
71 ; LE-LABEL: @shrinkExtractElt_i64_to_i16_0(
72 ; LE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <12 x i16>
73 ; LE-NEXT: [[T:%.*]] = extractelement <12 x i16> [[TMP1]], i64 0
74 ; LE-NEXT: ret i16 [[T]]
76 ; BE-LABEL: @shrinkExtractElt_i64_to_i16_0(
77 ; BE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <12 x i16>
78 ; BE-NEXT: [[T:%.*]] = extractelement <12 x i16> [[TMP1]], i64 3
79 ; BE-NEXT: ret i16 [[T]]
81 %e = extractelement <3 x i64> %x, i16 0
82 %t = trunc i64 %e to i16
86 define i16 @shrinkExtractElt_i64_to_i16_1(<3 x i64> %x) {
87 ; LE-LABEL: @shrinkExtractElt_i64_to_i16_1(
88 ; LE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <12 x i16>
89 ; LE-NEXT: [[T:%.*]] = extractelement <12 x i16> [[TMP1]], i64 4
90 ; LE-NEXT: ret i16 [[T]]
92 ; BE-LABEL: @shrinkExtractElt_i64_to_i16_1(
93 ; BE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <12 x i16>
94 ; BE-NEXT: [[T:%.*]] = extractelement <12 x i16> [[TMP1]], i64 7
95 ; BE-NEXT: ret i16 [[T]]
97 %e = extractelement <3 x i64> %x, i16 1
98 %t = trunc i64 %e to i16
102 define i16 @shrinkExtractElt_i64_to_i16_2(<3 x i64> %x) {
103 ; LE-LABEL: @shrinkExtractElt_i64_to_i16_2(
104 ; LE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <12 x i16>
105 ; LE-NEXT: [[T:%.*]] = extractelement <12 x i16> [[TMP1]], i64 8
106 ; LE-NEXT: ret i16 [[T]]
108 ; BE-LABEL: @shrinkExtractElt_i64_to_i16_2(
109 ; BE-NEXT: [[TMP1:%.*]] = bitcast <3 x i64> [[X:%.*]] to <12 x i16>
110 ; BE-NEXT: [[T:%.*]] = extractelement <12 x i16> [[TMP1]], i64 11
111 ; BE-NEXT: ret i16 [[T]]
113 %e = extractelement <3 x i64> %x, i16 2
114 %t = trunc i64 %e to i16
118 ; Crazy types may be ok.
119 define i11 @shrinkExtractElt_i33_to_11_2(<3 x i33> %x) {
120 ; LE-LABEL: @shrinkExtractElt_i33_to_11_2(
121 ; LE-NEXT: [[TMP1:%.*]] = bitcast <3 x i33> [[X:%.*]] to <9 x i11>
122 ; LE-NEXT: [[T:%.*]] = extractelement <9 x i11> [[TMP1]], i64 6
123 ; LE-NEXT: ret i11 [[T]]
125 ; BE-LABEL: @shrinkExtractElt_i33_to_11_2(
126 ; BE-NEXT: [[TMP1:%.*]] = bitcast <3 x i33> [[X:%.*]] to <9 x i11>
127 ; BE-NEXT: [[T:%.*]] = extractelement <9 x i11> [[TMP1]], i64 8
128 ; BE-NEXT: ret i11 [[T]]
130 %e = extractelement <3 x i33> %x, i16 2
131 %t = trunc i33 %e to i11
135 ; Do not optimize if it would result in an invalid bitcast instruction.
136 define i13 @shrinkExtractElt_i67_to_i13_2(<3 x i67> %x) {
137 ; ANY-LABEL: @shrinkExtractElt_i67_to_i13_2(
138 ; ANY-NEXT: [[E:%.*]] = extractelement <3 x i67> [[X:%.*]], i64 2
139 ; ANY-NEXT: [[T:%.*]] = trunc i67 [[E]] to i13
140 ; ANY-NEXT: ret i13 [[T]]
142 %e = extractelement <3 x i67> %x, i459 2
143 %t = trunc i67 %e to i13
147 ; Do not optimize if the bitcast instruction would be valid, but the
148 ; transform would be wrong.
149 define i30 @shrinkExtractElt_i40_to_i30_1(<3 x i40> %x) {
150 ; ANY-LABEL: @shrinkExtractElt_i40_to_i30_1(
151 ; ANY-NEXT: [[E:%.*]] = extractelement <3 x i40> [[X:%.*]], i64 1
152 ; ANY-NEXT: [[T:%.*]] = trunc i40 [[E]] to i30
153 ; ANY-NEXT: ret i30 [[T]]
155 %e = extractelement <3 x i40> %x, i32 1
156 %t = trunc i40 %e to i30
160 ; Do not canonicalize if that would increase the instruction count.
161 declare void @use(i64)
162 define i16 @shrinkExtractElt_i64_to_i16_2_extra_use(<3 x i64> %x) {
163 ; ANY-LABEL: @shrinkExtractElt_i64_to_i16_2_extra_use(
164 ; ANY-NEXT: [[E:%.*]] = extractelement <3 x i64> [[X:%.*]], i64 2
165 ; ANY-NEXT: call void @use(i64 [[E]])
166 ; ANY-NEXT: [[T:%.*]] = trunc i64 [[E]] to i16
167 ; ANY-NEXT: ret i16 [[T]]
169 %e = extractelement <3 x i64> %x, i64 2
170 call void @use(i64 %e)
171 %t = trunc i64 %e to i16
175 ; Check to ensure PR45314 remains fixed.
176 define <4 x i64> @PR45314(<4 x i64> %x) {
177 ; LE-LABEL: @PR45314(
178 ; LE-NEXT: [[TMP1:%.*]] = bitcast <4 x i64> [[X:%.*]] to <8 x i32>
179 ; LE-NEXT: [[S:%.*]] = shufflevector <8 x i32> [[TMP1]], <8 x i32> poison, <8 x i32> zeroinitializer
180 ; LE-NEXT: [[B:%.*]] = bitcast <8 x i32> [[S]] to <4 x i64>
181 ; LE-NEXT: ret <4 x i64> [[B]]
183 ; BE-LABEL: @PR45314(
184 ; BE-NEXT: [[TMP1:%.*]] = bitcast <4 x i64> [[X:%.*]] to <8 x i32>
185 ; BE-NEXT: [[S:%.*]] = shufflevector <8 x i32> [[TMP1]], <8 x i32> poison, <8 x i32> <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
186 ; BE-NEXT: [[B:%.*]] = bitcast <8 x i32> [[S]] to <4 x i64>
187 ; BE-NEXT: ret <4 x i64> [[B]]
189 %e = extractelement <4 x i64> %x, i32 0
190 %t = trunc i64 %e to i32
191 %i = insertelement <8 x i32> poison, i32 %t, i32 0
192 %s = shufflevector <8 x i32> %i, <8 x i32> poison, <8 x i32> zeroinitializer
193 %b = bitcast <8 x i32> %s to <4 x i64>