Skip to content

Commit 8310fd3

Browse files
[MachineLICM] Give opportunity to analyze physregs for invariance. (#84779)
At the moment MachineLoopInfo has a very simple way to determine if a use of a physical register will be invariant: if it is not a constant value or if it's not an ignorable use, then it's not considered invariant. From a compile-time performance perspective this makes a lot of sense, but it limits code that uses implicit physical registers from being hoisted until the later MachineLICM pass (after register allocation), which has a lot fewer opportunities to hoist. For AArch64 SME we use an implicit physical register ($vg) to avoid rematerialization beyond certain instructions. Doing this led to regressions because simple expressions were no longer hoisted by Early MachineLICM. This patch adds some extra checks to 'isLoopInvariant' to see if any of the defs are found in the loop. If not, we can considered it loop invariant. We expect the impact on compile-time to be negligible because there is an incentive for users to reduce the need for the smstart/smstop instructions that define $vg. In either case, we've put the functionality under a target interface to limit this only to specific registers.
1 parent 719e077 commit 8310fd3

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-1
lines changed

llvm/include/llvm/CodeGen/MachineLoopInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> {
8989
private:
9090
friend class LoopInfoBase<MachineBasicBlock, MachineLoop>;
9191

92+
/// Returns true if the given physreg has no defs inside the loop.
93+
bool isLoopInvariantImplicitPhysReg(Register Reg) const;
94+
9295
explicit MachineLoop(MachineBasicBlock *MBB)
9396
: LoopBase<MachineBasicBlock, MachineLoop>(MBB) {}
9497

llvm/include/llvm/CodeGen/TargetRegisterInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,12 @@ class TargetRegisterInfo : public MCRegisterInfo {
572572
return false;
573573
}
574574

575+
/// Returns true if MachineLoopInfo should analyze the given physreg
576+
/// for loop invariance.
577+
virtual bool shouldAnalyzePhysregInMachineLoopInfo(MCRegister R) const {
578+
return false;
579+
}
580+
575581
/// Physical registers that may be modified within a function but are
576582
/// guaranteed to be restored before any uses. This is useful for targets that
577583
/// have call sequences where a GOT register may be updated by the caller

llvm/lib/CodeGen/MachineLoopInfo.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,23 @@ MDNode *MachineLoop::getLoopID() const {
198198
return LoopID;
199199
}
200200

201+
bool MachineLoop::isLoopInvariantImplicitPhysReg(Register Reg) const {
202+
MachineFunction *MF = getHeader()->getParent();
203+
MachineRegisterInfo *MRI = &MF->getRegInfo();
204+
205+
if (MRI->isConstantPhysReg(Reg))
206+
return true;
207+
208+
if (!MF->getSubtarget()
209+
.getRegisterInfo()
210+
->shouldAnalyzePhysregInMachineLoopInfo(Reg))
211+
return false;
212+
213+
return !llvm::any_of(
214+
MRI->def_instructions(Reg),
215+
[this, Reg](const MachineInstr &MI) { return this->contains(&MI); });
216+
}
217+
201218
bool MachineLoop::isLoopInvariant(MachineInstr &I,
202219
const Register ExcludeReg) const {
203220
MachineFunction *MF = I.getParent()->getParent();
@@ -226,7 +243,7 @@ bool MachineLoop::isLoopInvariant(MachineInstr &I,
226243
// it could get allocated to something with a def during allocation.
227244
// However, if the physreg is known to always be caller saved/restored
228245
// then this use is safe to hoist.
229-
if (!MRI->isConstantPhysReg(Reg) &&
246+
if (!isLoopInvariantImplicitPhysReg(Reg) &&
230247
!(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
231248
!TII->isIgnorableUse(MO))
232249
return false;

llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,3 +1072,8 @@ bool AArch64RegisterInfo::shouldCoalesce(
10721072

10731073
return true;
10741074
}
1075+
1076+
bool AArch64RegisterInfo::shouldAnalyzePhysregInMachineLoopInfo(
1077+
MCRegister R) const {
1078+
return R == AArch64::VG;
1079+
}

llvm/lib/Target/AArch64/AArch64RegisterInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ class AArch64RegisterInfo final : public AArch64GenRegisterInfo {
145145

146146
void getOffsetOpcodes(const StackOffset &Offset,
147147
SmallVectorImpl<uint64_t> &Ops) const override;
148+
149+
bool shouldAnalyzePhysregInMachineLoopInfo(MCRegister R) const override;
148150
};
149151

150152
} // end namespace llvm
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
2+
# RUN: llc -mtriple=aarch64--linux-gnu -run-pass=early-machinelicm %s -verify-machineinstrs -o - | FileCheck %s
3+
---
4+
name: test_should_hoist_pfalse
5+
tracksRegLiveness: true
6+
body: |
7+
; CHECK-LABEL: name: test_should_hoist_pfalse
8+
; CHECK: bb.0:
9+
; CHECK-NEXT: successors: %bb.1(0x80000000)
10+
; CHECK-NEXT: liveins: $x0, $x1
11+
; CHECK-NEXT: {{ $}}
12+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr64 = COPY $x1
13+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr64 = COPY $x0
14+
; CHECK-NEXT: MSRpstatesvcrImm1 1, 1, csr_aarch64_smstartstop, implicit-def dead $nzcv, implicit $vg, implicit-def $vg
15+
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gpr64all = COPY [[COPY1]]
16+
; CHECK-NEXT: [[COPY3:%[0-9]+]]:gpr64all = COPY [[COPY]]
17+
; CHECK-NEXT: [[PFALSE:%[0-9]+]]:ppr = PFALSE implicit $vg
18+
; CHECK-NEXT: {{ $}}
19+
; CHECK-NEXT: bb.1:
20+
; CHECK-NEXT: successors: %bb.2(0x40000000), %bb.1(0x40000000)
21+
; CHECK-NEXT: {{ $}}
22+
; CHECK-NEXT: [[PHI:%[0-9]+]]:gpr64common = PHI [[COPY2]], %bb.0, %5, %bb.1
23+
; CHECK-NEXT: [[PHI1:%[0-9]+]]:gpr64sp = PHI [[COPY3]], %bb.0, %7, %bb.1
24+
; CHECK-NEXT: STR_PXI [[PFALSE]], [[PHI]], 0
25+
; CHECK-NEXT: [[SUBSXri:%[0-9]+]]:gpr64 = SUBSXri [[PHI1]], 1, 0, implicit-def $nzcv
26+
; CHECK-NEXT: [[COPY4:%[0-9]+]]:gpr64all = COPY [[SUBSXri]]
27+
; CHECK-NEXT: [[INCD_XPiI:%[0-9]+]]:gpr64 = INCD_XPiI [[PHI]], 31, 1
28+
; CHECK-NEXT: [[COPY5:%[0-9]+]]:gpr64all = COPY [[INCD_XPiI]]
29+
; CHECK-NEXT: Bcc 1, %bb.1, implicit $nzcv
30+
; CHECK-NEXT: B %bb.2
31+
; CHECK-NEXT: {{ $}}
32+
; CHECK-NEXT: bb.2:
33+
; CHECK-NEXT: MSRpstatesvcrImm1 1, 0, csr_aarch64_smstartstop, implicit-def dead $nzcv, implicit $vg, implicit-def $vg
34+
; CHECK-NEXT: RET_ReallyLR
35+
bb.0:
36+
successors: %bb.1
37+
liveins: $x0, $x1
38+
39+
%5:gpr64 = COPY $x1
40+
%4:gpr64 = COPY $x0
41+
MSRpstatesvcrImm1 1, 1, csr_aarch64_smstartstop, implicit-def dead $nzcv, implicit $vg, implicit-def $vg
42+
%6:gpr64all = COPY %4
43+
%7:gpr64all = COPY %5
44+
45+
bb.1:
46+
successors: %bb.2, %bb.1
47+
48+
%0:gpr64common = PHI %6, %bb.0, %3, %bb.1
49+
%1:gpr64sp = PHI %7, %bb.0, %2, %bb.1
50+
%8:ppr = PFALSE implicit $vg
51+
STR_PXI killed %8, %0, 0
52+
%9:gpr64 = SUBSXri %1, 1, 0, implicit-def $nzcv
53+
%2:gpr64all = COPY %9
54+
%10:gpr64 = INCD_XPiI %0, 31, 1
55+
%3:gpr64all = COPY %10
56+
57+
58+
Bcc 1, %bb.1, implicit $nzcv
59+
B %bb.2
60+
61+
bb.2:
62+
MSRpstatesvcrImm1 1, 0, csr_aarch64_smstartstop, implicit-def dead $nzcv, implicit $vg, implicit-def $vg
63+
RET_ReallyLR
64+
...

0 commit comments

Comments
 (0)