Nirav Dave | a7c041d | 2017-01-31 17:00:27 +0000 | [diff] [blame] | 1 | //===-- FEntryInsertion.cpp - Patchable prologues for LLVM -------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Nirav Dave | a7c041d | 2017-01-31 17:00:27 +0000 | [diff] [blame] | 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" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/TargetFrameLowering.h" |
| 18 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Nirav Dave | a7c041d | 2017-01-31 17:00:27 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/Module.h" |
Nirav Dave | a7c041d | 2017-01-31 17:00:27 +0000 | [diff] [blame] | 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 = |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 38 | MF.getFunction().getFnAttribute("fentry-call").getValueAsString(); |
Nirav Dave | a7c041d | 2017-01-31 17:00:27 +0000 | [diff] [blame] | 39 | if (FEntryName != "true") |
| 40 | return false; |
| 41 | |
| 42 | auto &FirstMBB = *MF.begin(); |
Nirav Dave | a7c041d | 2017-01-31 17:00:27 +0000 | [diff] [blame] | 43 | auto *TII = MF.getSubtarget().getInstrInfo(); |
Manoj Gupta | 7ebbe65 | 2017-08-01 15:39:12 +0000 | [diff] [blame] | 44 | BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(), |
Nirav Dave | a7c041d | 2017-01-31 17:00:27 +0000 | [diff] [blame] | 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) |