LLVM 23.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"
18#include "RISCVSubtarget.h"
19#include "llvm/ADT/Statistic.h"
24
25#define DEBUG_TYPE "riscv-indirect-branch-tracking"
26#define PASS_NAME "RISC-V Indirect Branch Tracking"
27
28using namespace llvm;
29
31 "riscv-landing-pad-label", cl::ReallyHidden,
32 cl::desc("Use preferred fixed label for all labels"));
33
34namespace {
35class RISCVIndirectBranchTracking : public MachineFunctionPass {
36public:
37 static char ID;
38 RISCVIndirectBranchTracking() : MachineFunctionPass(ID) {}
39
40 StringRef getPassName() const override { return PASS_NAME; }
41
42 bool runOnMachineFunction(MachineFunction &MF) override;
43
44private:
45 const Align LpadAlign = Align(4);
46};
47
48} // end anonymous namespace
49
50INITIALIZE_PASS(RISCVIndirectBranchTracking, DEBUG_TYPE, PASS_NAME, false,
51 false)
52
53char RISCVIndirectBranchTracking::ID = 0;
54
56 return new RISCVIndirectBranchTracking();
57}
58
59static void
62 if (!I.isValid())
63 I = MBB.begin();
64 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(RISCV::AUIPC), RISCV::X0)
65 .addImm(Label);
66}
67
68static bool isCallReturnTwice(const MachineOperand &MOp) {
69 if (!MOp.isGlobal())
70 return false;
71 auto *CalleeFn = dyn_cast<Function>(MOp.getGlobal());
72 if (!CalleeFn)
73 return false;
74 AttributeList Attrs = CalleeFn->getAttributes();
75 return Attrs.hasFnAttr(Attribute::ReturnsTwice);
76}
77
78bool RISCVIndirectBranchTracking::runOnMachineFunction(MachineFunction &MF) {
79 const auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
80 const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
81
82 if (!MF.getInfo<RISCVMachineFunctionInfo>()->hasCFProtectionBranch())
83 return false;
84
85 uint32_t FixedLabel = 0;
86 if (PreferredLandingPadLabel.getNumOccurrences() > 0) {
88 report_fatal_error("riscv-landing-pad-label=<val>, <val> needs to fit in "
89 "unsigned 20-bits");
90 FixedLabel = PreferredLandingPadLabel;
91 }
92
93 bool Changed = false;
94 for (MachineBasicBlock &MBB : MF) {
95 if (&MBB == &MF.front()) {
96 Function &F = MF.getFunction();
97 // When trap is taken, landing pad is not needed.
98 if (F.hasFnAttribute("interrupt"))
99 continue;
100
101 if (F.hasAddressTaken() || !F.hasLocalLinkage()) {
102 emitLpad(MBB, TII, FixedLabel);
103 if (MF.getAlignment() < LpadAlign)
104 MF.setAlignment(LpadAlign);
105 Changed = true;
106 }
107 continue;
108 }
109
110 if (MBB.hasAddressTaken()) {
111 emitLpad(MBB, TII, FixedLabel);
112 if (MBB.getAlignment() < LpadAlign)
113 MBB.setAlignment(LpadAlign);
114 Changed = true;
115 }
116 }
117
118 // Check for calls to functions with ReturnsTwice attribute and insert
119 // LPAD after such calls
120 for (MachineBasicBlock &MBB : MF) {
121 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
122 if (I->isCall() && I->getNumOperands() > 0 &&
123 isCallReturnTwice(I->getOperand(0))) {
124 auto NextI = std::next(I);
125 emitLpad(MBB, TII, FixedLabel, NextI);
126 Changed = true;
127 }
128 }
129 }
130
131 return Changed;
132}
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool isCallReturnTwice(const MachineOperand &MOp)
cl::opt< uint32_t > PreferredLandingPadLabel("riscv-landing-pad-label", cl::ReallyHidden, cl::desc("Use preferred fixed label for all labels"))
static void emitLpad(MachineBasicBlock &MBB, const RISCVInstrInfo *TII, uint32_t Label, MachineBasicBlock::iterator I=MachineBasicBlock::iterator{})
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.
MachineInstrBundleIterator< MachineInstr > iterator
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.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress 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.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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:189
FunctionPass * createRISCVIndirectBranchTrackingPass()