From 5b9e14bc6df7cd9e6b1ca59819e26d3ad8aab0c1 Mon Sep 17 00:00:00 2001 From: Andrea Di Biagio Date: Wed, 8 May 2019 10:44:05 +0000 Subject: [PATCH] [MCA] Slightly refactor CodeRegion.h. NFCI Also, use a SmallVector instead of a std::vector for the code region. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360240 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/llvm-mca/CodeRegion.cpp | 23 ++++++++++++++--------- tools/llvm-mca/CodeRegion.h | 23 ++++++++--------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/tools/llvm-mca/CodeRegion.cpp b/tools/llvm-mca/CodeRegion.cpp index 83541675774..6d4192250aa 100644 --- a/tools/llvm-mca/CodeRegion.cpp +++ b/tools/llvm-mca/CodeRegion.cpp @@ -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("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::SMLoc Loc) const { 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::StringRef Description, llvm::SMLoc Loc) { // 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(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 Loc) { 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 &Region) { + [Loc](const UniqueCodeRegion &Region) { return Region->isLocInRange(Loc); }); if (It != Regions.rend()) diff --git a/tools/llvm-mca/CodeRegion.h b/tools/llvm-mca/CodeRegion.h index 1cdfeabfef4..cc474b78ca7 100644 --- a/tools/llvm-mca/CodeRegion.h +++ b/tools/llvm-mca/CodeRegion.h @@ -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 Instructions; + llvm::SmallVector 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> 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(Description, Loc)); - } + using UniqueCodeRegion = std::unique_ptr; + std::vector Regions; CodeRegions(const CodeRegions &) = delete; CodeRegions &operator=(const CodeRegions &) = delete; public: - typedef std::vector>::iterator iterator; - typedef std::vector>::const_iterator - const_iterator; + typedef std::vector::iterator iterator; + typedef std::vector::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 getInstructionSequence(unsigned Idx) const { return Regions[Idx]->getInstructions(); } bool empty() const { - return llvm::all_of(Regions, [](const std::unique_ptr &Region) { + return llvm::all_of(Regions, [](const UniqueCodeRegion &Region) { return Region->empty(); }); } -- 2.40.0