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
34 bool runOnMachineFunction(MachineFunction &F) override {
35 return FEntryInserter().run(MF&: F);
36 }
37};
38}
39
40PreservedAnalyses FEntryInserterPass::run(MachineFunction &MF,
41 MachineFunctionAnalysisManager &AM) {
42 if (!FEntryInserter().run(MF))
43 return PreservedAnalyses::all();
44 return getMachineFunctionPassPreservedAnalyses();
45}
46
47bool FEntryInserter::run(MachineFunction &MF) {
48 const std::string FEntryName = std::string(
49 MF.getFunction().getFnAttribute(Kind: "fentry-call").getValueAsString());
50 if (FEntryName != "true")
51 return false;
52
53 auto &FirstMBB = *MF.begin();
54 auto *TII = MF.getSubtarget().getInstrInfo();
55 BuildMI(BB&: FirstMBB, I: FirstMBB.begin(), MIMD: DebugLoc(),
56 MCID: TII->get(Opcode: TargetOpcode::FENTRY_CALL));
57 return true;
58}
59
60char FEntryInserterLegacy::ID = 0;
61char &llvm::FEntryInserterID = FEntryInserterLegacy::ID;
62INITIALIZE_PASS(FEntryInserterLegacy, "fentry-insert", "Insert fentry calls",
63 false, false)
64