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/Instrumentation.h"
15 #include "llvm-c/Initialization.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/IR/IntrinsicInst.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/InitializePasses.h"
20 #include "llvm/PassRegistry.h"
24 /// Moves I before IP. Returns new insert point.
25 static BasicBlock::iterator
moveBeforeInsertPoint(BasicBlock::iterator I
, BasicBlock::iterator IP
) {
26 // If I is IP, move the insert point down.
30 // Otherwise, move I before IP and return IP.
36 /// Instrumentation passes often insert conditional checks into entry blocks.
37 /// Call this function before splitting the entry block to move instructions
38 /// that must remain in the entry block up before the split point. Static
39 /// allocas and llvm.localescape calls, for example, must remain in the entry
41 BasicBlock::iterator
llvm::PrepareToSplitEntryBlock(BasicBlock
&BB
,
42 BasicBlock::iterator IP
) {
43 assert(&BB
.getParent()->getEntryBlock() == &BB
);
44 for (auto I
= IP
, E
= BB
.end(); I
!= E
; ++I
) {
45 bool KeepInEntry
= false;
46 if (auto *AI
= dyn_cast
<AllocaInst
>(I
)) {
47 if (AI
->isStaticAlloca())
49 } else if (auto *II
= dyn_cast
<IntrinsicInst
>(I
)) {
50 if (II
->getIntrinsicID() == llvm::Intrinsic::localescape
)
54 IP
= moveBeforeInsertPoint(I
, IP
);
59 // Create a constant for Str so that we can pass it to the run-time lib.
60 GlobalVariable
*llvm::createPrivateGlobalForString(Module
&M
, StringRef Str
,
62 const char *NamePrefix
) {
63 Constant
*StrConst
= ConstantDataArray::getString(M
.getContext(), Str
);
64 // We use private linkage for module-local strings. If they can be merged
65 // with another one, we set the unnamed_addr attribute.
67 new GlobalVariable(M
, StrConst
->getType(), true,
68 GlobalValue::PrivateLinkage
, StrConst
, NamePrefix
);
70 GV
->setUnnamedAddr(GlobalValue::UnnamedAddr::Global
);
71 GV
->setAlignment(Align(1)); // Strings may not be merged w/o setting
72 // alignment explicitly.
76 Comdat
*llvm::getOrCreateFunctionComdat(Function
&F
, Triple
&T
) {
77 if (auto Comdat
= F
.getComdat()) return Comdat
;
79 Module
*M
= F
.getParent();
81 // Make a new comdat for the function. Use the "no duplicates" selection kind
82 // if the object file format supports it. For COFF we restrict it to non-weak
84 Comdat
*C
= M
->getOrInsertComdat(F
.getName());
85 if (T
.isOSBinFormatELF() || (T
.isOSBinFormatCOFF() && !F
.isWeakForLinker()))
86 C
->setSelectionKind(Comdat::NoDeduplicate
);
91 /// initializeInstrumentation - Initialize all passes in the TransformUtils
93 void llvm::initializeInstrumentation(PassRegistry
&Registry
) {
94 initializeAddressSanitizerLegacyPassPass(Registry
);
95 initializeModuleAddressSanitizerLegacyPassPass(Registry
);
96 initializeMemProfilerLegacyPassPass(Registry
);
97 initializeModuleMemProfilerLegacyPassPass(Registry
);
98 initializeBoundsCheckingLegacyPassPass(Registry
);
99 initializeControlHeightReductionLegacyPassPass(Registry
);
100 initializeGCOVProfilerLegacyPassPass(Registry
);
101 initializePGOInstrumentationGenLegacyPassPass(Registry
);
102 initializePGOInstrumentationUseLegacyPassPass(Registry
);
103 initializePGOIndirectCallPromotionLegacyPassPass(Registry
);
104 initializePGOMemOPSizeOptLegacyPassPass(Registry
);
105 initializeCGProfileLegacyPassPass(Registry
);
106 initializeInstrOrderFileLegacyPassPass(Registry
);
107 initializeInstrProfilingLegacyPassPass(Registry
);
108 initializeMemorySanitizerLegacyPassPass(Registry
);
109 initializeHWAddressSanitizerLegacyPassPass(Registry
);
110 initializeThreadSanitizerLegacyPassPass(Registry
);
111 initializeModuleSanitizerCoverageLegacyPassPass(Registry
);
112 initializeDataFlowSanitizerLegacyPassPass(Registry
);
115 /// LLVMInitializeInstrumentation - C binding for
116 /// initializeInstrumentation.
117 void LLVMInitializeInstrumentation(LLVMPassRegistryRef R
) {
118 initializeInstrumentation(*unwrap(R
));