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 CLANG_CODEGEN_CODEGENPGO_H 15 #define CLANG_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 class RegionCounter; 28 29 /// Per-function PGO state. This class should generally not be used directly, 30 /// but instead through the CodeGenFunction and RegionCounter types. 31 class CodeGenPGO { 32 private: 33 CodeGenModule &CGM; 34 std::unique_ptr<std::string> PrefixedFuncName; 35 StringRef RawFuncName; 36 llvm::GlobalValue::LinkageTypes VarLinkage; 37 38 unsigned NumRegionCounters; 39 uint64_t FunctionHash; 40 llvm::GlobalVariable *RegionCounters; 41 std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap; 42 std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap; 43 std::unique_ptr<std::vector<uint64_t>> RegionCounts; 44 uint64_t CurrentRegionCount; 45 std::string CoverageMapping; 46 /// \brief A flag that is set to true when this function doesn't need 47 /// to have coverage mapping data. 48 bool SkipCoverageMapping; 49 50 public: 51 CodeGenPGO(CodeGenModule &CGM) 52 : CGM(CGM), NumRegionCounters(0), FunctionHash(0), 53 RegionCounters(nullptr), CurrentRegionCount(0), 54 SkipCoverageMapping(false) {} 55 56 /// Whether or not we have PGO region data for the current function. This is 57 /// false both when we have no data at all and when our data has been 58 /// discarded. 59 bool haveRegionCounts() const { return RegionCounts != nullptr; } 60 61 /// Get the string used to identify this function in the profile data. 62 /// For functions with local linkage, this includes the main file name. 63 StringRef getFuncName() const { return StringRef(*PrefixedFuncName); } 64 std::string getFuncVarName(StringRef VarName) const { 65 return ("__llvm_profile_" + VarName + "_" + RawFuncName).str(); 66 } 67 68 /// Return the counter value of the current region. 69 uint64_t getCurrentRegionCount() const { return CurrentRegionCount; } 70 71 /// Set the counter value for the current region. This is used to keep track 72 /// of changes to the most recent counter from control flow and non-local 73 /// exits. 74 void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; } 75 76 /// Indicate that the current region is never reached, and thus should have a 77 /// counter value of zero. This is important so that subsequent regions can 78 /// correctly track their parent counts. 79 void setCurrentRegionUnreachable() { setCurrentRegionCount(0); } 80 81 /// Check if an execution count is known for a given statement. If so, return 82 /// true and put the value in Count; else return false. 83 bool getStmtCount(const Stmt *S, uint64_t &Count) { 84 if (!StmtCountMap) 85 return false; 86 llvm::DenseMap<const Stmt*, uint64_t>::const_iterator 87 I = StmtCountMap->find(S); 88 if (I == StmtCountMap->end()) 89 return false; 90 Count = I->second; 91 return true; 92 } 93 94 /// If the execution count for the current statement is known, record that 95 /// as the current count. 96 void setCurrentStmt(const Stmt *S) { 97 uint64_t Count; 98 if (getStmtCount(S, Count)) 99 setCurrentRegionCount(Count); 100 } 101 102 /// Calculate branch weights appropriate for PGO data 103 llvm::MDNode *createBranchWeights(uint64_t TrueCount, uint64_t FalseCount); 104 llvm::MDNode *createBranchWeights(ArrayRef<uint64_t> Weights); 105 llvm::MDNode *createLoopWeights(const Stmt *Cond, RegionCounter &Cnt); 106 107 /// Check if we need to emit coverage mapping for a given declaration 108 void checkGlobalDecl(GlobalDecl GD); 109 /// Assign counters to regions and configure them for PGO of a given 110 /// function. Does nothing if instrumentation is not enabled and either 111 /// generates global variables or associates PGO data with each of the 112 /// counters depending on whether we are generating or using instrumentation. 113 void assignRegionCounters(const Decl *D, llvm::Function *Fn); 114 /// Emit static data structures for instrumentation data. 115 void emitInstrumentationData(); 116 /// Clean up region counter state. Must be called if assignRegionCounters is 117 /// used. 118 void destroyRegionCounters(); 119 /// Emit static initialization code, if any. 120 static llvm::Function *emitInitialization(CodeGenModule &CGM); 121 /// Emit a coverage mapping range with a counter zero 122 /// for an unused declaration. 123 void emitEmptyCounterMapping(const Decl *D, StringRef FuncName, 124 llvm::GlobalValue::LinkageTypes Linkage); 125 private: 126 void setFuncName(llvm::Function *Fn); 127 void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage); 128 void setVarLinkage(llvm::GlobalValue::LinkageTypes Linkage); 129 void mapRegionCounters(const Decl *D); 130 void computeRegionCounts(const Decl *D); 131 void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader, 132 llvm::Function *Fn); 133 void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader, 134 bool IsInMainFile); 135 void emitCounterVariables(); 136 llvm::GlobalVariable *buildDataVar(); 137 void emitCounterRegionMapping(const Decl *D); 138 139 /// Emit code to increment the counter at the given index 140 void emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter); 141 142 /// Return the region counter for the given statement. This should only be 143 /// called on statements that have a dedicated counter. 144 unsigned getRegionCounter(const Stmt *S) { 145 if (!RegionCounterMap) 146 return 0; 147 return (*RegionCounterMap)[S]; 148 } 149 150 /// Return the region count for the counter at the given index. 151 uint64_t getRegionCount(unsigned Counter) { 152 if (!haveRegionCounts()) 153 return 0; 154 return (*RegionCounts)[Counter]; 155 } 156 157 friend class RegionCounter; 158 }; 159 160 /// A counter for a particular region. This is the primary interface through 161 /// which clients manage PGO counters and their values. 162 class RegionCounter { 163 CodeGenPGO *PGO; 164 unsigned Counter; 165 uint64_t Count; 166 uint64_t ParentCount; 167 uint64_t RegionCount; 168 int64_t Adjust; 169 170 RegionCounter(CodeGenPGO &PGO, unsigned CounterIndex) 171 : PGO(&PGO), Counter(CounterIndex), Count(PGO.getRegionCount(Counter)), 172 ParentCount(PGO.getCurrentRegionCount()), Adjust(0) {} 173 174 public: 175 RegionCounter(CodeGenPGO &PGO, const Stmt *S) 176 : PGO(&PGO), Counter(PGO.getRegionCounter(S)), 177 Count(PGO.getRegionCount(Counter)), 178 ParentCount(PGO.getCurrentRegionCount()), Adjust(0) {} 179 180 /// Get the value of the counter. In most cases this is the number of times 181 /// the region of the counter was entered, but for switch labels it's the 182 /// number of direct jumps to that label. 183 uint64_t getCount() const { return Count; } 184 185 /// Get the value of the counter with adjustments applied. Adjustments occur 186 /// when control enters or leaves the region abnormally; i.e., if there is a 187 /// jump to a label within the region, or if the function can return from 188 /// within the region. The adjusted count, then, is the value of the counter 189 /// at the end of the region. 190 uint64_t getAdjustedCount() const { 191 return Count + Adjust; 192 } 193 194 /// Get the value of the counter in this region's parent, i.e., the region 195 /// that was active when this region began. This is useful for deriving 196 /// counts in implicitly counted regions, like the false case of a condition 197 /// or the normal exits of a loop. 198 uint64_t getParentCount() const { return ParentCount; } 199 200 /// Activate the counter by emitting an increment and starting to track 201 /// adjustments. If AddIncomingFallThrough is true, the current region count 202 /// will be added to the counter for the purposes of tracking the region. 203 void beginRegion(CGBuilderTy &Builder, bool AddIncomingFallThrough=false) { 204 beginRegion(AddIncomingFallThrough); 205 PGO->emitCounterIncrement(Builder, Counter); 206 } 207 void beginRegion(bool AddIncomingFallThrough=false) { 208 RegionCount = Count; 209 if (AddIncomingFallThrough) 210 RegionCount += PGO->getCurrentRegionCount(); 211 PGO->setCurrentRegionCount(RegionCount); 212 } 213 214 /// For counters on boolean branches, begins tracking adjustments for the 215 /// uncounted path. 216 void beginElseRegion() { 217 RegionCount = ParentCount - Count; 218 PGO->setCurrentRegionCount(RegionCount); 219 } 220 221 /// Reset the current region count. 222 void setCurrentRegionCount(uint64_t CurrentCount) { 223 RegionCount = CurrentCount; 224 PGO->setCurrentRegionCount(RegionCount); 225 } 226 227 /// Adjust for non-local control flow after emitting a subexpression or 228 /// substatement. This must be called to account for constructs such as gotos, 229 /// labels, and returns, so that we can ensure that our region's count is 230 /// correct in the code that follows. 231 void adjustForControlFlow() { 232 Adjust += PGO->getCurrentRegionCount() - RegionCount; 233 // Reset the region count in case this is called again later. 234 RegionCount = PGO->getCurrentRegionCount(); 235 } 236 237 /// Commit all adjustments to the current region. If the region is a loop, 238 /// the LoopAdjust value should be the count of all the breaks and continues 239 /// from the loop, to compensate for those counts being deducted from the 240 /// adjustments for the body of the loop. 241 void applyAdjustmentsToRegion(uint64_t LoopAdjust) { 242 PGO->setCurrentRegionCount(ParentCount + Adjust + LoopAdjust); 243 } 244 }; 245 246 } // end namespace CodeGen 247 } // end namespace clang 248 249 #endif 250