[llvm-mca] Simplify the logic in FetchStage. NFCI
authorAndrea Di Biagio <Andrea_DiBiagio@sn.scee.net>
Wed, 24 Oct 2018 19:37:45 +0000 (19:37 +0000)
committerAndrea Di Biagio <Andrea_DiBiagio@sn.scee.net>
Wed, 24 Oct 2018 19:37:45 +0000 (19:37 +0000)
Only method 'getNextInstruction()' needs to interact with the SourceMgr.

llvm-svn: 345185

llvm/tools/llvm-mca/include/Stages/FetchStage.h
llvm/tools/llvm-mca/lib/Pipeline.cpp
llvm/tools/llvm-mca/lib/Stages/FetchStage.cpp

index 10a89c9..45e30e1 100644 (file)
@@ -24,7 +24,7 @@
 namespace mca {
 
 class FetchStage final : public Stage {
-  std::unique_ptr<Instruction> CurrentInstruction;
+  InstRef CurrentInstruction;
   using InstMap = std::map<unsigned, std::unique_ptr<Instruction>>;
   InstMap Instructions;
   InstrBuilder &IB;
index 2d9aa6b..ad49522 100644 (file)
@@ -39,13 +39,14 @@ bool Pipeline::hasWorkToProcess() {
 Error Pipeline::run() {
   assert(!Stages.empty() && "Unexpected empty pipeline found!");
 
-  while (hasWorkToProcess()) {
+  do {
     notifyCycleBegin();
     if (Error Err = runCycle())
       return Err;
     notifyCycleEnd();
     ++Cycles;
-  }
+  } while (hasWorkToProcess());
+
   return ErrorSuccess();
 }
 
index 8bd0bd9..e607db9 100644 (file)
 namespace mca {
 
 bool FetchStage::hasWorkToComplete() const {
-  return CurrentInstruction.get() || SM.hasNext();
+  return CurrentInstruction.isValid();
 }
 
 bool FetchStage::isAvailable(const InstRef & /* unused */) const {
-  if (!CurrentInstruction)
-    return false;
-  assert(SM.hasNext() && "Unexpected internal state!");
-  const SourceRef SR = SM.peekNext();
-  InstRef IR(SR.first, CurrentInstruction.get());
-  return checkNextStage(IR);
+  if (CurrentInstruction.isValid())
+    return checkNextStage(CurrentInstruction);
+  return false;
 }
 
 llvm::Error FetchStage::getNextInstruction() {
-  assert(!CurrentInstruction && "There is already an instruction to process!");
+  assert(!CurrentInstruction.isValid() &&
+         "There is already an instruction to process!");
   if (!SM.hasNext())
     return llvm::ErrorSuccess();
   const SourceRef SR = SM.peekNext();
@@ -39,28 +37,25 @@ llvm::Error FetchStage::getNextInstruction() {
       IB.createInstruction(SR.second);
   if (!InstOrErr)
     return InstOrErr.takeError();
-  CurrentInstruction = std::move(InstOrErr.get());
+  std::unique_ptr<Instruction> Inst = std::move(InstOrErr.get());
+  CurrentInstruction = InstRef(SR.first, Inst.get());
+  Instructions[SR.first] = std::move(Inst);
+  SM.updateNext();
   return llvm::ErrorSuccess();
 }
 
 llvm::Error FetchStage::execute(InstRef & /*unused */) {
-  assert(CurrentInstruction && "There is no instruction to process!");
-  const SourceRef SR = SM.peekNext();
-  InstRef IR(SR.first, CurrentInstruction.get());
-  assert(checkNextStage(IR) && "Invalid fetch!");
-
-  Instructions[IR.getSourceIndex()] = std::move(CurrentInstruction);
-  if (llvm::Error Val = moveToTheNextStage(IR))
+  assert(CurrentInstruction.isValid() && "There is no instruction to process!");
+  if (llvm::Error Val = moveToTheNextStage(CurrentInstruction))
     return Val;
 
-  SM.updateNext();
-
   // Move the program counter.
+  CurrentInstruction.invalidate();
   return getNextInstruction();
 }
 
 llvm::Error FetchStage::cycleStart() {
-  if (!CurrentInstruction)
+  if (!CurrentInstruction.isValid())
     return getNextInstruction();
   return llvm::ErrorSuccess();
 }