121
|
1 //===- X86LegalizerInfo.cpp --------------------------------------*- C++ -*-==//
|
|
2 //
|
|
3 // The LLVM Compiler Infrastructure
|
|
4 //
|
|
5 // This file is distributed under the University of Illinois Open Source
|
|
6 // License. See LICENSE.TXT for details.
|
|
7 //
|
|
8 //===----------------------------------------------------------------------===//
|
|
9 /// \file
|
|
10 /// This file implements the targeting of the Machinelegalizer class for X86.
|
|
11 /// \todo This should be generated by TableGen.
|
|
12 //===----------------------------------------------------------------------===//
|
|
13
|
|
14 #include "X86LegalizerInfo.h"
|
|
15 #include "X86Subtarget.h"
|
|
16 #include "X86TargetMachine.h"
|
134
|
17 #include "llvm/CodeGen/TargetOpcodes.h"
|
121
|
18 #include "llvm/CodeGen/ValueTypes.h"
|
|
19 #include "llvm/IR/DerivedTypes.h"
|
|
20 #include "llvm/IR/Type.h"
|
|
21
|
|
22 using namespace llvm;
|
|
23 using namespace TargetOpcode;
|
134
|
24 using namespace LegalizeActions;
|
|
25
|
|
26 /// FIXME: The following static functions are SizeChangeStrategy functions
|
|
27 /// that are meant to temporarily mimic the behaviour of the old legalization
|
|
28 /// based on doubling/halving non-legal types as closely as possible. This is
|
|
29 /// not entirly possible as only legalizing the types that are exactly a power
|
|
30 /// of 2 times the size of the legal types would require specifying all those
|
|
31 /// sizes explicitly.
|
|
32 /// In practice, not specifying those isn't a problem, and the below functions
|
|
33 /// should disappear quickly as we add support for legalizing non-power-of-2
|
|
34 /// sized types further.
|
|
35 static void
|
|
36 addAndInterleaveWithUnsupported(LegalizerInfo::SizeAndActionsVec &result,
|
|
37 const LegalizerInfo::SizeAndActionsVec &v) {
|
|
38 for (unsigned i = 0; i < v.size(); ++i) {
|
|
39 result.push_back(v[i]);
|
|
40 if (i + 1 < v[i].first && i + 1 < v.size() &&
|
|
41 v[i + 1].first != v[i].first + 1)
|
|
42 result.push_back({v[i].first + 1, Unsupported});
|
|
43 }
|
|
44 }
|
|
45
|
|
46 static LegalizerInfo::SizeAndActionsVec
|
|
47 widen_1(const LegalizerInfo::SizeAndActionsVec &v) {
|
|
48 assert(v.size() >= 1);
|
|
49 assert(v[0].first > 1);
|
|
50 LegalizerInfo::SizeAndActionsVec result = {{1, WidenScalar},
|
|
51 {2, Unsupported}};
|
|
52 addAndInterleaveWithUnsupported(result, v);
|
|
53 auto Largest = result.back().first;
|
|
54 result.push_back({Largest + 1, Unsupported});
|
|
55 return result;
|
|
56 }
|
121
|
57
|
|
58 X86LegalizerInfo::X86LegalizerInfo(const X86Subtarget &STI,
|
|
59 const X86TargetMachine &TM)
|
|
60 : Subtarget(STI), TM(TM) {
|
|
61
|
|
62 setLegalizerInfo32bit();
|
|
63 setLegalizerInfo64bit();
|
|
64 setLegalizerInfoSSE1();
|
|
65 setLegalizerInfoSSE2();
|
|
66 setLegalizerInfoSSE41();
|
|
67 setLegalizerInfoAVX();
|
|
68 setLegalizerInfoAVX2();
|
|
69 setLegalizerInfoAVX512();
|
|
70 setLegalizerInfoAVX512DQ();
|
|
71 setLegalizerInfoAVX512BW();
|
|
72
|
134
|
73 setLegalizeScalarToDifferentSizeStrategy(G_PHI, 0, widen_1);
|
|
74 for (unsigned BinOp : {G_SUB, G_MUL, G_AND, G_OR, G_XOR})
|
|
75 setLegalizeScalarToDifferentSizeStrategy(BinOp, 0, widen_1);
|
|
76 for (unsigned MemOp : {G_LOAD, G_STORE})
|
|
77 setLegalizeScalarToDifferentSizeStrategy(MemOp, 0,
|
|
78 narrowToSmallerAndWidenToSmallest);
|
|
79 setLegalizeScalarToDifferentSizeStrategy(
|
|
80 G_GEP, 1, widenToLargerTypesUnsupportedOtherwise);
|
|
81 setLegalizeScalarToDifferentSizeStrategy(
|
|
82 G_CONSTANT, 0, widenToLargerTypesAndNarrowToLargest);
|
|
83
|
121
|
84 computeTables();
|
|
85 }
|
|
86
|
|
87 void X86LegalizerInfo::setLegalizerInfo32bit() {
|
|
88
|
|
89 const LLT p0 = LLT::pointer(0, TM.getPointerSize() * 8);
|
|
90 const LLT s1 = LLT::scalar(1);
|
|
91 const LLT s8 = LLT::scalar(8);
|
|
92 const LLT s16 = LLT::scalar(16);
|
|
93 const LLT s32 = LLT::scalar(32);
|
|
94 const LLT s64 = LLT::scalar(64);
|
134
|
95 const LLT s128 = LLT::scalar(128);
|
121
|
96
|
|
97 for (auto Ty : {p0, s1, s8, s16, s32})
|
|
98 setAction({G_IMPLICIT_DEF, Ty}, Legal);
|
|
99
|
|
100 for (auto Ty : {s8, s16, s32, p0})
|
|
101 setAction({G_PHI, Ty}, Legal);
|
|
102
|
134
|
103 for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR})
|
121
|
104 for (auto Ty : {s8, s16, s32})
|
|
105 setAction({BinOp, Ty}, Legal);
|
|
106
|
|
107 for (unsigned Op : {G_UADDE}) {
|
|
108 setAction({Op, s32}, Legal);
|
|
109 setAction({Op, 1, s1}, Legal);
|
|
110 }
|
|
111
|
|
112 for (unsigned MemOp : {G_LOAD, G_STORE}) {
|
|
113 for (auto Ty : {s8, s16, s32, p0})
|
|
114 setAction({MemOp, Ty}, Legal);
|
|
115
|
|
116 // And everything's fine in addrspace 0.
|
|
117 setAction({MemOp, 1, p0}, Legal);
|
|
118 }
|
|
119
|
|
120 // Pointer-handling
|
|
121 setAction({G_FRAME_INDEX, p0}, Legal);
|
|
122 setAction({G_GLOBAL_VALUE, p0}, Legal);
|
|
123
|
|
124 setAction({G_GEP, p0}, Legal);
|
|
125 setAction({G_GEP, 1, s32}, Legal);
|
|
126
|
|
127 // Control-flow
|
|
128 setAction({G_BRCOND, s1}, Legal);
|
|
129
|
|
130 // Constants
|
|
131 for (auto Ty : {s8, s16, s32, p0})
|
|
132 setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
|
|
133
|
|
134 // Extensions
|
|
135 for (auto Ty : {s8, s16, s32}) {
|
|
136 setAction({G_ZEXT, Ty}, Legal);
|
|
137 setAction({G_SEXT, Ty}, Legal);
|
|
138 setAction({G_ANYEXT, Ty}, Legal);
|
|
139 }
|
134
|
140 setAction({G_ANYEXT, s128}, Legal);
|
121
|
141
|
|
142 // Comparison
|
|
143 setAction({G_ICMP, s1}, Legal);
|
|
144
|
|
145 for (auto Ty : {s8, s16, s32, p0})
|
|
146 setAction({G_ICMP, 1, Ty}, Legal);
|
134
|
147
|
|
148 // Merge/Unmerge
|
|
149 for (const auto &Ty : {s16, s32, s64}) {
|
|
150 setAction({G_MERGE_VALUES, Ty}, Legal);
|
|
151 setAction({G_UNMERGE_VALUES, 1, Ty}, Legal);
|
|
152 }
|
|
153 for (const auto &Ty : {s8, s16, s32}) {
|
|
154 setAction({G_MERGE_VALUES, 1, Ty}, Legal);
|
|
155 setAction({G_UNMERGE_VALUES, Ty}, Legal);
|
|
156 }
|
121
|
157 }
|
|
158
|
|
159 void X86LegalizerInfo::setLegalizerInfo64bit() {
|
|
160
|
|
161 if (!Subtarget.is64Bit())
|
|
162 return;
|
|
163
|
|
164 const LLT s64 = LLT::scalar(64);
|
134
|
165 const LLT s128 = LLT::scalar(128);
|
121
|
166
|
|
167 setAction({G_IMPLICIT_DEF, s64}, Legal);
|
134
|
168 // Need to have that, as tryFoldImplicitDef will create this pattern:
|
|
169 // s128 = EXTEND (G_IMPLICIT_DEF s32/s64) -> s128 = G_IMPLICIT_DEF
|
|
170 setAction({G_IMPLICIT_DEF, s128}, Legal);
|
121
|
171
|
|
172 setAction({G_PHI, s64}, Legal);
|
|
173
|
|
174 for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR})
|
|
175 setAction({BinOp, s64}, Legal);
|
|
176
|
|
177 for (unsigned MemOp : {G_LOAD, G_STORE})
|
|
178 setAction({MemOp, s64}, Legal);
|
|
179
|
|
180 // Pointer-handling
|
|
181 setAction({G_GEP, 1, s64}, Legal);
|
|
182
|
|
183 // Constants
|
|
184 setAction({TargetOpcode::G_CONSTANT, s64}, Legal);
|
|
185
|
|
186 // Extensions
|
|
187 for (unsigned extOp : {G_ZEXT, G_SEXT, G_ANYEXT}) {
|
|
188 setAction({extOp, s64}, Legal);
|
|
189 }
|
|
190
|
|
191 // Comparison
|
|
192 setAction({G_ICMP, 1, s64}, Legal);
|
134
|
193
|
|
194 // Merge/Unmerge
|
|
195 setAction({G_MERGE_VALUES, s128}, Legal);
|
|
196 setAction({G_UNMERGE_VALUES, 1, s128}, Legal);
|
|
197 setAction({G_MERGE_VALUES, 1, s128}, Legal);
|
|
198 setAction({G_UNMERGE_VALUES, s128}, Legal);
|
121
|
199 }
|
|
200
|
|
201 void X86LegalizerInfo::setLegalizerInfoSSE1() {
|
|
202 if (!Subtarget.hasSSE1())
|
|
203 return;
|
|
204
|
|
205 const LLT s32 = LLT::scalar(32);
|
134
|
206 const LLT s64 = LLT::scalar(64);
|
121
|
207 const LLT v4s32 = LLT::vector(4, 32);
|
|
208 const LLT v2s64 = LLT::vector(2, 64);
|
|
209
|
|
210 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMUL, G_FDIV})
|
|
211 for (auto Ty : {s32, v4s32})
|
|
212 setAction({BinOp, Ty}, Legal);
|
|
213
|
|
214 for (unsigned MemOp : {G_LOAD, G_STORE})
|
|
215 for (auto Ty : {v4s32, v2s64})
|
|
216 setAction({MemOp, Ty}, Legal);
|
|
217
|
|
218 // Constants
|
|
219 setAction({TargetOpcode::G_FCONSTANT, s32}, Legal);
|
134
|
220
|
|
221 // Merge/Unmerge
|
|
222 for (const auto &Ty : {v4s32, v2s64}) {
|
|
223 setAction({G_MERGE_VALUES, Ty}, Legal);
|
|
224 setAction({G_UNMERGE_VALUES, 1, Ty}, Legal);
|
|
225 }
|
|
226 setAction({G_MERGE_VALUES, 1, s64}, Legal);
|
|
227 setAction({G_UNMERGE_VALUES, s64}, Legal);
|
121
|
228 }
|
|
229
|
|
230 void X86LegalizerInfo::setLegalizerInfoSSE2() {
|
|
231 if (!Subtarget.hasSSE2())
|
|
232 return;
|
|
233
|
|
234 const LLT s32 = LLT::scalar(32);
|
|
235 const LLT s64 = LLT::scalar(64);
|
|
236 const LLT v16s8 = LLT::vector(16, 8);
|
|
237 const LLT v8s16 = LLT::vector(8, 16);
|
|
238 const LLT v4s32 = LLT::vector(4, 32);
|
|
239 const LLT v2s64 = LLT::vector(2, 64);
|
|
240
|
134
|
241 const LLT v32s8 = LLT::vector(32, 8);
|
|
242 const LLT v16s16 = LLT::vector(16, 16);
|
|
243 const LLT v8s32 = LLT::vector(8, 32);
|
|
244 const LLT v4s64 = LLT::vector(4, 64);
|
|
245
|
121
|
246 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMUL, G_FDIV})
|
|
247 for (auto Ty : {s64, v2s64})
|
|
248 setAction({BinOp, Ty}, Legal);
|
|
249
|
|
250 for (unsigned BinOp : {G_ADD, G_SUB})
|
|
251 for (auto Ty : {v16s8, v8s16, v4s32, v2s64})
|
|
252 setAction({BinOp, Ty}, Legal);
|
|
253
|
|
254 setAction({G_MUL, v8s16}, Legal);
|
|
255
|
|
256 setAction({G_FPEXT, s64}, Legal);
|
|
257 setAction({G_FPEXT, 1, s32}, Legal);
|
|
258
|
|
259 // Constants
|
|
260 setAction({TargetOpcode::G_FCONSTANT, s64}, Legal);
|
134
|
261
|
|
262 // Merge/Unmerge
|
|
263 for (const auto &Ty :
|
|
264 {v16s8, v32s8, v8s16, v16s16, v4s32, v8s32, v2s64, v4s64}) {
|
|
265 setAction({G_MERGE_VALUES, Ty}, Legal);
|
|
266 setAction({G_UNMERGE_VALUES, 1, Ty}, Legal);
|
|
267 }
|
|
268 for (const auto &Ty : {v16s8, v8s16, v4s32, v2s64}) {
|
|
269 setAction({G_MERGE_VALUES, 1, Ty}, Legal);
|
|
270 setAction({G_UNMERGE_VALUES, Ty}, Legal);
|
|
271 }
|
121
|
272 }
|
|
273
|
|
274 void X86LegalizerInfo::setLegalizerInfoSSE41() {
|
|
275 if (!Subtarget.hasSSE41())
|
|
276 return;
|
|
277
|
|
278 const LLT v4s32 = LLT::vector(4, 32);
|
|
279
|
|
280 setAction({G_MUL, v4s32}, Legal);
|
|
281 }
|
|
282
|
|
283 void X86LegalizerInfo::setLegalizerInfoAVX() {
|
|
284 if (!Subtarget.hasAVX())
|
|
285 return;
|
|
286
|
|
287 const LLT v16s8 = LLT::vector(16, 8);
|
|
288 const LLT v8s16 = LLT::vector(8, 16);
|
|
289 const LLT v4s32 = LLT::vector(4, 32);
|
|
290 const LLT v2s64 = LLT::vector(2, 64);
|
|
291
|
|
292 const LLT v32s8 = LLT::vector(32, 8);
|
134
|
293 const LLT v64s8 = LLT::vector(64, 8);
|
121
|
294 const LLT v16s16 = LLT::vector(16, 16);
|
134
|
295 const LLT v32s16 = LLT::vector(32, 16);
|
121
|
296 const LLT v8s32 = LLT::vector(8, 32);
|
134
|
297 const LLT v16s32 = LLT::vector(16, 32);
|
121
|
298 const LLT v4s64 = LLT::vector(4, 64);
|
134
|
299 const LLT v8s64 = LLT::vector(8, 64);
|
121
|
300
|
|
301 for (unsigned MemOp : {G_LOAD, G_STORE})
|
|
302 for (auto Ty : {v8s32, v4s64})
|
|
303 setAction({MemOp, Ty}, Legal);
|
|
304
|
|
305 for (auto Ty : {v32s8, v16s16, v8s32, v4s64}) {
|
|
306 setAction({G_INSERT, Ty}, Legal);
|
|
307 setAction({G_EXTRACT, 1, Ty}, Legal);
|
|
308 }
|
|
309 for (auto Ty : {v16s8, v8s16, v4s32, v2s64}) {
|
|
310 setAction({G_INSERT, 1, Ty}, Legal);
|
|
311 setAction({G_EXTRACT, Ty}, Legal);
|
|
312 }
|
134
|
313 // Merge/Unmerge
|
|
314 for (const auto &Ty :
|
|
315 {v32s8, v64s8, v16s16, v32s16, v8s32, v16s32, v4s64, v8s64}) {
|
|
316 setAction({G_MERGE_VALUES, Ty}, Legal);
|
|
317 setAction({G_UNMERGE_VALUES, 1, Ty}, Legal);
|
|
318 }
|
|
319 for (const auto &Ty :
|
|
320 {v16s8, v32s8, v8s16, v16s16, v4s32, v8s32, v2s64, v4s64}) {
|
|
321 setAction({G_MERGE_VALUES, 1, Ty}, Legal);
|
|
322 setAction({G_UNMERGE_VALUES, Ty}, Legal);
|
|
323 }
|
121
|
324 }
|
|
325
|
|
326 void X86LegalizerInfo::setLegalizerInfoAVX2() {
|
|
327 if (!Subtarget.hasAVX2())
|
|
328 return;
|
|
329
|
|
330 const LLT v32s8 = LLT::vector(32, 8);
|
|
331 const LLT v16s16 = LLT::vector(16, 16);
|
|
332 const LLT v8s32 = LLT::vector(8, 32);
|
|
333 const LLT v4s64 = LLT::vector(4, 64);
|
|
334
|
134
|
335 const LLT v64s8 = LLT::vector(64, 8);
|
|
336 const LLT v32s16 = LLT::vector(32, 16);
|
|
337 const LLT v16s32 = LLT::vector(16, 32);
|
|
338 const LLT v8s64 = LLT::vector(8, 64);
|
|
339
|
121
|
340 for (unsigned BinOp : {G_ADD, G_SUB})
|
|
341 for (auto Ty : {v32s8, v16s16, v8s32, v4s64})
|
|
342 setAction({BinOp, Ty}, Legal);
|
|
343
|
|
344 for (auto Ty : {v16s16, v8s32})
|
|
345 setAction({G_MUL, Ty}, Legal);
|
134
|
346
|
|
347 // Merge/Unmerge
|
|
348 for (const auto &Ty : {v64s8, v32s16, v16s32, v8s64}) {
|
|
349 setAction({G_MERGE_VALUES, Ty}, Legal);
|
|
350 setAction({G_UNMERGE_VALUES, 1, Ty}, Legal);
|
|
351 }
|
|
352 for (const auto &Ty : {v32s8, v16s16, v8s32, v4s64}) {
|
|
353 setAction({G_MERGE_VALUES, 1, Ty}, Legal);
|
|
354 setAction({G_UNMERGE_VALUES, Ty}, Legal);
|
|
355 }
|
121
|
356 }
|
|
357
|
|
358 void X86LegalizerInfo::setLegalizerInfoAVX512() {
|
|
359 if (!Subtarget.hasAVX512())
|
|
360 return;
|
|
361
|
|
362 const LLT v16s8 = LLT::vector(16, 8);
|
|
363 const LLT v8s16 = LLT::vector(8, 16);
|
|
364 const LLT v4s32 = LLT::vector(4, 32);
|
|
365 const LLT v2s64 = LLT::vector(2, 64);
|
|
366
|
|
367 const LLT v32s8 = LLT::vector(32, 8);
|
|
368 const LLT v16s16 = LLT::vector(16, 16);
|
|
369 const LLT v8s32 = LLT::vector(8, 32);
|
|
370 const LLT v4s64 = LLT::vector(4, 64);
|
|
371
|
|
372 const LLT v64s8 = LLT::vector(64, 8);
|
|
373 const LLT v32s16 = LLT::vector(32, 16);
|
|
374 const LLT v16s32 = LLT::vector(16, 32);
|
|
375 const LLT v8s64 = LLT::vector(8, 64);
|
|
376
|
|
377 for (unsigned BinOp : {G_ADD, G_SUB})
|
|
378 for (auto Ty : {v16s32, v8s64})
|
|
379 setAction({BinOp, Ty}, Legal);
|
|
380
|
|
381 setAction({G_MUL, v16s32}, Legal);
|
|
382
|
|
383 for (unsigned MemOp : {G_LOAD, G_STORE})
|
|
384 for (auto Ty : {v16s32, v8s64})
|
|
385 setAction({MemOp, Ty}, Legal);
|
|
386
|
|
387 for (auto Ty : {v64s8, v32s16, v16s32, v8s64}) {
|
|
388 setAction({G_INSERT, Ty}, Legal);
|
|
389 setAction({G_EXTRACT, 1, Ty}, Legal);
|
|
390 }
|
|
391 for (auto Ty : {v32s8, v16s16, v8s32, v4s64, v16s8, v8s16, v4s32, v2s64}) {
|
|
392 setAction({G_INSERT, 1, Ty}, Legal);
|
|
393 setAction({G_EXTRACT, Ty}, Legal);
|
|
394 }
|
|
395
|
|
396 /************ VLX *******************/
|
|
397 if (!Subtarget.hasVLX())
|
|
398 return;
|
|
399
|
|
400 for (auto Ty : {v4s32, v8s32})
|
|
401 setAction({G_MUL, Ty}, Legal);
|
|
402 }
|
|
403
|
|
404 void X86LegalizerInfo::setLegalizerInfoAVX512DQ() {
|
|
405 if (!(Subtarget.hasAVX512() && Subtarget.hasDQI()))
|
|
406 return;
|
|
407
|
|
408 const LLT v8s64 = LLT::vector(8, 64);
|
|
409
|
|
410 setAction({G_MUL, v8s64}, Legal);
|
|
411
|
|
412 /************ VLX *******************/
|
|
413 if (!Subtarget.hasVLX())
|
|
414 return;
|
|
415
|
|
416 const LLT v2s64 = LLT::vector(2, 64);
|
|
417 const LLT v4s64 = LLT::vector(4, 64);
|
|
418
|
|
419 for (auto Ty : {v2s64, v4s64})
|
|
420 setAction({G_MUL, Ty}, Legal);
|
|
421 }
|
|
422
|
|
423 void X86LegalizerInfo::setLegalizerInfoAVX512BW() {
|
|
424 if (!(Subtarget.hasAVX512() && Subtarget.hasBWI()))
|
|
425 return;
|
|
426
|
|
427 const LLT v64s8 = LLT::vector(64, 8);
|
|
428 const LLT v32s16 = LLT::vector(32, 16);
|
|
429
|
|
430 for (unsigned BinOp : {G_ADD, G_SUB})
|
|
431 for (auto Ty : {v64s8, v32s16})
|
|
432 setAction({BinOp, Ty}, Legal);
|
|
433
|
|
434 setAction({G_MUL, v32s16}, Legal);
|
|
435
|
|
436 /************ VLX *******************/
|
|
437 if (!Subtarget.hasVLX())
|
|
438 return;
|
|
439
|
|
440 const LLT v8s16 = LLT::vector(8, 16);
|
|
441 const LLT v16s16 = LLT::vector(16, 16);
|
|
442
|
|
443 for (auto Ty : {v8s16, v16s16})
|
|
444 setAction({G_MUL, Ty}, Legal);
|
|
445 }
|