Home | History | Annotate | Download | only in CodeGen
      1 //===-- 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 
     26 using namespace llvm;
     27 
     28 namespace {
     29 struct 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 };
     38 }
     39 
     40 bool 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
     68   // PATCHABLE_RET instructions.
     69   SmallVector<MachineInstr *, 4> Terminators;
     70   for (auto &MBB : MF) {
     71     for (auto &T : MBB.terminators()) {
     72       // FIXME: Handle tail calls here too?
     73       if (T.isReturn() && T.getOpcode() == TII->getReturnOpcode()) {
     74         // Replace return instructions with:
     75         //   PATCHABLE_RET <Opcode>, <Operand>...
     76         auto MIB = BuildMI(MBB, T, T.getDebugLoc(),
     77                            TII->get(TargetOpcode::PATCHABLE_RET))
     78                        .addImm(T.getOpcode());
     79         for (auto &MO : T.operands())
     80           MIB.addOperand(MO);
     81         Terminators.push_back(&T);
     82         break;
     83       }
     84     }
     85   }
     86 
     87   for (auto &I : Terminators)
     88     I->eraseFromParent();
     89 
     90   return true;
     91 }
     92 
     93 char XRayInstrumentation::ID = 0;
     94 char &llvm::XRayInstrumentationID = XRayInstrumentation::ID;
     95 INITIALIZE_PASS(XRayInstrumentation, "xray-instrumentation", "Insert XRay ops",
     96                 false, false)
     97