33 #define DEBUG_TYPE "x86-domain-reassignment" 35 STATISTIC(NumClosuresConverted,
"Number of closures converted by the pass");
42 enum RegDomain { NoDomain = -1, GPRDomain, MaskDomain, OtherDomain, NumDomains };
45 return X86::GR64RegClass.hasSubClassEq(RC) ||
46 X86::GR32RegClass.hasSubClassEq(RC) ||
47 X86::GR16RegClass.hasSubClassEq(RC) ||
48 X86::GR8RegClass.hasSubClassEq(RC);
53 return X86::VK16RegClass.hasSubClassEq(RC);
68 assert(Domain == MaskDomain &&
"add domain");
69 if (X86::GR8RegClass.hasSubClassEq(SrcRC))
70 return &X86::VK8RegClass;
71 if (X86::GR16RegClass.hasSubClassEq(SrcRC))
72 return &X86::VK16RegClass;
73 if (X86::GR32RegClass.hasSubClassEq(SrcRC))
74 return &X86::VK32RegClass;
75 if (X86::GR64RegClass.hasSubClassEq(SrcRC))
76 return &X86::VK64RegClass;
82 class InstrConverterBase {
87 InstrConverterBase(
unsigned SrcOpcode) : SrcOpcode(SrcOpcode) {}
89 virtual ~InstrConverterBase() {}
95 "Wrong instruction passed to converter");
113 class InstrIgnore :
public InstrConverterBase {
115 InstrIgnore(
unsigned SrcOpcode) : InstrConverterBase(SrcOpcode) {}
119 assert(isLegal(MI, TII) &&
"Cannot convert instruction");
130 class InstrReplacer :
public InstrConverterBase {
135 InstrReplacer(
unsigned SrcOpcode,
unsigned DstOpcode)
136 : InstrConverterBase(SrcOpcode), DstOpcode(DstOpcode) {}
140 if (!InstrConverterBase::isLegal(MI, TII))
145 if (MO.isReg() && MO.isDef() && !MO.isDead() &&
153 assert(isLegal(MI, TII) &&
"Cannot convert instruction");
172 class InstrReplacerDstCOPY :
public InstrConverterBase {
176 InstrReplacerDstCOPY(
unsigned SrcOpcode,
unsigned DstOpcode)
177 : InstrConverterBase(SrcOpcode), DstOpcode(DstOpcode) {}
181 assert(isLegal(MI, TII) &&
"Cannot convert instruction");
189 for (
unsigned Idx = 1, End = MI->
getNumOperands(); Idx < End; ++Idx)
192 BuildMI(*MBB, MI, DL, TII->
get(TargetOpcode::COPY))
208 class InstrCOPYReplacer :
public InstrReplacer {
212 InstrCOPYReplacer(
unsigned SrcOpcode,
RegDomain DstDomain,
unsigned DstOpcode)
213 : InstrReplacer(SrcOpcode, DstOpcode), DstDomain(DstDomain) {}
217 if (!InstrConverterBase::isLegal(MI, TII))
224 (X86::GR8RegClass.
contains(DstReg) ||
225 X86::GR16RegClass.
contains(DstReg)))
229 (X86::GR8RegClass.
contains(SrcReg) ||
230 X86::GR16RegClass.
contains(SrcReg)))
251 if (OpDomain == DstDomain)
259 class InstrReplaceWithCopy :
public InstrConverterBase {
264 InstrReplaceWithCopy(
unsigned SrcOpcode,
unsigned SrcOpIdx)
265 : InstrConverterBase(SrcOpcode), SrcOpIdx(SrcOpIdx) {}
269 assert(isLegal(MI, TII) &&
"Cannot convert instruction");
271 TII->
get(TargetOpcode::COPY))
284 typedef std::pair<int, unsigned> InstrConverterBaseKeyTy;
287 InstrConverterBaseMap;
309 std::bitset<NumDomains> LegalDstDomains;
316 Closure(
unsigned ID, std::initializer_list<RegDomain> LegalDstDomainList) :
ID(ID) {
318 LegalDstDomains.set(
D);
322 void setAllIllegal() { LegalDstDomains.reset(); }
325 bool hasLegalDstDomain()
const {
return LegalDstDomains.any(); }
328 bool isLegal(
RegDomain RD)
const {
return LegalDstDomains[RD]; }
331 void setIllegal(
RegDomain RD) { LegalDstDomains[RD] =
false; }
335 bool insertEdge(
unsigned Reg) {
336 return Edges.
insert(Reg).second;
353 dbgs() <<
"Registers: ";
355 for (
unsigned Reg : Edges) {
361 dbgs() <<
"\n" <<
"Instructions:";
369 unsigned getID()
const {
399 return "X86 Domain Reassignment Pass";
404 InstrConverterBaseMap Converters;
407 void initConverters();
410 void buildClosure(Closure &,
unsigned Reg);
413 void visitRegister(Closure &,
unsigned Reg,
RegDomain &Domain,
417 void reassign(
const Closure &
C,
RegDomain Domain)
const;
423 bool isReassignmentProfitable(
const Closure &C,
RegDomain Domain)
const;
426 double calculateCost(
const Closure &C,
RegDomain Domain)
const;
433 void X86DomainReassignment::visitRegister(Closure &
C,
unsigned Reg,
436 if (EnclosedEdges.count(Reg))
442 if (!
MRI->hasOneDef(Reg))
445 RegDomain RD = getDomain(
MRI->getRegClass(Reg),
MRI->getTargetRegisterInfo());
447 if (Domain == NoDomain)
456 void X86DomainReassignment::encloseInstr(Closure &C,
MachineInstr *
MI) {
457 auto I = EnclosedInstrs.find(MI);
458 if (
I != EnclosedInstrs.end()) {
459 if (
I->second != C.getID())
466 EnclosedInstrs[
MI] = C.getID();
467 C.addInstruction(MI);
472 for (
int i = 0; i != NumDomains; ++i) {
474 InstrConverterBase *IC = Converters.lookup({i, MI->
getOpcode()});
475 if (!IC || !IC->isLegal(MI,
TII))
481 double X86DomainReassignment::calculateCost(
const Closure &C,
483 assert(C.isLegal(DstDomain) &&
"Cannot calculate cost for illegal closure");
486 for (
auto *MI : C.instructions())
488 Converters.lookup({DstDomain, MI->getOpcode()})->getExtraCost(MI,
MRI);
492 bool X86DomainReassignment::isReassignmentProfitable(
const Closure &C,
494 return calculateCost(C, Domain) < 0.0;
497 void X86DomainReassignment::reassign(
const Closure &C,
RegDomain Domain)
const {
498 assert(C.isLegal(Domain) &&
"Cannot convert illegal closure");
503 for (
auto *MI : C.instructions())
504 if (Converters.lookup({Domain, MI->getOpcode()})
505 ->convertInstr(MI,
TII,
MRI))
510 for (
unsigned Reg : C.edges()) {
511 MRI->setRegClass(Reg, getDstRC(
MRI->getRegClass(Reg), Domain));
512 for (
auto &MO :
MRI->use_operands(Reg)) {
520 for (
auto MI : ToErase)
533 if (MemOpStart == -1)
537 for (
unsigned MemOpIdx = MemOpStart,
539 MemOpIdx < MemOpEnd; ++MemOpIdx) {
541 if (
Op.isReg() &&
Op.getReg() ==
Reg)
547 void X86DomainReassignment::buildClosure(Closure &C,
unsigned Reg) {
550 visitRegister(C, Reg, Domain, Worklist);
551 while (!Worklist.
empty()) {
555 if (!C.insertEdge(CurReg))
557 EnclosedEdges.insert(Reg);
560 encloseInstr(C, DefMI);
570 for (
int OpIdx = 0; OpIdx < OpEnd; ++OpIdx) {
571 if (OpIdx == MemOp) {
577 if (!
Op.isReg() || !
Op.isUse())
579 visitRegister(C,
Op.getReg(), Domain, Worklist);
583 for (
auto &
UseMI :
MRI->use_nodbg_instructions(CurReg)) {
590 encloseInstr(C, &
UseMI);
592 for (
auto &DefOp :
UseMI.defs()) {
601 visitRegister(C, DefReg, Domain, Worklist);
607 void X86DomainReassignment::initConverters() {
608 Converters[{MaskDomain, TargetOpcode::PHI}] =
609 new InstrIgnore(TargetOpcode::PHI);
611 Converters[{MaskDomain, TargetOpcode::IMPLICIT_DEF}] =
612 new InstrIgnore(TargetOpcode::IMPLICIT_DEF);
614 Converters[{MaskDomain, TargetOpcode::INSERT_SUBREG}] =
615 new InstrReplaceWithCopy(TargetOpcode::INSERT_SUBREG, 2);
617 Converters[{MaskDomain, TargetOpcode::COPY}] =
618 new InstrCOPYReplacer(TargetOpcode::COPY, MaskDomain, TargetOpcode::COPY);
620 auto createReplacerDstCOPY = [&](
unsigned From,
unsigned To) {
621 Converters[{MaskDomain, From}] =
new InstrReplacerDstCOPY(From, To);
624 createReplacerDstCOPY(X86::MOVZX32rm16, X86::KMOVWkm);
625 createReplacerDstCOPY(X86::MOVZX64rm16, X86::KMOVWkm);
627 createReplacerDstCOPY(X86::MOVZX32rr16, X86::KMOVWkk);
628 createReplacerDstCOPY(X86::MOVZX64rr16, X86::KMOVWkk);
631 createReplacerDstCOPY(X86::MOVZX16rm8, X86::KMOVBkm);
632 createReplacerDstCOPY(X86::MOVZX32rm8, X86::KMOVBkm);
633 createReplacerDstCOPY(X86::MOVZX64rm8, X86::KMOVBkm);
635 createReplacerDstCOPY(X86::MOVZX16rr8, X86::KMOVBkk);
636 createReplacerDstCOPY(X86::MOVZX32rr8, X86::KMOVBkk);
637 createReplacerDstCOPY(X86::MOVZX64rr8, X86::KMOVBkk);
640 auto createReplacer = [&](
unsigned From,
unsigned To) {
641 Converters[{MaskDomain, From}] =
new InstrReplacer(From, To);
644 createReplacer(X86::MOV16rm, X86::KMOVWkm);
645 createReplacer(X86::MOV16mr, X86::KMOVWmk);
646 createReplacer(X86::MOV16rr, X86::KMOVWkk);
647 createReplacer(X86::SHR16ri, X86::KSHIFTRWri);
648 createReplacer(X86::SHL16ri, X86::KSHIFTLWri);
649 createReplacer(X86::NOT16r, X86::KNOTWrr);
650 createReplacer(X86::OR16rr, X86::KORWrr);
651 createReplacer(X86::AND16rr, X86::KANDWrr);
652 createReplacer(X86::XOR16rr, X86::KXORWrr);
655 createReplacer(X86::MOV32rm, X86::KMOVDkm);
656 createReplacer(X86::MOV64rm, X86::KMOVQkm);
658 createReplacer(X86::MOV32mr, X86::KMOVDmk);
659 createReplacer(X86::MOV64mr, X86::KMOVQmk);
661 createReplacer(X86::MOV32rr, X86::KMOVDkk);
662 createReplacer(X86::MOV64rr, X86::KMOVQkk);
664 createReplacer(X86::SHR32ri, X86::KSHIFTRDri);
665 createReplacer(X86::SHR64ri, X86::KSHIFTRQri);
667 createReplacer(X86::SHL32ri, X86::KSHIFTLDri);
668 createReplacer(X86::SHL64ri, X86::KSHIFTLQri);
670 createReplacer(X86::ADD32rr, X86::KADDDrr);
671 createReplacer(X86::ADD64rr, X86::KADDQrr);
673 createReplacer(X86::NOT32r, X86::KNOTDrr);
674 createReplacer(X86::NOT64r, X86::KNOTQrr);
676 createReplacer(X86::OR32rr, X86::KORDrr);
677 createReplacer(X86::OR64rr, X86::KORQrr);
679 createReplacer(X86::AND32rr, X86::KANDDrr);
680 createReplacer(X86::AND64rr, X86::KANDQrr);
682 createReplacer(X86::ANDN32rr, X86::KANDNDrr);
683 createReplacer(X86::ANDN64rr, X86::KANDNQrr);
685 createReplacer(X86::XOR32rr, X86::KXORDrr);
686 createReplacer(X86::XOR64rr, X86::KXORQrr);
695 createReplacer(X86::ADD8rr, X86::KADDBrr);
696 createReplacer(X86::ADD16rr, X86::KADDWrr);
698 createReplacer(X86::AND8rr, X86::KANDBrr);
700 createReplacer(X86::MOV8rm, X86::KMOVBkm);
701 createReplacer(X86::MOV8mr, X86::KMOVBmk);
702 createReplacer(X86::MOV8rr, X86::KMOVBkk);
704 createReplacer(X86::NOT8r, X86::KNOTBrr);
706 createReplacer(X86::OR8rr, X86::KORBrr);
708 createReplacer(X86::SHR8ri, X86::KSHIFTRBri);
709 createReplacer(X86::SHL8ri, X86::KSHIFTLBri);
716 createReplacer(X86::XOR8rr, X86::KXORBrr);
720 bool X86DomainReassignment::runOnMachineFunction(
MachineFunction &MF) {
727 dbgs() <<
"***** Machine Function before Domain Reassignment *****\n");
736 if (!STI->hasAVX512() || !STI->hasBWI())
740 assert(
MRI->isSSA() &&
"Expected MIR to be in SSA form");
742 TII = STI->getInstrInfo();
744 bool Changed =
false;
746 EnclosedEdges.clear();
747 EnclosedInstrs.clear();
749 std::vector<Closure> Closures;
752 unsigned ClosureID = 0;
753 for (
unsigned Idx = 0; Idx <
MRI->getNumVirtRegs(); ++Idx) {
757 if (!isGPR(
MRI->getRegClass(Reg)))
761 if (EnclosedEdges.count(Reg))
765 Closure
C(ClosureID++, {MaskDomain});
766 buildClosure(C, Reg);
769 if (!C.empty() && C.isLegal(MaskDomain))
770 Closures.push_back(std::move(C));
773 for (Closure &C : Closures) {
775 if (isReassignmentProfitable(C, MaskDomain)) {
776 reassign(C, MaskDomain);
777 ++NumClosuresConverted;
785 dbgs() <<
"***** Machine Function after Domain Reassignment *****\n");
792 "X86 Domain Reassignment Pass",
false,
false)
796 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...
const MachineInstrBuilder & add(const MachineOperand &MO) const
static bool usedAsAddr(const MachineInstr &MI, unsigned Reg, const TargetInstrInfo *TII)
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
iterator_range< mop_iterator > explicit_operands()
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Describe properties that are true of each instruction in the target description file.
AddrNumOperands - Total number of operands in a memory reference.
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
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
iterator_range< mop_iterator > operands()
FunctionPass * createX86DomainReassignmentPass()
Return a Machine IR pass that reassigns instruction chains from one domain to another, when profitable.
return AArch64::GPR64RegClass contains(Reg)
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
virtual 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...
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.
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)
Compute whether all of the def operands are repeated in the uses and therefore should be skipped...
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.
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
BlockVerifier::State From
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
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.
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())
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
StringRef - Represent a constant reference to a string, i.e.
inst_range instructions(Function *F)
Register getReg() const
getReg - Returns the register number.
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
Wrapper class representing virtual and physical registers.
int getMemoryOperandNo(uint64_t TSFlags)
The function returns the MCInst operand # for the first field of the memory operand.