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/MachineFunction.h"
14 #include "llvm/CodeGen/MachineFunctionPass.h"
15 #include "llvm/CodeGen/MachineInstrBuilder.h"
16 #include "llvm/CodeGen/TargetInstrInfo.h"
17 #include "llvm/CodeGen/TargetSubtargetInfo.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/InitializePasses.h"
20 
21 using namespace llvm;
22 
23 namespace {
24 struct FEntryInserter : public MachineFunctionPass {
25   static char ID; // Pass identification, replacement for typeid
26   FEntryInserter() : MachineFunctionPass(ID) {
27     initializeFEntryInserterPass(*PassRegistry::getPassRegistry());
28   }
29 
30   bool runOnMachineFunction(MachineFunction &F) override;
31 };
32 }
33 
34 bool FEntryInserter::runOnMachineFunction(MachineFunction &MF) {
35   const std::string FEntryName = std::string(
36       MF.getFunction().getFnAttribute("fentry-call").getValueAsString());
37   if (FEntryName != "true")
38     return false;
39 
40   auto &FirstMBB = *MF.begin();
41   auto *TII = MF.getSubtarget().getInstrInfo();
42   BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
43           TII->get(TargetOpcode::FENTRY_CALL));
44   return true;
45 }
46 
47 char FEntryInserter::ID = 0;
48 char &llvm::FEntryInserterID = FEntryInserter::ID;
49 INITIALIZE_PASS(FEntryInserter, "fentry-insert", "Insert fentry calls", false,
50                 false)
51