From 7be45b0f85c81454ee65e19e5a9bc87260af39e7 Mon Sep 17 00:00:00 2001 From: Andrea Di Biagio Date: Wed, 24 Oct 2018 15:06:27 +0000 Subject: [PATCH] [llvm-mca] Refactor class SourceMgr. NFCI Added begin()/end() methods to allow the usage of SourceMgr in foreach loops. With this change, method getMCInstFromIndex() (as well as a couple of other methods) are now redundant, and can be removed from the public interface. llvm-svn: 345147 --- .../llvm-mca/Views/InstructionInfoView.cpp | 4 +- .../llvm-mca/Views/ResourcePressureView.cpp | 10 ++-- llvm/tools/llvm-mca/Views/TimelineView.cpp | 51 ++++++++++--------- llvm/tools/llvm-mca/include/SourceMgr.h | 22 +++----- 4 files changed, 43 insertions(+), 44 deletions(-) diff --git a/llvm/tools/llvm-mca/Views/InstructionInfoView.cpp b/llvm/tools/llvm-mca/Views/InstructionInfoView.cpp index a2e3001383a1..0a97e569c47f 100644 --- a/llvm/tools/llvm-mca/Views/InstructionInfoView.cpp +++ b/llvm/tools/llvm-mca/Views/InstructionInfoView.cpp @@ -22,7 +22,6 @@ void InstructionInfoView::printView(raw_ostream &OS) const { std::string Buffer; raw_string_ostream TempStream(Buffer); const MCSchedModel &SM = STI.getSchedModel(); - unsigned Instructions = Source.size(); std::string Instruction; raw_string_ostream InstrStream(Instruction); @@ -32,8 +31,7 @@ void InstructionInfoView::printView(raw_ostream &OS) const { << "[4]: MayLoad\n[5]: MayStore\n[6]: HasSideEffects (U)\n\n"; TempStream << "[1] [2] [3] [4] [5] [6] Instructions:\n"; - for (unsigned I = 0, E = Instructions; I < E; ++I) { - const MCInst &Inst = Source.getMCInstFromIndex(I); + for (const MCInst &Inst : Source) { const MCInstrDesc &MCDesc = MCII.get(Inst.getOpcode()); // Obtain the scheduling class information from the instruction. diff --git a/llvm/tools/llvm-mca/Views/ResourcePressureView.cpp b/llvm/tools/llvm-mca/Views/ResourcePressureView.cpp index bba1e70bc268..17c801259d9e 100644 --- a/llvm/tools/llvm-mca/Views/ResourcePressureView.cpp +++ b/llvm/tools/llvm-mca/Views/ResourcePressureView.cpp @@ -148,13 +148,15 @@ void ResourcePressureView::printResourcePressurePerInstruction( std::string Instruction; raw_string_ostream InstrStream(Instruction); - for (unsigned I = 0, E = Source.size(); I < E; ++I) { + unsigned InstrIndex = 0; + for (const MCInst &MCI : Source) { + unsigned BaseEltIdx = InstrIndex * NumResourceUnits; for (unsigned J = 0; J < NumResourceUnits; ++J) { - double Usage = ResourceUsage[J + I * NumResourceUnits]; + double Usage = ResourceUsage[J + BaseEltIdx]; printResourcePressure(FOS, Usage / Executions, (J + 1) * 7); } - MCIP.printInst(&Source.getMCInstFromIndex(I), InstrStream, "", STI); + MCIP.printInst(&MCI, InstrStream, "", STI); InstrStream.flush(); StringRef Str(Instruction); @@ -167,6 +169,8 @@ void ResourcePressureView::printResourcePressurePerInstruction( FOS.flush(); OS << Buffer; Buffer = ""; + + ++InstrIndex; } } } // namespace mca diff --git a/llvm/tools/llvm-mca/Views/TimelineView.cpp b/llvm/tools/llvm-mca/Views/TimelineView.cpp index 1ad7271b2a47..d802d42352d8 100644 --- a/llvm/tools/llvm-mca/Views/TimelineView.cpp +++ b/llvm/tools/llvm-mca/Views/TimelineView.cpp @@ -177,11 +177,10 @@ void TimelineView::printAverageWaitTimes(raw_ostream &OS) const { formatted_raw_ostream FOS(OS); unsigned Executions = Timeline.size() / AsmSequence.size(); - for (unsigned I = 0, E = WaitTime.size(); I < E; ++I) { - printWaitTimeEntry(FOS, WaitTime[I], I, Executions); + unsigned IID = 0; + for (const MCInst &Inst : AsmSequence) { + printWaitTimeEntry(FOS, WaitTime[IID], IID, Executions); // Append the instruction info at the end of the line. - const MCInst &Inst = AsmSequence.getMCInstFromIndex(I); - MCIP.printInst(&Inst, InstrStream, "", STI); InstrStream.flush(); @@ -191,6 +190,8 @@ void TimelineView::printAverageWaitTimes(raw_ostream &OS) const { FOS << " " << Str << '\n'; FOS.flush(); Instruction = ""; + + ++IID; } } @@ -266,25 +267,29 @@ void TimelineView::printTimeline(raw_ostream &OS) const { std::string Instruction; raw_string_ostream InstrStream(Instruction); - for (unsigned I = 0, E = Timeline.size(); I < E; ++I) { - const TimelineViewEntry &Entry = Timeline[I]; - if (Entry.CycleRetired == 0) - return; - - unsigned Iteration = I / AsmSequence.size(); - unsigned SourceIndex = I % AsmSequence.size(); - printTimelineViewEntry(FOS, Entry, Iteration, SourceIndex); - // Append the instruction info at the end of the line. - const MCInst &Inst = AsmSequence.getMCInstFromIndex(I); - MCIP.printInst(&Inst, InstrStream, "", STI); - InstrStream.flush(); - - // Consume any tabs or spaces at the beginning of the string. - StringRef Str(Instruction); - Str = Str.ltrim(); - FOS << " " << Str << '\n'; - FOS.flush(); - Instruction = ""; + unsigned IID = 0; + const unsigned Iterations = Timeline.size() / AsmSequence.size(); + for (unsigned Iteration = 0; Iteration < Iterations; ++Iteration) { + for (const MCInst &Inst : AsmSequence) { + const TimelineViewEntry &Entry = Timeline[IID]; + if (Entry.CycleRetired == 0) + return; + + unsigned SourceIndex = IID % AsmSequence.size(); + printTimelineViewEntry(FOS, Entry, Iteration, SourceIndex); + // Append the instruction info at the end of the line. + MCIP.printInst(&Inst, InstrStream, "", STI); + InstrStream.flush(); + + // Consume any tabs or spaces at the beginning of the string. + StringRef Str(Instruction); + Str = Str.ltrim(); + FOS << " " << Str << '\n'; + FOS.flush(); + Instruction = ""; + + ++IID; + } } } } // namespace mca diff --git a/llvm/tools/llvm-mca/include/SourceMgr.h b/llvm/tools/llvm-mca/include/SourceMgr.h index 894128363605..e7cd358afd46 100644 --- a/llvm/tools/llvm-mca/include/SourceMgr.h +++ b/llvm/tools/llvm-mca/include/SourceMgr.h @@ -27,7 +27,7 @@ typedef std::pair SourceRef; class SourceMgr { llvm::ArrayRef Sequence; unsigned Current; - unsigned Iterations; + const unsigned Iterations; static const unsigned DefaultIterations = 100; public: @@ -35,27 +35,19 @@ public: : 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(); } - llvm::ArrayRef getSequence() const { return Sequence; } - - bool hasNext() const { return Current < (Iterations * size()); } - void updateNext() { Current++; } + bool hasNext() const { return Current < (Iterations * Sequence.size()); } + void updateNext() { ++Current; } const SourceRef peekNext() const { assert(hasNext() && "Already at end of sequence!"); - unsigned Index = getCurrentInstructionIndex(); - return SourceRef(Current, Sequence[Index]); - } - - unsigned getCurrentInstructionIndex() const { - return Current % Sequence.size(); + return SourceRef(Current, Sequence[Current % Sequence.size()]); } - const llvm::MCInst &getMCInstFromIndex(unsigned Index) const { - return Sequence[Index % size()]; - } + using const_iterator = llvm::ArrayRef::const_iterator; + const_iterator begin() const { return Sequence.begin(); } + const_iterator end() const { return Sequence.end(); } bool isEmpty() const { return size() == 0; } }; -- 2.34.1