[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
diff --git a/llvm/tools/llvm-mca/Views/InstructionInfoView.cpp b/llvm/tools/llvm-mca/Views/InstructionInfoView.cpp
index a2e3001..0a97e56 100644
--- a/llvm/tools/llvm-mca/Views/InstructionInfoView.cpp
+++ b/llvm/tools/llvm-mca/Views/InstructionInfoView.cpp
@@ -22,7 +22,6 @@
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 @@
<< "[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 bba1e70..17c8012 100644
--- a/llvm/tools/llvm-mca/Views/ResourcePressureView.cpp
+++ b/llvm/tools/llvm-mca/Views/ResourcePressureView.cpp
@@ -148,13 +148,15 @@
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 @@
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 1ad7271..d802d42 100644
--- a/llvm/tools/llvm-mca/Views/TimelineView.cpp
+++ b/llvm/tools/llvm-mca/Views/TimelineView.cpp
@@ -177,11 +177,10 @@
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 @@
FOS << " " << Str << '\n';
FOS.flush();
Instruction = "";
+
+ ++IID;
}
}
@@ -266,25 +267,29 @@
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 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 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();
+ 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 = "";
+ // 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 8941283..e7cd358 100644
--- a/llvm/tools/llvm-mca/include/SourceMgr.h
+++ b/llvm/tools/llvm-mca/include/SourceMgr.h
@@ -27,7 +27,7 @@
class SourceMgr {
llvm::ArrayRef<llvm::MCInst> Sequence;
unsigned Current;
- unsigned Iterations;
+ const unsigned Iterations;
static const unsigned DefaultIterations = 100;
public:
@@ -35,27 +35,19 @@
: 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<llvm::MCInst> 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]);
+ return SourceRef(Current, Sequence[Current % Sequence.size()]);
}
- unsigned getCurrentInstructionIndex() const {
- return Current % Sequence.size();
- }
-
- const llvm::MCInst &getMCInstFromIndex(unsigned Index) const {
- return Sequence[Index % size()];
- }
+ using const_iterator = llvm::ArrayRef<llvm::MCInst>::const_iterator;
+ const_iterator begin() const { return Sequence.begin(); }
+ const_iterator end() const { return Sequence.end(); }
bool isEmpty() const { return size() == 0; }
};