23 #define DEBUG_TYPE "execution-fix"
59 unsigned AvailableDomains;
71 bool isCollapsed()
const {
return Instrs.
empty(); }
74 bool hasDomain(
unsigned domain)
const {
75 return AvailableDomains & (1u << domain);
79 void addDomain(
unsigned domain) {
80 AvailableDomains |= 1u << domain;
84 void setSingleDomain(
unsigned domain) {
85 AvailableDomains = 1u << domain;
89 unsigned getCommonDomains(
unsigned mask)
const {
90 return AvailableDomains & mask;
94 unsigned getFirstDomain()
const {
98 DomainValue() : Refs(0) { clear(); }
102 AvailableDomains = 0;
134 std::vector<int> AliasMap;
135 const unsigned NumRegs;
141 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads;
152 bool SeenUnknownBackEdge;
165 virtual const char *getPassName()
const {
166 return "Execution dependency fix";
171 int regIndex(
unsigned Reg);
174 DomainValue *alloc(
int domain = -1);
175 DomainValue *retain(DomainValue *DV) {
179 void release(DomainValue*);
180 DomainValue *resolve(DomainValue*&);
183 void setLiveReg(
int rx, DomainValue *DV);
185 void force(
int rx,
unsigned domain);
186 void collapse(DomainValue *dv,
unsigned domain);
187 bool merge(DomainValue *
A, DomainValue *B);
195 bool shouldBreakDependence(
MachineInstr*,
unsigned OpIdx,
unsigned Pref);
204 int ExeDepsFix::regIndex(
unsigned Reg) {
205 assert(Reg < AliasMap.size() &&
"Invalid register");
206 return AliasMap[
Reg];
209 DomainValue *ExeDepsFix::alloc(
int domain) {
210 DomainValue *dv = Avail.empty() ?
211 new(Allocator.Allocate()) DomainValue :
212 Avail.pop_back_val();
214 dv->addDomain(domain);
215 assert(dv->Refs == 0 &&
"Reference count wasn't cleared");
216 assert(!dv->Next &&
"Chained DomainValue shouldn't have been recycled");
222 void ExeDepsFix::release(DomainValue *DV) {
224 assert(DV->Refs &&
"Bad DomainValue");
229 if (DV->AvailableDomains && !DV->isCollapsed())
230 collapse(DV, DV->getFirstDomain());
232 DomainValue *Next = DV->Next;
242 DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
243 DomainValue *DV = DVRef;
244 if (!DV || !DV->Next)
259 void ExeDepsFix::setLiveReg(
int rx, DomainValue *dv) {
260 assert(
unsigned(rx) < NumRegs &&
"Invalid index");
261 assert(LiveRegs &&
"Must enter basic block first.");
263 if (LiveRegs[rx].
Value == dv)
265 if (LiveRegs[rx].
Value)
266 release(LiveRegs[rx].Value);
267 LiveRegs[rx].Value = retain(dv);
271 void ExeDepsFix::kill(
int rx) {
272 assert(
unsigned(rx) < NumRegs &&
"Invalid index");
273 assert(LiveRegs &&
"Must enter basic block first.");
274 if (!LiveRegs[rx].
Value)
277 release(LiveRegs[rx].Value);
278 LiveRegs[rx].Value = 0;
282 void ExeDepsFix::force(
int rx,
unsigned domain) {
283 assert(
unsigned(rx) < NumRegs &&
"Invalid index");
284 assert(LiveRegs &&
"Must enter basic block first.");
285 if (DomainValue *dv = LiveRegs[rx].
Value) {
286 if (dv->isCollapsed())
287 dv->addDomain(domain);
288 else if (dv->hasDomain(domain))
289 collapse(dv, domain);
293 collapse(dv, dv->getFirstDomain());
294 assert(LiveRegs[rx].Value &&
"Not live after collapse?");
295 LiveRegs[rx].Value->addDomain(domain);
299 setLiveReg(rx, alloc(domain));
305 void ExeDepsFix::collapse(DomainValue *dv,
unsigned domain) {
306 assert(dv->hasDomain(domain) &&
"Cannot collapse");
309 while (!dv->Instrs.empty())
310 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
311 dv->setSingleDomain(domain);
314 if (LiveRegs && dv->Refs > 1)
315 for (
unsigned rx = 0; rx != NumRegs; ++rx)
316 if (LiveRegs[rx].Value == dv)
317 setLiveReg(rx, alloc(domain));
322 bool ExeDepsFix::merge(DomainValue *
A, DomainValue *B) {
323 assert(!A->isCollapsed() &&
"Cannot merge into collapsed");
324 assert(!B->isCollapsed() &&
"Cannot merge from collapsed");
328 unsigned common = A->getCommonDomains(B->AvailableDomains);
331 A->AvailableDomains = common;
332 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
339 for (
unsigned rx = 0; rx != NumRegs; ++rx)
340 if (LiveRegs[rx].Value == B)
348 SeenUnknownBackEdge =
false;
359 LiveRegs =
new LiveReg[NumRegs];
362 for (
unsigned rx = 0; rx != NumRegs; ++rx) {
363 LiveRegs[rx].Value = 0;
364 LiveRegs[rx].Def = -(1 << 20);
371 int rx = regIndex(*i);
377 LiveRegs[rx].Def = -1;
385 pe = MBB->
pred_end(); pi != pe; ++pi) {
386 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
387 if (fi == LiveOuts.end()) {
388 SeenUnknownBackEdge =
true;
391 assert(fi->second &&
"Can't have NULL entries");
393 for (
unsigned rx = 0; rx != NumRegs; ++rx) {
395 LiveRegs[rx].Def = std::max(LiveRegs[rx].
Def, fi->second[rx].Def);
397 DomainValue *pdv = resolve(fi->second[rx].Value);
400 if (!LiveRegs[rx].Value) {
406 if (LiveRegs[rx].Value->isCollapsed()) {
408 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
409 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
410 collapse(pdv, Domain);
415 if (!pdv->isCollapsed())
416 merge(LiveRegs[rx].Value, pdv);
418 force(rx, pdv->getFirstDomain());
422 << (SeenUnknownBackEdge ?
": incomplete\n" :
": all preds known\n"));
426 assert(LiveRegs &&
"Must enter basic block first.");
429 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
434 for (
unsigned i = 0, e = NumRegs; i != e; ++i)
435 LiveRegs[i].
Def -= CurInstr;
439 for (
unsigned i = 0, e = NumRegs; i != e; ++i)
440 release(LiveRegs[i].Value);
451 std::pair<uint16_t, uint16_t> DomP =
TII->getExecutionDomain(MI);
454 visitSoftInstr(MI, DomP.second);
456 visitHardInstr(MI, DomP.first);
461 processDefs(MI, !DomP.first);
466 bool ExeDepsFix::shouldBreakDependence(
MachineInstr *MI,
unsigned OpIdx,
472 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
473 DEBUG(
dbgs() <<
"Clearance: " << Clearance <<
", want " << Pref);
475 if (Pref > Clearance) {
476 DEBUG(
dbgs() <<
": Break dependency.\n");
481 if (!SeenUnknownBackEdge || Pref <=
unsigned(CurInstr)) {
486 DEBUG(
dbgs() <<
": Wait for back-edge to resolve.\n");
495 assert(!MI->
isDebugValue() &&
"Won't process debug values");
499 unsigned Pref =
TII->getUndefRegClearance(MI, OpNum, TRI);
501 if (shouldBreakDependence(MI, OpNum, Pref))
502 UndefReads.push_back(std::make_pair(MI, OpNum));
515 int rx = regIndex(MO.
getReg());
520 DEBUG(
dbgs() << TRI->getName(RC->getRegister(rx)) <<
":\t" << CurInstr
525 unsigned Pref =
TII->getPartialRegUpdateClearance(MI, i, TRI);
526 if (Pref && shouldBreakDependence(MI, i, Pref))
527 TII->breakPartialRegDependency(MI, i, TRI);
530 LiveRegs[rx].Def = CurInstr;
546 if (UndefReads.empty())
552 SE = MBB->
succ_end(); SI != SE; ++SI) {
553 LiveUnits.addLiveIns(*SI, *TRI);
556 unsigned OpIdx = UndefReads.back().second;
561 LiveUnits.stepBackward(*
I, *TRI);
563 if (UndefMI == &*
I) {
565 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI);
567 UndefReads.pop_back();
568 if (UndefReads.empty())
571 UndefMI = UndefReads.back().first;
572 OpIdx = UndefReads.back().second;
579 void ExeDepsFix::visitHardInstr(
MachineInstr *mi,
unsigned domain) {
584 if (!mo.
isReg())
continue;
585 int rx = regIndex(mo.
getReg());
586 if (rx < 0)
continue;
593 if (!mo.
isReg())
continue;
594 int rx = regIndex(mo.
getReg());
595 if (rx < 0)
continue;
602 void ExeDepsFix::visitSoftInstr(
MachineInstr *mi,
unsigned mask) {
605 unsigned available = mask;
613 if (!mo.
isReg())
continue;
614 int rx = regIndex(mo.
getReg());
615 if (rx < 0)
continue;
616 if (DomainValue *dv = LiveRegs[rx].Value) {
618 unsigned common = dv->getCommonDomains(available);
620 if (dv->isCollapsed()) {
624 if (common) available = common;
638 TII->setExecutionDomain(mi, domain);
639 visitHardInstr(mi, domain);
648 const LiveReg &LR = LiveRegs[rx];
650 if (!LR.Value->getCommonDomains(available)) {
655 bool Inserted =
false;
657 i != e && !Inserted; ++i) {
658 if (LR.Def < i->Def) {
670 while (!Regs.
empty()) {
674 dv->AvailableDomains = dv->getCommonDomains(available);
675 assert(dv->AvailableDomains &&
"Domain should have been filtered");
681 if (Latest == dv || Latest->Next)
683 if (merge(dv, Latest))
688 if (LiveRegs[*i].Value == Latest)
695 dv->AvailableDomains = available;
697 dv->Instrs.push_back(mi);
705 if (!mo.
isReg())
continue;
706 int rx = regIndex(mo.
getReg());
707 if (rx < 0)
continue;
708 if (!LiveRegs[rx].Value || (mo.
isDef() && LiveRegs[rx].Value != dv)) {
717 TII = MF->getTarget().getInstrInfo();
720 assert(NumRegs == RC->getNumRegs() &&
"Bad regclass");
722 DEBUG(
dbgs() <<
"********** FIX EXECUTION DEPENDENCIES: "
723 << RC->getName() <<
" **********\n");
727 bool anyregs =
false;
730 if (MF->getRegInfo().isPhysRegUsed(*
I)) {
734 if (!anyregs)
return false;
737 if (AliasMap.empty()) {
740 AliasMap.resize(TRI->getNumRegs(), -1);
741 for (
unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
751 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
753 enterBasicBlock(MBB);
754 if (SeenUnknownBackEdge)
759 processUndefReads(MBB);
760 leaveBasicBlock(MBB);
765 for (
unsigned i = 0, e = Loops.
size(); i != e; ++i) {
767 enterBasicBlock(MBB);
770 if (!
I->isDebugValue())
771 processDefs(
I,
false);
772 processUndefReads(MBB);
773 leaveBasicBlock(MBB);
778 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
779 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
780 if (FI == LiveOuts.end() || !FI->second)
782 for (
unsigned i = 0, e = NumRegs; i != e; ++i)
783 if (FI->second[i].Value)
784 release(FI->second[i].Value);
790 Allocator.DestroyAll();
797 return new ExeDepsFix(RC);
void push_back(const T &Elt)
const MCPhysReg * const_iterator
mop_iterator operands_end()
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions. Register definitions always occur...
std::vector< unsigned >::const_iterator livein_iterator
iterator insert(iterator I, const T &Elt)
const MCInstrDesc & getDesc() const
livein_iterator livein_begin() const
const HexagonInstrInfo * TII
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
bool isReg() const
isReg - Tests if this is a MO_Register operand.
ID
LLVM Calling Convention Representation.
unsigned getNumOperands() const
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type 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...
reverse_iterator rbegin()
bool isDebugValue() const
bundle_iterator< MachineInstr, instr_iterator > iterator
livein_iterator livein_end() const
const MachineOperand & getOperand(unsigned i) const
bool isVariadic(QueryType Type=IgnoreBundle) const
succ_iterator succ_begin()
pred_iterator pred_begin()
std::vector< NodeType * >::reverse_iterator rpo_iterator
std::vector< MachineBasicBlock * >::const_iterator const_pred_iterator
FunctionPass * createExecutionDependencyFixPass(const TargetRegisterClass *RC)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
virtual void getAnalysisUsage(AnalysisUsage &AU) const
unsigned getReg() const
getReg - Returns the register number.
virtual const HexagonRegisterInfo & getRegisterInfo() const
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
std::reverse_iterator< iterator > reverse_iterator
LLVM Value Representation.
mop_iterator operands_begin()
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction. Note that variadic (isVari...
bool isPowerOf2_32(uint32_t Value)