Skip to content

Commit fd453e2

Browse files
committed
[TwoAddressInstruction] Use member functions instead of static helpers
This just avoids explicitly passing around common pointers like MRI and TII. NFC.
1 parent d9c82e1 commit fd453e2

File tree

1 file changed

+76
-57
lines changed

1 file changed

+76
-57
lines changed

llvm/lib/CodeGen/TwoAddressInstructionPass.cpp

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,33 @@ class TwoAddressInstructionPass : public MachineFunctionPass {
116116
// registers. e.g. r1 = move v1024.
117117
DenseMap<Register, Register> DstRegMap;
118118

119-
void removeClobberedSrcRegMap(MachineInstr *MI);
119+
MachineInstr *getSingleDef(Register Reg, MachineBasicBlock *BB) const;
120120

121121
bool isRevCopyChain(Register FromReg, Register ToReg, int Maxlen);
122122

123123
bool noUseAfterLastDef(Register Reg, unsigned Dist, unsigned &LastDef);
124124

125+
bool isCopyToReg(MachineInstr &MI, Register &SrcReg, Register &DstReg,
126+
bool &IsSrcPhys, bool &IsDstPhys) const;
127+
128+
bool isPlainlyKilled(const MachineInstr *MI, Register Reg) const;
129+
bool isPlainlyKilled(const MachineOperand &MO) const;
130+
131+
bool isKilled(MachineInstr &MI, Register Reg, bool allowFalsePositives) const;
132+
133+
MachineInstr *findOnlyInterestingUse(Register Reg, MachineBasicBlock *MBB,
134+
bool &IsCopy, Register &DstReg,
135+
bool &IsDstPhys) const;
136+
137+
bool regsAreCompatible(Register RegA, Register RegB) const;
138+
139+
void removeMapRegEntry(const MachineOperand &MO,
140+
DenseMap<Register, Register> &RegMap) const;
141+
142+
void removeClobberedSrcRegMap(MachineInstr *MI);
143+
144+
bool regOverlapsSet(const SmallVectorImpl<Register> &Set, Register Reg) const;
145+
125146
bool isProfitableToCommute(Register RegA, Register RegB, Register RegC,
126147
MachineInstr *MI, unsigned Dist);
127148

@@ -199,8 +220,9 @@ INITIALIZE_PASS_END(TwoAddressInstructionPass, DEBUG_TYPE,
199220
"Two-Address instruction pass", false, false)
200221

201222
/// Return the MachineInstr* if it is the single def of the Reg in current BB.
202-
static MachineInstr *getSingleDef(Register Reg, MachineBasicBlock *BB,
203-
const MachineRegisterInfo *MRI) {
223+
MachineInstr *
224+
TwoAddressInstructionPass::getSingleDef(Register Reg,
225+
MachineBasicBlock *BB) const {
204226
MachineInstr *Ret = nullptr;
205227
for (MachineInstr &DefMI : MRI->def_instructions(Reg)) {
206228
if (DefMI.getParent() != BB || DefMI.isDebugValue())
@@ -224,7 +246,7 @@ bool TwoAddressInstructionPass::isRevCopyChain(Register FromReg, Register ToReg,
224246
int Maxlen) {
225247
Register TmpReg = FromReg;
226248
for (int i = 0; i < Maxlen; i++) {
227-
MachineInstr *Def = getSingleDef(TmpReg, MBB, MRI);
249+
MachineInstr *Def = getSingleDef(TmpReg, MBB);
228250
if (!Def || !Def->isCopy())
229251
return false;
230252

@@ -263,9 +285,9 @@ bool TwoAddressInstructionPass::noUseAfterLastDef(Register Reg, unsigned Dist,
263285
/// Return true if the specified MI is a copy instruction or an extract_subreg
264286
/// instruction. It also returns the source and destination registers and
265287
/// whether they are physical registers by reference.
266-
static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII,
267-
Register &SrcReg, Register &DstReg, bool &IsSrcPhys,
268-
bool &IsDstPhys) {
288+
bool TwoAddressInstructionPass::isCopyToReg(MachineInstr &MI, Register &SrcReg,
289+
Register &DstReg, bool &IsSrcPhys,
290+
bool &IsDstPhys) const {
269291
SrcReg = 0;
270292
DstReg = 0;
271293
if (MI.isCopy()) {
@@ -285,8 +307,8 @@ static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII,
285307

286308
/// Test if the given register value, which is used by the
287309
/// given instruction, is killed by the given instruction.
288-
static bool isPlainlyKilled(const MachineInstr *MI, Register Reg,
289-
LiveIntervals *LIS) {
310+
bool TwoAddressInstructionPass::isPlainlyKilled(const MachineInstr *MI,
311+
Register Reg) const {
290312
if (LIS && Reg.isVirtual() && !LIS->isNotInMIMap(*MI)) {
291313
// FIXME: Sometimes tryInstructionTransform() will add instructions and
292314
// test whether they can be folded before keeping them. In this case it
@@ -311,8 +333,9 @@ static bool isPlainlyKilled(const MachineInstr *MI, Register Reg,
311333

312334
/// Test if the register used by the given operand is killed by the operand's
313335
/// instruction.
314-
static bool isPlainlyKilled(const MachineOperand &MO, LiveIntervals *LIS) {
315-
return MO.isKill() || isPlainlyKilled(MO.getParent(), MO.getReg(), LIS);
336+
bool TwoAddressInstructionPass::isPlainlyKilled(
337+
const MachineOperand &MO) const {
338+
return MO.isKill() || isPlainlyKilled(MO.getParent(), MO.getReg());
316339
}
317340

318341
/// Test if the given register value, which is used by the given
@@ -332,15 +355,14 @@ static bool isPlainlyKilled(const MachineOperand &MO, LiveIntervals *LIS) {
332355
///
333356
/// If allowFalsePositives is true then likely kills are treated as kills even
334357
/// if it can't be proven that they are kills.
335-
static bool isKilled(MachineInstr &MI, Register Reg,
336-
const MachineRegisterInfo *MRI, const TargetInstrInfo *TII,
337-
LiveIntervals *LIS, bool allowFalsePositives) {
358+
bool TwoAddressInstructionPass::isKilled(MachineInstr &MI, Register Reg,
359+
bool allowFalsePositives) const {
338360
MachineInstr *DefMI = &MI;
339361
while (true) {
340362
// All uses of physical registers are likely to be kills.
341363
if (Reg.isPhysical() && (allowFalsePositives || MRI->hasOneUse(Reg)))
342364
return true;
343-
if (!isPlainlyKilled(DefMI, Reg, LIS))
365+
if (!isPlainlyKilled(DefMI, Reg))
344366
return false;
345367
if (Reg.isPhysical())
346368
return true;
@@ -354,7 +376,7 @@ static bool isKilled(MachineInstr &MI, Register Reg,
354376
Register SrcReg, DstReg;
355377
// If the def is something other than a copy, then it isn't going to
356378
// be coalesced, so follow the kill flag.
357-
if (!isCopyToReg(*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
379+
if (!isCopyToReg(*DefMI, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
358380
return true;
359381
Reg = SrcReg;
360382
}
@@ -378,17 +400,15 @@ static bool isTwoAddrUse(MachineInstr &MI, Register Reg, Register &DstReg) {
378400

379401
/// Given a register, if all its uses are in the same basic block, return the
380402
/// last use instruction if it's a copy or a two-address use.
381-
static MachineInstr *
382-
findOnlyInterestingUse(Register Reg, MachineBasicBlock *MBB,
383-
MachineRegisterInfo *MRI, const TargetInstrInfo *TII,
384-
bool &IsCopy, Register &DstReg, bool &IsDstPhys,
385-
LiveIntervals *LIS) {
403+
MachineInstr *TwoAddressInstructionPass::findOnlyInterestingUse(
404+
Register Reg, MachineBasicBlock *MBB, bool &IsCopy, Register &DstReg,
405+
bool &IsDstPhys) const {
386406
MachineOperand *UseOp = nullptr;
387407
for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
388408
MachineInstr *MI = MO.getParent();
389409
if (MI->getParent() != MBB)
390410
return nullptr;
391-
if (isPlainlyKilled(MI, Reg, LIS))
411+
if (isPlainlyKilled(MI, Reg))
392412
UseOp = &MO;
393413
}
394414
if (!UseOp)
@@ -397,7 +417,7 @@ findOnlyInterestingUse(Register Reg, MachineBasicBlock *MBB,
397417

398418
Register SrcReg;
399419
bool IsSrcPhys;
400-
if (isCopyToReg(UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) {
420+
if (isCopyToReg(UseMI, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) {
401421
IsCopy = true;
402422
return &UseMI;
403423
}
@@ -437,8 +457,8 @@ static MCRegister getMappedReg(Register Reg,
437457
}
438458

439459
/// Return true if the two registers are equal or aliased.
440-
static bool regsAreCompatible(Register RegA, Register RegB,
441-
const TargetRegisterInfo *TRI) {
460+
bool TwoAddressInstructionPass::regsAreCompatible(Register RegA,
461+
Register RegB) const {
442462
if (RegA == RegB)
443463
return true;
444464
if (!RegA || !RegB)
@@ -447,9 +467,8 @@ static bool regsAreCompatible(Register RegA, Register RegB,
447467
}
448468

449469
/// From RegMap remove entries mapped to a physical register which overlaps MO.
450-
static void removeMapRegEntry(const MachineOperand &MO,
451-
DenseMap<Register, Register> &RegMap,
452-
const TargetRegisterInfo *TRI) {
470+
void TwoAddressInstructionPass::removeMapRegEntry(
471+
const MachineOperand &MO, DenseMap<Register, Register> &RegMap) const {
453472
assert(
454473
(MO.isReg() || MO.isRegMask()) &&
455474
"removeMapRegEntry must be called with a register or regmask operand.");
@@ -497,27 +516,27 @@ void TwoAddressInstructionPass::removeClobberedSrcRegMap(MachineInstr *MI) {
497516
return;
498517

499518
Register Src = MI->getOperand(1).getReg();
500-
if (regsAreCompatible(Dst, getMappedReg(Src, SrcRegMap), TRI))
519+
if (regsAreCompatible(Dst, getMappedReg(Src, SrcRegMap)))
501520
return;
502521
}
503522

504523
for (const MachineOperand &MO : MI->operands()) {
505524
if (MO.isRegMask()) {
506-
removeMapRegEntry(MO, SrcRegMap, TRI);
525+
removeMapRegEntry(MO, SrcRegMap);
507526
continue;
508527
}
509528
if (!MO.isReg() || !MO.isDef())
510529
continue;
511530
Register Reg = MO.getReg();
512531
if (!Reg || Reg.isVirtual())
513532
continue;
514-
removeMapRegEntry(MO, SrcRegMap, TRI);
533+
removeMapRegEntry(MO, SrcRegMap);
515534
}
516535
}
517536

518537
// Returns true if Reg is equal or aliased to at least one register in Set.
519-
static bool regOverlapsSet(const SmallVectorImpl<Register> &Set, Register Reg,
520-
const TargetRegisterInfo *TRI) {
538+
bool TwoAddressInstructionPass::regOverlapsSet(
539+
const SmallVectorImpl<Register> &Set, Register Reg) const {
521540
for (unsigned R : Set)
522541
if (TRI->regsOverlap(R, Reg))
523542
return true;
@@ -553,7 +572,7 @@ bool TwoAddressInstructionPass::isProfitableToCommute(Register RegA,
553572
// insert => %reg1030 = COPY %reg1029
554573
// %reg1030 = ADD8rr killed %reg1029, killed %reg1028, implicit dead %eflags
555574

556-
if (!isPlainlyKilled(MI, RegC, LIS))
575+
if (!isPlainlyKilled(MI, RegC))
557576
return false;
558577

559578
// Ok, we have something like:
@@ -570,8 +589,8 @@ bool TwoAddressInstructionPass::isProfitableToCommute(Register RegA,
570589
if (ToRegA) {
571590
MCRegister FromRegB = getMappedReg(RegB, SrcRegMap);
572591
MCRegister FromRegC = getMappedReg(RegC, SrcRegMap);
573-
bool CompB = FromRegB && regsAreCompatible(FromRegB, ToRegA, TRI);
574-
bool CompC = FromRegC && regsAreCompatible(FromRegC, ToRegA, TRI);
592+
bool CompB = FromRegB && regsAreCompatible(FromRegB, ToRegA);
593+
bool CompC = FromRegC && regsAreCompatible(FromRegC, ToRegA);
575594

576595
// Compute if any of the following are true:
577596
// -RegB is not tied to a register and RegC is compatible with RegA.
@@ -675,7 +694,7 @@ bool TwoAddressInstructionPass::isProfitableToConv3Addr(Register RegA,
675694
if (!FromRegB)
676695
return false;
677696
MCRegister ToRegA = getMappedReg(RegA, DstRegMap);
678-
return (ToRegA && !regsAreCompatible(FromRegB, ToRegA, TRI));
697+
return (ToRegA && !regsAreCompatible(FromRegB, ToRegA));
679698
}
680699

681700
/// Convert the specified two-address instruction into a three address one.
@@ -728,8 +747,8 @@ void TwoAddressInstructionPass::scanUses(Register DstReg) {
728747
bool IsCopy = false;
729748
Register NewReg;
730749
Register Reg = DstReg;
731-
while (MachineInstr *UseMI = findOnlyInterestingUse(Reg, MBB, MRI, TII,IsCopy,
732-
NewReg, IsDstPhys, LIS)) {
750+
while (MachineInstr *UseMI =
751+
findOnlyInterestingUse(Reg, MBB, IsCopy, NewReg, IsDstPhys)) {
733752
if (IsCopy && !Processed.insert(UseMI).second)
734753
break;
735754

@@ -781,7 +800,7 @@ void TwoAddressInstructionPass::processCopy(MachineInstr *MI) {
781800

782801
bool IsSrcPhys, IsDstPhys;
783802
Register SrcReg, DstReg;
784-
if (!isCopyToReg(*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
803+
if (!isCopyToReg(*MI, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
785804
return;
786805

787806
if (IsDstPhys && !IsSrcPhys) {
@@ -865,7 +884,7 @@ bool TwoAddressInstructionPass::rescheduleMIBelowKill(
865884
Defs.push_back(MOReg);
866885
else {
867886
Uses.push_back(MOReg);
868-
if (MOReg != Reg && isPlainlyKilled(MO, LIS))
887+
if (MOReg != Reg && isPlainlyKilled(MO))
869888
Kills.push_back(MOReg);
870889
}
871890
}
@@ -876,7 +895,7 @@ bool TwoAddressInstructionPass::rescheduleMIBelowKill(
876895
MachineBasicBlock::iterator End = AfterMI;
877896
while (End != MBB->end()) {
878897
End = skipDebugInstructionsForward(End, MBB->end());
879-
if (End->isCopy() && regOverlapsSet(Defs, End->getOperand(1).getReg(), TRI))
898+
if (End->isCopy() && regOverlapsSet(Defs, End->getOperand(1).getReg()))
880899
Defs.push_back(End->getOperand(0).getReg());
881900
else
882901
break;
@@ -905,20 +924,20 @@ bool TwoAddressInstructionPass::rescheduleMIBelowKill(
905924
if (!MOReg)
906925
continue;
907926
if (MO.isDef()) {
908-
if (regOverlapsSet(Uses, MOReg, TRI))
927+
if (regOverlapsSet(Uses, MOReg))
909928
// Physical register use would be clobbered.
910929
return false;
911-
if (!MO.isDead() && regOverlapsSet(Defs, MOReg, TRI))
930+
if (!MO.isDead() && regOverlapsSet(Defs, MOReg))
912931
// May clobber a physical register def.
913932
// FIXME: This may be too conservative. It's ok if the instruction
914933
// is sunken completely below the use.
915934
return false;
916935
} else {
917-
if (regOverlapsSet(Defs, MOReg, TRI))
936+
if (regOverlapsSet(Defs, MOReg))
918937
return false;
919-
bool isKill = isPlainlyKilled(MO, LIS);
920-
if (MOReg != Reg && ((isKill && regOverlapsSet(Uses, MOReg, TRI)) ||
921-
regOverlapsSet(Kills, MOReg, TRI)))
938+
bool isKill = isPlainlyKilled(MO);
939+
if (MOReg != Reg && ((isKill && regOverlapsSet(Uses, MOReg)) ||
940+
regOverlapsSet(Kills, MOReg)))
922941
// Don't want to extend other live ranges and update kills.
923942
return false;
924943
if (MOReg == Reg && !isKill)
@@ -1044,7 +1063,7 @@ bool TwoAddressInstructionPass::rescheduleKillAboveMI(
10441063
continue;
10451064
if (isDefTooClose(MOReg, DI->second, MI))
10461065
return false;
1047-
bool isKill = isPlainlyKilled(MO, LIS);
1066+
bool isKill = isPlainlyKilled(MO);
10481067
if (MOReg == Reg && !isKill)
10491068
return false;
10501069
Uses.push_back(MOReg);
@@ -1079,14 +1098,14 @@ bool TwoAddressInstructionPass::rescheduleKillAboveMI(
10791098
if (!MOReg)
10801099
continue;
10811100
if (MO.isUse()) {
1082-
if (regOverlapsSet(Defs, MOReg, TRI))
1101+
if (regOverlapsSet(Defs, MOReg))
10831102
// Moving KillMI can clobber the physical register if the def has
10841103
// not been seen.
10851104
return false;
1086-
if (regOverlapsSet(Kills, MOReg, TRI))
1105+
if (regOverlapsSet(Kills, MOReg))
10871106
// Don't want to extend other live ranges and update kills.
10881107
return false;
1089-
if (&OtherMI != MI && MOReg == Reg && !isPlainlyKilled(MO, LIS))
1108+
if (&OtherMI != MI && MOReg == Reg && !isPlainlyKilled(MO))
10901109
// We can't schedule across a use of the register in question.
10911110
return false;
10921111
} else {
@@ -1096,9 +1115,9 @@ bool TwoAddressInstructionPass::rescheduleKillAboveMI(
10961115

10971116
for (unsigned i = 0, e = OtherDefs.size(); i != e; ++i) {
10981117
Register MOReg = OtherDefs[i];
1099-
if (regOverlapsSet(Uses, MOReg, TRI))
1118+
if (regOverlapsSet(Uses, MOReg))
11001119
return false;
1101-
if (MOReg.isPhysical() && regOverlapsSet(LiveDefs, MOReg, TRI))
1120+
if (MOReg.isPhysical() && regOverlapsSet(LiveDefs, MOReg))
11021121
return false;
11031122
// Physical register def is seen.
11041123
llvm::erase_value(Defs, MOReg);
@@ -1169,7 +1188,7 @@ bool TwoAddressInstructionPass::tryInstructionCommute(MachineInstr *MI,
11691188

11701189
// If OtherOp dies but BaseOp does not, swap the OtherOp and BaseOp
11711190
// operands. This makes the live ranges of DstOp and OtherOp joinable.
1172-
bool OtherOpKilled = isKilled(*MI, OtherOpReg, MRI, TII, LIS, false);
1191+
bool OtherOpKilled = isKilled(*MI, OtherOpReg, false);
11731192
bool DoCommute = !BaseOpKilled && OtherOpKilled;
11741193

11751194
if (!DoCommute &&
@@ -1220,7 +1239,7 @@ tryInstructionTransform(MachineBasicBlock::iterator &mi,
12201239
Register regB = MI.getOperand(SrcIdx).getReg();
12211240

12221241
assert(regB.isVirtual() && "cannot make instruction into two-address form");
1223-
bool regBKilled = isKilled(MI, regB, MRI, TII, LIS, true);
1242+
bool regBKilled = isKilled(MI, regB, true);
12241243

12251244
if (regA.isVirtual())
12261245
scanUses(regA);
@@ -1252,7 +1271,7 @@ tryInstructionTransform(MachineBasicBlock::iterator &mi,
12521271
// confusing the three address conversion below.
12531272
if (Commuted) {
12541273
regB = MI.getOperand(SrcIdx).getReg();
1255-
regBKilled = isKilled(MI, regB, MRI, TII, LIS, true);
1274+
regBKilled = isKilled(MI, regB, true);
12561275
}
12571276

12581277
if (MI.isConvertibleTo3Addr()) {

0 commit comments

Comments
 (0)