Skip to content

Commit 6b09402

Browse files
committed
[CriticalAntiDepBreaker] Use Register and MCRegister. NFC
1 parent fc4bce3 commit 6b09402

File tree

2 files changed

+82
-82
lines changed

2 files changed

+82
-82
lines changed

llvm/lib/CodeGen/CriticalAntiDepBreaker.cpp

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
6767
for (const MachineBasicBlock *Succ : BB->successors())
6868
for (const auto &LI : Succ->liveins()) {
6969
for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
70-
unsigned Reg = (*AI).id();
71-
Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
72-
KillIndices[Reg] = BBSize;
70+
MCRegister Reg = *AI;
71+
Classes[Reg.id()] = reinterpret_cast<TargetRegisterClass *>(-1);
72+
KillIndices[Reg.id()] = BBSize;
7373
DefIndices[Reg] = ~0u;
7474
}
7575
}
@@ -85,10 +85,10 @@ void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
8585
if (!IsReturnBlock && !Pristine.test(Reg))
8686
continue;
8787
for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
88-
unsigned Reg = (*AI).id();
89-
Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
90-
KillIndices[Reg] = BBSize;
91-
DefIndices[Reg] = ~0u;
88+
MCRegister Reg = *AI;
89+
Classes[Reg.id()] = reinterpret_cast<TargetRegisterClass *>(-1);
90+
KillIndices[Reg.id()] = BBSize;
91+
DefIndices[Reg.id()] = ~0u;
9292
}
9393
}
9494
}
@@ -182,7 +182,8 @@ void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr &MI) {
182182
MachineOperand &MO = MI.getOperand(i);
183183
if (!MO.isReg()) continue;
184184
Register Reg = MO.getReg();
185-
if (Reg == 0) continue;
185+
if (!Reg)
186+
continue;
186187
const TargetRegisterClass *NewRC = nullptr;
187188

188189
if (i < MI.getDesc().getNumOperands())
@@ -278,7 +279,8 @@ void CriticalAntiDepBreaker::ScanInstruction(MachineInstr &MI, unsigned Count) {
278279

279280
if (!MO.isReg()) continue;
280281
Register Reg = MO.getReg();
281-
if (Reg == 0) continue;
282+
if (!Reg)
283+
continue;
282284
if (!MO.isDef()) continue;
283285

284286
// Ignore two-addr defs.
@@ -308,7 +310,8 @@ void CriticalAntiDepBreaker::ScanInstruction(MachineInstr &MI, unsigned Count) {
308310
MachineOperand &MO = MI.getOperand(i);
309311
if (!MO.isReg()) continue;
310312
Register Reg = MO.getReg();
311-
if (Reg == 0) continue;
313+
if (!Reg)
314+
continue;
312315
if (!MO.isUse()) continue;
313316

314317
const TargetRegisterClass *NewRC = nullptr;
@@ -327,10 +330,10 @@ void CriticalAntiDepBreaker::ScanInstruction(MachineInstr &MI, unsigned Count) {
327330
// It wasn't previously live but now it is, this is a kill.
328331
// Repeat for all aliases.
329332
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
330-
unsigned AliasReg = (*AI).id();
331-
if (KillIndices[AliasReg] == ~0u) {
332-
KillIndices[AliasReg] = Count;
333-
DefIndices[AliasReg] = ~0u;
333+
MCRegister AliasReg = *AI;
334+
if (KillIndices[AliasReg.id()] == ~0u) {
335+
KillIndices[AliasReg.id()] = Count;
336+
DefIndices[AliasReg.id()] = ~0u;
334337
}
335338
}
336339
}
@@ -347,10 +350,9 @@ void CriticalAntiDepBreaker::ScanInstruction(MachineInstr &MI, unsigned Count) {
347350
// RegRefs because the def is inserted by PrescanInstruction and not erased
348351
// during ScanInstruction. So checking for an instruction with definitions of
349352
// both NewReg and AntiDepReg covers it.
350-
bool
351-
CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
352-
RegRefIter RegRefEnd,
353-
unsigned NewReg) {
353+
bool CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
354+
RegRefIter RegRefEnd,
355+
MCRegister NewReg) {
354356
for (RegRefIter I = RegRefBegin; I != RegRefEnd; ++I ) {
355357
MachineOperand *RefOper = I->second;
356358

@@ -389,15 +391,12 @@ CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
389391
return false;
390392
}
391393

392-
unsigned CriticalAntiDepBreaker::
393-
findSuitableFreeRegister(RegRefIter RegRefBegin,
394-
RegRefIter RegRefEnd,
395-
unsigned AntiDepReg,
396-
unsigned LastNewReg,
397-
const TargetRegisterClass *RC,
398-
SmallVectorImpl<unsigned> &Forbid) {
394+
MCRegister CriticalAntiDepBreaker::findSuitableFreeRegister(
395+
RegRefIter RegRefBegin, RegRefIter RegRefEnd, MCRegister AntiDepReg,
396+
MCRegister LastNewReg, const TargetRegisterClass *RC,
397+
const SmallVectorImpl<Register> &Forbid) {
399398
ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(RC);
400-
for (unsigned NewReg : Order) {
399+
for (MCRegister NewReg : Order) {
401400
// Don't replace a register with itself.
402401
if (NewReg == AntiDepReg) continue;
403402
// Don't replace a register with one that was recently used to repair
@@ -410,17 +409,18 @@ findSuitableFreeRegister(RegRefIter RegRefBegin,
410409
if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg)) continue;
411410
// If NewReg is dead and NewReg's most recent def is not before
412411
// AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
413-
assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
414-
&& "Kill and Def maps aren't consistent for AntiDepReg!");
412+
assert(((KillIndices[AntiDepReg.id()] == ~0u) !=
413+
(DefIndices[AntiDepReg.id()] == ~0u)) &&
414+
"Kill and Def maps aren't consistent for AntiDepReg!");
415415
assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
416416
&& "Kill and Def maps aren't consistent for NewReg!");
417417
if (KillIndices[NewReg] != ~0u ||
418418
Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
419-
KillIndices[AntiDepReg] > DefIndices[NewReg])
419+
KillIndices[AntiDepReg.id()] > DefIndices[NewReg])
420420
continue;
421421
// If NewReg overlaps any of the forbidden registers, we can't use it.
422422
bool Forbidden = false;
423-
for (unsigned R : Forbid)
423+
for (Register R : Forbid)
424424
if (TRI->regsOverlap(NewReg, R)) {
425425
Forbidden = true;
426426
break;
@@ -430,7 +430,7 @@ findSuitableFreeRegister(RegRefIter RegRefBegin,
430430
}
431431

432432
// No registers are free and available!
433-
return 0;
433+
return MCRegister();
434434
}
435435

436436
unsigned CriticalAntiDepBreaker::
@@ -517,7 +517,7 @@ BreakAntiDependencies(const std::vector<SUnit> &SUnits,
517517
// fix that remaining critical edge too. This is a little more involved,
518518
// because unlike the most recent register, less recent registers should
519519
// still be considered, though only if no other registers are available.
520-
std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);
520+
std::vector<MCRegister> LastNewReg(TRI->getNumRegs(), MCRegister());
521521

522522
// Attempt to break anti-dependence edges on the critical path. Walk the
523523
// instructions from the bottom up, tracking information about liveness
@@ -549,22 +549,22 @@ BreakAntiDependencies(const std::vector<SUnit> &SUnits,
549549
// anti-dependencies. The current code here only knows how to break one
550550
// edge per instruction. Note that we'd have to be able to break all of
551551
// the anti-dependencies in an instruction in order to be effective.
552-
unsigned AntiDepReg = 0;
552+
MCRegister AntiDepReg;
553553
if (&MI == CriticalPathMI) {
554554
if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
555555
const SUnit *NextSU = Edge->getSUnit();
556556

557557
// Only consider anti-dependence edges.
558558
if (Edge->getKind() == SDep::Anti) {
559-
AntiDepReg = Edge->getReg();
560-
assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
559+
AntiDepReg = Edge->getReg().asMCReg();
560+
assert(AntiDepReg && "Anti-dependence on reg0?");
561561
if (!MRI.isAllocatable(AntiDepReg))
562562
// Don't break anti-dependencies on non-allocatable registers.
563-
AntiDepReg = 0;
564-
else if (KeepRegs.test(AntiDepReg))
563+
AntiDepReg = MCRegister();
564+
else if (KeepRegs.test(AntiDepReg.id()))
565565
// Don't break anti-dependencies if a use down below requires
566566
// this exact register.
567-
AntiDepReg = 0;
567+
AntiDepReg = MCRegister();
568568
else {
569569
// If the SUnit has other dependencies on the SUnit that it
570570
// anti-depends on, don't bother breaking the anti-dependency
@@ -579,7 +579,7 @@ BreakAntiDependencies(const std::vector<SUnit> &SUnits,
579579
? (P.getKind() != SDep::Anti || P.getReg() != AntiDepReg)
580580
: (P.getKind() == SDep::Data &&
581581
P.getReg() == AntiDepReg)) {
582-
AntiDepReg = 0;
582+
AntiDepReg = MCRegister();
583583
break;
584584
}
585585
}
@@ -595,15 +595,15 @@ BreakAntiDependencies(const std::vector<SUnit> &SUnits,
595595

596596
PrescanInstruction(MI);
597597

598-
SmallVector<unsigned, 2> ForbidRegs;
598+
SmallVector<Register, 2> ForbidRegs;
599599

600600
// If MI's defs have a special allocation requirement, don't allow
601601
// any def registers to be changed. Also assume all registers
602602
// defined in a call must not be changed (ABI).
603603
if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI))
604604
// If this instruction's defs have special allocation requirement, don't
605605
// break this anti-dependency.
606-
AntiDepReg = 0;
606+
AntiDepReg = MCRegister();
607607
else if (AntiDepReg) {
608608
// If this instruction has a use of AntiDepReg, breaking it
609609
// is invalid. If the instruction defines other registers,
@@ -612,9 +612,10 @@ BreakAntiDependencies(const std::vector<SUnit> &SUnits,
612612
for (const MachineOperand &MO : MI.operands()) {
613613
if (!MO.isReg()) continue;
614614
Register Reg = MO.getReg();
615-
if (Reg == 0) continue;
615+
if (!Reg)
616+
continue;
616617
if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
617-
AntiDepReg = 0;
618+
AntiDepReg = MCRegister();
618619
break;
619620
}
620621
if (MO.isDef() && Reg != AntiDepReg)
@@ -624,34 +625,35 @@ BreakAntiDependencies(const std::vector<SUnit> &SUnits,
624625

625626
// Determine AntiDepReg's register class, if it is live and is
626627
// consistently used within a single class.
627-
const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg]
628-
: nullptr;
629-
assert((AntiDepReg == 0 || RC != nullptr) &&
628+
const TargetRegisterClass *RC =
629+
AntiDepReg ? Classes[AntiDepReg.id()] : nullptr;
630+
assert((!AntiDepReg || RC != nullptr) &&
630631
"Register should be live if it's causing an anti-dependence!");
631632
if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
632-
AntiDepReg = 0;
633+
AntiDepReg = MCRegister();
633634

634635
// Look for a suitable register to use to break the anti-dependence.
635636
//
636637
// TODO: Instead of picking the first free register, consider which might
637638
// be the best.
638-
if (AntiDepReg != 0) {
639-
std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
640-
std::multimap<unsigned, MachineOperand *>::iterator>
641-
Range = RegRefs.equal_range(AntiDepReg);
642-
if (unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,
643-
AntiDepReg,
644-
LastNewReg[AntiDepReg],
645-
RC, ForbidRegs)) {
639+
if (AntiDepReg) {
640+
std::pair<std::multimap<MCRegister, MachineOperand *>::iterator,
641+
std::multimap<MCRegister, MachineOperand *>::iterator>
642+
Range = RegRefs.equal_range(AntiDepReg);
643+
if (MCRegister NewReg = findSuitableFreeRegister(
644+
Range.first, Range.second, AntiDepReg, LastNewReg[AntiDepReg], RC,
645+
ForbidRegs)) {
646646
LLVM_DEBUG(dbgs() << "Breaking anti-dependence edge on "
647647
<< printReg(AntiDepReg, TRI) << " with "
648648
<< RegRefs.count(AntiDepReg) << " references"
649649
<< " using " << printReg(NewReg, TRI) << "!\n");
650650

651651
// Update the references to the old register to refer to the new
652652
// register.
653-
for (std::multimap<unsigned, MachineOperand *>::iterator
654-
Q = Range.first, QE = Range.second; Q != QE; ++Q) {
653+
for (std::multimap<MCRegister, MachineOperand *>::iterator
654+
Q = Range.first,
655+
QE = Range.second;
656+
Q != QE; ++Q) {
655657
Q->second->setReg(NewReg);
656658
// If the SU for the instruction being updated has debug information
657659
// related to the anti-dependency register, make sure to update that
@@ -665,22 +667,22 @@ BreakAntiDependencies(const std::vector<SUnit> &SUnits,
665667
// We just went back in time and modified history; the
666668
// liveness information for the anti-dependence reg is now
667669
// inconsistent. Set the state as if it were dead.
668-
Classes[NewReg] = Classes[AntiDepReg];
669-
DefIndices[NewReg] = DefIndices[AntiDepReg];
670-
KillIndices[NewReg] = KillIndices[AntiDepReg];
671-
assert(((KillIndices[NewReg] == ~0u) !=
672-
(DefIndices[NewReg] == ~0u)) &&
673-
"Kill and Def maps aren't consistent for NewReg!");
674-
675-
Classes[AntiDepReg] = nullptr;
676-
DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
677-
KillIndices[AntiDepReg] = ~0u;
678-
assert(((KillIndices[AntiDepReg] == ~0u) !=
679-
(DefIndices[AntiDepReg] == ~0u)) &&
680-
"Kill and Def maps aren't consistent for AntiDepReg!");
670+
Classes[NewReg.id()] = Classes[AntiDepReg.id()];
671+
DefIndices[NewReg.id()] = DefIndices[AntiDepReg.id()];
672+
KillIndices[NewReg.id()] = KillIndices[AntiDepReg.id()];
673+
assert(((KillIndices[NewReg.id()] == ~0u) !=
674+
(DefIndices[NewReg.id()] == ~0u)) &&
675+
"Kill and Def maps aren't consistent for NewReg!");
676+
677+
Classes[AntiDepReg.id()] = nullptr;
678+
DefIndices[AntiDepReg.id()] = KillIndices[AntiDepReg.id()];
679+
KillIndices[AntiDepReg.id()] = ~0u;
680+
assert(((KillIndices[AntiDepReg.id()] == ~0u) !=
681+
(DefIndices[AntiDepReg.id()] == ~0u)) &&
682+
"Kill and Def maps aren't consistent for AntiDepReg!");
681683

682684
RegRefs.erase(AntiDepReg);
683-
LastNewReg[AntiDepReg] = NewReg;
685+
LastNewReg[AntiDepReg.id()] = NewReg;
684686
++Broken;
685687
}
686688
}

llvm/lib/CodeGen/CriticalAntiDepBreaker.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class LLVM_LIBRARY_VISIBILITY CriticalAntiDepBreaker : public AntiDepBreaker {
5353
std::vector<const TargetRegisterClass *> Classes;
5454

5555
/// Map registers to all their references within a live range.
56-
std::multimap<unsigned, MachineOperand *> RegRefs;
56+
std::multimap<MCRegister, MachineOperand *> RegRefs;
5757

5858
using RegRefIter =
59-
std::multimap<unsigned, MachineOperand *>::const_iterator;
59+
std::multimap<MCRegister, MachineOperand *>::const_iterator;
6060

6161
/// The index of the most recent kill (proceeding bottom-up),
6262
/// or ~0u if the register is not live.
@@ -96,15 +96,13 @@ class LLVM_LIBRARY_VISIBILITY CriticalAntiDepBreaker : public AntiDepBreaker {
9696
private:
9797
void PrescanInstruction(MachineInstr &MI);
9898
void ScanInstruction(MachineInstr &MI, unsigned Count);
99-
bool isNewRegClobberedByRefs(RegRefIter RegRefBegin,
100-
RegRefIter RegRefEnd,
101-
unsigned NewReg);
102-
unsigned findSuitableFreeRegister(RegRefIter RegRefBegin,
103-
RegRefIter RegRefEnd,
104-
unsigned AntiDepReg,
105-
unsigned LastNewReg,
106-
const TargetRegisterClass *RC,
107-
SmallVectorImpl<unsigned> &Forbid);
99+
bool isNewRegClobberedByRefs(RegRefIter RegRefBegin, RegRefIter RegRefEnd,
100+
MCRegister NewReg);
101+
MCRegister
102+
findSuitableFreeRegister(RegRefIter RegRefBegin, RegRefIter RegRefEnd,
103+
MCRegister AntiDepReg, MCRegister LastNewReg,
104+
const TargetRegisterClass *RC,
105+
const SmallVectorImpl<Register> &Forbid);
108106
};
109107

110108
} // end namespace llvm

0 commit comments

Comments
 (0)