LLVM  10.0.0svn
Instrumentation.cpp
Go to the documentation of this file.
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
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"
21 
22 using namespace llvm;
23 
24 /// Moves I before IP. Returns new insert point.
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);
32  }
33  return IP;
34 }
35 
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.
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;
52  }
53  if (KeepInEntry)
54  IP = moveBeforeInsertPoint(I, IP);
55  }
56  return IP;
57 }
58 
59 // Create a constant for Str so that we can pass it to the run-time lib.
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)
71  GV->setAlignment(Align::None()); // Strings may not be merged w/o setting
72  // alignment explicitly.
73  return GV;
74 }
75 
77  const std::string &ModuleId) {
78  if (auto Comdat = F.getComdat()) return Comdat;
79  assert(F.hasName());
80  Module *M = F.getParent();
81  std::string Name = F.getName();
82 
83  // Make a unique comdat name for internal linkage things on ELF. On COFF, the
84  // name of the comdat group identifies the leader symbol of the comdat group.
85  // The linkage of the leader symbol is considered during comdat resolution,
86  // and internal symbols with the same name from different objects will not be
87  // merged.
88  if (T.isOSBinFormatELF() && F.hasLocalLinkage()) {
89  if (ModuleId.empty())
90  return nullptr;
91  Name += ModuleId;
92  }
93 
94  // Make a new comdat for the function. Use the "no duplicates" selection kind
95  // for non-weak symbols if the object file format supports it.
96  Comdat *C = M->getOrInsertComdat(Name);
97  if (T.isOSBinFormatCOFF() && !F.isWeakForLinker())
99  F.setComdat(C);
100  return C;
101 }
102 
103 /// initializeInstrumentation - Initialize all passes in the TransformUtils
104 /// library.
122 }
123 
124 /// LLVMInitializeInstrumentation - C binding for
125 /// initializeInstrumentation.
128 }
uint64_t CallInst * C
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2579
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:623
void initializeDataFlowSanitizerPass(PassRegistry &)
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:56
struct LLVMOpaquePassRegistry * LLVMPassRegistryRef
Definition: Types.h:131
F(f)
void initializeMemorySanitizerLegacyPassPass(PassRegistry &)
void initializeInstrProfilingLegacyPassPass(PassRegistry &)
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:204
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:245
static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP)
Moves I before IP. Returns new insert point.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
void initializeInstrOrderFileLegacyPassPass(PassRegistry &)
void setComdat(Comdat *C)
Definition: GlobalObject.h:122
void initializePGOInstrumentationGenLegacyPassPass(PassRegistry &)
void initializeHWAddressSanitizerLegacyPassPass(PassRegistry &)
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
Definition: GlobalValue.h:379
const BasicBlock & getEntryBlock() const
Definition: Function.h:669
void initializeModuleAddressSanitizerLegacyPassPass(PassRegistry &)
No other Module may specify this COMDAT.
Definition: Comdat.h:37
bool hasName() const
Definition: Value.h:252
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:628
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
void initializeThreadSanitizerLegacyPassPass(PassRegistry &)
void initializeInstrumentation(PassRegistry &)
Initialize all passes linked into the Instrumentation library.
void initializePGOInstrumentationUseLegacyPassPass(PassRegistry &)
void initializeControlHeightReductionLegacyPassPass(PassRegistry &)
static constexpr const Align None()
Returns a default constructed Align which corresponds to no alignment.
Definition: Alignment.h:93
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
void setSelectionKind(SelectionKind Val)
Definition: Comdat.h:45
iterator end()
Definition: BasicBlock.h:275
Module.h This file contains the declarations for the Module class.
void initializeGCOVProfilerLegacyPassPass(PassRegistry &)
void initializePGOIndirectCallPromotionLegacyPassPass(PassRegistry &)
GlobalVariable * createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, const char *NamePrefix="")
BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
void LLVMInitializeInstrumentation(LLVMPassRegistryRef R)
LLVMInitializeInstrumentation - C binding for initializeInstrumentation.
Comdat * GetOrCreateFunctionComdat(Function &F, Triple &T, const std::string &ModuleId)
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:89
void initializeModuleSanitizerCoverageLegacyPassPass(PassRegistry &)
void initializeAddressSanitizerLegacyPassPass(PassRegistry &)
const Comdat * getComdat() const
Definition: GlobalObject.h:120
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:219
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
#define I(x, y, z)
Definition: MD5.cpp:58
void initializeBoundsCheckingLegacyPassPass(PassRegistry &)
void initializePGOMemOPSizeOptLegacyPassPass(PassRegistry &)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38