39#define DEBUG_TYPE "x86-fast-pre-tile-config"
46class X86FastPreTileConfigImpl {
48 X86FastPreTileConfigImpl() : StackSlotForVirtReg(-1) {}
53 const X86Subtarget *ST =
nullptr;
54 const TargetInstrInfo *TII =
nullptr;
55 MachineRegisterInfo *MRI =
nullptr;
56 X86MachineFunctionInfo *X86FI =
nullptr;
57 MachineFrameInfo *MFI =
nullptr;
58 const TargetRegisterInfo *TRI =
nullptr;
59 MachineBasicBlock *MBB =
nullptr;
66 DenseMap<MachineInstr *, struct PHIInfo> VisitedPHIs;
69 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
73 BitVector MayLiveAcrossBlocks;
75 int getStackSpaceFor(
Register VirtReg);
76 void InitializeTileConfigStackSpace();
77 bool mayLiveOut(
Register VirtReg, MachineInstr *CfgMI);
80 MachineOperand *RowMO, MachineOperand *ColMO);
81 void canonicalizePHIs(MachineBasicBlock &MBB);
82 void convertPHI(MachineBasicBlock *MBB, MachineInstr &
PHI);
83 void convertPHIs(MachineBasicBlock &MBB);
84 bool configBasicBlock(MachineBasicBlock &MBB);
89 X86FastPreTileConfigLegacy() : MachineFunctionPass(ID) {}
92 StringRef getPassName()
const override {
93 return "Fast Tile Register Preconfigure";
104char X86FastPreTileConfigLegacy::ID = 0;
107 "Fast Tile Register Preconfigure",
false,
false)
114 auto MBBEnd =
MBB.end();
119 for (; &*
I !=
A && &*
I !=
B; ++
I)
127int X86FastPreTileConfigImpl::getStackSpaceFor(
Register VirtReg) {
129 int SS = StackSlotForVirtReg[VirtReg];
136 unsigned Size =
TRI->getSpillSize(RC);
141 StackSlotForVirtReg[VirtReg] = FrameIdx;
148bool X86FastPreTileConfigImpl::mayLiveOut(
Register VirtReg,
149 MachineInstr *CfgMI) {
154 if (UseInst.getParent() !=
MBB) {
173void X86FastPreTileConfigImpl::InitializeTileConfigStackSpace() {
174 MachineBasicBlock &
MBB = MF->
front();
200 unsigned StoreOpc = ST->
hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
225 int FI = getStackSpaceFor(VirtReg);
237 const unsigned NumRows = 16;
238 const unsigned RowSize = 64;
240 for (
unsigned Row = 0; Row < NumRows; ++Row) {
250 MachineInstrBuilder MIB =
268 Register OrigReg, MachineOperand *RowMO,
269 MachineOperand *ColMO) {
270 int FI = getStackSpaceFor(OrigReg);
291 const unsigned NumRows = 16;
292 const unsigned RowSize = 64;
301 for (
unsigned Row = 0; Row < NumRows; ++Row) {
308 TII->get(X86::VMOVUPSZrm), ScratchZMM);
317 PartialTile = Filled;
324 unsigned Opc = X86::PTILELOADDV;
328 TII->get(X86::MOV64ri), StrideReg)
358 if (
Reg.isVirtual() &&
359 (
MRI->getRegClass(
Reg)->getID() == X86::TILERegClassID)) {
363 if (
Reg >= X86::TMM0 &&
Reg <= X86::TMM7)
371 if (
MI.isDebugInstr() ||
MI.getNumOperands() < 3 || !
MI.isPseudo())
387 }
else if (
MI->isCopy()) {
388 TileReg =
MI->getOperand(1).getReg();
394 assert(
MI->isPHI() &&
"Unexpected PHI when get shape.");
412void X86FastPreTileConfigImpl::convertPHI(MachineBasicBlock *
MBB,
421 TII->get(X86::PHI), StackAddrReg);
424 TII->get(X86::PHI), RowReg);
427 TII->get(X86::PHI), ColReg);
429 VisitedPHIs[&
PHI] = {RowReg, ColReg, StackAddrReg};
431 for (
unsigned I = 1,
E =
PHI.getNumOperands();
I !=
E;
I += 2) {
438 MachineBasicBlock *InMBB =
PHI.getOperand(
I + 1).getMBB();
442 if (TileDefMI->
isPHI()) {
444 if (
auto It = VisitedPHIs.find(TileDefMI);
445 It != VisitedPHIs.end()) {
457 Register InStackAddrReg = It->second.StackAddr;
464 convertPHI(TileDefMI->
getParent(), *TileDefMI);
469 TileLoad->
getOpcode() == X86::ACE_TILERELOADV));
489 int FI = getStackSpaceFor(InTileReg);
493 TII->get(X86::LEA64r), InStackAddrReg)
505 TII->get(X86::ACE_TILERELOADV), TileReg)
513 MachineInstr *NewMI =
515 TII->get(X86::PTILELOADDV), TileReg)
522 PHI.eraseFromParent();
523 VisitedPHIs.erase(&
PHI);
533void X86FastPreTileConfigImpl::canonicalizePHIs(MachineBasicBlock &
MBB) {
534 SmallVector<MachineInstr *, 8> PHIs;
536 for (MachineInstr &
MI :
MBB) {
553 while (!PHIs.
empty()) {
558 MachineOperand *InMO =
nullptr;
559 MachineInstr *
DefMI =
nullptr;
560 for (
unsigned I = 1,
E =
PHI->getNumOperands();
I !=
E;
I += 2) {
562 MachineBasicBlock *InMBB =
PHI->getOperand(
I + 1).getMBB();
567 InMO = &
PHI->getOperand(
I);
578 MachineBasicBlock *InMBB =
PHI->getOperand(
I + 1).getMBB();
588void X86FastPreTileConfigImpl::convertPHIs(MachineBasicBlock &
MBB) {
589 SmallVector<MachineInstr *, 8> PHIs;
590 for (MachineInstr &
MI :
MBB) {
597 while (!PHIs.
empty()) {
600 convertPHI(&
MBB, *
MI);
606bool X86FastPreTileConfigImpl::configBasicBlock(MachineBasicBlock &
MBB) {
609 MachineInstr *LastShapeMI =
nullptr;
610 MachineInstr *LastTileCfg =
nullptr;
611 bool HasUnconfigTile =
false;
616 auto Config = [&](MachineInstr &Before) {
623 LastShapeMI =
nullptr;
626 auto HasTileOperand = [](MachineRegisterInfo *
MRI, MachineInstr &
MI) {
627 for (
const MachineOperand &MO :
MI.operands()) {
652 if (HasTileOperand(
MRI,
MI))
653 HasUnconfigTile =
true;
656 if (
MI.isCall() && HasUnconfigTile) {
663 auto UseIt =
MI.getIterator();
664 while (UseIt !=
MBB.
end()) {
665 if (HasTileOperand(
MRI, *UseIt))
672 HasUnconfigTile =
false;
714 MachineOperand *RowMO = &
MI.getOperand(1);
715 MachineOperand *ColMO = &
MI.getOperand(2);
738 if (mayLiveOut(TileReg, LastTileCfg))
739 spill(++
MI.getIterator(), TileReg,
false);
741 for (MachineInstr &
UseMI :
748 reload(
UseMI.getIterator(), TileReg, RowMO, ColMO);
753 reload(
UseMI.getIterator(), TileReg, RowMO, ColMO);
759 if (HasUnconfigTile) {
760 MachineInstr *Before;
761 if (LastShapeMI ==
nullptr || LastShapeMI->
isPHI())
772bool X86FastPreTileConfigImpl::runOnMachineFunction(
MachineFunction &MFunc) {
773 X86FI = MFunc.
getInfo<X86MachineFunctionInfo>();
789 StackSlotForVirtReg.resize(NumVirtRegs);
790 MayLiveAcrossBlocks.
clear();
794 MayLiveAcrossBlocks.
resize(NumVirtRegs * 3);
799 for (MachineBasicBlock &
MBB : MFunc)
800 canonicalizePHIs(
MBB);
805 ReversePostOrderTraversal<MachineFunction *> RPOT(MF);
806 for (MachineBasicBlock *
MBB : RPOT) {
808 Change |= configBasicBlock(*
MBB);
812 InitializeTileConfigStackSpace();
814 StackSlotForVirtReg.clear();
819 return new X86FastPreTileConfigLegacy();
822bool X86FastPreTileConfigLegacy::runOnMachineFunction(
MachineFunction &MF) {
823 X86FastPreTileConfigImpl Impl;
824 return Impl.runOnMachineFunction(MF);
830 X86FastPreTileConfigImpl Impl;
831 bool Changed = Impl.runOnMachineFunction(MF);
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
MachineRegisterInfo & MRI
static bool dominates(InstrPosIndexes &PosIndexes, const MachineInstr &A, const MachineInstr &B)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static bool isTileRegDef(MachineRegisterInfo *MRI, MachineInstr &MI)
static ShapeT getShape(MachineRegisterInfo *MRI, Register TileReg)
static bool isTileDef(MachineRegisterInfo *MRI, MachineInstr &MI)
static bool isTileRegister(MachineRegisterInfo *MRI, Register Reg)
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
bool test(unsigned Idx) const
Returns true if bit Idx is set.
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
void clear()
Removes all bits from the bitvector.
BitVector & set()
Set all bits in the bitvector.
FunctionPass class - This class is used to implement most global optimizations.
MachineInstrBundleIterator< const MachineInstr > const_iterator
LLVM_ABI iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
LLVM_ABI int CreateSpillStackObject(uint64_t Size, Align Alignment, TargetStackID::Value StackID=TargetStackID::Default)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
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 & addFrameIndex(int Idx) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
unsigned getNumOperands() const
Retuns the total number of operands.
void setImplicitPhysRegDefsDead()
Mark the implicit physreg defs named by the instruction description as dead.
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.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
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...
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(Register Reg) const
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Wrapper class representing virtual and physical registers.
unsigned virtRegIndex() const
Convert a virtual register number to a 0-based index.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
MachineOperand * getRow() const
MachineOperand * getCol() const
void push_back(const T &Elt)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
ACEProgModelEnum getACEProgModel() const
AMXProgModelEnum getAMXProgModel() const
unsigned getTileConfigSize() const
Align getTileConfigAlignment() const
const X86InstrInfo * getInstrInfo() const override
const X86RegisterInfo * getRegisterInfo() const override
self_iterator getIterator()
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Kill
The last use of a register.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
static const MachineInstrBuilder & addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset=0, bool mem=true)
addFrameReference - This function is used to add a reference to the base of an abstract object on the...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static const MachineInstrBuilder & addOffset(const MachineInstrBuilder &MIB, int Offset)
FunctionPass * createX86FastPreTileConfigLegacyPass()
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...
LLVM_ABI 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.
MCRegisterClass TargetRegisterClass