1 //===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the common initialization infrastructure for the
10 // Instrumentation library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Utils/Instrumentation.h"
15 #include "llvm/IR/DiagnosticInfo.h"
16 #include "llvm/IR/DiagnosticPrinter.h"
17 #include "llvm/IR/IntrinsicInst.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/TargetParser/Triple.h"
23 static cl::opt
<bool> ClIgnoreRedundantInstrumentation(
24 "ignore-redundant-instrumentation",
25 cl::desc("Ignore redundant instrumentation"), cl::Hidden
, cl::init(false));
27 /// Check if module has flag attached, if not add the flag.
28 bool llvm::checkIfAlreadyInstrumented(Module
&M
, StringRef Flag
) {
29 if (!M
.getModuleFlag(Flag
)) {
30 M
.addModuleFlag(Module::ModFlagBehavior::Override
, Flag
, 1);
33 if (ClIgnoreRedundantInstrumentation
)
35 std::string diagInfo
=
36 "Redundant instrumentation detected, with module flag: " +
38 M
.getContext().diagnose(
39 DiagnosticInfoInstrumentation(diagInfo
, DiagnosticSeverity::DS_Warning
));
43 /// Moves I before IP. Returns new insert point.
44 static BasicBlock::iterator
moveBeforeInsertPoint(BasicBlock::iterator I
,
45 BasicBlock::iterator IP
) {
46 // If I is IP, move the insert point down.
50 // Otherwise, move I before IP and return IP.
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
61 BasicBlock::iterator
llvm::PrepareToSplitEntryBlock(BasicBlock
&BB
,
62 BasicBlock::iterator IP
) {
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())
69 } else if (auto *II
= dyn_cast
<IntrinsicInst
>(I
)) {
70 if (II
->getIntrinsicID() == llvm::Intrinsic::localescape
)
74 IP
= moveBeforeInsertPoint(I
, IP
);
79 // Create a constant for Str so that we can pass it to the run-time lib.
80 GlobalVariable
*llvm::createPrivateGlobalForString(Module
&M
, StringRef Str
,
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.
87 new GlobalVariable(M
, StrConst
->getType(), true,
88 GlobalValue::PrivateLinkage
, StrConst
, NamePrefix
);
90 GV
->setUnnamedAddr(GlobalValue::UnnamedAddr::Global
);
91 GV
->setAlignment(Align(1)); // Strings may not be merged w/o setting
92 // alignment explicitly.
96 Comdat
*llvm::getOrCreateFunctionComdat(Function
&F
, Triple
&T
) {
97 if (auto Comdat
= F
.getComdat())
100 Module
*M
= F
.getParent();
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
105 Comdat
*C
= M
->getOrInsertComdat(F
.getName());
106 if (T
.isOSBinFormatELF() || (T
.isOSBinFormatCOFF() && !F
.isWeakForLinker()))
107 C
->setSelectionKind(Comdat::NoDeduplicate
);
112 void llvm::setGlobalVariableLargeSection(const Triple
&TargetTriple
,
113 GlobalVariable
&GV
) {
114 // Limit to x86-64 ELF.
115 if (TargetTriple
.getArch() != Triple::x86_64
||
116 TargetTriple
.getObjectFormat() != Triple::ELF
)
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
))
122 GV
.setCodeModel(CodeModel::Large
);