LLVM  8.0.1
Instrumentation.cpp
Go to the documentation of this file.
1 //===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the common initialization infrastructure for the
11 // Instrumentation library.
12 //
13 //===----------------------------------------------------------------------===//
14 
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"
22 
23 using namespace llvm;
24 
25 /// Moves I before IP. Returns new insert point.
27  // If I is IP, move the insert point down.
28  if (I == IP)
29  return ++IP;
30  // Otherwise, move I before IP and return IP.
31  I->moveBefore(&*IP);
32  return IP;
33 }
34 
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
39 /// block.
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())
47  KeepInEntry = true;
48  } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
49  if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
50  KeepInEntry = true;
51  }
52  if (KeepInEntry)
53  IP = moveBeforeInsertPoint(I, IP);
54  }
55  return IP;
56 }
57 
58 // Create a constant for Str so that we can pass it to the run-time lib.
60  bool AllowMerging,
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.
65  GlobalVariable *GV =
66  new GlobalVariable(M, StrConst->getType(), true,
67  GlobalValue::PrivateLinkage, StrConst, NamePrefix);
68  if (AllowMerging)
70  GV->setAlignment(1); // Strings may not be merged w/o setting align 1.
71  return GV;
72 }
73 
75  const std::string &ModuleId) {
76  if (auto Comdat = F.getComdat()) return Comdat;
77  assert(F.hasName());
78  Module *M = F.getParent();
79  std::string Name = F.getName();
80 
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
85  // merged.
86  if (T.isOSBinFormatELF() && F.hasLocalLinkage()) {
87  if (ModuleId.empty())
88  return nullptr;
89  Name += ModuleId;
90  }
91 
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())
97  F.setComdat(C);
98  return C;
99 }
100 
101 /// initializeInstrumentation - Initialize all passes in the TransformUtils
102 /// library.
120 }
121 
122 /// LLVMInitializeInstrumentation - C binding for
123 /// initializeInstrumentation.
126 }
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:2567
bool hasLocalLinkage() const
Definition: GlobalValue.h:436
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:604
void initializeDataFlowSanitizerPass(PassRegistry &)
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:45
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:57
struct LLVMOpaquePassRegistry * LLVMPassRegistryRef
Definition: Types.h:131
F(f)
void initializeMemorySanitizerLegacyPassPass(PassRegistry &)
void initializeInstrProfilingLegacyPassPass(PassRegistry &)
void initializeHWAddressSanitizerPass(PassRegistry &)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:195
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
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:245
void initializeEfficiencySanitizerPass(PassRegistry &)
void setComdat(Comdat *C)
Definition: GlobalObject.h:103
void initializePGOInstrumentationGenLegacyPassPass(PassRegistry &)
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
Definition: GlobalValue.h:370
const BasicBlock & getEntryBlock() const
Definition: Function.h:640
No other Module may specify this COMDAT.
Definition: Comdat.h:38
bool hasName() const
Definition: Value.h:251
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:609
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:42
void initializeAddressSanitizerModulePass(PassRegistry &)
void initializeThreadSanitizerLegacyPassPass(PassRegistry &)
void initializeInstrumentation(PassRegistry &)
Initialize all passes linked into the Instrumentation library.
void initializePGOInstrumentationUseLegacyPassPass(PassRegistry &)
void initializeControlHeightReductionLegacyPassPass(PassRegistry &)
void initializeAddressSanitizerPass(PassRegistry &)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
void setSelectionKind(SelectionKind Val)
Definition: Comdat.h:46
iterator end()
Definition: BasicBlock.h:271
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:90
const Comdat * getComdat() const
Definition: GlobalObject.h:101
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:216
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:107
#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:566
void initializeSanitizerCoverageModulePass(PassRegistry &)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:39