1 //===- EntryExitInstrumenter.cpp - Function Entry/Exit Instrumentation ----===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h" 10 #include "llvm/Analysis/GlobalsModRef.h" 11 #include "llvm/IR/DebugInfoMetadata.h" 12 #include "llvm/IR/Dominators.h" 13 #include "llvm/IR/Function.h" 14 #include "llvm/IR/Instructions.h" 15 #include "llvm/IR/Intrinsics.h" 16 #include "llvm/IR/Module.h" 17 #include "llvm/IR/Type.h" 18 #include "llvm/TargetParser/Triple.h" 19 20 using namespace llvm; 21 22 static void insertCall(Function &CurFn, StringRef Func, 23 Instruction *InsertionPt, DebugLoc DL) { 24 Module &M = *InsertionPt->getParent()->getParent()->getParent(); 25 LLVMContext &C = InsertionPt->getParent()->getContext(); 26 27 if (Func == "mcount" || 28 Func == ".mcount" || 29 Func == "llvm.arm.gnu.eabi.mcount" || 30 Func == "\01_mcount" || 31 Func == "\01mcount" || 32 Func == "__mcount" || 33 Func == "_mcount" || 34 Func == "__cyg_profile_func_enter_bare") { 35 Triple TargetTriple(M.getTargetTriple()); 36 if (TargetTriple.isOSAIX() && Func == "__mcount") { 37 Type *SizeTy = M.getDataLayout().getIntPtrType(C); 38 Type *SizePtrTy = SizeTy->getPointerTo(); 39 GlobalVariable *GV = new GlobalVariable(M, SizeTy, /*isConstant=*/false, 40 GlobalValue::InternalLinkage, 41 ConstantInt::get(SizeTy, 0)); 42 CallInst *Call = CallInst::Create( 43 M.getOrInsertFunction(Func, 44 FunctionType::get(Type::getVoidTy(C), {SizePtrTy}, 45 /*isVarArg=*/false)), 46 {GV}, "", InsertionPt); 47 Call->setDebugLoc(DL); 48 } else { 49 FunctionCallee Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C)); 50 CallInst *Call = CallInst::Create(Fn, "", InsertionPt); 51 Call->setDebugLoc(DL); 52 } 53 return; 54 } 55 56 if (Func == "__cyg_profile_func_enter" || Func == "__cyg_profile_func_exit") { 57 Type *ArgTypes[] = {Type::getInt8PtrTy(C), Type::getInt8PtrTy(C)}; 58 59 FunctionCallee Fn = M.getOrInsertFunction( 60 Func, FunctionType::get(Type::getVoidTy(C), ArgTypes, false)); 61 62 Instruction *RetAddr = CallInst::Create( 63 Intrinsic::getDeclaration(&M, Intrinsic::returnaddress), 64 ArrayRef<Value *>(ConstantInt::get(Type::getInt32Ty(C), 0)), "", 65 InsertionPt); 66 RetAddr->setDebugLoc(DL); 67 68 Value *Args[] = {ConstantExpr::getBitCast(&CurFn, Type::getInt8PtrTy(C)), 69 RetAddr}; 70 71 CallInst *Call = 72 CallInst::Create(Fn, ArrayRef<Value *>(Args), "", InsertionPt); 73 Call->setDebugLoc(DL); 74 return; 75 } 76 77 // We only know how to call a fixed set of instrumentation functions, because 78 // they all expect different arguments, etc. 79 report_fatal_error(Twine("Unknown instrumentation function: '") + Func + "'"); 80 } 81 82 static bool runOnFunction(Function &F, bool PostInlining) { 83 StringRef EntryAttr = PostInlining ? "instrument-function-entry-inlined" 84 : "instrument-function-entry"; 85 86 StringRef ExitAttr = PostInlining ? "instrument-function-exit-inlined" 87 : "instrument-function-exit"; 88 89 StringRef EntryFunc = F.getFnAttribute(EntryAttr).getValueAsString(); 90 StringRef ExitFunc = F.getFnAttribute(ExitAttr).getValueAsString(); 91 92 bool Changed = false; 93 94 // If the attribute is specified, insert instrumentation and then "consume" 95 // the attribute so that it's not inserted again if the pass should happen to 96 // run later for some reason. 97 98 if (!EntryFunc.empty()) { 99 DebugLoc DL; 100 if (auto SP = F.getSubprogram()) 101 DL = DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP); 102 103 insertCall(F, EntryFunc, &*F.begin()->getFirstInsertionPt(), DL); 104 Changed = true; 105 F.removeFnAttr(EntryAttr); 106 } 107 108 if (!ExitFunc.empty()) { 109 for (BasicBlock &BB : F) { 110 Instruction *T = BB.getTerminator(); 111 if (!isa<ReturnInst>(T)) 112 continue; 113 114 // If T is preceded by a musttail call, that's the real terminator. 115 if (CallInst *CI = BB.getTerminatingMustTailCall()) 116 T = CI; 117 118 DebugLoc DL; 119 if (DebugLoc TerminatorDL = T->getDebugLoc()) 120 DL = TerminatorDL; 121 else if (auto SP = F.getSubprogram()) 122 DL = DILocation::get(SP->getContext(), 0, 0, SP); 123 124 insertCall(F, ExitFunc, T, DL); 125 Changed = true; 126 } 127 F.removeFnAttr(ExitAttr); 128 } 129 130 return Changed; 131 } 132 133 PreservedAnalyses 134 llvm::EntryExitInstrumenterPass::run(Function &F, FunctionAnalysisManager &AM) { 135 runOnFunction(F, PostInlining); 136 PreservedAnalyses PA; 137 PA.preserveSet<CFGAnalyses>(); 138 return PA; 139 } 140 141 void llvm::EntryExitInstrumenterPass::printPipeline( 142 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) { 143 static_cast<PassInfoMixin<llvm::EntryExitInstrumenterPass> *>(this) 144 ->printPipeline(OS, MapClassName2PassName); 145 OS << '<'; 146 if (PostInlining) 147 OS << "post-inline"; 148 OS << '>'; 149 } 150