LLVM 20.0.0git
Instrumentation.cpp
Go to the documentation of this file.
1//===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
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// This file defines the common initialization infrastructure for the
10// Instrumentation library.
11//
12//===----------------------------------------------------------------------===//
13
18#include "llvm/IR/Module.h"
20
21using namespace llvm;
22
24 "ignore-redundant-instrumentation",
25 cl::desc("Ignore redundant instrumentation"), cl::Hidden, cl::init(false));
26
27namespace {
28/// Diagnostic information for IR instrumentation reporting.
29class DiagnosticInfoInstrumentation : public DiagnosticInfo {
30 const Twine &Msg;
31
32public:
33 DiagnosticInfoInstrumentation(const Twine &DiagMsg,
35 : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
36 void print(DiagnosticPrinter &DP) const override { DP << Msg; }
37};
38} // namespace
39
40/// Check if module has flag attached, if not add the flag.
42 if (!M.getModuleFlag(Flag)) {
43 M.addModuleFlag(Module::ModFlagBehavior::Override, Flag, 1);
44 return false;
45 }
47 return true;
48 std::string diagInfo =
49 "Redundant instrumentation detected, with module flag: " +
50 std::string(Flag);
51 M.getContext().diagnose(
52 DiagnosticInfoInstrumentation(diagInfo, DiagnosticSeverity::DS_Warning));
53 return true;
54}
55
56/// Moves I before IP. Returns new insert point.
58 // If I is IP, move the insert point down.
59 if (I == IP) {
60 ++IP;
61 } else {
62 // Otherwise, move I before IP and return IP.
63 I->moveBefore(&*IP);
64 }
65 return IP;
66}
67
68/// Instrumentation passes often insert conditional checks into entry blocks.
69/// Call this function before splitting the entry block to move instructions
70/// that must remain in the entry block up before the split point. Static
71/// allocas and llvm.localescape calls, for example, must remain in the entry
72/// block.
75 assert(&BB.getParent()->getEntryBlock() == &BB);
76 for (auto I = IP, E = BB.end(); I != E; ++I) {
77 bool KeepInEntry = false;
78 if (auto *AI = dyn_cast<AllocaInst>(I)) {
79 if (AI->isStaticAlloca())
80 KeepInEntry = true;
81 } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
82 if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
83 KeepInEntry = true;
84 }
85 if (KeepInEntry)
86 IP = moveBeforeInsertPoint(I, IP);
87 }
88 return IP;
89}
90
91// Create a constant for Str so that we can pass it to the run-time lib.
93 bool AllowMerging,
94 Twine NamePrefix) {
95 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
96 // We use private linkage for module-local strings. If they can be merged
97 // with another one, we set the unnamed_addr attribute.
98 GlobalVariable *GV =
99 new GlobalVariable(M, StrConst->getType(), true,
100 GlobalValue::PrivateLinkage, StrConst, NamePrefix);
101 if (AllowMerging)
102 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
103 GV->setAlignment(Align(1)); // Strings may not be merged w/o setting
104 // alignment explicitly.
105 return GV;
106}
107
109 if (auto Comdat = F.getComdat()) return Comdat;
110 assert(F.hasName());
111 Module *M = F.getParent();
112
113 // Make a new comdat for the function. Use the "no duplicates" selection kind
114 // if the object file format supports it. For COFF we restrict it to non-weak
115 // symbols.
116 Comdat *C = M->getOrInsertComdat(F.getName());
117 if (T.isOSBinFormatELF() || (T.isOSBinFormatCOFF() && !F.isWeakForLinker()))
118 C->setSelectionKind(Comdat::NoDeduplicate);
119 F.setComdat(C);
120 return C;
121}
122
124 GlobalVariable &GV) {
125 // Limit to x86-64 ELF.
126 if (TargetTriple.getArch() != Triple::x86_64 ||
127 TargetTriple.getObjectFormat() != Triple::ELF)
128 return;
129 // Limit to medium/large code models.
130 std::optional<CodeModel::Model> CM = GV.getParent()->getCodeModel();
131 if (!CM || (*CM != CodeModel::Medium && *CM != CodeModel::Large))
132 return;
134}
static cl::opt< bool > ClIgnoreRedundantInstrumentation("ignore-redundant-instrumentation", cl::desc("Ignore redundant instrumentation"), cl::Hidden, cl::init(false))
static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP)
Moves I before IP. Returns new insert point.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Module.h This file contains the declarations for the Module class.
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator end()
Definition: BasicBlock.h:461
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:219
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
@ NoDeduplicate
No deduplication is performed.
Definition: Comdat.h:39
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2950
This is an important base class in LLVM.
Definition: Constant.h:42
This is the base abstract class for diagnostic reporting in the backend.
virtual void print(DiagnosticPrinter &DP) const =0
Print using the given DP a user-friendly message.
Interface for custom diagnostic printing.
const BasicBlock & getEntryBlock() const
Definition: Function.h:807
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition: Globals.cpp:137
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:231
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:60
void setCodeModel(CodeModel::Model CM)
Change the code model for this global.
Definition: Globals.cpp:527
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
std::optional< CodeModel::Model > getCodeModel() const
Returns the code model (tiny, small, kernel, medium or large model)
Definition: Module.cpp:612
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ObjectFormatType getObjectFormat() const
Get the object format for this triple.
Definition: Triple.h:399
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:373
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
GlobalVariable * createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, Twine NamePrefix="")
@ DK_Linker
Comdat * getOrCreateFunctionComdat(Function &F, Triple &T)
void setGlobalVariableLargeSection(const Triple &TargetTriple, GlobalVariable &GV)
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
@ DS_Warning
BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
bool checkIfAlreadyInstrumented(Module &M, StringRef Flag)
Check if module has flag attached, if not add the flag.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39