Recommit r373598 "[yaml2obj/obj2yaml] - Add support for SHT_LLVM_ADDRSIG sections."
[llvm-complete.git] / lib / Transforms / Instrumentation / Instrumentation.cpp
blob3a63cda60b31c2c8013cb6da5b5b5bad9fcdec37
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.
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"
22 using namespace llvm;
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.
27 if (I == IP) {
28 ++IP;
29 } else {
30 // Otherwise, move I before IP and return IP.
31 I->moveBefore(&*IP);
33 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
40 /// block.
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())
48 KeepInEntry = true;
49 } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
50 if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
51 KeepInEntry = true;
53 if (KeepInEntry)
54 IP = moveBeforeInsertPoint(I, IP);
56 return 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,
61 bool AllowMerging,
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.
66 GlobalVariable *GV =
67 new GlobalVariable(M, StrConst->getType(), true,
68 GlobalValue::PrivateLinkage, StrConst, NamePrefix);
69 if (AllowMerging)
70 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
71 GV->setAlignment(1); // Strings may not be merged w/o setting align 1.
72 return GV;
75 Comdat *llvm::GetOrCreateFunctionComdat(Function &F, Triple &T,
76 const std::string &ModuleId) {
77 if (auto Comdat = F.getComdat()) return Comdat;
78 assert(F.hasName());
79 Module *M = F.getParent();
80 std::string Name = F.getName();
82 // Make a unique comdat name for internal linkage things on ELF. On COFF, the
83 // name of the comdat group identifies the leader symbol of the comdat group.
84 // The linkage of the leader symbol is considered during comdat resolution,
85 // and internal symbols with the same name from different objects will not be
86 // merged.
87 if (T.isOSBinFormatELF() && F.hasLocalLinkage()) {
88 if (ModuleId.empty())
89 return nullptr;
90 Name += ModuleId;
93 // Make a new comdat for the function. Use the "no duplicates" selection kind
94 // for non-weak symbols if the object file format supports it.
95 Comdat *C = M->getOrInsertComdat(Name);
96 if (T.isOSBinFormatCOFF() && !F.isWeakForLinker())
97 C->setSelectionKind(Comdat::NoDuplicates);
98 F.setComdat(C);
99 return C;
102 /// initializeInstrumentation - Initialize all passes in the TransformUtils
103 /// library.
104 void llvm::initializeInstrumentation(PassRegistry &Registry) {
105 initializeAddressSanitizerLegacyPassPass(Registry);
106 initializeModuleAddressSanitizerLegacyPassPass(Registry);
107 initializeBoundsCheckingLegacyPassPass(Registry);
108 initializeControlHeightReductionLegacyPassPass(Registry);
109 initializeGCOVProfilerLegacyPassPass(Registry);
110 initializePGOInstrumentationGenLegacyPassPass(Registry);
111 initializePGOInstrumentationUseLegacyPassPass(Registry);
112 initializePGOIndirectCallPromotionLegacyPassPass(Registry);
113 initializePGOMemOPSizeOptLegacyPassPass(Registry);
114 initializeInstrOrderFileLegacyPassPass(Registry);
115 initializeInstrProfilingLegacyPassPass(Registry);
116 initializeMemorySanitizerLegacyPassPass(Registry);
117 initializeHWAddressSanitizerLegacyPassPass(Registry);
118 initializeThreadSanitizerLegacyPassPass(Registry);
119 initializeModuleSanitizerCoverageLegacyPassPass(Registry);
120 initializeDataFlowSanitizerPass(Registry);
123 /// LLVMInitializeInstrumentation - C binding for
124 /// initializeInstrumentation.
125 void LLVMInitializeInstrumentation(LLVMPassRegistryRef R) {
126 initializeInstrumentation(*unwrap(R));