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/TargetInstrInfo.h"
19#include "llvm/CodeGen/TargetSubtargetInfo.h"
20#include "llvm/IR/Function.h"
21#include "llvm/InitializePasses.h"
22
23using namespace llvm;
24
25namespace {
26struct FEntryInserter {
27 bool run(MachineFunction &MF);
28};
29
30struct FEntryInserterLegacy : public MachineFunctionPass {
31 static char ID; // Pass identification, replacement for typeid
32 FEntryInserterLegacy() : MachineFunctionPass(ID) {
33 initializeFEntryInserterLegacyPass(*PassRegistry::getPassRegistry());
34 }
35
36 bool runOnMachineFunction(MachineFunction &F) override {
37 return FEntryInserter().run(MF&: F);
38 }
39};
40}
41
42PreservedAnalyses FEntryInserterPass::run(MachineFunction &MF,
43 MachineFunctionAnalysisManager &AM) {
44 if (!FEntryInserter().run(MF))
45 return PreservedAnalyses::all();
46 return getMachineFunctionPassPreservedAnalyses();
47}
48
49bool FEntryInserter::run(MachineFunction &MF) {
50 const std::string FEntryName = std::string(
51 MF.getFunction().getFnAttribute(Kind: "fentry-call").getValueAsString());
52 if (FEntryName != "true")
53 return false;
54
55 auto &FirstMBB = *MF.begin();
56 auto *TII = MF.getSubtarget().getInstrInfo();
57 BuildMI(BB&: FirstMBB, I: FirstMBB.begin(), MIMD: DebugLoc(),
58 MCID: TII->get(Opcode: TargetOpcode::FENTRY_CALL));
59 return true;
60}
61
62char FEntryInserterLegacy::ID = 0;
63char &llvm::FEntryInserterID = FEntryInserterLegacy::ID;
64INITIALIZE_PASS(FEntryInserterLegacy, "fentry-insert", "Insert fentry calls",
65 false, false)
66