blob: c2194929e2e7f0d4092800dae7db7e659ce97f48 [file] [log] [blame]
Nirav Davea7c041d2017-01-31 17:00:27 +00001//===-- FEntryInsertion.cpp - Patchable prologues for LLVM -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Nirav Davea7c041d2017-01-31 17:00:27 +00006//
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"
David Blaikie3f833ed2017-11-08 01:01:31 +000017#include "llvm/CodeGen/TargetFrameLowering.h"
18#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000019#include "llvm/CodeGen/TargetSubtargetInfo.h"
Nirav Davea7c041d2017-01-31 17:00:27 +000020#include "llvm/IR/Function.h"
21#include "llvm/IR/Module.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080022#include "llvm/InitializePasses.h"
Nirav Davea7c041d2017-01-31 17:00:27 +000023
24using namespace llvm;
25
26namespace {
27struct FEntryInserter : public MachineFunctionPass {
28 static char ID; // Pass identification, replacement for typeid
29 FEntryInserter() : MachineFunctionPass(ID) {
30 initializeFEntryInserterPass(*PassRegistry::getPassRegistry());
31 }
32
33 bool runOnMachineFunction(MachineFunction &F) override;
34};
35}
36
37bool FEntryInserter::runOnMachineFunction(MachineFunction &MF) {
Benjamin Krameradcd0262020-01-28 20:23:46 +010038 const std::string FEntryName = std::string(
39 MF.getFunction().getFnAttribute("fentry-call").getValueAsString());
Nirav Davea7c041d2017-01-31 17:00:27 +000040 if (FEntryName != "true")
41 return false;
42
43 auto &FirstMBB = *MF.begin();
Nirav Davea7c041d2017-01-31 17:00:27 +000044 auto *TII = MF.getSubtarget().getInstrInfo();
Manoj Gupta7ebbe652017-08-01 15:39:12 +000045 BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
Nirav Davea7c041d2017-01-31 17:00:27 +000046 TII->get(TargetOpcode::FENTRY_CALL));
47 return true;
48}
49
50char FEntryInserter::ID = 0;
51char &llvm::FEntryInserterID = FEntryInserter::ID;
52INITIALIZE_PASS(FEntryInserter, "fentry-insert", "Insert fentry calls", false,
53 false)