xref: /freebsd/contrib/llvm-project/llvm/lib/Target/RISCV/RISCVPushPopOptimizer.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1*5f757f3fSDimitry Andric //===------- RISCVPushPopOptimizer.cpp - RISC-V Push/Pop opt. pass --------===//
206c3fb27SDimitry Andric //
306c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
506c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606c3fb27SDimitry Andric //
706c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
806c3fb27SDimitry Andric //
9*5f757f3fSDimitry Andric // This file contains a pass that replaces Zcmp POP instructions with
10*5f757f3fSDimitry Andric // POPRET[Z] where possible.
1106c3fb27SDimitry Andric //
1206c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
1306c3fb27SDimitry Andric 
1406c3fb27SDimitry Andric #include "RISCVInstrInfo.h"
1506c3fb27SDimitry Andric #include "RISCVMachineFunctionInfo.h"
1606c3fb27SDimitry Andric 
1706c3fb27SDimitry Andric using namespace llvm;
1806c3fb27SDimitry Andric 
1906c3fb27SDimitry Andric #define RISCV_PUSH_POP_OPT_NAME "RISC-V Zcmp Push/Pop optimization pass"
2006c3fb27SDimitry Andric 
2106c3fb27SDimitry Andric namespace {
2206c3fb27SDimitry Andric struct RISCVPushPopOpt : public MachineFunctionPass {
2306c3fb27SDimitry Andric   static char ID;
2406c3fb27SDimitry Andric 
25*5f757f3fSDimitry Andric   RISCVPushPopOpt() : MachineFunctionPass(ID) {}
2606c3fb27SDimitry Andric 
2706c3fb27SDimitry Andric   const RISCVInstrInfo *TII;
2806c3fb27SDimitry Andric   const TargetRegisterInfo *TRI;
2906c3fb27SDimitry Andric 
3006c3fb27SDimitry Andric   // Track which register units have been modified and used.
3106c3fb27SDimitry Andric   LiveRegUnits ModifiedRegUnits, UsedRegUnits;
3206c3fb27SDimitry Andric 
3306c3fb27SDimitry Andric   bool usePopRet(MachineBasicBlock::iterator &MBBI,
3406c3fb27SDimitry Andric                  MachineBasicBlock::iterator &NextI, bool IsReturnZero);
3506c3fb27SDimitry Andric   bool adjustRetVal(MachineBasicBlock::iterator &MBBI);
3606c3fb27SDimitry Andric   bool runOnMachineFunction(MachineFunction &Fn) override;
3706c3fb27SDimitry Andric 
3806c3fb27SDimitry Andric   StringRef getPassName() const override { return RISCV_PUSH_POP_OPT_NAME; }
3906c3fb27SDimitry Andric };
4006c3fb27SDimitry Andric 
4106c3fb27SDimitry Andric char RISCVPushPopOpt::ID = 0;
4206c3fb27SDimitry Andric 
4306c3fb27SDimitry Andric } // end of anonymous namespace
4406c3fb27SDimitry Andric 
4506c3fb27SDimitry Andric INITIALIZE_PASS(RISCVPushPopOpt, "riscv-push-pop-opt", RISCV_PUSH_POP_OPT_NAME,
4606c3fb27SDimitry Andric                 false, false)
4706c3fb27SDimitry Andric 
4806c3fb27SDimitry Andric // Check if POP instruction was inserted into the MBB and return iterator to it.
4906c3fb27SDimitry Andric static MachineBasicBlock::iterator containsPop(MachineBasicBlock &MBB) {
5006c3fb27SDimitry Andric   for (MachineBasicBlock::iterator MBBI = MBB.begin(); MBBI != MBB.end();
5106c3fb27SDimitry Andric        MBBI = next_nodbg(MBBI, MBB.end()))
5206c3fb27SDimitry Andric     if (MBBI->getOpcode() == RISCV::CM_POP)
5306c3fb27SDimitry Andric       return MBBI;
5406c3fb27SDimitry Andric 
5506c3fb27SDimitry Andric   return MBB.end();
5606c3fb27SDimitry Andric }
5706c3fb27SDimitry Andric 
5806c3fb27SDimitry Andric bool RISCVPushPopOpt::usePopRet(MachineBasicBlock::iterator &MBBI,
5906c3fb27SDimitry Andric                                 MachineBasicBlock::iterator &NextI,
6006c3fb27SDimitry Andric                                 bool IsReturnZero) {
6106c3fb27SDimitry Andric   // Since Pseudo instruction lowering happen later in the pipeline,
6206c3fb27SDimitry Andric   // this will detect all ret instruction.
6306c3fb27SDimitry Andric   DebugLoc DL = NextI->getDebugLoc();
6406c3fb27SDimitry Andric   unsigned Opc = IsReturnZero ? RISCV::CM_POPRETZ : RISCV::CM_POPRET;
6506c3fb27SDimitry Andric   BuildMI(*NextI->getParent(), NextI, DL, TII->get(Opc))
6606c3fb27SDimitry Andric       .add(MBBI->getOperand(0))
6706c3fb27SDimitry Andric       .add(MBBI->getOperand(1));
6806c3fb27SDimitry Andric 
6906c3fb27SDimitry Andric   MBBI->eraseFromParent();
7006c3fb27SDimitry Andric   NextI->eraseFromParent();
7106c3fb27SDimitry Andric   return true;
7206c3fb27SDimitry Andric }
7306c3fb27SDimitry Andric 
7406c3fb27SDimitry Andric // Search for last assignment to a0 and if possible use ret_val slot of POP to
7506c3fb27SDimitry Andric // store return value.
7606c3fb27SDimitry Andric bool RISCVPushPopOpt::adjustRetVal(MachineBasicBlock::iterator &MBBI) {
7706c3fb27SDimitry Andric   MachineBasicBlock::reverse_iterator RE = MBBI->getParent()->rend();
7806c3fb27SDimitry Andric   // Track which register units have been modified and used between the POP
7906c3fb27SDimitry Andric   // insn and the last assignment to register a0.
8006c3fb27SDimitry Andric   ModifiedRegUnits.clear();
8106c3fb27SDimitry Andric   UsedRegUnits.clear();
8206c3fb27SDimitry Andric   // Since POP instruction is in Epilogue no normal instructions will follow
8306c3fb27SDimitry Andric   // after it. Therefore search only previous ones to find the return value.
8406c3fb27SDimitry Andric   for (MachineBasicBlock::reverse_iterator I =
8506c3fb27SDimitry Andric            next_nodbg(MBBI.getReverse(), RE);
8606c3fb27SDimitry Andric        I != RE; I = next_nodbg(I, RE)) {
8706c3fb27SDimitry Andric     MachineInstr &MI = *I;
8806c3fb27SDimitry Andric     if (auto OperandPair = TII->isCopyInstrImpl(MI)) {
8906c3fb27SDimitry Andric       Register DestReg = OperandPair->Destination->getReg();
9006c3fb27SDimitry Andric       Register Source = OperandPair->Source->getReg();
9106c3fb27SDimitry Andric       if (DestReg == RISCV::X10 && Source == RISCV::X0) {
9206c3fb27SDimitry Andric         MI.removeFromParent();
9306c3fb27SDimitry Andric         return true;
9406c3fb27SDimitry Andric       }
9506c3fb27SDimitry Andric     }
9606c3fb27SDimitry Andric     // Update modified / used register units.
9706c3fb27SDimitry Andric     LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits, TRI);
9806c3fb27SDimitry Andric     // If a0 was modified or used, there is no possibility
9906c3fb27SDimitry Andric     // of using ret_val slot of POP instruction.
10006c3fb27SDimitry Andric     if (!ModifiedRegUnits.available(RISCV::X10) ||
10106c3fb27SDimitry Andric         !UsedRegUnits.available(RISCV::X10))
10206c3fb27SDimitry Andric       return false;
10306c3fb27SDimitry Andric   }
10406c3fb27SDimitry Andric   return false;
10506c3fb27SDimitry Andric }
10606c3fb27SDimitry Andric 
10706c3fb27SDimitry Andric bool RISCVPushPopOpt::runOnMachineFunction(MachineFunction &Fn) {
10806c3fb27SDimitry Andric   if (skipFunction(Fn.getFunction()))
10906c3fb27SDimitry Andric     return false;
11006c3fb27SDimitry Andric 
11106c3fb27SDimitry Andric   // If Zcmp extension is not supported, abort.
11206c3fb27SDimitry Andric   const RISCVSubtarget *Subtarget = &Fn.getSubtarget<RISCVSubtarget>();
11306c3fb27SDimitry Andric   if (!Subtarget->hasStdExtZcmp())
11406c3fb27SDimitry Andric     return false;
11506c3fb27SDimitry Andric 
11606c3fb27SDimitry Andric   // If frame pointer elimination has been disabled, abort to avoid breaking the
11706c3fb27SDimitry Andric   // ABI.
11806c3fb27SDimitry Andric   if (Fn.getTarget().Options.DisableFramePointerElim(Fn))
11906c3fb27SDimitry Andric     return false;
12006c3fb27SDimitry Andric 
12106c3fb27SDimitry Andric   TII = Subtarget->getInstrInfo();
12206c3fb27SDimitry Andric   TRI = Subtarget->getRegisterInfo();
12306c3fb27SDimitry Andric   // Resize the modified and used register unit trackers.  We do this once
12406c3fb27SDimitry Andric   // per function and then clear the register units each time we determine
12506c3fb27SDimitry Andric   // correct return value for the POP.
12606c3fb27SDimitry Andric   ModifiedRegUnits.init(*TRI);
12706c3fb27SDimitry Andric   UsedRegUnits.init(*TRI);
12806c3fb27SDimitry Andric   bool Modified = false;
12906c3fb27SDimitry Andric   for (auto &MBB : Fn) {
13006c3fb27SDimitry Andric     MachineBasicBlock::iterator MBBI = containsPop(MBB);
13106c3fb27SDimitry Andric     MachineBasicBlock::iterator NextI = next_nodbg(MBBI, MBB.end());
1328a4dda33SDimitry Andric     if (MBBI != MBB.end() && NextI != MBB.end() &&
1338a4dda33SDimitry Andric         NextI->getOpcode() == RISCV::PseudoRET)
13406c3fb27SDimitry Andric       Modified |= usePopRet(MBBI, NextI, adjustRetVal(MBBI));
13506c3fb27SDimitry Andric   }
13606c3fb27SDimitry Andric   return Modified;
13706c3fb27SDimitry Andric }
13806c3fb27SDimitry Andric 
13906c3fb27SDimitry Andric /// createRISCVPushPopOptimizationPass - returns an instance of the
14006c3fb27SDimitry Andric /// Push/Pop optimization pass.
14106c3fb27SDimitry Andric FunctionPass *llvm::createRISCVPushPopOptimizationPass() {
14206c3fb27SDimitry Andric   return new RISCVPushPopOpt();
14306c3fb27SDimitry Andric }
144