[llvm] r360240 - [MCA] Slightly refactor CodeRegion.h. NFCI

Andrea Di Biagio via llvm-commits llvm-commits at lists.llvm.org
Wed May 8 03:44:05 PDT 2019


Author: adibiagio
Date: Wed May  8 03:44:05 2019
New Revision: 360240

URL: http://llvm.org/viewvc/llvm-project?rev=360240&view=rev
Log:
[MCA] Slightly refactor CodeRegion.h. NFCI

Also, use a SmallVector instead of a std::vector for the code region.

Modified:
    llvm/trunk/tools/llvm-mca/CodeRegion.cpp
    llvm/trunk/tools/llvm-mca/CodeRegion.h

Modified: llvm/trunk/tools/llvm-mca/CodeRegion.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mca/CodeRegion.cpp?rev=360240&r1=360239&r2=360240&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mca/CodeRegion.cpp (original)
+++ llvm/trunk/tools/llvm-mca/CodeRegion.cpp Wed May  8 03:44:05 2019
@@ -16,7 +16,12 @@
 namespace llvm {
 namespace mca {
 
-bool CodeRegion::isLocInRange(llvm::SMLoc Loc) const {
+CodeRegions::CodeRegions(llvm::SourceMgr &S) : SM(S) {
+  // Create a default region for the input code sequence.
+  Regions.emplace_back(make_unique<CodeRegion>("Default", SMLoc()));
+}
+
+bool CodeRegion::isLocInRange(SMLoc Loc) const {
   if (RangeEnd.isValid() && Loc.getPointer() > RangeEnd.getPointer())
     return false;
   if (RangeStart.isValid() && Loc.getPointer() < RangeStart.getPointer())
@@ -24,11 +29,11 @@ bool CodeRegion::isLocInRange(llvm::SMLo
   return true;
 }
 
-void CodeRegions::beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) {
+void CodeRegions::beginRegion(StringRef Description, SMLoc Loc) {
   assert(!Regions.empty() && "Missing Default region");
   const CodeRegion &CurrentRegion = *Regions.back();
   if (CurrentRegion.startLoc().isValid() && !CurrentRegion.endLoc().isValid()) {
-    SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
+    SM.PrintMessage(Loc, SourceMgr::DK_Warning,
                     "Ignoring invalid region start");
     return;
   }
@@ -36,14 +41,14 @@ void CodeRegions::beginRegion(llvm::Stri
   // Remove the default region if there are user defined regions.
   if (!CurrentRegion.startLoc().isValid())
     Regions.erase(Regions.begin());
-  addRegion(Description, Loc);
+  Regions.emplace_back(make_unique<CodeRegion>(Description, Loc));
 }
 
-void CodeRegions::endRegion(llvm::SMLoc Loc) {
+void CodeRegions::endRegion(SMLoc Loc) {
   assert(!Regions.empty() && "Missing Default region");
   CodeRegion &CurrentRegion = *Regions.back();
   if (CurrentRegion.endLoc().isValid()) {
-    SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
+    SM.PrintMessage(Loc, SourceMgr::DK_Warning,
                     "Ignoring invalid region end");
     return;
   }
@@ -51,11 +56,11 @@ void CodeRegions::endRegion(llvm::SMLoc
   CurrentRegion.setEndLocation(Loc);
 }
 
-void CodeRegions::addInstruction(const llvm::MCInst &Instruction) {
-  const llvm::SMLoc &Loc = Instruction.getLoc();
+void CodeRegions::addInstruction(const MCInst &Instruction) {
+  const SMLoc &Loc = Instruction.getLoc();
   const auto It =
       std::find_if(Regions.rbegin(), Regions.rend(),
-                   [Loc](const std::unique_ptr<CodeRegion> &Region) {
+                   [Loc](const UniqueCodeRegion &Region) {
                      return Region->isLocInRange(Loc);
                    });
   if (It != Regions.rend())

Modified: llvm/trunk/tools/llvm-mca/CodeRegion.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mca/CodeRegion.h?rev=360240&r1=360239&r2=360240&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mca/CodeRegion.h (original)
+++ llvm/trunk/tools/llvm-mca/CodeRegion.h Wed May  8 03:44:05 2019
@@ -34,6 +34,7 @@
 #define LLVM_TOOLS_LLVM_MCA_CODEREGION_H
 
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/MC/MCInst.h"
 #include "llvm/Support/SMLoc.h"
@@ -50,7 +51,7 @@ class CodeRegion {
   // An optional descriptor for this region.
   llvm::StringRef Description;
   // Instructions that form this region.
-  std::vector<llvm::MCInst> Instructions;
+  llvm::SmallVector<llvm::MCInst, 8> Instructions;
   // Source location range.
   llvm::SMLoc RangeStart;
   llvm::SMLoc RangeEnd;
@@ -82,20 +83,15 @@ class CodeRegions {
   // A source manager. Used by the tool to generate meaningful warnings.
   llvm::SourceMgr &SM;
 
-  std::vector<std::unique_ptr<CodeRegion>> Regions;
-
-  // Construct a new region of code guarded by LLVM-MCA comments.
-  void addRegion(llvm::StringRef Description, llvm::SMLoc Loc) {
-    Regions.emplace_back(llvm::make_unique<CodeRegion>(Description, Loc));
-  }
+  using UniqueCodeRegion = std::unique_ptr<CodeRegion>;
+  std::vector<UniqueCodeRegion> Regions;
 
   CodeRegions(const CodeRegions &) = delete;
   CodeRegions &operator=(const CodeRegions &) = delete;
 
 public:
-  typedef std::vector<std::unique_ptr<CodeRegion>>::iterator iterator;
-  typedef std::vector<std::unique_ptr<CodeRegion>>::const_iterator
-      const_iterator;
+  typedef std::vector<UniqueCodeRegion>::iterator iterator;
+  typedef std::vector<UniqueCodeRegion>::const_iterator const_iterator;
 
   iterator begin() { return Regions.begin(); }
   iterator end() { return Regions.end(); }
@@ -107,17 +103,14 @@ public:
   void addInstruction(const llvm::MCInst &Instruction);
   llvm::SourceMgr &getSourceMgr() const { return SM; }
 
-  CodeRegions(llvm::SourceMgr &S) : SM(S) {
-    // Create a default region for the input code sequence.
-    addRegion("Default", llvm::SMLoc());
-  }
+  CodeRegions(llvm::SourceMgr &S);
 
   llvm::ArrayRef<llvm::MCInst> getInstructionSequence(unsigned Idx) const {
     return Regions[Idx]->getInstructions();
   }
 
   bool empty() const {
-    return llvm::all_of(Regions, [](const std::unique_ptr<CodeRegion> &Region) {
+    return llvm::all_of(Regions, [](const UniqueCodeRegion &Region) {
       return Region->empty();
     });
   }




More information about the llvm-commits mailing list