LLVM 24.0.0git
RISCVPushPopOptimizer.cpp
Go to the documentation of this file.
1//===------- RISCVPushPopOptimizer.cpp - RISC-V Push/Pop opt. pass --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains a pass that replaces Zcmp POP instructions with
10// POPRET[Z] where possible.
11//
12//===----------------------------------------------------------------------===//
13
14#include "RISCV.h"
15#include "RISCVInstrInfo.h"
21#include "llvm/IR/Analysis.h"
22#include "llvm/Pass.h"
23
24using namespace llvm;
25
26#define RISCV_PUSH_POP_OPT_NAME "RISC-V Zcmp Push/Pop optimization pass"
27
28namespace {
29class RISCVPushPopOptLegacy : public MachineFunctionPass {
30public:
31 static char ID;
32
33 RISCVPushPopOptLegacy() : MachineFunctionPass(ID) {}
34 bool runOnMachineFunction(MachineFunction &MF) override;
35
36 StringRef getPassName() const override { return RISCV_PUSH_POP_OPT_NAME; }
37
38 void getAnalysisUsage(AnalysisUsage &AU) const override {
39 AU.setPreservesCFG();
41 }
42};
43
44class RISCVPushPopOptImpl {
45 const RISCVInstrInfo *TII;
47
48 // Track which register units have been modified and used.
49 LiveRegUnits ModifiedRegUnits, UsedRegUnits;
50
51 bool usePopRet(MachineBasicBlock::iterator &MBBI,
52 MachineBasicBlock::iterator &NextI, bool IsReturnZero);
53 bool adjustRetVal(MachineBasicBlock::iterator &MBBI);
54
55public:
56 bool runOnMachineFunction(MachineFunction &MF);
57};
58
59char RISCVPushPopOptLegacy::ID = 0;
60
61} // end of anonymous namespace
62
63INITIALIZE_PASS(RISCVPushPopOptLegacy, "riscv-push-pop-opt",
64 RISCV_PUSH_POP_OPT_NAME, false, false)
65
66static bool isPop(unsigned Opcode) {
67 switch (Opcode) {
68 case RISCV::CM_POP:
69 case RISCV::QC_CM_POP:
70 return true;
71 default:
72 return false;
73 }
74}
75
76static unsigned getPopRetOpcode(unsigned PopOpcode, bool IsReturnZero) {
77 assert(isPop(PopOpcode) && "Unexpected Pop Opcode");
78
79 switch (PopOpcode) {
80 case RISCV::CM_POP:
81 return IsReturnZero ? RISCV::CM_POPRETZ : RISCV::CM_POPRET;
82 case RISCV::QC_CM_POP:
83 return IsReturnZero ? RISCV::QC_CM_POPRETZ : RISCV::QC_CM_POPRET;
84 default:
85 llvm_unreachable("Unhandled Pop Opcode");
86 }
87}
88
89bool RISCVPushPopOptImpl::usePopRet(MachineBasicBlock::iterator &MBBI,
91 bool IsReturnZero) {
92 // Since Pseudo instruction lowering happen later in the pipeline,
93 // this will detect all ret instruction.
94 DebugLoc DL = NextI->getDebugLoc();
95 unsigned Opc = getPopRetOpcode(MBBI->getOpcode(), IsReturnZero);
96 MachineInstrBuilder PopRetBuilder =
97 BuildMI(*NextI->getParent(), NextI, DL, TII->get(Opc))
98 .add(MBBI->getOperand(0))
99 .add(MBBI->getOperand(1))
101
102 // Copy over the variable implicit uses and defs from the CM_POP. They depend
103 // on what register list has been picked during frame lowering.
104 const MCInstrDesc &PopDesc = MBBI->getDesc();
105 unsigned FirstNonDeclaredOp = PopDesc.getNumOperands() +
106 PopDesc.NumImplicitUses +
107 PopDesc.NumImplicitDefs;
108 for (unsigned i = FirstNonDeclaredOp; i < MBBI->getNumOperands(); ++i)
109 PopRetBuilder.add(MBBI->getOperand(i));
110
111 for (auto I = std::next(MBBI); I != NextI;) {
112 if (I->isCFIInstruction())
113 I = I->eraseFromParent();
114 else
115 ++I;
116 }
117
119 NextI->eraseFromParent();
120 return true;
121}
122
123// Search for last assignment to a0 and if possible use ret_val slot of POP to
124// store return value.
125bool RISCVPushPopOptImpl::adjustRetVal(MachineBasicBlock::iterator &MBBI) {
127 // Track which register units have been modified and used between the POP
128 // insn and the last assignment to register a0.
129 ModifiedRegUnits.clear();
130 UsedRegUnits.clear();
131 // Since POP instruction is in Epilogue no normal instructions will follow
132 // after it. Therefore search only previous ones to find the return value.
134 next_nodbg(MBBI.getReverse(), RE);
135 I != RE; I = next_nodbg(I, RE)) {
136 MachineInstr &MI = *I;
137 if (auto OperandPair = TII->isCopyInstrImpl(MI)) {
138 Register DestReg = OperandPair->Destination->getReg();
139 Register Source = OperandPair->Source->getReg();
140 if (DestReg == RISCV::X10 && Source == RISCV::X0) {
141 MI.removeFromParent();
142 return true;
143 }
144 }
145 // Update modified / used register units.
146 LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits, TRI);
147 // If a0 was modified or used, there is no possibility
148 // of using ret_val slot of POP instruction.
149 if (!ModifiedRegUnits.available(RISCV::X10) ||
150 !UsedRegUnits.available(RISCV::X10))
151 return false;
152 }
153 return false;
154}
155
156bool RISCVPushPopOptImpl::runOnMachineFunction(MachineFunction &MF) {
157 // If Zcmp extension is not supported, abort.
158 const RISCVSubtarget *Subtarget = &MF.getSubtarget<RISCVSubtarget>();
159 if (!Subtarget->hasStdExtZcmp() && !Subtarget->hasVendorXqccmp())
160 return false;
161
162 // We don't want any popret[z] instructions when emitting code with shadow
163 // stack protection. Note that this pass would actually fail to insert any
164 // popret[z] instructions in this case since the cm.pop and ret will not be
165 // adjacent. But there's no point in running a pass that won't do anything.
166 if ((MF.getFunction().hasFnAttribute("hw-shadow-stack") &&
167 Subtarget->hasStdExtZimop()) ||
168 MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
169 return false;
170
173
174 // Resize the modified and used register unit trackers. We do this once
175 // per function and then clear the register units each time we determine
176 // correct return value for the POP.
177 ModifiedRegUnits.init(*TRI);
178 UsedRegUnits.init(*TRI);
179
180 bool Modified = false;
181 for (auto &MBB : MF) {
182 // RET should be the only terminator.
183 auto RetMBBI = MBB.getFirstTerminator();
184 if (RetMBBI == MBB.end() || RetMBBI->getOpcode() != RISCV::PseudoRET ||
185 RetMBBI == MBB.begin())
186 continue;
187
188 // Skip CFI and debug instructions between POP and RET to find the POP.
189 auto PopMBBI = RetMBBI;
190 while (PopMBBI != MBB.begin()) {
191 --PopMBBI;
192
193 if (PopMBBI->isCFIInstruction() || PopMBBI->isDebugInstr() ||
194 PopMBBI->isPseudoProbe())
195 continue;
196
197 if (isPop(PopMBBI->getOpcode()) &&
198 PopMBBI->getFlag(MachineInstr::FrameDestroy))
199 Modified |= usePopRet(PopMBBI, RetMBBI, adjustRetVal(PopMBBI));
200
201 break;
202 }
203 }
204
205 return Modified;
206}
207
208/// createRISCVPushPopOptimizationPass - returns an instance of the
209/// Push/Pop optimization pass.
211 return new RISCVPushPopOptLegacy();
212}
213
214bool RISCVPushPopOptLegacy::runOnMachineFunction(MachineFunction &MF) {
215 if (skipFunction(MF.getFunction()))
216 return false;
217 RISCVPushPopOptImpl Impl;
218 return Impl.runOnMachineFunction(MF);
219}
220
221PreservedAnalyses
224 RISCVPushPopOptImpl Impl;
225 return Impl.runOnMachineFunction(MF)
229}
const AArch64Subtarget & Subtarget
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
IRTranslator LLVM IR MI
unsigned Opc
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool isPop(unsigned Opcode)
#define RISCV_PUSH_POP_OPT_NAME
static unsigned getPopRetOpcode(unsigned PopOpcode, bool IsReturnZero)
std::optional< DestSourcePair > isCopyInstrImpl(const MachineInstr &MI) const override
If the specific machine instruction is an instruction that moves/copies value from one register to an...
const AArch64RegisterInfo * getRegisterInfo() const override
const AArch64InstrInfo * getInstrInfo() const override
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
A set of register units used to track register liveness.
static void accumulateUsedDefed(const MachineInstr &MI, LiveRegUnits &ModifiedRegUnits, LiveRegUnits &UsedRegUnits, const TargetRegisterInfo *TRI)
For a machine instruction MI, adds all register units used in UsedRegUnits and defined or clobbered i...
bool available(MCRegister Reg) const
Returns true if no part of physical register Reg is live.
void init(const TargetRegisterInfo &TRI)
Initialize and clear the set.
void clear()
Clears the set.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
uint8_t NumImplicitUses
uint8_t NumImplicitDefs
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
reverse_iterator rend()
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & add(const MachineOperand &MO) const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It, then continue incrementing it while it points to a debug instruction.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createRISCVPushPopOptLegacyPass()
createRISCVPushPopOptimizationPass - returns an instance of the Push/Pop optimization pass.