Skip to content

Refactor recomputeLiveIns to operate on whole CFG #79498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions llvm/include/llvm/CodeGen/LivePhysRegs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "llvm/ADT/SparseSet.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/MC/MCRegister.h"
#include "llvm/MC/MCRegisterInfo.h"
Expand Down Expand Up @@ -193,11 +194,31 @@ void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs);
void computeAndAddLiveIns(LivePhysRegs &LiveRegs,
MachineBasicBlock &MBB);

/// Convenience function for recomputing live-in's for \p MBB.
static inline void recomputeLiveIns(MachineBasicBlock &MBB) {
/// Function to update the live-in's for a basic block and return whether any
/// changes were made.
static inline bool updateBlockLiveInfo(MachineBasicBlock &MBB) {
LivePhysRegs LPR;
auto oldLiveIns = MBB.getLiveIns();

MBB.clearLiveIns();
computeAndAddLiveIns(LPR, MBB);
MBB.sortUniqueLiveIns();

auto newLiveIns = MBB.getLiveIns();
return oldLiveIns != newLiveIns;
}

/// Convenience function for recomputing live-in's for the entire CFG until
/// convergence is reached.
static inline void recomputeLiveIns(MachineFunction &MF) {
bool anyChanged;
do {
anyChanged = false;
for (auto MFI = MF.rbegin(), MFE = MF.rend(); MFI != MFE; ++MFI) {
MachineBasicBlock &MBB = *MFI;
anyChanged |= updateBlockLiveInfo(MBB);
}
} while (anyChanged);
}

} // end namespace llvm
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/CodeGen/MachineBasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class MachineBasicBlock

RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask)
: PhysReg(PhysReg), LaneMask(LaneMask) {}

bool operator==(const RegisterMaskPair &other) const {
return PhysReg == other.PhysReg && LaneMask == other.LaneMask;
}
};

private:
Expand Down Expand Up @@ -473,6 +477,8 @@ class MachineBasicBlock
/// Remove entry from the livein set and return iterator to the next.
livein_iterator removeLiveIn(livein_iterator I);

std::vector<RegisterMaskPair> getLiveIns() const { return LiveIns; }

