26 #define DEBUG_TYPE "execution-deps-fix"
31 ExecutionDepsFix::regIndices(
unsigned Reg)
const {
32 assert(Reg < AliasMap.size() &&
"Invalid register");
33 const auto &Entry = AliasMap[
Reg];
43 assert(dv->
Refs == 0 &&
"Reference count wasn't cleared");
44 assert(!dv->
Next &&
"Chained DomainValue shouldn't have been recycled");
87 void ExecutionDepsFix::setLiveReg(
int rx,
DomainValue *dv) {
88 assert(
unsigned(rx) < NumRegs &&
"Invalid index");
89 assert(LiveRegs &&
"Must enter basic block first.");
91 if (LiveRegs[rx].
Value == dv)
93 if (LiveRegs[rx].
Value)
94 release(LiveRegs[rx].Value);
95 LiveRegs[rx].
Value = retain(dv);
99 void ExecutionDepsFix::kill(
int rx) {
100 assert(
unsigned(rx) < NumRegs &&
"Invalid index");
101 assert(LiveRegs &&
"Must enter basic block first.");
102 if (!LiveRegs[rx].
Value)
105 release(LiveRegs[rx].Value);
106 LiveRegs[rx].
Value =
nullptr;
110 void ExecutionDepsFix::force(
int rx,
unsigned domain) {
111 assert(
unsigned(rx) < NumRegs &&
"Invalid index");
112 assert(LiveRegs &&
"Must enter basic block first.");
117 collapse(dv, domain);
122 assert(LiveRegs[rx].Value &&
"Not live after collapse?");
127 setLiveReg(rx, alloc(domain));
133 void ExecutionDepsFix::collapse(
DomainValue *dv,
unsigned domain) {
137 while (!dv->
Instrs.empty())
142 if (LiveRegs && dv->
Refs > 1)
143 for (
unsigned rx = 0; rx != NumRegs; ++rx)
144 if (LiveRegs[rx].Value == dv)
145 setLiveReg(rx, alloc(domain));
166 for (
unsigned rx = 0; rx != NumRegs; ++rx) {
167 assert(LiveRegs &&
"no space allocated for live registers");
168 if (LiveRegs[rx].Value == B)
185 LiveRegs =
new LiveReg[NumRegs];
188 for (
unsigned rx = 0; rx != NumRegs; ++rx) {
189 LiveRegs[rx].
Value =
nullptr;
190 LiveRegs[rx].
Def = -(1 << 20);
195 for (
const auto &LI : MBB->
liveins()) {
196 for (
int rx : regIndices(LI.PhysReg)) {
200 LiveRegs[rx].
Def = -1;
209 pe = MBB->
pred_end(); pi != pe; ++pi) {
210 auto fi = MBBInfos.
find(*pi);
212 "Should have pre-allocated MBBInfos for all MBBs");
213 LiveReg *Incoming = fi->second.OutRegs;
216 if (Incoming ==
nullptr) {
220 for (
unsigned rx = 0; rx != NumRegs; ++rx) {
227 if (!LiveRegs[rx].Value) {
233 if (LiveRegs[rx].Value->isCollapsed()) {
237 collapse(pdv, Domain);
243 merge(LiveRegs[rx].Value, pdv);
250 << (!isBlockDone(MBB) ?
": incomplete\n" :
": all preds known\n"));
254 assert(LiveRegs &&
"Must enter basic block first.");
255 LiveReg *OldOutRegs = MBBInfos[MBB].OutRegs;
257 MBBInfos[MBB].OutRegs = LiveRegs;
263 for (
unsigned i = 0, e = NumRegs; i != e; ++i)
264 LiveRegs[i].
Def -= CurInstr;
268 for (
unsigned i = 0, e = NumRegs; i != e; ++i)
269 release(OldOutRegs[i].Value);
280 visitSoftInstr(MI, DomP.second);
282 visitHardInstr(MI, DomP.first);
293 bool ExecutionDepsFix::pickBestRegisterForUndef(
MachineInstr *MI,
294 unsigned OpIdx,
unsigned Pref) {
298 unsigned OriginalReg = MO.
getReg();
301 if (AliasMap[OriginalReg].size() != 1)
311 if (!CurrMO.isReg() || CurrMO.isDef() || CurrMO.isUndef() ||
316 MO.
setReg(CurrMO.getReg());
322 unsigned MaxClearance = 0;
323 unsigned MaxClearanceReg = OriginalReg;
325 for (
auto Reg : Order) {
327 "Reg is expected to be mapped to a single index");
328 int RCrx = *regIndices(
Reg).begin();
329 unsigned Clearance = CurInstr - LiveRegs[RCrx].
Def;
330 if (Clearance <= MaxClearance)
332 MaxClearance = Clearance;
333 MaxClearanceReg =
Reg;
335 if (MaxClearance > Pref)
340 if (MaxClearanceReg != OriginalReg)
341 MO.
setReg(MaxClearanceReg);
348 bool ExecutionDepsFix::shouldBreakDependence(
MachineInstr *MI,
unsigned OpIdx,
351 for (
int rx : regIndices(reg)) {
352 unsigned Clearance = CurInstr - LiveRegs[rx].
Def;
353 DEBUG(
dbgs() <<
"Clearance: " << Clearance <<
", want " << Pref);
355 if (Pref > Clearance) {
356 DEBUG(
dbgs() <<
": Break dependency.\n");
369 void ExecutionDepsFix::processDefs(
MachineInstr *MI,
bool breakDependency,
375 if (breakDependency) {
378 bool HadTrueDependency = pickBestRegisterForUndef(MI, OpNum, Pref);
382 if (!HadTrueDependency && shouldBreakDependence(MI, OpNum, Pref))
383 UndefReads.push_back(std::make_pair(MI, OpNum));
395 for (
int rx : regIndices(MO.
getReg())) {
400 if (breakDependency) {
404 if (Pref && shouldBreakDependence(MI, i, Pref))
409 LiveRegs[rx].
Def = CurInstr;
426 if (UndefReads.empty())
436 unsigned OpIdx = UndefReads.back().second;
446 UndefReads.pop_back();
447 if (UndefReads.empty())
450 UndefMI = UndefReads.back().first;
451 OpIdx = UndefReads.back().second;
458 void ExecutionDepsFix::visitHardInstr(
MachineInstr *mi,
unsigned domain) {
463 if (!mo.
isReg())
continue;
464 for (
int rx : regIndices(mo.
getReg())) {
472 if (!mo.
isReg())
continue;
473 for (
int rx : regIndices(mo.
getReg())) {
481 void ExecutionDepsFix::visitSoftInstr(
MachineInstr *mi,
unsigned mask) {
484 unsigned available = mask;
492 if (!mo.
isReg())
continue;
493 for (
int rx : regIndices(mo.
getReg())) {
504 if (common) available = common;
519 visitHardInstr(mi, domain);
526 for (
int rx : used) {
527 assert(LiveRegs &&
"no space allocated for live registers");
528 const LiveReg &LR = LiveRegs[rx];
535 auto I = std::upper_bound(Regs.
begin(), Regs.
end(), &LR,
537 return LHS->
Def < RHS->Def;
545 while (!Regs.
empty()) {
556 if (Latest == dv || Latest->
Next)
558 if (merge(dv, Latest))
563 assert(LiveRegs &&
"no space allocated for live registers");
564 if (LiveRegs[i].Value == Latest)
582 if (!mo.
isReg())
continue;
583 for (
int rx : regIndices(mo.
getReg())) {
584 if (!LiveRegs[rx].Value || (mo.
isDef() && LiveRegs[rx].
Value != dv)) {
594 enterBasicBlock(MBB);
599 bool breakDependency = isBlockDone(MBB);
604 Kill = visitInstr(&MI);
605 processDefs(&MI, breakDependency, Kill);
609 processUndefReads(MBB);
610 leaveBasicBlock(MBB);
614 return MBBInfos[MBB].PrimaryCompleted &&
615 MBBInfos[MBB].IncomingCompleted == MBBInfos[MBB].PrimaryIncoming &&
616 MBBInfos[MBB].IncomingProcessed == MBB->
pred_size();
629 DEBUG(
dbgs() <<
"********** FIX EXECUTION DEPENDENCIES: "
630 << TRI->getRegClassName(RC) <<
" **********\n");
634 bool anyregs =
false;
636 for (
unsigned Reg : *RC) {
642 if (!anyregs)
return false;
645 if (AliasMap.empty()) {
648 AliasMap.resize(TRI->getNumRegs());
649 for (
unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
652 AliasMap[*AI].push_back(i);
656 for (
auto &MBB : mf) {
658 MBBInfos.
insert(std::make_pair(&MBB, InitialInfo));
693 MBBI = RPOT.
begin(), MBBE = RPOT.
end(); MBBI != MBBE; ++MBBI) {
697 MBBInfos[MBB].PrimaryCompleted =
true;
698 MBBInfos[MBB].PrimaryIncoming = MBBInfos[MBB].IncomingProcessed;
701 while (!Workqueue.
empty()) {
704 processBasicBlock(ActiveMBB, Primary);
705 bool Done = isBlockDone(ActiveMBB);
707 if (!isBlockDone(Succ)) {
709 MBBInfos[Succ].IncomingProcessed++;
712 MBBInfos[Succ].IncomingCompleted++;
714 if (isBlockDone(Succ)) {
729 MBBI != MBBE; ++MBBI) {
731 if (!isBlockDone(MBB)) {
732 processBasicBlock(MBB,
false);
740 MBBI = RPOT.
begin(), MBBE = RPOT.
end(); MBBI != MBBE; ++MBBI) {
741 auto FI = MBBInfos.
find(*MBBI);
742 if (FI == MBBInfos.
end() || !FI->second.OutRegs)
744 for (
unsigned i = 0, e = NumRegs; i != e; ++i)
745 if (FI->second.OutRegs[i].Value)
746 release(FI->second.OutRegs[i].Value);
747 delete[] FI->second.OutRegs;
752 Allocator.DestroyAll();
bool hasDomain(unsigned domain) const
void push_back(const T &Elt)
mop_iterator operands_end()
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
iterator_range< livein_iterator > liveins() const
void addDomain(unsigned domain)
unsigned getRegister(unsigned i) const
Return the specified register in the class.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
Describe properties that are true of each instruction in the target description file.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
unsigned AvailableDomains
std::vector< MachineBasicBlock * >::const_iterator const_pred_iterator
iterator_range< mop_iterator > operands()
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
iterator_range< succ_iterator > successors()
virtual unsigned getUndefRegClearance(const MachineInstr &MI, unsigned &OpNum, const TargetRegisterInfo *TRI) const
Return the minimum clearance before an instruction that reads an unused register. ...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool isPhysRegUsed(unsigned PhysReg) const
Return true if the specified register is modified or read in this function.
virtual std::pair< uint16_t, uint16_t > getExecutionDomain(const MachineInstr &MI) const
Return the current execution domain and bit mask of possible domains for instruction.
virtual unsigned getPartialRegUpdateClearance(const MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const
Returns the preferred minimum clearance before an instruction with an unwanted partial register updat...
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Reg
All possible values of the reg field in the ModR/M byte.
DomainValue * Value
Value currently in this register, or NULL when no value is being tracked.
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
LLVM_NODISCARD bool empty() const
unsigned getNumOperands() const
Access to explicit operands of the instruction.
virtual void setExecutionDomain(MachineInstr &MI, unsigned Domain) const
Change the opcode of MI to execute in Domain.
unsigned getCommonDomains(unsigned mask) const
reverse_iterator rbegin()
iterator find(const_arg_type_t< KeyT > Val)
bool isDebugValue() const
void init(const MachineRegisterInfo &MRI)
int Def
Instruction that defined this register, relative to the beginning of the current basic block...
unsigned const MachineRegisterInfo * MRI
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
unsigned getFirstDomain() const
virtual void breakPartialRegDependency(MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const
Insert a dependency-breaking instruction before MI to eliminate an unwanted dependency on OpNum...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
const MachineOperand & getOperand(unsigned i) const
MCRegAliasIterator enumerates all registers aliasing Reg.
bool isVariadic(QueryType Type=IgnoreBundle) const
Return true if this instruction can have a variable number of operands.
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
SmallVector< MachineInstr *, 8 > Instrs
void runOnMachineFunction(const MachineFunction &MF)
runOnFunction - Prepare to answer questions about MF.
A set of live virtual registers and physical register units.
Information about a live register.
pred_iterator pred_begin()
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
MachineOperand class - Representation of each machine instruction operand.
void setSingleDomain(unsigned domain)
LLVM_NODISCARD T pop_back_val()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
A range adaptor for a pair of iterators.
unsigned getNumRegs() const
Return the number of registers in this class.
iterator insert(iterator I, T &&Elt)
A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track of execution domains...
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
LaneBitmask contains(unsigned Reg) const
Representation of each machine instruction.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
typename std::vector< NodeRef >::reverse_iterator rpo_iterator
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual const TargetInstrInfo * getInstrInfo() const
LLVM Value Representation.
mop_iterator operands_begin()
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
const TargetRegisterClass * getRegClass(const MCInstrDesc &TID, unsigned OpNum, const TargetRegisterInfo *TRI, const MachineFunction &MF) const
Given a machine instruction descriptor, returns the register class constraint for OpNum...
unsigned pred_size() const
bool contains(unsigned Reg) const
Return true if the specified register is included in this register class.