clang  3.8.0
CodeGenPGO.h
Go to the documentation of this file.
1 //===--- CodeGenPGO.h - PGO Instrumentation for LLVM CodeGen ----*- C++ -*-===//
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 // Instrumentation-based profile-guided optimization
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
15 #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
16 
17 #include "CGBuilder.h"
18 #include "CodeGenModule.h"
19 #include "CodeGenTypes.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include <memory>
24 
25 namespace clang {
26 namespace CodeGen {
27 
28 /// Per-function PGO state.
29 class CodeGenPGO {
30 private:
31  CodeGenModule &CGM;
32  std::string FuncName;
33  llvm::GlobalVariable *FuncNameVar;
34 
35  unsigned NumRegionCounters;
36  uint64_t FunctionHash;
37  std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
38  std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
39  std::vector<uint64_t> RegionCounts;
40  uint64_t CurrentRegionCount;
41  /// \brief A flag that is set to true when this function doesn't need
42  /// to have coverage mapping data.
43  bool SkipCoverageMapping;
44 
45 public:
47  : CGM(CGM), NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0),
48  SkipCoverageMapping(false) {}
49 
50  /// Whether or not we have PGO region data for the current function. This is
51  /// false both when we have no data at all and when our data has been
52  /// discarded.
53  bool haveRegionCounts() const { return !RegionCounts.empty(); }
54 
55  /// Return the counter value of the current region.
56  uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
57 
58  /// Set the counter value for the current region. This is used to keep track
59  /// of changes to the most recent counter from control flow and non-local
60  /// exits.
61  void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
62 
63  /// Check if an execution count is known for a given statement. If so, return
64  /// true and put the value in Count; else return false.
66  if (!StmtCountMap)
67  return None;
68  auto I = StmtCountMap->find(S);
69  if (I == StmtCountMap->end())
70  return None;
71  return I->second;
72  }
73 
74  /// If the execution count for the current statement is known, record that
75  /// as the current count.
76  void setCurrentStmt(const Stmt *S) {
77  if (auto Count = getStmtCount(S))
79  }
80 
81  /// Assign counters to regions and configure them for PGO of a given
82  /// function. Does nothing if instrumentation is not enabled and either
83  /// generates global variables or associates PGO data with each of the
84  /// counters depending on whether we are generating or using instrumentation.
85  void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
86  /// Emit a coverage mapping range with a counter zero
87  /// for an unused declaration.
88  void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
89  llvm::GlobalValue::LinkageTypes Linkage);
90 private:
91  void setFuncName(llvm::Function *Fn);
92  void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
93  void mapRegionCounters(const Decl *D);
94  void computeRegionCounts(const Decl *D);
95  void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
96  llvm::Function *Fn);
97  void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
98  bool IsInMainFile);
99  void emitCounterRegionMapping(const Decl *D);
100 
101 public:
103 
104  /// Return the region count for the counter at the given index.
105  uint64_t getRegionCount(const Stmt *S) {
106  if (!RegionCounterMap)
107  return 0;
108  if (!haveRegionCounts())
109  return 0;
110  return RegionCounts[(*RegionCounterMap)[S]];
111  }
112 };
113 
114 } // end namespace CodeGen
115 } // end namespace clang
116 
117 #endif
Optional< uint64_t > getStmtCount(const Stmt *S)
Check if an execution count is known for a given statement.
Definition: CodeGenPGO.h:65
void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S)
Definition: CodeGenPGO.cpp:728
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:77
uint64_t getCurrentRegionCount() const
Return the counter value of the current region.
Definition: CodeGenPGO.h:56
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:25
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4381
void setCurrentRegionCount(uint64_t Count)
Set the counter value for the current region.
Definition: CodeGenPGO.h:61
void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn)
Assign counters to regions and configure them for PGO of a given function.
Definition: CodeGenPGO.cpp:608
detail::InMemoryDirectory::const_iterator I
bool haveRegionCounts() const
Whether or not we have PGO region data for the current function.
Definition: CodeGenPGO.h:53
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:28
#define false
Definition: stdbool.h:33
Per-function PGO state.
Definition: CodeGenPGO.h:29
This class organizes the cross-function state that is used while generating LLVM code.
void emitEmptyCounterMapping(const Decl *D, StringRef FuncName, llvm::GlobalValue::LinkageTypes Linkage)
Emit a coverage mapping range with a counter zero for an unused declaration.
Definition: CodeGenPGO.cpp:680
void setCurrentStmt(const Stmt *S)
If the execution count for the current statement is known, record that as the current count...
Definition: CodeGenPGO.h:76
BoundNodesTreeBuilder *const Builder
uint64_t getRegionCount(const Stmt *S)
Return the region count for the counter at the given index.
Definition: CodeGenPGO.h:105
CodeGenPGO(CodeGenModule &CGM)
Definition: CodeGenPGO.h:46