LLVM 24.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"
17#include "RISCVSubtarget.h"
22#include "llvm/IR/Analysis.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "riscv-lpad-setup"
27#define PASS_NAME "RISC-V Landing Pad Setup"
28
29namespace {
30
31class RISCVLandingPadSetupLegacy : public MachineFunctionPass {
32public:
33 static char ID;
34
35 RISCVLandingPadSetupLegacy() : MachineFunctionPass(ID) {}
36
37 bool runOnMachineFunction(MachineFunction &F) override;
38
39 StringRef getPassName() const override { return PASS_NAME; }
40
41 void getAnalysisUsage(AnalysisUsage &AU) const override {
42 AU.setPreservesCFG();
44 }
45};
46
47} // end anonymous namespace
48
50 const auto &STI = MF.getSubtarget<RISCVSubtarget>();
51 const RISCVInstrInfo &TII = *STI.getInstrInfo();
52
54 return false;
55
56 uint32_t Label = 0;
57 if (std::optional<uint32_t> Preferred = STI.getCLOpts().landing_pad_label) {
58 if (!isUInt<20>(*Preferred))
59 report_fatal_error("riscv-landing-pad-label=<val>, <val> needs to fit in "
60 "unsigned 20-bits");
61 Label = *Preferred;
62 }
63
64 // Zicfilp does not check X7 if landing pad label is zero.
65 if (Label == 0)
66 return false;
67
68 bool Changed = false;
69 for (MachineBasicBlock &MBB : MF)
71 if (MI.getOpcode() != RISCV::PseudoBRIND &&
72 MI.getOpcode() != RISCV::PseudoCALLIndirect &&
73 MI.getOpcode() != RISCV::PseudoTAILIndirect)
74 continue;
75 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(RISCV::LUI), RISCV::X7)
76 .addImm(Label);
78 Changed = true;
79 }
80
81 return Changed;
82}
83
84INITIALIZE_PASS(RISCVLandingPadSetupLegacy, DEBUG_TYPE, PASS_NAME, false, false)
85
86char RISCVLandingPadSetupLegacy::ID = 0;
87
89 return new RISCVLandingPadSetupLegacy();
90}
91
92bool RISCVLandingPadSetupLegacy::runOnMachineFunction(MachineFunction &MF) {
93 return runLandingPadSetup(MF);
94}
95
96PreservedAnalyses
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool runLandingPadSetup(MachineFunction &MF)
#define PASS_NAME
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
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.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
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)
RISCVMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private RISCV-...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createRISCVLandingPadSetupLegacyPass()
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:649
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190