CodeRegion.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //===-------------------------- CodeRegion.cpp -----------------*- 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 methods from the CodeRegions interface.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeRegion.h"
  14. namespace llvm {
  15. namespace mca {
  16. bool CodeRegion::isLocInRange(SMLoc Loc) const {
  17. if (RangeEnd.isValid() && Loc.getPointer() > RangeEnd.getPointer())
  18. return false;
  19. if (RangeStart.isValid() && Loc.getPointer() < RangeStart.getPointer())
  20. return false;
  21. return true;
  22. }
  23. void CodeRegions::addInstruction(const MCInst &Instruction) {
  24. SMLoc Loc = Instruction.getLoc();
  25. for (UniqueCodeRegion &Region : Regions)
  26. if (Region->isLocInRange(Loc))
  27. Region->addInstruction(Instruction);
  28. }
  29. AnalysisRegions::AnalysisRegions(llvm::SourceMgr &S) : CodeRegions(S) {
  30. // Create a default region for the input code sequence.
  31. Regions.emplace_back(std::make_unique<CodeRegion>("", SMLoc()));
  32. }
  33. void AnalysisRegions::beginRegion(StringRef Description, SMLoc Loc) {
  34. if (ActiveRegions.empty()) {
  35. // Remove the default region if there is at least one user defined region.
  36. // By construction, only the default region has an invalid start location.
  37. if (Regions.size() == 1 && !Regions[0]->startLoc().isValid() &&
  38. !Regions[0]->endLoc().isValid()) {
  39. ActiveRegions[Description] = 0;
  40. Regions[0] = std::make_unique<CodeRegion>(Description, Loc);
  41. return;
  42. }
  43. } else {
  44. auto It = ActiveRegions.find(Description);
  45. if (It != ActiveRegions.end()) {
  46. const CodeRegion &R = *Regions[It->second];
  47. if (Description.empty()) {
  48. SM.PrintMessage(Loc, llvm::SourceMgr::DK_Error,
  49. "found multiple overlapping anonymous regions");
  50. SM.PrintMessage(R.startLoc(), llvm::SourceMgr::DK_Note,
  51. "Previous anonymous region was defined here");
  52. FoundErrors = true;
  53. return;
  54. }
  55. SM.PrintMessage(Loc, llvm::SourceMgr::DK_Error,
  56. "overlapping regions cannot have the same name");
  57. SM.PrintMessage(R.startLoc(), llvm::SourceMgr::DK_Note,
  58. "region " + Description + " was previously defined here");
  59. FoundErrors = true;
  60. return;
  61. }
  62. }
  63. ActiveRegions[Description] = Regions.size();
  64. Regions.emplace_back(std::make_unique<CodeRegion>(Description, Loc));
  65. }
  66. void AnalysisRegions::endRegion(StringRef Description, SMLoc Loc) {
  67. if (Description.empty()) {
  68. // Special case where there is only one user defined region,
  69. // and this LLVM-MCA-END directive doesn't provide a region name.
  70. // In this case, we assume that the user simply wanted to just terminate
  71. // the only active region.
  72. if (ActiveRegions.size() == 1) {
  73. auto It = ActiveRegions.begin();
  74. Regions[It->second]->setEndLocation(Loc);
  75. ActiveRegions.erase(It);
  76. return;
  77. }
  78. // Special case where the region end marker applies to the default region.
  79. if (ActiveRegions.empty() && Regions.size() == 1 &&
  80. !Regions[0]->startLoc().isValid() && !Regions[0]->endLoc().isValid()) {
  81. Regions[0]->setEndLocation(Loc);
  82. return;
  83. }
  84. }
  85. auto It = ActiveRegions.find(Description);
  86. if (It != ActiveRegions.end()) {
  87. Regions[It->second]->setEndLocation(Loc);
  88. ActiveRegions.erase(It);
  89. return;
  90. }
  91. FoundErrors = true;
  92. SM.PrintMessage(Loc, llvm::SourceMgr::DK_Error,
  93. "found an invalid region end directive");
  94. if (!Description.empty()) {
  95. SM.PrintMessage(Loc, llvm::SourceMgr::DK_Note,
  96. "unable to find an active region named " + Description);
  97. } else {
  98. SM.PrintMessage(Loc, llvm::SourceMgr::DK_Note,
  99. "unable to find an active anonymous region");
  100. }
  101. }
  102. InstrumentRegions::InstrumentRegions(llvm::SourceMgr &S) : CodeRegions(S) {}
  103. void InstrumentRegions::beginRegion(StringRef Description, SMLoc Loc,
  104. SharedInstrument I) {
  105. if (Description.empty()) {
  106. SM.PrintMessage(Loc, llvm::SourceMgr::DK_Error,
  107. "anonymous instrumentation regions are not permitted");
  108. FoundErrors = true;
  109. return;
  110. }
  111. auto It = ActiveRegions.find(Description);
  112. if (It != ActiveRegions.end()) {
  113. const CodeRegion &R = *Regions[It->second];
  114. SM.PrintMessage(
  115. Loc, llvm::SourceMgr::DK_Error,
  116. "overlapping instrumentation regions cannot be of the same kind");
  117. SM.PrintMessage(R.startLoc(), llvm::SourceMgr::DK_Note,
  118. "instrumentation region " + Description +
  119. " was previously defined here");
  120. FoundErrors = true;
  121. return;
  122. }
  123. ActiveRegions[Description] = Regions.size();
  124. Regions.emplace_back(std::make_unique<InstrumentRegion>(Description, Loc, I));
  125. }
  126. void InstrumentRegions::endRegion(StringRef Description, SMLoc Loc) {
  127. auto It = ActiveRegions.find(Description);
  128. if (It != ActiveRegions.end()) {
  129. Regions[It->second]->setEndLocation(Loc);
  130. ActiveRegions.erase(It);
  131. return;
  132. }
  133. FoundErrors = true;
  134. SM.PrintMessage(Loc, llvm::SourceMgr::DK_Error,
  135. "found an invalid instrumentation region end directive");
  136. if (!Description.empty()) {
  137. SM.PrintMessage(Loc, llvm::SourceMgr::DK_Note,
  138. "unable to find an active instrumentation region named " +
  139. Description);
  140. }
  141. }
  142. const SmallVector<SharedInstrument>
  143. InstrumentRegions::getActiveInstruments(SMLoc Loc) const {
  144. SmallVector<SharedInstrument> AI;
  145. for (auto &R : Regions) {
  146. if (R->isLocInRange(Loc)) {
  147. InstrumentRegion *IR = static_cast<InstrumentRegion *>(R.get());
  148. AI.emplace_back(IR->getInstrument());
  149. }
  150. }
  151. return AI;
  152. }
  153. } // namespace mca
  154. } // namespace llvm