90 #define DEBUG_TYPE "si-wqm" 104 explicit PrintState(
int State) : State(State) {}
109 if (PS.State & StateWQM)
111 if (PS.State & StateWWM) {
112 if (PS.State & StateWQM)
116 if (PS.State & StateExact) {
117 if (PS.State & (StateWQM | StateWWM))
142 WorkItem() =
default;
165 std::vector<WorkItem> &Worklist);
166 void markInstructionUses(
const MachineInstr &MI,
char Flag,
167 std::vector<WorkItem> &Worklist);
168 char scanInstructions(
MachineFunction &MF, std::vector<WorkItem> &Worklist);
169 void propagateInstruction(
MachineInstr &MI, std::vector<WorkItem> &Worklist);
173 bool requiresCorrectState(
const MachineInstr &MI)
const;
182 unsigned SaveWQM,
unsigned LiveMaskReg);
191 void lowerLiveMaskQueries(
unsigned LiveMaskReg);
192 void lowerCopyInstrs();
202 StringRef getPassName()
const override {
return "SI Whole Quad Mode"; }
226 return new SIWholeQuadMode;
231 for (
const auto &BII : Blocks) {
234 <<
" InNeeds = " << PrintState(BII.second.InNeeds)
235 <<
", Needs = " << PrintState(BII.second.Needs)
236 <<
", OutNeeds = " << PrintState(BII.second.OutNeeds) <<
"\n\n";
239 auto III = Instructions.find(&MI);
240 if (III == Instructions.end())
243 dbgs() <<
" " << MI <<
" Needs = " << PrintState(III->second.Needs)
244 <<
", OutNeeds = " << PrintState(III->second.OutNeeds) <<
'\n';
251 std::vector<WorkItem> &Worklist) {
252 InstrInfo &II = Instructions[&
MI];
254 assert(!(Flag & StateExact) && Flag != 0);
260 Flag &= ~II.Disabled;
264 if ((II.Needs & Flag) == Flag)
268 Worklist.push_back(&MI);
272 void SIWholeQuadMode::markInstructionUses(
const MachineInstr &MI,
char Flag,
273 std::vector<WorkItem> &Worklist) {
275 if (!
Use.isReg() || !
Use.isUse())
284 if (Reg == AMDGPU::EXEC || Reg == AMDGPU::EXEC_LO)
288 LiveRange &LR = LIS->getRegUnit(*RegUnit);
298 markInstruction(*LIS->getInstructionFromIndex(Value->
def),
Flag,
306 markInstruction(
DefMI, Flag, Worklist);
313 std::vector<WorkItem> &Worklist) {
314 char GlobalFlags = 0;
324 for (
auto BI = RPOT.
begin(), BE = RPOT.
end(); BI != BE; ++BI) {
326 BlockInfo &BBI = Blocks[&MBB];
328 for (
auto II = MBB.
begin(),
IE = MBB.
end(); II !=
IE; ++II) {
330 InstrInfo &III = Instructions[&
MI];
334 if (
TII->isWQM(Opcode)) {
338 markInstructionUses(MI, StateWQM, Worklist);
339 GlobalFlags |= StateWQM;
345 LowerToCopyInstrs.push_back(&MI);
346 }
else if (Opcode == AMDGPU::SOFT_WQM) {
347 LowerToCopyInstrs.push_back(&MI);
350 }
else if (Opcode == AMDGPU::WWM) {
354 markInstructionUses(MI, StateWWM, Worklist);
355 GlobalFlags |= StateWWM;
356 LowerToMovInstrs.push_back(&MI);
358 }
else if (Opcode == AMDGPU::V_SET_INACTIVE_B32 ||
359 Opcode == AMDGPU::V_SET_INACTIVE_B64) {
360 III.Disabled = StateWWM;
362 if (Inactive.
isReg()) {
364 LowerToCopyInstrs.push_back(&MI);
369 markInstruction(
DefMI, StateWWM, Worklist);
375 }
else if (
TII->isDisableWQM(MI)) {
376 BBI.Needs |= StateExact;
377 if (!(BBI.InNeeds & StateExact)) {
378 BBI.InNeeds |= StateExact;
379 Worklist.push_back(&MBB);
381 GlobalFlags |= StateExact;
382 III.Disabled = StateWQM | StateWWM;
385 if (Opcode == AMDGPU::SI_PS_LIVE) {
386 LiveMaskQueries.push_back(&MI);
387 }
else if (WQMOutputs) {
398 TRI->hasVectorRegisters(
TRI->getPhysRegClass(Reg))) {
409 markInstruction(MI, Flags, Worklist);
410 GlobalFlags |= Flags;
418 if (GlobalFlags & StateWQM) {
420 markInstruction(*MI, StateWQM, Worklist);
422 markInstruction(*MI, StateWQM, Worklist);
428 void SIWholeQuadMode::propagateInstruction(
MachineInstr &MI,
429 std::vector<WorkItem>& Worklist) {
431 InstrInfo II = Instructions[&
MI];
432 BlockInfo &BI = Blocks[MBB];
436 if ((II.OutNeeds & StateWQM) && !(II.Disabled & StateWQM) &&
438 Instructions[&
MI].Needs = StateWQM;
443 if (II.Needs & StateWQM) {
444 BI.Needs |= StateWQM;
445 if (!(BI.InNeeds & StateWQM)) {
446 BI.InNeeds |= StateWQM;
447 Worklist.push_back(MBB);
453 char InNeeds = (II.Needs & ~StateWWM) | II.OutNeeds;
454 if (!PrevMI->isPHI()) {
455 InstrInfo &PrevII = Instructions[PrevMI];
456 if ((PrevII.OutNeeds | InNeeds) != PrevII.OutNeeds) {
457 PrevII.OutNeeds |= InNeeds;
458 Worklist.push_back(PrevMI);
464 assert(!(II.Needs & StateExact));
467 markInstructionUses(MI, II.Needs, Worklist);
471 if (II.Needs & StateWWM)
472 BI.Needs |= StateWWM;
476 std::vector<WorkItem>& Worklist) {
477 BlockInfo BI = Blocks[&MBB];
482 InstrInfo &LastII = Instructions[LastMI];
483 if ((LastII.OutNeeds | BI.OutNeeds) != LastII.OutNeeds) {
484 LastII.OutNeeds |= BI.OutNeeds;
485 Worklist.push_back(LastMI);
491 BlockInfo &PredBI = Blocks[Pred];
492 if ((PredBI.OutNeeds | BI.InNeeds) == PredBI.OutNeeds)
495 PredBI.OutNeeds |= BI.InNeeds;
496 PredBI.InNeeds |= BI.InNeeds;
497 Worklist.push_back(Pred);
502 BlockInfo &SuccBI = Blocks[Succ];
503 if ((SuccBI.InNeeds | BI.OutNeeds) == SuccBI.InNeeds)
506 SuccBI.InNeeds |= BI.OutNeeds;
507 Worklist.push_back(Succ);
512 std::vector<WorkItem> Worklist;
513 char GlobalFlags = scanInstructions(MF, Worklist);
515 while (!Worklist.empty()) {
516 WorkItem WI = Worklist.back();
520 propagateInstruction(*WI.MI, Worklist);
522 propagateBlock(*WI.MBB, Worklist);
533 bool SIWholeQuadMode::requiresCorrectState(
const MachineInstr &MI)
const {
538 if (
TII->isScalarUnit(MI))
561 Register SaveReg =
MRI->createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
565 .addReg(AMDGPU::SCC);
570 LIS->InsertMachineInstrInMaps(*Save);
571 LIS->InsertMachineInstrInMaps(*Restore);
572 LIS->createAndComputeVirtRegInterval(SaveReg);
584 return PreferLast ? Last : First;
587 auto MBBE = MBB.
end();
588 SlotIndex FirstIdx = First != MBBE ? LIS->getInstructionIndex(*First)
589 : LIS->getMBBEndIdx(&MBB);
591 Last != MBBE ? LIS->getInstructionIndex(*Last) : LIS->getMBBEndIdx(&MBB);
592 SlotIndex Idx = PreferLast ? LastIdx : FirstIdx;
615 if (
MachineInstr *MI = LIS->getInstructionFromIndex(Idx))
618 assert(Idx == LIS->getMBBEndIdx(&MBB));
623 MBBI = saveSCC(MBB, MBBI);
630 unsigned SaveWQM,
unsigned LiveMaskReg) {
635 AMDGPU::S_AND_SAVEEXEC_B32 : AMDGPU::S_AND_SAVEEXEC_B64),
639 unsigned Exec =
ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
641 AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64),
647 LIS->InsertMachineInstrInMaps(*MI);
655 unsigned Exec =
ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
661 AMDGPU::S_WQM_B32 : AMDGPU::S_WQM_B64),
666 LIS->InsertMachineInstrInMaps(*MI);
677 LIS->InsertMachineInstrInMaps(*MI);
682 unsigned SavedOrig) {
687 ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC)
689 LIS->InsertMachineInstrInMaps(*MI);
694 auto BII = Blocks.find(&MBB);
695 if (BII == Blocks.end())
698 const BlockInfo &BI = BII->second;
702 if (!isEntry && BI.Needs == StateWQM && BI.OutNeeds != StateExact)
708 unsigned SavedWQMReg = 0;
709 unsigned SavedNonWWMReg = 0;
710 bool WQMFromExec = isEntry;
711 char State = (isEntry || !(BI.InNeeds & StateWQM)) ? StateExact : StateWQM;
712 char NonWWMState = 0;
730 char Needs = StateExact | StateWQM;
744 if (requiresCorrectState(MI)) {
745 auto III = Instructions.find(&MI);
746 if (III != Instructions.end()) {
747 if (III->second.Needs & StateWWM)
749 else if (III->second.Needs & StateWQM)
752 Needs &= ~III->second.Disabled;
753 OutNeeds = III->second.OutNeeds;
758 Needs = StateExact | StateWQM | StateWWM;
764 if (MI.
getOpcode() == AMDGPU::SI_ELSE && BI.OutNeeds == StateExact)
770 if (BI.OutNeeds & StateWQM)
772 else if (BI.OutNeeds == StateExact)
775 Needs = StateWQM | StateExact;
779 if (!(Needs & State)) {
781 if (State == StateWWM || Needs == StateWWM) {
790 prepareInsertion(MBB, First, II, Needs == StateWQM,
791 Needs == StateExact || WQMFromExec);
793 if (State == StateWWM) {
795 fromWWM(MBB, Before, SavedNonWWMReg);
799 if (Needs == StateWWM) {
801 SavedNonWWMReg =
MRI->createVirtualRegister(BoolRC);
802 toWWM(MBB, Before, SavedNonWWMReg);
805 if (State == StateWQM && (Needs & StateExact) && !(Needs & StateWQM)) {
806 if (!WQMFromExec && (OutNeeds & StateWQM))
807 SavedWQMReg =
MRI->createVirtualRegister(BoolRC);
809 toExact(MBB, Before, SavedWQMReg, LiveMaskReg);
811 }
else if (State == StateExact && (Needs & StateWQM) &&
812 !(Needs & StateExact)) {
813 assert(WQMFromExec == (SavedWQMReg == 0));
815 toWQM(MBB, Before, SavedWQMReg);
818 LIS->createAndComputeVirtRegInterval(SavedWQMReg);
830 if (Needs != (StateExact | StateWQM | StateWWM)) {
831 if (Needs != (StateExact | StateWQM))
842 void SIWholeQuadMode::lowerLiveMaskQueries(
unsigned LiveMaskReg) {
848 .addReg(LiveMaskReg);
850 LIS->ReplaceMachineInstrInMaps(*MI, *Copy);
855 void SIWholeQuadMode::lowerCopyInstrs() {
861 if (
TRI->isVGPR(*
MRI, Reg)) {
863 ?
MRI->getRegClass(Reg)
864 :
TRI->getPhysRegClass(Reg);
866 const unsigned MovOp =
TII->getMovOpcode(regClass);
876 if (MI->
getOpcode() == AMDGPU::V_SET_INACTIVE_B32 ||
877 MI->
getOpcode() == AMDGPU::V_SET_INACTIVE_B64) {
894 Instructions.clear();
896 LiveMaskQueries.clear();
897 LowerToCopyInstrs.clear();
898 LowerToMovInstrs.clear();
903 TII =
ST->getInstrInfo();
906 LIS = &getAnalysis<LiveIntervals>();
908 char GlobalFlags = analyzeFunction(MF);
909 unsigned LiveMaskReg = 0;
910 unsigned Exec =
ST->isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
911 if (!(GlobalFlags & StateWQM)) {
912 lowerLiveMaskQueries(Exec);
913 if (!(GlobalFlags & StateWWM) && LowerToCopyInstrs.empty() && LowerToMovInstrs.empty())
914 return !LiveMaskQueries.empty();
920 if (GlobalFlags & StateExact || !LiveMaskQueries.empty()) {
921 LiveMaskReg =
MRI->createVirtualRegister(
TRI->getBoolRC());
923 TII->
get(AMDGPU::COPY), LiveMaskReg)
925 LIS->InsertMachineInstrInMaps(*MI);
928 lowerLiveMaskQueries(LiveMaskReg);
930 if (GlobalFlags == StateWQM) {
933 AMDGPU::S_WQM_B32 : AMDGPU::S_WQM_B64),
948 for (
auto BII : Blocks)
949 processBlock(*BII.first, LiveMaskReg, BII.first == &*MF.
begin());
bool isPHIDef() const
Returns true if this value is defined by a PHI instruction (or was, PHI instructions may have been el...
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
AMDGPU specific subclass of TargetSubtarget.
SlotIndex getBaseIndex() const
Returns the base index for associated with this index.
SlotIndex def
The index of the defining instruction.
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 > uses()
Returns a range that includes all operands that are register uses.
INITIALIZE_PASS_BEGIN(SIWholeQuadMode, DEBUG_TYPE, "SI Whole Quad Mode", false, false) INITIALIZE_PASS_END(SIWholeQuadMode
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
bool isTransient() const
Return true if this is a transient instruction that is either very likely to be eliminated during reg...
This represents a simple continuous liveness interval for a value.
unsigned const TargetRegisterInfo * TRI
VNInfo - Value Number Information.
iterator_range< succ_iterator > successors()
This class represents the liveness of a register, stack slot, etc.
static const AMDGPUSubtarget & get(const MachineFunction &MF)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
SlotIndex getNextIndex() const
Returns the next index.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
A Use represents the edge between a Value definition and its users.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void untieRegOperand(unsigned OpIdx)
Break any tie involving OpIdx.
reverse_iterator rbegin()
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
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.
FunctionPass * createSIWholeQuadModePass()
Represent the analysis usage information of a pass.
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
void setImm(int64_t immVal)
FunctionPass class - This class is used to implement most global optimizations.
iterator_range< pred_iterator > predecessors()
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
const MachineBasicBlock & front() const
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
MachineInstrBuilder MachineInstrBuilder & DefMI
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.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
Interface definition for SIInstrInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const Segment * getSegmentContaining(SlotIndex Idx) const
Return the segment that contains the specified index, or null if there is none.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
SlotIndex - An opaque wrapper around machine indexes.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Wrapper class representing virtual and physical registers.
const SIRegisterInfo * getRegisterInfo() const override