Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 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/OwningPtr.h" |
| 22 | #include "llvm/ADT/StringMap.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | |
| 25 | namespace clang { |
| 26 | namespace CodeGen { |
| 27 | class RegionCounter; |
| 28 | |
| 29 | /// The raw counter data from an instrumented PGO binary |
| 30 | class PGOProfileData { |
| 31 | private: |
| 32 | /// The PGO data |
| 33 | llvm::OwningPtr<llvm::MemoryBuffer> DataBuffer; |
| 34 | /// Offsets into DataBuffer for each function's counters |
| 35 | llvm::StringMap<unsigned> DataOffsets; |
| 36 | CodeGenModule &CGM; |
| 37 | public: |
| 38 | PGOProfileData(CodeGenModule &CGM, std::string Path); |
| 39 | /// Fill Counts with the profile data for the given function name. Returns |
| 40 | /// false on success. |
| 41 | bool getFunctionCounts(StringRef MangledName, std::vector<uint64_t> &Counts); |
| 42 | }; |
| 43 | |
| 44 | /// Per-function PGO state. This class should generally not be used directly, |
| 45 | /// but instead through the CodeGenFunction and RegionCounter types. |
| 46 | class CodeGenPGO { |
| 47 | private: |
| 48 | CodeGenModule &CGM; |
| 49 | |
| 50 | unsigned NumRegionCounters; |
| 51 | llvm::GlobalVariable *RegionCounters; |
| 52 | llvm::DenseMap<const Stmt*, unsigned> *RegionCounterMap; |
| 53 | std::vector<uint64_t> *RegionCounts; |
| 54 | uint64_t CurrentRegionCount; |
| 55 | |
| 56 | public: |
| 57 | CodeGenPGO(CodeGenModule &CGM) |
| 58 | : CGM(CGM), NumRegionCounters(0), RegionCounters(0), RegionCounterMap(0), |
| 59 | RegionCounts(0), CurrentRegionCount(0) {} |
| 60 | ~CodeGenPGO() {} |
| 61 | |
| 62 | /// Whether or not we have PGO region data for the current function. This is |
| 63 | /// false both when we have no data at all and when our data has been |
| 64 | /// discarded. |
| 65 | bool haveRegionCounts() const { return RegionCounts != 0; } |
| 66 | |
| 67 | /// Return the counter value of the current region. |
| 68 | uint64_t getCurrentRegionCount() const { return CurrentRegionCount; } |
| 69 | /// Return the counter value of the current region, or \p Min if it is larger. |
| 70 | uint64_t getCurrentRegionCountWithMin(uint64_t Min) { |
| 71 | return std::max(Min, CurrentRegionCount); |
| 72 | } |
| 73 | /// Set the counter value for the current region. This is used to keep track |
| 74 | /// of changes to the most recent counter from control flow and non-local |
| 75 | /// exits. |
| 76 | void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; } |
| 77 | |
| 78 | /// Calculate branch weights appropriate for PGO data |
| 79 | llvm::MDNode *createBranchWeights(uint64_t TrueCount, uint64_t FalseCount); |
| 80 | llvm::MDNode *createBranchWeights(ArrayRef<uint64_t> Weights); |
| 81 | |
| 82 | /// Assign counters to regions and configure them for PGO of a given |
| 83 | /// function. Does nothing if instrumentation is not enabled and either |
| 84 | /// generates global variables or associates PGO data with each of the |
| 85 | /// counters depending on whether we are generating or using instrumentation. |
| 86 | void assignRegionCounters(GlobalDecl &GD); |
| 87 | /// Emit code to write counts for a given function to disk, if necessary. |
| 88 | void emitWriteoutFunction(GlobalDecl &GD); |
| 89 | /// Clean up region counter state. Must be called if assignRegionCounters is |
| 90 | /// used. |
| 91 | void destroyRegionCounters(); |
| 92 | /// Emit the logic to register region counter write out functions. Returns a |
| 93 | /// function that implements this logic. |
| 94 | static llvm::Function *emitInitialization(CodeGenModule &CGM); |
| 95 | |
| 96 | private: |
| 97 | void mapRegionCounters(const Decl *D); |
| 98 | void loadRegionCounts(GlobalDecl &GD, PGOProfileData *PGOData); |
| 99 | void emitCounterVariables(); |
| 100 | |
| 101 | /// Emit code to increment the counter at the given index |
| 102 | void emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter); |
| 103 | |
| 104 | /// Return the region counter for the given statement. This should only be |
| 105 | /// called on statements that have a dedicated counter. |
| 106 | unsigned getRegionCounter(const Stmt *S) { |
| 107 | if (RegionCounterMap == 0) |
| 108 | return 0; |
| 109 | return (*RegionCounterMap)[S]; |
| 110 | } |
| 111 | |
| 112 | /// Return the region count for the counter at the given index. |
| 113 | uint64_t getRegionCount(unsigned Counter) { |
| 114 | if (!haveRegionCounts()) |
| 115 | return 0; |
| 116 | return (*RegionCounts)[Counter]; |
| 117 | } |
| 118 | |
| 119 | friend class RegionCounter; |
| 120 | }; |
| 121 | |
| 122 | /// A counter for a particular region. This is the primary interface through |
| 123 | /// which clients manage PGO counters and their values. |
| 124 | class RegionCounter { |
| 125 | CodeGenPGO *PGO; |
| 126 | unsigned Counter; |
| 127 | uint64_t Count; |
| 128 | uint64_t ParentCount; |
| 129 | uint64_t RegionCount; |
| 130 | int64_t Adjust; |
| 131 | |
| 132 | RegionCounter(CodeGenPGO &PGO, unsigned CounterIndex) |
| 133 | : PGO(&PGO), Counter(CounterIndex), Count(PGO.getRegionCount(Counter)), |
| 134 | ParentCount(PGO.getCurrentRegionCount()), Adjust(0) {} |
| 135 | |
| 136 | public: |
| 137 | RegionCounter(CodeGenPGO &PGO, const Stmt *S) |
| 138 | : PGO(&PGO), Counter(PGO.getRegionCounter(S)), |
| 139 | Count(PGO.getRegionCount(Counter)), |
| 140 | ParentCount(PGO.getCurrentRegionCount()), Adjust(0) {} |
| 141 | |
| 142 | /// Get the value of the counter. In most cases this is the number of times |
| 143 | /// the region of the counter was entered, but for switch labels it's the |
| 144 | /// number of direct jumps to that label. |
| 145 | uint64_t getCount() const { return Count; } |
| 146 | /// Get the value of the counter with adjustments applied. Adjustments occur |
| 147 | /// when control enters or leaves the region abnormally, ie, if there is a |
| 148 | /// jump to a label within the region, or if the function can return from |
| 149 | /// within the region. The adjusted count, then, is the value of the counter |
| 150 | /// at the end of the region. |
| 151 | uint64_t getAdjustedCount() const { |
Chandler Carruth | acafded | 2014-01-07 06:52:12 +0000 | [diff] [blame^] | 152 | assert((Adjust > 0 || (uint64_t)(-Adjust) <= Count) && "Negative count"); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 153 | return Count + Adjust; |
| 154 | } |
| 155 | /// Get the value of the counter in this region's parent, ie, the region that |
| 156 | /// was active when this region began. This is useful for deriving counts in |
| 157 | /// implicitly counted regions, like the false case of a condition or the |
| 158 | /// normal exits of a loop. |
| 159 | uint64_t getParentCount() const { return ParentCount; } |
| 160 | |
| 161 | /// Get the number of times the condition of a loop will evaluate false. This |
| 162 | /// is the number of times we enter the loop, adjusted by the difference |
| 163 | /// between entering and exiting the loop body normally, excepting that |
| 164 | /// 'continue' statements also bring us back here. |
| 165 | /// |
| 166 | /// Undefined if this counter is not counting a loop. |
| 167 | uint64_t getLoopExitCount() const { |
| 168 | return getParentCount() + getContinueCounter().getCount() + |
| 169 | getAdjustedCount() - getCount(); |
| 170 | } |
| 171 | /// Get the associated break counter. Undefined if this counter is not |
| 172 | /// counting a loop. |
| 173 | RegionCounter getBreakCounter() const { |
| 174 | return RegionCounter(*PGO, Counter + 1); |
| 175 | } |
| 176 | /// Get the associated continue counter. Undefined if this counter is not |
| 177 | /// counting a loop. |
| 178 | RegionCounter getContinueCounter() const { |
| 179 | return RegionCounter(*PGO, Counter + 2); |
| 180 | } |
| 181 | |
| 182 | /// Activate the counter by emitting an increment and starting to track |
| 183 | /// adjustments. If AddIncomingFallThrough is true, the current region count |
| 184 | /// will be added to the counter for the purposes of tracking the region. |
| 185 | void beginRegion(CGBuilderTy &Builder, bool AddIncomingFallThrough=false) { |
| 186 | RegionCount = Count; |
| 187 | if (AddIncomingFallThrough) |
| 188 | RegionCount += PGO->getCurrentRegionCount(); |
| 189 | PGO->setCurrentRegionCount(RegionCount); |
| 190 | PGO->emitCounterIncrement(Builder, Counter); |
| 191 | } |
| 192 | /// For counters on boolean branches, begins tracking adjustments for the |
| 193 | /// uncounted path. |
| 194 | void beginElseRegion() { |
| 195 | RegionCount = ParentCount - Count; |
| 196 | PGO->setCurrentRegionCount(RegionCount); |
| 197 | } |
| 198 | |
| 199 | /// Control may either enter or leave the region, so the count at the end may |
| 200 | /// be different from the start. Call this to track that adjustment without |
| 201 | /// modifying the current count. Must not be called before one of beginRegion |
| 202 | /// or beginElseRegion. |
| 203 | void adjustFallThroughCount() { |
| 204 | Adjust += PGO->getCurrentRegionCount() - RegionCount; |
| 205 | } |
| 206 | /// Commit all adjustments to the current region. This should be called after |
| 207 | /// all blocks that adjust the fallthrough count have been emitted. |
| 208 | void applyAdjustmentsToRegion() { |
| 209 | PGO->setCurrentRegionCount(ParentCount + Adjust); |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | } // end namespace CodeGen |
| 214 | } // end namespace clang |
| 215 | |
| 216 | #endif |