LLVM  8.0.0svn
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/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  return ++IP;
29  // Otherwise, move I before IP and return IP.
30  I->moveBefore(&*IP);
31  return IP;
32 }
33 
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
38 /// block.
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())
46  KeepInEntry = true;
47  } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
48  if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
49  KeepInEntry = true;
50  }
51  if (KeepInEntry)
52  IP = moveBeforeInsertPoint(I, IP);
53  }
54  return IP;
55 }
56 
57 // Create a constant for Str so that we can pass it to the run-time lib.
59  bool AllowMerging,
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.
64  GlobalVariable *GV =
65  new GlobalVariable(M, StrConst->getType(), true,
66  GlobalValue::PrivateLinkage, StrConst, NamePrefix);
67  if (AllowMerging)
69  GV->setAlignment(1); // Strings may not be merged w/o setting align 1.
70  return GV;
71 }
72 
74  const std::string &ModuleId) {
75  if (auto Comdat = F.getComdat()) return Comdat;
76  assert(F.hasName());
77  Module *M = F.getParent();
78  std::string Name = F.getName();
79  if (F.hasLocalLinkage()) {
80  if (ModuleId.empty())
81  return nullptr;
82  Name += ModuleId;
83  }
84  F.setComdat(M->getOrInsertComdat(Name));
85  return F.getComdat();
86 }
87 
88 /// initializeInstrumentation - Initialize all passes in the TransformUtils
89 /// library.
107 }
108 
109 /// LLVMInitializeInstrumentation - C binding for
110 /// initializeInstrumentation.
113 }
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:2515
bool hasLocalLinkage() const
Definition: GlobalValue.h:436
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
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 initializeAddressSanitizerModuleLegacyPassPass(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:243
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 &)
const BasicBlock & getEntryBlock() const
Definition: Function.h:640
bool hasName() const
Definition: Value.h:251
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:42
void initializeInstrumentation(PassRegistry &)
Initialize all passes linked into the Instrumentation library.
void initializePGOInstrumentationUseLegacyPassPass(PassRegistry &)
void initializeControlHeightReductionLegacyPassPass(PassRegistry &)
Iterator for intrusive lists based on ilist_node.
iterator end()
Definition: BasicBlock.h:265
Module.h This file contains the declarations for the Module class.
void initializeGCOVProfilerLegacyPassPass(PassRegistry &)
void initializeMemorySanitizerPass(PassRegistry &)
void initializePGOIndirectCallPromotionLegacyPassPass(PassRegistry &)
void initializeThreadSanitizerPass(PassRegistry &)
GlobalVariable * createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, const char *NamePrefix="")
Comdat * GetOrCreateFunctionComdat(Function &F, const std::string &ModuleId)
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.
void initializeAddressSanitizerLegacyPassPass(PassRegistry &)
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:224
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