Skip to content

Commit 21d1770

Browse files
authored
[NFC] Refactor looping over recomputeLiveIns into function (#88040)
#79940 put calls to recomputeLiveIns into a loop, to repeatedly call the function until the computation converges. However, this repeats a lot of code. This changes moves the loop into a function to simplify the handling. Note that this changes the order in which recomputeLiveIns is called. For example, ``` bool anyChange = false; do { anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*LoopMBB); } while (anyChange); ``` only begins to recompute the live-ins for LoopMBB after the computation for ExitMBB has converged. With this change, all basic blocks have a recomputation of the live-ins for each loop iteration. This can result in less or more calls, depending on the situation.
1 parent a855eea commit 21d1770

9 files changed

+32
-61
lines changed

llvm/include/llvm/CodeGen/LivePhysRegs.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
namespace llvm {
4141

42+
template <typename T> class ArrayRef;
43+
4244
class MachineInstr;
4345
class MachineFunction;
4446
class MachineOperand;
@@ -207,6 +209,22 @@ static inline bool recomputeLiveIns(MachineBasicBlock &MBB) {
207209
return oldLiveIns != newLiveIns;
208210
}
209211

212+
/// Convenience function for recomputing live-in's for a set of MBBs until the
213+
/// computation converges.
214+
inline void fullyRecomputeLiveIns(ArrayRef<MachineBasicBlock *> MBBs) {
215+
MachineBasicBlock *const *Data = MBBs.data();
216+
const size_t Len = MBBs.size();
217+
while (true) {
218+
bool AnyChange = false;
219+
for (size_t I = 0; I < Len; ++I)
220+
if (recomputeLiveIns(*Data[I]))
221+
AnyChange = true;
222+
if (!AnyChange)
223+
return;
224+
}
225+
}
226+
227+
210228
} // end namespace llvm
211229

212230
#endif // LLVM_CODEGEN_LIVEPHYSREGS_H

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,12 +2047,8 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
20472047
MBB->splice(Loc, TBB, TBB->begin(), TIB);
20482048
FBB->erase(FBB->begin(), FIB);
20492049

2050-
if (UpdateLiveIns) {
2051-
bool anyChange = false;
2052-
do {
2053-
anyChange = recomputeLiveIns(*TBB) || recomputeLiveIns(*FBB);
2054-
} while (anyChange);
2055-
}
2050+
if (UpdateLiveIns)
2051+
fullyRecomputeLiveIns({TBB, FBB});
20562052

20572053
++NumHoist;
20582054
return true;

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4325,10 +4325,7 @@ AArch64FrameLowering::inlineStackProbeLoopExactMultiple(
43254325
ExitMBB->transferSuccessorsAndUpdatePHIs(&MBB);
43264326
MBB.addSuccessor(LoopMBB);
43274327
// Update liveins.
4328-
bool anyChange = false;
4329-
do {
4330-
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*LoopMBB);
4331-
} while (anyChange);
4328+
fullyRecomputeLiveIns({ExitMBB, LoopMBB});
43324329

43334330
return ExitMBB->begin();
43344331
}

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9556,15 +9556,8 @@ AArch64InstrInfo::probedStackAlloc(MachineBasicBlock::iterator MBBI,
95569556
MBB.addSuccessor(LoopTestMBB);
95579557

95589558
// Update liveins.
9559-
if (MF.getRegInfo().reservedRegsFrozen()) {
9560-
bool anyChange = false;
9561-
do {
9562-
anyChange = recomputeLiveIns(*ExitMBB) ||
9563-
recomputeLiveIns(*LoopBodyMBB) ||
9564-
recomputeLiveIns(*LoopTestMBB);
9565-
} while (anyChange);
9566-
;
9567-
}
9559+
if (MF.getRegInfo().reservedRegsFrozen())
9560+
fullyRecomputeLiveIns({ExitMBB, LoopBodyMBB, LoopTestMBB});
95689561

95699562
return ExitMBB->begin();
95709563
}

llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,13 +1806,7 @@ void ARMLowOverheadLoops::Expand(LowOverheadLoop &LoLoop) {
18061806
PostOrderLoopTraversal DFS(LoLoop.ML, *MLI);
18071807
DFS.ProcessLoop();
18081808
const SmallVectorImpl<MachineBasicBlock*> &PostOrder = DFS.getOrder();
1809-
bool anyChange = false;
1810-
do {
1811-
anyChange = false;
1812-
for (auto *MBB : PostOrder) {
1813-
anyChange = recomputeLiveIns(*MBB) || anyChange;
1814-
}
1815-
} while (anyChange);
1809+
fullyRecomputeLiveIns(PostOrder);
18161810

18171811
for (auto *MBB : reverse(PostOrder))
18181812
recomputeLivenessFlags(*MBB);

llvm/lib/Target/PowerPC/PPCExpandAtomicPseudoInsts.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,7 @@ bool PPCExpandAtomicPseudo::expandAtomicRMW128(
208208
.addMBB(LoopMBB);
209209
CurrentMBB->addSuccessor(LoopMBB);
210210
CurrentMBB->addSuccessor(ExitMBB);
211-
bool anyChange = false;
212-
do {
213-
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*LoopMBB);
214-
} while (anyChange);
211+
fullyRecomputeLiveIns({ExitMBB, LoopMBB});
215212
NMBBI = MBB.end();
216213
MI.eraseFromParent();
217214
return true;
@@ -288,11 +285,7 @@ bool PPCExpandAtomicPseudo::expandAtomicCmpSwap128(
288285
CurrentMBB->addSuccessor(LoopCmpMBB);
289286
CurrentMBB->addSuccessor(ExitMBB);
290287

291-
bool anyChange = false;
292-
do {
293-
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*CmpSuccMBB) ||
294-
recomputeLiveIns(*LoopCmpMBB);
295-
} while (anyChange);
288+
fullyRecomputeLiveIns({ExitMBB, CmpSuccMBB, LoopCmpMBB});
296289
NMBBI = MBB.end();
297290
MI.eraseFromParent();
298291
return true;

llvm/lib/Target/PowerPC/PPCFrameLowering.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,11 +1435,7 @@ void PPCFrameLowering::inlineStackProbe(MachineFunction &MF,
14351435
ProbeLoopBodyMBB->addSuccessor(ProbeLoopBodyMBB);
14361436
}
14371437
// Update liveins.
1438-
bool anyChange = false;
1439-
do {
1440-
anyChange = recomputeLiveIns(*ProbeExitMBB) ||
1441-
recomputeLiveIns(*ProbeLoopBodyMBB);
1442-
} while (anyChange);
1438+
fullyRecomputeLiveIns({ProbeExitMBB, ProbeLoopBodyMBB});
14431439
return ProbeExitMBB;
14441440
};
14451441
// For case HasBP && MaxAlign > 1, we have to realign the SP by performing
@@ -1531,10 +1527,7 @@ void PPCFrameLowering::inlineStackProbe(MachineFunction &MF,
15311527
buildDefCFAReg(*ExitMBB, ExitMBB->begin(), SPReg);
15321528
}
15331529
// Update liveins.
1534-
bool anyChange = false;
1535-
do {
1536-
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*LoopMBB);
1537-
} while (anyChange);
1530+
fullyRecomputeLiveIns({ExitMBB, LoopMBB});
15381531
}
15391532
}
15401533
++NumPrologProbed;

llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -824,10 +824,7 @@ void SystemZELFFrameLowering::inlineStackProbe(
824824
StackAllocMI->eraseFromParent();
825825
if (DoneMBB != nullptr) {
826826
// Compute the live-in lists for the new blocks.
827-
bool anyChange = false;
828-
do {
829-
anyChange = recomputeLiveIns(*DoneMBB) || recomputeLiveIns(*LoopMBB);
830-
} while (anyChange);
827+
fullyRecomputeLiveIns({DoneMBB, LoopMBB});
831828
}
832829
}
833830

@@ -1425,10 +1422,7 @@ void SystemZXPLINKFrameLowering::inlineStackProbe(
14251422
StackAllocMI->eraseFromParent();
14261423

14271424
// Compute the live-in lists for the new blocks.
1428-
bool anyChange = false;
1429-
do {
1430-
anyChange = recomputeLiveIns(*StackExtMBB) || recomputeLiveIns(*NextMBB);
1431-
} while (anyChange);
1425+
fullyRecomputeLiveIns({StackExtMBB, NextMBB});
14321426
}
14331427

14341428
bool SystemZXPLINKFrameLowering::hasFP(const MachineFunction &MF) const {

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -885,10 +885,7 @@ void X86FrameLowering::emitStackProbeInlineGenericLoop(
885885
}
886886

887887
// Update Live In information
888-
bool anyChange = false;
889-
do {
890-
anyChange = recomputeLiveIns(*tailMBB) || recomputeLiveIns(*testMBB);
891-
} while (anyChange);
888+
fullyRecomputeLiveIns({tailMBB, testMBB});
892889
}
893890

894891
void X86FrameLowering::emitStackProbeInlineWindowsCoreCLR64(
@@ -1380,11 +1377,7 @@ void X86FrameLowering::BuildStackAlignAND(MachineBasicBlock &MBB,
13801377
footMBB->addSuccessor(&MBB);
13811378
}
13821379

1383-
bool anyChange = false;
1384-
do {
1385-
anyChange = recomputeLiveIns(*footMBB) || recomputeLiveIns(*bodyMBB) ||
1386-
recomputeLiveIns(*headMBB) || recomputeLiveIns(MBB);
1387-
} while (anyChange);
1380+
fullyRecomputeLiveIns({footMBB, bodyMBB, headMBB, &MBB});
13881381
}
13891382
} else {
13901383
MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(AndOp), Reg)

0 commit comments

Comments
 (0)