[llvm-mca] Use llvm::ArrayRef in class SourceMgr. NFCI

Class SourceMgr now uses type ArrayRef<MCInst> to reference the
sequence of code from a "CodeRegion".

llvm-svn: 344911
This commit is contained in:
Andrea Di Biagio 2018-10-22 15:36:15 +00:00
parent 6f5cd7c67f
commit 01b9fd6868
5 changed files with 31 additions and 33 deletions

View File

@ -52,15 +52,15 @@ void CodeRegions::endRegion(SMLoc Loc) {
CurrentRegion.setEndLocation(Loc);
}
void CodeRegions::addInstruction(std::unique_ptr<const MCInst> Instruction) {
const 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) {
return Region->isLocInRange(Loc);
});
if (It != Regions.rend())
(*It)->addInstruction(std::move(Instruction));
(*It)->addInstruction(Instruction);
}
} // namespace mca

View File

@ -34,6 +34,7 @@
#ifndef LLVM_TOOLS_LLVM_MCA_CODEREGION_H
#define LLVM_TOOLS_LLVM_MCA_CODEREGION_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/MC/MCInst.h"
#include "llvm/Support/SMLoc.h"
@ -49,7 +50,7 @@ class CodeRegion {
// An optional descriptor for this region.
llvm::StringRef Description;
// Instructions that form this region.
std::vector<std::unique_ptr<const llvm::MCInst>> Instructions;
std::vector<llvm::MCInst> Instructions;
// Source location range.
llvm::SMLoc RangeStart;
llvm::SMLoc RangeEnd;
@ -61,8 +62,8 @@ public:
CodeRegion(llvm::StringRef Desc, llvm::SMLoc Start)
: Description(Desc), RangeStart(Start), RangeEnd() {}
void addInstruction(std::unique_ptr<const llvm::MCInst> Instruction) {
Instructions.emplace_back(std::move(Instruction));
void addInstruction(const llvm::MCInst &Instruction) {
Instructions.emplace_back(Instruction);
}
llvm::SMLoc startLoc() const { return RangeStart; }
@ -72,10 +73,7 @@ public:
bool empty() const { return Instructions.empty(); }
bool isLocInRange(llvm::SMLoc Loc) const;
const std::vector<std::unique_ptr<const llvm::MCInst>> &
getInstructions() const {
return Instructions;
}
llvm::ArrayRef<llvm::MCInst> getInstructions() const { return Instructions; }
llvm::StringRef getDescription() const { return Description; }
};
@ -106,23 +104,21 @@ public:
void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc);
void endRegion(llvm::SMLoc Loc);
void addInstruction(std::unique_ptr<const llvm::MCInst> Instruction);
void addInstruction(const llvm::MCInst &Instruction);
CodeRegions(llvm::SourceMgr &S) : SM(S) {
// Create a default region for the input code sequence.
addRegion("Default", llvm::SMLoc());
}
const std::vector<std::unique_ptr<const llvm::MCInst>> &
getInstructionSequence(unsigned Idx) const {
llvm::ArrayRef<llvm::MCInst> getInstructionSequence(unsigned Idx) const {
return Regions[Idx]->getInstructions();
}
bool empty() const {
return std::all_of(Regions.begin(), Regions.end(),
[](const std::unique_ptr<CodeRegion> &Region) {
return Region->empty();
});
return llvm::all_of(Regions, [](const std::unique_ptr<CodeRegion> &Region) {
return Region->empty();
});
}
};

View File

@ -16,29 +16,29 @@
#ifndef LLVM_TOOLS_LLVM_MCA_SOURCEMGR_H
#define LLVM_TOOLS_LLVM_MCA_SOURCEMGR_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/MC/MCInst.h"
#include <vector>
namespace mca {
typedef std::pair<unsigned, const llvm::MCInst *> SourceRef;
typedef std::pair<unsigned, const llvm::MCInst &> SourceRef;
class SourceMgr {
using InstVec = std::vector<std::unique_ptr<const llvm::MCInst>>;
const InstVec &Sequence;
llvm::ArrayRef<llvm::MCInst> Sequence;
unsigned Current;
unsigned Iterations;
static const unsigned DefaultIterations = 100;
public:
SourceMgr(const InstVec &MCInstSequence, unsigned NumIterations)
SourceMgr(llvm::ArrayRef<llvm::MCInst> MCInstSequence, unsigned NumIterations)
: Sequence(MCInstSequence), Current(0),
Iterations(NumIterations ? NumIterations : DefaultIterations) {}
unsigned getCurrentIteration() const { return Current / Sequence.size(); }
unsigned getNumIterations() const { return Iterations; }
unsigned size() const { return Sequence.size(); }
const InstVec &getSequence() const { return Sequence; }
llvm::ArrayRef<llvm::MCInst> getSequence() const { return Sequence; }
bool hasNext() const { return Current < (Iterations * size()); }
void updateNext() { Current++; }
@ -46,7 +46,7 @@ public:
const SourceRef peekNext() const {
assert(hasNext() && "Already at end of sequence!");
unsigned Index = getCurrentInstructionIndex();
return SourceRef(Current, Sequence[Index].get());
return SourceRef(Current, Sequence[Index]);
}
unsigned getCurrentInstructionIndex() const {
@ -54,7 +54,7 @@ public:
}
const llvm::MCInst &getMCInstFromIndex(unsigned Index) const {
return *Sequence[Index % size()];
return Sequence[Index % size()];
}
bool isEmpty() const { return size() == 0; }

View File

@ -36,7 +36,7 @@ llvm::Error FetchStage::getNextInstruction() {
return llvm::ErrorSuccess();
const SourceRef SR = SM.peekNext();
llvm::Expected<std::unique_ptr<Instruction>> InstOrErr =
IB.createInstruction(*SR.second);
IB.createInstruction(SR.second);
if (!InstOrErr)
return InstOrErr.takeError();
CurrentInstruction = std::move(InstOrErr.get());

View File

@ -68,13 +68,15 @@ static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
cl::value_desc("filename"));
static cl::opt<std::string>
ArchName("march", cl::desc("Target arch to assemble for, "
"see -version for available targets"),
ArchName("march",
cl::desc("Target arch to assemble for, "
"see -version for available targets"),
cl::cat(ToolOptions));
static cl::opt<std::string>
TripleName("mtriple", cl::desc("Target triple to assemble for, "
"see -version for available targets"),
TripleName("mtriple",
cl::desc("Target triple to assemble for, "
"see -version for available targets"),
cl::cat(ToolOptions));
static cl::opt<std::string>
@ -270,9 +272,10 @@ public:
: MCStreamer(Context), Regions(R) {}
// We only want to intercept the emission of new instructions.
virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
virtual void EmitInstruction(const MCInst &Inst,
const MCSubtargetInfo & /* unused */,
bool /* unused */) override {
Regions.addInstruction(llvm::make_unique<const MCInst>(Inst));
Regions.addInstruction(Inst);
}
bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
@ -290,8 +293,7 @@ public:
void EmitCOFFSymbolType(int Type) override {}
void EndCOFFSymbolDef() override {}
const std::vector<std::unique_ptr<const MCInst>> &
GetInstructionSequence(unsigned Index) const {
ArrayRef<MCInst> GetInstructionSequence(unsigned Index) const {
return Regions.getInstructionSequence(Index);
}
};