1 //===--- CodeGenPGO.h - PGO Instrumentation for LLVM CodeGen ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Instrumentation-based profile-guided optimization
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
15 #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
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"
28 /// Per-function PGO state.
33 llvm::GlobalVariable *FuncNameVar;
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;
46 CodeGenPGO(CodeGenModule &CGM)
47 : CGM(CGM), NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0),
48 SkipCoverageMapping(false) {}
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
53 bool haveRegionCounts() const { return !RegionCounts.empty(); }
55 /// Return the counter value of the current region.
56 uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
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
61 void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
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.
65 Optional<uint64_t> getStmtCount(const Stmt *S) {
68 auto I = StmtCountMap->find(S);
69 if (I == StmtCountMap->end())
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))
78 setCurrentRegionCount(*Count);
81 /// Check if we need to emit coverage mapping for a given declaration
82 void checkGlobalDecl(GlobalDecl GD);
83 /// Assign counters to regions and configure them for PGO of a given
84 /// function. Does nothing if instrumentation is not enabled and either
85 /// generates global variables or associates PGO data with each of the
86 /// counters depending on whether we are generating or using instrumentation.
87 void assignRegionCounters(const Decl *D, llvm::Function *Fn);
88 /// Emit a coverage mapping range with a counter zero
89 /// for an unused declaration.
90 void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
91 llvm::GlobalValue::LinkageTypes Linkage);
93 void setFuncName(llvm::Function *Fn);
94 void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
95 void createFuncNameVar(llvm::GlobalValue::LinkageTypes Linkage);
96 void mapRegionCounters(const Decl *D);
97 void computeRegionCounts(const Decl *D);
98 void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
100 void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
102 void emitCounterVariables();
103 void emitCounterRegionMapping(const Decl *D);
106 void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S);
108 /// Return the region count for the counter at the given index.
109 uint64_t getRegionCount(const Stmt *S) {
110 if (!RegionCounterMap)
112 if (!haveRegionCounts())
114 return RegionCounts[(*RegionCounterMap)[S]];
118 } // end namespace CodeGen
119 } // end namespace clang