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 "clang/Frontend/CodeGenOptions.h" 21 #include "llvm/ProfileData/InstrProfReader.h" 22 #include <array> 23 #include <memory> 24 25 namespace clang { 26 namespace CodeGen { 27 28 /// Per-function PGO state. 29 class CodeGenPGO { 30 private: 31 CodeGenModule &CGM; 32 std::string FuncName; 33 llvm::GlobalVariable *FuncNameVar; 34 35 std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites; 36 unsigned NumRegionCounters; 37 uint64_t FunctionHash; 38 std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap; 39 std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap; 40 std::unique_ptr<llvm::InstrProfRecord> ProfRecord; 41 std::vector<uint64_t> RegionCounts; 42 uint64_t CurrentRegionCount; 43 /// \brief A flag that is set to true when this function doesn't need 44 /// to have coverage mapping data. 45 bool SkipCoverageMapping; 46 47 public: 48 CodeGenPGO(CodeGenModule &CGM) 49 : CGM(CGM), NumValueSites({{0}}), NumRegionCounters(0), 50 FunctionHash(0), CurrentRegionCount(0), SkipCoverageMapping(false) {} 51 52 /// Whether or not we have PGO region data for the current function. This is 53 /// false both when we have no data at all and when our data has been 54 /// discarded. 55 bool haveRegionCounts() const { return !RegionCounts.empty(); } 56 57 /// Return the counter value of the current region. 58 uint64_t getCurrentRegionCount() const { return CurrentRegionCount; } 59 60 /// Set the counter value for the current region. This is used to keep track 61 /// of changes to the most recent counter from control flow and non-local 62 /// exits. 63 void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; } 64 65 /// Check if an execution count is known for a given statement. If so, return 66 /// true and put the value in Count; else return false. 67 Optional<uint64_t> getStmtCount(const Stmt *S) { 68 if (!StmtCountMap) 69 return None; 70 auto I = StmtCountMap->find(S); 71 if (I == StmtCountMap->end()) 72 return None; 73 return I->second; 74 } 75 76 /// If the execution count for the current statement is known, record that 77 /// as the current count. 78 void setCurrentStmt(const Stmt *S) { 79 if (auto Count = getStmtCount(S)) 80 setCurrentRegionCount(*Count); 81 } 82 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(GlobalDecl GD, 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); 92 // Insert instrumentation or attach profile metadata at value sites 93 void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind, 94 llvm::Instruction *ValueSite, llvm::Value *ValuePtr); 95 private: 96 void setFuncName(llvm::Function *Fn); 97 void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage); 98 void mapRegionCounters(const Decl *D); 99 void computeRegionCounts(const Decl *D); 100 void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader, 101 llvm::Function *Fn); 102 void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader, 103 bool IsInMainFile); 104 bool skipRegionMappingForDecl(const Decl *D); 105 void emitCounterRegionMapping(const Decl *D); 106 107 public: 108 void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S); 109 110 /// Return the region count for the counter at the given index. 111 uint64_t getRegionCount(const Stmt *S) { 112 if (!RegionCounterMap) 113 return 0; 114 if (!haveRegionCounts()) 115 return 0; 116 return RegionCounts[(*RegionCounterMap)[S]]; 117 } 118 }; 119 120 } // end namespace CodeGen 121 } // end namespace clang 122 123 #endif 124