clang  6.0.0svn
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/ProfileData/InstrProfReader.h"
22 #include <array>
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  std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
36  unsigned NumRegionCounters;
37  uint64_t FunctionHash;
38  std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
39  std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
40  std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
41  std::vector<uint64_t> RegionCounts;
42  uint64_t CurrentRegionCount;
43 
44 public:
46  : CGM(CGM), NumValueSites({{0}}), NumRegionCounters(0), FunctionHash(0),
47  CurrentRegionCount(0) {}
48 
49  /// Whether or not we have PGO region data for the current function. This is
50  /// false both when we have no data at all and when our data has been
51  /// discarded.
52  bool haveRegionCounts() const { return !RegionCounts.empty(); }
53 
54  /// Return the counter value of the current region.
55  uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
56 
57  /// Set the counter value for the current region. This is used to keep track
58  /// of changes to the most recent counter from control flow and non-local
59  /// exits.
60  void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
61 
62  /// Check if an execution count is known for a given statement. If so, return
63  /// true and put the value in Count; else return false.
65  if (!StmtCountMap)
66  return None;
67  auto I = StmtCountMap->find(S);
68  if (I == StmtCountMap->end())
69  return None;
70  return I->second;
71  }
72 
73  /// If the execution count for the current statement is known, record that
74  /// as the current count.
75  void setCurrentStmt(const Stmt *S) {
76  if (auto Count = getStmtCount(S))
77  setCurrentRegionCount(*Count);
78  }
79 
80  /// Assign counters to regions and configure them for PGO of a given
81  /// function. Does nothing if instrumentation is not enabled and either
82  /// generates global variables or associates PGO data with each of the
83  /// counters depending on whether we are generating or using instrumentation.
84  void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
85  /// Emit a coverage mapping range with a counter zero
86  /// for an unused declaration.
87  void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
88  llvm::GlobalValue::LinkageTypes Linkage);
89  // Insert instrumentation or attach profile metadata at value sites
90  void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
91  llvm::Instruction *ValueSite, llvm::Value *ValuePtr);
92 private:
93  void setFuncName(llvm::Function *Fn);
94  void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
95  void mapRegionCounters(const Decl *D);
96  void computeRegionCounts(const Decl *D);
97  void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
98  llvm::Function *Fn);
99  void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
100  bool IsInMainFile);
101  bool skipRegionMappingForDecl(const Decl *D);
102  void emitCounterRegionMapping(const Decl *D);
103 
104 public:
105  void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S,
106  llvm::Value *StepV);
107 
108  /// Return the region count for the counter at the given index.
109  uint64_t getRegionCount(const Stmt *S) {
110  if (!RegionCounterMap)
111  return 0;
112  if (!haveRegionCounts())
113  return 0;
114  return RegionCounts[(*RegionCounterMap)[S]];
115  }
116 };
117 
118 } // end namespace CodeGen
119 } // end namespace clang
120 
121 #endif
Optional< uint64_t > getStmtCount(const Stmt *S)
Check if an execution count is known for a given statement.
Definition: CodeGenPGO.h:64
Stmt - This represents one statement.
Definition: Stmt.h:60
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:81
void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S, llvm::Value *StepV)
Definition: CodeGenPGO.cpp:745
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:25
void setCurrentRegionCount(uint64_t Count)
Set the counter value for the current region.
Definition: CodeGenPGO.h:60
void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn)
Assign counters to regions and configure them for PGO of a given function.
Definition: CodeGenPGO.cpp:618
void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind, llvm::Instruction *ValueSite, llvm::Value *ValuePtr)
Definition: CodeGenPGO.cpp:770
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:29
Per-function PGO state.
Definition: CodeGenPGO.h:29
uint64_t getCurrentRegionCount() const
Return the counter value of the current region.
Definition: CodeGenPGO.h:55
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
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:701
void setCurrentStmt(const Stmt *S)
If the execution count for the current statement is known, record that as the current count...
Definition: CodeGenPGO.h:75
uint64_t getRegionCount(const Stmt *S)
Return the region count for the counter at the given index.
Definition: CodeGenPGO.h:109
bool haveRegionCounts() const
Whether or not we have PGO region data for the current function.
Definition: CodeGenPGO.h:52
CodeGenPGO(CodeGenModule &CGM)
Definition: CodeGenPGO.h:45