1//===-- FEntryInsertion.cpp - Patchable prologues for LLVM -------------===//
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// This file edits function bodies to insert fentry calls.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/FEntryInserter.h"
14#include "llvm/CodeGen/MachineFunction.h"
15#include "llvm/CodeGen/MachineFunctionPass.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
17#include "llvm/CodeGen/MachinePassManager.h"
18#include "llvm/CodeGen/RegisterClassInfo.h"
19#include "llvm/CodeGen/TargetInstrInfo.h"
20#include "llvm/CodeGen/TargetSubtargetInfo.h"
21#include "llvm/IR/Function.h"
22#include "llvm/InitializePasses.h"
23
24using namespace llvm;
25
26namespace {
27struct FEntryInserter {
28 bool run(MachineFunction &MF);
29};
30
31struct FEntryInserterLegacy : public MachineFunctionPass {
32 static char ID; // Pass identification, replacement for typeid
33 FEntryInserterLegacy() : MachineFunctionPass(ID) {}
34
35 bool runOnMachineFunction(MachineFunction &F) override {
36 return FEntryInserter().run(MF&: F);
37 }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
41 MachineFunctionPass::getAnalysisUsage(AU);
42 }
43};
44}
45
46PreservedAnalyses FEntryInserterPass::run(MachineFunction &MF,
47 MachineFunctionAnalysisManager &AM) {
48 if (!FEntryInserter().run(MF))
49 return PreservedAnalyses::all();
50 return getMachineFunctionPassPreservedAnalyses();
51}
52
53bool FEntryInserter::run(MachineFunction &MF) {
54 const std::string FEntryName = std::string(
55 MF.getFunction().getFnAttribute(Kind: "fentry-call").getValueAsString());
56 if (FEntryName != "true")
57 return false;
58
59 auto &FirstMBB = *MF.begin();
60 auto *TII = MF.getSubtarget().getInstrInfo();
61 BuildMI(BB&: FirstMBB, I: FirstMBB.begin(), MIMD: DebugLoc(),
62 MCID: TII->get(Opcode: TargetOpcode::FENTRY_CALL));
63 return true;
64}
65
66char FEntryInserterLegacy::ID = 0;
67char &llvm::FEntryInserterID = FEntryInserterLegacy::ID;
68INITIALIZE_PASS(FEntryInserterLegacy, "fentry-insert", "Insert fentry calls",
69 false, false)
70