LLVM 24.0.0git
X86InstructionSelector.cpp
Go to the documentation of this file.
1//===- X86InstructionSelector.cpp -----------------------------------------===//
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 InstructionSelector class for
10/// X86.
11/// \todo This should be generated by TableGen.
12//===----------------------------------------------------------------------===//
13
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86InstrInfo.h"
18#include "X86RegisterBankInfo.h"
19#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
21#include "X86TargetMachine.h"
39#include "llvm/IR/DataLayout.h"
40#include "llvm/IR/InstrTypes.h"
41#include "llvm/IR/IntrinsicsX86.h"
43#include "llvm/Support/Debug.h"
47#include <cassert>
48#include <cstdint>
49#include <tuple>
50
51#define DEBUG_TYPE "X86-isel"
52
53using namespace llvm;
54using namespace MIPatternMatch;
55
56namespace {
57
58#define GET_GLOBALISEL_PREDICATE_BITSET
59#include "X86GenGlobalISel.inc"
60#undef GET_GLOBALISEL_PREDICATE_BITSET
61
62class X86InstructionSelector : public InstructionSelector {
63public:
64 X86InstructionSelector(const X86TargetMachine &TM, const X86Subtarget &STI,
65 const X86RegisterBankInfo &RBI);
66
67 bool select(MachineInstr &I) override;
68 static const char *getName() { return DEBUG_TYPE; }
69
70private:
71 /// tblgen-erated 'select' implementation, used as the initial selector for
72 /// the patterns that don't require complex C++.
73 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
74
75 // TODO: remove after supported by Tablegen-erated instruction selection.
76 unsigned getLoadStoreOp(const LLT &Ty, const RegisterBank &RB, unsigned Opc,
77 Align Alignment) const;
78 // TODO: remove once p0<->i32/i64 matching is available
79 unsigned getPtrLoadStoreOp(const LLT &Ty, const RegisterBank &RB,
80 unsigned Opc) const;
81
83 MachineFunction &MF) const;
84 bool selectFrameIndexOrGep(MachineInstr &I, MachineRegisterInfo &MRI,
85 MachineFunction &MF) const;
86 bool selectGlobalValue(MachineInstr &I, MachineRegisterInfo &MRI,
87 MachineFunction &MF) const;
88 bool selectConstant(MachineInstr &I, MachineRegisterInfo &MRI,
89 MachineFunction &MF) const;
90 bool selectTruncOrPtrToInt(MachineInstr &I, MachineRegisterInfo &MRI,
91 MachineFunction &MF) const;
92 bool selectZext(MachineInstr &I, MachineRegisterInfo &MRI,
93 MachineFunction &MF) const;
94 bool selectAnyext(MachineInstr &I, MachineRegisterInfo &MRI,
95 MachineFunction &MF) const;
96 bool selectCmp(MachineInstr &I, MachineRegisterInfo &MRI,
97 MachineFunction &MF) const;
98 bool selectFCmp(MachineInstr &I, MachineRegisterInfo &MRI,
99 MachineFunction &MF) const;
100 bool selectUAddSub(MachineInstr &I, MachineRegisterInfo &MRI,
101 MachineFunction &MF) const;
104 MachineFunction &MF);
106 MachineFunction &MF);
107 bool selectInsert(MachineInstr &I, MachineRegisterInfo &MRI,
108 MachineFunction &MF) const;
109 bool selectExtract(MachineInstr &I, MachineRegisterInfo &MRI,
110 MachineFunction &MF) const;
111 bool selectCondBranch(MachineInstr &I, MachineRegisterInfo &MRI,
112 MachineFunction &MF) const;
113 bool selectTurnIntoCOPY(MachineInstr &I, MachineRegisterInfo &MRI,
114 const Register DstReg,
115 const TargetRegisterClass *DstRC,
116 const Register SrcReg,
117 const TargetRegisterClass *SrcRC) const;
118 bool materializeFP(MachineInstr &I, MachineRegisterInfo &MRI,
119 MachineFunction &MF) const;
120 bool selectImplicitDefOrPHI(MachineInstr &I, MachineRegisterInfo &MRI) const;
121 bool selectMulDivRem(MachineInstr &I, MachineRegisterInfo &MRI,
122 MachineFunction &MF) const;
123 bool selectSelect(MachineInstr &I, MachineRegisterInfo &MRI,
124 MachineFunction &MF) const;
125
126 ComplexRendererFns selectAddr(MachineOperand &Root) const;
127
128 // emit insert subreg instruction and insert it before MachineInstr &I
129 bool emitInsertSubreg(Register DstReg, Register SrcReg, MachineInstr &I,
131 // emit extract subreg instruction and insert it before MachineInstr &I
132 bool emitExtractSubreg(Register DstReg, Register SrcReg, MachineInstr &I,
134
135 const TargetRegisterClass *getRegClass(LLT Ty, const RegisterBank &RB) const;
137 MachineRegisterInfo &MRI) const;
138
139 const X86TargetMachine &TM;
140 const X86Subtarget &STI;
141 const X86InstrInfo &TII;
142 const X86RegisterInfo &TRI;
143 const X86RegisterBankInfo &RBI;
144
145#define GET_GLOBALISEL_PREDICATES_DECL
146#include "X86GenGlobalISel.inc"
147#undef GET_GLOBALISEL_PREDICATES_DECL
148
149#define GET_GLOBALISEL_TEMPORARIES_DECL
150#include "X86GenGlobalISel.inc"
151#undef GET_GLOBALISEL_TEMPORARIES_DECL
152};
153
154} // end anonymous namespace
155
156#define GET_GLOBALISEL_IMPL
157#include "X86GenGlobalISel.inc"
158#undef GET_GLOBALISEL_IMPL
159
160X86InstructionSelector::X86InstructionSelector(const X86TargetMachine &TM,
161 const X86Subtarget &STI,
162 const X86RegisterBankInfo &RBI)
163 : TM(TM), STI(STI), TII(*STI.getInstrInfo()), TRI(*STI.getRegisterInfo()),
164 RBI(RBI),
166#include "X86GenGlobalISel.inc"
169#include "X86GenGlobalISel.inc"
171{
172}
173
174// FIXME: This should be target-independent, inferred from the types declared
175// for each class in the bank.
177X86InstructionSelector::getRegClass(LLT Ty, const RegisterBank &RB) const {
178 if (RB.getID() == X86::GPRRegBankID) {
179 if (Ty.getSizeInBits() <= 8)
180 return &X86::GR8RegClass;
181 if (Ty.getSizeInBits() == 16)
182 return &X86::GR16RegClass;
183 if (Ty.getSizeInBits() == 32)
184 return &X86::GR32RegClass;
185 if (Ty.getSizeInBits() == 64)
186 return &X86::GR64RegClass;
187 }
188 if (RB.getID() == X86::VECRRegBankID) {
189 if (Ty.getSizeInBits() == 16)
190 return STI.hasAVX512() ? &X86::FR16XRegClass : &X86::FR16RegClass;
191 if (Ty.getSizeInBits() == 32)
192 return STI.hasAVX512() ? &X86::FR32XRegClass : &X86::FR32RegClass;
193 if (Ty.getSizeInBits() == 64)
194 return STI.hasAVX512() ? &X86::FR64XRegClass : &X86::FR64RegClass;
195 if (Ty.getSizeInBits() == 128)
196 return STI.hasAVX512() ? &X86::VR128XRegClass : &X86::VR128RegClass;
197 if (Ty.getSizeInBits() == 256)
198 return STI.hasAVX512() ? &X86::VR256XRegClass : &X86::VR256RegClass;
199 if (Ty.getSizeInBits() == 512)
200 return &X86::VR512RegClass;
201 }
202
203 if (RB.getID() == X86::PSRRegBankID) {
204 if (Ty.getSizeInBits() == 80)
205 return &X86::RFP80RegClass;
206 if (Ty.getSizeInBits() == 64)
207 return &X86::RFP64RegClass;
208 if (Ty.getSizeInBits() == 32)
209 return &X86::RFP32RegClass;
210 }
211
212 llvm_unreachable("Unknown RegBank!");
213}
214
216X86InstructionSelector::getRegClass(LLT Ty, Register Reg,
217 MachineRegisterInfo &MRI) const {
218 const RegisterBank &RegBank = *RBI.getRegBank(Reg, MRI, TRI);
219 return getRegClass(Ty, RegBank);
220}
221
222static unsigned getSubRegIndex(const TargetRegisterClass *RC) {
223 unsigned SubIdx = X86::NoSubRegister;
224 if (RC == &X86::GR32RegClass) {
225 SubIdx = X86::sub_32bit;
226 } else if (RC == &X86::GR16RegClass) {
227 SubIdx = X86::sub_16bit;
228 } else if (RC == &X86::GR8RegClass) {
229 SubIdx = X86::sub_8bit;
230 }
231
232 return SubIdx;
233}
234
236 assert(Reg.isPhysical());
237 if (X86::GR64RegClass.contains(Reg))
238 return &X86::GR64RegClass;
239 if (X86::GR32RegClass.contains(Reg))
240 return &X86::GR32RegClass;
241 if (X86::GR16RegClass.contains(Reg))
242 return &X86::GR16RegClass;
243 if (X86::GR8RegClass.contains(Reg))
244 return &X86::GR8RegClass;
245
246 llvm_unreachable("Unknown RegClass for PhysReg!");
247}
248
249// Set X86 Opcode and constrain DestReg.
250bool X86InstructionSelector::selectCopy(MachineInstr &I,
251 MachineRegisterInfo &MRI) const {
252 Register DstReg = I.getOperand(0).getReg();
253 const unsigned DstSize = TRI.getRegSizeInBits(DstReg, MRI);
254 const RegisterBank &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
255
256 Register SrcReg = I.getOperand(1).getReg();
257 const unsigned SrcSize = TRI.getRegSizeInBits(SrcReg, MRI);
258 const RegisterBank &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
259
260 if (DstReg.isPhysical()) {
261 assert(I.isCopy() && "Generic operators do not allow physical registers");
262
263 if (DstSize > SrcSize && SrcRegBank.getID() == X86::GPRRegBankID &&
264 DstRegBank.getID() == X86::GPRRegBankID) {
265
266 const TargetRegisterClass *SrcRC =
267 getRegClass(MRI.getType(SrcReg), SrcRegBank);
268 const TargetRegisterClass *DstRC = getRegClassFromGRPhysReg(DstReg);
269
270 if (SrcRC != DstRC) {
271 // This case can be generated by ABI lowering, perform anyext
272 unsigned SubIdx = getSubRegIndex(SrcRC);
273 // In 32-bit mode only some registers have 8-bit subregisters.
274 const TargetRegisterClass *ExtRC =
275 TRI.getSubClassWithSubReg(DstRC, SubIdx);
276 Register ImpDef = MRI.createVirtualRegister(ExtRC);
277 BuildMI(*I.getParent(), I, I.getDebugLoc(),
278 TII.get(TargetOpcode::IMPLICIT_DEF), ImpDef);
279 Register ExtSrc = MRI.createVirtualRegister(ExtRC);
280 BuildMI(*I.getParent(), I, I.getDebugLoc(),
281 TII.get(TargetOpcode::INSERT_SUBREG), ExtSrc)
282 .addReg(ImpDef)
283 .addReg(SrcReg)
284 .addImm(SubIdx);
285
286 I.getOperand(1).setReg(ExtSrc);
287 }
288 }
289
290 // Special case GPR16 -> XMM
291 if (SrcSize == 16 && SrcRegBank.getID() == X86::GPRRegBankID &&
292 (DstRegBank.getID() == X86::VECRRegBankID)) {
293
294 const DebugLoc &DL = I.getDebugLoc();
295
296 // Any extend GPR16 -> GPR32
297 Register ImpDef = MRI.createVirtualRegister(&X86::GR32RegClass);
298 BuildMI(*I.getParent(), I, DL, TII.get(TargetOpcode::IMPLICIT_DEF),
299 ImpDef);
300 Register ExtReg = MRI.createVirtualRegister(&X86::GR32RegClass);
301 BuildMI(*I.getParent(), I, DL, TII.get(TargetOpcode::INSERT_SUBREG),
302 ExtReg)
303 .addReg(ImpDef)
304 .addReg(SrcReg)
305 .addImm(X86::sub_16bit);
306
307 // Copy GR32 -> XMM
308 BuildMI(*I.getParent(), I, DL, TII.get(TargetOpcode::COPY), DstReg)
309 .addReg(ExtReg);
310
311 I.eraseFromParent();
312 }
313
314 // Special case XMM -> GR16
315 if (DstSize == 16 && DstRegBank.getID() == X86::GPRRegBankID &&
316 (SrcRegBank.getID() == X86::VECRRegBankID)) {
317
318 const DebugLoc &DL = I.getDebugLoc();
319
320 // Move XMM to GR32 register.
321 Register Temp32 = MRI.createVirtualRegister(&X86::GR32RegClass);
322 BuildMI(*I.getParent(), I, DL, TII.get(TargetOpcode::COPY), Temp32)
323 .addReg(SrcReg);
324
325 // Extract the lower 16 bits
326 if (Register Dst32 = TRI.getMatchingSuperReg(DstReg, X86::sub_16bit,
327 &X86::GR32RegClass)) {
328 // Optimization for Physical Dst (e.g. AX): Copy to EAX directly.
329 BuildMI(*I.getParent(), I, DL, TII.get(TargetOpcode::COPY), Dst32)
330 .addReg(Temp32);
331 } else {
332 // Handle if there is no super.
333 BuildMI(*I.getParent(), I, DL, TII.get(TargetOpcode::COPY), DstReg)
334 .addReg(Temp32, {}, X86::sub_16bit);
335 }
336
337 I.eraseFromParent();
338 }
339
340 return true;
341 }
342
343 assert((!SrcReg.isPhysical() || I.isCopy()) &&
344 "No phys reg on generic operators");
345 assert(
346 (DstSize == SrcSize ||
347 // Copies are a mean to setup initial types, the number of
348 // bits may not exactly match.
349 (SrcReg.isPhysical() && DstSize <= TRI.getRegSizeInBits(SrcReg, MRI))) &&
350 "Copy with different width?!");
351
352 const TargetRegisterClass *DstRC =
353 getRegClass(MRI.getType(DstReg), DstRegBank);
354
355 if (SrcRegBank.getID() == X86::GPRRegBankID &&
356 DstRegBank.getID() == X86::GPRRegBankID && SrcSize > DstSize &&
357 SrcReg.isPhysical()) {
358 // Change the physical register to performe truncate.
359
360 const TargetRegisterClass *SrcRC = getRegClassFromGRPhysReg(SrcReg);
361
362 if (DstRC != SrcRC) {
363 I.getOperand(1).setSubReg(getSubRegIndex(DstRC));
364 I.getOperand(1).substPhysReg(SrcReg, TRI);
365 }
366 }
367
368 // No need to constrain SrcReg. It will get constrained when
369 // we hit another of its use or its defs.
370 // Copies do not have constraints.
371 const TargetRegisterClass *OldRC = MRI.getRegClassOrNull(DstReg);
372 if (!OldRC || !DstRC->hasSubClassEq(OldRC)) {
373 if (!RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
374 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
375 << " operand\n");
376 return false;
377 }
378 }
379 I.setDesc(TII.get(X86::COPY));
380 return true;
381}
382
383bool X86InstructionSelector::select(MachineInstr &I) {
384 assert(I.getParent() && "Instruction should be in a basic block!");
385 assert(I.getParent()->getParent() && "Instruction should be in a function!");
386
387 MachineBasicBlock &MBB = *I.getParent();
389 MachineRegisterInfo &MRI = MF.getRegInfo();
390
391 unsigned Opcode = I.getOpcode();
392 if (!isPreISelGenericOpcode(Opcode) && !I.isPreISelOpcode()) {
393 // Certain non-generic instructions also need some special handling.
394
395 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
396 return false;
397
398 if (I.isCopy())
399 return selectCopy(I, MRI);
400
401 return true;
402 }
403
404 assert(I.getNumOperands() == I.getNumExplicitOperands() &&
405 "Generic instruction has unexpected implicit operands\n");
406
407 if (selectImpl(I, *CoverageInfo))
408 return true;
409
410 LLVM_DEBUG(dbgs() << " C++ instruction selection: "; I.print(dbgs()));
411
412 // TODO: This should be implemented by tblgen.
413 switch (I.getOpcode()) {
414 default:
415 return false;
416 case TargetOpcode::G_STORE:
417 case TargetOpcode::G_LOAD:
418 return selectLoadStoreOp(I, MRI, MF);
419 case TargetOpcode::G_PTR_ADD:
420 case TargetOpcode::G_FRAME_INDEX:
421 return selectFrameIndexOrGep(I, MRI, MF);
422 case TargetOpcode::G_GLOBAL_VALUE:
423 return selectGlobalValue(I, MRI, MF);
424 case TargetOpcode::G_CONSTANT:
425 return selectConstant(I, MRI, MF);
426 case TargetOpcode::G_FCONSTANT:
427 return materializeFP(I, MRI, MF);
428 case TargetOpcode::G_PTRTOINT:
429 case TargetOpcode::G_TRUNC:
430 return selectTruncOrPtrToInt(I, MRI, MF);
431 case TargetOpcode::G_INTTOPTR:
432 case TargetOpcode::G_FREEZE:
433 return selectCopy(I, MRI);
434 case TargetOpcode::G_ZEXT:
435 return selectZext(I, MRI, MF);
436 case TargetOpcode::G_ANYEXT:
437 return selectAnyext(I, MRI, MF);
438 case TargetOpcode::G_ICMP:
439 return selectCmp(I, MRI, MF);
440 case TargetOpcode::G_FCMP:
441 return selectFCmp(I, MRI, MF);
442 case TargetOpcode::G_UADDE:
443 case TargetOpcode::G_UADDO:
444 case TargetOpcode::G_USUBE:
445 case TargetOpcode::G_USUBO:
446 return selectUAddSub(I, MRI, MF);
447 case TargetOpcode::G_UNMERGE_VALUES:
448 return selectUnmergeValues(I, MRI, MF);
449 case TargetOpcode::G_MERGE_VALUES:
450 case TargetOpcode::G_CONCAT_VECTORS:
451 return selectMergeValues(I, MRI, MF);
452 case TargetOpcode::G_EXTRACT:
453 return selectExtract(I, MRI, MF);
454 case TargetOpcode::G_INSERT:
455 return selectInsert(I, MRI, MF);
456 case TargetOpcode::G_BRCOND:
457 return selectCondBranch(I, MRI, MF);
458 case TargetOpcode::G_IMPLICIT_DEF:
459 case TargetOpcode::G_PHI:
460 return selectImplicitDefOrPHI(I, MRI);
461 case TargetOpcode::G_MUL:
462 case TargetOpcode::G_SMULH:
463 case TargetOpcode::G_UMULH:
464 case TargetOpcode::G_SDIV:
465 case TargetOpcode::G_UDIV:
466 case TargetOpcode::G_SREM:
467 case TargetOpcode::G_UREM:
468 return selectMulDivRem(I, MRI, MF);
469 case TargetOpcode::G_SELECT:
470 return selectSelect(I, MRI, MF);
471 }
472
473 return false;
474}
475
476unsigned X86InstructionSelector::getPtrLoadStoreOp(const LLT &Ty,
477 const RegisterBank &RB,
478 unsigned Opc) const {
479 assert((Opc == TargetOpcode::G_STORE || Opc == TargetOpcode::G_LOAD) &&
480 "Only G_STORE and G_LOAD are expected for selection");
481 if (Ty.isPointer() && X86::GPRRegBankID == RB.getID()) {
482 bool IsLoad = (Opc == TargetOpcode::G_LOAD);
483 switch (Ty.getSizeInBits()) {
484 default:
485 break;
486 case 32:
487 return IsLoad ? X86::MOV32rm : X86::MOV32mr;
488 case 64:
489 return IsLoad ? X86::MOV64rm : X86::MOV64mr;
490 }
491 }
492 return Opc;
493}
494
495unsigned X86InstructionSelector::getLoadStoreOp(const LLT &Ty,
496 const RegisterBank &RB,
497 unsigned Opc,
498 Align Alignment) const {
499 bool Isload = (Opc == TargetOpcode::G_LOAD);
500 bool HasAVX = STI.hasAVX();
501 bool HasAVX512 = STI.hasAVX512();
502 bool HasVLX = STI.hasVLX();
503
504 if (Ty == LLT::scalar(8)) {
505 if (X86::GPRRegBankID == RB.getID())
506 return Isload ? X86::MOV8rm : X86::MOV8mr;
507 } else if (Ty == LLT::scalar(16)) {
508 if (X86::GPRRegBankID == RB.getID())
509 return Isload ? X86::MOV16rm : X86::MOV16mr;
510 } else if (Ty == LLT::scalar(32)) {
511 if (X86::GPRRegBankID == RB.getID())
512 return Isload ? X86::MOV32rm : X86::MOV32mr;
513 if (X86::VECRRegBankID == RB.getID())
514 return Isload ? (HasAVX512 ? X86::VMOVSSZrm_alt :
515 HasAVX ? X86::VMOVSSrm_alt :
516 X86::MOVSSrm_alt)
517 : (HasAVX512 ? X86::VMOVSSZmr :
518 HasAVX ? X86::VMOVSSmr :
519 X86::MOVSSmr);
520 if (X86::PSRRegBankID == RB.getID())
521 return Isload ? X86::LD_Fp32m : X86::ST_Fp32m;
522 } else if (Ty == LLT::scalar(64)) {
523 if (X86::GPRRegBankID == RB.getID())
524 return Isload ? X86::MOV64rm : X86::MOV64mr;
525 if (X86::VECRRegBankID == RB.getID())
526 return Isload ? (HasAVX512 ? X86::VMOVSDZrm_alt :
527 HasAVX ? X86::VMOVSDrm_alt :
528 X86::MOVSDrm_alt)
529 : (HasAVX512 ? X86::VMOVSDZmr :
530 HasAVX ? X86::VMOVSDmr :
531 X86::MOVSDmr);
532 if (X86::PSRRegBankID == RB.getID())
533 return Isload ? X86::LD_Fp64m : X86::ST_Fp64m;
534 } else if (Ty == LLT::scalar(80)) {
535 return Isload ? X86::LD_Fp80m : X86::ST_FpP80m;
536 } else if (Ty.isVector() && Ty.getSizeInBits() == 128) {
537 if (Alignment >= Align(16))
538 return Isload ? (HasVLX ? X86::VMOVAPSZ128rm
539 : HasAVX512
540 ? X86::VMOVAPSZ128rm_NOVLX
541 : HasAVX ? X86::VMOVAPSrm : X86::MOVAPSrm)
542 : (HasVLX ? X86::VMOVAPSZ128mr
543 : HasAVX512
544 ? X86::VMOVAPSZ128mr_NOVLX
545 : HasAVX ? X86::VMOVAPSmr : X86::MOVAPSmr);
546 else
547 return Isload ? (HasVLX ? X86::VMOVUPSZ128rm
548 : HasAVX512
549 ? X86::VMOVUPSZ128rm_NOVLX
550 : HasAVX ? X86::VMOVUPSrm : X86::MOVUPSrm)
551 : (HasVLX ? X86::VMOVUPSZ128mr
552 : HasAVX512
553 ? X86::VMOVUPSZ128mr_NOVLX
554 : HasAVX ? X86::VMOVUPSmr : X86::MOVUPSmr);
555 } else if (Ty.isVector() && Ty.getSizeInBits() == 256) {
556 if (Alignment >= Align(32))
557 return Isload ? (HasVLX ? X86::VMOVAPSZ256rm
558 : HasAVX512 ? X86::VMOVAPSZ256rm_NOVLX
559 : X86::VMOVAPSYrm)
560 : (HasVLX ? X86::VMOVAPSZ256mr
561 : HasAVX512 ? X86::VMOVAPSZ256mr_NOVLX
562 : X86::VMOVAPSYmr);
563 else
564 return Isload ? (HasVLX ? X86::VMOVUPSZ256rm
565 : HasAVX512 ? X86::VMOVUPSZ256rm_NOVLX
566 : X86::VMOVUPSYrm)
567 : (HasVLX ? X86::VMOVUPSZ256mr
568 : HasAVX512 ? X86::VMOVUPSZ256mr_NOVLX
569 : X86::VMOVUPSYmr);
570 } else if (Ty.isVector() && Ty.getSizeInBits() == 512) {
571 if (Alignment >= Align(64))
572 return Isload ? X86::VMOVAPSZrm : X86::VMOVAPSZmr;
573 else
574 return Isload ? X86::VMOVUPSZrm : X86::VMOVUPSZmr;
575 }
576 return Opc;
577}
578
579// Fill in an address from the given instruction.
582 const X86Subtarget &STI, X86AddressMode &AM) {
583 assert(I.getOperand(0).isReg() && "unsupported operand.");
584 assert(MRI.getType(I.getOperand(0).getReg()).isPointer() &&
585 "unsupported type.");
586
587 switch (I.getOpcode()) {
588 default:
589 break;
590 case TargetOpcode::G_FRAME_INDEX:
591 AM.Base.FrameIndex = I.getOperand(1).getIndex();
593 return true;
594 case TargetOpcode::G_PTR_ADD: {
595 if (auto COff = getIConstantVRegSExtVal(I.getOperand(2).getReg(), MRI)) {
596 int64_t Imm = *COff;
597 if (isInt<32>(Imm)) { // Check for displacement overflow.
598 AM.Disp = static_cast<int32_t>(Imm);
599 AM.Base.Reg = I.getOperand(1).getReg();
600 return true;
601 }
602 }
603 break;
604 }
605 case TargetOpcode::G_GLOBAL_VALUE:
606 case X86::G_WRAPPER_RIP: {
607 auto GV = I.getOperand(1).getGlobal();
608 if (GV->isThreadLocal()) {
609 return false; // TODO: we don't support TLS yet.
610 }
611 // Can't handle alternate code models yet.
612 if (TM.getCodeModel() != CodeModel::Small)
613 return false;
614 AM.GV = GV;
616
617 // TODO: This reference is relative to the pic base. not supported yet.
619 return false;
620
621 if (STI.isPICStyleRIPRel() || AM.GVOpFlags == X86II::MO_GOTPCREL ||
623 // Use rip-relative addressing.
624 assert(AM.Base.Reg == 0 && AM.IndexReg == 0 &&
625 "RIP-relative addresses can't have additional register operands");
626 AM.Base.Reg = X86::RIP;
627 }
628 return true;
629 }
630 case TargetOpcode::G_CONSTANT_POOL: {
631 // TODO: Need a separate move for Large model
632 if (TM.getCodeModel() == CodeModel::Large)
633 return false;
634
635 AM.GVOpFlags = STI.classifyLocalReference(nullptr);
636 if (AM.GVOpFlags == X86II::MO_GOTOFF)
637 AM.Base.Reg = STI.getInstrInfo()->getGlobalBaseReg(I.getMF());
638 else if (STI.is64Bit())
639 AM.Base.Reg = X86::RIP;
640 AM.CP = true;
641 AM.Disp = I.getOperand(1).getIndex();
642 return true;
643 }
644 }
645 // Default behavior.
646 AM.Base.Reg = I.getOperand(0).getReg();
647 return true;
648}
649
650bool X86InstructionSelector::selectLoadStoreOp(MachineInstr &I,
651 MachineRegisterInfo &MRI,
652 MachineFunction &MF) const {
653 unsigned Opc = I.getOpcode();
654
655 assert((Opc == TargetOpcode::G_STORE || Opc == TargetOpcode::G_LOAD) &&
656 "Only G_STORE and G_LOAD are expected for selection");
657
658 const Register DefReg = I.getOperand(0).getReg();
659 LLT Ty = MRI.getType(DefReg);
660 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
661
662 assert(I.hasOneMemOperand());
663 auto &MemOp = **I.memoperands_begin();
664 if (MemOp.isAtomic()) {
665 // Note: for unordered operations, we rely on the fact the appropriate MMO
666 // is already on the instruction we're mutating, and thus we don't need to
667 // make any changes. So long as we select an opcode which is capable of
668 // loading or storing the appropriate size atomically, the rest of the
669 // backend is required to respect the MMO state.
670 if (!MemOp.isUnordered()) {
671 LLVM_DEBUG(dbgs() << "Atomic ordering not supported yet\n");
672 return false;
673 }
674 if (MemOp.getAlign() < Ty.getSizeInBits() / 8) {
675 LLVM_DEBUG(dbgs() << "Unaligned atomics not supported yet\n");
676 return false;
677 }
678 }
679
680 unsigned NewOpc = getPtrLoadStoreOp(Ty, RB, Opc);
681 if (NewOpc == Opc)
682 return false;
683
684 I.setDesc(TII.get(NewOpc));
685 MachineInstrBuilder MIB(MF, I);
686 MachineInstr *Ptr = MRI.getVRegDef(I.getOperand(1).getReg());
687
688 X86AddressMode AM;
689 if (!X86SelectAddress(*Ptr, TM, MRI, STI, AM))
690 return false;
691
692 if (Opc == TargetOpcode::G_LOAD) {
693 I.removeOperand(1);
694 addFullAddress(MIB, AM);
695 } else {
696 // G_STORE (VAL, Addr), X86Store instruction (Addr, VAL)
697 I.removeOperand(1);
698 I.removeOperand(0);
699 addFullAddress(MIB, AM).addUse(DefReg);
700 }
702 I.addImplicitDefUseOperands(MF);
703 return true;
704}
705
706static unsigned getLeaOP(LLT Ty, const X86Subtarget &STI) {
707 if (Ty == LLT::pointer(0, 64))
708 return X86::LEA64r;
709 else if (Ty == LLT::pointer(0, 32))
710 return STI.isTarget64BitILP32() ? X86::LEA64_32r : X86::LEA32r;
711 else
712 llvm_unreachable("Can't get LEA opcode. Unsupported type.");
713}
714
715bool X86InstructionSelector::selectFrameIndexOrGep(MachineInstr &I,
716 MachineRegisterInfo &MRI,
717 MachineFunction &MF) const {
718 unsigned Opc = I.getOpcode();
719
720 assert((Opc == TargetOpcode::G_FRAME_INDEX || Opc == TargetOpcode::G_PTR_ADD) &&
721 "unexpected instruction");
722
723 const Register DefReg = I.getOperand(0).getReg();
724 LLT Ty = MRI.getType(DefReg);
725
726 // Use LEA to calculate frame index and GEP
727 unsigned NewOpc = getLeaOP(Ty, STI);
728 I.setDesc(TII.get(NewOpc));
729 MachineInstrBuilder MIB(MF, I);
730
731 if (Opc == TargetOpcode::G_FRAME_INDEX) {
732 addOffset(MIB, 0);
733 } else {
734 MachineOperand &InxOp = I.getOperand(2);
735 I.addOperand(InxOp); // set IndexReg
736 InxOp.ChangeToImmediate(1); // set Scale
737 MIB.addImm(0).addReg(0);
738 }
739
741 return true;
742}
743
744bool X86InstructionSelector::selectGlobalValue(MachineInstr &I,
745 MachineRegisterInfo &MRI,
746 MachineFunction &MF) const {
747 assert((I.getOpcode() == TargetOpcode::G_GLOBAL_VALUE) &&
748 "unexpected instruction");
749
750 X86AddressMode AM;
751 if (!X86SelectAddress(I, TM, MRI, STI, AM))
752 return false;
753
754 const Register DefReg = I.getOperand(0).getReg();
755 LLT Ty = MRI.getType(DefReg);
756 unsigned NewOpc = getLeaOP(Ty, STI);
757
758 I.setDesc(TII.get(NewOpc));
759 MachineInstrBuilder MIB(MF, I);
760
761 I.removeOperand(1);
762 addFullAddress(MIB, AM);
763
765 return true;
766}
767
768bool X86InstructionSelector::selectConstant(MachineInstr &I,
769 MachineRegisterInfo &MRI,
770 MachineFunction &MF) const {
771 assert((I.getOpcode() == TargetOpcode::G_CONSTANT) &&
772 "unexpected instruction");
773
774 const Register DefReg = I.getOperand(0).getReg();
775 LLT Ty = MRI.getType(DefReg);
776
777 if (RBI.getRegBank(DefReg, MRI, TRI)->getID() != X86::GPRRegBankID)
778 return false;
779
780 uint64_t Val = 0;
781 if (I.getOperand(1).isCImm()) {
782 Val = I.getOperand(1).getCImm()->getZExtValue();
783 I.getOperand(1).ChangeToImmediate(Val);
784 } else if (I.getOperand(1).isImm()) {
785 Val = I.getOperand(1).getImm();
786 } else
787 llvm_unreachable("Unsupported operand type.");
788
789 unsigned NewOpc;
790 switch (Ty.getSizeInBits()) {
791 case 8:
792 NewOpc = X86::MOV8ri;
793 break;
794 case 16:
795 NewOpc = X86::MOV16ri;
796 break;
797 case 32:
798 NewOpc = X86::MOV32ri;
799 break;
800 case 64:
801 NewOpc = X86::getMOVriOpcode(/*Use64BitReg=*/true, Val);
802 break;
803 default:
804 llvm_unreachable("Can't select G_CONSTANT, unsupported type.");
805 }
806
807 I.setDesc(TII.get(NewOpc));
809 return true;
810}
811
812// Helper function for selectTruncOrPtrToInt and selectAnyext.
813// Returns true if DstRC lives on a floating register class and
814// SrcRC lives on a 128-bit vector class.
815static bool canTurnIntoCOPY(const TargetRegisterClass *DstRC,
816 const TargetRegisterClass *SrcRC) {
817 return (DstRC == &X86::FR32RegClass || DstRC == &X86::FR32XRegClass ||
818 DstRC == &X86::FR64RegClass || DstRC == &X86::FR64XRegClass) &&
819 (SrcRC == &X86::VR128RegClass || SrcRC == &X86::VR128XRegClass);
820}
821
822bool X86InstructionSelector::selectTurnIntoCOPY(
823 MachineInstr &I, MachineRegisterInfo &MRI, const Register DstReg,
824 const TargetRegisterClass *DstRC, const Register SrcReg,
825 const TargetRegisterClass *SrcRC) const {
826
827 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
828 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
829 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
830 << " operand\n");
831 return false;
832 }
833 I.setDesc(TII.get(X86::COPY));
834 return true;
835}
836
837bool X86InstructionSelector::selectTruncOrPtrToInt(MachineInstr &I,
838 MachineRegisterInfo &MRI,
839 MachineFunction &MF) const {
840 assert((I.getOpcode() == TargetOpcode::G_TRUNC ||
841 I.getOpcode() == TargetOpcode::G_PTRTOINT) &&
842 "unexpected instruction");
843
844 const Register DstReg = I.getOperand(0).getReg();
845 const Register SrcReg = I.getOperand(1).getReg();
846
847 const LLT DstTy = MRI.getType(DstReg);
848 const LLT SrcTy = MRI.getType(SrcReg);
849
850 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
851 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
852
853 if (DstRB.getID() != SrcRB.getID()) {
854 LLVM_DEBUG(dbgs() << TII.getName(I.getOpcode())
855 << " input/output on different banks\n");
856 return false;
857 }
858
859 const TargetRegisterClass *DstRC = getRegClass(DstTy, DstRB);
860 const TargetRegisterClass *SrcRC = getRegClass(SrcTy, SrcRB);
861
862 if (!DstRC || !SrcRC)
863 return false;
864
865 // If that's truncation of the value that lives on the vector class and goes
866 // into the floating class, just replace it with copy, as we are able to
867 // select it as a regular move.
868 if (canTurnIntoCOPY(DstRC, SrcRC))
869 return selectTurnIntoCOPY(I, MRI, DstReg, DstRC, SrcReg, SrcRC);
870
871 if (DstRB.getID() != X86::GPRRegBankID)
872 return false;
873
874 unsigned SubIdx;
875 if (DstRC == SrcRC) {
876 // Nothing to be done
877 SubIdx = X86::NoSubRegister;
878 } else if (DstRC == &X86::GR32RegClass) {
879 SubIdx = X86::sub_32bit;
880 } else if (DstRC == &X86::GR16RegClass) {
881 SubIdx = X86::sub_16bit;
882 } else if (DstRC == &X86::GR8RegClass) {
883 SubIdx = X86::sub_8bit;
884 } else {
885 return false;
886 }
887
888 SrcRC = TRI.getSubClassWithSubReg(SrcRC, SubIdx);
889
890 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
891 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
892 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
893 << "\n");
894 return false;
895 }
896
897 I.getOperand(1).setSubReg(SubIdx);
898
899 I.setDesc(TII.get(X86::COPY));
900 return true;
901}
902
903bool X86InstructionSelector::selectZext(MachineInstr &I,
904 MachineRegisterInfo &MRI,
905 MachineFunction &MF) const {
906 assert((I.getOpcode() == TargetOpcode::G_ZEXT) && "unexpected instruction");
907
908 const Register DstReg = I.getOperand(0).getReg();
909 const Register SrcReg = I.getOperand(1).getReg();
910
911 const LLT DstTy = MRI.getType(DstReg);
912 const LLT SrcTy = MRI.getType(SrcReg);
913
914 assert(!(SrcTy == LLT::scalar(8) && DstTy == LLT::scalar(16)) &&
915 "8=>16 Zext is handled by tablegen");
916 assert(!(SrcTy == LLT::scalar(8) && DstTy == LLT::scalar(32)) &&
917 "8=>32 Zext is handled by tablegen");
918 assert(!(SrcTy == LLT::scalar(16) && DstTy == LLT::scalar(32)) &&
919 "16=>32 Zext is handled by tablegen");
920 assert(!(SrcTy == LLT::scalar(8) && DstTy == LLT::scalar(64)) &&
921 "8=>64 Zext is handled by tablegen");
922 assert(!(SrcTy == LLT::scalar(16) && DstTy == LLT::scalar(64)) &&
923 "16=>64 Zext is handled by tablegen");
924 assert(!(SrcTy == LLT::scalar(32) && DstTy == LLT::scalar(64)) &&
925 "32=>64 Zext is handled by tablegen");
926
927 if (SrcTy != LLT::scalar(1))
928 return false;
929
930 unsigned AndOpc;
931 if (DstTy == LLT::scalar(8))
932 AndOpc = X86::AND8ri;
933 else if (DstTy == LLT::scalar(16))
934 AndOpc = X86::AND16ri;
935 else if (DstTy == LLT::scalar(32))
936 AndOpc = X86::AND32ri;
937 else if (DstTy == LLT::scalar(64))
938 AndOpc = X86::AND64ri32;
939 else
940 return false;
941
942 Register DefReg = SrcReg;
943 if (DstTy != LLT::scalar(8)) {
944 // In 32-bit mode only some registers have 8-bit subregisters.
945 const TargetRegisterClass *ExtRC = TRI.getSubClassWithSubReg(
946 getRegClass(DstTy, DstReg, MRI), X86::sub_8bit);
947 Register ImpDefReg = MRI.createVirtualRegister(ExtRC);
948 BuildMI(*I.getParent(), I, I.getDebugLoc(),
949 TII.get(TargetOpcode::IMPLICIT_DEF), ImpDefReg);
950
951 DefReg = MRI.createVirtualRegister(ExtRC);
952 BuildMI(*I.getParent(), I, I.getDebugLoc(),
953 TII.get(TargetOpcode::INSERT_SUBREG), DefReg)
954 .addReg(ImpDefReg)
955 .addReg(SrcReg)
956 .addImm(X86::sub_8bit);
957 }
958
959 MachineInstr &AndInst =
960 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AndOpc), DstReg)
961 .addReg(DefReg)
962 .addImm(1);
963
965
966 I.eraseFromParent();
967 return true;
968}
969
970bool X86InstructionSelector::selectAnyext(MachineInstr &I,
971 MachineRegisterInfo &MRI,
972 MachineFunction &MF) const {
973 assert((I.getOpcode() == TargetOpcode::G_ANYEXT) && "unexpected instruction");
974
975 const Register DstReg = I.getOperand(0).getReg();
976 const Register SrcReg = I.getOperand(1).getReg();
977
978 const LLT DstTy = MRI.getType(DstReg);
979 const LLT SrcTy = MRI.getType(SrcReg);
980
981 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
982 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
983
984 assert(DstRB.getID() == SrcRB.getID() &&
985 "G_ANYEXT input/output on different banks\n");
986
987 assert(DstTy.getSizeInBits() > SrcTy.getSizeInBits() &&
988 "G_ANYEXT incorrect operand size");
989
990 const TargetRegisterClass *DstRC = getRegClass(DstTy, DstRB);
991 const TargetRegisterClass *SrcRC = getRegClass(SrcTy, SrcRB);
992
993 // If that's ANY_EXT of the value that lives on the floating class and goes
994 // into the vector class, just replace it with copy, as we are able to select
995 // it as a regular move.
996 if (canTurnIntoCOPY(SrcRC, DstRC))
997 return selectTurnIntoCOPY(I, MRI, SrcReg, SrcRC, DstReg, DstRC);
998
999 if (DstRB.getID() != X86::GPRRegBankID)
1000 return false;
1001
1002 unsigned SubIdx = getSubRegIndex(SrcRC);
1003 // In 32-bit mode only some registers have 8-bit subregisters.
1004 if (SrcRC != DstRC)
1005 DstRC = TRI.getSubClassWithSubReg(DstRC, SubIdx);
1006
1007 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1008 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
1009 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
1010 << " operand\n");
1011 return false;
1012 }
1013
1014 if (SrcRC == DstRC) {
1015 I.setDesc(TII.get(X86::COPY));
1016 return true;
1017 }
1018
1019 Register ImpDef = MRI.createVirtualRegister(DstRC);
1020 BuildMI(*I.getParent(), I, I.getDebugLoc(),
1021 TII.get(TargetOpcode::IMPLICIT_DEF), ImpDef);
1022 BuildMI(*I.getParent(), I, I.getDebugLoc(),
1023 TII.get(TargetOpcode::INSERT_SUBREG), DstReg)
1024 .addReg(ImpDef)
1025 .addReg(SrcReg)
1026 .addImm(SubIdx);
1027
1028 I.eraseFromParent();
1029 return true;
1030}
1031
1032bool X86InstructionSelector::selectCmp(MachineInstr &I,
1033 MachineRegisterInfo &MRI,
1034 MachineFunction &MF) const {
1035 assert((I.getOpcode() == TargetOpcode::G_ICMP) && "unexpected instruction");
1036
1038 bool SwapArgs;
1039 std::tie(CC, SwapArgs) = X86::getX86ConditionCode(
1040 (CmpInst::Predicate)I.getOperand(1).getPredicate());
1041
1042 Register LHS = I.getOperand(2).getReg();
1043 Register RHS = I.getOperand(3).getReg();
1044
1045 if (SwapArgs)
1046 std::swap(LHS, RHS);
1047
1048 unsigned OpCmp;
1049 LLT Ty = MRI.getType(LHS);
1050
1051 switch (Ty.getSizeInBits()) {
1052 default:
1053 return false;
1054 case 8:
1055 OpCmp = X86::CMP8rr;
1056 break;
1057 case 16:
1058 OpCmp = X86::CMP16rr;
1059 break;
1060 case 32:
1061 OpCmp = X86::CMP32rr;
1062 break;
1063 case 64:
1064 OpCmp = X86::CMP64rr;
1065 break;
1066 }
1067
1068 MachineInstr &CmpInst =
1069 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(OpCmp))
1070 .addReg(LHS)
1071 .addReg(RHS);
1072
1073 MachineInstr &SetInst = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1074 TII.get(X86::SETCCr), I.getOperand(0).getReg()).addImm(CC);
1075
1078
1079 I.eraseFromParent();
1080 return true;
1081}
1082
1083bool X86InstructionSelector::selectFCmp(MachineInstr &I,
1084 MachineRegisterInfo &MRI,
1085 MachineFunction &MF) const {
1086 assert((I.getOpcode() == TargetOpcode::G_FCMP) && "unexpected instruction");
1087
1088 Register LhsReg = I.getOperand(2).getReg();
1089 Register RhsReg = I.getOperand(3).getReg();
1091 (CmpInst::Predicate)I.getOperand(1).getPredicate();
1092
1093 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
1094 static const uint16_t SETFOpcTable[2][3] = {
1095 {X86::COND_E, X86::COND_NP, X86::AND8rr},
1096 {X86::COND_NE, X86::COND_P, X86::OR8rr}};
1097 const uint16_t *SETFOpc = nullptr;
1098 switch (Predicate) {
1099 default:
1100 break;
1101 case CmpInst::FCMP_OEQ:
1102 SETFOpc = &SETFOpcTable[0][0];
1103 break;
1104 case CmpInst::FCMP_UNE:
1105 SETFOpc = &SETFOpcTable[1][0];
1106 break;
1107 }
1108
1109 assert((LhsReg.isVirtual() && RhsReg.isVirtual()) &&
1110 "Both arguments of FCMP need to be virtual!");
1111 auto *LhsBank = RBI.getRegBank(LhsReg, MRI, TRI);
1112 [[maybe_unused]] auto *RhsBank = RBI.getRegBank(RhsReg, MRI, TRI);
1113 assert((LhsBank == RhsBank) &&
1114 "Both banks assigned to FCMP arguments need to be same!");
1115
1116 bool IsX87 = LhsBank->getID() == X86::PSRRegBankID;
1117
1118 // Compute the opcode for the CMP instruction.
1119 unsigned OpCmp;
1120 LLT Ty = MRI.getType(LhsReg);
1121 switch (Ty.getSizeInBits()) {
1122 default:
1123 return false;
1124 case 32:
1125 OpCmp = IsX87 ? X86::UCOM_FpIr32 : X86::UCOMISSrr;
1126 break;
1127 case 64:
1128 OpCmp = IsX87 ? X86::UCOM_FpIr64 : X86::UCOMISDrr;
1129 break;
1130 case 80:
1131 OpCmp = X86::UCOM_FpIr80;
1132 break;
1133 }
1134
1135 Register ResultReg = I.getOperand(0).getReg();
1137 ResultReg,
1138 *getRegClass(LLT::scalar(8), *RBI.getRegBank(ResultReg, MRI, TRI)), MRI);
1139 if (SETFOpc) {
1140 MachineInstr &CmpInst =
1141 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(OpCmp))
1142 .addReg(LhsReg)
1143 .addReg(RhsReg);
1144 if (IsX87)
1145 CmpInst.getOperand(3).setIsDead(); // Mark unused FPSW def dead.
1146
1147 Register FlagReg1 = MRI.createVirtualRegister(&X86::GR8RegClass);
1148 Register FlagReg2 = MRI.createVirtualRegister(&X86::GR8RegClass);
1149 MachineInstr &Set1 = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1150 TII.get(X86::SETCCr), FlagReg1).addImm(SETFOpc[0]);
1151 MachineInstr &Set2 = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1152 TII.get(X86::SETCCr), FlagReg2).addImm(SETFOpc[1]);
1153 MachineInstr &Set3 = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1154 TII.get(SETFOpc[2]), ResultReg)
1155 .addReg(FlagReg1)
1156 .addReg(FlagReg2)
1157 .setOperandDead(3);
1162
1163 I.eraseFromParent();
1164 return true;
1165 }
1166
1168 bool SwapArgs;
1169 std::tie(CC, SwapArgs) = X86::getX86ConditionCode(Predicate);
1170 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
1171
1172 if (SwapArgs)
1173 std::swap(LhsReg, RhsReg);
1174
1175 // Emit a compare of LHS/RHS.
1176 MachineInstr &CmpInst =
1177 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(OpCmp))
1178 .addReg(LhsReg)
1179 .addReg(RhsReg);
1180 if (IsX87)
1181 CmpInst.getOperand(3).setIsDead(); // Mark unused FPSW def dead.
1182
1183 MachineInstr &Set =
1184 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(X86::SETCCr), ResultReg).addImm(CC);
1187 I.eraseFromParent();
1188 return true;
1189}
1190
1191bool X86InstructionSelector::selectUAddSub(MachineInstr &I,
1192 MachineRegisterInfo &MRI,
1193 MachineFunction &MF) const {
1194 assert((I.getOpcode() == TargetOpcode::G_UADDE ||
1195 I.getOpcode() == TargetOpcode::G_UADDO ||
1196 I.getOpcode() == TargetOpcode::G_USUBE ||
1197 I.getOpcode() == TargetOpcode::G_USUBO) &&
1198 "unexpected instruction");
1199
1200 auto &CarryMI = cast<GAddSubCarryOut>(I);
1201
1202 const Register DstReg = CarryMI.getDstReg();
1203 const Register CarryOutReg = CarryMI.getCarryOutReg();
1204 const Register Op0Reg = CarryMI.getLHSReg();
1205 const Register Op1Reg = CarryMI.getRHSReg();
1206 bool IsSub = CarryMI.isSub();
1207
1208 const LLT DstTy = MRI.getType(DstReg);
1209 assert(DstTy.isScalar() && "selectUAddSub only supported for scalar types");
1210
1211 // TODO: Handle immediate argument variants?
1212 unsigned OpADC, OpADD, OpSBB, OpSUB;
1213 switch (DstTy.getSizeInBits()) {
1214 case 8:
1215 OpADC = X86::ADC8rr;
1216 OpADD = X86::ADD8rr;
1217 OpSBB = X86::SBB8rr;
1218 OpSUB = X86::SUB8rr;
1219 break;
1220 case 16:
1221 OpADC = X86::ADC16rr;
1222 OpADD = X86::ADD16rr;
1223 OpSBB = X86::SBB16rr;
1224 OpSUB = X86::SUB16rr;
1225 break;
1226 case 32:
1227 OpADC = X86::ADC32rr;
1228 OpADD = X86::ADD32rr;
1229 OpSBB = X86::SBB32rr;
1230 OpSUB = X86::SUB32rr;
1231 break;
1232 case 64:
1233 OpADC = X86::ADC64rr;
1234 OpADD = X86::ADD64rr;
1235 OpSBB = X86::SBB64rr;
1236 OpSUB = X86::SUB64rr;
1237 break;
1238 default:
1239 llvm_unreachable("selectUAddSub unsupported type.");
1240 }
1241
1242 const RegisterBank &CarryRB = *RBI.getRegBank(CarryOutReg, MRI, TRI);
1243 const TargetRegisterClass *CarryRC =
1244 getRegClass(MRI.getType(CarryOutReg), CarryRB);
1245
1246 unsigned Opcode = IsSub ? OpSUB : OpADD;
1247
1248 // G_UADDE/G_USUBE - find CarryIn def instruction.
1249 if (auto CarryInMI = dyn_cast<GAddSubCarryInOut>(&I)) {
1250 Register CarryInReg = CarryInMI->getCarryInReg();
1251 MachineInstr *Def = MRI.getVRegDef(CarryInReg);
1252 while (Def->getOpcode() == TargetOpcode::G_TRUNC) {
1253 CarryInReg = Def->getOperand(1).getReg();
1254 Def = MRI.getVRegDef(CarryInReg);
1255 }
1256
1257 // TODO - handle more CF generating instructions
1258 if (Def->getOpcode() == TargetOpcode::G_UADDE ||
1259 Def->getOpcode() == TargetOpcode::G_UADDO ||
1260 Def->getOpcode() == TargetOpcode::G_USUBE ||
1261 Def->getOpcode() == TargetOpcode::G_USUBO) {
1262 // The carry-in is a SETB byte (0 or 1) from a chained add/sub.
1263 // Materialize EFLAGS.CF from that byte for the following ADC/SBB
1264 // by emitting NEG, which sets CF iff its operand is non-zero.
1265 if (!RBI.constrainGenericRegister(CarryInReg, *CarryRC, MRI))
1266 return false;
1267
1268 Register NegDef = MRI.createVirtualRegister(CarryRC);
1269 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(X86::NEG8r), NegDef)
1270 .addReg(CarryInReg);
1271
1272 Opcode = IsSub ? OpSBB : OpADC;
1273 } else if (auto val = getIConstantVRegVal(CarryInReg, MRI)) {
1274 // carry is constant, support only 0.
1275 if (*val != 0)
1276 return false;
1277
1278 Opcode = IsSub ? OpSUB : OpADD;
1279 } else
1280 return false;
1281 }
1282
1283 MachineInstr &Inst =
1284 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Opcode), DstReg)
1285 .addReg(Op0Reg)
1286 .addReg(Op1Reg);
1287
1288 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(X86::SETCCr), CarryOutReg)
1290
1292 if (!RBI.constrainGenericRegister(CarryOutReg, *CarryRC, MRI))
1293 return false;
1294
1295 I.eraseFromParent();
1296 return true;
1297}
1298
1299bool X86InstructionSelector::selectExtract(MachineInstr &I,
1300 MachineRegisterInfo &MRI,
1301 MachineFunction &MF) const {
1302 assert((I.getOpcode() == TargetOpcode::G_EXTRACT) &&
1303 "unexpected instruction");
1304
1305 const Register DstReg = I.getOperand(0).getReg();
1306 const Register SrcReg = I.getOperand(1).getReg();
1307 int64_t Index = I.getOperand(2).getImm();
1308
1309 const LLT DstTy = MRI.getType(DstReg);
1310 const LLT SrcTy = MRI.getType(SrcReg);
1311
1312 // Meanwile handle vector type only.
1313 if (!DstTy.isVector())
1314 return false;
1315
1316 if (Index % DstTy.getSizeInBits() != 0)
1317 return false; // Not extract subvector.
1318
1319 if (Index == 0) {
1320 // Replace by extract subreg copy.
1321 if (!emitExtractSubreg(DstReg, SrcReg, I, MRI, MF))
1322 return false;
1323
1324 I.eraseFromParent();
1325 return true;
1326 }
1327
1328 bool HasAVX = STI.hasAVX();
1329 bool HasAVX512 = STI.hasAVX512();
1330 bool HasVLX = STI.hasVLX();
1331
1332 if (SrcTy.getSizeInBits() == 256 && DstTy.getSizeInBits() == 128) {
1333 if (HasVLX)
1334 I.setDesc(TII.get(X86::VEXTRACTF32X4Z256rri));
1335 else if (HasAVX)
1336 I.setDesc(TII.get(X86::VEXTRACTF128rri));
1337 else
1338 return false;
1339 } else if (SrcTy.getSizeInBits() == 512 && HasAVX512) {
1340 if (DstTy.getSizeInBits() == 128)
1341 I.setDesc(TII.get(X86::VEXTRACTF32X4Zrri));
1342 else if (DstTy.getSizeInBits() == 256)
1343 I.setDesc(TII.get(X86::VEXTRACTF64X4Zrri));
1344 else
1345 return false;
1346 } else
1347 return false;
1348
1349 // Convert to X86 VEXTRACT immediate.
1350 Index = Index / DstTy.getSizeInBits();
1351 I.getOperand(2).setImm(Index);
1352
1354 return true;
1355}
1356
1357bool X86InstructionSelector::emitExtractSubreg(Register DstReg, Register SrcReg,
1358 MachineInstr &I,
1359 MachineRegisterInfo &MRI,
1360 MachineFunction &MF) const {
1361 const LLT DstTy = MRI.getType(DstReg);
1362 const LLT SrcTy = MRI.getType(SrcReg);
1363 unsigned SubIdx = X86::NoSubRegister;
1364
1365 if (!DstTy.isVector() || !SrcTy.isVector())
1366 return false;
1367
1368 assert(SrcTy.getSizeInBits() > DstTy.getSizeInBits() &&
1369 "Incorrect Src/Dst register size");
1370
1371 if (DstTy.getSizeInBits() == 128)
1372 SubIdx = X86::sub_xmm;
1373 else if (DstTy.getSizeInBits() == 256)
1374 SubIdx = X86::sub_ymm;
1375 else
1376 return false;
1377
1378 const TargetRegisterClass *DstRC = getRegClass(DstTy, DstReg, MRI);
1379 const TargetRegisterClass *SrcRC = getRegClass(SrcTy, SrcReg, MRI);
1380
1381 SrcRC = TRI.getSubClassWithSubReg(SrcRC, SubIdx);
1382
1383 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1384 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
1385 LLVM_DEBUG(dbgs() << "Failed to constrain EXTRACT_SUBREG\n");
1386 return false;
1387 }
1388
1389 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(X86::COPY), DstReg)
1390 .addReg(SrcReg, {}, SubIdx);
1391
1392 return true;
1393}
1394
1395bool X86InstructionSelector::emitInsertSubreg(Register DstReg, Register SrcReg,
1396 MachineInstr &I,
1397 MachineRegisterInfo &MRI,
1398 MachineFunction &MF) const {
1399 const LLT DstTy = MRI.getType(DstReg);
1400 const LLT SrcTy = MRI.getType(SrcReg);
1401 unsigned SubIdx = X86::NoSubRegister;
1402
1403 // TODO: support scalar types
1404 if (!DstTy.isVector() || !SrcTy.isVector())
1405 return false;
1406
1407 assert(SrcTy.getSizeInBits() < DstTy.getSizeInBits() &&
1408 "Incorrect Src/Dst register size");
1409
1410 if (SrcTy.getSizeInBits() == 128)
1411 SubIdx = X86::sub_xmm;
1412 else if (SrcTy.getSizeInBits() == 256)
1413 SubIdx = X86::sub_ymm;
1414 else
1415 return false;
1416
1417 const TargetRegisterClass *SrcRC = getRegClass(SrcTy, SrcReg, MRI);
1418 const TargetRegisterClass *DstRC = getRegClass(DstTy, DstReg, MRI);
1419
1420 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1421 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
1422 LLVM_DEBUG(dbgs() << "Failed to constrain INSERT_SUBREG\n");
1423 return false;
1424 }
1425
1426 Register ImpDefReg = MRI.createVirtualRegister(DstRC);
1427 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(X86::IMPLICIT_DEF),
1428 ImpDefReg);
1429
1430 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(X86::INSERT_SUBREG),
1431 DstReg)
1432 .addReg(ImpDefReg)
1433 .addReg(SrcReg)
1434 .addImm(SubIdx);
1435
1436 return true;
1437}
1438
1439bool X86InstructionSelector::selectInsert(MachineInstr &I,
1440 MachineRegisterInfo &MRI,
1441 MachineFunction &MF) const {
1442 assert((I.getOpcode() == TargetOpcode::G_INSERT) && "unexpected instruction");
1443
1444 const Register DstReg = I.getOperand(0).getReg();
1445 const Register SrcReg = I.getOperand(1).getReg();
1446 const Register InsertReg = I.getOperand(2).getReg();
1447 int64_t Index = I.getOperand(3).getImm();
1448
1449 const LLT DstTy = MRI.getType(DstReg);
1450 const LLT InsertRegTy = MRI.getType(InsertReg);
1451
1452 // Meanwile handle vector type only.
1453 if (!DstTy.isVector())
1454 return false;
1455
1456 if (Index % InsertRegTy.getSizeInBits() != 0)
1457 return false; // Not insert subvector.
1458
1459 if (Index == 0 && mi_match(SrcReg, MRI, m_GImplicitDef())) {
1460 // Replace by subreg copy.
1461 if (!emitInsertSubreg(DstReg, InsertReg, I, MRI, MF))
1462 return false;
1463
1464 I.eraseFromParent();
1465 return true;
1466 }
1467
1468 bool HasAVX = STI.hasAVX();
1469 bool HasAVX512 = STI.hasAVX512();
1470 bool HasVLX = STI.hasVLX();
1471
1472 if (DstTy.getSizeInBits() == 256 && InsertRegTy.getSizeInBits() == 128) {
1473 if (HasVLX)
1474 I.setDesc(TII.get(X86::VINSERTF32X4Z256rri));
1475 else if (HasAVX)
1476 I.setDesc(TII.get(X86::VINSERTF128rri));
1477 else
1478 return false;
1479 } else if (DstTy.getSizeInBits() == 512 && HasAVX512) {
1480 if (InsertRegTy.getSizeInBits() == 128)
1481 I.setDesc(TII.get(X86::VINSERTF32X4Zrri));
1482 else if (InsertRegTy.getSizeInBits() == 256)
1483 I.setDesc(TII.get(X86::VINSERTF64X4Zrri));
1484 else
1485 return false;
1486 } else
1487 return false;
1488
1489 // Convert to X86 VINSERT immediate.
1490 Index = Index / InsertRegTy.getSizeInBits();
1491
1492 I.getOperand(3).setImm(Index);
1493
1495 return true;
1496}
1497
1498bool X86InstructionSelector::selectUnmergeValues(
1499 MachineInstr &I, MachineRegisterInfo &MRI, MachineFunction &MF) {
1500 assert((I.getOpcode() == TargetOpcode::G_UNMERGE_VALUES) &&
1501 "unexpected instruction");
1502
1503 // Split to extracts.
1504 unsigned NumDefs = I.getNumOperands() - 1;
1505 Register SrcReg = I.getOperand(NumDefs).getReg();
1506 unsigned DefSize = MRI.getType(I.getOperand(0).getReg()).getSizeInBits();
1507
1508 for (unsigned Idx = 0; Idx < NumDefs; ++Idx) {
1509 MachineInstr &ExtrInst =
1510 *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1511 TII.get(TargetOpcode::G_EXTRACT), I.getOperand(Idx).getReg())
1512 .addReg(SrcReg)
1513 .addImm(Idx * DefSize);
1514
1515 if (!select(ExtrInst))
1516 return false;
1517 }
1518
1519 I.eraseFromParent();
1520 return true;
1521}
1522
1523bool X86InstructionSelector::selectMergeValues(
1524 MachineInstr &I, MachineRegisterInfo &MRI, MachineFunction &MF) {
1525 assert((I.getOpcode() == TargetOpcode::G_MERGE_VALUES ||
1526 I.getOpcode() == TargetOpcode::G_CONCAT_VECTORS) &&
1527 "unexpected instruction");
1528
1529 // Split to inserts.
1530 Register DstReg = I.getOperand(0).getReg();
1531 Register SrcReg0 = I.getOperand(1).getReg();
1532
1533 const LLT DstTy = MRI.getType(DstReg);
1534 const LLT SrcTy = MRI.getType(SrcReg0);
1535 unsigned SrcSize = SrcTy.getSizeInBits();
1536
1537 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
1538
1539 // For the first src use insertSubReg.
1541 MRI.setRegBank(DefReg, RegBank);
1542 if (!emitInsertSubreg(DefReg, I.getOperand(1).getReg(), I, MRI, MF))
1543 return false;
1544
1545 for (unsigned Idx = 2; Idx < I.getNumOperands(); ++Idx) {
1547 MRI.setRegBank(Tmp, RegBank);
1548
1549 MachineInstr &InsertInst = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1550 TII.get(TargetOpcode::G_INSERT), Tmp)
1551 .addReg(DefReg)
1552 .addReg(I.getOperand(Idx).getReg())
1553 .addImm((Idx - 1) * SrcSize);
1554
1555 DefReg = Tmp;
1556
1557 if (!select(InsertInst))
1558 return false;
1559 }
1560
1561 MachineInstr &CopyInst = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1562 TII.get(TargetOpcode::COPY), DstReg)
1563 .addReg(DefReg);
1564
1565 if (!select(CopyInst))
1566 return false;
1567
1568 I.eraseFromParent();
1569 return true;
1570}
1571
1572bool X86InstructionSelector::selectCondBranch(MachineInstr &I,
1573 MachineRegisterInfo &MRI,
1574 MachineFunction &MF) const {
1575 assert((I.getOpcode() == TargetOpcode::G_BRCOND) && "unexpected instruction");
1576
1577 const Register CondReg = I.getOperand(0).getReg();
1578 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
1579
1580 MachineInstr &TestInst =
1581 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(X86::TEST8ri))
1582 .addReg(CondReg)
1583 .addImm(1);
1584 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(X86::JCC_1))
1585 .addMBB(DestMBB).addImm(X86::COND_NE);
1586
1587 constrainSelectedInstRegOperands(TestInst, TII, TRI, RBI);
1588
1589 I.eraseFromParent();
1590 return true;
1591}
1592
1593bool X86InstructionSelector::materializeFP(MachineInstr &I,
1594 MachineRegisterInfo &MRI,
1595 MachineFunction &MF) const {
1596 assert((I.getOpcode() == TargetOpcode::G_FCONSTANT) &&
1597 "unexpected instruction");
1598
1599 // Can't handle alternate code models yet.
1601 if (CM != CodeModel::Small && CM != CodeModel::Large)
1602 return false;
1603
1604 const Register DstReg = I.getOperand(0).getReg();
1605 const LLT DstTy = MRI.getType(DstReg);
1606 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
1607 // Create the load from the constant pool.
1608 const ConstantFP *CFP = I.getOperand(1).getFPImm();
1609 const auto &DL = MF.getDataLayout();
1610 Align Alignment = DL.getPrefTypeAlign(CFP->getType());
1611 const DebugLoc &DbgLoc = I.getDebugLoc();
1612
1613 unsigned Opc =
1614 getLoadStoreOp(DstTy, RegBank, TargetOpcode::G_LOAD, Alignment);
1615
1616 unsigned CPI = MF.getConstantPool()->getConstantPoolIndex(CFP, Alignment);
1617 MachineInstr *LoadInst = nullptr;
1618 unsigned char OpFlag = STI.classifyLocalReference(nullptr);
1619
1620 if (CM == CodeModel::Large && STI.is64Bit()) {
1621 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
1622 // they cannot be folded into immediate fields.
1623
1624 Register AddrReg = MRI.createVirtualRegister(&X86::GR64RegClass);
1625 BuildMI(*I.getParent(), I, DbgLoc, TII.get(X86::MOV64ri), AddrReg)
1626 .addConstantPoolIndex(CPI, 0, OpFlag);
1627
1628 MachineMemOperand *MMO = MF.getMachineMemOperand(
1630 LLT::pointer(0, DL.getPointerSizeInBits()), Alignment);
1631
1632 LoadInst =
1633 addDirectMem(BuildMI(*I.getParent(), I, DbgLoc, TII.get(Opc), DstReg),
1634 AddrReg)
1635 .addMemOperand(MMO);
1636
1637 } else if (CM == CodeModel::Small || !STI.is64Bit()) {
1638 // Handle the case when globals fit in our immediate field.
1639 // This is true for X86-32 always and X86-64 when in -mcmodel=small mode.
1640
1641 // x86-32 PIC requires a PIC base register for constant pools.
1642 unsigned PICBase = 0;
1643 if (OpFlag == X86II::MO_PIC_BASE_OFFSET || OpFlag == X86II::MO_GOTOFF) {
1644 // PICBase can be allocated by TII.getGlobalBaseReg(&MF).
1645 // In DAGISEL the code that initialize it generated by the CGBR pass.
1646 return false; // TODO support the mode.
1647 } else if (STI.is64Bit() && TM.getCodeModel() == CodeModel::Small)
1648 PICBase = X86::RIP;
1649
1650 LoadInst = addConstantPoolReference(
1651 BuildMI(*I.getParent(), I, DbgLoc, TII.get(Opc), DstReg), CPI, PICBase,
1652 OpFlag);
1653 } else
1654 return false;
1655
1656 constrainSelectedInstRegOperands(*LoadInst, TII, TRI, RBI);
1657 I.eraseFromParent();
1658 return true;
1659}
1660
1661bool X86InstructionSelector::selectImplicitDefOrPHI(
1662 MachineInstr &I, MachineRegisterInfo &MRI) const {
1663 assert((I.getOpcode() == TargetOpcode::G_IMPLICIT_DEF ||
1664 I.getOpcode() == TargetOpcode::G_PHI) &&
1665 "unexpected instruction");
1666
1667 Register DstReg = I.getOperand(0).getReg();
1668
1669 if (!MRI.getRegClassOrNull(DstReg)) {
1670 const LLT DstTy = MRI.getType(DstReg);
1671 const TargetRegisterClass *RC = getRegClass(DstTy, DstReg, MRI);
1672
1673 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
1674 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
1675 << " operand\n");
1676 return false;
1677 }
1678 }
1679
1680 if (I.getOpcode() == TargetOpcode::G_IMPLICIT_DEF)
1681 I.setDesc(TII.get(X86::IMPLICIT_DEF));
1682 else
1683 I.setDesc(TII.get(X86::PHI));
1684
1685 return true;
1686}
1687
1688bool X86InstructionSelector::selectMulDivRem(MachineInstr &I,
1689 MachineRegisterInfo &MRI,
1690 MachineFunction &MF) const {
1691 // The implementation of this function is adapted from X86FastISel.
1692 assert((I.getOpcode() == TargetOpcode::G_MUL ||
1693 I.getOpcode() == TargetOpcode::G_SMULH ||
1694 I.getOpcode() == TargetOpcode::G_UMULH ||
1695 I.getOpcode() == TargetOpcode::G_SDIV ||
1696 I.getOpcode() == TargetOpcode::G_SREM ||
1697 I.getOpcode() == TargetOpcode::G_UDIV ||
1698 I.getOpcode() == TargetOpcode::G_UREM) &&
1699 "unexpected instruction");
1700
1701 const Register DstReg = I.getOperand(0).getReg();
1702 const Register Op1Reg = I.getOperand(1).getReg();
1703 const Register Op2Reg = I.getOperand(2).getReg();
1704
1705 const LLT RegTy = MRI.getType(DstReg);
1706 assert(RegTy == MRI.getType(Op1Reg) && RegTy == MRI.getType(Op2Reg) &&
1707 "Arguments and return value types must match");
1708
1709 const RegisterBank *RegRB = RBI.getRegBank(DstReg, MRI, TRI);
1710 if (!RegRB || RegRB->getID() != X86::GPRRegBankID)
1711 return false;
1712
1713 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1714 const static unsigned NumOps = 7; // SDiv/SRem/UDiv/URem/Mul/SMulH/UMulh
1715 const static bool S = true; // IsSigned
1716 const static bool U = false; // !IsSigned
1717 const static unsigned Copy = TargetOpcode::COPY;
1718
1719 // For the X86 IDIV instruction, in most cases the dividend
1720 // (numerator) must be in a specific register pair highreg:lowreg,
1721 // producing the quotient in lowreg and the remainder in highreg.
1722 // For most data types, to set up the instruction, the dividend is
1723 // copied into lowreg, and lowreg is sign-extended into highreg. The
1724 // exception is i8, where the dividend is defined as a single register rather
1725 // than a register pair, and we therefore directly sign-extend the dividend
1726 // into lowreg, instead of copying, and ignore the highreg.
1727 const static struct MulDivRemEntry {
1728 // The following portion depends only on the data type.
1729 unsigned SizeInBits;
1730 unsigned LowInReg; // low part of the register pair
1731 unsigned HighInReg; // high part of the register pair
1732 // The following portion depends on both the data type and the operation.
1733 struct MulDivRemResult {
1734 unsigned OpMulDivRem; // The specific MUL/DIV opcode to use.
1735 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1736 // highreg, or copying a zero into highreg.
1737 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1738 // zero/sign-extending into lowreg for i8.
1739 unsigned ResultReg; // Register containing the desired result.
1740 bool IsOpSigned; // Whether to use signed or unsigned form.
1741 } ResultTable[NumOps];
1742 } OpTable[NumTypes] = {
1743 {8,
1744 X86::AX,
1745 0,
1746 {
1747 {X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S}, // SDiv
1748 {X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S}, // SRem
1749 {X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U}, // UDiv
1750 {X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U}, // URem
1751 {X86::IMUL8r, 0, X86::MOVSX16rr8, X86::AL, S}, // Mul
1752 {X86::IMUL8r, 0, X86::MOVSX16rr8, X86::AH, S}, // SMulH
1753 {X86::MUL8r, 0, X86::MOVZX16rr8, X86::AH, U}, // UMulH
1754 }}, // i8
1755 {16,
1756 X86::AX,
1757 X86::DX,
1758 {
1759 {X86::IDIV16r, X86::CWD, Copy, X86::AX, S}, // SDiv
1760 {X86::IDIV16r, X86::CWD, Copy, X86::DX, S}, // SRem
1761 {X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U}, // UDiv
1762 {X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U}, // URem
1763 {X86::IMUL16r, X86::MOV32r0, Copy, X86::AX, S}, // Mul
1764 {X86::IMUL16r, X86::MOV32r0, Copy, X86::DX, S}, // SMulH
1765 {X86::MUL16r, X86::MOV32r0, Copy, X86::DX, U}, // UMulH
1766 }}, // i16
1767 {32,
1768 X86::EAX,
1769 X86::EDX,
1770 {
1771 {X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S}, // SDiv
1772 {X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S}, // SRem
1773 {X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U}, // UDiv
1774 {X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U}, // URem
1775 {X86::IMUL32r, X86::MOV32r0, Copy, X86::EAX, S}, // Mul
1776 {X86::IMUL32r, X86::MOV32r0, Copy, X86::EDX, S}, // SMulH
1777 {X86::MUL32r, X86::MOV32r0, Copy, X86::EDX, U}, // UMulH
1778 }}, // i32
1779 {64,
1780 X86::RAX,
1781 X86::RDX,
1782 {
1783 {X86::IDIV64r, X86::CQO, Copy, X86::RAX, S}, // SDiv
1784 {X86::IDIV64r, X86::CQO, Copy, X86::RDX, S}, // SRem
1785 {X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U}, // UDiv
1786 {X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U}, // URem
1787 {X86::IMUL64r, X86::MOV32r0, Copy, X86::RAX, S}, // Mul
1788 {X86::IMUL64r, X86::MOV32r0, Copy, X86::RDX, S}, // SMulH
1789 {X86::MUL64r, X86::MOV32r0, Copy, X86::RDX, U}, // UMulH
1790 }}, // i64
1791 };
1792
1793 auto OpEntryIt = llvm::find_if(OpTable, [RegTy](const MulDivRemEntry &El) {
1794 return El.SizeInBits == RegTy.getSizeInBits();
1795 });
1796 if (OpEntryIt == std::end(OpTable))
1797 return false;
1798
1799 unsigned OpIndex;
1800 switch (I.getOpcode()) {
1801 default:
1802 llvm_unreachable("Unexpected mul/div/rem opcode");
1803 case TargetOpcode::G_SDIV:
1804 OpIndex = 0;
1805 break;
1806 case TargetOpcode::G_SREM:
1807 OpIndex = 1;
1808 break;
1809 case TargetOpcode::G_UDIV:
1810 OpIndex = 2;
1811 break;
1812 case TargetOpcode::G_UREM:
1813 OpIndex = 3;
1814 break;
1815 case TargetOpcode::G_MUL:
1816 OpIndex = 4;
1817 break;
1818 case TargetOpcode::G_SMULH:
1819 OpIndex = 5;
1820 break;
1821 case TargetOpcode::G_UMULH:
1822 OpIndex = 6;
1823 break;
1824 }
1825
1826 const MulDivRemEntry &TypeEntry = *OpEntryIt;
1827 const MulDivRemEntry::MulDivRemResult &OpEntry =
1828 TypeEntry.ResultTable[OpIndex];
1829
1830 const TargetRegisterClass *RegRC = getRegClass(RegTy, *RegRB);
1831 if (!RBI.constrainGenericRegister(Op1Reg, *RegRC, MRI) ||
1832 !RBI.constrainGenericRegister(Op2Reg, *RegRC, MRI) ||
1833 !RBI.constrainGenericRegister(DstReg, *RegRC, MRI)) {
1834 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
1835 << " operand\n");
1836 return false;
1837 }
1838
1839 // Move op1 into low-order input register.
1840 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(OpEntry.OpCopy),
1841 TypeEntry.LowInReg)
1842 .addReg(Op1Reg);
1843
1844 // Zero-extend or sign-extend into high-order input register.
1845 if (OpEntry.OpSignExtend) {
1846 if (OpEntry.IsOpSigned)
1847 BuildMI(*I.getParent(), I, I.getDebugLoc(),
1848 TII.get(OpEntry.OpSignExtend));
1849 else {
1850 Register Zero32 = MRI.createVirtualRegister(&X86::GR32RegClass);
1851 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(X86::MOV32r0), Zero32)
1852 .setOperandDead(1);
1853
1854 // Copy the zero into the appropriate sub/super/identical physical
1855 // register. Unfortunately the operations needed are not uniform enough
1856 // to fit neatly into the table above.
1857 if (RegTy.getSizeInBits() == 16) {
1858 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Copy),
1859 TypeEntry.HighInReg)
1860 .addReg(Zero32, {}, X86::sub_16bit);
1861 } else if (RegTy.getSizeInBits() == 32) {
1862 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Copy),
1863 TypeEntry.HighInReg)
1864 .addReg(Zero32);
1865 } else if (RegTy.getSizeInBits() == 64) {
1866 BuildMI(*I.getParent(), I, I.getDebugLoc(),
1867 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1868 .addReg(Zero32)
1869 .addImm(X86::sub_32bit);
1870 }
1871 }
1872 }
1873
1874 // Generate the DIV/IDIV/MUL/IMUL instruction.
1875 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(OpEntry.OpMulDivRem))
1876 .addReg(Op2Reg);
1877
1878 // For i8 remainder, we can't reference ah directly, as we'll end
1879 // up with bogus copies like %r9b = COPY %ah. Reference ax
1880 // instead to prevent ah references in a rex instruction.
1881 //
1882 // The current assumption of the fast register allocator is that isel
1883 // won't generate explicit references to the GR8_NOREX registers. If
1884 // the allocator and/or the backend get enhanced to be more robust in
1885 // that regard, this can be, and should be, removed.
1886 if (OpEntry.ResultReg == X86::AH && STI.is64Bit()) {
1887 Register SourceSuperReg = MRI.createVirtualRegister(&X86::GR16RegClass);
1888 Register ResultSuperReg = MRI.createVirtualRegister(&X86::GR16RegClass);
1889 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Copy), SourceSuperReg)
1890 .addReg(X86::AX);
1891
1892 // Shift AX right by 8 bits instead of using AH.
1893 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(X86::SHR16ri),
1894 ResultSuperReg)
1895 .addReg(SourceSuperReg)
1896 .addImm(8);
1897
1898 // Now reference the 8-bit subreg of the result.
1899 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY),
1900 DstReg)
1901 .addReg(ResultSuperReg, {}, X86::sub_8bit);
1902 } else {
1903 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY),
1904 DstReg)
1905 .addReg(OpEntry.ResultReg);
1906 }
1907 I.eraseFromParent();
1908
1909 return true;
1910}
1911
1912bool X86InstructionSelector::selectSelect(MachineInstr &I,
1913 MachineRegisterInfo &MRI,
1914 MachineFunction &MF) const {
1915 GSelect &Sel = cast<GSelect>(I);
1916 Register DstReg = Sel.getReg(0);
1917 BuildMI(*Sel.getParent(), Sel, Sel.getDebugLoc(), TII.get(X86::TEST32rr))
1918 .addReg(Sel.getCondReg())
1919 .addReg(Sel.getCondReg());
1920
1921 unsigned OpCmp;
1922 LLT Ty = MRI.getType(DstReg);
1923 if (Ty.getSizeInBits() == 80) {
1924 BuildMI(*Sel.getParent(), Sel, Sel.getDebugLoc(), TII.get(X86::CMOVE_Fp80),
1925 DstReg)
1926 .addReg(Sel.getTrueReg())
1927 .addReg(Sel.getFalseReg());
1928 } else {
1929 switch (Ty.getSizeInBits()) {
1930 default:
1931 return false;
1932 case 8:
1933 OpCmp = X86::CMOV_GR8;
1934 break;
1935 case 16:
1936 OpCmp = STI.canUseCMOV() ? X86::CMOV16rr : X86::CMOV_GR16;
1937 break;
1938 case 32:
1939 OpCmp = STI.canUseCMOV() ? X86::CMOV32rr : X86::CMOV_GR32;
1940 break;
1941 case 64:
1942 assert(STI.is64Bit() && STI.canUseCMOV());
1943 OpCmp = X86::CMOV64rr;
1944 break;
1945 }
1946 BuildMI(*Sel.getParent(), Sel, Sel.getDebugLoc(), TII.get(OpCmp), DstReg)
1947 .addReg(Sel.getTrueReg())
1948 .addReg(Sel.getFalseReg())
1950 }
1951 const TargetRegisterClass *DstRC = getRegClass(Ty, DstReg, MRI);
1952 if (!RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
1953 LLVM_DEBUG(dbgs() << "Failed to constrain CMOV\n");
1954 return false;
1955 }
1956
1957 Sel.eraseFromParent();
1958 return true;
1959}
1960
1961InstructionSelector::ComplexRendererFns
1962X86InstructionSelector::selectAddr(MachineOperand &Root) const {
1963 MachineInstr *MI = Root.getParent();
1964
1965 MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
1966 MachineInstr *Ptr = MRI.getVRegDef(Root.getReg());
1967 X86AddressMode AM;
1968 if (!X86SelectAddress(*Ptr, TM, MRI, STI, AM))
1969 return std::nullopt;
1970
1971 if (AM.IndexReg)
1972 return std::nullopt;
1973
1974 return {// Base
1975 {[=](MachineInstrBuilder &MIB) {
1977 MIB.addUse(AM.Base.Reg);
1978 else {
1980 "Unknown type of address base");
1981 MIB.addFrameIndex(AM.Base.FrameIndex);
1982 }
1983 },
1984 // Scale
1985 [=](MachineInstrBuilder &MIB) { MIB.addImm(AM.Scale); },
1986 // Index
1987 [=](MachineInstrBuilder &MIB) { MIB.addUse(0); },
1988 // Disp
1989 [=](MachineInstrBuilder &MIB) {
1990 if (AM.GV)
1991 MIB.addGlobalAddress(AM.GV, AM.Disp, AM.GVOpFlags);
1992 else if (AM.CP)
1993 MIB.addConstantPoolIndex(AM.Disp, 0, AM.GVOpFlags);
1994 else
1995 MIB.addImm(AM.Disp);
1996 },
1997 // Segment
1998 [=](MachineInstrBuilder &MIB) { MIB.addUse(0); }}};
1999}
2000
2001InstructionSelector *
2003 const X86Subtarget &Subtarget,
2004 const X86RegisterBankInfo &RBI) {
2005 return new X86InstructionSelector(TM, Subtarget, RBI);
2006}
const AArch64Subtarget & Subtarget
static const TargetRegisterClass * getRegClass(const MachineInstr &MI, Register Reg)
#define GET_GLOBALISEL_PREDICATES_INIT
#define GET_GLOBALISEL_TEMPORARIES_INIT
static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
uint64_t uint64_t
uint64_t Imm
static bool selectMergeValues(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII, MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
static bool selectUnmergeValues(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII, MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define DEBUG_TYPE
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
IRTranslator LLVM IR MI
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
Implement a low-level type suitable for MachineInstr level instruction selection.
#define I(x, y, z)
Definition MD5.cpp:57
Contains matchers for matching SSA Machine Instructions.
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static unsigned selectLoadStoreOp(unsigned GenericOpc, unsigned RegBankID, unsigned OpSize)
static StringRef getName(Value *V)
RISCVCC::CondCode CC
MachineRegisterInfo & MRI
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
#define LLVM_DEBUG(...)
Definition Debug.h:119
static bool X86SelectAddress(MachineInstr &I, const X86TargetMachine &TM, const MachineRegisterInfo &MRI, const X86Subtarget &STI, X86AddressMode &AM)
static bool canTurnIntoCOPY(const TargetRegisterClass *DstRC, const TargetRegisterClass *SrcRC)
static unsigned getLeaOP(LLT Ty, const X86Subtarget &STI)
static const TargetRegisterClass * getRegClassFromGRPhysReg(Register Reg)
Value * RHS
Value * LHS
This file declares the targeting of the RegisterBankInfo class for X86.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:756
Register getCondReg() const
Register getFalseReg() const
Register getTrueReg() const
Register getReg(unsigned Idx) const
Access the Idx'th operand as a register and return it.
constexpr bool isScalar() const
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr bool isVector() const
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr bool isPointer() const
bool hasSubClassEq(const MCRegisterClass *RC) const
Returns true if RC is a sub-class of or equal to this class.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI unsigned getConstantPoolIndex(const Constant *C, Align Alignment)
getConstantPoolIndex - Create a new entry in the constant pool or return an existing one.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
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.
const MachineInstrBuilder & setOperandDead(unsigned OpIdx) const
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addConstantPoolIndex(unsigned Idx, int Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
@ MOLoad
The memory access reads data.
MachineOperand class - Representation of each machine instruction operand.
void setIsDead(bool Val=true)
LLVM_ABI void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
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 void setRegBank(Register Reg, const RegisterBank &RegBank)
Set the register bank to RegBank for Reg.
const MachineFunction & getMF() const
LLVM_ABI Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
const TargetRegisterClass * getRegClassOrNull(Register Reg) const
Return the register class of Reg, or null if Reg has not been assigned a register class yet.
static const TargetRegisterClass * constrainGenericRegister(Register Reg, const TargetRegisterClass &RC, MachineRegisterInfo &MRI)
Constrain the (possibly generic) virtual register Reg to RC.
const RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
This class implements the register bank concept.
unsigned getID() const
Get the identifier of this register bank.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
CodeModel::Model getCodeModel() const
Returns the code model.
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
Register getGlobalBaseReg(MachineFunction *MF) const
getGlobalBaseReg - Return a virtual register initialized with the the global base register value.
This class provides the information for the target register banks.
bool canUseCMOV() const
bool isTarget64BitILP32() const
Is this x86_64 with the ILP32 programming model (x32 ABI)?
const X86InstrInfo * getInstrInfo() const override
bool hasAVX512() const
unsigned char classifyGlobalReference(const GlobalValue *GV, const Module &M) const
bool isPICStyleRIPRel() const
unsigned char classifyLocalReference(const GlobalValue *GV) const
Classify a global variable reference for the current subtarget according to how we should reference i...
bool hasAVX() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
ImplicitDefMatch m_GImplicitDef()
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
@ X86
Windows x64, Windows Itanium (IA-64)
Definition MCAsmInfo.h:53
@ MO_GOTPCREL_NORELAX
MO_GOTPCREL_NORELAX - Same as MO_GOTPCREL except that R_X86_64_GOTPCREL relocations are guaranteed to...
@ MO_GOTOFF
MO_GOTOFF - On a symbol operand this indicates that the immediate is the offset to the location of th...
@ MO_PIC_BASE_OFFSET
MO_PIC_BASE_OFFSET - On a symbol operand this indicates that the immediate should get the value of th...
@ MO_GOTPCREL
MO_GOTPCREL - On a symbol operand this indicates that the immediate is offset to the GOT entry for th...
@ LAST_VALID_COND
Definition X86BaseInfo.h:95
std::pair< CondCode, bool > getX86ConditionCode(CmpInst::Predicate Predicate)
Return a pair of condition code for the given predicate and whether the instruction operands should b...
unsigned getMOVriOpcode(bool Use64BitReg, int64_t Imm)
Return a MOVri opcode for materializing Imm into a 32- or 64-bit GPR.
StringMapEntry< std::atomic< TypeEntryBody * > > TypeEntry
Definition TypePool.h:31
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
static bool isGlobalRelativeToPICBase(unsigned char TargetFlag)
isGlobalRelativeToPICBase - Return true if the specified global value reference is relative to a 32-b...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI std::optional< APInt > getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT, return the corresponding value.
Definition Utils.cpp:301
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void constrainSelectedInstRegOperands(MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
Mutate the newly-selected instruction I to constrain its (possibly generic) virtual register operands...
Definition Utils.cpp:163
bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel.
static const MachineInstrBuilder & addConstantPoolReference(const MachineInstrBuilder &MIB, unsigned CPI, Register GlobalBaseReg, unsigned char OpFlags)
addConstantPoolReference - This function is used to add a reference to the base of a constant value s...
static const MachineInstrBuilder & addFullAddress(const MachineInstrBuilder &MIB, const X86AddressMode &AM)
LLVM_ABI std::optional< int64_t > getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT fits in int64_t returns it.
Definition Utils.cpp:321
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
static const MachineInstrBuilder & addOffset(const MachineInstrBuilder &MIB, int Offset)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
static const MachineInstrBuilder & addDirectMem(const MachineInstrBuilder &MIB, Register Reg)
addDirectMem - This function is used to add a direct memory reference to the current instruction – th...
InstructionSelector * createX86InstructionSelector(const X86TargetMachine &TM, const X86Subtarget &, const X86RegisterBankInfo &)
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
static LLVM_ABI MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.
X86AddressMode - This struct holds a generalized full x86 address mode.
const GlobalValue * GV
union llvm::X86AddressMode::BaseUnion Base
enum llvm::X86AddressMode::@202116273335065351270200035056227005202106004277 BaseType