blob: f918ad022b5839dfe2d927533e69a656dabea2fb [file] [log] [blame]
Igor Bregerb4442f32017-02-10 07:05:56 +00001//===- 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"
Igor Breger531a2032017-03-26 08:11:12 +000016#include "X86TargetMachine.h"
Igor Bregerb4442f32017-02-10 07:05:56 +000017#include "llvm/CodeGen/ValueTypes.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Type.h"
20#include "llvm/Target/TargetOpcodes.h"
21
22using namespace llvm;
Igor Breger321cf3c2017-03-03 08:06:46 +000023using namespace TargetOpcode;
Igor Bregerb4442f32017-02-10 07:05:56 +000024
Igor Breger531a2032017-03-26 08:11:12 +000025X86LegalizerInfo::X86LegalizerInfo(const X86Subtarget &STI,
26 const X86TargetMachine &TM)
27 : Subtarget(STI), TM(TM) {
Igor Bregerb4442f32017-02-10 07:05:56 +000028
29 setLegalizerInfo32bit();
30 setLegalizerInfo64bit();
Igor Breger321cf3c2017-03-03 08:06:46 +000031 setLegalizerInfoSSE1();
32 setLegalizerInfoSSE2();
Igor Breger605b9652017-05-08 09:03:37 +000033 setLegalizerInfoSSE41();
Igor Breger617be6e2017-05-23 08:23:51 +000034 setLegalizerInfoAVX();
Igor Breger605b9652017-05-08 09:03:37 +000035 setLegalizerInfoAVX2();
36 setLegalizerInfoAVX512();
37 setLegalizerInfoAVX512DQ();
38 setLegalizerInfoAVX512BW();
Igor Bregerb4442f32017-02-10 07:05:56 +000039
40 computeTables();
41}
42
43void X86LegalizerInfo::setLegalizerInfo32bit() {
44
Igor Bregera8ba5722017-03-23 15:25:57 +000045 if (Subtarget.is64Bit())
46 return;
47
48 const LLT p0 = LLT::pointer(0, 32);
Igor Breger29537882017-04-07 14:41:59 +000049 const LLT s1 = LLT::scalar(1);
Igor Bregerb4442f32017-02-10 07:05:56 +000050 const LLT s8 = LLT::scalar(8);
51 const LLT s16 = LLT::scalar(16);
52 const LLT s32 = LLT::scalar(32);
Igor Breger29537882017-04-07 14:41:59 +000053 const LLT s64 = LLT::scalar(64);
Igor Bregerb4442f32017-02-10 07:05:56 +000054
Igor Breger47be5fb2017-08-24 07:06:27 +000055 for (auto Ty : {p0, s1, s8, s16, s32})
56 setAction({G_IMPLICIT_DEF, Ty}, Legal);
57
Igor Bregerd5b59cf2017-06-28 11:39:04 +000058 for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR})
Igor Bregera8ba5722017-03-23 15:25:57 +000059 for (auto Ty : {s8, s16, s32})
60 setAction({BinOp, Ty}, Legal);
61
Igor Breger28f290f2017-05-17 12:48:08 +000062 for (unsigned Op : {G_UADDE}) {
63 setAction({Op, s32}, Legal);
64 setAction({Op, 1, s1}, Legal);
65 }
66
Igor Bregera8ba5722017-03-23 15:25:57 +000067 for (unsigned MemOp : {G_LOAD, G_STORE}) {
68 for (auto Ty : {s8, s16, s32, p0})
69 setAction({MemOp, Ty}, Legal);
70
Igor Bregerd8b51e12017-07-10 09:26:09 +000071 setAction({MemOp, s1}, WidenScalar);
Igor Bregera8ba5722017-03-23 15:25:57 +000072 // And everything's fine in addrspace 0.
73 setAction({MemOp, 1, p0}, Legal);
Igor Bregerf7359d82017-02-22 12:25:09 +000074 }
Igor Breger531a2032017-03-26 08:11:12 +000075
76 // Pointer-handling
77 setAction({G_FRAME_INDEX, p0}, Legal);
Igor Breger717bd362017-07-02 08:58:29 +000078 setAction({G_GLOBAL_VALUE, p0}, Legal);
Igor Breger29537882017-04-07 14:41:59 +000079
Igor Breger810c6252017-05-08 09:40:43 +000080 setAction({G_GEP, p0}, Legal);
81 setAction({G_GEP, 1, s32}, Legal);
82
83 for (auto Ty : {s1, s8, s16})
84 setAction({G_GEP, 1, Ty}, WidenScalar);
85
Igor Breger685889c2017-08-21 10:51:54 +000086 // Control-flow
87 setAction({G_BRCOND, s1}, Legal);
88
Igor Breger29537882017-04-07 14:41:59 +000089 // Constants
90 for (auto Ty : {s8, s16, s32, p0})
91 setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
92
93 setAction({TargetOpcode::G_CONSTANT, s1}, WidenScalar);
94 setAction({TargetOpcode::G_CONSTANT, s64}, NarrowScalar);
Igor Bregerc08a7832017-05-01 06:30:16 +000095
96 // Extensions
Igor Bregerd48c5e42017-07-10 09:07:34 +000097 for (auto Ty : {s8, s16, s32}) {
98 setAction({G_ZEXT, Ty}, Legal);
99 setAction({G_SEXT, Ty}, Legal);
100 }
Igor Bregerc08a7832017-05-01 06:30:16 +0000101
Igor Bregerfda31e62017-05-10 06:52:58 +0000102 for (auto Ty : {s1, s8, s16}) {
Igor Bregerc08a7832017-05-01 06:30:16 +0000103 setAction({G_ZEXT, 1, Ty}, Legal);
104 setAction({G_SEXT, 1, Ty}, Legal);
105 }
Igor Bregerc7b59772017-05-11 07:17:40 +0000106
107 // Comparison
108 setAction({G_ICMP, s1}, Legal);
109
110 for (auto Ty : {s8, s16, s32, p0})
111 setAction({G_ICMP, 1, Ty}, Legal);
Igor Bregerb4442f32017-02-10 07:05:56 +0000112}
Igor Bregerb4442f32017-02-10 07:05:56 +0000113
Igor Bregerf7359d82017-02-22 12:25:09 +0000114void X86LegalizerInfo::setLegalizerInfo64bit() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000115
116 if (!Subtarget.is64Bit())
117 return;
118
Igor Breger531a2032017-03-26 08:11:12 +0000119 const LLT p0 = LLT::pointer(0, TM.getPointerSize() * 8);
Igor Breger29537882017-04-07 14:41:59 +0000120 const LLT s1 = LLT::scalar(1);
Igor Bregera8ba5722017-03-23 15:25:57 +0000121 const LLT s8 = LLT::scalar(8);
122 const LLT s16 = LLT::scalar(16);
123 const LLT s32 = LLT::scalar(32);
Igor Bregerb4442f32017-02-10 07:05:56 +0000124 const LLT s64 = LLT::scalar(64);
125
Igor Breger47be5fb2017-08-24 07:06:27 +0000126 for (auto Ty : {p0, s1, s8, s16, s32, s64})
127 setAction({G_IMPLICIT_DEF, Ty}, Legal);
128
Igor Bregerd5b59cf2017-06-28 11:39:04 +0000129 for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR})
Igor Bregera8ba5722017-03-23 15:25:57 +0000130 for (auto Ty : {s8, s16, s32, s64})
131 setAction({BinOp, Ty}, Legal);
132
133 for (unsigned MemOp : {G_LOAD, G_STORE}) {
134 for (auto Ty : {s8, s16, s32, s64, p0})
135 setAction({MemOp, Ty}, Legal);
136
Igor Bregerd8b51e12017-07-10 09:26:09 +0000137 setAction({MemOp, s1}, WidenScalar);
Igor Bregera8ba5722017-03-23 15:25:57 +0000138 // And everything's fine in addrspace 0.
139 setAction({MemOp, 1, p0}, Legal);
140 }
Igor Breger531a2032017-03-26 08:11:12 +0000141
142 // Pointer-handling
143 setAction({G_FRAME_INDEX, p0}, Legal);
Igor Breger717bd362017-07-02 08:58:29 +0000144 setAction({G_GLOBAL_VALUE, p0}, Legal);
Igor Breger29537882017-04-07 14:41:59 +0000145
Igor Breger810c6252017-05-08 09:40:43 +0000146 setAction({G_GEP, p0}, Legal);
147 setAction({G_GEP, 1, s32}, Legal);
148 setAction({G_GEP, 1, s64}, Legal);
149
150 for (auto Ty : {s1, s8, s16})
151 setAction({G_GEP, 1, Ty}, WidenScalar);
152
Igor Breger685889c2017-08-21 10:51:54 +0000153 // Control-flow
154 setAction({G_BRCOND, s1}, Legal);
155
Igor Breger29537882017-04-07 14:41:59 +0000156 // Constants
157 for (auto Ty : {s8, s16, s32, s64, p0})
158 setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
159
160 setAction({TargetOpcode::G_CONSTANT, s1}, WidenScalar);
Igor Bregerc08a7832017-05-01 06:30:16 +0000161
162 // Extensions
Igor Bregerd48c5e42017-07-10 09:07:34 +0000163 for (auto Ty : {s8, s16, s32, s64}) {
Igor Bregerc08a7832017-05-01 06:30:16 +0000164 setAction({G_ZEXT, Ty}, Legal);
165 setAction({G_SEXT, Ty}, Legal);
166 }
167
Igor Bregerfda31e62017-05-10 06:52:58 +0000168 for (auto Ty : {s1, s8, s16, s32}) {
Igor Bregerc08a7832017-05-01 06:30:16 +0000169 setAction({G_ZEXT, 1, Ty}, Legal);
170 setAction({G_SEXT, 1, Ty}, Legal);
171 }
Igor Bregerc7b59772017-05-11 07:17:40 +0000172
173 // Comparison
174 setAction({G_ICMP, s1}, Legal);
175
176 for (auto Ty : {s8, s16, s32, s64, p0})
177 setAction({G_ICMP, 1, Ty}, Legal);
Igor Breger321cf3c2017-03-03 08:06:46 +0000178}
179
180void X86LegalizerInfo::setLegalizerInfoSSE1() {
181 if (!Subtarget.hasSSE1())
182 return;
183
184 const LLT s32 = LLT::scalar(32);
185 const LLT v4s32 = LLT::vector(4, 32);
Igor Bregera8ba5722017-03-23 15:25:57 +0000186 const LLT v2s64 = LLT::vector(2, 64);
Igor Breger321cf3c2017-03-03 08:06:46 +0000187
188 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMUL, G_FDIV})
189 for (auto Ty : {s32, v4s32})
190 setAction({BinOp, Ty}, Legal);
Igor Bregera8ba5722017-03-23 15:25:57 +0000191
192 for (unsigned MemOp : {G_LOAD, G_STORE})
193 for (auto Ty : {v4s32, v2s64})
194 setAction({MemOp, Ty}, Legal);
Igor Breger321cf3c2017-03-03 08:06:46 +0000195}
196
197void X86LegalizerInfo::setLegalizerInfoSSE2() {
198 if (!Subtarget.hasSSE2())
199 return;
200
201 const LLT s64 = LLT::scalar(64);
Igor Breger842b5b32017-05-18 11:10:56 +0000202 const LLT v16s8 = LLT::vector(16, 8);
Igor Breger605b9652017-05-08 09:03:37 +0000203 const LLT v8s16 = LLT::vector(8, 16);
Igor Breger321cf3c2017-03-03 08:06:46 +0000204 const LLT v4s32 = LLT::vector(4, 32);
205 const LLT v2s64 = LLT::vector(2, 64);
206
207 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMUL, G_FDIV})
208 for (auto Ty : {s64, v2s64})
209 setAction({BinOp, Ty}, Legal);
210
211 for (unsigned BinOp : {G_ADD, G_SUB})
Igor Breger842b5b32017-05-18 11:10:56 +0000212 for (auto Ty : {v16s8, v8s16, v4s32, v2s64})
Igor Breger321cf3c2017-03-03 08:06:46 +0000213 setAction({BinOp, Ty}, Legal);
Igor Breger605b9652017-05-08 09:03:37 +0000214
215 setAction({G_MUL, v8s16}, Legal);
216}
217
218void X86LegalizerInfo::setLegalizerInfoSSE41() {
219 if (!Subtarget.hasSSE41())
220 return;
221
222 const LLT v4s32 = LLT::vector(4, 32);
223
224 setAction({G_MUL, v4s32}, Legal);
225}
226
Igor Breger617be6e2017-05-23 08:23:51 +0000227void X86LegalizerInfo::setLegalizerInfoAVX() {
228 if (!Subtarget.hasAVX())
229 return;
230
Igor Breger1c29be72017-06-22 09:43:35 +0000231 const LLT v16s8 = LLT::vector(16, 8);
232 const LLT v8s16 = LLT::vector(8, 16);
233 const LLT v4s32 = LLT::vector(4, 32);
234 const LLT v2s64 = LLT::vector(2, 64);
235
236 const LLT v32s8 = LLT::vector(32, 8);
237 const LLT v16s16 = LLT::vector(16, 16);
Igor Breger617be6e2017-05-23 08:23:51 +0000238 const LLT v8s32 = LLT::vector(8, 32);
239 const LLT v4s64 = LLT::vector(4, 64);
240
241 for (unsigned MemOp : {G_LOAD, G_STORE})
242 for (auto Ty : {v8s32, v4s64})
243 setAction({MemOp, Ty}, Legal);
Igor Breger1c29be72017-06-22 09:43:35 +0000244
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000245 for (auto Ty : {v32s8, v16s16, v8s32, v4s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000246 setAction({G_INSERT, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000247 setAction({G_EXTRACT, 1, Ty}, Legal);
248 }
249 for (auto Ty : {v16s8, v8s16, v4s32, v2s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000250 setAction({G_INSERT, 1, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000251 setAction({G_EXTRACT, Ty}, Legal);
252 }
Igor Breger617be6e2017-05-23 08:23:51 +0000253}
254
Igor Breger605b9652017-05-08 09:03:37 +0000255void X86LegalizerInfo::setLegalizerInfoAVX2() {
256 if (!Subtarget.hasAVX2())
257 return;
258
Igor Breger842b5b32017-05-18 11:10:56 +0000259 const LLT v32s8 = LLT::vector(32, 8);
Igor Breger605b9652017-05-08 09:03:37 +0000260 const LLT v16s16 = LLT::vector(16, 16);
261 const LLT v8s32 = LLT::vector(8, 32);
Igor Breger842b5b32017-05-18 11:10:56 +0000262 const LLT v4s64 = LLT::vector(4, 64);
263
264 for (unsigned BinOp : {G_ADD, G_SUB})
265 for (auto Ty : {v32s8, v16s16, v8s32, v4s64})
266 setAction({BinOp, Ty}, Legal);
Igor Breger605b9652017-05-08 09:03:37 +0000267
268 for (auto Ty : {v16s16, v8s32})
269 setAction({G_MUL, Ty}, Legal);
270}
271
272void X86LegalizerInfo::setLegalizerInfoAVX512() {
273 if (!Subtarget.hasAVX512())
274 return;
275
Igor Breger1c29be72017-06-22 09:43:35 +0000276 const LLT v16s8 = LLT::vector(16, 8);
277 const LLT v8s16 = LLT::vector(8, 16);
278 const LLT v4s32 = LLT::vector(4, 32);
279 const LLT v2s64 = LLT::vector(2, 64);
280
281 const LLT v32s8 = LLT::vector(32, 8);
282 const LLT v16s16 = LLT::vector(16, 16);
283 const LLT v8s32 = LLT::vector(8, 32);
284 const LLT v4s64 = LLT::vector(4, 64);
285
286 const LLT v64s8 = LLT::vector(64, 8);
287 const LLT v32s16 = LLT::vector(32, 16);
Igor Breger605b9652017-05-08 09:03:37 +0000288 const LLT v16s32 = LLT::vector(16, 32);
Igor Breger842b5b32017-05-18 11:10:56 +0000289 const LLT v8s64 = LLT::vector(8, 64);
290
291 for (unsigned BinOp : {G_ADD, G_SUB})
292 for (auto Ty : {v16s32, v8s64})
293 setAction({BinOp, Ty}, Legal);
Igor Breger605b9652017-05-08 09:03:37 +0000294
295 setAction({G_MUL, v16s32}, Legal);
296
Igor Breger617be6e2017-05-23 08:23:51 +0000297 for (unsigned MemOp : {G_LOAD, G_STORE})
298 for (auto Ty : {v16s32, v8s64})
299 setAction({MemOp, Ty}, Legal);
300
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000301 for (auto Ty : {v64s8, v32s16, v16s32, v8s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000302 setAction({G_INSERT, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000303 setAction({G_EXTRACT, 1, Ty}, Legal);
304 }
305 for (auto Ty : {v32s8, v16s16, v8s32, v4s64, v16s8, v8s16, v4s32, v2s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000306 setAction({G_INSERT, 1, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000307 setAction({G_EXTRACT, Ty}, Legal);
308 }
Igor Breger1c29be72017-06-22 09:43:35 +0000309
Igor Breger605b9652017-05-08 09:03:37 +0000310 /************ VLX *******************/
311 if (!Subtarget.hasVLX())
312 return;
313
Igor Breger605b9652017-05-08 09:03:37 +0000314 for (auto Ty : {v4s32, v8s32})
315 setAction({G_MUL, Ty}, Legal);
316}
317
318void X86LegalizerInfo::setLegalizerInfoAVX512DQ() {
319 if (!(Subtarget.hasAVX512() && Subtarget.hasDQI()))
320 return;
321
322 const LLT v8s64 = LLT::vector(8, 64);
323
324 setAction({G_MUL, v8s64}, Legal);
325
326 /************ VLX *******************/
327 if (!Subtarget.hasVLX())
328 return;
329
330 const LLT v2s64 = LLT::vector(2, 64);
331 const LLT v4s64 = LLT::vector(4, 64);
332
333 for (auto Ty : {v2s64, v4s64})
334 setAction({G_MUL, Ty}, Legal);
335}
336
337void X86LegalizerInfo::setLegalizerInfoAVX512BW() {
338 if (!(Subtarget.hasAVX512() && Subtarget.hasBWI()))
339 return;
340
Igor Breger842b5b32017-05-18 11:10:56 +0000341 const LLT v64s8 = LLT::vector(64, 8);
Igor Breger605b9652017-05-08 09:03:37 +0000342 const LLT v32s16 = LLT::vector(32, 16);
343
Igor Breger842b5b32017-05-18 11:10:56 +0000344 for (unsigned BinOp : {G_ADD, G_SUB})
345 for (auto Ty : {v64s8, v32s16})
346 setAction({BinOp, Ty}, Legal);
347
Igor Breger605b9652017-05-08 09:03:37 +0000348 setAction({G_MUL, v32s16}, Legal);
349
350 /************ VLX *******************/
351 if (!Subtarget.hasVLX())
352 return;
353
354 const LLT v8s16 = LLT::vector(8, 16);
355 const LLT v16s16 = LLT::vector(16, 16);
356
357 for (auto Ty : {v8s16, v16s16})
358 setAction({G_MUL, Ty}, Legal);
Igor Bregerb4442f32017-02-10 07:05:56 +0000359}