blob: 05fde0fb92d3e1fc4ea03ac815974cffa727c3e7 [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 Bregerd5b59cf2017-06-28 11:39:04 +000055 for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR})
Igor Bregera8ba5722017-03-23 15:25:57 +000056 for (auto Ty : {s8, s16, s32})
57 setAction({BinOp, Ty}, Legal);
58
Igor Breger28f290f2017-05-17 12:48:08 +000059 for (unsigned Op : {G_UADDE}) {
60 setAction({Op, s32}, Legal);
61 setAction({Op, 1, s1}, Legal);
62 }
63
Igor Bregera8ba5722017-03-23 15:25:57 +000064 for (unsigned MemOp : {G_LOAD, G_STORE}) {
65 for (auto Ty : {s8, s16, s32, p0})
66 setAction({MemOp, Ty}, Legal);
67
Igor Bregerd8b51e12017-07-10 09:26:09 +000068 setAction({MemOp, s1}, WidenScalar);
Igor Bregera8ba5722017-03-23 15:25:57 +000069 // And everything's fine in addrspace 0.
70 setAction({MemOp, 1, p0}, Legal);
Igor Bregerf7359d82017-02-22 12:25:09 +000071 }
Igor Breger531a2032017-03-26 08:11:12 +000072
73 // Pointer-handling
74 setAction({G_FRAME_INDEX, p0}, Legal);
Igor Breger717bd362017-07-02 08:58:29 +000075 setAction({G_GLOBAL_VALUE, p0}, Legal);
Igor Breger29537882017-04-07 14:41:59 +000076
Igor Breger810c6252017-05-08 09:40:43 +000077 setAction({G_GEP, p0}, Legal);
78 setAction({G_GEP, 1, s32}, Legal);
79
80 for (auto Ty : {s1, s8, s16})
81 setAction({G_GEP, 1, Ty}, WidenScalar);
82
Igor Breger685889c2017-08-21 10:51:54 +000083 // Control-flow
84 setAction({G_BRCOND, s1}, Legal);
85
Igor Breger29537882017-04-07 14:41:59 +000086 // Constants
87 for (auto Ty : {s8, s16, s32, p0})
88 setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
89
90 setAction({TargetOpcode::G_CONSTANT, s1}, WidenScalar);
91 setAction({TargetOpcode::G_CONSTANT, s64}, NarrowScalar);
Igor Bregerc08a7832017-05-01 06:30:16 +000092
93 // Extensions
Igor Bregerd48c5e42017-07-10 09:07:34 +000094 for (auto Ty : {s8, s16, s32}) {
95 setAction({G_ZEXT, Ty}, Legal);
96 setAction({G_SEXT, Ty}, Legal);
97 }
Igor Bregerc08a7832017-05-01 06:30:16 +000098
Igor Bregerfda31e62017-05-10 06:52:58 +000099 for (auto Ty : {s1, s8, s16}) {
Igor Bregerc08a7832017-05-01 06:30:16 +0000100 setAction({G_ZEXT, 1, Ty}, Legal);
101 setAction({G_SEXT, 1, Ty}, Legal);
102 }
Igor Bregerc7b59772017-05-11 07:17:40 +0000103
104 // Comparison
105 setAction({G_ICMP, s1}, Legal);
106
107 for (auto Ty : {s8, s16, s32, p0})
108 setAction({G_ICMP, 1, Ty}, Legal);
Igor Bregerb4442f32017-02-10 07:05:56 +0000109}
Igor Bregerb4442f32017-02-10 07:05:56 +0000110
Igor Bregerf7359d82017-02-22 12:25:09 +0000111void X86LegalizerInfo::setLegalizerInfo64bit() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000112
113 if (!Subtarget.is64Bit())
114 return;
115
Igor Breger531a2032017-03-26 08:11:12 +0000116 const LLT p0 = LLT::pointer(0, TM.getPointerSize() * 8);
Igor Breger29537882017-04-07 14:41:59 +0000117 const LLT s1 = LLT::scalar(1);
Igor Bregera8ba5722017-03-23 15:25:57 +0000118 const LLT s8 = LLT::scalar(8);
119 const LLT s16 = LLT::scalar(16);
120 const LLT s32 = LLT::scalar(32);
Igor Bregerb4442f32017-02-10 07:05:56 +0000121 const LLT s64 = LLT::scalar(64);
122
Igor Bregerd5b59cf2017-06-28 11:39:04 +0000123 for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR})
Igor Bregera8ba5722017-03-23 15:25:57 +0000124 for (auto Ty : {s8, s16, s32, s64})
125 setAction({BinOp, Ty}, Legal);
126
127 for (unsigned MemOp : {G_LOAD, G_STORE}) {
128 for (auto Ty : {s8, s16, s32, s64, p0})
129 setAction({MemOp, Ty}, Legal);
130
Igor Bregerd8b51e12017-07-10 09:26:09 +0000131 setAction({MemOp, s1}, WidenScalar);
Igor Bregera8ba5722017-03-23 15:25:57 +0000132 // And everything's fine in addrspace 0.
133 setAction({MemOp, 1, p0}, Legal);
134 }
Igor Breger531a2032017-03-26 08:11:12 +0000135
136 // Pointer-handling
137 setAction({G_FRAME_INDEX, p0}, Legal);
Igor Breger717bd362017-07-02 08:58:29 +0000138 setAction({G_GLOBAL_VALUE, p0}, Legal);
Igor Breger29537882017-04-07 14:41:59 +0000139
Igor Breger810c6252017-05-08 09:40:43 +0000140 setAction({G_GEP, p0}, Legal);
141 setAction({G_GEP, 1, s32}, Legal);
142 setAction({G_GEP, 1, s64}, Legal);
143
144 for (auto Ty : {s1, s8, s16})
145 setAction({G_GEP, 1, Ty}, WidenScalar);
146
Igor Breger685889c2017-08-21 10:51:54 +0000147 // Control-flow
148 setAction({G_BRCOND, s1}, Legal);
149
Igor Breger29537882017-04-07 14:41:59 +0000150 // Constants
151 for (auto Ty : {s8, s16, s32, s64, p0})
152 setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
153
154 setAction({TargetOpcode::G_CONSTANT, s1}, WidenScalar);
Igor Bregerc08a7832017-05-01 06:30:16 +0000155
156 // Extensions
Igor Bregerd48c5e42017-07-10 09:07:34 +0000157 for (auto Ty : {s8, s16, s32, s64}) {
Igor Bregerc08a7832017-05-01 06:30:16 +0000158 setAction({G_ZEXT, Ty}, Legal);
159 setAction({G_SEXT, Ty}, Legal);
160 }
161
Igor Bregerfda31e62017-05-10 06:52:58 +0000162 for (auto Ty : {s1, s8, s16, s32}) {
Igor Bregerc08a7832017-05-01 06:30:16 +0000163 setAction({G_ZEXT, 1, Ty}, Legal);
164 setAction({G_SEXT, 1, Ty}, Legal);
165 }
Igor Bregerc7b59772017-05-11 07:17:40 +0000166
167 // Comparison
168 setAction({G_ICMP, s1}, Legal);
169
170 for (auto Ty : {s8, s16, s32, s64, p0})
171 setAction({G_ICMP, 1, Ty}, Legal);
Igor Breger321cf3c2017-03-03 08:06:46 +0000172}
173
174void X86LegalizerInfo::setLegalizerInfoSSE1() {
175 if (!Subtarget.hasSSE1())
176 return;
177
178 const LLT s32 = LLT::scalar(32);
179 const LLT v4s32 = LLT::vector(4, 32);
Igor Bregera8ba5722017-03-23 15:25:57 +0000180 const LLT v2s64 = LLT::vector(2, 64);
Igor Breger321cf3c2017-03-03 08:06:46 +0000181
182 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMUL, G_FDIV})
183 for (auto Ty : {s32, v4s32})
184 setAction({BinOp, Ty}, Legal);
Igor Bregera8ba5722017-03-23 15:25:57 +0000185
186 for (unsigned MemOp : {G_LOAD, G_STORE})
187 for (auto Ty : {v4s32, v2s64})
188 setAction({MemOp, Ty}, Legal);
Igor Breger321cf3c2017-03-03 08:06:46 +0000189}
190
191void X86LegalizerInfo::setLegalizerInfoSSE2() {
192 if (!Subtarget.hasSSE2())
193 return;
194
195 const LLT s64 = LLT::scalar(64);
Igor Breger842b5b32017-05-18 11:10:56 +0000196 const LLT v16s8 = LLT::vector(16, 8);
Igor Breger605b9652017-05-08 09:03:37 +0000197 const LLT v8s16 = LLT::vector(8, 16);
Igor Breger321cf3c2017-03-03 08:06:46 +0000198 const LLT v4s32 = LLT::vector(4, 32);
199 const LLT v2s64 = LLT::vector(2, 64);
200
201 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMUL, G_FDIV})
202 for (auto Ty : {s64, v2s64})
203 setAction({BinOp, Ty}, Legal);
204
205 for (unsigned BinOp : {G_ADD, G_SUB})
Igor Breger842b5b32017-05-18 11:10:56 +0000206 for (auto Ty : {v16s8, v8s16, v4s32, v2s64})
Igor Breger321cf3c2017-03-03 08:06:46 +0000207 setAction({BinOp, Ty}, Legal);
Igor Breger605b9652017-05-08 09:03:37 +0000208
209 setAction({G_MUL, v8s16}, Legal);
210}
211
212void X86LegalizerInfo::setLegalizerInfoSSE41() {
213 if (!Subtarget.hasSSE41())
214 return;
215
216 const LLT v4s32 = LLT::vector(4, 32);
217
218 setAction({G_MUL, v4s32}, Legal);
219}
220
Igor Breger617be6e2017-05-23 08:23:51 +0000221void X86LegalizerInfo::setLegalizerInfoAVX() {
222 if (!Subtarget.hasAVX())
223 return;
224
Igor Breger1c29be72017-06-22 09:43:35 +0000225 const LLT v16s8 = LLT::vector(16, 8);
226 const LLT v8s16 = LLT::vector(8, 16);
227 const LLT v4s32 = LLT::vector(4, 32);
228 const LLT v2s64 = LLT::vector(2, 64);
229
230 const LLT v32s8 = LLT::vector(32, 8);
231 const LLT v16s16 = LLT::vector(16, 16);
Igor Breger617be6e2017-05-23 08:23:51 +0000232 const LLT v8s32 = LLT::vector(8, 32);
233 const LLT v4s64 = LLT::vector(4, 64);
234
235 for (unsigned MemOp : {G_LOAD, G_STORE})
236 for (auto Ty : {v8s32, v4s64})
237 setAction({MemOp, Ty}, Legal);
Igor Breger1c29be72017-06-22 09:43:35 +0000238
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000239 for (auto Ty : {v32s8, v16s16, v8s32, v4s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000240 setAction({G_INSERT, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000241 setAction({G_EXTRACT, 1, Ty}, Legal);
242 }
243 for (auto Ty : {v16s8, v8s16, v4s32, v2s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000244 setAction({G_INSERT, 1, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000245 setAction({G_EXTRACT, Ty}, Legal);
246 }
Igor Breger617be6e2017-05-23 08:23:51 +0000247}
248
Igor Breger605b9652017-05-08 09:03:37 +0000249void X86LegalizerInfo::setLegalizerInfoAVX2() {
250 if (!Subtarget.hasAVX2())
251 return;
252
Igor Breger842b5b32017-05-18 11:10:56 +0000253 const LLT v32s8 = LLT::vector(32, 8);
Igor Breger605b9652017-05-08 09:03:37 +0000254 const LLT v16s16 = LLT::vector(16, 16);
255 const LLT v8s32 = LLT::vector(8, 32);
Igor Breger842b5b32017-05-18 11:10:56 +0000256 const LLT v4s64 = LLT::vector(4, 64);
257
258 for (unsigned BinOp : {G_ADD, G_SUB})
259 for (auto Ty : {v32s8, v16s16, v8s32, v4s64})
260 setAction({BinOp, Ty}, Legal);
Igor Breger605b9652017-05-08 09:03:37 +0000261
262 for (auto Ty : {v16s16, v8s32})
263 setAction({G_MUL, Ty}, Legal);
264}
265
266void X86LegalizerInfo::setLegalizerInfoAVX512() {
267 if (!Subtarget.hasAVX512())
268 return;
269
Igor Breger1c29be72017-06-22 09:43:35 +0000270 const LLT v16s8 = LLT::vector(16, 8);
271 const LLT v8s16 = LLT::vector(8, 16);
272 const LLT v4s32 = LLT::vector(4, 32);
273 const LLT v2s64 = LLT::vector(2, 64);
274
275 const LLT v32s8 = LLT::vector(32, 8);
276 const LLT v16s16 = LLT::vector(16, 16);
277 const LLT v8s32 = LLT::vector(8, 32);
278 const LLT v4s64 = LLT::vector(4, 64);
279
280 const LLT v64s8 = LLT::vector(64, 8);
281 const LLT v32s16 = LLT::vector(32, 16);
Igor Breger605b9652017-05-08 09:03:37 +0000282 const LLT v16s32 = LLT::vector(16, 32);
Igor Breger842b5b32017-05-18 11:10:56 +0000283 const LLT v8s64 = LLT::vector(8, 64);
284
285 for (unsigned BinOp : {G_ADD, G_SUB})
286 for (auto Ty : {v16s32, v8s64})
287 setAction({BinOp, Ty}, Legal);
Igor Breger605b9652017-05-08 09:03:37 +0000288
289 setAction({G_MUL, v16s32}, Legal);
290
Igor Breger617be6e2017-05-23 08:23:51 +0000291 for (unsigned MemOp : {G_LOAD, G_STORE})
292 for (auto Ty : {v16s32, v8s64})
293 setAction({MemOp, Ty}, Legal);
294
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000295 for (auto Ty : {v64s8, v32s16, v16s32, v8s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000296 setAction({G_INSERT, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000297 setAction({G_EXTRACT, 1, Ty}, Legal);
298 }
299 for (auto Ty : {v32s8, v16s16, v8s32, v4s64, v16s8, v8s16, v4s32, v2s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000300 setAction({G_INSERT, 1, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000301 setAction({G_EXTRACT, Ty}, Legal);
302 }
Igor Breger1c29be72017-06-22 09:43:35 +0000303
Igor Breger605b9652017-05-08 09:03:37 +0000304 /************ VLX *******************/
305 if (!Subtarget.hasVLX())
306 return;
307
Igor Breger605b9652017-05-08 09:03:37 +0000308 for (auto Ty : {v4s32, v8s32})
309 setAction({G_MUL, Ty}, Legal);
310}
311
312void X86LegalizerInfo::setLegalizerInfoAVX512DQ() {
313 if (!(Subtarget.hasAVX512() && Subtarget.hasDQI()))
314 return;
315
316 const LLT v8s64 = LLT::vector(8, 64);
317
318 setAction({G_MUL, v8s64}, Legal);
319
320 /************ VLX *******************/
321 if (!Subtarget.hasVLX())
322 return;
323
324 const LLT v2s64 = LLT::vector(2, 64);
325 const LLT v4s64 = LLT::vector(4, 64);
326
327 for (auto Ty : {v2s64, v4s64})
328 setAction({G_MUL, Ty}, Legal);
329}
330
331void X86LegalizerInfo::setLegalizerInfoAVX512BW() {
332 if (!(Subtarget.hasAVX512() && Subtarget.hasBWI()))
333 return;
334
Igor Breger842b5b32017-05-18 11:10:56 +0000335 const LLT v64s8 = LLT::vector(64, 8);
Igor Breger605b9652017-05-08 09:03:37 +0000336 const LLT v32s16 = LLT::vector(32, 16);
337
Igor Breger842b5b32017-05-18 11:10:56 +0000338 for (unsigned BinOp : {G_ADD, G_SUB})
339 for (auto Ty : {v64s8, v32s16})
340 setAction({BinOp, Ty}, Legal);
341
Igor Breger605b9652017-05-08 09:03:37 +0000342 setAction({G_MUL, v32s16}, Legal);
343
344 /************ VLX *******************/
345 if (!Subtarget.hasVLX())
346 return;
347
348 const LLT v8s16 = LLT::vector(8, 16);
349 const LLT v16s16 = LLT::vector(16, 16);
350
351 for (auto Ty : {v8s16, v16s16})
352 setAction({G_MUL, Ty}, Legal);
Igor Bregerb4442f32017-02-10 07:05:56 +0000353}