[llvm] [AArch64MIPeepholeOpt] Coalesce sibling base-address materializations (PR #223682)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 21 23:26:05 PDT 2026
================
@@ -957,6 +966,106 @@ bool AArch64MIPeepholeOptImpl::visitCopy(MachineInstr &MI) {
return true;
}
+bool AArch64MIPeepholeOptImpl::shareBaseAddresses(MachineBasicBlock &MBB) {
+ if (!EnableBaseAddressCSE)
+ return false;
+
+ bool Changed = false;
+
+ struct Cand {
+ MachineInstr *DefMI;
+ Register SrcReg;
+ unsigned Opc;
+ int64_t C;
+ Register BaseReg;
+ MachineInstr *UserMI;
+ int Scale;
+ int64_t OldOff;
+ };
+ SmallVector<Cand, 8> Cands;
+ using Key = std::pair<Register, unsigned>;
+ MapVector<Key, SmallVector<unsigned, 4>> Groups;
+
+ DenseMap<MachineInstr *, unsigned> Pos;
+ unsigned Idx = 0;
+ for (MachineInstr &MI : MBB.instrs()) {
+ Pos[&MI] = Idx++;
+ unsigned Opc = MI.getOpcode();
+ if (Opc != AArch64::ADDXri && Opc != AArch64::SUBXri)
+ continue;
+ if (!MI.getOperand(2).isImm() || !MI.getOperand(3).isImm() ||
+ MI.getOperand(3).getImm() != 0) // shift must be 0 (no LSL #12)
+ continue;
+ Register SrcReg = MI.getOperand(1).getReg();
+ Register BaseReg = MI.getOperand(0).getReg();
+ if (!SrcReg.isVirtual() || !BaseReg.isVirtual() ||
+ !MRI->hasOneUse(BaseReg)) // secondary def is erased below
+ continue;
+ MachineInstr &User = *MRI->use_instr_begin(BaseReg);
+ if (!User.mayLoadOrStore() || AArch64InstrInfo::isPairedLdSt(User) ||
+ AArch64InstrInfo::isPreLdSt(User) || // different operand layout
+ TII->hasUnscaledLdStOffset(User) || // rewrite below assumes scaled
+ !AArch64InstrInfo::getLdStOffsetOp(User).isImm())
+ continue;
----------------
whokeke wrote:
Added a comment above the exclusion list. After the four checks (mayLoadOrStore, !isPairedLdSt, !isPreLdSt, !hasUnscaledLdStOffset, offset isImm()), what's left is a scaled, non-paired, non-pre-indexed load/store with an immediate offset — the exact shape the base+uimm12-offset rewrite below assumes. I kept the exclusion style rather than switching to an inclusive opcode allowlist, since isPairedLdSt/isPreLdSt/hasUnscaledLdStOffset are existing shared helpers that already partition the opcode space correctly, and re-deriving an allowlist here risks missing an opcode they cover. Happy to switch if you'd still prefer an explicit list.
https://github.com/llvm/llvm-project/pull/223682
More information about the llvm-commits
mailing list