2017-11-15 05:09:45 +08:00
|
|
|
//===- EntryExitInstrumenter.cpp - Function Entry/Exit Instrumentation ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
|
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
2017-11-29 02:44:26 +08:00
|
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
2017-11-15 05:09:45 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/Pass.h"
|
2018-03-29 01:44:36 +08:00
|
|
|
#include "llvm/Transforms/Utils.h"
|
2017-11-15 05:09:45 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static void insertCall(Function &CurFn, StringRef Func,
|
2017-11-29 02:44:26 +08:00
|
|
|
Instruction *InsertionPt, DebugLoc DL) {
|
2017-11-15 05:09:45 +08:00
|
|
|
Module &M = *InsertionPt->getParent()->getParent()->getParent();
|
|
|
|
LLVMContext &C = InsertionPt->getParent()->getContext();
|
|
|
|
|
|
|
|
if (Func == "mcount" ||
|
|
|
|
Func == ".mcount" ||
|
|
|
|
Func == "\01__gnu_mcount_nc" ||
|
|
|
|
Func == "\01_mcount" ||
|
|
|
|
Func == "\01mcount" ||
|
|
|
|
Func == "__mcount" ||
|
2017-11-22 01:22:19 +08:00
|
|
|
Func == "_mcount" ||
|
|
|
|
Func == "__cyg_profile_func_enter_bare") {
|
2017-11-15 05:09:45 +08:00
|
|
|
Constant *Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
|
2017-11-29 02:44:26 +08:00
|
|
|
CallInst *Call = CallInst::Create(Fn, "", InsertionPt);
|
|
|
|
Call->setDebugLoc(DL);
|
2017-11-15 05:09:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Func == "__cyg_profile_func_enter" || Func == "__cyg_profile_func_exit") {
|
|
|
|
Type *ArgTypes[] = {Type::getInt8PtrTy(C), Type::getInt8PtrTy(C)};
|
|
|
|
|
|
|
|
Constant *Fn = M.getOrInsertFunction(
|
|
|
|
Func, FunctionType::get(Type::getVoidTy(C), ArgTypes, false));
|
|
|
|
|
|
|
|
Instruction *RetAddr = CallInst::Create(
|
|
|
|
Intrinsic::getDeclaration(&M, Intrinsic::returnaddress),
|
|
|
|
ArrayRef<Value *>(ConstantInt::get(Type::getInt32Ty(C), 0)), "",
|
|
|
|
InsertionPt);
|
2017-11-29 02:44:26 +08:00
|
|
|
RetAddr->setDebugLoc(DL);
|
2017-11-15 05:09:45 +08:00
|
|
|
|
|
|
|
Value *Args[] = {ConstantExpr::getBitCast(&CurFn, Type::getInt8PtrTy(C)),
|
|
|
|
RetAddr};
|
|
|
|
|
2017-11-29 02:44:26 +08:00
|
|
|
CallInst *Call =
|
|
|
|
CallInst::Create(Fn, ArrayRef<Value *>(Args), "", InsertionPt);
|
|
|
|
Call->setDebugLoc(DL);
|
2017-11-15 05:09:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only know how to call a fixed set of instrumentation functions, because
|
|
|
|
// they all expect different arguments, etc.
|
|
|
|
report_fatal_error(Twine("Unknown instrumentation function: '") + Func + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool runOnFunction(Function &F, bool PostInlining) {
|
|
|
|
StringRef EntryAttr = PostInlining ? "instrument-function-entry-inlined"
|
|
|
|
: "instrument-function-entry";
|
|
|
|
|
|
|
|
StringRef ExitAttr = PostInlining ? "instrument-function-exit-inlined"
|
|
|
|
: "instrument-function-exit";
|
|
|
|
|
|
|
|
StringRef EntryFunc = F.getFnAttribute(EntryAttr).getValueAsString();
|
|
|
|
StringRef ExitFunc = F.getFnAttribute(ExitAttr).getValueAsString();
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// If the attribute is specified, insert instrumentation and then "consume"
|
|
|
|
// the attribute so that it's not inserted again if the pass should happen to
|
|
|
|
// run later for some reason.
|
|
|
|
|
|
|
|
if (!EntryFunc.empty()) {
|
2017-11-29 02:44:26 +08:00
|
|
|
DebugLoc DL;
|
|
|
|
if (auto SP = F.getSubprogram())
|
|
|
|
DL = DebugLoc::get(SP->getScopeLine(), 0, SP);
|
|
|
|
|
|
|
|
insertCall(F, EntryFunc, &*F.begin()->getFirstInsertionPt(), DL);
|
2017-11-15 05:09:45 +08:00
|
|
|
Changed = true;
|
|
|
|
F.removeAttribute(AttributeList::FunctionIndex, EntryAttr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ExitFunc.empty()) {
|
|
|
|
for (BasicBlock &BB : F) {
|
2018-04-06 18:14:09 +08:00
|
|
|
Instruction *T = BB.getTerminator();
|
|
|
|
if (!isa<ReturnInst>(T))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If T is preceded by a musttail call, that's the real terminator.
|
|
|
|
Instruction *Prev = T->getPrevNode();
|
|
|
|
if (BitCastInst *BCI = dyn_cast_or_null<BitCastInst>(Prev))
|
|
|
|
Prev = BCI->getPrevNode();
|
|
|
|
if (CallInst *CI = dyn_cast_or_null<CallInst>(Prev)) {
|
|
|
|
if (CI->isMustTailCall())
|
|
|
|
T = CI;
|
|
|
|
}
|
|
|
|
|
2017-11-29 02:44:26 +08:00
|
|
|
DebugLoc DL;
|
|
|
|
if (DebugLoc TerminatorDL = T->getDebugLoc())
|
|
|
|
DL = TerminatorDL;
|
|
|
|
else if (auto SP = F.getSubprogram())
|
|
|
|
DL = DebugLoc::get(0, 0, SP);
|
|
|
|
|
2018-04-06 18:14:09 +08:00
|
|
|
insertCall(F, ExitFunc, T, DL);
|
|
|
|
Changed = true;
|
2017-11-15 05:09:45 +08:00
|
|
|
}
|
|
|
|
F.removeAttribute(AttributeList::FunctionIndex, ExitAttr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct EntryExitInstrumenter : public FunctionPass {
|
|
|
|
static char ID;
|
|
|
|
EntryExitInstrumenter() : FunctionPass(ID) {
|
|
|
|
initializeEntryExitInstrumenterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
|
|
|
}
|
|
|
|
bool runOnFunction(Function &F) override { return ::runOnFunction(F, false); }
|
|
|
|
};
|
|
|
|
char EntryExitInstrumenter::ID = 0;
|
|
|
|
|
|
|
|
struct PostInlineEntryExitInstrumenter : public FunctionPass {
|
|
|
|
static char ID;
|
|
|
|
PostInlineEntryExitInstrumenter() : FunctionPass(ID) {
|
|
|
|
initializePostInlineEntryExitInstrumenterPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
|
|
|
}
|
|
|
|
bool runOnFunction(Function &F) override { return ::runOnFunction(F, true); }
|
|
|
|
};
|
|
|
|
char PostInlineEntryExitInstrumenter::ID = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
INITIALIZE_PASS(
|
|
|
|
EntryExitInstrumenter, "ee-instrument",
|
|
|
|
"Instrument function entry/exit with calls to e.g. mcount() (pre inlining)",
|
2017-11-15 07:13:38 +08:00
|
|
|
false, false)
|
2017-11-15 05:09:45 +08:00
|
|
|
INITIALIZE_PASS(PostInlineEntryExitInstrumenter, "post-inline-ee-instrument",
|
|
|
|
"Instrument function entry/exit with calls to e.g. mcount() "
|
|
|
|
"(post inlining)",
|
2017-11-15 07:13:38 +08:00
|
|
|
false, false)
|
2017-11-15 05:09:45 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createEntryExitInstrumenterPass() {
|
|
|
|
return new EntryExitInstrumenter();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createPostInlineEntryExitInstrumenterPass() {
|
|
|
|
return new PostInlineEntryExitInstrumenter();
|
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses
|
|
|
|
llvm::EntryExitInstrumenterPass::run(Function &F, FunctionAnalysisManager &AM) {
|
|
|
|
runOnFunction(F, PostInlining);
|
|
|
|
PreservedAnalyses PA;
|
|
|
|
PA.preserveSet<CFGAnalyses>();
|
|
|
|
return PA;
|
|
|
|
}
|