LLVM 20.0.0git
RISCVLandingPadSetup.cpp
Go to the documentation of this file.
1//===------------ RISCVLandingPadSetup.cpp ---------------------------------==//
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 is a RISC-V pass to setup landing pad labels for indirect jumps.
10// Currently this pass only supports fixed labels.
11//
12//===----------------------------------------------------------------------===//
13
14#include "RISCV.h"
15#include "RISCVInstrInfo.h"
16#include "RISCVSubtarget.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "riscv-lpad-setup"
24#define PASS_NAME "RISC-V Landing Pad Setup"
25
27
28namespace {
29
30class RISCVLandingPadSetup : public MachineFunctionPass {
31public:
32 static char ID;
33
34 RISCVLandingPadSetup() : MachineFunctionPass(ID) {}
35
37
38 StringRef getPassName() const override { return PASS_NAME; }
39
40 void getAnalysisUsage(AnalysisUsage &AU) const override {
41 AU.setPreservesCFG();
43 }
44};
45
46} // end anonymous namespace
47
48bool RISCVLandingPadSetup::runOnMachineFunction(MachineFunction &MF) {
49 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
50 const RISCVInstrInfo &TII = *STI.getInstrInfo();
51
52 if (!STI.hasStdExtZicfilp())
53 return false;
54
55 uint32_t Label = 0;
56 if (PreferredLandingPadLabel.getNumOccurrences() > 0) {
57 if (!isUInt<20>(PreferredLandingPadLabel))
58 report_fatal_error("riscv-landing-pad-label=<val>, <val> needs to fit in "
59 "unsigned 20-bits");
61 }
62
63 // Zicfilp does not check X7 if landing pad label is zero.
64 if (Label == 0)
65 return false;
66
67 bool Changed = false;
68 for (MachineBasicBlock &MBB : MF)
70 if (MI.getOpcode() != RISCV::PseudoBRINDNonX7 &&
71 MI.getOpcode() != RISCV::PseudoCALLIndirectNonX7 &&
72 MI.getOpcode() != RISCV::PseudoTAILIndirectNonX7)
73 continue;
74 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(RISCV::LUI), RISCV::X7)
75 .addImm(Label);
77 Changed = true;
78 }
79
80 return Changed;
81}
82
83INITIALIZE_PASS(RISCVLandingPadSetup, DEBUG_TYPE, PASS_NAME, false, false)
84
85char RISCVLandingPadSetup::ID = 0;
86
88 return new RISCVLandingPadSetup();
89}
MachineBasicBlock & MBB
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
cl::opt< uint32_t > PreferredLandingPadLabel
#define PASS_NAME
#define DEBUG_TYPE
#define PASS_NAME
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createRISCVLandingPadSetupPass()
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167