blob: 043a1a48db6ee9de28b82eca497c9d0429f500f6 [file] [log] [blame]
Dean Michael Berris52735fc2016-07-14 04:06:33 +00001//===-- XRayInstrumentation.cpp - Adds XRay instrumentation to functions. -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a MachineFunctionPass that inserts the appropriate
11// XRay instrumentation instructions. We look for XRay-specific attributes
12// on the function to determine whether we should insert the replacement
13// operations.
14//
15//===---------------------------------------------------------------------===//
16
17#include "llvm/CodeGen/Analysis.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/Support/TargetRegistry.h"
23#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetSubtargetInfo.h"
25
26using namespace llvm;
27
28namespace {
29struct XRayInstrumentation : public MachineFunctionPass {
30 static char ID;
31
32 XRayInstrumentation() : MachineFunctionPass(ID) {
33 initializeXRayInstrumentationPass(*PassRegistry::getPassRegistry());
34 }
35
36 bool runOnMachineFunction(MachineFunction &MF) override;
37};
Renato Golin049f3872016-09-08 17:10:39 +000038}
Dean Michael Berris52735fc2016-07-14 04:06:33 +000039
Renato Golin049f3872016-09-08 17:10:39 +000040bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
41 auto &F = *MF.getFunction();
42 auto InstrAttr = F.getFnAttribute("function-instrument");
43 bool AlwaysInstrument = !InstrAttr.hasAttribute(Attribute::None) &&
44 InstrAttr.isStringAttribute() &&
45 InstrAttr.getValueAsString() == "xray-always";
46 Attribute Attr = F.getFnAttribute("xray-instruction-threshold");
47 unsigned XRayThreshold = 0;
48 if (!AlwaysInstrument) {
49 if (Attr.hasAttribute(Attribute::None) || !Attr.isStringAttribute())
50 return false; // XRay threshold attribute not found.
51 if (Attr.getValueAsString().getAsInteger(10, XRayThreshold))
52 return false; // Invalid value for threshold.
53 if (F.size() < XRayThreshold)
54 return false; // Function is too small.
55 }
56
57 // FIXME: Do the loop triviality analysis here or in an earlier pass.
58
59 // First, insert an PATCHABLE_FUNCTION_ENTER as the first instruction of the
60 // MachineFunction.
61 auto &FirstMBB = *MF.begin();
62 auto &FirstMI = *FirstMBB.begin();
63 auto *TII = MF.getSubtarget().getInstrInfo();
64 BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(),
65 TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
66
67 // Then we look for *all* terminators and returns, then replace those with
Dean Michael Berris52735fc2016-07-14 04:06:33 +000068 // PATCHABLE_RET instructions.
69 SmallVector<MachineInstr *, 4> Terminators;
70 for (auto &MBB : MF) {
71 for (auto &T : MBB.terminators()) {
Dean Michael Berrise8ae5ba2016-09-01 01:29:13 +000072 unsigned Opc = 0;
Dean Michael Berris52735fc2016-07-14 04:06:33 +000073 if (T.isReturn() && T.getOpcode() == TII->getReturnOpcode()) {
74 // Replace return instructions with:
75 // PATCHABLE_RET <Opcode>, <Operand>...
Dean Michael Berrise8ae5ba2016-09-01 01:29:13 +000076 Opc = TargetOpcode::PATCHABLE_RET;
77 }
78 if (TII->isTailCall(T)) {
79 // Treat the tail call as a return instruction, which has a
80 // different-looking sled than the normal return case.
81 Opc = TargetOpcode::PATCHABLE_TAIL_CALL;
82 }
83 if (Opc != 0) {
84 auto MIB = BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc))
Dean Michael Berris52735fc2016-07-14 04:06:33 +000085 .addImm(T.getOpcode());
86 for (auto &MO : T.operands())
87 MIB.addOperand(MO);
88 Terminators.push_back(&T);
Dean Michael Berris52735fc2016-07-14 04:06:33 +000089 }
90 }
91 }
92
93 for (auto &I : Terminators)
94 I->eraseFromParent();
95
96 return true;
97}
98
99char XRayInstrumentation::ID = 0;
100char &llvm::XRayInstrumentationID = XRayInstrumentation::ID;
101INITIALIZE_PASS(XRayInstrumentation, "xray-instrumentation", "Insert XRay ops",
Dean Michael Berris086639a2016-07-14 11:46:41 +0000102 false, false)