Skip to content

[MachineLICM] Give opportunity to analyze physregs for invariance. #84779

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 2 commits into from
Mar 15, 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
3 changes: 3 additions & 0 deletions llvm/include/llvm/CodeGen/MachineLoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> {
private:
friend class LoopInfoBase<MachineBasicBlock, MachineLoop>;

/// Returns true if the given physreg has no defs inside the loop.
bool isLoopInvariantImplicitPhysReg(Register Reg) const;

explicit MachineLoop(MachineBasicBlock *MBB)
: LoopBase<MachineBasicBlock, MachineLoop>(MBB) {}

Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/CodeGen/TargetRegisterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ class TargetRegisterInfo : public MCRegisterInfo {
return false;
}

/// Returns true if MachineLoopInfo should analyze the given physreg
/// for loop invariance.
virtual bool shouldAnalyzePhysregInMachineLoopInfo(MCRegister R) const {
return false;
}

/// Physical registers that may be modified within a function but are
/// guaranteed to be restored before any uses. This is useful for targets that
/// have call sequences where a GOT register may be updated by the caller
Expand Down
19 changes: 18 additions & 1 deletion llvm/lib/CodeGen/MachineLoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ MDNode *MachineLoop::getLoopID() const {
return LoopID;
}

bool MachineLoop::isLoopInvariantImplicitPhysReg(Register Reg) const {
MachineFunction *MF = getHeader()->getParent();
MachineRegisterInfo *MRI = &MF->getRegInfo();

if (MRI->isConstantPhysReg(Reg))
return true;

if (!MF->getSubtarget()
.getRegisterInfo()
->shouldAnalyzePhysregInMachineLoopInfo(Reg))
return false;

return !llvm::any_of(
MRI->def_instructions(Reg),
[this, Reg](const MachineInstr &MI) { return this->contains(&MI); });
}

bool MachineLoop::isLoopInvariant(MachineInstr &I,
const Register ExcludeReg) const {
MachineFunction *MF = I.getParent()->getParent();
Expand Down Expand Up @@ -226,7 +243,7 @@ bool MachineLoop::isLoopInvariant(MachineInstr &I,
// it could get allocated to something with a def during allocation.
// However, if the physreg is known to always be caller saved/restored
// then this use is safe to hoist.
if (!MRI->isConstantPhysReg(Reg) &&
if (!isLoopInvariantImplicitPhysReg(Reg) &&
!(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
!TII->isIgnorableUse(MO))
return false;
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1072,3 +1072,8 @@ bool AArch64RegisterInfo::shouldCoalesce(

return true;
}

bool AArch64RegisterInfo::shouldAnalyzePhysregInMachineLoopInfo(
MCRegister R) const {
return R == AArch64::VG;
}
2 changes: 2 additions & 0 deletions llvm/lib/Target/AArch64/AArch64RegisterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ class AArch64RegisterInfo final : public AArch64GenRegisterInfo {

void getOffsetOpcodes(const StackOffset &Offset,
SmallVectorImpl<uint64_t> &Ops) const override;

bool shouldAnalyzePhysregInMachineLoopInfo(MCRegister R) const override;
};

} // end namespace llvm
Expand Down
64 changes: 64 additions & 0 deletions llvm/test/CodeGen/AArch64/sme-machine-licm-vg.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
# RUN: llc -mtriple=aarch64--linux-gnu -run-pass=early-machinelicm %s -verify-machineinstrs -o - | FileCheck %s
---
name: test_should_hoist_pfalse
tracksRegLiveness: true
body: |
; CHECK-LABEL: name: test_should_hoist_pfalse
; CHECK: bb.0:
; CHECK-NEXT: successors: %bb.1(0x80000000)
; CHECK-NEXT: liveins: $x0, $x1
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr64 = COPY $x1
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr64 = COPY $x0
; CHECK-NEXT: MSRpstatesvcrImm1 1, 1, csr_aarch64_smstartstop, implicit-def dead $nzcv, implicit $vg, implicit-def $vg
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr64all = COPY [[COPY1]]
; CHECK-NEXT: [[COPY3:%[0-9]+]]:gpr64all = COPY [[COPY]]
; CHECK-NEXT: [[PFALSE:%[0-9]+]]:ppr = PFALSE implicit $vg
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.1:
; CHECK-NEXT: successors: %bb.2(0x40000000), %bb.1(0x40000000)
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[PHI:%[0-9]+]]:gpr64common = PHI [[COPY2]], %bb.0, %5, %bb.1
; CHECK-NEXT: [[PHI1:%[0-9]+]]:gpr64sp = PHI [[COPY3]], %bb.0, %7, %bb.1
; CHECK-NEXT: STR_PXI [[PFALSE]], [[PHI]], 0
; CHECK-NEXT: [[SUBSXri:%[0-9]+]]:gpr64 = SUBSXri [[PHI1]], 1, 0, implicit-def $nzcv
; CHECK-NEXT: [[COPY4:%[0-9]+]]:gpr64all = COPY [[SUBSXri]]
; CHECK-NEXT: [[INCD_XPiI:%[0-9]+]]:gpr64 = INCD_XPiI [[PHI]], 31, 1
; CHECK-NEXT: [[COPY5:%[0-9]+]]:gpr64all = COPY [[INCD_XPiI]]
; CHECK-NEXT: Bcc 1, %bb.1, implicit $nzcv
; CHECK-NEXT: B %bb.2
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.2:
; CHECK-NEXT: MSRpstatesvcrImm1 1, 0, csr_aarch64_smstartstop, implicit-def dead $nzcv, implicit $vg, implicit-def $vg
; CHECK-NEXT: RET_ReallyLR
bb.0:
successors: %bb.1
liveins: $x0, $x1

%5:gpr64 = COPY $x1
%4:gpr64 = COPY $x0
MSRpstatesvcrImm1 1, 1, csr_aarch64_smstartstop, implicit-def dead $nzcv, implicit $vg, implicit-def $vg
%6:gpr64all = COPY %4
%7:gpr64all = COPY %5

bb.1:
successors: %bb.2, %bb.1

%0:gpr64common = PHI %6, %bb.0, %3, %bb.1
%1:gpr64sp = PHI %7, %bb.0, %2, %bb.1
%8:ppr = PFALSE implicit $vg
STR_PXI killed %8, %0, 0
%9:gpr64 = SUBSXri %1, 1, 0, implicit-def $nzcv
%2:gpr64all = COPY %9
%10:gpr64 = INCD_XPiI %0, 31, 1
%3:gpr64all = COPY %10


Bcc 1, %bb.1, implicit $nzcv
B %bb.2

bb.2:
MSRpstatesvcrImm1 1, 0, csr_aarch64_smstartstop, implicit-def dead $nzcv, implicit $vg, implicit-def $vg
RET_ReallyLR
...