clang API Documentation

CodeGenPGO.h
Go to the documentation of this file.
00001 //===--- CodeGenPGO.h - PGO Instrumentation for LLVM CodeGen ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Instrumentation-based profile-guided optimization
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
00015 #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
00016 
00017 #include "CGBuilder.h"
00018 #include "CodeGenModule.h"
00019 #include "CodeGenTypes.h"
00020 #include "clang/Frontend/CodeGenOptions.h"
00021 #include "llvm/ADT/StringMap.h"
00022 #include "llvm/Support/MemoryBuffer.h"
00023 #include <memory>
00024 
00025 namespace clang {
00026 namespace CodeGen {
00027 class RegionCounter;
00028 
00029 /// Per-function PGO state. This class should generally not be used directly,
00030 /// but instead through the CodeGenFunction and RegionCounter types.
00031 class CodeGenPGO {
00032 private:
00033   CodeGenModule &CGM;
00034   std::unique_ptr<std::string> PrefixedFuncName;
00035   StringRef RawFuncName;
00036   llvm::GlobalValue::LinkageTypes VarLinkage;
00037 
00038   unsigned NumRegionCounters;
00039   uint64_t FunctionHash;
00040   llvm::GlobalVariable *RegionCounters;
00041   std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
00042   std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
00043   std::unique_ptr<std::vector<uint64_t>> RegionCounts;
00044   uint64_t CurrentRegionCount;
00045   std::string CoverageMapping;
00046   /// \brief A flag that is set to true when this function doesn't need
00047   /// to have coverage mapping data.
00048   bool SkipCoverageMapping;
00049 
00050 public:
00051   CodeGenPGO(CodeGenModule &CGM)
00052       : CGM(CGM), NumRegionCounters(0), FunctionHash(0),
00053         RegionCounters(nullptr), CurrentRegionCount(0),
00054         SkipCoverageMapping(false) {}
00055 
00056   /// Whether or not we have PGO region data for the current function. This is
00057   /// false both when we have no data at all and when our data has been
00058   /// discarded.
00059   bool haveRegionCounts() const { return RegionCounts != nullptr; }
00060 
00061   /// Get the string used to identify this function in the profile data.
00062   /// For functions with local linkage, this includes the main file name.
00063   StringRef getFuncName() const { return StringRef(*PrefixedFuncName); }
00064   std::string getFuncVarName(StringRef VarName) const {
00065     return ("__llvm_profile_" + VarName + "_" + RawFuncName).str();
00066   }
00067 
00068   /// Return the counter value of the current region.
00069   uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
00070 
00071   /// Set the counter value for the current region. This is used to keep track
00072   /// of changes to the most recent counter from control flow and non-local
00073   /// exits.
00074   void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
00075 
00076   /// Indicate that the current region is never reached, and thus should have a
00077   /// counter value of zero. This is important so that subsequent regions can
00078   /// correctly track their parent counts.
00079   void setCurrentRegionUnreachable() { setCurrentRegionCount(0); }
00080 
00081   /// Check if an execution count is known for a given statement. If so, return
00082   /// true and put the value in Count; else return false.
00083   bool getStmtCount(const Stmt *S, uint64_t &Count) {
00084     if (!StmtCountMap)
00085       return false;
00086     llvm::DenseMap<const Stmt*, uint64_t>::const_iterator
00087       I = StmtCountMap->find(S);
00088     if (I == StmtCountMap->end())
00089       return false;
00090     Count = I->second;
00091     return true;
00092   }
00093 
00094   /// If the execution count for the current statement is known, record that
00095   /// as the current count.
00096   void setCurrentStmt(const Stmt *S) {
00097     uint64_t Count;
00098     if (getStmtCount(S, Count))
00099       setCurrentRegionCount(Count);
00100   }
00101 
00102   /// Calculate branch weights appropriate for PGO data
00103   llvm::MDNode *createBranchWeights(uint64_t TrueCount, uint64_t FalseCount);
00104   llvm::MDNode *createBranchWeights(ArrayRef<uint64_t> Weights);
00105   llvm::MDNode *createLoopWeights(const Stmt *Cond, RegionCounter &Cnt);
00106 
00107   /// Check if we need to emit coverage mapping for a given declaration
00108   void checkGlobalDecl(GlobalDecl GD);
00109   /// Assign counters to regions and configure them for PGO of a given
00110   /// function. Does nothing if instrumentation is not enabled and either
00111   /// generates global variables or associates PGO data with each of the
00112   /// counters depending on whether we are generating or using instrumentation.
00113   void assignRegionCounters(const Decl *D, llvm::Function *Fn);
00114   /// Emit static data structures for instrumentation data.
00115   void emitInstrumentationData();
00116   /// Clean up region counter state. Must be called if assignRegionCounters is
00117   /// used.
00118   void destroyRegionCounters();
00119   /// Emit static initialization code, if any.
00120   static llvm::Function *emitInitialization(CodeGenModule &CGM);
00121   /// Emit a coverage mapping range with a counter zero
00122   /// for an unused declaration.
00123   void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
00124                                llvm::GlobalValue::LinkageTypes Linkage);
00125 private:
00126   void setFuncName(llvm::Function *Fn);
00127   void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
00128   void setVarLinkage(llvm::GlobalValue::LinkageTypes Linkage);
00129   void mapRegionCounters(const Decl *D);
00130   void computeRegionCounts(const Decl *D);
00131   void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
00132                                llvm::Function *Fn);
00133   void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
00134                         bool IsInMainFile);
00135   void emitCounterVariables();
00136   llvm::GlobalVariable *buildDataVar();
00137   void emitCounterRegionMapping(const Decl *D);
00138 
00139   /// Emit code to increment the counter at the given index
00140   void emitCounterIncrement(CGBuilderTy &Builder, unsigned Counter);
00141 
00142   /// Return the region counter for the given statement. This should only be
00143   /// called on statements that have a dedicated counter.
00144   unsigned getRegionCounter(const Stmt *S) {
00145     if (!RegionCounterMap)
00146       return 0;
00147     return (*RegionCounterMap)[S];
00148   }
00149 
00150   /// Return the region count for the counter at the given index.
00151   uint64_t getRegionCount(unsigned Counter) {
00152     if (!haveRegionCounts())
00153       return 0;
00154     return (*RegionCounts)[Counter];
00155   }
00156 
00157   friend class RegionCounter;
00158 };
00159 
00160 /// A counter for a particular region. This is the primary interface through
00161 /// which clients manage PGO counters and their values.
00162 class RegionCounter {
00163   CodeGenPGO *PGO;
00164   unsigned Counter;
00165   uint64_t Count;
00166   uint64_t ParentCount;
00167   uint64_t RegionCount;
00168   int64_t Adjust;
00169 
00170   RegionCounter(CodeGenPGO &PGO, unsigned CounterIndex)
00171     : PGO(&PGO), Counter(CounterIndex), Count(PGO.getRegionCount(Counter)),
00172       ParentCount(PGO.getCurrentRegionCount()), Adjust(0) {}
00173 
00174 public:
00175   RegionCounter(CodeGenPGO &PGO, const Stmt *S)
00176     : PGO(&PGO), Counter(PGO.getRegionCounter(S)),
00177       Count(PGO.getRegionCount(Counter)),
00178       ParentCount(PGO.getCurrentRegionCount()), Adjust(0) {}
00179 
00180   /// Get the value of the counter. In most cases this is the number of times
00181   /// the region of the counter was entered, but for switch labels it's the
00182   /// number of direct jumps to that label.
00183   uint64_t getCount() const { return Count; }
00184 
00185   /// Get the value of the counter with adjustments applied. Adjustments occur
00186   /// when control enters or leaves the region abnormally; i.e., if there is a
00187   /// jump to a label within the region, or if the function can return from
00188   /// within the region. The adjusted count, then, is the value of the counter
00189   /// at the end of the region.
00190   uint64_t getAdjustedCount() const {
00191     return Count + Adjust;
00192   }
00193 
00194   /// Get the value of the counter in this region's parent, i.e., the region
00195   /// that was active when this region began. This is useful for deriving
00196   /// counts in implicitly counted regions, like the false case of a condition
00197   /// or the normal exits of a loop.
00198   uint64_t getParentCount() const { return ParentCount; }
00199 
00200   /// Activate the counter by emitting an increment and starting to track
00201   /// adjustments. If AddIncomingFallThrough is true, the current region count
00202   /// will be added to the counter for the purposes of tracking the region.
00203   void beginRegion(CGBuilderTy &Builder, bool AddIncomingFallThrough=false) {
00204     beginRegion(AddIncomingFallThrough);
00205     PGO->emitCounterIncrement(Builder, Counter);
00206   }
00207   void beginRegion(bool AddIncomingFallThrough=false) {
00208     RegionCount = Count;
00209     if (AddIncomingFallThrough)
00210       RegionCount += PGO->getCurrentRegionCount();
00211     PGO->setCurrentRegionCount(RegionCount);
00212   }
00213 
00214   /// For counters on boolean branches, begins tracking adjustments for the
00215   /// uncounted path.
00216   void beginElseRegion() {
00217     RegionCount = ParentCount - Count;
00218     PGO->setCurrentRegionCount(RegionCount);
00219   }
00220 
00221   /// Reset the current region count.
00222   void setCurrentRegionCount(uint64_t CurrentCount) {
00223     RegionCount = CurrentCount;
00224     PGO->setCurrentRegionCount(RegionCount);
00225   }
00226 
00227   /// Adjust for non-local control flow after emitting a subexpression or
00228   /// substatement. This must be called to account for constructs such as gotos,
00229   /// labels, and returns, so that we can ensure that our region's count is
00230   /// correct in the code that follows.
00231   void adjustForControlFlow() {
00232     Adjust += PGO->getCurrentRegionCount() - RegionCount;
00233     // Reset the region count in case this is called again later.
00234     RegionCount = PGO->getCurrentRegionCount();
00235   }
00236 
00237   /// Commit all adjustments to the current region. If the region is a loop,
00238   /// the LoopAdjust value should be the count of all the breaks and continues
00239   /// from the loop, to compensate for those counts being deducted from the
00240   /// adjustments for the body of the loop.
00241   void applyAdjustmentsToRegion(uint64_t LoopAdjust) {
00242     PGO->setCurrentRegionCount(ParentCount + Adjust + LoopAdjust);
00243   }
00244 };
00245 
00246 }  // end namespace CodeGen
00247 }  // end namespace clang
00248 
00249 #endif