Skip to content

Commit

Permalink
[NFC] Refactor looping over recomputeLiveIns into function (#88040)
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
redstar committed Apr 15, 2024
1 parent a855eea commit 21d1770
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 61 deletions.
18 changes: 18 additions & 0 deletions llvm/include/llvm/CodeGen/LivePhysRegs.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

namespace llvm {

template <typename T> class ArrayRef;

class MachineInstr;
class MachineFunction;
class MachineOperand;
Expand Down Expand Up @@ -207,6 +209,22 @@ static inline bool recomputeLiveIns(MachineBasicBlock &MBB) {
return oldLiveIns != newLiveIns;
}

/// Convenience function for recomputing live-in's for a set of MBBs until the
/// computation converges.
inline void fullyRecomputeLiveIns(ArrayRef<MachineBasicBlock *> MBBs) {
MachineBasicBlock *const *Data = MBBs.data();
const size_t Len = MBBs.size();
while (true) {
bool AnyChange = false;
for (size_t I = 0; I < Len; ++I)
if (recomputeLiveIns(*Data[I]))
AnyChange = true;
if (!AnyChange)
return;
}
}


} // end namespace llvm

#endif // LLVM_CODEGEN_LIVEPHYSREGS_H
8 changes: 2 additions & 6 deletions llvm/lib/CodeGen/BranchFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2047,12 +2047,8 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
MBB->splice(Loc, TBB, TBB->begin(), TIB);
FBB->erase(FBB->begin(), FIB);

if (UpdateLiveIns) {
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*TBB) || recomputeLiveIns(*FBB);
} while (anyChange);
}
if (UpdateLiveIns)
fullyRecomputeLiveIns({TBB, FBB});

++NumHoist;
return true;
Expand Down
5 changes: 1 addition & 4 deletions llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4325,10 +4325,7 @@ AArch64FrameLowering::inlineStackProbeLoopExactMultiple(
ExitMBB->transferSuccessorsAndUpdatePHIs(&MBB);
MBB.addSuccessor(LoopMBB);
// Update liveins.
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*LoopMBB);
} while (anyChange);
fullyRecomputeLiveIns({ExitMBB, LoopMBB});

return ExitMBB->begin();
}
Expand Down
11 changes: 2 additions & 9 deletions llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9556,15 +9556,8 @@ AArch64InstrInfo::probedStackAlloc(MachineBasicBlock::iterator MBBI,
MBB.addSuccessor(LoopTestMBB);

// Update liveins.
if (MF.getRegInfo().reservedRegsFrozen()) {
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ExitMBB) ||
recomputeLiveIns(*LoopBodyMBB) ||
recomputeLiveIns(*LoopTestMBB);
} while (anyChange);
;
}
if (MF.getRegInfo().reservedRegsFrozen())
fullyRecomputeLiveIns({ExitMBB, LoopBodyMBB, LoopTestMBB});

