1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H |
14 | #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H |
15 | |
16 | #include "CGBuilder.h" |
17 | #include "CodeGenModule.h" |
18 | #include "CodeGenTypes.h" |
19 | #include "llvm/ProfileData/InstrProfReader.h" |
20 | #include <array> |
21 | #include <memory> |
22 | |
23 | namespace clang { |
24 | namespace CodeGen { |
25 | |
26 | |
27 | class CodeGenPGO { |
28 | private: |
29 | CodeGenModule &CGM; |
30 | std::string FuncName; |
31 | llvm::GlobalVariable *FuncNameVar; |
32 | |
33 | std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites; |
34 | unsigned NumRegionCounters; |
35 | uint64_t FunctionHash; |
36 | std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap; |
37 | std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap; |
38 | std::unique_ptr<llvm::InstrProfRecord> ProfRecord; |
39 | std::vector<uint64_t> RegionCounts; |
40 | uint64_t CurrentRegionCount; |
41 | |
42 | public: |
43 | CodeGenPGO(CodeGenModule &CGM) |
44 | : CGM(CGM), NumValueSites({{0}}), NumRegionCounters(0), FunctionHash(0), |
45 | CurrentRegionCount(0) {} |
46 | |
47 | |
48 | |
49 | |
50 | bool haveRegionCounts() const { return !RegionCounts.empty(); } |
51 | |
52 | |
53 | uint64_t getCurrentRegionCount() const { return CurrentRegionCount; } |
54 | |
55 | |
56 | |
57 | |
58 | void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; } |
59 | |
60 | |
61 | |
62 | Optional<uint64_t> getStmtCount(const Stmt *S) { |
63 | if (!StmtCountMap) |
64 | return None; |
65 | auto I = StmtCountMap->find(S); |
66 | if (I == StmtCountMap->end()) |
67 | return None; |
68 | return I->second; |
69 | } |
70 | |
71 | |
72 | |
73 | void setCurrentStmt(const Stmt *S) { |
74 | if (auto Count = getStmtCount(S)) |
75 | setCurrentRegionCount(*Count); |
76 | } |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn); |
83 | |
84 | |
85 | void emitEmptyCounterMapping(const Decl *D, StringRef FuncName, |
86 | llvm::GlobalValue::LinkageTypes Linkage); |
87 | |
88 | void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind, |
89 | llvm::Instruction *ValueSite, llvm::Value *ValuePtr); |
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 | bool skipRegionMappingForDecl(const Decl *D); |
100 | void emitCounterRegionMapping(const Decl *D); |
101 | |
102 | public: |
103 | void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S, |
104 | llvm::Value *StepV); |
105 | |
106 | |
107 | uint64_t getRegionCount(const Stmt *S) { |
108 | if (!RegionCounterMap) |
109 | return 0; |
110 | if (!haveRegionCounts()) |
111 | return 0; |
112 | return RegionCounts[(*RegionCounterMap)[S]]; |
113 | } |
114 | }; |
115 | |
116 | } |
117 | } |
118 | |
119 | #endif |
120 | |