Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
5.0 KiB

  1. //===- ProfileDataLoader.h - Load & convert profile info ----*- 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. // The ProfileDataLoader class is used to load profiling data from a dump file.
  11. // The ProfileDataT<FType, BType> class is used to store the mapping of this
  12. // data to control flow edges.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ANALYSIS_PROFILEDATALOADER_H
  16. #define LLVM_ANALYSIS_PROFILEDATALOADER_H
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include <string>
  23. namespace llvm {
  24. class ModulePass;
  25. class Function;
  26. class BasicBlock;
  27. // Helper for dumping edges to dbgs().
  28. raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *,
  29. const BasicBlock *> E);
  30. /// \brief The ProfileDataT<FType, BType> class is used to store the mapping of
  31. /// profiling data to control flow edges.
  32. ///
  33. /// An edge is defined by its source and sink basic blocks.
  34. template<class FType, class BType>
  35. class ProfileDataT {
  36. public:
  37. // The profiling information defines an Edge by its source and sink basic
  38. // blocks.
  39. typedef std::pair<const BType*, const BType*> Edge;
  40. private:
  41. typedef DenseMap<Edge, unsigned> EdgeWeights;
  42. /// \brief Count the number of times a transition between two blocks is
  43. /// executed.
  44. ///
  45. /// As a special case, we also hold an edge from the null BasicBlock to the
  46. /// entry block to indicate how many times the function was entered.
  47. DenseMap<const FType*, EdgeWeights> EdgeInformation;
  48. public:
  49. /// getFunction() - Returns the Function for an Edge.
  50. static const FType *getFunction(Edge e) {
  51. // e.first may be NULL
  52. assert(((!e.first) || (e.first->getParent() == e.second->getParent()))
  53. && "A ProfileData::Edge can not be between two functions");
  54. assert(e.second && "A ProfileData::Edge must have a real sink");
  55. return e.second->getParent();
  56. }
  57. /// getEdge() - Creates an Edge between two BasicBlocks.
  58. static Edge getEdge(const BType *Src, const BType *Dest) {
  59. return Edge(Src, Dest);
  60. }
  61. /// getEdgeWeight - Return the number of times that a given edge was
  62. /// executed.
  63. unsigned getEdgeWeight(Edge e) const {
  64. const FType *f = getFunction(e);
  65. assert((EdgeInformation.find(f) != EdgeInformation.end())
  66. && "No profiling information for function");
  67. EdgeWeights weights = EdgeInformation.find(f)->second;
  68. assert((weights.find(e) != weights.end())
  69. && "No profiling information for edge");
  70. return weights.find(e)->second;
  71. }
  72. /// addEdgeWeight - Add 'weight' to the already stored execution count for
  73. /// this edge.
  74. void addEdgeWeight(Edge e, unsigned weight) {
  75. EdgeInformation[getFunction(e)][e] += weight;
  76. }
  77. };
  78. typedef ProfileDataT<Function, BasicBlock> ProfileData;
  79. //typedef ProfileDataT<MachineFunction, MachineBasicBlock> MachineProfileData;
  80. /// The ProfileDataLoader class is used to load raw profiling data from the
  81. /// dump file.
  82. class ProfileDataLoader {
  83. private:
  84. /// The name of the file where the raw profiling data is stored.
  85. const std::string &Filename;
  86. /// A vector of the command line arguments used when the target program was
  87. /// run to generate profiling data. One entry per program run.
  88. SmallVector<std::string, 1> CommandLines;
  89. /// The raw values for how many times each edge was traversed, values from
  90. /// multiple program runs are accumulated.
  91. SmallVector<unsigned, 32> EdgeCounts;
  92. public:
  93. /// ProfileDataLoader ctor - Read the specified profiling data file, exiting
  94. /// the program if the file is invalid or broken.
  95. ProfileDataLoader(const char *ToolName, const std::string &Filename);
  96. /// A special value used to represent the weight of an edge which has not
  97. /// been counted yet.
  98. static const unsigned Uncounted;
  99. /// getNumExecutions - Return the number of times the target program was run
  100. /// to generate this profiling data.
  101. unsigned getNumExecutions() const { return CommandLines.size(); }
  102. /// getExecution - Return the command line parameters used to generate the
  103. /// i'th set of profiling data.
  104. const std::string &getExecution(unsigned i) const { return CommandLines[i]; }
  105. const std::string &getFileName() const { return Filename; }
  106. /// getRawEdgeCounts - Return the raw profiling data, this is just a list of
  107. /// numbers with no mappings to edges.
  108. ArrayRef<unsigned> getRawEdgeCounts() const { return EdgeCounts; }
  109. };
  110. /// createProfileMetadataLoaderPass - This function returns a Pass that loads
  111. /// the profiling information for the module from the specified filename.
  112. ModulePass *createProfileMetadataLoaderPass(const std::string &Filename);
  113. } // End llvm namespace
  114. #endif