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