17a7e6055SDimitry Andric //===-- FEntryInsertion.cpp - Patchable prologues for LLVM -------------===//
27a7e6055SDimitry Andric //
37a7e6055SDimitry Andric //                     The LLVM Compiler Infrastructure
47a7e6055SDimitry Andric //
57a7e6055SDimitry Andric // This file is distributed under the University of Illinois Open Source
67a7e6055SDimitry Andric // License. See LICENSE.TXT for details.
77a7e6055SDimitry Andric //
87a7e6055SDimitry Andric //===----------------------------------------------------------------------===//
97a7e6055SDimitry Andric //
107a7e6055SDimitry Andric // This file edits function bodies to insert fentry calls.
117a7e6055SDimitry Andric //
127a7e6055SDimitry Andric //===----------------------------------------------------------------------===//
137a7e6055SDimitry Andric 
147a7e6055SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
157a7e6055SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
167a7e6055SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
177a7e6055SDimitry Andric #include "llvm/CodeGen/Passes.h"
18*2cab237bSDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
19*2cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
20*2cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
217a7e6055SDimitry Andric #include "llvm/IR/Function.h"
227a7e6055SDimitry Andric #include "llvm/IR/Module.h"
237a7e6055SDimitry Andric 
247a7e6055SDimitry Andric using namespace llvm;
257a7e6055SDimitry Andric 
267a7e6055SDimitry Andric namespace {
277a7e6055SDimitry Andric struct FEntryInserter : public MachineFunctionPass {
287a7e6055SDimitry Andric   static char ID; // Pass identification, replacement for typeid
FEntryInserter__anon4a8d72dd0111::FEntryInserter297a7e6055SDimitry Andric   FEntryInserter() : MachineFunctionPass(ID) {
307a7e6055SDimitry Andric     initializeFEntryInserterPass(*PassRegistry::getPassRegistry());
317a7e6055SDimitry Andric   }
327a7e6055SDimitry Andric 
337a7e6055SDimitry Andric   bool runOnMachineFunction(MachineFunction &F) override;
347a7e6055SDimitry Andric };
357a7e6055SDimitry Andric }
367a7e6055SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)377a7e6055SDimitry Andric bool FEntryInserter::runOnMachineFunction(MachineFunction &MF) {
387a7e6055SDimitry Andric   const std::string FEntryName =
39*2cab237bSDimitry Andric       MF.getFunction().getFnAttribute("fentry-call").getValueAsString();
407a7e6055SDimitry Andric   if (FEntryName != "true")
417a7e6055SDimitry Andric     return false;
427a7e6055SDimitry Andric 
437a7e6055SDimitry Andric   auto &FirstMBB = *MF.begin();
447a7e6055SDimitry Andric   auto *TII = MF.getSubtarget().getInstrInfo();
45*2cab237bSDimitry Andric   BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
467a7e6055SDimitry Andric           TII->get(TargetOpcode::FENTRY_CALL));
477a7e6055SDimitry Andric   return true;
487a7e6055SDimitry Andric }
497a7e6055SDimitry Andric 
507a7e6055SDimitry Andric char FEntryInserter::ID = 0;
517a7e6055SDimitry Andric char &llvm::FEntryInserterID = FEntryInserter::ID;
527a7e6055SDimitry Andric INITIALIZE_PASS(FEntryInserter, "fentry-insert", "Insert fentry calls", false,
537a7e6055SDimitry Andric                 false)
54