22 #define DEBUG_TYPE "legalizer" 31 static bool isArtifactCast(
unsigned Opc) {
33 case TargetOpcode::G_TRUNC:
34 case TargetOpcode::G_SEXT:
35 case TargetOpcode::G_ZEXT:
36 case TargetOpcode::G_ANYEXT:
46 : Builder(B), MRI(MRI), LI(LI) {}
63 markInstAndDefDead(MI, *MRI.
getVRegDef(SrcReg), DeadInsts);
76 markInstAndDefDead(MI, *ExtMI, DeadInsts);
83 if (SrcMI->getOpcode() == TargetOpcode::G_CONSTANT) {
85 if (isInstLegal({TargetOpcode::G_CONSTANT, {DstTy}})) {
86 auto &CstVal = SrcMI->getOperand(1);
88 DstReg, CstVal.getCImm()->getValue().sext(DstTy.
getSizeInBits()));
90 markInstAndDefDead(MI, *SrcMI, DeadInsts);
94 return tryFoldImplicitDef(MI, DeadInsts, UpdatedDefs);
110 if (isInstUnsupported({TargetOpcode::G_AND, {DstTy}}) ||
111 isConstantUnsupported(DstTy))
120 markInstAndDefDead(MI, *MRI.
getVRegDef(SrcReg), DeadInsts);
127 if (SrcMI->getOpcode() == TargetOpcode::G_CONSTANT) {
129 if (isInstLegal({TargetOpcode::G_CONSTANT, {DstTy}})) {
130 auto &CstVal = SrcMI->getOperand(1);
132 DstReg, CstVal.getCImm()->getValue().zext(DstTy.
getSizeInBits()));
134 markInstAndDefDead(MI, *SrcMI, DeadInsts);
138 return tryFoldImplicitDef(MI, DeadInsts, UpdatedDefs);
154 if (isInstUnsupported({TargetOpcode::G_SEXT_INREG, {DstTy}}))
160 TargetOpcode::G_SEXT_INREG, {DstReg},
162 markInstAndDefDead(MI, *MRI.
getVRegDef(SrcReg), DeadInsts);
165 return tryFoldImplicitDef(MI, DeadInsts, UpdatedDefs);
180 if (SrcMI->getOpcode() == TargetOpcode::G_CONSTANT) {
182 if (isInstLegal({TargetOpcode::G_CONSTANT, {DstTy}})) {
183 auto &CstVal = SrcMI->getOperand(1);
185 DstReg, CstVal.getCImm()->getValue().trunc(DstTy.
getSizeInBits()));
187 markInstAndDefDead(MI, *SrcMI, DeadInsts);
200 assert(Opcode == TargetOpcode::G_ANYEXT || Opcode == TargetOpcode::G_ZEXT ||
201 Opcode == TargetOpcode::G_SEXT);
209 if (Opcode == TargetOpcode::G_ANYEXT) {
211 if (isInstUnsupported({TargetOpcode::G_IMPLICIT_DEF, {DstTy}}))
213 LLVM_DEBUG(
dbgs() <<
".. Combine G_ANYEXT(G_IMPLICIT_DEF): " << MI;);
214 Builder.
buildInstr(TargetOpcode::G_IMPLICIT_DEF, {DstReg}, {});
219 if (isConstantUnsupported(DstTy))
221 LLVM_DEBUG(
dbgs() <<
".. Combine G_[SZ]EXT(G_IMPLICIT_DEF): " << MI;);
226 markInstAndDefDead(MI, *
DefMI, DeadInsts);
238 case TargetOpcode::G_BUILD_VECTOR:
239 case TargetOpcode::G_MERGE_VALUES:
265 case TargetOpcode::G_CONCAT_VECTORS: {
276 if (ConvertOp == TargetOpcode::G_TRUNC)
297 unsigned ConvertOp = 0;
301 if (isArtifactCast(SrcOp)) {
306 if (!MergeI || !canFoldMergeOpcode(MergeI->
getOpcode(),
307 ConvertOp, OpTy, DestTy))
312 if (NumMergeRegs < NumDefs) {
313 if (NumDefs % NumMergeRegs != 0)
324 const unsigned NewNumDefs = NumDefs / NumMergeRegs;
325 for (
unsigned Idx = 0; Idx < NumMergeRegs; ++Idx) {
327 for (
unsigned j = 0, DefIdx = Idx * NewNumDefs; j < NewNumDefs;
337 for (
unsigned j = 0; j < NumMergeRegs; ++j)
342 for (
unsigned j = 0; j < NumMergeRegs; ++j)
343 Builder.
buildInstr(ConvertOp, {DstRegs[j]}, {TmpRegs[j]});
350 }
else if (NumMergeRegs > NumDefs) {
351 if (ConvertOp != 0 || NumMergeRegs % NumDefs != 0)
362 const unsigned NumRegs = NumMergeRegs / NumDefs;
363 for (
unsigned DefIdx = 0; DefIdx < NumDefs; ++DefIdx) {
365 for (
unsigned j = 0, Idx = NumRegs * DefIdx + 1; j < NumRegs;
377 if (!ConvertOp && DestTy != MergeSrcTy)
378 ConvertOp = TargetOpcode::G_BITCAST;
383 for (
unsigned Idx = 0; Idx < NumDefs; ++Idx) {
386 Builder.
buildInstr(ConvertOp, {DefReg}, {MergeSrc});
390 markInstAndDefDead(MI, *MergeI, DeadInsts);
394 assert(DestTy == MergeSrcTy &&
395 "Bitcast and the other kinds of conversions should " 396 "have happened earlier");
398 for (
unsigned Idx = 0; Idx < NumDefs; ++Idx) {
405 markInstAndDefDead(MI, *MergeI, DeadInsts);
411 case TargetOpcode::G_MERGE_VALUES:
412 case TargetOpcode::G_BUILD_VECTOR:
413 case TargetOpcode::G_CONCAT_VECTORS:
439 if (!MergeI || !isMergeLikeOpcode(MergeI->getOpcode()))
449 unsigned NumMergeSrcs = MergeI->getNumOperands() - 1;
450 unsigned MergeSrcSize = SrcTy.
getSizeInBits() / NumMergeSrcs;
451 unsigned MergeSrcIdx = Offset / MergeSrcSize;
454 unsigned EndMergeSrcIdx = (Offset + ExtractDstSize - 1) / MergeSrcSize;
457 if (MergeSrcIdx != EndMergeSrcIdx)
462 Builder.
buildExtract(DstReg, MergeI->getOperand(MergeSrcIdx + 1).getReg(),
463 Offset - MergeSrcIdx * MergeSrcSize);
465 markInstAndDefDead(MI, *MergeI, DeadInsts);
479 if (!DeadInsts.
empty())
480 deleteMarkedDeadInsts(DeadInsts, WrapperObserver);
487 bool Changed =
false;
491 case TargetOpcode::G_ANYEXT:
492 Changed = tryCombineAnyExt(MI, DeadInsts, UpdatedDefs);
494 case TargetOpcode::G_ZEXT:
495 Changed = tryCombineZExt(MI, DeadInsts, UpdatedDefs);
497 case TargetOpcode::G_SEXT:
498 Changed = tryCombineSExt(MI, DeadInsts, UpdatedDefs);
500 case TargetOpcode::G_UNMERGE_VALUES:
501 Changed = tryCombineMerges(MI, DeadInsts, UpdatedDefs);
503 case TargetOpcode::G_EXTRACT:
504 Changed = tryCombineExtract(MI, DeadInsts, UpdatedDefs);
506 case TargetOpcode::G_TRUNC:
507 Changed = tryCombineTrunc(MI, DeadInsts, UpdatedDefs);
521 while (!UpdatedDefs.
empty()) {
525 switch (
Use.getOpcode()) {
527 case TargetOpcode::G_ANYEXT:
528 case TargetOpcode::G_ZEXT:
529 case TargetOpcode::G_SEXT:
530 case TargetOpcode::G_UNMERGE_VALUES:
531 case TargetOpcode::G_EXTRACT:
532 case TargetOpcode::G_TRUNC:
536 case TargetOpcode::COPY: {
556 case TargetOpcode::COPY:
557 case TargetOpcode::G_TRUNC:
558 case TargetOpcode::G_ZEXT:
559 case TargetOpcode::G_ANYEXT:
560 case TargetOpcode::G_SEXT:
561 case TargetOpcode::G_UNMERGE_VALUES:
563 case TargetOpcode::G_EXTRACT:
589 while (PrevMI != &DefMI) {
590 unsigned PrevRegSrc = getArtifactSrcReg(*PrevMI);
594 if (TmpDef != &DefMI) {
597 "Expecting copy or artifact cast here");
617 for (
auto *DeadMI : DeadInsts) {
620 DeadMI->eraseFromParentAndMarkDBGValuesForRemoval();
637 bool isConstantUnsupported(
LLT Ty)
const {
639 return isInstUnsupported({TargetOpcode::G_CONSTANT, {Ty}});
642 return isInstUnsupported({TargetOpcode::G_CONSTANT, {EltTy}}) ||
643 isInstUnsupported({TargetOpcode::G_BUILD_VECTOR, {Ty, EltTy}});
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
bind_ty< MachineInstr * > m_MInstr(MachineInstr *&MI)
MachineInstrBuilder buildUnmerge(ArrayRef< LLT > Res, const SrcOp &Op)
Build and insert Res0, ...
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
UnaryOp_match< SrcTy, TargetOpcode::G_ANYEXT > m_GAnyExt(const SrcTy &Src)
This class represents lattice values for constants.
unsigned getScalarSizeInBits() const
void push_back(const T &Elt)
The LegalityQuery object bundles together all the information that's needed to decide whether a given...
APInt zext(unsigned width) const
Zero extend to a new width.
UnaryOp_match< SrcTy, TargetOpcode::G_ZEXT > m_GZExt(const SrcTy &Src)
LLT getType(unsigned Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register...
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
MachineInstr * getDefIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, folding away any trivial copies.
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
MachineInstrBuilder buildExtract(const DstOp &Res, const SrcOp &Src, uint64_t Index)
Build and insert `Res0, ...
bool tryCombineInstruction(MachineInstr &MI, SmallVectorImpl< MachineInstr *> &DeadInsts, GISelObserverWrapper &WrapperObserver)
Try to combine away MI.
Or< Preds... > m_any_of(Preds &&... preds)
LegalizeActionStep getAction(const LegalityQuery &Query) const
Determine what action should be taken to legalize the described instruction.
unsigned getNumOperands() const
Retuns the total number of operands.
A Use represents the edge between a Value definition and its users.
MachineInstrBuilder buildAnyExtOrTrunc(const DstOp &Res, const SrcOp &Op)
Res = COPY Op depending on the differing sizes of Res and Op.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
This operation is completely unsupported on the target.
LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
bool tryFoldImplicitDef(MachineInstr &MI, SmallVectorImpl< MachineInstr *> &DeadInsts, SmallVectorImpl< Register > &UpdatedDefs)
Try to fold G_[ASZ]EXT (G_IMPLICIT_DEF).
UnaryOp_match< SrcTy, TargetOpcode::COPY > m_Copy(SrcTy &&Src)
And< Preds... > m_all_of(Preds &&... preds)
bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
unsigned const MachineRegisterInfo * MRI
Sentinel value for when no action was found in the specified table.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
Helper class to build MachineInstr.
void setInstr(MachineInstr &MI)
Set the insertion point to before MI.
static bool isMergeLikeOpcode(unsigned Opc)
LegalizationArtifactCombiner(MachineIRBuilder &B, MachineRegisterInfo &MRI, const LegalizerInfo &LI)
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
static bool canFoldMergeOpcode(unsigned MergeOp, unsigned ConvertOp, LLT OpTy, LLT DestTy)
MachineInstr * getOpcodeDef(unsigned Opcode, Register Reg, const MachineRegisterInfo &MRI)
See if Reg is defined by an single def instruction that is Opcode.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool tryCombineAnyExt(MachineInstr &MI, SmallVectorImpl< MachineInstr *> &DeadInsts, SmallVectorImpl< Register > &UpdatedDefs)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
MachineInstrBuilder MachineInstrBuilder & DefMI
unsigned getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
LLVM_NODISCARD T pop_back_val()
bool tryCombineExtract(MachineInstr &MI, SmallVectorImpl< MachineInstr *> &DeadInsts, SmallVectorImpl< Register > &UpdatedDefs)
This file declares the MachineIRBuilder class.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool tryCombineSExt(MachineInstr &MI, SmallVectorImpl< MachineInstr *> &DeadInsts, SmallVectorImpl< Register > &UpdatedDefs)
Class for arbitrary precision integers.
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
UnaryOp_match< SrcTy, TargetOpcode::G_SEXT > m_GSExt(const SrcTy &Src)
void replaceRegWith(unsigned FromReg, unsigned ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
UnaryOp_match< SrcTy, TargetOpcode::G_TRUNC > m_GTrunc(const SrcTy &Src)
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
LegalizeAction Action
The action to take or the final answer.
bool hasOneUse(unsigned RegNo) const
hasOneUse - Return true if there is exactly one instruction using the specified register.
LLVM_NODISCARD bool empty() const
bool tryCombineMerges(MachineInstr &MI, SmallVectorImpl< MachineInstr *> &DeadInsts, SmallVectorImpl< Register > &UpdatedDefs)
iterator_range< use_instr_iterator > use_instructions(unsigned Reg) const
bool tryCombineTrunc(MachineInstr &MI, SmallVectorImpl< MachineInstr *> &DeadInsts, SmallVectorImpl< Register > &UpdatedDefs)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool tryCombineZExt(MachineInstr &MI, SmallVectorImpl< MachineInstr *> &DeadInsts, SmallVectorImpl< Register > &UpdatedDefs)
operand_type_match m_Reg()
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1)
Build and insert Res = G_AND Op0, Op1.
MachineInstrBuilder buildMerge(const DstOp &Res, ArrayRef< Register > Ops)
Build and insert Res = G_MERGE_VALUES Op0, ...
Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
Simple wrapper observer that takes several observers, and calls each one for each event...
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
The operation is expected to be selectable directly by the target, and no transformation is necessary...
Wrapper class representing virtual and physical registers.