BasicBlockSectionsProfileReader.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- BasicBlockSectionsProfileReader.h - BB sections profile reader pass ==//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This pass creates the basic block cluster info by reading the basic block
  15. // sections profile. The cluster info will be used by the basic-block-sections
  16. // pass to arrange basic blocks in their sections.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
  20. #define LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
  21. #include "llvm/ADT/SmallSet.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/StringMap.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/InitializePasses.h"
  26. #include "llvm/Pass.h"
  27. #include "llvm/Support/Error.h"
  28. #include "llvm/Support/LineIterator.h"
  29. #include "llvm/Support/MemoryBuffer.h"
  30. using namespace llvm;
  31. namespace llvm {
  32. // The cluster information for a machine basic block.
  33. struct BBClusterInfo {
  34. // Unique ID for this basic block.
  35. unsigned BBID;
  36. // Cluster ID this basic block belongs to.
  37. unsigned ClusterID;
  38. // Position of basic block within the cluster.
  39. unsigned PositionInCluster;
  40. };
  41. using ProgramBBClusterInfoMapTy = StringMap<SmallVector<BBClusterInfo>>;
  42. class BasicBlockSectionsProfileReader : public ImmutablePass {
  43. public:
  44. static char ID;
  45. BasicBlockSectionsProfileReader(const MemoryBuffer *Buf)
  46. : ImmutablePass(ID), MBuf(Buf) {
  47. initializeBasicBlockSectionsProfileReaderPass(
  48. *PassRegistry::getPassRegistry());
  49. };
  50. BasicBlockSectionsProfileReader() : ImmutablePass(ID) {
  51. initializeBasicBlockSectionsProfileReaderPass(
  52. *PassRegistry::getPassRegistry());
  53. }
  54. StringRef getPassName() const override {
  55. return "Basic Block Sections Profile Reader";
  56. }
  57. // Returns true if basic block sections profile exist for function \p
  58. // FuncName.
  59. bool isFunctionHot(StringRef FuncName) const;
  60. // Returns a pair with first element representing whether basic block sections
  61. // profile exist for the function \p FuncName, and the second element
  62. // representing the basic block sections profile (cluster info) for this
  63. // function. If the first element is true and the second element is empty, it
  64. // means unique basic block sections are desired for all basic blocks of the
  65. // function.
  66. std::pair<bool, SmallVector<BBClusterInfo>>
  67. getBBClusterInfoForFunction(StringRef FuncName) const;
  68. /// Read profiles of basic blocks if available here.
  69. void initializePass() override;
  70. private:
  71. StringRef getAliasName(StringRef FuncName) const {
  72. auto R = FuncAliasMap.find(FuncName);
  73. return R == FuncAliasMap.end() ? FuncName : R->second;
  74. }
  75. // This contains the basic-block-sections profile.
  76. const MemoryBuffer *MBuf = nullptr;
  77. // This encapsulates the BB cluster information for the whole program.
  78. //
  79. // For every function name, it contains the cluster information for (all or
  80. // some of) its basic blocks. The cluster information for every basic block
  81. // includes its cluster ID along with the position of the basic block in that
  82. // cluster.
  83. ProgramBBClusterInfoMapTy ProgramBBClusterInfo;
  84. // Some functions have alias names. We use this map to find the main alias
  85. // name for which we have mapping in ProgramBBClusterInfo.
  86. StringMap<StringRef> FuncAliasMap;
  87. };
  88. // Creates a BasicBlockSectionsProfileReader pass to parse the basic block
  89. // sections profile. \p Buf is a memory buffer that contains the list of
  90. // functions and basic block ids to selectively enable basic block sections.
  91. ImmutablePass *
  92. createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf);
  93. } // namespace llvm
  94. #endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
  95. #ifdef __GNUC__
  96. #pragma GCC diagnostic pop
  97. #endif