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"
20 #include "clang/Frontend/CodeGenOptions.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:
46   CodeGenPGO(CodeGenModule &CGM)
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   /// Indicate that the current region is never reached, and thus should have a
64   /// counter value of zero. This is important so that subsequent regions can
65   /// correctly track their parent counts.
66   void setCurrentRegionUnreachable() { setCurrentRegionCount(0); }
67 
68   /// Check if an execution count is known for a given statement. If so, return
69   /// true and put the value in Count; else return false.
70   Optional<uint64_t> getStmtCount(const Stmt *S) {
71     if (!StmtCountMap)
72       return None;
73     auto I = StmtCountMap->find(S);
74     if (I == StmtCountMap->end())
75       return None;
76     return I->second;
77   }
78 
79   /// If the execution count for the current statement is known, record that
80   /// as the current count.
81   void setCurrentStmt(const Stmt *S) {
82     if (auto Count = getStmtCount(S))
83       setCurrentRegionCount(*Count);
84   }
85 
86   /// Calculate branch weights appropriate for PGO data
87   llvm::MDNode *createBranchWeights(uint64_t TrueCount, uint64_t FalseCount);
88   llvm::MDNode *createBranchWeights(ArrayRef<uint64_t> Weights);
89   llvm::MDNode *createLoopWeights(const Stmt *Cond, uint64_t LoopCount);
90 
91   /// Check if we need to emit coverage mapping for a given declaration
92   void checkGlobalDecl(GlobalDecl GD);
93   /// Assign counters to regions and configure them for PGO of a given
94   /// function. Does nothing if instrumentation is not enabled and either
95   /// generates global variables or associates PGO data with each of the
96   /// counters depending on whether we are generating or using instrumentation.
97   void assignRegionCounters(const Decl *D, llvm::Function *Fn);
98   /// Emit a coverage mapping range with a counter zero
99   /// for an unused declaration.
100   void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
101                                llvm::GlobalValue::LinkageTypes Linkage);
102 private:
103   void setFuncName(llvm::Function *Fn);
104   void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
105   void createFuncNameVar(llvm::GlobalValue::LinkageTypes Linkage);
106   void mapRegionCounters(const Decl *D);
107   void computeRegionCounts(const Decl *D);
108   void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
109                                llvm::Function *Fn);
110   void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
111                         bool IsInMainFile);
112   void emitCounterVariables();
113   void emitCounterRegionMapping(const Decl *D);
114 
115 public:
116   void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S);
117 
118   /// Return the region count for the counter at the given index.
119   uint64_t getRegionCount(const Stmt *S) {
120     if (!RegionCounterMap)
121       return 0;
122     if (!haveRegionCounts())
123       return 0;
124     return RegionCounts[(*RegionCounterMap)[S]];
125   }
126 };
127 
128 }  // end namespace CodeGen
129 }  // end namespace clang
130 
131 #endif
132