blob: b1075995be2954c71010ef45c1879bb75bc2c96e [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 Breger29537882017-04-07 14:41:59 +000083 // Constants
84 for (auto Ty : {s8, s16, s32, p0})
85 setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
86
87 setAction({TargetOpcode::G_CONSTANT, s1}, WidenScalar);
88 setAction({TargetOpcode::G_CONSTANT, s64}, NarrowScalar);
Igor Bregerc08a7832017-05-01 06:30:16 +000089
90 // Extensions
Igor Bregerd48c5e42017-07-10 09:07:34 +000091 for (auto Ty : {s8, s16, s32}) {
92 setAction({G_ZEXT, Ty}, Legal);
93 setAction({G_SEXT, Ty}, Legal);
94 }
Igor Bregerc08a7832017-05-01 06:30:16 +000095
Igor Bregerfda31e62017-05-10 06:52:58 +000096 for (auto Ty : {s1, s8, s16}) {
Igor Bregerc08a7832017-05-01 06:30:16 +000097 setAction({G_ZEXT, 1, Ty}, Legal);
98 setAction({G_SEXT, 1, Ty}, Legal);
99 }
Igor Bregerc7b59772017-05-11 07:17:40 +0000100
101 // Comparison
102 setAction({G_ICMP, s1}, Legal);
103
104 for (auto Ty : {s8, s16, s32, p0})
105 setAction({G_ICMP, 1, Ty}, Legal);
Igor Bregerb4442f32017-02-10 07:05:56 +0000106}
Igor Bregerb4442f32017-02-10 07:05:56 +0000107
Igor Bregerf7359d82017-02-22 12:25:09 +0000108void X86LegalizerInfo::setLegalizerInfo64bit() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000109
110 if (!Subtarget.is64Bit())
111 return;
112
Igor Breger531a2032017-03-26 08:11:12 +0000113 const LLT p0 = LLT::pointer(0, TM.getPointerSize() * 8);
Igor Breger29537882017-04-07 14:41:59 +0000114 const LLT s1 = LLT::scalar(1);
Igor Bregera8ba5722017-03-23 15:25:57 +0000115 const LLT s8 = LLT::scalar(8);
116 const LLT s16 = LLT::scalar(16);
117 const LLT s32 = LLT::scalar(32);
Igor Bregerb4442f32017-02-10 07:05:56 +0000118 const LLT s64 = LLT::scalar(64);
119
Igor Bregerd5b59cf2017-06-28 11:39:04 +0000120 for (unsigned BinOp : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR})
Igor Bregera8ba5722017-03-23 15:25:57 +0000121 for (auto Ty : {s8, s16, s32, s64})
122 setAction({BinOp, Ty}, Legal);
123
124 for (unsigned MemOp : {G_LOAD, G_STORE}) {
125 for (auto Ty : {s8, s16, s32, s64, p0})
126 setAction({MemOp, Ty}, Legal);
127
Igor Bregerd8b51e12017-07-10 09:26:09 +0000128 setAction({MemOp, s1}, WidenScalar);
Igor Bregera8ba5722017-03-23 15:25:57 +0000129 // And everything's fine in addrspace 0.
130 setAction({MemOp, 1, p0}, Legal);
131 }
Igor Breger531a2032017-03-26 08:11:12 +0000132
133 // Pointer-handling
134 setAction({G_FRAME_INDEX, p0}, Legal);
Igor Breger717bd362017-07-02 08:58:29 +0000135 setAction({G_GLOBAL_VALUE, p0}, Legal);
Igor Breger29537882017-04-07 14:41:59 +0000136
Igor Breger810c6252017-05-08 09:40:43 +0000137 setAction({G_GEP, p0}, Legal);
138 setAction({G_GEP, 1, s32}, Legal);
139 setAction({G_GEP, 1, s64}, Legal);
140
141 for (auto Ty : {s1, s8, s16})
142 setAction({G_GEP, 1, Ty}, WidenScalar);
143
Igor Breger29537882017-04-07 14:41:59 +0000144 // Constants
145 for (auto Ty : {s8, s16, s32, s64, p0})
146 setAction({TargetOpcode::G_CONSTANT, Ty}, Legal);
147
148 setAction({TargetOpcode::G_CONSTANT, s1}, WidenScalar);
Igor Bregerc08a7832017-05-01 06:30:16 +0000149
150 // Extensions
Igor Bregerd48c5e42017-07-10 09:07:34 +0000151 for (auto Ty : {s8, s16, s32, s64}) {
Igor Bregerc08a7832017-05-01 06:30:16 +0000152 setAction({G_ZEXT, Ty}, Legal);
153 setAction({G_SEXT, Ty}, Legal);
154 }
155
Igor Bregerfda31e62017-05-10 06:52:58 +0000156 for (auto Ty : {s1, s8, s16, s32}) {
Igor Bregerc08a7832017-05-01 06:30:16 +0000157 setAction({G_ZEXT, 1, Ty}, Legal);
158 setAction({G_SEXT, 1, Ty}, Legal);
159 }
Igor Bregerc7b59772017-05-11 07:17:40 +0000160
161 // Comparison
162 setAction({G_ICMP, s1}, Legal);
163
164 for (auto Ty : {s8, s16, s32, s64, p0})
165 setAction({G_ICMP, 1, Ty}, Legal);
Igor Breger321cf3c2017-03-03 08:06:46 +0000166}
167
168void X86LegalizerInfo::setLegalizerInfoSSE1() {
169 if (!Subtarget.hasSSE1())
170 return;
171
172 const LLT s32 = LLT::scalar(32);
173 const LLT v4s32 = LLT::vector(4, 32);
Igor Bregera8ba5722017-03-23 15:25:57 +0000174 const LLT v2s64 = LLT::vector(2, 64);
Igor Breger321cf3c2017-03-03 08:06:46 +0000175
176 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMUL, G_FDIV})
177 for (auto Ty : {s32, v4s32})
178 setAction({BinOp, Ty}, Legal);
Igor Bregera8ba5722017-03-23 15:25:57 +0000179
180 for (unsigned MemOp : {G_LOAD, G_STORE})
181 for (auto Ty : {v4s32, v2s64})
182 setAction({MemOp, Ty}, Legal);
Igor Breger321cf3c2017-03-03 08:06:46 +0000183}
184
185void X86LegalizerInfo::setLegalizerInfoSSE2() {
186 if (!Subtarget.hasSSE2())
187 return;
188
189 const LLT s64 = LLT::scalar(64);
Igor Breger842b5b32017-05-18 11:10:56 +0000190 const LLT v16s8 = LLT::vector(16, 8);
Igor Breger605b9652017-05-08 09:03:37 +0000191 const LLT v8s16 = LLT::vector(8, 16);
Igor Breger321cf3c2017-03-03 08:06:46 +0000192 const LLT v4s32 = LLT::vector(4, 32);
193 const LLT v2s64 = LLT::vector(2, 64);
194
195 for (unsigned BinOp : {G_FADD, G_FSUB, G_FMUL, G_FDIV})
196 for (auto Ty : {s64, v2s64})
197 setAction({BinOp, Ty}, Legal);
198
199 for (unsigned BinOp : {G_ADD, G_SUB})
Igor Breger842b5b32017-05-18 11:10:56 +0000200 for (auto Ty : {v16s8, v8s16, v4s32, v2s64})
Igor Breger321cf3c2017-03-03 08:06:46 +0000201 setAction({BinOp, Ty}, Legal);
Igor Breger605b9652017-05-08 09:03:37 +0000202
203 setAction({G_MUL, v8s16}, Legal);
204}
205
206void X86LegalizerInfo::setLegalizerInfoSSE41() {
207 if (!Subtarget.hasSSE41())
208 return;
209
210 const LLT v4s32 = LLT::vector(4, 32);
211
212 setAction({G_MUL, v4s32}, Legal);
213}
214
Igor Breger617be6e2017-05-23 08:23:51 +0000215void X86LegalizerInfo::setLegalizerInfoAVX() {
216 if (!Subtarget.hasAVX())
217 return;
218
Igor Breger1c29be72017-06-22 09:43:35 +0000219 const LLT v16s8 = LLT::vector(16, 8);
220 const LLT v8s16 = LLT::vector(8, 16);
221 const LLT v4s32 = LLT::vector(4, 32);
222 const LLT v2s64 = LLT::vector(2, 64);
223
224 const LLT v32s8 = LLT::vector(32, 8);
225 const LLT v16s16 = LLT::vector(16, 16);
Igor Breger617be6e2017-05-23 08:23:51 +0000226 const LLT v8s32 = LLT::vector(8, 32);
227 const LLT v4s64 = LLT::vector(4, 64);
228
229 for (unsigned MemOp : {G_LOAD, G_STORE})
230 for (auto Ty : {v8s32, v4s64})
231 setAction({MemOp, Ty}, Legal);
Igor Breger1c29be72017-06-22 09:43:35 +0000232
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000233 for (auto Ty : {v32s8, v16s16, v8s32, v4s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000234 setAction({G_INSERT, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000235 setAction({G_EXTRACT, 1, Ty}, Legal);
236 }
237 for (auto Ty : {v16s8, v8s16, v4s32, v2s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000238 setAction({G_INSERT, 1, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000239 setAction({G_EXTRACT, Ty}, Legal);
240 }
Igor Breger617be6e2017-05-23 08:23:51 +0000241}
242
Igor Breger605b9652017-05-08 09:03:37 +0000243void X86LegalizerInfo::setLegalizerInfoAVX2() {
244 if (!Subtarget.hasAVX2())
245 return;
246
Igor Breger842b5b32017-05-18 11:10:56 +0000247 const LLT v32s8 = LLT::vector(32, 8);
Igor Breger605b9652017-05-08 09:03:37 +0000248 const LLT v16s16 = LLT::vector(16, 16);
249 const LLT v8s32 = LLT::vector(8, 32);
Igor Breger842b5b32017-05-18 11:10:56 +0000250 const LLT v4s64 = LLT::vector(4, 64);
251
252 for (unsigned BinOp : {G_ADD, G_SUB})
253 for (auto Ty : {v32s8, v16s16, v8s32, v4s64})
254 setAction({BinOp, Ty}, Legal);
Igor Breger605b9652017-05-08 09:03:37 +0000255
256 for (auto Ty : {v16s16, v8s32})
257 setAction({G_MUL, Ty}, Legal);
258}
259
260void X86LegalizerInfo::setLegalizerInfoAVX512() {
261 if (!Subtarget.hasAVX512())
262 return;
263
Igor Breger1c29be72017-06-22 09:43:35 +0000264 const LLT v16s8 = LLT::vector(16, 8);
265 const LLT v8s16 = LLT::vector(8, 16);
266 const LLT v4s32 = LLT::vector(4, 32);
267 const LLT v2s64 = LLT::vector(2, 64);
268
269 const LLT v32s8 = LLT::vector(32, 8);
270 const LLT v16s16 = LLT::vector(16, 16);
271 const LLT v8s32 = LLT::vector(8, 32);
272 const LLT v4s64 = LLT::vector(4, 64);
273
274 const LLT v64s8 = LLT::vector(64, 8);
275 const LLT v32s16 = LLT::vector(32, 16);
Igor Breger605b9652017-05-08 09:03:37 +0000276 const LLT v16s32 = LLT::vector(16, 32);
Igor Breger842b5b32017-05-18 11:10:56 +0000277 const LLT v8s64 = LLT::vector(8, 64);
278
279 for (unsigned BinOp : {G_ADD, G_SUB})
280 for (auto Ty : {v16s32, v8s64})
281 setAction({BinOp, Ty}, Legal);
Igor Breger605b9652017-05-08 09:03:37 +0000282
283 setAction({G_MUL, v16s32}, Legal);
284
Igor Breger617be6e2017-05-23 08:23:51 +0000285 for (unsigned MemOp : {G_LOAD, G_STORE})
286 for (auto Ty : {v16s32, v8s64})
287 setAction({MemOp, Ty}, Legal);
288
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000289 for (auto Ty : {v64s8, v32s16, v16s32, v8s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000290 setAction({G_INSERT, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000291 setAction({G_EXTRACT, 1, Ty}, Legal);
292 }
293 for (auto Ty : {v32s8, v16s16, v8s32, v4s64, v16s8, v8s16, v4s32, v2s64}) {
Igor Breger1c29be72017-06-22 09:43:35 +0000294 setAction({G_INSERT, 1, Ty}, Legal);
Tim Northoverc2d5e6d2017-06-26 20:34:13 +0000295 setAction({G_EXTRACT, Ty}, Legal);
296 }
Igor Breger1c29be72017-06-22 09:43:35 +0000297
Igor Breger605b9652017-05-08 09:03:37 +0000298 /************ VLX *******************/
299 if (!Subtarget.hasVLX())
300 return;
301
Igor Breger605b9652017-05-08 09:03:37 +0000302 for (auto Ty : {v4s32, v8s32})
303 setAction({G_MUL, Ty}, Legal);
304}
305
306void X86LegalizerInfo::setLegalizerInfoAVX512DQ() {
307 if (!(Subtarget.hasAVX512() && Subtarget.hasDQI()))
308 return;
309
310 const LLT v8s64 = LLT::vector(8, 64);
311
312 setAction({G_MUL, v8s64}, Legal);
313
314 /************ VLX *******************/
315 if (!Subtarget.hasVLX())
316 return;
317
318 const LLT v2s64 = LLT::vector(2, 64);
319 const LLT v4s64 = LLT::vector(4, 64);
320
321 for (auto Ty : {v2s64, v4s64})
322 setAction({G_MUL, Ty}, Legal);
323}
324
325void X86LegalizerInfo::setLegalizerInfoAVX512BW() {
326 if (!(Subtarget.hasAVX512() && Subtarget.hasBWI()))
327 return;
328
Igor Breger842b5b32017-05-18 11:10:56 +0000329 const LLT v64s8 = LLT::vector(64, 8);
Igor Breger605b9652017-05-08 09:03:37 +0000330 const LLT v32s16 = LLT::vector(32, 16);
331
Igor Breger842b5b32017-05-18 11:10:56 +0000332 for (unsigned BinOp : {G_ADD, G_SUB})
333 for (auto Ty : {v64s8, v32s16})
334 setAction({BinOp, Ty}, Legal);
335
Igor Breger605b9652017-05-08 09:03:37 +0000336 setAction({G_MUL, v32s16}, Legal);
337
338 /************ VLX *******************/
339 if (!Subtarget.hasVLX())
340 return;
341
342 const LLT v8s16 = LLT::vector(8, 16);
343 const LLT v16s16 = LLT::vector(16, 16);
344
345 for (auto Ty : {v8s16, v16s16})
346 setAction({G_MUL, Ty}, Legal);
Igor Bregerb4442f32017-02-10 07:05:56 +0000347}