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/MachineFunctionAnalysisManager.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
25#include "llvm/CodeGen/MachinePassManager.h"
26#include "llvm/IR/Analysis.h"
27#include "llvm/Pass.h"
28
29#define DEBUG_TYPE "riscv-indirect-branch-tracking"
30#define PASS_NAME "RISC-V Indirect Branch Tracking"
31
32using namespace llvm;
33
34namespace {
35class RISCVIndirectBranchTrackingLegacy : public MachineFunctionPass {
36public:
37 static char ID;
38 RISCVIndirectBranchTrackingLegacy() : MachineFunctionPass(ID) {}
39
40 StringRef getPassName() const override { return PASS_NAME; }
41
42 void getAnalysisUsage(AnalysisUsage &AU) const override {
43 AU.setPreservesCFG();
44 MachineFunctionPass::getAnalysisUsage(AU);
45 }
46
47 bool runOnMachineFunction(MachineFunction &MF) override;
48};
49
50} // end anonymous namespace
51
52INITIALIZE_PASS(RISCVIndirectBranchTrackingLegacy, DEBUG_TYPE, PASS_NAME, false,
53 false)
54
55char RISCVIndirectBranchTrackingLegacy::ID = 0;
56
57FunctionPass *llvm::createRISCVIndirectBranchTrackingLegacyPass() {
58 return new RISCVIndirectBranchTrackingLegacy();
59}
60
61static void
62emitLpad(MachineBasicBlock &MBB, const RISCVInstrInfo *TII, uint32_t Label,
63 MachineBasicBlock::iterator I = MachineBasicBlock::iterator{}) {
64 if (!I.isValid())
65 I = MBB.begin();
66 BuildMI(BB&: MBB, I, MIMD: MBB.findDebugLoc(MBBI: I), MCID: TII->get(Opcode: RISCV::AUIPC), DestReg: RISCV::X0)
67 .addImm(Val: Label);
68}
69
70static bool runIndirectBranchTracking(MachineFunction &MF) {
71 const auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
72 const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
73 const Align LpadAlign = Align(4);
74
75 if (!MF.getInfo<RISCVMachineFunctionInfo>()->hasCFProtectionBranch())
76 return false;
77
78 uint32_t FixedLabel = 0;
79 if (std::optional<uint32_t> Label = Subtarget.getCLOpts().landing_pad_label) {
80 if (!isUInt<20>(x: *Label))
81 report_fatal_error(reason: "riscv-landing-pad-label=<val>, <val> needs to fit in "
82 "unsigned 20-bits");
83 FixedLabel = *Label;
84 }
85
86 bool Changed = false;
87 for (MachineBasicBlock &MBB : MF) {
88 if (&MBB == &MF.front()) {
89 Function &F = MF.getFunction();
90 // When trap is taken, landing pad is not needed.
91 if (F.hasFnAttribute(Kind: "interrupt"))
92 continue;
93
94 if (F.hasAddressTaken() || !F.hasLocalLinkage()) {
95 emitLpad(MBB, TII, Label: FixedLabel);
96 if (MF.getAlignment() < LpadAlign)
97 MF.setAlignment(LpadAlign);
98 Changed = true;
99 }
100 continue;
101 }
102
103 if (MBB.hasAddressTaken()) {
104 emitLpad(MBB, TII, Label: FixedLabel);
105 if (MBB.getAlignment() < LpadAlign)
106 MBB.setAlignment(LpadAlign);
107 Changed = true;
108 }
109 }
110
111 return Changed;
112}
113
114bool RISCVIndirectBranchTrackingLegacy::runOnMachineFunction(
115 MachineFunction &MF) {
116 return runIndirectBranchTracking(MF);
117}
118
119PreservedAnalyses
120RISCVIndirectBranchTrackingPass::run(MachineFunction &MF,
121 MachineFunctionAnalysisManager &MFAM) {
122 return runIndirectBranchTracking(MF)
123 ? getMachineFunctionPassPreservedAnalyses()
124 .preserveSet<CFGAnalyses>()
125 : PreservedAnalyses::all();
126}
127