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