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
27/// Check if module has flag attached, if not add the flag.
29 if (!M.getModuleFlag(Flag)) {
30 M.addModuleFlag(Module::ModFlagBehavior::Override, Flag, 1);
31 return false;
32 }
34 return true;
35 std::string diagInfo =
36 "Redundant instrumentation detected, with module flag: " +
37 std::string(Flag);
38 M.getContext().diagnose(
39 DiagnosticInfoInstrumentation(diagInfo, DiagnosticSeverity::DS_Warning));
40 return true;
41}
42
43/// Moves I before IP. Returns new insert point.
46 // If I is IP, move the insert point down.
47 if (I == IP) {
48 ++IP;
49 } else {
50 // Otherwise, move I before IP and return IP.
51 I->moveBefore(&*IP);
52 }
53 return IP;
54}
55
56/// Instrumentation passes often insert conditional checks into entry blocks.
57/// Call this function before splitting the entry block to move instructions
58/// that must remain in the entry block up before the split point. Static
59/// allocas and llvm.localescape calls, for example, must remain in the entry
60/// block.
63 assert(&BB.getParent()->getEntryBlock() == &BB);
64 for (auto I = IP, E = BB.end(); I != E; ++I) {
65 bool KeepInEntry = false;
66 if (auto *AI = dyn_cast<AllocaInst>(I)) {
67 if (AI->isStaticAlloca())
68 KeepInEntry = true;
69 } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
70 if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
71 KeepInEntry = true;
72 }
73 if (KeepInEntry)
74 IP = moveBeforeInsertPoint(I, IP);
75 }
76 return IP;
77}
78
79// Create a constant for Str so that we can pass it to the run-time lib.
81 bool AllowMerging,
82 Twine NamePrefix) {
83 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
84 // We use private linkage for module-local strings. If they can be merged
85 // with another one, we set the unnamed_addr attribute.
86 GlobalVariable *GV =
87 new GlobalVariable(M, StrConst->getType(), true,
88 GlobalValue::PrivateLinkage, StrConst, NamePrefix);
89 if (AllowMerging)
90 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
91 GV->setAlignment(Align(1)); // Strings may not be merged w/o setting
92 // alignment explicitly.
93 return GV;
94}
95
97 if (auto Comdat = F.getComdat())
98 return Comdat;
99 assert(F.hasName());
100 Module *M = F.getParent();
101
102 // Make a new comdat for the function. Use the "no duplicates" selection kind
103 // if the object file format supports it. For COFF we restrict it to non-weak
104 // symbols.
105 Comdat *C = M->getOrInsertComdat(F.getName());
106 if (T.isOSBinFormatELF() || (T.isOSBinFormatCOFF() && !F.isWeakForLinker()))
107 C->setSelectionKind(Comdat::NoDeduplicate);
108 F.setComdat(C);
109 return C;
110}
111
113 GlobalVariable &GV) {
114 // Limit to x86-64 ELF.
115 if (TargetTriple.getArch() != Triple::x86_64 ||
116 TargetTriple.getObjectFormat() != Triple::ELF)
117 return;
118 // Limit to medium/large code models.
119 std::optional<CodeModel::Model> CM = GV.getParent()->getCodeModel();
120 if (!CM || (*CM != CodeModel::Medium && *CM != CodeModel::Large))
121 return;
123}
Module.h This file contains the declarations for the Module class.
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
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:2990
This is an important base class in LLVM.
Definition: Constant.h:42
Diagnostic information for IR instrumentation reporting.
const BasicBlock & getEntryBlock() const
Definition: Function.h:809
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition: Globals.cpp:143
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:534
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:647
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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:409
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:383
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="")
Comdat * getOrCreateFunctionComdat(Function &F, Triple &T)
void setGlobalVariableLargeSection(const Triple &TargetTriple, GlobalVariable &GV)
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