[llvm] [AArch64] Cache AArch64RegisterInfo::isAnyArgRegReserved (PR #190957)

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 8 05:45:59 PDT 2026


================
@@ -577,9 +577,18 @@ bool AArch64RegisterInfo::isStrictlyReservedReg(const MachineFunction &MF,
 }
 
 bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const {
-  return llvm::any_of(*AArch64::GPR64argRegClass.MC, [this, &MF](MCPhysReg r) {
-    return isStrictlyReservedReg(MF, r);
-  });
+  auto *AFI = MF.getInfo<AArch64FunctionInfo>();
+  if (std::optional<bool> Cached = AFI->getAnyArgRegReserved())
+    return *Cached;
+
+  bool Any =
+      llvm::any_of(*AArch64::GPR64argRegClass.MC, [this, &MF](MCPhysReg r) {
+        return isStrictlyReservedReg(MF, r);
+      });
+  // Re-computing this in getStrictlyReservedRegs during call lowering for
+  // every call is compile-time expensive, so we avoid by caching the result.
+  AFI->setAnyArgRegReserved(Any);
+  return Any;
----------------
MacDue wrote:

Looking at the original patch that introduced this (https://reviews.llvm.org/D48580), it looks like this could have been equivalently implemented as: 

```
bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const {
  auto &ST = MF.getSubtarget<AArch64Subtarget>();
  return llvm::any_of(*AArch64::GPR64argRegClass.MC, [&ST](MCPhysReg r) {
    return ST.isXRegisterReserved(r - AArch64::X0);
  });
}
```

As the intent was to guard against using `+reserve-x{0-7}` with function calls. It looks like implementing it terms of `isXRegisterReserved()` should be faster (since it's just a bitvector lookup). Could you check if this results in a similar speedup?

https://github.com/llvm/llvm-project/pull/190957


More information about the llvm-commits mailing list