blob: a122f490884eee6489f5293dd830067138e44d7a [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"
Nirav Davea7c041d2017-01-31 17:00:27 +000022
23using namespace llvm;
24
25namespace {
26struct 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
36bool FEntryInserter::runOnMachineFunction(MachineFunction &MF) {
37 const std::string FEntryName =
Matthias Braunf1caa282017-12-15 22:22:58 +000038 MF.getFunction().getFnAttribute("fentry-call").getValueAsString();
Nirav Davea7c041d2017-01-31 17:00:27 +000039 if (FEntryName != "true")
40 return false;
41
42 auto &FirstMBB = *MF.begin();
Nirav Davea7c041d2017-01-31 17:00:27 +000043 auto *TII = MF.getSubtarget().getInstrInfo();
Manoj Gupta7ebbe652017-08-01 15:39:12 +000044 BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
Nirav Davea7c041d2017-01-31 17:00:27 +000045 TII->get(TargetOpcode::FENTRY_CALL));
46 return true;
47}
48
49char FEntryInserter::ID = 0;
50char &llvm::FEntryInserterID = FEntryInserter::ID;
51INITIALIZE_PASS(FEntryInserter, "fentry-insert", "Insert fentry calls", false,
52 false)