LLVM 20.0.0git
RISCVIndirectBranchTracking.cpp
Go to the documentation of this file.
1//===------ RISCVIndirectBranchTracking.cpp - Enables lpad mechanism ------===//
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// The pass adds LPAD (AUIPC with rs1 = X0) machine instructions at the
10// beginning of each basic block or function that is referenced by an indrect
11// jump/call instruction.
12//
13//===----------------------------------------------------------------------===//
14
15#include "RISCV.h"
16#include "RISCVInstrInfo.h"
17#include "RISCVSubtarget.h"
18#include "llvm/ADT/Statistic.h"
22
23using namespace llvm;
24
26 "riscv-landing-pad-label", cl::ReallyHidden,
27 cl::desc("Use preferred fixed label for all labels"));
28
29namespace {
30class RISCVIndirectBranchTrackingPass : public MachineFunctionPass {
31public:
32 RISCVIndirectBranchTrackingPass() : MachineFunctionPass(ID) {}
33
34 StringRef getPassName() const override {
35 return "RISC-V Indirect Branch Tracking";
36 }
37
38 bool runOnMachineFunction(MachineFunction &MF) override;
39
40private:
41 static char ID;
42 const Align LpadAlign = Align(4);
43};
44
45} // end anonymous namespace
46
47char RISCVIndirectBranchTrackingPass::ID = 0;
48
50 return new RISCVIndirectBranchTrackingPass();
51}
52
54 uint32_t Label) {
55 auto I = MBB.begin();
56 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(RISCV::AUIPC), RISCV::X0)
57 .addImm(Label);
58}
59
60bool RISCVIndirectBranchTrackingPass::runOnMachineFunction(
61 MachineFunction &MF) {
62 const auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
63 const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
64 if (!Subtarget.hasStdExtZicfilp())
65 return false;
66
67 uint32_t FixedLabel = 0;
68 if (PreferredLandingPadLabel.getNumOccurrences() > 0) {
69 if (!isUInt<20>(PreferredLandingPadLabel))
70 report_fatal_error("riscv-landing-pad-label=<val>, <val> needs to fit in "
71 "unsigned 20-bits");
72 FixedLabel = PreferredLandingPadLabel;
73 }
74
75 bool Changed = false;
76 for (MachineBasicBlock &MBB : MF) {
77 if (&MBB == &MF.front()) {
78 Function &F = MF.getFunction();
79 // When trap is taken, landing pad is not needed.
80 if (F.hasFnAttribute("interrupt"))
81 continue;
82
83 if (F.hasAddressTaken() || !F.hasLocalLinkage()) {
84 emitLpad(MBB, TII, FixedLabel);
85 if (MF.getAlignment() < LpadAlign)
86 MF.setAlignment(LpadAlign);
87 Changed = true;
88 }
89 continue;
90 }
91
92 if (MBB.hasAddressTaken()) {
93 emitLpad(MBB, TII, FixedLabel);
94 if (MBB.getAlignment() < LpadAlign)
95 MBB.setAlignment(LpadAlign);
96 Changed = true;
97 }
98 }
99
100 return Changed;
101}
MachineBasicBlock & MBB
const HexagonInstrInfo * TII
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static void emitLpad(MachineBasicBlock &MBB, const RISCVInstrInfo *TII, uint32_t Label)
cl::opt< uint32_t > PreferredLandingPadLabel("riscv-landing-pad-label", cl::ReallyHidden, cl::desc("Use preferred fixed label for all labels"))
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
bool hasAddressTaken() const
Test whether this block is used as something other than the target of a terminator,...
void setAlignment(Align A)
Set alignment of the basic block.
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
Align getAlignment() const
Return alignment of the basic block.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
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.
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
@ ReallyHidden
Definition: CommandLine.h:138
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
FunctionPass * createRISCVIndirectBranchTrackingPass()
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39