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.
29 // Otherwise, move I before IP and return IP.
34 /// Instrumentation passes often insert conditional checks into entry blocks.
35 /// Call this function before splitting the entry block to move instructions
36 /// that must remain in the entry block up before the split point. Static
37 /// allocas and llvm.localescape calls, for example, must remain in the entry
39 BasicBlock::iterator
llvm::PrepareToSplitEntryBlock(BasicBlock
&BB
,
40 BasicBlock::iterator IP
) {
41 assert(&BB
.getParent()->getEntryBlock() == &BB
);
42 for (auto I
= IP
, E
= BB
.end(); I
!= E
; ++I
) {
43 bool KeepInEntry
= false;
44 if (auto *AI
= dyn_cast
<AllocaInst
>(I
)) {
45 if (AI
->isStaticAlloca())
47 } else if (auto *II
= dyn_cast
<IntrinsicInst
>(I
)) {
48 if (II
->getIntrinsicID() == llvm::Intrinsic::localescape
)
52 IP
= moveBeforeInsertPoint(I
, IP
);
57 // Create a constant for Str so that we can pass it to the run-time lib.
58 GlobalVariable
*llvm::createPrivateGlobalForString(Module
&M
, StringRef Str
,
60 const char *NamePrefix
) {
61 Constant
*StrConst
= ConstantDataArray::getString(M
.getContext(), Str
);
62 // We use private linkage for module-local strings. If they can be merged
63 // with another one, we set the unnamed_addr attribute.
65 new GlobalVariable(M
, StrConst
->getType(), true,
66 GlobalValue::PrivateLinkage
, StrConst
, NamePrefix
);
68 GV
->setUnnamedAddr(GlobalValue::UnnamedAddr::Global
);
69 GV
->setAlignment(1); // Strings may not be merged w/o setting align 1.
73 Comdat
*llvm::GetOrCreateFunctionComdat(Function
&F
, Triple
&T
,
74 const std::string
&ModuleId
) {
75 if (auto Comdat
= F
.getComdat()) return Comdat
;
77 Module
*M
= F
.getParent();
78 std::string Name
= F
.getName();
80 // Make a unique comdat name for internal linkage things on ELF. On COFF, the
81 // name of the comdat group identifies the leader symbol of the comdat group.
82 // The linkage of the leader symbol is considered during comdat resolution,
83 // and internal symbols with the same name from different objects will not be
85 if (T
.isOSBinFormatELF() && F
.hasLocalLinkage()) {
91 // Make a new comdat for the function. Use the "no duplicates" selection kind
92 // for non-weak symbols if the object file format supports it.
93 Comdat
*C
= M
->getOrInsertComdat(Name
);
94 if (T
.isOSBinFormatCOFF() && !F
.isWeakForLinker())
95 C
->setSelectionKind(Comdat::NoDuplicates
);
100 /// initializeInstrumentation - Initialize all passes in the TransformUtils
102 void llvm::initializeInstrumentation(PassRegistry
&Registry
) {
103 initializeAddressSanitizerLegacyPassPass(Registry
);
104 initializeModuleAddressSanitizerLegacyPassPass(Registry
);
105 initializeBoundsCheckingLegacyPassPass(Registry
);
106 initializeControlHeightReductionLegacyPassPass(Registry
);
107 initializeGCOVProfilerLegacyPassPass(Registry
);
108 initializePGOInstrumentationGenLegacyPassPass(Registry
);
109 initializePGOInstrumentationUseLegacyPassPass(Registry
);
110 initializePGOIndirectCallPromotionLegacyPassPass(Registry
);
111 initializePGOMemOPSizeOptLegacyPassPass(Registry
);
112 initializeInstrProfilingLegacyPassPass(Registry
);
113 initializeMemorySanitizerLegacyPassPass(Registry
);
114 initializeHWAddressSanitizerPass(Registry
);
115 initializeThreadSanitizerLegacyPassPass(Registry
);
116 initializeSanitizerCoverageModulePass(Registry
);
117 initializeDataFlowSanitizerPass(Registry
);
118 initializeEfficiencySanitizerPass(Registry
);
121 /// LLVMInitializeInstrumentation - C binding for
122 /// initializeInstrumentation.
123 void LLVMInitializeInstrumentation(LLVMPassRegistryRef R
) {
124 initializeInstrumentation(*unwrap(R
));