return ExitMBB->begin();
}
Expand Down
8 changes: 1 addition & 7 deletions llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1806,13 +1806,7 @@ void ARMLowOverheadLoops::Expand(LowOverheadLoop &LoLoop) {
PostOrderLoopTraversal DFS(LoLoop.ML, *MLI);
DFS.ProcessLoop();
const SmallVectorImpl<MachineBasicBlock*> &PostOrder = DFS.getOrder();
bool anyChange = false;
do {
anyChange = false;
for (auto *MBB : PostOrder) {
anyChange = recomputeLiveIns(*MBB) || anyChange;
}
} while (anyChange);
fullyRecomputeLiveIns(PostOrder);

for (auto *MBB : reverse(PostOrder))
recomputeLivenessFlags(*MBB);
Expand Down
11 changes: 2 additions & 9 deletions llvm/lib/Target/PowerPC/PPCExpandAtomicPseudoInsts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ bool PPCExpandAtomicPseudo::expandAtomicRMW128(
.addMBB(LoopMBB);
CurrentMBB->addSuccessor(LoopMBB);
CurrentMBB->addSuccessor(ExitMBB);
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*LoopMBB);
} while (anyChange);
fullyRecomputeLiveIns({ExitMBB, LoopMBB});
NMBBI = MBB.end();
MI.eraseFromParent();
return true;
Expand Down Expand Up @@ -288,11 +285,7 @@ bool PPCExpandAtomicPseudo::expandAtomicCmpSwap128(
CurrentMBB->addSuccessor(LoopCmpMBB);
CurrentMBB->addSuccessor(ExitMBB);

bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*CmpSuccMBB) ||
recomputeLiveIns(*LoopCmpMBB);
} while (anyChange);
fullyRecomputeLiveIns({ExitMBB, CmpSuccMBB, LoopCmpMBB});
NMBBI = MBB.end();
MI.eraseFromParent();
return true;
Expand Down
11 changes: 2 additions & 9 deletions llvm/lib/Target/PowerPC/PPCFrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1435,11 +1435,7 @@ void PPCFrameLowering::inlineStackProbe(MachineFunction &MF,
ProbeLoopBodyMBB->addSuccessor(ProbeLoopBodyMBB);
}
// Update liveins.
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ProbeExitMBB) ||
recomputeLiveIns(*ProbeLoopBodyMBB);
} while (anyChange);
fullyRecomputeLiveIns({ProbeExitMBB, ProbeLoopBodyMBB});
return ProbeExitMBB;
};
// For case HasBP && MaxAlign > 1, we have to realign the SP by performing
Expand Down Expand Up @@ -1531,10 +1527,7 @@ void PPCFrameLowering::inlineStackProbe(MachineFunction &MF,
buildDefCFAReg(*ExitMBB, ExitMBB->begin(), SPReg);
}
// Update liveins.
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*ExitMBB) || recomputeLiveIns(*LoopMBB);
} while (anyChange);
fullyRecomputeLiveIns({ExitMBB, LoopMBB});
}
}
++NumPrologProbed;
Expand Down
10 changes: 2 additions & 8 deletions llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,7 @@ void SystemZELFFrameLowering::inlineStackProbe(
StackAllocMI->eraseFromParent();
if (DoneMBB != nullptr) {
// Compute the live-in lists for the new blocks.
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*DoneMBB) || recomputeLiveIns(*LoopMBB);
} while (anyChange);
fullyRecomputeLiveIns({DoneMBB, LoopMBB});
}
}

Expand Down Expand Up @@ -1425,10 +1422,7 @@ void SystemZXPLINKFrameLowering::inlineStackProbe(
StackAllocMI->eraseFromParent();

// Compute the live-in lists for the new blocks.
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*StackExtMBB) || recomputeLiveIns(*NextMBB);
} while (anyChange);
fullyRecomputeLiveIns({StackExtMBB, NextMBB});
}

bool SystemZXPLINKFrameLowering::hasFP(const MachineFunction &MF) const {
Expand Down
11 changes: 2 additions & 9 deletions llvm/lib/Target/X86/X86FrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,10 +885,7 @@ void X86FrameLowering::emitStackProbeInlineGenericLoop(
}

// Update Live In information
bool anyChange = false;
do {
anyChange = recomputeLiveIns(*tailMBB) || recomputeLiveIns(*testMBB);
} while (anyChange);
fullyRecomputeLiveIns({tailMBB, testMBB});
}

void X86FrameLowering::emitStackProbeInlineWindowsCoreCLR64(
Expand Down Expand Up @@ -1380,11 +1377,7 @@ void X86FrameLowering::BuildStackAlignAND(MachineBasicBlock &MBB,
footMBB->addSuccessor(&MBB);
}

bool anyChange = false;
do {
anyChange = recomputeLiveIns(*footMBB) || recomputeLiveIns(*bodyMBB) ||
recomputeLiveIns(*headMBB) || recomputeLiveIns(MBB);
} while (anyChange);
fullyRecomputeLiveIns({footMBB, bodyMBB, headMBB, &MBB});
}
} else {
MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(AndOp), Reg)
Expand Down

0 comments on commit 21d1770

Please sign in to comment.