LLVM 22.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 rd = X0) machine instructions at the
10// beginning of each basic block or function that is referenced by an indirect
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
23#define DEBUG_TYPE "riscv-indrect-branch-tracking"
24#define PASS_NAME "RISC-V Indirect Branch Tracking"
25
26using namespace llvm;
27
29 "riscv-landing-pad-label", cl::ReallyHidden,
30 cl::desc("Use preferred fixed label for all labels"));
31
32namespace {
33class RISCVIndirectBranchTracking : public MachineFunctionPass {
34public:
35 static char ID;
36 RISCVIndirectBranchTracking() : MachineFunctionPass(ID) {}
37
38 StringRef getPassName() const override { return PASS_NAME; }
39
40 bool runOnMachineFunction(MachineFunction &MF) override;
41
42private:
43 const Align LpadAlign = Align(4);
44};
45
46} // end anonymous namespace
47
48INITIALIZE_PASS(RISCVIndirectBranchTracking, DEBUG_TYPE, PASS_NAME, false,
49 false)
50
51char RISCVIndirectBranchTracking::ID = 0;
52
54 return new RISCVIndirectBranchTracking();
55}
56
58 uint32_t Label) {
59 auto I = MBB.begin();
60 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(RISCV::AUIPC), RISCV::X0)
61 .addImm(Label);
62}
63
64bool RISCVIndirectBranchTracking::runOnMachineFunction(MachineFunction &MF) {
65 const auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
66 const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
67 if (!Subtarget.hasStdExtZicfilp())
68 return false;
69
70 uint32_t FixedLabel = 0;
71 if (PreferredLandingPadLabel.getNumOccurrences() > 0) {
73 report_fatal_error("riscv-landing-pad-label=<val>, <val> needs to fit in "
74 "unsigned 20-bits");
75 FixedLabel = PreferredLandingPadLabel;
76 }
77
78 bool Changed = false;
79 for (MachineBasicBlock &MBB : MF) {
80 if (&MBB == &MF.front()) {
81 Function &F = MF.getFunction();
82 // When trap is taken, landing pad is not needed.
83 if (F.hasFnAttribute("interrupt"))
84 continue;
85
86 if (F.hasAddressTaken() || !F.hasLocalLinkage()) {
87 emitLpad(MBB, TII, FixedLabel);
88 if (MF.getAlignment() < LpadAlign)
89 MF.setAlignment(LpadAlign);
90 Changed = true;
91 }
92 continue;
93 }
94
95 if (MBB.hasAddressTaken()) {
96 emitLpad(MBB, TII, FixedLabel);
97 if (MBB.getAlignment() < LpadAlign)
98 MBB.setAlignment(LpadAlign);
99 Changed = true;
100 }
101 }
102
103 return Changed;
104}
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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...
#define PASS_NAME
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
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.
Align getAlignment() const
Return alignment of the basic block.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
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.
Changed
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
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.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:198
FunctionPass * createRISCVIndirectBranchTrackingPass()