38 #define DEBUG_TYPE "x86-domain-reassignment" 40 STATISTIC(NumClosuresConverted,
"Number of closures converted by the pass");
47 enum RegDomain { NoDomain = -1, GPRDomain, MaskDomain, OtherDomain, NumDomains };
50 return X86::GR64RegClass.hasSubClassEq(RC) ||
51 X86::GR32RegClass.hasSubClassEq(RC) ||
52 X86::GR16RegClass.hasSubClassEq(RC) ||
53 X86::GR8RegClass.hasSubClassEq(RC);
58 return X86::VK16RegClass.hasSubClassEq(RC);
73 assert(Domain == MaskDomain &&
"add domain");
74 if (X86::GR8RegClass.hasSubClassEq(SrcRC))
75 return &X86::VK8RegClass;
76 if (X86::GR16RegClass.hasSubClassEq(SrcRC))
77 return &X86::VK16RegClass;
78 if (X86::GR32RegClass.hasSubClassEq(SrcRC))
79 return &X86::VK32RegClass;
80 if (X86::GR64RegClass.hasSubClassEq(SrcRC))
81 return &X86::VK64RegClass;
87 class InstrConverterBase {
92 InstrConverterBase(
unsigned SrcOpcode) : SrcOpcode(SrcOpcode) {}
94 virtual ~InstrConverterBase() {}
100 "Wrong instruction passed to converter");
118 class InstrIgnore :
public InstrConverterBase {
120 InstrIgnore(
unsigned SrcOpcode) : InstrConverterBase(SrcOpcode) {}
124 assert(isLegal(MI, TII) &&
"Cannot convert instruction");
135 class InstrReplacer :
public InstrConverterBase {
140 InstrReplacer(
unsigned SrcOpcode,
unsigned DstOpcode)
141 : InstrConverterBase(SrcOpcode), DstOpcode(DstOpcode) {}
145 if (!InstrConverterBase::isLegal(MI, TII))
150 if (MO.isReg() && MO.isDef() && !MO.isDead() &&
158 assert(isLegal(MI, TII) &&
"Cannot convert instruction");
177 class InstrReplacerDstCOPY :
public InstrConverterBase {
181 InstrReplacerDstCOPY(
unsigned SrcOpcode,
unsigned DstOpcode)
182 : InstrConverterBase(SrcOpcode), DstOpcode(DstOpcode) {}
186 assert(isLegal(MI, TII) &&
"Cannot convert instruction");
194 for (
unsigned Idx = 1, End = MI->
getNumOperands(); Idx < End; ++Idx)
197 BuildMI(*MBB, MI, DL, TII->
get(TargetOpcode::COPY))
213 class InstrCOPYReplacer :
public InstrReplacer {
217 InstrCOPYReplacer(
unsigned SrcOpcode,
RegDomain DstDomain,
unsigned DstOpcode)
218 : InstrReplacer(SrcOpcode, DstOpcode), DstDomain(DstDomain) {}
235 if (OpDomain == DstDomain)
243 class InstrReplaceWithCopy :
public InstrConverterBase {
248 InstrReplaceWithCopy(
unsigned SrcOpcode,
unsigned SrcOpIdx)
249 : InstrConverterBase(SrcOpcode), SrcOpIdx(SrcOpIdx) {}
253 assert(isLegal(MI, TII) &&
"Cannot convert instruction");
255 TII->
get(TargetOpcode::COPY))
268 typedef std::pair<int, unsigned> InstrConverterBaseKeyTy;
271 InstrConverterBaseMap;
293 std::bitset<NumDomains> LegalDstDomains;
300 Closure(
unsigned ID, std::initializer_list<RegDomain> LegalDstDomainList) :
ID(ID) {
302 LegalDstDomains.set(
D);
306 void setAllIllegal() { LegalDstDomains.reset(); }
309 bool hasLegalDstDomain()
const {
return LegalDstDomains.any(); }
312 bool isLegal(
RegDomain RD)
const {
return LegalDstDomains[RD]; }
315 void setIllegal(
RegDomain RD) { LegalDstDomains[RD] =
false; }
317 bool empty()
const {
return Edges.
empty(); }
319 bool insertEdge(
unsigned Reg) {
320 return Edges.
insert(Reg).second;
337 dbgs() <<
"Registers: ";
339 for (
unsigned Reg : Edges) {
345 dbgs() <<
"\n" <<
"Instructions:";
353 unsigned getID()
const {
385 return "X86 Domain Reassignment Pass";
390 InstrConverterBaseMap Converters;
393 void initConverters();
396 void buildClosure(Closure &,
unsigned Reg);
399 void visitRegister(Closure &,
unsigned Reg,
RegDomain &Domain,
403 void reassign(
const Closure &
C,
RegDomain Domain)
const;
409 bool isReassignmentProfitable(
const Closure &C,
RegDomain Domain)
const;
412 double calculateCost(
const Closure &C,
RegDomain Domain)
const;
419 void X86DomainReassignment::visitRegister(Closure &
C,
unsigned Reg,
422 if (EnclosedEdges.count(Reg))
428 if (!
MRI->hasOneDef(Reg))
431 RegDomain RD = getDomain(
MRI->getRegClass(Reg),
MRI->getTargetRegisterInfo());
433 if (Domain == NoDomain)
442 void X86DomainReassignment::encloseInstr(Closure &C,
MachineInstr *
MI) {
443 auto I = EnclosedInstrs.find(MI);
444 if (
I != EnclosedInstrs.end()) {
445 if (
I->second != C.getID())
452 EnclosedInstrs[
MI] = C.getID();
453 C.addInstruction(MI);
458 for (
int i = 0; i != NumDomains; ++i) {
460 InstrConverterBase *IC = Converters.lookup({i, MI->
getOpcode()});
461 if (!IC || !IC->isLegal(MI,
TII))
467 double X86DomainReassignment::calculateCost(
const Closure &C,
469 assert(C.isLegal(DstDomain) &&
"Cannot calculate cost for illegal closure");
472 for (
auto *MI : C.instructions())
474 Converters.lookup({DstDomain, MI->getOpcode()})->getExtraCost(MI,
MRI);
478 bool X86DomainReassignment::isReassignmentProfitable(
const Closure &C,
480 return calculateCost(C, Domain) < 0.0;
483 void X86DomainReassignment::reassign(
const Closure &C,
RegDomain Domain)
const {
484 assert(C.isLegal(Domain) &&
"Cannot convert illegal closure");
489 for (
auto *MI : C.instructions())
490 if (Converters.lookup({Domain, MI->getOpcode()})
491 ->convertInstr(MI,
TII,
MRI))
496 for (
unsigned Reg : C.edges()) {
497 MRI->setRegClass(Reg, getDstRC(
MRI->getRegClass(Reg), Domain));
498 for (
auto &MO :
MRI->use_operands(Reg)) {
506 for (
auto MI : ToErase)
519 if (MemOpStart == -1)
523 for (
unsigned MemOpIdx = MemOpStart,
525 MemOpIdx < MemOpEnd; ++MemOpIdx) {
527 if (
Op.isReg() &&
Op.getReg() ==
Reg)
533 void X86DomainReassignment::buildClosure(Closure &C,
unsigned Reg) {
536 visitRegister(C, Reg, Domain, Worklist);
537 while (!Worklist.
empty()) {
541 if (!C.insertEdge(CurReg))
545 encloseInstr(C, DefMI);
555 for (
int OpIdx = 0; OpIdx < OpEnd; ++OpIdx) {
556 if (OpIdx == MemOp) {
562 if (!
Op.isReg() || !
Op.isUse())
564 visitRegister(C,
Op.getReg(), Domain, Worklist);
568 for (
auto &
UseMI :
MRI->use_nodbg_instructions(CurReg)) {
575 encloseInstr(C, &
UseMI);
577 for (
auto &DefOp :
UseMI.defs()) {
581 unsigned DefReg = DefOp.getReg();
586 visitRegister(C, DefReg, Domain, Worklist);
592 void X86DomainReassignment::initConverters() {
593 Converters[{MaskDomain, TargetOpcode::PHI}] =
594 new InstrIgnore(TargetOpcode::PHI);
596 Converters[{MaskDomain, TargetOpcode::IMPLICIT_DEF}] =
597 new InstrIgnore(TargetOpcode::IMPLICIT_DEF);
599 Converters[{MaskDomain, TargetOpcode::INSERT_SUBREG}] =
600 new InstrReplaceWithCopy(TargetOpcode::INSERT_SUBREG, 2);
602 Converters[{MaskDomain, TargetOpcode::COPY}] =
603 new InstrCOPYReplacer(TargetOpcode::COPY, MaskDomain, TargetOpcode::COPY);
605 auto createReplacerDstCOPY = [&](
unsigned From,
unsigned To) {
606 Converters[{MaskDomain, From}] =
new InstrReplacerDstCOPY(From, To);
609 createReplacerDstCOPY(X86::MOVZX32rm16, X86::KMOVWkm);
610 createReplacerDstCOPY(X86::MOVZX64rm16, X86::KMOVWkm);
612 createReplacerDstCOPY(X86::MOVZX32rr16, X86::KMOVWkk);
613 createReplacerDstCOPY(X86::MOVZX64rr16, X86::KMOVWkk);
616 createReplacerDstCOPY(X86::MOVZX16rm8, X86::KMOVBkm);
617 createReplacerDstCOPY(X86::MOVZX32rm8, X86::KMOVBkm);
618 createReplacerDstCOPY(X86::MOVZX64rm8, X86::KMOVBkm);
620 createReplacerDstCOPY(X86::MOVZX16rr8, X86::KMOVBkk);
621 createReplacerDstCOPY(X86::MOVZX32rr8, X86::KMOVBkk);
622 createReplacerDstCOPY(X86::MOVZX64rr8, X86::KMOVBkk);
625 auto createReplacer = [&](
unsigned From,
unsigned To) {
626 Converters[{MaskDomain, From}] =
new InstrReplacer(From, To);
629 createReplacer(X86::MOV16rm, X86::KMOVWkm);
630 createReplacer(X86::MOV16mr, X86::KMOVWmk);
631 createReplacer(X86::MOV16rr, X86::KMOVWkk);
632 createReplacer(X86::SHR16ri, X86::KSHIFTRWri);
633 createReplacer(X86::SHL16ri, X86::KSHIFTLWri);
634 createReplacer(X86::NOT16r, X86::KNOTWrr);
635 createReplacer(X86::OR16rr, X86::KORWrr);
636 createReplacer(X86::AND16rr, X86::KANDWrr);
637 createReplacer(X86::XOR16rr, X86::KXORWrr);
640 createReplacer(X86::MOV32rm, X86::KMOVDkm);
641 createReplacer(X86::MOV64rm, X86::KMOVQkm);
643 createReplacer(X86::MOV32mr, X86::KMOVDmk);
644 createReplacer(X86::MOV64mr, X86::KMOVQmk);
646 createReplacer(X86::MOV32rr, X86::KMOVDkk);
647 createReplacer(X86::MOV64rr, X86::KMOVQkk);
649 createReplacer(X86::SHR32ri, X86::KSHIFTRDri);
650 createReplacer(X86::SHR64ri, X86::KSHIFTRQri);
652 createReplacer(X86::SHL32ri, X86::KSHIFTLDri);
653 createReplacer(X86::SHL64ri, X86::KSHIFTLQri);
655 createReplacer(X86::ADD32rr, X86::KADDDrr);
656 createReplacer(X86::ADD64rr, X86::KADDQrr);
658 createReplacer(X86::NOT32r, X86::KNOTDrr);
659 createReplacer(X86::NOT64r, X86::KNOTQrr);
661 createReplacer(X86::OR32rr, X86::KORDrr);
662 createReplacer(X86::OR64rr, X86::KORQrr);
664 createReplacer(X86::AND32rr, X86::KANDDrr);
665 createReplacer(X86::AND64rr, X86::KANDQrr);
667 createReplacer(X86::ANDN32rr, X86::KANDNDrr);
668 createReplacer(X86::ANDN64rr, X86::KANDNQrr);
670 createReplacer(X86::XOR32rr, X86::KXORDrr);
671 createReplacer(X86::XOR64rr, X86::KXORQrr);
680 createReplacer(X86::ADD8rr, X86::KADDBrr);
681 createReplacer(X86::ADD16rr, X86::KADDWrr);
683 createReplacer(X86::AND8rr, X86::KANDBrr);
685 createReplacer(X86::MOV8rm, X86::KMOVBkm);
686 createReplacer(X86::MOV8mr, X86::KMOVBmk);
687 createReplacer(X86::MOV8rr, X86::KMOVBkk);
689 createReplacer(X86::NOT8r, X86::KNOTBrr);
691 createReplacer(X86::OR8rr, X86::KORBrr);
693 createReplacer(X86::SHR8ri, X86::KSHIFTRBri);
694 createReplacer(X86::SHL8ri, X86::KSHIFTLBri);
701 createReplacer(X86::XOR8rr, X86::KXORBrr);
705 bool X86DomainReassignment::runOnMachineFunction(
MachineFunction &MF) {
712 dbgs() <<
"***** Machine Function before Domain Reassignment *****\n");
718 if (!STI->hasAVX512())
722 assert(
MRI->isSSA() &&
"Expected MIR to be in SSA form");
724 TII = STI->getInstrInfo();
726 bool Changed =
false;
728 EnclosedEdges.clear();
729 EnclosedInstrs.clear();
731 std::vector<Closure> Closures;
734 unsigned ClosureID = 0;
735 for (
unsigned Idx = 0; Idx <
MRI->getNumVirtRegs(); ++Idx) {
739 if (!isGPR(
MRI->getRegClass(Reg)))
743 if (EnclosedEdges.count(Reg))
747 Closure
C(ClosureID++, {MaskDomain});
748 buildClosure(C, Reg);
751 if (!C.empty() && C.isLegal(MaskDomain))
752 Closures.push_back(std::move(C));
755 for (Closure &C : Closures) {
757 if (isReassignmentProfitable(C, MaskDomain)) {
758 reassign(C, MaskDomain);
759 ++NumClosuresConverted;
767 dbgs() <<
"***** Machine Function after Domain Reassignment *****\n");
774 "X86 Domain Reassignment Pass",
false,
false)
778 return new X86DomainReassignment();
void DeleteContainerSeconds(Container &C)
In a container of pairs (usually a map) whose second element is a pointer, deletes the second element...
void push_back(const T &Elt)
const MachineInstrBuilder & add(const MachineOperand &MO) const
static bool usedAsAddr(const MachineInstr &MI, unsigned Reg, const TargetInstrInfo *TII)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AddrNumOperands - Total number of operands in a memory reference.
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
Compute iterated dominance frontiers using a linear time algorithm.
iterator_range< mop_iterator > explicit_operands()
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Describe properties that are true of each instruction in the target description file.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
iterator_range< mop_iterator > operands()
FunctionPass * createX86DomainReassignmentPass()
Return a Machine IR pass that reassigns instruction chains from one domain to another, when profitable.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Access to explicit operands of the instruction.
const TargetRegisterClass * getRegClass(const MCInstrDesc &MCID, unsigned OpNum, const TargetRegisterInfo *TRI, const MachineFunction &MF) const
Given a machine instruction descriptor, returns the register class constraint for OpNum...
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
INITIALIZE_PASS(X86DomainReassignment, "x86-domain-reassignment", "X86 Domain Reassignment Pass", false, false) FunctionPass *llvm
Returns an instance of the Domain Reassignment pass.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
static int getID(struct InternalInstruction *insn, const void *miiArg)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
TargetInstrInfo - Interface to description of machine instruction set.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
const TargetRegisterInfo * getTargetRegisterInfo() const
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineInstrBuilder & UseMI
std::pair< iterator, bool > insert(const ValueT &V)
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
unsigned getOperandBias(const MCInstrDesc &Desc)
getOperandBias - compute whether all of the def operands are repeated in the uses and therefore shoul...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
void print(raw_ostream &OS, const SlotIndexes *=nullptr) const
print - Print out the MachineFunction in a format suitable for debugging to the specified stream...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void initializeX86DomainReassignmentPass(PassRegistry &)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
MachineInstrBuilder MachineInstrBuilder & DefMI
LLVM_NODISCARD T pop_back_val()
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
void setPreservesCFG()
This function should be called by the pass, iff they do not:
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
A range adaptor for a pair of iterators.
iterator_range< mop_iterator > implicit_operands()
const MachineBasicBlock * getParent() const
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.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
static cl::opt< bool > DisableX86DomainReassignment("disable-x86-domain-reassignment", cl::Hidden, cl::desc("X86: Disable Virtual Register Reassignment."), cl::init(false))
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
inst_range instructions(Function *F)
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
bool hasImplicitDefOfPhysReg(unsigned Reg, const MCRegisterInfo *MRI=nullptr) const
Return true if this instruction implicitly defines the specified physical register.
const MachineOperand & getOperand(unsigned i) const
unsigned createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
int getMemoryOperandNo(uint64_t TSFlags)
getMemoryOperandNo - The function returns the MCInst operand # for the first field of the memory oper...