LLVM  10.0.0svn
EntryStage.cpp
Go to the documentation of this file.
1 //===---------------------- EntryStage.cpp ----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This file defines the Fetch stage of an instruction pipeline. Its sole
11 /// purpose in life is to produce instructions for the rest of the pipeline.
12 ///
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/MCA/Instruction.h"
17 
18 namespace llvm {
19 namespace mca {
20 
22  return static_cast<bool>(CurrentInstruction);
23 }
24 
25 bool EntryStage::isAvailable(const InstRef & /* unused */) const {
26  if (CurrentInstruction)
27  return checkNextStage(CurrentInstruction);
28  return false;
29 }
30 
31 void EntryStage::getNextInstruction() {
32  assert(!CurrentInstruction && "There is already an instruction to process!");
33  if (!SM.hasNext())
34  return;
35  SourceRef SR = SM.peekNext();
36  std::unique_ptr<Instruction> Inst = std::make_unique<Instruction>(SR.second);
37  CurrentInstruction = InstRef(SR.first, Inst.get());
38  Instructions.emplace_back(std::move(Inst));
39  SM.updateNext();
40 }
41 
43  assert(CurrentInstruction && "There is no instruction to process!");
44  if (llvm::Error Val = moveToTheNextStage(CurrentInstruction))
45  return Val;
46 
47  // Move the program counter.
48  CurrentInstruction.invalidate();
49  getNextInstruction();
50  return llvm::ErrorSuccess();
51 }
52 
54  if (!CurrentInstruction)
55  getNextInstruction();
56  return llvm::ErrorSuccess();
57 }
58 
60  // Find the first instruction which hasn't been retired.
61  auto Range = make_range(&Instructions[NumRetired], Instructions.end());
62  auto It = find_if(Range, [](const std::unique_ptr<Instruction> &I) {
63  return !I->isRetired();
64  });
65 
66  NumRetired = std::distance(Instructions.begin(), It);
67  // Erase instructions up to the first that hasn't been retired.
68  if ((NumRetired * 2) >= Instructions.size()) {
69  Instructions.erase(Instructions.begin(), It);
70  NumRetired = 0;
71  }
72 
73  return llvm::ErrorSuccess();
74 }
75 
76 } // namespace mca
77 } // namespace llvm
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool isAvailable(const InstRef &IR) const override
Returns true if it can execute IR during this cycle.
Definition: EntryStage.cpp:25
Subclass of Error for the sole purpose of identifying the success path in the type system...
Definition: Error.h:324
bool checkNextStage(const InstRef &IR) const
Definition: Stage.h:62
bool hasWorkToComplete() const override
Returns true if some instructions are still executing this stage.
Definition: EntryStage.cpp:21
An InstRef contains both a SourceMgr index and Instruction pair.
Definition: Instruction.h:562
This file defines the Entry stage of an instruction pipeline.
bool hasNext() const
Definition: SourceMgr.h:41
void invalidate()
Invalidate this reference.
Definition: Instruction.h:583
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1193
SourceRef peekNext() const
Definition: SourceMgr.h:44
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Error cycleEnd() override
Called once at the end of each cycle.
Definition: EntryStage.cpp:59
Error moveToTheNextStage(InstRef &IR)
Called when an instruction is ready to move the next pipeline stage.
Definition: Stage.h:70
#define I(x, y, z)
Definition: MD5.cpp:58
std::pair< unsigned, const Instruction & > SourceRef
Definition: SourceMgr.h:26
This file defines abstractions used by the Pipeline to model register reads, register writes and inst...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Error execute(InstRef &IR) override
The primary action that this stage performs on instruction IR.
Definition: EntryStage.cpp:42
Error cycleStart() override
Called once at the start of each cycle.
Definition: EntryStage.cpp:53