class liveout_iterator {
public:
using iterator_category = std::input_iterator_tag;
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/CodeGen/BranchFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2048,8 +2048,7 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
FBB->erase(FBB->begin(), FIB);

if (UpdateLiveIns) {
recomputeLiveIns(*TBB);
recomputeLiveIns(*FBB);
recomputeLiveIns(*MBB->getParent());
}

++NumHoist;
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4339,8 +4339,7 @@ AArch64FrameLowering::inlineStackProbeLoopExactMultiple(
ExitMBB->transferSuccessorsAndUpdatePHIs(&MBB);
MBB.addSuccessor(LoopMBB);
// Update liveins.
recomputeLiveIns(*LoopMBB);
recomputeLiveIns(*ExitMBB);
recomputeLiveIns(MF);

return ExitMBB->begin();
}
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9597,9 +9597,7 @@ AArch64InstrInfo::probedStackAlloc(MachineBasicBlock::iterator MBBI,

// Update liveins.
if (MF.getRegInfo().reservedRegsFrozen()) {
recomputeLiveIns(*LoopTestMBB);
recomputeLiveIns(*LoopBodyMBB);
recomputeLiveIns(*ExitMBB);
recomputeLiveIns(MF);
}

return ExitMBB->begin();
Expand Down
7 changes: 1 addition & 6 deletions llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1806,12 +1806,7 @@ void ARMLowOverheadLoops::Expand(LowOverheadLoop &LoLoop) {
PostOrderLoopTraversal DFS(LoLoop.ML, *MLI);
DFS.ProcessLoop();
const SmallVectorImpl<MachineBasicBlock*> &PostOrder = DFS.getOrder();
for (auto *MBB : PostOrder) {
recomputeLiveIns(*MBB);
// FIXME: For some reason, the live-in print order is non-deterministic for
// our tests and I can't out why... So just sort them.
MBB->sortUniqueLiveIns();
}
recomputeLiveIns(*LoLoop.MF);

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

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

Expand Down Expand Up @@ -1439,8 +1438,7 @@ void SystemZXPLINKFrameLowering::inlineStackProbe(
StackAllocMI->eraseFromParent();

// Compute the live-in lists for the new blocks.
recomputeLiveIns(*NextMBB);
recomputeLiveIns(*StackExtMBB);
recomputeLiveIns(MF);
}

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

// Update Live In information
recomputeLiveIns(*testMBB);
recomputeLiveIns(*tailMBB);
recomputeLiveIns(MF);
}

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

recomputeLiveIns(*headMBB);
recomputeLiveIns(*bodyMBB);
recomputeLiveIns(*footMBB);
recomputeLiveIns(MBB);
recomputeLiveIns(MF);
}
} else {
MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(AndOp), Reg)
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/AArch64/stack-probing-last-in-block.mir
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ body: |
; CHECK-LABEL: name: f
; CHECK: bb.0.entry:
; CHECK-NEXT: successors: %bb.3(0x80000000)
; CHECK-NEXT: liveins: $lr, $fp
; CHECK-NEXT: liveins: $fp, $lr
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: early-clobber $sp = frame-setup STPXpre killed $fp, killed $lr, $sp, -2 :: (store (s64) into %stack.2), (store (s64) into %stack.1)
; CHECK-NEXT: frame-setup CFI_INSTRUCTION def_cfa_offset 16
Expand Down
36 changes: 25 additions & 11 deletions llvm/test/CodeGen/SystemZ/branch-folder-hoist-livein.mir
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
# RUN: llc -verify-machineinstrs -O1 -mtriple=s390x-ibm-linux -o - %s -run-pass=branch-folder | FileCheck %s
--- |
target datalayout = "E-m:e-i1:8:16-i8:8:16-i64:64-f128:64-a:8:16-n32:64"
Expand All @@ -15,6 +16,30 @@
name: f1
tracksRegLiveness: true
body: |
; CHECK-LABEL: name: f1
; CHECK: bb.0:
; CHECK-NEXT: successors: %bb.2(0x7fffffff), %bb.1(0x00000001)
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: renamable $r1d = LGRL @b :: (load (s32) from got, align 8)
; CHECK-NEXT: renamable $r1l = LH killed renamable $r1d, 0, $noreg, implicit-def $r1d :: (dereferenceable load (s8) from @b)
; CHECK-NEXT: renamable $r2l = LHI 0
; CHECK-NEXT: renamable $r3d = LGRL @d :: (load (s32) from got, align 8)
; CHECK-NEXT: renamable $r4d = LLILL 0, implicit-def $r4q
; CHECK-NEXT: renamable $r4d = COPY killed renamable $r4d, implicit killed $r4q
; CHECK-NEXT: CHI killed renamable $r2l, 0, implicit-def $cc
; CHECK-NEXT: BRC 14, 6, %bb.2, implicit killed $cc
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.1:
; CHECK-NEXT: successors:
; CHECK-NEXT: liveins: $r3d, $r4d, $r1l
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: STH renamable $r1l, killed renamable $r3d, 0, $noreg, implicit killed $r4d :: (store (s8) into @d)
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.2:
; CHECK-NEXT: liveins: $r3d, $r4d, $r1l
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: STH renamable $r1l, killed renamable $r3d, 0, $noreg, implicit killed $r4d :: (store (s8) into @d)
; CHECK-NEXT: Return
bb.0:
successors: %bb.2(0x7fffffff), %bb.1(0x00000001)
liveins:
Expand Down Expand Up @@ -44,14 +69,3 @@ body: |
Return

...

# CHECK: renamable $r4d = COPY killed renamable $r4d, implicit killed $r4q
# CHECK-NEXT: CHI killed renamable $r2l, 0, implicit-def $cc
# CHECK-NEXT: BRC 14, 6, %bb.2, implicit killed $cc
# CHECK-NEXT: {{^ $}}
# CHECK-NEXT: bb.1:
# CHECK-NEXT: successors:
# CHECK-NEXT: liveins: $r1l, $r3d, $r4d

# CHECK: bb.2:
# CHECK-NEXT: liveins: $r1l, $r3d, $r4d
Loading