CodeRegion.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //===-------------------------- CodeRegion.h -------------------*- C++ -* -===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. ///
  10. /// This file implements class CodeRegion and CodeRegions.
  11. ///
  12. /// A CodeRegion describes a region of assembly code guarded by special LLVM-MCA
  13. /// comment directives.
  14. ///
  15. /// # LLVM-MCA-BEGIN foo
  16. /// ... ## asm
  17. /// # LLVM-MCA-END
  18. ///
  19. /// A comment starting with substring LLVM-MCA-BEGIN marks the beginning of a
  20. /// new region of code.
  21. /// A comment starting with substring LLVM-MCA-END marks the end of the
  22. /// last-seen region of code.
  23. ///
  24. /// Code regions are not allowed to overlap. Each region can have a optional
  25. /// description; internally, regions are described by a range of source
  26. /// locations (SMLoc objects).
  27. ///
  28. /// An instruction (a MCInst) is added to a region R only if its location is in
  29. /// range [R.RangeStart, R.RangeEnd].
  30. //
  31. //===----------------------------------------------------------------------===//
  32. #ifndef LLVM_TOOLS_LLVM_MCA_CODEREGION_H
  33. #define LLVM_TOOLS_LLVM_MCA_CODEREGION_H
  34. #include "llvm/ADT/ArrayRef.h"
  35. #include "llvm/ADT/SmallVector.h"
  36. #include "llvm/ADT/StringMap.h"
  37. #include "llvm/ADT/StringRef.h"
  38. #include "llvm/MC/MCInst.h"
  39. #include "llvm/Support/Error.h"
  40. #include "llvm/Support/SMLoc.h"
  41. #include "llvm/Support/SourceMgr.h"
  42. #include <vector>
  43. namespace llvm {
  44. namespace mca {
  45. /// A region of assembly code.
  46. ///
  47. /// It identifies a sequence of machine instructions.
  48. class CodeRegion {
  49. // An optional descriptor for this region.
  50. llvm::StringRef Description;
  51. // Instructions that form this region.
  52. llvm::SmallVector<llvm::MCInst, 16> Instructions;
  53. // Source location range.
  54. llvm::SMLoc RangeStart;
  55. llvm::SMLoc RangeEnd;
  56. CodeRegion(const CodeRegion &) = delete;
  57. CodeRegion &operator=(const CodeRegion &) = delete;
  58. public:
  59. CodeRegion(llvm::StringRef Desc, llvm::SMLoc Start)
  60. : Description(Desc), RangeStart(Start) {}
  61. void addInstruction(const llvm::MCInst &Instruction) {
  62. Instructions.emplace_back(Instruction);
  63. }
  64. llvm::SMLoc startLoc() const { return RangeStart; }
  65. llvm::SMLoc endLoc() const { return RangeEnd; }
  66. void setEndLocation(llvm::SMLoc End) { RangeEnd = End; }
  67. bool empty() const { return Instructions.empty(); }
  68. bool isLocInRange(llvm::SMLoc Loc) const;
  69. llvm::ArrayRef<llvm::MCInst> getInstructions() const { return Instructions; }
  70. llvm::StringRef getDescription() const { return Description; }
  71. };
  72. class CodeRegionParseError final : public Error {};
  73. class CodeRegions {
  74. // A source manager. Used by the tool to generate meaningful warnings.
  75. llvm::SourceMgr &SM;
  76. using UniqueCodeRegion = std::unique_ptr<CodeRegion>;
  77. std::vector<UniqueCodeRegion> Regions;
  78. llvm::StringMap<unsigned> ActiveRegions;
  79. bool FoundErrors;
  80. CodeRegions(const CodeRegions &) = delete;
  81. CodeRegions &operator=(const CodeRegions &) = delete;
  82. public:
  83. CodeRegions(llvm::SourceMgr &S);
  84. typedef std::vector<UniqueCodeRegion>::iterator iterator;
  85. typedef std::vector<UniqueCodeRegion>::const_iterator const_iterator;
  86. iterator begin() { return Regions.begin(); }
  87. iterator end() { return Regions.end(); }
  88. const_iterator begin() const { return Regions.cbegin(); }
  89. const_iterator end() const { return Regions.cend(); }
  90. void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc);
  91. void endRegion(llvm::StringRef Description, llvm::SMLoc Loc);
  92. void addInstruction(const llvm::MCInst &Instruction);
  93. llvm::SourceMgr &getSourceMgr() const { return SM; }
  94. llvm::ArrayRef<llvm::MCInst> getInstructionSequence(unsigned Idx) const {
  95. return Regions[Idx]->getInstructions();
  96. }
  97. bool empty() const {
  98. return llvm::all_of(Regions, [](const UniqueCodeRegion &Region) {
  99. return Region->empty();
  100. });
  101. }
  102. bool isValid() const { return !FoundErrors; }
  103. };
  104. } // namespace mca
  105. } // namespace llvm
  106. #endif