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 "RISCVMachineFunctionInfo.h"
18#include "RISCVSubtarget.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24
25#define DEBUG_TYPE "riscv-indirect-branch-tracking"
26#define PASS_NAME "RISC-V Indirect Branch Tracking"
27
28using namespace llvm;
29
30cl::opt<uint32_t> PreferredLandingPadLabel(
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
55FunctionPass *llvm::createRISCVIndirectBranchTrackingPass() {
56 return new RISCVIndirectBranchTracking();
57}
58
59static void
60emitLpad(MachineBasicBlock &MBB, const RISCVInstrInfo *TII, uint32_t Label,
61 MachineBasicBlock::iterator I = MachineBasicBlock::iterator{}) {
62 if (!I.isValid())
63 I = MBB.begin();
64 BuildMI(BB&: MBB, I, MIMD: MBB.findDebugLoc(MBBI: I), MCID: TII->get(Opcode: RISCV::AUIPC), DestReg: RISCV::X0)
65 .addImm(Val: Label);
66}
67
68bool RISCVIndirectBranchTracking::runOnMachineFunction(MachineFunction &MF) {
69 const auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
70 const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
71
72 if (!MF.getInfo<RISCVMachineFunctionInfo>()->hasCFProtectionBranch())
73 return false;
74
75 uint32_t FixedLabel = 0;
76 if (PreferredLandingPadLabel.getNumOccurrences() > 0) {
77 if (!isUInt<20>(x: PreferredLandingPadLabel))
78 report_fatal_error(reason: "riscv-landing-pad-label=<val>, <val> needs to fit in "
79 "unsigned 20-bits");
80 FixedLabel = PreferredLandingPadLabel;
81 }
82
83 bool Changed = false;
84 for (MachineBasicBlock &MBB : MF) {
85 if (&MBB == &MF.front()) {
86 Function &F = MF.getFunction();
87 // When trap is taken, landing pad is not needed.
88 if (F.hasFnAttribute(Kind: "interrupt"))
89 continue;
90
91 if (F.hasAddressTaken() || !F.hasLocalLinkage()) {
92 emitLpad(MBB, TII, Label: FixedLabel);
93 if (MF.getAlignment() < LpadAlign)
94 MF.setAlignment(LpadAlign);
95 Changed = true;
96 }
97 continue;
98 }
99
100 if (MBB.hasAddressTaken()) {
101 emitLpad(MBB, TII, Label: FixedLabel);
102 if (MBB.getAlignment() < LpadAlign)
103 MBB.setAlignment(LpadAlign);
104 Changed = true;
105 }
106 }
107
108 return Changed;
109}
110