[llvm] [RISCV] Fold `addi` into load / store even if they are in different BBs. (PR #67024)
Michael Maitland via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 21 20:23:05 PDT 2023
================
@@ -2278,6 +2279,81 @@ bool RISCVTargetLowering::isLegalElementTypeForRVV(EVT ScalarTy) const {
}
}
+static bool tryToFoldInstIntoUse(MachineInstr &UseMI, MachineInstr &MI) {
+
+ if (MI.getOpcode() != RISCV::ADDI)
+ return false;
+ if (!(MI.getOperand(0).isReg() && MI.getOperand(1).isReg()))
+ return false;
+
+ switch (UseMI.getOpcode()) {
+ default:
+ return false;
+ case RISCV::LB:
+ case RISCV::LH:
+ case RISCV::LW:
+ case RISCV::LD:
+ case RISCV::LBU:
+ case RISCV::LHU:
+ case RISCV::SB:
+ case RISCV::SH:
+ case RISCV::SW:
+ case RISCV::SD:
+ break;
+ }
+ MachineOperand &OriginalBaseMO = UseMI.getOperand(1);
+ if (!OriginalBaseMO.isReg())
+ return false;
+ if (OriginalBaseMO.getReg() != MI.getOperand(0).getReg())
+ return false;
+
+ MachineOperand &OriginalOffsetMO = UseMI.getOperand(2);
+ MachineOperand &ADDIOffsetMO = MI.getOperand(2);
+ if (!(OriginalOffsetMO.isImm() && ADDIOffsetMO.isImm()))
+ return false;
+
+ int64_t OriginalOffset = OriginalOffsetMO.getImm();
+ int64_t ADDIOffset = ADDIOffsetMO.getImm();
+ int64_t TotalOffset = OriginalOffset + ADDIOffset;
+ if (!isInt<12>(TotalOffset))
+ return false;
+
+ OriginalOffsetMO.setImm(TotalOffset);
+ OriginalBaseMO.setReg(MI.getOperand(1).getReg());
+ NumADDIsMerged++;
+ return true;
+}
+
+void RISCVTargetLowering::finalizeLowering(MachineFunction &MF) const {
+ TargetLoweringBase::finalizeLowering(MF);
+ MachineRegisterInfo &MRI = MF.getRegInfo();
+
+ SmallVector<MachineInstr *, 8> ToErase;
+ for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
+ MachineBasicBlock *MBB = &*I;
+ for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
+ MBBI != MBBE;) {
+ MachineInstr &MI = *MBBI++;
+ if (MI.getOpcode() != RISCV::ADDI)
----------------
michaelmaitland wrote:
I think you already check `Opcode == ADDI` and `Operand0.isReg()` in `tryToFoldInstIntoUse`
https://github.com/llvm/llvm-project/pull/67024
More information about the llvm-commits
mailing list