LLVM 24.0.0git
X86LegalizerInfo.cpp
Go to the documentation of this file.
1//===- X86LegalizerInfo.cpp --------------------------------------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file implements the targeting of the Machinelegalizer class for X86.
10/// \todo This should be generated by TableGen.
11//===----------------------------------------------------------------------===//
12
13#include "X86LegalizerInfo.h"
14#include "X86Subtarget.h"
15#include "X86TargetMachine.h"
25#include "llvm/IR/IntrinsicsX86.h"
26#include "llvm/IR/Type.h"
27
28using namespace llvm;
29using namespace MIPatternMatch;
30using namespace TargetOpcode;
31using namespace LegalizeActions;
32using namespace LegalityPredicates;
33
35 const X86TargetMachine &TM)
36 : Subtarget(STI) {
37
38 bool Is64Bit = Subtarget.is64Bit();
39 bool HasCMOV = Subtarget.canUseCMOV();
40 bool HasSSE1 = Subtarget.hasSSE1();
41 bool HasSSE2 = Subtarget.hasSSE2();
42 bool HasSSE41 = Subtarget.hasSSE41();
43 bool HasAVX = Subtarget.hasAVX();
44 bool HasAVX2 = Subtarget.hasAVX2();
45 bool HasAVX512 = Subtarget.hasAVX512();
46 bool HasVLX = Subtarget.hasVLX();
47 bool HasDQI = Subtarget.hasAVX512() && Subtarget.hasDQI();
48 bool HasBWI = Subtarget.hasAVX512() && Subtarget.hasBWI();
49 bool UseX87 = !Subtarget.useSoftFloat() && Subtarget.hasX87();
50 bool HasPOPCNT = Subtarget.hasPOPCNT();
51 bool HasLZCNT = Subtarget.hasLZCNT();
52 bool HasBMI = Subtarget.hasBMI();
53 bool HasVPOPCNTDQ = Subtarget.hasVPOPCNTDQ();
54 bool HasBITALG = Subtarget.hasBITALG();
55
56 const LLT p0 = LLT::pointer(0, Subtarget.isTarget64BitLP64() ? 64 : 32);
57 const LLT s1 = LLT::scalar(1);
58 const LLT s8 = LLT::scalar(8);
59 const LLT s16 = LLT::scalar(16);
60 const LLT s32 = LLT::scalar(32);
61 const LLT s64 = LLT::scalar(64);
62 const LLT s80 = LLT::scalar(80);
63 const LLT s128 = LLT::scalar(128);
64 const LLT sMaxScalar = Subtarget.is64Bit() ? s64 : s32;
65 const LLT v2s32 = LLT::fixed_vector(2, 32);
66 const LLT v4s8 = LLT::fixed_vector(4, 8);
67
68 const LLT v16s8 = LLT::fixed_vector(16, 8);
69 const LLT v8s16 = LLT::fixed_vector(8, 16);
70 const LLT v4s32 = LLT::fixed_vector(4, 32);
71 const LLT v2s64 = LLT::fixed_vector(2, 64);
72 const LLT v2p0 = LLT::fixed_vector(2, p0);
73
74 const LLT v32s8 = LLT::fixed_vector(32, 8);
75 const LLT v16s16 = LLT::fixed_vector(16, 16);
76 const LLT v8s32 = LLT::fixed_vector(8, 32);
77 const LLT v4s64 = LLT::fixed_vector(4, 64);
78 const LLT v4p0 = LLT::fixed_vector(4, p0);
79
80 const LLT v64s8 = LLT::fixed_vector(64, 8);
81 const LLT v32s16 = LLT::fixed_vector(32, 16);
82 const LLT v16s32 = LLT::fixed_vector(16, 32);
83 const LLT v8s64 = LLT::fixed_vector(8, 64);
84
85 const LLT s8MaxVector = HasAVX512 ? v64s8 : HasAVX ? v32s8 : v16s8;
86 const LLT s16MaxVector = HasAVX512 ? v32s16 : HasAVX ? v16s16 : v8s16;
87 const LLT s32MaxVector = HasAVX512 ? v16s32 : HasAVX ? v8s32 : v4s32;
88 const LLT s64MaxVector = HasAVX512 ? v8s64 : HasAVX ? v4s64 : v2s64;
89
90 // todo: AVX512 bool vector predicate types
91
92 // implicit/constants
93 // 32/64-bits needs support for s64/s128 to handle cases:
94 // s64 = EXTEND (G_IMPLICIT_DEF s32) -> s64 = G_IMPLICIT_DEF
95 // s128 = EXTEND (G_IMPLICIT_DEF s32/s64) -> s128 = G_IMPLICIT_DEF
97 {G_IMPLICIT_DEF, G_PHI, G_FREEZE, G_CONSTANT_FOLD_BARRIER})
98 .legalFor({p0, s1, s8, s16, s32, s64})
99 .legalFor(UseX87, {s80})
100 .legalFor(Is64Bit, {s128})
101 .legalFor(HasSSE2, {v16s8, v8s16, v4s32, v2s64})
102 .legalFor(HasAVX, {v32s8, v16s16, v8s32, v4s64})
103 .legalFor(HasAVX512, {v64s8, v32s16, v16s32, v8s64})
104 .widenScalarOrEltToNextPow2(0, /*Min=*/8)
105 .clampScalarOrElt(0, s8, sMaxScalar)
106 .moreElementsToNextPow2(0)
107 .clampNumElements(0, v16s8, s8MaxVector)
108 .clampNumElements(0, v8s16, s16MaxVector)
109 .clampNumElements(0, v4s32, s32MaxVector)
110 .clampNumElements(0, v2s64, s64MaxVector)
111 .clampMaxNumElements(0, p0,
112 Is64Bit ? s64MaxVector.getNumElements()
113 : s32MaxVector.getNumElements())
114 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0);
115
117 .legalFor({p0, s8, s16, s32})
118 .legalFor(Is64Bit, {s64})
119 .widenScalarToNextPow2(0, /*Min=*/8)
120 .clampScalar(0, s8, sMaxScalar);
121
122 getActionDefinitionsBuilder({G_LROUND, G_LLROUND})
123 .widenScalarIf(typeIs(1, s16),
124 [=](const LegalityQuery &) {
125 return std::pair<unsigned, LLT>(1, s32);
126 })
127 .libcall();
128
130 {G_FCOS, G_FCOSH, G_FACOS, G_FSIN, G_FSINH, G_FASIN, G_FTAN,
131 G_FTANH, G_FATAN, G_FATAN2, G_FPOW, G_FEXP, G_FEXP2, G_FEXP10,
132 G_FLOG, G_FLOG2, G_FLOG10, G_FPOWI, G_FSINCOS, G_FCEIL, G_FFLOOR})
133 .libcall();
134
136 .legalFor(UseX87 && !HasSSE1, {s32})
137 .legalFor(UseX87 && !HasSSE2, {s64})
138 .legalFor(UseX87, {s80})
139 .lower();
140
142 .legalFor(HasSSE1 || UseX87, {s32})
143 .legalFor(HasSSE2 || UseX87, {s64})
144 .legalFor(UseX87, {s80});
145
146 getActionDefinitionsBuilder({G_GET_ROUNDING, G_SET_ROUNDING})
147 .customFor({s32});
148
149 // merge/unmerge
150 for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) {
151 unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1;
152 unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0;
154 .widenScalarToNextPow2(LitTyIdx, /*Min=*/8)
155 .widenScalarToNextPow2(BigTyIdx, /*Min=*/16)
156 .minScalar(LitTyIdx, s8)
157 .minScalar(BigTyIdx, s32)
158 .legalIf([=](const LegalityQuery &Q) {
159 switch (Q.Types[BigTyIdx].getSizeInBits()) {
160 case 16:
161 case 32:
162 case 64:
163 case 128:
164 case 256:
165 case 512:
166 break;
167 default:
168 return false;
169 }
170 switch (Q.Types[LitTyIdx].getSizeInBits()) {
171 case 8:
172 case 16:
173 case 32:
174 case 64:
175 case 128:
176 case 256:
177 return true;
178 default:
179 return false;
180 }
181 });
182 }
183
184 getActionDefinitionsBuilder({G_UMIN, G_UMAX, G_SMIN, G_SMAX})
185 .widenScalarToNextPow2(0, /*Min=*/32)
186 .lower();
187
188 // integer addition/subtraction
189 getActionDefinitionsBuilder({G_ADD, G_SUB})
190 .legalFor({s8, s16, s32})
191 .legalFor(Is64Bit, {s64})
192 .legalFor(HasSSE2, {v16s8, v8s16, v4s32, v2s64})
193 .legalFor(HasAVX2, {v32s8, v16s16, v8s32, v4s64})
194 .legalFor(HasAVX512, {v16s32, v8s64})
195 .legalFor(HasBWI, {v64s8, v32s16})
196 .clampMinNumElements(0, s8, 16)
197 .clampMinNumElements(0, s16, 8)
198 .clampMinNumElements(0, s32, 4)
199 .clampMinNumElements(0, s64, 2)
200 .clampMaxNumElements(0, s8, HasBWI ? 64 : (HasAVX2 ? 32 : 16))
201 .clampMaxNumElements(0, s16, HasBWI ? 32 : (HasAVX2 ? 16 : 8))
202 .clampMaxNumElements(0, s32, HasAVX512 ? 16 : (HasAVX2 ? 8 : 4))
203 .clampMaxNumElements(0, s64, HasAVX512 ? 8 : (HasAVX2 ? 4 : 2))
204 .widenScalarToNextPow2(0, /*Min=*/32)
205 .clampScalar(0, s8, sMaxScalar)
206 .scalarize(0);
207
208 getActionDefinitionsBuilder({G_UADDE, G_UADDO, G_USUBE, G_USUBO})
209 .legalFor({{s8, s8}, {s16, s8}, {s32, s8}})
210 .legalFor(Is64Bit, {{s64, s8}})
211 .widenScalarToNextPow2(0, /*Min=*/32)
212 .clampScalar(0, s8, sMaxScalar)
213 .clampScalar(1, s8, s8)
214 .scalarize(0);
215
216 // integer multiply
218 .legalFor({s8, s16, s32})
219 .legalFor(Is64Bit, {s64})
220 .legalFor(HasSSE2, {v8s16})
221 .legalFor(HasSSE41, {v4s32})
222 .legalFor(HasAVX2, {v16s16, v8s32})
223 .legalFor(HasAVX512, {v16s32})
224 .legalFor(HasDQI, {v8s64})
225 .legalFor(HasDQI && HasVLX, {v2s64, v4s64})
226 .legalFor(HasBWI, {v32s16})
227 .clampMinNumElements(0, s16, 8)
228 .clampMinNumElements(0, s32, 4)
229 .clampMinNumElements(0, s64, HasVLX ? 2 : 8)
230 .clampMaxNumElements(0, s16, HasBWI ? 32 : (HasAVX2 ? 16 : 8))
231 .clampMaxNumElements(0, s32, HasAVX512 ? 16 : (HasAVX2 ? 8 : 4))
232 .clampMaxNumElements(0, s64, 8)
233 .widenScalarToNextPow2(0, /*Min=*/32)
234 .clampScalar(0, s8, sMaxScalar)
235 .scalarize(0);
236
237 getActionDefinitionsBuilder({G_SMULH, G_UMULH})
238 .legalFor({s8, s16, s32})
239 .legalFor(Is64Bit, {s64})
240 .widenScalarToNextPow2(0, /*Min=*/32)
241 .clampScalar(0, s8, sMaxScalar)
242 .scalarize(0);
243
244 // integer divisions
245 getActionDefinitionsBuilder({G_SDIV, G_SREM, G_UDIV, G_UREM})
246 .legalFor({s8, s16, s32})
247 .legalFor(Is64Bit, {s64})
248 .libcallFor({s64})
249 .clampScalar(0, s8, sMaxScalar);
250
251 // integer shifts
252 getActionDefinitionsBuilder({G_SHL, G_LSHR, G_ASHR})
253 .legalFor({{s8, s8}, {s16, s8}, {s32, s8}})
254 .legalFor(Is64Bit, {{s64, s8}})
255 .clampScalar(0, s8, sMaxScalar)
256 .clampScalar(1, s8, s8);
257
258 // integer logic
259 getActionDefinitionsBuilder({G_AND, G_OR, G_XOR})
260 .legalFor({s8, s16, s32})
261 .legalFor(Is64Bit, {s64})
262 .legalFor(HasSSE2, {v16s8, v8s16, v4s32, v2s64})
263 .legalFor(HasAVX, {v32s8, v16s16, v8s32, v4s64})
264 .legalFor(HasAVX512, {v64s8, v32s16, v16s32, v8s64})
265 .clampNumElements(0, v16s8, s8MaxVector)
266 .clampNumElements(0, v8s16, s16MaxVector)
267 .clampNumElements(0, v4s32, s32MaxVector)
268 .clampNumElements(0, v2s64, s64MaxVector)
269 .widenScalarToNextPow2(0, /*Min=*/32)
270 .clampScalar(0, s8, sMaxScalar)
271 .scalarize(0);
272
273 // integer comparison
274 const std::initializer_list<LLT> IntTypes32 = {s8, s16, s32, p0};
275 const std::initializer_list<LLT> IntTypes64 = {s8, s16, s32, s64, p0};
276
278 .legalForCartesianProduct({s8}, Is64Bit ? IntTypes64 : IntTypes32)
279 .clampScalar(0, s8, s8)
280 .widenScalarToNextPow2(1, /*Min=*/8)
281 .clampScalar(1, s8, sMaxScalar);
282
283 // bswap
285 .legalFor({s32})
286 .legalFor(Is64Bit, {s64})
287 .widenScalarToNextPow2(0, /*Min=*/32)
288 .clampScalar(0, s32, sMaxScalar);
289
290 // popcount
291 // Vector forms are only legal when a VPOPCNT instruction is available.
292 // 128-bit and 256-bit forms only need the instruction feature itself: for
293 // subtargets without VLX, the avx512_unary_lowering patterns in
294 // X86InstrAVX512.td widen them to the 512-bit instruction. Other vector
295 // queries intentionally match no rule here and are reported as unsupported
296 // rather than falling through to the scalar-only rules below.
298 .legalFor(HasPOPCNT, {{s16, s16}, {s32, s32}})
299 .legalFor(HasPOPCNT && Is64Bit, {{s64, s64}})
300 .legalFor(HasVPOPCNTDQ, {{v4s32, v4s32},
301 {v8s32, v8s32},
302 {v16s32, v16s32},
303 {v2s64, v2s64},
304 {v4s64, v4s64},
305 {v8s64, v8s64}})
306 .legalFor(HasBITALG, {{v16s8, v16s8},
307 {v32s8, v32s8},
308 {v64s8, v64s8},
309 {v8s16, v8s16},
310 {v16s16, v16s16},
311 {v32s16, v32s16}})
312 .widenScalarToNextPow2(1, /*Min=*/16)
313 .clampScalar(1, s16, sMaxScalar)
314 .scalarSameSizeAs(0, 1);
315
316 // count leading zeros (LZCNT)
318 .legalFor(HasLZCNT, {{s16, s16}, {s32, s32}})
319 .legalFor(HasLZCNT && Is64Bit, {{s64, s64}})
320 .widenScalarToNextPow2(1, /*Min=*/16)
321 .clampScalar(1, s16, sMaxScalar)
322 .scalarSameSizeAs(0, 1);
323
324 // count trailing zeros
325 getActionDefinitionsBuilder(G_CTTZ_ZERO_POISON)
326 .legalFor({{s16, s16}, {s32, s32}})
327 .legalFor(Is64Bit, {{s64, s64}})
328 .widenScalarToNextPow2(1, /*Min=*/16)
329 .clampScalar(1, s16, sMaxScalar)
330 .scalarSameSizeAs(0, 1);
331
333 .legalFor(HasBMI, {{s16, s16}, {s32, s32}})
334 .legalFor(HasBMI && Is64Bit, {{s64, s64}})
335 .widenScalarToNextPow2(1, /*Min=*/16)
336 .clampScalar(1, s16, sMaxScalar)
337 .scalarSameSizeAs(0, 1);
338
341
342 // pointer handling
343 const std::initializer_list<LLT> PtrTypes32 = {s1, s8, s16, s32};
344 const std::initializer_list<LLT> PtrTypes64 = {s1, s8, s16, s32, s64};
345
347 .legalForCartesianProduct(Is64Bit ? PtrTypes64 : PtrTypes32, {p0})
348 .maxScalar(0, sMaxScalar)
349 .widenScalarToNextPow2(0, /*Min*/ 8);
350
351 getActionDefinitionsBuilder(G_INTTOPTR).legalFor({{p0, sMaxScalar}});
352
353 getActionDefinitionsBuilder(G_CONSTANT_POOL).legalFor({p0});
354
356 .legalFor({{p0, s32}})
357 .legalFor(Is64Bit, {{p0, s64}})
358 .widenScalarToNextPow2(1, /*Min*/ 32)
359 .clampScalar(1, s32, sMaxScalar);
360
361 getActionDefinitionsBuilder(G_FRAME_INDEX).legalFor({p0});
362
363 getActionDefinitionsBuilder(G_GLOBAL_VALUE).customFor({p0});
364
365 // load/store: add more corner cases
366 for (unsigned Op : {G_LOAD, G_STORE}) {
367 auto &Action = getActionDefinitionsBuilder(Op);
368 Action.legalForTypesWithMemDesc({{s8, p0, s8, 1},
369 {s16, p0, s16, 1},
370 {s32, p0, s32, 1},
371 {s80, p0, s80, 1},
372 {p0, p0, p0, 1},
373 {v4s8, p0, v4s8, 1}});
374 if (Is64Bit)
375 Action.legalForTypesWithMemDesc(
376 {{s64, p0, s64, 1}, {v2s32, p0, v2s32, 1}});
377
378 if (HasSSE1)
379 Action.legalForTypesWithMemDesc({{v4s32, p0, v4s32, 1}});
380 if (HasSSE2)
381 Action.legalForTypesWithMemDesc({{v16s8, p0, v16s8, 1},
382 {v8s16, p0, v8s16, 1},
383 {v2s64, p0, v2s64, 1},
384 {v2p0, p0, v2p0, 1}});
385 if (HasAVX)
386 Action.legalForTypesWithMemDesc({{v32s8, p0, v32s8, 1},
387 {v16s16, p0, v16s16, 1},
388 {v8s32, p0, v8s32, 1},
389 {v4s64, p0, v4s64, 1},
390 {v4p0, p0, v4p0, 1}});
391 if (HasAVX512)
392 Action.legalForTypesWithMemDesc({{v64s8, p0, v64s8, 1},
393 {v32s16, p0, v32s16, 1},
394 {v16s32, p0, v16s32, 1},
395 {v8s64, p0, v8s64, 1}});
396
397 // X86 supports extending loads but not stores for GPRs
398 if (Op == G_LOAD) {
399 Action.legalForTypesWithMemDesc({{s8, p0, s1, 1},
400 {s16, p0, s8, 1},
401 {s32, p0, s8, 1},
402 {s32, p0, s16, 1}});
403 if (Is64Bit)
404 Action.legalForTypesWithMemDesc(
405 {{s64, p0, s8, 1}, {s64, p0, s16, 1}, {s64, p0, s32, 1}});
406 } else {
407 Action.customIf([=](const LegalityQuery &Query) {
408 return Query.Types[0] != Query.MMODescrs[0].MemoryTy;
409 });
410 }
411 Action.widenScalarToNextPow2(0, /*Min=*/8)
412 .clampScalar(0, s8, sMaxScalar)
413 .scalarize(0);
414 }
415
416 for (unsigned Op : {G_SEXTLOAD, G_ZEXTLOAD}) {
417 auto &Action = getActionDefinitionsBuilder(Op);
418 Action.legalForTypesWithMemDesc(
419 {{s16, p0, s8, 1}, {s32, p0, s8, 1}, {s32, p0, s16, 1}});
420 if (Is64Bit)
421 Action.legalForTypesWithMemDesc(
422 {{s64, p0, s8, 1}, {s64, p0, s16, 1}, {s64, p0, s32, 1}});
423 // TODO - SSE41/AVX2/AVX512F/AVX512BW vector extensions
424 }
425
426 for (unsigned Op : {G_FPEXTLOAD, G_FPTRUNCSTORE}) {
427 auto &Action = getActionDefinitionsBuilder(Op);
428 Action.legalForTypesWithMemDesc(
429 UseX87, {{s80, p0, s32, 1}, {s80, p0, s64, 1}, {s64, p0, s32, 1}});
430 }
431
432 // sext, zext, and anyext
434 .legalFor({s8, s16, s32, s128})
435 .legalFor(Is64Bit, {s64})
436 .widenScalarToNextPow2(0, /*Min=*/8)
437 .clampScalar(0, s8, sMaxScalar)
438 .widenScalarToNextPow2(1, /*Min=*/8)
439 .clampScalar(1, s8, sMaxScalar)
440 .scalarize(0);
441
442 getActionDefinitionsBuilder({G_SEXT, G_ZEXT})
443 .legalFor({s8, s16, s32})
444 .legalFor(Is64Bit, {s64})
445 .widenScalarToNextPow2(0, /*Min=*/8)
446 .clampScalar(0, s8, sMaxScalar)
447 .widenScalarToNextPow2(1, /*Min=*/8)
448 .clampScalar(1, s8, sMaxScalar)
449 .scalarize(0);
450
452 {s1, s8, s16, s32, s64}, {s8, s16, s32, s64, s128});
453
454 getActionDefinitionsBuilder(G_SEXT_INREG).lower();
455
456 // fp constants
457 getActionDefinitionsBuilder(G_FCONSTANT)
458 .legalFor({s32, s64})
459 .legalFor(UseX87, {s80});
460
461 // fp arithmetic
462 getActionDefinitionsBuilder({G_FADD, G_FSUB, G_FMUL, G_FDIV})
463 .legalFor({s32, s64})
464 .legalFor(HasSSE1, {v4s32})
465 .legalFor(HasSSE2, {v2s64})
466 .legalFor(HasAVX, {v8s32, v4s64})
467 .legalFor(HasAVX512, {v16s32, v8s64})
468 .legalFor(UseX87, {s80});
469
471 .legalFor(UseX87, {s80})
472 .legalFor(UseX87 && !Is64Bit, {s64})
473 .lower();
474
475 // fp comparison
477 .legalFor(HasSSE1 || UseX87, {s8, s32})
478 .legalFor(HasSSE2 || UseX87, {s8, s64})
479 .legalFor(UseX87, {s8, s80})
480 .clampScalar(0, s8, s8)
481 .clampScalar(1, s32, HasSSE2 ? s64 : s32)
483
484 // fp conversions
486 .legalFor(HasSSE2, {{s64, s32}})
487 .legalFor(HasAVX, {{v4s64, v4s32}})
488 .legalFor(HasAVX512, {{v8s64, v8s32}})
489 .lowerFor(UseX87, {{s64, s32}, {s80, s32}, {s80, s64}})
490 .libcall();
491
493 .legalFor(HasSSE2, {{s32, s64}})
494 .legalFor(HasAVX, {{v4s32, v4s64}})
495 .legalFor(HasAVX512, {{v8s32, v8s64}})
496 .lowerFor(UseX87, {{s32, s64}, {s32, s80}, {s64, s80}});
497
499 .legalFor(HasSSE1, {{s32, s32}})
500 .legalFor(HasSSE1 && Is64Bit, {{s32, s64}})
501 .legalFor(HasSSE2, {{s64, s32}})
502 .legalFor(HasSSE2 && Is64Bit, {{s64, s64}})
503 .clampScalar(1, (UseX87 && !HasSSE1) ? s16 : s32, sMaxScalar)
505 .customForCartesianProduct(UseX87, {s32, s64, s80}, {s16, s32, s64})
506 .clampScalar(0, s32, HasSSE2 ? s64 : s32)
508
510 .legalFor(HasSSE1, {{s32, s32}})
511 .legalFor(HasSSE1 && Is64Bit, {{s64, s32}})
512 .legalFor(HasSSE2, {{s32, s64}})
513 .legalFor(HasSSE2 && Is64Bit, {{s64, s64}})
514 .clampScalar(0, (UseX87 && !HasSSE1) ? s16 : s32, sMaxScalar)
516 .customForCartesianProduct(UseX87, {s16, s32, s64}, {s32, s64, s80})
517 .clampScalar(1, s32, HasSSE2 ? s64 : s32)
519
520 // For G_UITOFP and G_FPTOUI without AVX512, we have to custom legalize types
521 // <= s32 manually. Otherwise, in custom handler there is no way to
522 // understand whether s32 is an original type and we need to promote it to
523 // s64 or s32 is obtained after widening and we shouldn't widen it to s64.
524 //
525 // For AVX512 we simply widen types as there is direct mapping from opcodes
526 // to asm instructions.
528 .legalFor(HasAVX512, {{s32, s32}, {s32, s64}, {s64, s32}, {s64, s64}})
529 .customIf([=](const LegalityQuery &Query) {
530 return !HasAVX512 &&
531 ((HasSSE1 && typeIs(0, s32)(Query)) ||
532 (HasSSE2 && typeIs(0, s64)(Query))) &&
533 scalarNarrowerThan(1, Is64Bit ? 64 : 32)(Query);
534 })
535 .lowerIf([=](const LegalityQuery &Query) {
536 // Lower conversions from s64
537 return !HasAVX512 &&
538 ((HasSSE1 && typeIs(0, s32)(Query)) ||
539 (HasSSE2 && typeIs(0, s64)(Query))) &&
540 (Is64Bit && typeIs(1, s64)(Query));
541 })
542 .clampScalar(0, s32, HasSSE2 ? s64 : s32)
544 .clampScalar(1, s32, sMaxScalar)
546
548 .legalFor(HasAVX512, {{s32, s32}, {s32, s64}, {s64, s32}, {s64, s64}})
549 .customIf([=](const LegalityQuery &Query) {
550 return !HasAVX512 &&
551 ((HasSSE1 && typeIs(1, s32)(Query)) ||
552 (HasSSE2 && typeIs(1, s64)(Query))) &&
553 scalarNarrowerThan(0, Is64Bit ? 64 : 32)(Query);
554 })
555 // TODO: replace with customized legalization using
556 // specifics of cvttsd2si. The selection of this node requires
557 // a vector type. Either G_SCALAR_TO_VECTOR is needed or more advanced
558 // support of G_BUILD_VECTOR/G_INSERT_VECTOR_ELT is required beforehand.
559 .lowerIf([=](const LegalityQuery &Query) {
560 return !HasAVX512 &&
561 ((HasSSE1 && typeIs(1, s32)(Query)) ||
562 (HasSSE2 && typeIs(1, s64)(Query))) &&
563 (Is64Bit && typeIs(0, s64)(Query));
564 })
565 .clampScalar(0, s32, sMaxScalar)
567 .clampScalar(1, s32, HasSSE2 ? s64 : s32)
569
570 // vector ops
571 getActionDefinitionsBuilder(G_BUILD_VECTOR)
572 .customIf([=](const LegalityQuery &Query) {
573 return (HasSSE1 && typeInSet(0, {v4s32})(Query)) ||
574 (HasSSE2 && typeInSet(0, {v2s64, v8s16, v16s8})(Query)) ||
575 (HasAVX && typeInSet(0, {v4s64, v8s32, v16s16, v32s8})(Query)) ||
576 (HasAVX512 &&
577 typeInSet(0, {v8s64, v16s32, v32s16, v64s8})(Query));
578 })
579 .clampNumElements(0, v16s8, s8MaxVector)
580 .clampNumElements(0, v8s16, s16MaxVector)
581 .clampNumElements(0, v4s32, s32MaxVector)
582 .clampNumElements(0, v2s64, s64MaxVector)
584
585 getActionDefinitionsBuilder({G_EXTRACT, G_INSERT})
586 .legalIf([=](const LegalityQuery &Query) {
587 unsigned SubIdx = Query.Opcode == G_EXTRACT ? 0 : 1;
588 unsigned FullIdx = Query.Opcode == G_EXTRACT ? 1 : 0;
589 return (HasAVX && typePairInSet(SubIdx, FullIdx,
590 {{v16s8, v32s8},
591 {v8s16, v16s16},
592 {v4s32, v8s32},
593 {v2s64, v4s64}})(Query)) ||
594 (HasAVX512 && typePairInSet(SubIdx, FullIdx,
595 {{v16s8, v64s8},
596 {v32s8, v64s8},
597 {v8s16, v32s16},
598 {v16s16, v32s16},
599 {v4s32, v16s32},
600 {v8s32, v16s32},
601 {v2s64, v8s64},
602 {v4s64, v8s64}})(Query));
603 });
604
605 // todo: only permit dst types up to max legal vector register size?
606 getActionDefinitionsBuilder(G_CONCAT_VECTORS)
607 .legalFor(
608 HasSSE1,
609 {{v32s8, v16s8}, {v16s16, v8s16}, {v8s32, v4s32}, {v4s64, v2s64}})
610 .legalFor(HasAVX, {{v64s8, v16s8},
611 {v64s8, v32s8},
612 {v32s16, v8s16},
613 {v32s16, v16s16},
614 {v16s32, v4s32},
615 {v16s32, v8s32},
616 {v8s64, v2s64},
617 {v8s64, v4s64}});
618
619 // todo: vectors and address spaces
621 .legalFor({{s16, s32}, {s32, s32}, {p0, s32}})
622 .legalFor(!HasCMOV, {{s8, s32}})
623 .legalFor(Is64Bit, {{s64, s32}})
624 .legalFor(UseX87, {{s80, s32}})
625 .clampScalar(1, s32, s32)
626 .widenScalarToNextPow2(0, /*Min=*/8)
627 .clampScalar(0, HasCMOV ? s16 : s8, sMaxScalar);
628
629 // memory intrinsics
630 getActionDefinitionsBuilder({G_MEMCPY, G_MEMMOVE, G_MEMSET}).libcall();
631
632 getActionDefinitionsBuilder({G_DYN_STACKALLOC, G_STACKSAVE, G_STACKRESTORE})
633 .lower();
634
635 // fp intrinsics
636 // fpclass for i686 is disabled for llvm issue #171992
637 getActionDefinitionsBuilder(G_IS_FPCLASS)
638 .lowerFor(Is64Bit, {{s1, s32}, {s1, s64}, {s1, s80}});
639
640 getActionDefinitionsBuilder({G_INTRINSIC_ROUNDEVEN, G_INTRINSIC_TRUNC})
641 .scalarize(0)
642 .minScalar(0, LLT::scalar(32))
643 .libcall();
644
645 getActionDefinitionsBuilder({G_INTRINSIC, G_INTRINSIC_W_SIDE_EFFECTS})
646 .alwaysLegal();
647 getActionDefinitionsBuilder({G_TRAP, G_DEBUGTRAP, G_UBSANTRAP}).alwaysLegal();
648 getActionDefinitionsBuilder(G_INVOKE_REGION_START).alwaysLegal();
649
650 verify(*STI.getInstrInfo());
651}
652
654 LostDebugLocObserver &LocObserver) const {
655 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
656 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
657 switch (MI.getOpcode()) {
658 default:
659 // No idea what to do.
660 return false;
661 case TargetOpcode::G_BUILD_VECTOR:
662 return legalizeBuildVector(MI, MRI, Helper);
663 case TargetOpcode::G_FPTOUI:
664 return legalizeFPTOUI(MI, MRI, Helper);
665 case TargetOpcode::G_UITOFP:
666 return legalizeUITOFP(MI, MRI, Helper);
667 case TargetOpcode::G_STORE:
668 return legalizeNarrowingStore(MI, MRI, Helper);
669 case TargetOpcode::G_SITOFP:
670 return legalizeSITOFP(MI, MRI, Helper);
671 case TargetOpcode::G_FPTOSI:
672 return legalizeFPTOSI(MI, MRI, Helper);
673 case TargetOpcode::G_GET_ROUNDING:
674 return legalizeGETROUNDING(MI, MRI, Helper);
675 case TargetOpcode::G_SET_ROUNDING:
676 return legalizeSETROUNDING(MI, MRI, Helper);
677 case TargetOpcode::G_GLOBAL_VALUE:
678 return legalizeGLOBAL_VALUE(MI, MRI, Helper);
679 }
680 llvm_unreachable("expected switch to return");
681}
682
683bool X86LegalizerInfo::legalizeSITOFP(MachineInstr &MI,
685 LegalizerHelper &Helper) const {
686 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
687 MachineFunction &MF = *MI.getMF();
688 auto [Dst, DstTy, Src, SrcTy] = MI.getFirst2RegLLTs();
689
690 assert((SrcTy.getSizeInBits() == 16 || SrcTy.getSizeInBits() == 32 ||
691 SrcTy.getSizeInBits() == 64) &&
692 "Unexpected source type for SITOFP in X87 mode.");
693
694 TypeSize MemSize = SrcTy.getSizeInBytes();
695 MachinePointerInfo PtrInfo;
696 Align Alignmt = Helper.getStackTemporaryAlignment(SrcTy);
697 auto SlotPointer = Helper.createStackTemporary(MemSize, Alignmt, PtrInfo);
699 PtrInfo, MachineMemOperand::MOStore, MemSize, Align(MemSize));
700
701 // Store the integer value on the FPU stack.
702 MIRBuilder.buildStore(Src, SlotPointer, *StoreMMO);
703
705 PtrInfo, MachineMemOperand::MOLoad, MemSize, Align(MemSize));
706 MIRBuilder.buildInstr(X86::G_FILD)
707 .addDef(Dst)
708 .addUse(SlotPointer.getReg(0))
709 .addMemOperand(LoadMMO);
710
711 MI.eraseFromParent();
712 return true;
713}
714
715bool X86LegalizerInfo::legalizeFPTOSI(MachineInstr &MI,
717 LegalizerHelper &Helper) const {
718 MachineFunction &MF = *MI.getMF();
719 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
720 auto [Dst, DstTy, Src, SrcTy] = MI.getFirst2RegLLTs();
721
722 TypeSize MemSize = DstTy.getSizeInBytes();
723 MachinePointerInfo PtrInfo;
724 Align Alignmt = Helper.getStackTemporaryAlignment(DstTy);
725 auto SlotPointer = Helper.createStackTemporary(MemSize, Alignmt, PtrInfo);
727 PtrInfo, MachineMemOperand::MOStore, MemSize, Align(MemSize));
728
729 MIRBuilder.buildInstr(X86::G_FIST)
730 .addUse(Src)
731 .addUse(SlotPointer.getReg(0))
732 .addMemOperand(StoreMMO);
733
734 MIRBuilder.buildLoad(Dst, SlotPointer, PtrInfo, Align(MemSize));
735 MI.eraseFromParent();
736 return true;
737}
738
739bool X86LegalizerInfo::legalizeBuildVector(MachineInstr &MI,
741 LegalizerHelper &Helper) const {
742 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
743 const auto &BuildVector = cast<GBuildVector>(MI);
744 Register Dst = BuildVector.getReg(0);
745 LLT DstTy = MRI.getType(Dst);
746 MachineFunction &MF = MIRBuilder.getMF();
747 LLVMContext &Ctx = MF.getFunction().getContext();
748 uint64_t DstTySize = DstTy.getScalarSizeInBits();
749
750 SmallVector<Constant *, 4> CstIdxs;
751 for (unsigned i = 0; i < BuildVector.getNumSources(); ++i) {
752 Register Source = BuildVector.getSourceReg(i);
753
754 auto ValueAndReg = getIConstantVRegValWithLookThrough(Source, MRI);
755 if (ValueAndReg) {
756 CstIdxs.emplace_back(ConstantInt::get(Ctx, ValueAndReg->Value));
757 continue;
758 }
759
760 auto FPValueAndReg = getFConstantVRegValWithLookThrough(Source, MRI);
761 if (FPValueAndReg) {
762 CstIdxs.emplace_back(ConstantFP::get(Ctx, FPValueAndReg->Value));
763 continue;
764 }
765
766 if (getOpcodeDef<GImplicitDef>(Source, MRI)) {
767 CstIdxs.emplace_back(UndefValue::get(Type::getIntNTy(Ctx, DstTySize)));
768 continue;
769 }
770 return false;
771 }
772
773 Constant *ConstVal = ConstantVector::get(CstIdxs);
774
775 const DataLayout &DL = MIRBuilder.getDataLayout();
776 unsigned AddrSpace = DL.getDefaultGlobalsAddressSpace();
777 Align Alignment(DL.getABITypeAlign(ConstVal->getType()));
778 auto Addr = MIRBuilder.buildConstantPool(
779 LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace)),
780 MF.getConstantPool()->getConstantPoolIndex(ConstVal, Alignment));
781 MachineMemOperand *MMO =
783 MachineMemOperand::MOLoad, DstTy, Alignment);
784
785 MIRBuilder.buildLoad(Dst, Addr, *MMO);
786 MI.eraseFromParent();
787 return true;
788}
789
790bool X86LegalizerInfo::legalizeFPTOUI(MachineInstr &MI,
792 LegalizerHelper &Helper) const {
793 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
794 auto [Dst, DstTy, Src, SrcTy] = MI.getFirst2RegLLTs();
795 unsigned DstSizeInBits = DstTy.getScalarSizeInBits();
796 const LLT s32 = LLT::scalar(32);
797 const LLT s64 = LLT::scalar(64);
798
799 // Simply reuse FPTOSI when it is possible to widen the type
800 if (DstSizeInBits <= 32) {
801 auto Casted = MIRBuilder.buildFPTOSI(DstTy == s32 ? s64 : s32, Src);
802 MIRBuilder.buildTrunc(Dst, Casted);
803 MI.eraseFromParent();
804 return true;
805 }
806
807 return false;
808}
809
810bool X86LegalizerInfo::legalizeUITOFP(MachineInstr &MI,
812 LegalizerHelper &Helper) const {
813 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
814 auto [Dst, DstTy, Src, SrcTy] = MI.getFirst2RegLLTs();
815 const LLT s32 = LLT::scalar(32);
816 const LLT s64 = LLT::scalar(64);
817
818 // Simply reuse SITOFP when it is possible to widen the type
819 if (SrcTy.getSizeInBits() <= 32) {
820 auto Ext = MIRBuilder.buildZExt(SrcTy == s32 ? s64 : s32, Src);
821 MIRBuilder.buildSITOFP(Dst, Ext);
822 MI.eraseFromParent();
823 return true;
824 }
825
826 return false;
827}
828
829bool X86LegalizerInfo::legalizeNarrowingStore(MachineInstr &MI,
831 LegalizerHelper &Helper) const {
832 auto &Store = cast<GStore>(MI);
833 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
834 MachineMemOperand &MMO = **Store.memoperands_begin();
835 MachineFunction &MF = MIRBuilder.getMF();
836 LLT ValTy = MRI.getType(Store.getValueReg());
837 auto *NewMMO = MF.getMachineMemOperand(&MMO, MMO.getPointerInfo(), ValTy);
838
840 Store.setMemRefs(MF, {NewMMO});
842 return true;
843}
844
845bool X86LegalizerInfo::legalizeGETROUNDING(MachineInstr &MI,
847 LegalizerHelper &Helper) const {
848 /*
849 The rounding mode is in bits 11:10 of FPSR, and has the following
850 settings:
851 00 Round to nearest
852 01 Round to -inf
853 10 Round to +inf
854 11 Round to 0
855
856 GET_ROUNDING, on the other hand, expects the following:
857 -1 Undefined
858 0 Round to 0
859 1 Round to nearest
860 2 Round to +inf
861 3 Round to -inf
862
863 To perform the conversion, we use a packed lookup table of the four 2-bit
864 values that we can index by FPSP[11:10]
865 0x2d --> (0b00,10,11,01) --> (0,2,3,1) >> FPSR[11:10]
866
867 (0x2d >> ((FPSR >> 9) & 6)) & 3
868 */
869
870 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
871 MachineFunction &MF = MIRBuilder.getMF();
872 Register Dst = MI.getOperand(0).getReg();
873 LLT DstTy = MRI.getType(Dst);
874 const LLT s8 = LLT::scalar(8);
875 const LLT s16 = LLT::scalar(16);
876 const LLT s32 = LLT::scalar(32);
877
878 // Save FP Control Word to stack slot
879 int MemSize = 2;
880 Align Alignment = Align(2);
881 MachinePointerInfo PtrInfo;
882 auto StackTemp = Helper.createStackTemporary(TypeSize::getFixed(MemSize),
883 Alignment, PtrInfo);
884 Register StackPtr = StackTemp.getReg(0);
885
886 auto StoreMMO = MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore,
887 MemSize, Alignment);
888
889 // Store FP Control Word to stack slot using G_FNSTCW16
890 MIRBuilder.buildInstr(X86::G_FNSTCW16)
891 .addUse(StackPtr)
892 .addMemOperand(StoreMMO);
893
894 // Load FP Control Word from stack slot
895 auto LoadMMO = MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOLoad,
896 MemSize, Alignment);
897
898 auto CWD32 =
899 MIRBuilder.buildZExt(s32, MIRBuilder.buildLoad(s16, StackPtr, *LoadMMO));
900 auto Shifted8 = MIRBuilder.buildTrunc(
901 s8, MIRBuilder.buildLShr(s32, CWD32, MIRBuilder.buildConstant(s8, 9)));
902 auto Masked32 = MIRBuilder.buildZExt(
903 s32, MIRBuilder.buildAnd(s8, Shifted8, MIRBuilder.buildConstant(s8, 6)));
904
905 // LUT is a packed lookup table (0x2d) used to map the 2-bit x87 FPU rounding
906 // mode (from bits 11:10 of the control word) to the values expected by
907 // GET_ROUNDING. The mapping is performed by shifting LUT right by the
908 // extracted rounding mode and masking the result with 3 to obtain the final
909 auto LUT = MIRBuilder.buildConstant(s32, 0x2d);
910 auto LUTShifted = MIRBuilder.buildLShr(s32, LUT, Masked32);
911 auto RetVal =
912 MIRBuilder.buildAnd(s32, LUTShifted, MIRBuilder.buildConstant(s32, 3));
913 auto RetValTrunc = MIRBuilder.buildZExtOrTrunc(DstTy, RetVal);
914
915 MIRBuilder.buildCopy(Dst, RetValTrunc);
916
917 MI.eraseFromParent();
918 return true;
919}
920
921bool X86LegalizerInfo::legalizeSETROUNDING(MachineInstr &MI,
923 LegalizerHelper &Helper) const {
924 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
925 MachineFunction &MF = MIRBuilder.getMF();
926 Register Src = MI.getOperand(0).getReg();
927 const LLT s8 = LLT::scalar(8);
928 const LLT s16 = LLT::scalar(16);
929 const LLT s32 = LLT::scalar(32);
930
931 // Allocate stack slot for control word and MXCSR (4 bytes).
932 int MemSize = 4;
933 Align Alignment = Align(4);
934 MachinePointerInfo PtrInfo;
935 auto StackTemp = Helper.createStackTemporary(TypeSize::getFixed(MemSize),
936 Alignment, PtrInfo);
937 Register StackPtr = StackTemp.getReg(0);
938
939 auto StoreMMO =
941 MIRBuilder.buildInstr(X86::G_FNSTCW16)
942 .addUse(StackPtr)
943 .addMemOperand(StoreMMO);
944
945 auto LoadMMO =
947 auto CWD16 = MIRBuilder.buildLoad(s16, StackPtr, *LoadMMO);
948
949 // Clear RM field (bits 11:10)
950 auto ClearedCWD =
951 MIRBuilder.buildAnd(s16, CWD16, MIRBuilder.buildConstant(s16, 0xf3ff));
952
953 // Check if Src is a constant
954 Register RMBits;
955 Register MXCSRRMBits;
956
957 APInt SrcCst;
958 if (mi_match(Src, MRI, m_ICst(SrcCst))) {
959 uint64_t RM = SrcCst.getZExtValue();
960 int FieldVal = X86::getRoundingModeX86(RM);
961
962 if (FieldVal == X86::rmInvalid) {
963 FieldVal = X86::rmToNearest;
964 LLVMContext &C = MF.getFunction().getContext();
965 C.diagnose(DiagnosticInfoUnsupported(
966 MF.getFunction(), "rounding mode is not supported by X86 hardware",
967 DiagnosticLocation(MI.getDebugLoc()), DS_Error));
968 return false;
969 }
970
971 FieldVal = FieldVal << 3;
972 RMBits = MIRBuilder.buildConstant(s16, FieldVal).getReg(0);
973 MXCSRRMBits = MIRBuilder.buildConstant(s32, FieldVal).getReg(0);
974 } else {
975 // Convert Src (rounding mode) to bits for control word
976 // (0xc9 << (2 * Src + 4)) & 0xc00
977 auto Src32 = MIRBuilder.buildZExtOrTrunc(s32, Src);
978 auto ShiftAmt = MIRBuilder.buildAdd(
979 s32, MIRBuilder.buildShl(s32, Src32, MIRBuilder.buildConstant(s32, 1)),
980 MIRBuilder.buildConstant(s32, 4));
981 auto ShiftAmt8 = MIRBuilder.buildTrunc(s8, ShiftAmt);
982 auto Shifted = MIRBuilder.buildShl(s16, MIRBuilder.buildConstant(s16, 0xc9),
983 ShiftAmt8);
984 RMBits =
985 MIRBuilder.buildAnd(s16, Shifted, MIRBuilder.buildConstant(s16, 0xc00))
986 .getReg(0);
987
988 // For non-constant case, we still need to compute MXCSR bits dynamically
989 auto RMBits32 = MIRBuilder.buildZExt(s32, RMBits);
990 MXCSRRMBits =
991 MIRBuilder.buildShl(s32, RMBits32, MIRBuilder.buildConstant(s32, 3))
992 .getReg(0);
993 }
994 // Update rounding mode bits
995 auto NewCWD =
996 MIRBuilder.buildOr(s16, ClearedCWD, RMBits, MachineInstr::Disjoint);
997
998 // Store new FP Control Word to stack
999 auto StoreNewMMO =
1001 MIRBuilder.buildStore(NewCWD, StackPtr, *StoreNewMMO);
1002
1003 // Load FP control word from the slot using G_FLDCW16
1004 auto LoadNewMMO =
1006 MIRBuilder.buildInstr(X86::G_FLDCW16)
1007 .addUse(StackPtr)
1008 .addMemOperand(LoadNewMMO);
1009
1010 if (Subtarget.hasSSE1()) {
1011 // Store MXCSR to stack (use STMXCSR)
1012 auto StoreMXCSRMMO = MF.getMachineMemOperand(
1013 PtrInfo, MachineMemOperand::MOStore, 4, Align(4));
1014 MIRBuilder.buildInstr(TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS)
1015 .addIntrinsicID(Intrinsic::x86_sse_stmxcsr)
1016 .addUse(StackPtr)
1017 .addMemOperand(StoreMXCSRMMO);
1018
1019 // Load MXCSR from stack
1020 auto LoadMXCSRMMO = MF.getMachineMemOperand(
1021 PtrInfo, MachineMemOperand::MOLoad, 4, Align(4));
1022 auto MXCSR = MIRBuilder.buildLoad(s32, StackPtr, *LoadMXCSRMMO);
1023
1024 // Clear RM field (bits 14:13)
1025 auto ClearedMXCSR = MIRBuilder.buildAnd(
1026 s32, MXCSR, MIRBuilder.buildConstant(s32, 0xffff9fff));
1027
1028 // Update rounding mode bits
1029 auto NewMXCSR = MIRBuilder.buildOr(s32, ClearedMXCSR, MXCSRRMBits);
1030
1031 // Store new MXCSR to stack
1032 auto StoreNewMXCSRMMO = MF.getMachineMemOperand(
1033 PtrInfo, MachineMemOperand::MOStore, 4, Align(4));
1034 MIRBuilder.buildStore(NewMXCSR, StackPtr, *StoreNewMXCSRMMO);
1035
1036 // Load MXCSR from stack (use LDMXCSR)
1037 auto LoadNewMXCSRMMO = MF.getMachineMemOperand(
1038 PtrInfo, MachineMemOperand::MOLoad, 4, Align(4));
1039 MIRBuilder.buildInstr(TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS)
1040 .addIntrinsicID(Intrinsic::x86_sse_ldmxcsr)
1041 .addUse(StackPtr)
1042 .addMemOperand(LoadNewMXCSRMMO);
1043 }
1044
1045 MI.eraseFromParent();
1046 return true;
1047}
1048
1049bool X86LegalizerInfo::legalizeGLOBAL_VALUE(MachineInstr &MI,
1051 LegalizerHelper &Helper) const {
1052 const GlobalValue *GV = MI.getOperand(1).getGlobal();
1053 Register Dst = MI.getOperand(0).getReg();
1054 LLT DstTy = MRI.getType(Dst);
1055 unsigned GVOpFlags = Subtarget.classifyGlobalReference(GV);
1056
1057 // For stub references (GOT/PLT), we need G_WRAPPER_RIP + load
1058 if (isGlobalStubReference(GVOpFlags)) {
1059 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
1060 MachineFunction &MF = MIRBuilder.getMF();
1061
1062 Register StubAddr = MRI.createGenericVirtualRegister(DstTy);
1063 MIRBuilder.buildInstr(X86::G_WRAPPER_RIP)
1064 .addDef(StubAddr)
1065 .addGlobalAddress(GV);
1066
1067 MachineMemOperand *MMO = MF.getMachineMemOperand(
1069 Align(DstTy.getSizeInBytes()));
1070 MIRBuilder.buildLoad(Dst, StubAddr, *MMO);
1071 MI.eraseFromParent();
1072 }
1073 return true;
1074}
1075
1077 MachineInstr &MI) const {
1078 return true;
1079}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
bpf unwind lower
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static void scalarize(Instruction *I, SmallVectorImpl< Instruction * > &Worklist)
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
IRTranslator LLVM IR MI
Contains matchers for matching SSA Machine Instructions.
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
This file declares the MachineIRBuilder class.
Promote Memory to Register
Definition Mem2Reg.cpp:110
ppc ctr loops verify
MachineRegisterInfo & MRI
static const char LUT[]
This file declares the targeting of the Machinelegalizer class for X86.
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:352
virtual void changingInstr(MachineInstr &MI)=0
This instruction is about to be mutated in some way.
virtual void changedInstr(MachineInstr &MI)=0
This instruction was mutated in some way.
constexpr unsigned getScalarSizeInBits() const
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
constexpr TypeSize getSizeInBytes() const
Returns the total size of the type in bytes, i.e.
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
LegalizeRuleSet & minScalar(unsigned TypeIdx, const LLT Ty)
Ensure the scalar is at least as wide as Ty.
LegalizeRuleSet & legalFor(std::initializer_list< LLT > Types)
The instruction is legal when type index 0 is any type in the given list.
LegalizeRuleSet & scalarSameSizeAs(unsigned TypeIdx, unsigned SameSizeIdx)
Change the type TypeIdx to have the same scalar size as type SameSizeIdx.
LegalizeRuleSet & libcall()
The instruction is emitted as a library call.
LegalizeRuleSet & clampMaxNumElements(unsigned TypeIdx, const LLT EltTy, unsigned MaxElements)
Limit the number of elements in EltTy vectors to at most MaxElements.
LegalizeRuleSet & clampMinNumElements(unsigned TypeIdx, const LLT EltTy, unsigned MinElements)
Limit the number of elements in EltTy vectors to at least MinElements.
LegalizeRuleSet & customForCartesianProduct(std::initializer_list< LLT > Types)
LegalizeRuleSet & moreElementsToNextPow2(unsigned TypeIdx)
Add more elements to the vector to reach the next power of two.
LegalizeRuleSet & lower()
The instruction is lowered.
LegalizeRuleSet & lowerFor(std::initializer_list< LLT > Types)
The instruction is lowered when type index 0 is any type in the given list.
LegalizeRuleSet & clampScalar(unsigned TypeIdx, const LLT MinTy, const LLT MaxTy)
Limit the range of scalar sizes to MinTy and MaxTy.
LegalizeRuleSet & alwaysLegal()
LegalizeRuleSet & clampNumElements(unsigned TypeIdx, const LLT MinTy, const LLT MaxTy)
Limit the number of elements for the given vectors to at least MinTy's number of elements and at most...
LegalizeRuleSet & customIf(LegalityPredicate Predicate)
LegalizeRuleSet & widenScalarToNextPow2(unsigned TypeIdx, unsigned MinSize=0)
Widen the scalar to the next power of two that is at least MinSize.
LegalizeRuleSet & scalarize(unsigned TypeIdx)
LegalizeRuleSet & legalForCartesianProduct(std::initializer_list< LLT > Types)
The instruction is legal when type indexes 0 and 1 are both in the given list.
LegalizeRuleSet & legalIf(LegalityPredicate Predicate)
The instruction is legal if predicate is true.
LegalizeRuleSet & customFor(std::initializer_list< LLT > Types)
LLVM_ABI MachineInstrBuilder createStackTemporary(TypeSize Bytes, Align Alignment, MachinePointerInfo &PtrInfo)
Create a stack temporary based on the size in bytes and the alignment.
GISelChangeObserver & Observer
To keep track of changes made by the LegalizerHelper.
MachineIRBuilder & MIRBuilder
Expose MIRBuilder so clients can set their own RecordInsertInstruction functions.
LLVM_ABI Align getStackTemporaryAlignment(LLT Type, Align MinAlign=Align()) const
Return the alignment to use for a stack temporary object with the given type.
LegalizeRuleSet & getActionDefinitionsBuilder(unsigned Opcode)
Get the action definition builder for the given opcode.
LLVM_ABI unsigned getConstantPoolIndex(const Constant *C, Align Alignment)
getConstantPoolIndex - Create a new entry in the constant pool or return an existing one.
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineConstantPool * getConstantPool()
getConstantPool - Return the constant pool object for the current function.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
Helper class to build MachineInstr.
MachineInstrBuilder buildFPTOSI(const DstOp &Dst, const SrcOp &Src0)
Build and insert Res = G_FPTOSI Src0.
MachineInstrBuilder buildAdd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_ADD Op0, Op1.
MachineInstrBuilder buildConstantPool(const DstOp &Res, unsigned Idx)
Build and insert Res = G_CONSTANT_POOL Idx.
MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1)
Build and insert Res = G_AND Op0, Op1.
MachineInstrBuilder buildLShr(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_ZEXT Op.
MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert Res = G_LOAD Addr, MMO.
MachineInstrBuilder buildZExtOrTrunc(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_ZEXT Op, Res = G_TRUNC Op, or Res = COPY Op depending on the differing sizes...
MachineInstrBuilder buildShl(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineInstrBuilder buildStore(const SrcOp &Val, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert G_STORE Val, Addr, MMO.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineInstrBuilder buildSITOFP(const DstOp &Dst, const SrcOp &Src0)
Build and insert Res = G_SITOFP Src0.
MachineFunction & getMF()
Getter for the function we currently build.
MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_TRUNC Op.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildOr(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_OR Op0, Op1.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
const DataLayout & getDataLayout() const
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addIntrinsicID(Intrinsic::ID ID) const
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
A description of a memory reference used in the backend.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
LLVM_ABI Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
reference emplace_back(ArgTypes &&... Args)
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:352
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:298
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
bool legalizeCustom(LegalizerHelper &Helper, MachineInstr &MI, LostDebugLocObserver &LocObserver) const override
Called for instructions with the Custom LegalizationAction.
bool legalizeIntrinsic(LegalizerHelper &Helper, MachineInstr &MI) const override
X86LegalizerInfo(const X86Subtarget &STI, const X86TargetMachine &TM)
const X86InstrInfo * getInstrInfo() const override
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
LLVM_ABI LegalityPredicate scalarOrEltWiderThan(unsigned TypeIdx, unsigned Size)
True iff the specified type index is a scalar or a vector with an element type that's wider than the ...
LLVM_ABI LegalityPredicate typeInSet(unsigned TypeIdx, std::initializer_list< LLT > TypesInit)
True iff the given type index is one of the specified types.
LLVM_ABI LegalityPredicate typePairInSet(unsigned TypeIdx0, unsigned TypeIdx1, std::initializer_list< std::pair< LLT, LLT > > TypesInit)
True iff the given types for the given pair of type indexes is one of the specified type pairs.
LLVM_ABI LegalityPredicate typeIs(unsigned TypeIdx, LLT TypesInit)
True iff the given type index is the specified type.
LLVM_ABI LegalityPredicate scalarNarrowerThan(unsigned TypeIdx, unsigned Size)
True iff the specified type index is a scalar that's narrower than the given size.
ConstantMatch< APInt > m_ICst(APInt &Cst)
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
Invariant opcodes: All instruction sets have these as their low opcodes.
int getRoundingModeX86(unsigned RM)
Convert LLVM rounding mode to X86 rounding mode.
This is an optimization pass for GlobalISel generic memory operations.
static bool isGlobalStubReference(unsigned char TargetFlag)
isGlobalStubReference - Return true if the specified TargetFlag operand is a reference to a stub for ...
LLVM_ABI MachineInstr * getOpcodeDef(unsigned Opcode, Register Reg, const MachineRegisterInfo &MRI)
See if Reg is defined by an single def instruction that is Opcode.
Definition Utils.cpp:679
@ Store
The extracted value is stored (ExtractElement only).
DWARFExpression::Operation Op
LLVM_ABI std::optional< FPValueAndVReg > getFConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_FCONSTANT returns it...
Definition Utils.cpp:454
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI std::optional< ValueAndVReg > getIConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT returns its...
Definition Utils.cpp:440
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
The LegalityQuery object bundles together all the information that's needed to decide whether a given...
ArrayRef< MemDesc > MMODescrs
Operations which require memory can use this to place requirements on the memory type for each MMO.
ArrayRef< LLT > Types
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.
static LLVM_ABI MachinePointerInfo getGOT(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a GOT entry.