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 "RISCVTargetMachine.h"
19#include "llvm/ADT/Statistic.h"
23
24using namespace llvm;
25
27 "riscv-landing-pad-label", cl::ReallyHidden,
28 cl::desc("Use preferred fixed label for all labels"));
29
30namespace {
31class RISCVIndirectBranchTrackingPass : public MachineFunctionPass {
32public:
33 RISCVIndirectBranchTrackingPass() : MachineFunctionPass(ID) {}
34
35 StringRef getPassName() const override {
36 return "RISC-V Indirect Branch Tracking";
37 }
38
39 bool runOnMachineFunction(MachineFunction &MF) override;
40
41private:
42 static char ID;
43 const Align LpadAlign = Align(4);
44};
45
46} // end anonymous namespace
47
48char RISCVIndirectBranchTrackingPass::ID = 0;
49
51 return new RISCVIndirectBranchTrackingPass();
52}
53
55 uint32_t Label) {
56 auto I = MBB.begin();
57 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(RISCV::AUIPC), RISCV::X0)
58 .addImm(Label);
59}
60
61bool RISCVIndirectBranchTrackingPass::runOnMachineFunction(
62 MachineFunction &MF) {
63 const auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
64 const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
65 if (!Subtarget.hasStdExtZicfilp())
66 return false;
67
68 uint32_t FixedLabel = 0;
69 if (PreferredLandingPadLabel.getNumOccurrences() > 0) {
70 if (!isUInt<20>(PreferredLandingPadLabel))
71 report_fatal_error("riscv-landing-pad-label=<val>, <val> needs to fit in "
72 "unsigned 20-bits");
73 FixedLabel = PreferredLandingPadLabel;
74 }
75
76 bool Changed = false;
77 for (MachineBasicBlock &MBB : MF) {
78 if (&MBB == &MF.front()) {
79 Function &F = MF.getFunction();
80 // When trap is taken, landing pad is not needed.
81 if (F.hasFnAttribute("interrupt"))
82 continue;
83
84 if (F.hasAddressTaken() || !F.hasLocalLinkage()) {
85 emitLpad(MBB, TII, FixedLabel);
86 if (MF.getAlignment() < LpadAlign)
87 MF.setAlignment(LpadAlign);
88 Changed = true;
89 }
90 continue;
91 }
92
93 if (MBB.hasAddressTaken()) {
94 emitLpad(MBB, TII, FixedLabel);
95 if (MBB.getAlignment() < LpadAlign)
96 MBB.setAlignment(LpadAlign);
97 Changed = true;
98 }
99 }
100
101 return Changed;
102}
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:50
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