33 #define DEBUG_TYPE "X86-isel"
39 #ifndef LLVM_BUILD_GLOBAL_ISEL
40 #error "You shouldn't build this"
45 #define GET_GLOBALISEL_PREDICATE_BITSET
46 #include "X86GenGlobalISel.inc"
47 #undef GET_GLOBALISEL_PREDICATE_BITSET
63 uint64_t Alignment)
const;
92 bool emitInsertSubreg(
unsigned DstReg,
unsigned SrcReg,
MachineInstr &I,
95 bool emitExtractSubreg(
unsigned DstReg,
unsigned SrcReg,
MachineInstr &I,
108 #define GET_GLOBALISEL_PREDICATES_DECL
109 #include "X86GenGlobalISel.inc"
110 #undef GET_GLOBALISEL_PREDICATES_DECL
112 #define GET_GLOBALISEL_TEMPORARIES_DECL
113 #include "X86GenGlobalISel.inc"
114 #undef GET_GLOBALISEL_TEMPORARIES_DECL
119 #define GET_GLOBALISEL_IMPL
120 #include "X86GenGlobalISel.inc"
121 #undef GET_GLOBALISEL_IMPL
127 TRI(*STI.getRegisterInfo()), RBI(RBI),
129 #include
"X86GenGlobalISel.inc"
132 #include
"X86GenGlobalISel.inc"
141 if (RB.
getID() == X86::GPRRegBankID) {
143 return &X86::GR8RegClass;
145 return &X86::GR16RegClass;
147 return &X86::GR32RegClass;
149 return &X86::GR64RegClass;
151 if (RB.
getID() == X86::VECRRegBankID) {
153 return STI.hasAVX512() ? &X86::FR32XRegClass : &X86::FR32RegClass;
155 return STI.hasAVX512() ? &X86::FR64XRegClass : &X86::FR64RegClass;
157 return STI.hasAVX512() ? &X86::VR128XRegClass : &X86::VR128RegClass;
159 return STI.hasAVX512() ? &X86::VR256XRegClass : &X86::VR256RegClass;
161 return &X86::VR512RegClass;
170 const RegisterBank &RegBank = *RBI.getRegBank(Reg, MRI, TRI);
179 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
180 assert(I.
isCopy() &&
"Generic operators do not allow physical registers");
184 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
187 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
189 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.
isCopy()) &&
190 "No phys reg on generic operators");
191 assert((DstSize == SrcSize ||
194 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
195 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI))) &&
196 "Copy with different width?!");
200 switch (RegBank.
getID()) {
201 case X86::GPRRegBankID:
202 assert((DstSize <= 64) &&
"GPRs cannot get more than 64-bit width values.");
206 if (SrcSize > DstSize && TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
207 if (RC == &X86::GR32RegClass)
209 else if (RC == &X86::GR16RegClass)
211 else if (RC == &X86::GR8RegClass)
217 case X86::VECRRegBankID:
229 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
239 bool X86InstructionSelector::select(
MachineInstr &I)
const {
259 "Generic instruction has unexpected implicit operands\n");
267 if (selectLoadStoreOp(I, MRI, MF))
269 if (selectFrameIndexOrGep(I, MRI, MF))
271 if (selectGlobalValue(I, MRI, MF))
273 if (selectConstant(I, MRI, MF))
275 if (selectTrunc(I, MRI, MF))
277 if (selectZext(I, MRI, MF))
279 if (selectCmp(I, MRI, MF))
281 if (selectUadde(I, MRI, MF))
287 if (selectExtract(I, MRI, MF))
289 if (selectInsert(I, MRI, MF))
295 unsigned X86InstructionSelector::getLoadStoreOp(
LLT &Ty,
const RegisterBank &RB,
297 uint64_t Alignment)
const {
298 bool Isload = (Opc == TargetOpcode::G_LOAD);
299 bool HasAVX = STI.hasAVX();
300 bool HasAVX512 = STI.hasAVX512();
301 bool HasVLX = STI.hasVLX();
303 if (Ty == LLT::scalar(8)) {
304 if (X86::GPRRegBankID == RB.
getID())
305 return Isload ? X86::MOV8rm : X86::MOV8mr;
306 }
else if (Ty == LLT::scalar(16)) {
307 if (X86::GPRRegBankID == RB.
getID())
308 return Isload ? X86::MOV16rm : X86::MOV16mr;
309 }
else if (Ty == LLT::scalar(32) || Ty == LLT::pointer(0, 32)) {
310 if (X86::GPRRegBankID == RB.
getID())
311 return Isload ? X86::MOV32rm : X86::MOV32mr;
312 if (X86::VECRRegBankID == RB.
getID())
313 return Isload ? (HasAVX512 ? X86::VMOVSSZrm
314 : HasAVX ? X86::VMOVSSrm : X86::MOVSSrm)
315 : (HasAVX512 ? X86::VMOVSSZmr
316 : HasAVX ? X86::VMOVSSmr : X86::MOVSSmr);
317 }
else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
318 if (X86::GPRRegBankID == RB.
getID())
319 return Isload ? X86::MOV64rm : X86::MOV64mr;
320 if (X86::VECRRegBankID == RB.
getID())
321 return Isload ? (HasAVX512 ? X86::VMOVSDZrm
322 : HasAVX ? X86::VMOVSDrm : X86::MOVSDrm)
323 : (HasAVX512 ? X86::VMOVSDZmr
324 : HasAVX ? X86::VMOVSDmr : X86::MOVSDmr);
327 return Isload ? (HasVLX ? X86::VMOVAPSZ128rm
329 ? X86::VMOVAPSZ128rm_NOVLX
330 : HasAVX ? X86::VMOVAPSrm : X86::MOVAPSrm)
331 : (HasVLX ? X86::VMOVAPSZ128mr
333 ? X86::VMOVAPSZ128mr_NOVLX
334 : HasAVX ? X86::VMOVAPSmr : X86::MOVAPSmr);
336 return Isload ? (HasVLX ? X86::VMOVUPSZ128rm
338 ? X86::VMOVUPSZ128rm_NOVLX
339 : HasAVX ? X86::VMOVUPSrm : X86::MOVUPSrm)
340 : (HasVLX ? X86::VMOVUPSZ128mr
342 ? X86::VMOVUPSZ128mr_NOVLX
343 : HasAVX ? X86::VMOVUPSmr : X86::MOVUPSmr);
346 return Isload ? (HasVLX ? X86::VMOVAPSZ256rm
347 : HasAVX512 ? X86::VMOVAPSZ256rm_NOVLX
349 : (HasVLX ? X86::VMOVAPSZ256mr
350 : HasAVX512 ? X86::VMOVAPSZ256mr_NOVLX
353 return Isload ? (HasVLX ? X86::VMOVUPSZ256rm
354 : HasAVX512 ? X86::VMOVUPSZ256rm_NOVLX
356 : (HasVLX ? X86::VMOVUPSZ256mr
357 : HasAVX512 ? X86::VMOVUPSZ256mr_NOVLX
361 return Isload ? X86::VMOVAPSZrm : X86::VMOVAPSZmr;
363 return Isload ? X86::VMOVUPSZrm : X86::VMOVUPSZmr;
374 "unsupported type.");
376 if (I.
getOpcode() == TargetOpcode::G_GEP) {
380 AM.
Disp =
static_cast<int32_t
>(Imm);
385 }
else if (I.
getOpcode() == TargetOpcode::G_FRAME_INDEX) {
387 AM.
BaseType = X86AddressMode::FrameIndexBase;
396 bool X86InstructionSelector::selectLoadStoreOp(
MachineInstr &I,
402 if (Opc != TargetOpcode::G_STORE && Opc != TargetOpcode::G_LOAD)
407 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
410 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
411 DEBUG(
dbgs() <<
"Atomic load/store not supported yet\n");
415 unsigned NewOpc = getLoadStoreOp(Ty, RB, Opc, MemOp.getAlignment());
424 if (Opc == TargetOpcode::G_LOAD) {
433 return constrainSelectedInstRegOperands(I,
TII, TRI, RBI);
437 if (Ty == LLT::pointer(0, 64))
439 else if (Ty == LLT::pointer(0, 32))
445 bool X86InstructionSelector::selectFrameIndexOrGep(
MachineInstr &I,
450 if (Opc != TargetOpcode::G_FRAME_INDEX && Opc != TargetOpcode::G_GEP)
457 unsigned NewOpc =
getLeaOP(Ty, STI);
461 if (Opc == TargetOpcode::G_FRAME_INDEX) {
467 MIB.addImm(0).addReg(0);
470 return constrainSelectedInstRegOperands(I,
TII, TRI, RBI);
473 bool X86InstructionSelector::selectGlobalValue(
MachineInstr &I,
478 if (Opc != TargetOpcode::G_GLOBAL_VALUE)
482 if (GV->isThreadLocal()) {
492 AM.
GVOpFlags = STI.classifyGlobalReference(GV);
502 if (STI.isPICStyleRIPRel()) {
510 unsigned NewOpc =
getLeaOP(Ty, STI);
518 return constrainSelectedInstRegOperands(I,
TII, TRI, RBI);
521 bool X86InstructionSelector::selectConstant(
MachineInstr &I,
524 if (I.
getOpcode() != TargetOpcode::G_CONSTANT)
530 if (RBI.getRegBank(DefReg, MRI, TRI)->getID() != X86::GPRRegBankID)
545 NewOpc = X86::MOV8ri;
548 NewOpc = X86::MOV16ri;
551 NewOpc = X86::MOV32ri;
556 NewOpc = X86::MOV64ri32;
558 NewOpc = X86::MOV64ri;
566 return constrainSelectedInstRegOperands(I,
TII, TRI, RBI);
569 bool X86InstructionSelector::selectTrunc(
MachineInstr &I,
572 if (I.
getOpcode() != TargetOpcode::G_TRUNC)
581 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
582 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
585 DEBUG(
dbgs() <<
"G_TRUNC input/output on different banks\n");
589 if (DstRB.
getID() != X86::GPRRegBankID)
601 if (DstRC == SrcRC) {
603 SubIdx = X86::NoSubRegister;
604 }
else if (DstRC == &X86::GR32RegClass) {
605 SubIdx = X86::sub_32bit;
606 }
else if (DstRC == &X86::GR16RegClass) {
607 SubIdx = X86::sub_16bit;
608 }
else if (DstRC == &X86::GR8RegClass) {
609 SubIdx = X86::sub_8bit;
614 SrcRC = TRI.getSubClassWithSubReg(SrcRC, SubIdx);
616 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
617 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
618 DEBUG(
dbgs() <<
"Failed to constrain G_TRUNC\n");
628 bool X86InstructionSelector::selectZext(
MachineInstr &I,
631 if (I.
getOpcode() != TargetOpcode::G_ZEXT)
640 if (SrcTy != LLT::scalar(1))
644 if (DstTy == LLT::scalar(8))
645 AndOpc = X86::AND8ri;
646 else if (DstTy == LLT::scalar(16))
647 AndOpc = X86::AND16ri8;
648 else if (DstTy == LLT::scalar(32))
649 AndOpc = X86::AND32ri8;
650 else if (DstTy == LLT::scalar(64))
651 AndOpc = X86::AND64ri8;
655 unsigned DefReg = SrcReg;
656 if (DstTy != LLT::scalar(8)) {
659 TII.get(TargetOpcode::SUBREG_TO_REG), DefReg)
670 constrainSelectedInstRegOperands(AndInst,
TII, TRI, RBI);
679 if (I.
getOpcode() != TargetOpcode::G_ICMP)
704 OpCmp = X86::CMP16rr;
707 OpCmp = X86::CMP32rr;
710 OpCmp = X86::CMP64rr;
722 constrainSelectedInstRegOperands(CmpInst,
TII, TRI, RBI);
723 constrainSelectedInstRegOperands(SetInst,
TII, TRI, RBI);
729 bool X86InstructionSelector::selectUadde(
MachineInstr &I,
732 if (I.
getOpcode() != TargetOpcode::G_UADDE)
743 if (DstTy != LLT::scalar(32))
748 while (Def->
getOpcode() == TargetOpcode::G_TRUNC) {
754 if (Def->
getOpcode() == TargetOpcode::G_UADDE) {
760 if (!RBI.constrainGenericRegister(CarryInReg, X86::GR32RegClass, MRI))
763 Opcode = X86::ADC32rr;
769 Opcode = X86::ADD32rr;
779 .addReg(X86::EFLAGS);
781 if (!constrainSelectedInstRegOperands(AddInst,
TII, TRI, RBI) ||
782 !RBI.constrainGenericRegister(CarryOutReg, X86::GR32RegClass, MRI))
789 bool X86InstructionSelector::selectExtract(
MachineInstr &I,
793 if (I.
getOpcode() != TargetOpcode::G_EXTRACT)
812 if (!emitExtractSubreg(DstReg, SrcReg, I, MRI, MF))
819 bool HasAVX = STI.hasAVX();
820 bool HasAVX512 = STI.hasAVX512();
821 bool HasVLX = STI.hasVLX();
844 return constrainSelectedInstRegOperands(I,
TII, TRI, RBI);
847 bool X86InstructionSelector::emitExtractSubreg(
unsigned DstReg,
unsigned SrcReg,
854 unsigned SubIdx = X86::NoSubRegister;
860 "Incorrect Src/Dst register size");
863 SubIdx = X86::sub_xmm;
865 SubIdx = X86::sub_ymm;
872 SrcRC = TRI.getSubClassWithSubReg(SrcRC, SubIdx);
874 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
875 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
876 DEBUG(
dbgs() <<
"Failed to constrain G_TRUNC\n");
881 .addReg(SrcReg, 0, SubIdx);
886 bool X86InstructionSelector::emitInsertSubreg(
unsigned DstReg,
unsigned SrcReg,
893 unsigned SubIdx = X86::NoSubRegister;
900 "Incorrect Src/Dst register size");
903 SubIdx = X86::sub_xmm;
905 SubIdx = X86::sub_ymm;
912 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
913 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
914 DEBUG(
dbgs() <<
"Failed to constrain INSERT_SUBREG\n");
925 bool X86InstructionSelector::selectInsert(
MachineInstr &I,
929 if (I.
getOpcode() != TargetOpcode::G_INSERT)
938 const LLT InsertRegTy = MRI.
getType(InsertReg);
949 if (!emitInsertSubreg(DstReg, InsertReg, I, MRI, MF))
956 bool HasAVX = STI.hasAVX();
957 bool HasAVX512 = STI.hasAVX512();
958 bool HasVLX = STI.hasVLX();
982 return constrainSelectedInstRegOperands(I,
TII, TRI, RBI);
988 if (I.
getOpcode() != TargetOpcode::G_UNMERGE_VALUES)
996 for (
unsigned Idx = 0; Idx < NumDefs; ++Idx) {
1004 if (!select(ExtrInst))
1015 if (I.
getOpcode() != TargetOpcode::G_MERGE_VALUES)
1026 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
1036 unsigned Tmp = MRI.createGenericVirtualRegister(DstTy);
1037 MRI.setRegBank(Tmp, RegBank);
1040 TII.get(TargetOpcode::G_INSERT), Tmp)
1043 .addImm((Idx - 1) * SrcSize);
1047 if (!select(InsertInst))
1052 TII.get(TargetOpcode::COPY), DstReg)
1055 if (!select(CopyInst))
1065 return new X86InstructionSelector(TM, Subtarget, RBI);
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
This class is the base class for the comparison instructions.
const GlobalValue * getGlobal() const
LLT getType(unsigned VReg) const
Get the low-level type of VReg or LLT{} if VReg is not a generic (target independent) virtual registe...
static bool selectMergeValues(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII, MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool hasSubClassEq(const TargetRegisterClass *RC) const
Returns true if RC is a sub-class of or equal to this class.
void setRegBank(unsigned Reg, const RegisterBank &RegBank)
Set the register bank to RegBank for Reg.
void print(raw_ostream &OS, bool SkipOpers=false, bool SkipDebugLoc=false, const TargetInstrInfo *TII=nullptr) const
Debugging supportPrint this MI to OS.
unsigned createGenericVirtualRegister(LLT Ty)
Create and return a new generic virtual register with low-level type Ty.
const HexagonInstrInfo * TII
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
This class provides the information for the target register banks.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
static bool isGlobalStubReference(unsigned char TargetFlag)
isGlobalStubReference - Return true if the specified TargetFlag operand is a reference to a stub for ...
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Reg
All possible values of the reg field in the ModR/M byte.
static int getRegClass(RegisterKind Is, unsigned RegWidth)
union llvm::X86AddressMode::@422 Base
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
static bool isGlobalRelativeToPICBase(unsigned char TargetFlag)
isGlobalRelativeToPICBase - Return true if the specified global value reference is relative to a 32-b...
void RemoveOperand(unsigned i)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
const MachineBasicBlock * getParent() const
bool isImplicitDef() const
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
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...
enum llvm::X86AddressMode::@421 BaseType
unsigned const MachineRegisterInfo * MRI
#define GET_GLOBALISEL_PREDICATES_INIT
static unsigned getSizeInBits(unsigned Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI)
Get the size in bits of Reg.
const MachineOperand & getOperand(unsigned i) const
unsigned getID() const
Get the identifier of this register bank.
bool isCImm() const
isCImm - Test if this is a MO_CImmediate operand.
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
int64_t addOffset(int64_t LHS, int64_t RHS)
void substPhysReg(unsigned Reg, const TargetRegisterInfo &)
substPhysReg - Substitute the current register with the physical register Reg, taking any existing Su...
void setImm(int64_t immVal)
static bool selectUnmergeValues(MachineInstrBuilder &MIB, const ARMBaseInstrInfo &TII, MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
void X86SelectAddress(const MachineInstr &I, const MachineRegisterInfo &MRI, X86AddressMode &AM)
unsigned getSETFromCond(CondCode CC, bool HasMemoryOperand=false)
Return a set opcode for the given condition and whether it has a memory operand.
This file declares the targeting of the RegisterBankInfo class for X86.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
constexpr bool isInt< 32 >(int64_t x)
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
bool isTarget64BitILP32() const
Is this x86_64 with the ILP32 programming model (x32 ABI)?
MachineOperand class - Representation of each machine instruction operand.
unsigned getPredicate() const
InstructionSelector * createX86InstructionSelector(const X86TargetMachine &TM, X86Subtarget &, X86RegisterBankInfo &)
This class implements the register bank concept.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
const ConstantInt * getCImm() const
static bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel...
Optional< int64_t > getConstantVRegVal(unsigned VReg, const MachineRegisterInfo &MRI)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides the logic to select generic machine instructions.
Representation of each machine instruction.
static unsigned getLeaOP(LLT Ty, const X86Subtarget &STI)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setSubReg(unsigned subReg)
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
#define GET_GLOBALISEL_TEMPORARIES_INIT
unsigned getReg() const
getReg - Returns the register number.
const TargetRegisterClass * getRegClassOrNull(unsigned Reg) const
Return the register class of Reg, or null if Reg has not been assigned a register class yet...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MachineInstrBuilder & addUse(unsigned RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
unsigned getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
X86AddressMode - This struct holds a generalized full x86 address mode.
static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII, MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
static const MachineInstrBuilder & addFullAddress(const MachineInstrBuilder &MIB, const X86AddressMode &AM)
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.