LLVM: lib/Transforms/Utils/EntryExitInstrumenter.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

22

23using namespace llvm;

24

27 Module &M = *InsertionPt->getParent()->getParent()->getParent();

28 LLVMContext &C = InsertionPt->getParent()->getContext();

29

30 if (Func == "mcount" ||

31 Func == ".mcount" ||

32 Func == "llvm.arm.gnu.eabi.mcount" ||

33 Func == "\01_mcount" ||

34 Func == "\01mcount" ||

35 Func == "__mcount" ||

36 Func == "_mcount" ||

37 Func == "__cyg_profile_func_enter_bare") {

38 Triple TargetTriple(M.getTargetTriple());

39 if (TargetTriple.isOSAIX() && Func == "__mcount") {

40 Type *SizeTy = M.getDataLayout().getIntPtrType(C);

44 ConstantInt::get(SizeTy, 0));

46 M.getOrInsertFunction(Func,

48 false)),

49 {GV}, "", InsertionPt);

50 Call->setDebugLoc(DL);

51 } else if (TargetTriple.isRISCV() || TargetTriple.isAArch64() ||

53

54

55

60

63 false));

65 Call->setDebugLoc(DL);

66 } else if (TargetTriple.isSystemZ()) {

67

68

71 } else {

74 Call->setDebugLoc(DL);

75 }

76 return;

77 }

78

79 if (Func == "__cyg_profile_func_enter" || Func == "__cyg_profile_func_exit") {

81

84

88 InsertionPt);

90

91 Value *Args[] = {&CurFn, RetAddr};

94 Call->setDebugLoc(DL);

95 return;

96 }

97

98

99

101}

102

104

105

106

107

108 if (F.hasFnAttribute(Attribute::Naked))

109 return false;

110

111

112

113

114 if (F.hasAvailableExternallyLinkage())

115 return false;

116

117 StringRef EntryAttr = PostInlining ? "instrument-function-entry-inlined"

118 : "instrument-function-entry";

119

120 StringRef ExitAttr = PostInlining ? "instrument-function-exit-inlined"

121 : "instrument-function-exit";

122

123 StringRef EntryFunc = F.getFnAttribute(EntryAttr).getValueAsString();

124 StringRef ExitFunc = F.getFnAttribute(ExitAttr).getValueAsString();

125

127

128

129

130

131

132 if (!EntryFunc.empty()) {

134 if (auto SP = F.getSubprogram())

135 DL = DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP);

136

137 insertCall(F, EntryFunc, F.begin()->getFirstInsertionPt(), DL);

139 F.removeFnAttr(EntryAttr);

140 }

141

142 if (!ExitFunc.empty()) {

146 continue;

147

148

149 if (CallInst *CI = BB.getTerminatingMustTailCall())

150 T = CI;

151

153 if (DebugLoc TerminatorDL = T->getDebugLoc())

154 DL = TerminatorDL;

155 else if (auto SP = F.getSubprogram())

157

160 }

161 F.removeFnAttr(ExitAttr);

162 }

163

165}

166

167namespace {

168struct PostInlineEntryExitInstrumenter : public FunctionPass {

169 static char ID;

170 PostInlineEntryExitInstrumenter() : FunctionPass(ID) {

173 }

174 void getAnalysisUsage(AnalysisUsage &AU) const override {

177 }

178 bool runOnFunction(Function &F) override { return ::runOnFunction(F, true); }

179};

180char PostInlineEntryExitInstrumenter::ID = 0;

181}

182

184 PostInlineEntryExitInstrumenter, "post-inline-ee-instrument",

185 "Instrument function entry/exit with calls to e.g. mcount() "

186 "(post inlining)",

187 false, false)

190 PostInlineEntryExitInstrumenter, "post-inline-ee-instrument",

191 "Instrument function entry/exit with calls to e.g. mcount() "

192 "(post inlining)",

194

196 return new PostInlineEntryExitInstrumenter();

197}

198

207

212 OS << '<';

214 OS << "post-inline";

215 OS << '>';

216}

MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL

static bool runOnFunction(Function &F, bool PostInlining)

Definition EntryExitInstrumenter.cpp:103

static void insertCall(Function &CurFn, StringRef Func, BasicBlock::iterator InsertionPt, DebugLoc DL)

Definition EntryExitInstrumenter.cpp:25

This is the interface for a simple mod/ref and alias analysis over globals.

Module.h This file contains the declarations for the Module class.

#define INITIALIZE_PASS_DEPENDENCY(depName)

#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)

#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)

AnalysisUsage & addPreserved()

Add the specified Pass class to the set of analyses preserved by this pass.

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)

Return a uniquified Attribute object.

LLVM Basic Block Representation.

InstListType::iterator iterator

Instruction iterators...

Represents analyses that only rely on functions' control flow.

This class represents a function call, abstracting a target machine's calling convention.

static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)

Legacy analysis pass which computes a DominatorTree.

A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...

FunctionPass class - This class is used to implement most global optimizations.

static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)

This static method is the primary way of constructing a FunctionType.

void addFnAttr(Attribute::AttrKind Kind)

Add function attributes to this function.

@ InternalLinkage

Rename collisions when linking (static functions).

void setDebugLoc(DebugLoc Loc)

Set the debug location information for this instruction.

This is an important class for using LLVM in a threaded context.

static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)

A Module instance is used to store all the information related to an LLVM module.

static LLVM_ABI PassRegistry * getPassRegistry()

getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...

static PointerType * getUnqual(Type *ElementType)

This constructs a pointer to an object of the specified type in the default address space (address sp...

A set of analyses that are preserved following a run of a transformation pass.

static PreservedAnalyses all()

Construct a special preserved set that preserves all passes.

PreservedAnalyses & preserveSet()

Mark an analysis set as preserved.

StringRef - Represent a constant reference to a string, i.e.

constexpr bool empty() const

empty - Check if the string is empty.

Triple - Helper class for working with autoconf configuration names.

bool isRISCV() const

Tests whether the target is RISC-V (32- and 64-bit).

bool isOSAIX() const

Tests whether the OS is AIX.

bool isAArch64() const

Tests whether the target is AArch64 (little and big endian).

bool isSystemZ() const

Tests whether the target is SystemZ.

bool isLoongArch() const

Tests whether the target is LoongArch (32- and 64-bit).

Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...

The instances of the Type class are immutable: once they are created, they are never changed.

static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)

static LLVM_ABI Type * getVoidTy(LLVMContext &C)

LLVM Value Representation.

An efficient, type-erasing, non-owning reference to a callable.

This class implements an extremely fast bulk output stream that can only output to a stream.

@ C

The default llvm calling convention, compatible with C.

LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})

Look up the Function declaration of the intrinsic id in the Module M.

This is an optimization pass for GlobalISel generic memory operations.

LLVM_ABI FunctionPass * createPostInlineEntryExitInstrumenterPass()

Definition EntryExitInstrumenter.cpp:195

LLVM_ABI void initializePostInlineEntryExitInstrumenterPass(PassRegistry &)

LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)

bool isa(const From &Val)

isa - Return true if the parameter to the template is an instance of one of the template type argu...

AnalysisManager< Function > FunctionAnalysisManager

Convenience typedef for the Function analysis manager.

PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)

Definition EntryExitInstrumenter.cpp:200

void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)

Definition EntryExitInstrumenter.cpp:208

A CRTP mix-in to automatically provide informational APIs needed for passes.