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"
19
20using namespace llvm;
21
22#define DEBUG_TYPE "riscv-lpad-setup"
23#define PASS_NAME "RISC-V Landing Pad Setup"
24
26
27namespace {
28
29class RISCVLandingPadSetup : public MachineFunctionPass {
30public:
31 static char ID;
32
33 RISCVLandingPadSetup() : MachineFunctionPass(ID) {}
34
36
37 StringRef getPassName() const override { return PASS_NAME; }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 AU.setPreservesCFG();
42 }
43};
44
45} // end anonymous namespace
46
47bool RISCVLandingPadSetup::runOnMachineFunction(MachineFunction &MF) {
48 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
49 const RISCVInstrInfo &TII = *STI.getInstrInfo();
50
51 if (!STI.hasStdExtZicfilp())
52 return false;
53
54 uint32_t Label = 0;
55 if (PreferredLandingPadLabel.getNumOccurrences() > 0) {
56 if (!isUInt<20>(PreferredLandingPadLabel))
57 report_fatal_error("riscv-landing-pad-label=<val>, <val> needs to fit in "
58 "unsigned 20-bits");
60 }
61
62 // Zicfilp does not check X7 if landing pad label is zero.
63 if (Label == 0)
64 return false;
65
66 bool Changed = false;
67 for (MachineBasicBlock &MBB : MF)
69 if (MI.getOpcode() != RISCV::PseudoBRINDNonX7 &&
70 MI.getOpcode() != RISCV::PseudoCALLIndirectNonX7 &&
71 MI.getOpcode() != RISCV::PseudoTAILIndirectNonX7)
72 continue;
73 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(RISCV::LUI), RISCV::X7)
74 .addImm(Label);
76 Changed = true;
77 }
78
79 return Changed;
80}
81
82INITIALIZE_PASS(RISCVLandingPadSetup, DEBUG_TYPE, PASS_NAME, false, false)
83
84char RISCVLandingPadSetup::ID = 0;
85
87 return new RISCVLandingPadSetup();
88}
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:51
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:657
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167