LLVM  3.7.0
InstrProfiling.cpp
Go to the documentation of this file.
1 //===-- InstrProfiling.cpp - Frontend instrumentation based profiling -----===//
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 pass lowers instrprof_increment intrinsics emitted by a frontend for
11 // profiling. It also builds the data structures and initialization code needed
12 // for updating execution counts and emitting the profile at runtime.
13 //
14 //===----------------------------------------------------------------------===//
15 
17 
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/IntrinsicInst.h"
21 #include "llvm/IR/Module.h"
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "instrprof"
27 
28 namespace {
29 
30 class InstrProfiling : public ModulePass {
31 public:
32  static char ID;
33 
34  InstrProfiling() : ModulePass(ID) {}
35 
36  InstrProfiling(const InstrProfOptions &Options)
37  : ModulePass(ID), Options(Options) {}
38 
39  const char *getPassName() const override {
40  return "Frontend instrumentation-based coverage lowering";
41  }
42 
43  bool runOnModule(Module &M) override;
44 
45  void getAnalysisUsage(AnalysisUsage &AU) const override {
46  AU.setPreservesCFG();
47  }
48 
49 private:
50  InstrProfOptions Options;
51  Module *M;
53  std::vector<Value *> UsedVars;
54 
55  bool isMachO() const {
56  return Triple(M->getTargetTriple()).isOSBinFormatMachO();
57  }
58 
59  /// Get the section name for the counter variables.
60  StringRef getCountersSection() const {
61  return isMachO() ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts";
62  }
63 
64  /// Get the section name for the name variables.
65  StringRef getNameSection() const {
66  return isMachO() ? "__DATA,__llvm_prf_names" : "__llvm_prf_names";
67  }
68 
69  /// Get the section name for the profile data variables.
70  StringRef getDataSection() const {
71  return isMachO() ? "__DATA,__llvm_prf_data" : "__llvm_prf_data";
72  }
73 
74  /// Get the section name for the coverage mapping data.
75  StringRef getCoverageSection() const {
76  return isMachO() ? "__DATA,__llvm_covmap" : "__llvm_covmap";
77  }
78 
79  /// Replace instrprof_increment with an increment of the appropriate value.
80  void lowerIncrement(InstrProfIncrementInst *Inc);
81 
82  /// Set up the section and uses for coverage data and its references.
83  void lowerCoverageData(GlobalVariable *CoverageData);
84 
85  /// Get the region counters for an increment, creating them if necessary.
86  ///
87  /// If the counter array doesn't yet exist, the profile data variables
88  /// referring to them will also be created.
89  GlobalVariable *getOrCreateRegionCounters(InstrProfIncrementInst *Inc);
90 
91  /// Emit runtime registration functions for each profile data variable.
92  void emitRegistration();
93 
94  /// Emit the necessary plumbing to pull in the runtime initialization.
95  void emitRuntimeHook();
96 
97  /// Add uses of our data variables and runtime hook.
98  void emitUses();
99 
100  /// Create a static initializer for our data, on platforms that need it,
101  /// and for any profile output file that was specified.
102  void emitInitialization();
103 };
104 
105 } // anonymous namespace
106 
107 char InstrProfiling::ID = 0;
108 INITIALIZE_PASS(InstrProfiling, "instrprof",
109  "Frontend instrumentation-based coverage lowering.", false,
110  false)
111 
113  return new InstrProfiling(Options);
114 }
115 
116 bool InstrProfiling::runOnModule(Module &M) {
117  bool MadeChange = false;
118 
119  this->M = &M;
120  RegionCounters.clear();
121  UsedVars.clear();
122 
123  for (Function &F : M)
124  for (BasicBlock &BB : F)
125  for (auto I = BB.begin(), E = BB.end(); I != E;)
126  if (auto *Inc = dyn_cast<InstrProfIncrementInst>(I++)) {
127  lowerIncrement(Inc);
128  MadeChange = true;
129  }
130  if (GlobalVariable *Coverage = M.getNamedGlobal("__llvm_coverage_mapping")) {
131  lowerCoverageData(Coverage);
132  MadeChange = true;
133  }
134  if (!MadeChange)
135  return false;
136 
137  emitRegistration();
138  emitRuntimeHook();
139  emitUses();
140  emitInitialization();
141  return true;
142 }
143 
144 void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) {
145  GlobalVariable *Counters = getOrCreateRegionCounters(Inc);
146 
147  IRBuilder<> Builder(Inc->getParent(), *Inc);
148  uint64_t Index = Inc->getIndex()->getZExtValue();
149  Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters, 0, Index);
150  Value *Count = Builder.CreateLoad(Addr, "pgocount");
151  Count = Builder.CreateAdd(Count, Builder.getInt64(1));
152  Inc->replaceAllUsesWith(Builder.CreateStore(Count, Addr));
153  Inc->eraseFromParent();
154 }
155 
156 void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageData) {
157  CoverageData->setSection(getCoverageSection());
158  CoverageData->setAlignment(8);
159 
160  Constant *Init = CoverageData->getInitializer();
161  // We're expecting { i32, i32, i32, i32, [n x { i8*, i32, i32 }], [m x i8] }
162  // for some C. If not, the frontend's given us something broken.
163  assert(Init->getNumOperands() == 6 && "bad number of fields in coverage map");
164  assert(isa<ConstantArray>(Init->getAggregateElement(4)) &&
165  "invalid function list in coverage map");
166  ConstantArray *Records = cast<ConstantArray>(Init->getAggregateElement(4));
167  for (unsigned I = 0, E = Records->getNumOperands(); I < E; ++I) {
168  Constant *Record = Records->getOperand(I);
169  Value *V = const_cast<Value *>(Record->getOperand(0))->stripPointerCasts();
170 
171  assert(isa<GlobalVariable>(V) && "Missing reference to function name");
172  GlobalVariable *Name = cast<GlobalVariable>(V);
173 
174  // If we have region counters for this name, we've already handled it.
175  auto It = RegionCounters.find(Name);
176  if (It != RegionCounters.end())
177  continue;
178 
179  // Move the name variable to the right section.
180  Name->setSection(getNameSection());
181  Name->setAlignment(1);
182  }
183 }
184 
185 /// Get the name of a profiling variable for a particular function.
187  auto *Arr = cast<ConstantDataArray>(Inc->getName()->getInitializer());
188  StringRef Name = Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
189  return ("__llvm_profile_" + VarName + "_" + Name).str();
190 }
191 
193 InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
194  GlobalVariable *Name = Inc->getName();
195  auto It = RegionCounters.find(Name);
196  if (It != RegionCounters.end())
197  return It->second;
198 
199  // Move the name variable to the right section. Make sure it is placed in the
200  // same comdat as its associated function. Otherwise, we may get multiple
201  // counters for the same function in certain cases.
202  Function *Fn = Inc->getParent()->getParent();
203  Name->setSection(getNameSection());
204  Name->setAlignment(1);
205  Name->setComdat(Fn->getComdat());
206 
207  uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
208  LLVMContext &Ctx = M->getContext();
209  ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
210 
211  // Create the counters variable.
212  auto *Counters = new GlobalVariable(*M, CounterTy, false, Name->getLinkage(),
213  Constant::getNullValue(CounterTy),
214  getVarName(Inc, "counters"));
215  Counters->setVisibility(Name->getVisibility());
216  Counters->setSection(getCountersSection());
217  Counters->setAlignment(8);
218  Counters->setComdat(Fn->getComdat());
219 
220  RegionCounters[Inc->getName()] = Counters;
221 
222  // Create data variable.
223  auto *NameArrayTy = Name->getType()->getPointerElementType();
224  auto *Int32Ty = Type::getInt32Ty(Ctx);
225  auto *Int64Ty = Type::getInt64Ty(Ctx);
226  auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
227  auto *Int64PtrTy = Type::getInt64PtrTy(Ctx);
228 
229  Type *DataTypes[] = {Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int64PtrTy};
230  auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes));
231  Constant *DataVals[] = {
232  ConstantInt::get(Int32Ty, NameArrayTy->getArrayNumElements()),
233  ConstantInt::get(Int32Ty, NumCounters),
234  ConstantInt::get(Int64Ty, Inc->getHash()->getZExtValue()),
235  ConstantExpr::getBitCast(Name, Int8PtrTy),
236  ConstantExpr::getBitCast(Counters, Int64PtrTy)};
237  auto *Data = new GlobalVariable(*M, DataTy, true, Name->getLinkage(),
238  ConstantStruct::get(DataTy, DataVals),
239  getVarName(Inc, "data"));
240  Data->setVisibility(Name->getVisibility());
241  Data->setSection(getDataSection());
242  Data->setAlignment(8);
243  Data->setComdat(Fn->getComdat());
244 
245  // Mark the data variable as used so that it isn't stripped out.
246  UsedVars.push_back(Data);
247 
248  return Counters;
249 }
250 
251 void InstrProfiling::emitRegistration() {
252  // Don't do this for Darwin. compiler-rt uses linker magic.
253  if (Triple(M->getTargetTriple()).isOSDarwin())
254  return;
255 
256  // Construct the function.
257  auto *VoidTy = Type::getVoidTy(M->getContext());
258  auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext());
259  auto *RegisterFTy = FunctionType::get(VoidTy, false);
260  auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage,
261  "__llvm_profile_register_functions", M);
262  RegisterF->setUnnamedAddr(true);
263  if (Options.NoRedZone)
264  RegisterF->addFnAttr(Attribute::NoRedZone);
265 
266  auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false);
267  auto *RuntimeRegisterF =
269  "__llvm_profile_register_function", M);
270 
271  IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF));
272  for (Value *Data : UsedVars)
273  IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy));
274  IRB.CreateRetVoid();
275 }
276 
277 void InstrProfiling::emitRuntimeHook() {
278  const char *const RuntimeVarName = "__llvm_profile_runtime";
279  const char *const RuntimeUserName = "__llvm_profile_runtime_user";
280 
281  // If the module's provided its own runtime, we don't need to do anything.
282  if (M->getGlobalVariable(RuntimeVarName))
283  return;
284 
285  // Declare an external variable that will pull in the runtime initialization.
286  auto *Int32Ty = Type::getInt32Ty(M->getContext());
287  auto *Var =
288  new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
289  nullptr, RuntimeVarName);
290 
291  // Make a function that uses it.
292  auto *User =
293  Function::Create(FunctionType::get(Int32Ty, false),
294  GlobalValue::LinkOnceODRLinkage, RuntimeUserName, M);
295  User->addFnAttr(Attribute::NoInline);
296  if (Options.NoRedZone)
297  User->addFnAttr(Attribute::NoRedZone);
298  User->setVisibility(GlobalValue::HiddenVisibility);
299 
300  IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", User));
301  auto *Load = IRB.CreateLoad(Var);
302  IRB.CreateRet(Load);
303 
304  // Mark the user variable as used so that it isn't stripped out.
305  UsedVars.push_back(User);
306 }
307 
308 void InstrProfiling::emitUses() {
309  if (UsedVars.empty())
310  return;
311 
312  GlobalVariable *LLVMUsed = M->getGlobalVariable("llvm.used");
313  std::vector<Constant *> MergedVars;
314  if (LLVMUsed) {
315  // Collect the existing members of llvm.used.
316  ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
317  for (unsigned I = 0, E = Inits->getNumOperands(); I != E; ++I)
318  MergedVars.push_back(Inits->getOperand(I));
319  LLVMUsed->eraseFromParent();
320  }
321 
322  Type *i8PTy = Type::getInt8PtrTy(M->getContext());
323  // Add uses for our data.
324  for (auto *Value : UsedVars)
325  MergedVars.push_back(
326  ConstantExpr::getBitCast(cast<Constant>(Value), i8PTy));
327 
328  // Recreate llvm.used.
329  ArrayType *ATy = ArrayType::get(i8PTy, MergedVars.size());
330  LLVMUsed =
331  new GlobalVariable(*M, ATy, false, GlobalValue::AppendingLinkage,
332  ConstantArray::get(ATy, MergedVars), "llvm.used");
333 
334  LLVMUsed->setSection("llvm.metadata");
335 }
336 
337 void InstrProfiling::emitInitialization() {
338  std::string InstrProfileOutput = Options.InstrProfileOutput;
339 
340  Constant *RegisterF = M->getFunction("__llvm_profile_register_functions");
341  if (!RegisterF && InstrProfileOutput.empty())
342  return;
343 
344  // Create the initialization function.
345  auto *VoidTy = Type::getVoidTy(M->getContext());
346  auto *F =
347  Function::Create(FunctionType::get(VoidTy, false),
348  GlobalValue::InternalLinkage, "__llvm_profile_init", M);
349  F->setUnnamedAddr(true);
350  F->addFnAttr(Attribute::NoInline);
351  if (Options.NoRedZone)
352  F->addFnAttr(Attribute::NoRedZone);
353 
354  // Add the basic block and the necessary calls.
355  IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F));
356  if (RegisterF)
357  IRB.CreateCall(RegisterF, {});
358  if (!InstrProfileOutput.empty()) {
359  auto *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
360  auto *SetNameTy = FunctionType::get(VoidTy, Int8PtrTy, false);
361  auto *SetNameF =
363  "__llvm_profile_override_default_filename", M);
364 
365  // Create variable for profile name.
366  Constant *ProfileNameConst =
367  ConstantDataArray::getString(M->getContext(), InstrProfileOutput, true);
368  GlobalVariable *ProfileName =
369  new GlobalVariable(*M, ProfileNameConst->getType(), true,
370  GlobalValue::PrivateLinkage, ProfileNameConst);
371 
372  IRB.CreateCall(SetNameF, IRB.CreatePointerCast(ProfileName, Int8PtrTy));
373  }
374  IRB.CreateRetVoid();
375 
376  appendToGlobalCtors(*M, F, 0);
377 }
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:145
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
LinkageTypes getLinkage() const
Definition: GlobalValue.h:289
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
getString - This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2612
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:46
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:139
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
unsigned getNumOperands() const
Definition: User.h:138
void appendToGlobalCtors(Module &M, Function *F, int Priority)
Append F to the list of global ctors of module M with the given Priority.
Definition: ModuleUtils.cpp:73
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:48
Externally visible function.
Definition: GlobalValue.h:40
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
F(f)
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:240
void setAlignment(unsigned Align)
Definition: Globals.cpp:77
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:261
ConstantInt * getHash() const
static PointerType * getInt64PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:295
Type * getPointerElementType() const
Definition: Type.h:366
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:178
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:308
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:517
ConstantInt * getIndex() const
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Globals.cpp:194
ConstantInt * getNumCounters() const
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:117
ArrayType - Class to represent array types.
Definition: DerivedTypes.h:336
void setComdat(Comdat *C)
Definition: GlobalObject.h:63
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
FunctionType::get - This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:361
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1835
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:43
GlobalVariable * getName() const
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
This is an important base class in LLVM.
Definition: Constant.h:41
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:873
ModulePass * createInstrProfilingPass(const InstrProfOptions &Options=InstrProfOptions())
Insert frontend instrumentation based profiling.
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:225
Value * getOperand(unsigned i) const
Definition: User.h:118
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:103
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1008
Constant * getAggregateElement(unsigned Elt) const
getAggregateElement - For aggregates (struct/array/vector) return the constant that corresponds to th...
Definition: Constants.cpp:250
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:283
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
StructType::get - This static method is the primary way to create a literal StructType.
Definition: Type.cpp:404
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
INITIALIZE_PASS(InstrProfiling,"instrprof","Frontend instrumentation-based coverage lowering.", false, false) ModulePass *llvm
ConstantArray - Constant Array Declarations.
Definition: Constants.h:356
Options for the frontend instrumentation based profiling pass.
static std::string getVarName(InstrProfIncrementInst *Inc, StringRef VarName)
Get the name of a profiling variable for a particular function.
This represents the llvm.instrprof_increment intrinsic.
Disable redzone.
Definition: Attributes.h:94
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:185
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:239
#define I(x, y, z)
Definition: MD5.cpp:54
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
static ArrayType * get(Type *ElementType, uint64_t NumElements)
ArrayType::get - This static method is the primary way to construct an ArrayType. ...
Definition: Type.cpp:686
Rename collisions when linking (static functions).
Definition: GlobalValue.h:47
aarch64 promote const
LLVM Value Representation.
Definition: Value.h:69
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
Definition: Function.h:121
void setSection(StringRef S)
Definition: Globals.cpp:126
const BasicBlock * getParent() const
Definition: Instruction.h:72