1 //===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the common initialization infrastructure for the
11 // Instrumentation library.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/Instrumentation.h"
16 #include "llvm-c/Initialization.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/PassRegistry.h"
25 /// Moves I before IP. Returns new insert point.
26 static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP) {
27 // If I is IP, move the insert point down.
30 // Otherwise, move I before IP and return IP.
35 /// Instrumentation passes often insert conditional checks into entry blocks.
36 /// Call this function before splitting the entry block to move instructions
37 /// that must remain in the entry block up before the split point. Static
38 /// allocas and llvm.localescape calls, for example, must remain in the entry
40 BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB,
41 BasicBlock::iterator IP) {
42 assert(&BB.getParent()->getEntryBlock() == &BB);
43 for (auto I = IP, E = BB.end(); I != E; ++I) {
44 bool KeepInEntry = false;
45 if (auto *AI = dyn_cast<AllocaInst>(I)) {
46 if (AI->isStaticAlloca())
48 } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
49 if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
53 IP = moveBeforeInsertPoint(I, IP);
58 // Create a constant for Str so that we can pass it to the run-time lib.
59 GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,
61 const char *NamePrefix) {
62 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
63 // We use private linkage for module-local strings. If they can be merged
64 // with another one, we set the unnamed_addr attribute.
66 new GlobalVariable(M, StrConst->getType(), true,
67 GlobalValue::PrivateLinkage, StrConst, NamePrefix);
69 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
70 GV->setAlignment(1); // Strings may not be merged w/o setting align 1.
74 Comdat *llvm::GetOrCreateFunctionComdat(Function &F, Triple &T,
75 const std::string &ModuleId) {
76 if (auto Comdat = F.getComdat()) return Comdat;
78 Module *M = F.getParent();
79 std::string Name = F.getName();
81 // Make a unique comdat name for internal linkage things on ELF. On COFF, the
82 // name of the comdat group identifies the leader symbol of the comdat group.
83 // The linkage of the leader symbol is considered during comdat resolution,
84 // and internal symbols with the same name from different objects will not be
86 if (T.isOSBinFormatELF() && F.hasLocalLinkage()) {
92 // Make a new comdat for the function. Use the "no duplicates" selection kind
93 // for non-weak symbols if the object file format supports it.
94 Comdat *C = M->getOrInsertComdat(Name);
95 if (T.isOSBinFormatCOFF() && !F.isWeakForLinker())
96 C->setSelectionKind(Comdat::NoDuplicates);
101 /// initializeInstrumentation - Initialize all passes in the TransformUtils
103 void llvm::initializeInstrumentation(PassRegistry &Registry) {
104 initializeAddressSanitizerPass(Registry);
105 initializeAddressSanitizerModulePass(Registry);
106 initializeBoundsCheckingLegacyPassPass(Registry);
107 initializeControlHeightReductionLegacyPassPass(Registry);
108 initializeGCOVProfilerLegacyPassPass(Registry);
109 initializePGOInstrumentationGenLegacyPassPass(Registry);
110 initializePGOInstrumentationUseLegacyPassPass(Registry);
111 initializePGOIndirectCallPromotionLegacyPassPass(Registry);
112 initializePGOMemOPSizeOptLegacyPassPass(Registry);
113 initializeInstrProfilingLegacyPassPass(Registry);
114 initializeMemorySanitizerLegacyPassPass(Registry);
115 initializeHWAddressSanitizerPass(Registry);
116 initializeThreadSanitizerLegacyPassPass(Registry);
117 initializeSanitizerCoverageModulePass(Registry);
118 initializeDataFlowSanitizerPass(Registry);
119 initializeEfficiencySanitizerPass(Registry);
122 /// LLVMInitializeInstrumentation - C binding for
123 /// initializeInstrumentation.
124 void LLVMInitializeInstrumentation(LLVMPassRegistryRef R) {
125 initializeInstrumentation(*unwrap(R));