LLVM  16.0.0git
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) || !SM.isEnd();
23 }
24 
25 bool EntryStage::isAvailable(const InstRef & /* unused */) const {
26  if (CurrentInstruction)
27  return checkNextStage(CurrentInstruction);
28  return false;
29 }
30 
31 Error EntryStage::getNextInstruction() {
32  assert(!CurrentInstruction && "There is already an instruction to process!");
33  if (!SM.hasNext()) {
34  if (!SM.isEnd())
35  return llvm::make_error<InstStreamPause>();
36  else
37  return llvm::ErrorSuccess();
38  }
39  SourceRef SR = SM.peekNext();
40  std::unique_ptr<Instruction> Inst = std::make_unique<Instruction>(SR.second);
41  CurrentInstruction = InstRef(SR.first, Inst.get());
42  Instructions.emplace_back(std::move(Inst));
43  SM.updateNext();
44  return llvm::ErrorSuccess();
45 }
46 
48  assert(CurrentInstruction && "There is no instruction to process!");
49  if (llvm::Error Val = moveToTheNextStage(CurrentInstruction))
50  return Val;
51 
52  // Move the program counter.
53  CurrentInstruction.invalidate();
54  return getNextInstruction();
55 }
56 
58  if (!CurrentInstruction)
59  return getNextInstruction();
60  return llvm::ErrorSuccess();
61 }
62 
64  assert(!CurrentInstruction);
65  return getNextInstruction();
66 }
67 
69  // Find the first instruction which hasn't been retired.
70  auto Range = make_range(&Instructions[NumRetired], Instructions.end());
71  auto It = find_if(Range, [](const std::unique_ptr<Instruction> &I) {
72  return !I->isRetired();
73  });
74 
75  NumRetired = std::distance(Instructions.begin(), It);
76  // Erase instructions up to the first that hasn't been retired.
77  if ((NumRetired * 2) >= Instructions.size()) {
78  Instructions.erase(Instructions.begin(), It);
79  NumRetired = 0;
80  }
81 
82  return llvm::ErrorSuccess();
83 }
84 
85 } // namespace mca
86 } // namespace llvm
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
llvm::mca::EntryStage::cycleStart
Error cycleStart() override
Called once at the start of each cycle.
Definition: EntryStage.cpp:57
llvm::mca::EntryStage::isAvailable
bool isAvailable(const InstRef &IR) const override
Returns true if it can execute IR during this cycle.
Definition: EntryStage.cpp:25
llvm::mca::EntryStage::cycleEnd
Error cycleEnd() override
Called once at the end of each cycle.
Definition: EntryStage.cpp:68
llvm::mca::EntryStage::hasWorkToComplete
bool hasWorkToComplete() const override
Returns true if some instructions are still executing this stage.
Definition: EntryStage.cpp:21
Instruction.h
llvm::mca::SourceMgr::peekNext
virtual SourceRef peekNext() const =0
The next SourceRef.
llvm::mca::EntryStage::cycleResume
Error cycleResume() override
Called after the pipeline is resumed from pausing state.
Definition: EntryStage.cpp:63
llvm::mca::SourceMgr::hasNext
virtual bool hasNext() const =0
Whether there is any SourceRef to inspect / peek next.
llvm::mca::SourceMgr::updateNext
virtual void updateNext()=0
Advance to the next SourceRef.
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::ErrorSuccess
Subclass of Error for the sole purpose of identifying the success path in the type system.
Definition: Error.h:327
I
#define I(x, y, z)
Definition: MD5.cpp:58
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::mca::InstRef
An InstRef contains both a SourceMgr index and Instruction pair.
Definition: Instruction.h:720
llvm::mca::Stage::checkNextStage
bool checkNextStage(const InstRef &IR) const
Definition: Stage.h:65
llvm::mca::SourceMgr::isEnd
virtual bool isEnd() const =0
Whether the instruction stream has eneded.
llvm::find_if
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1761
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::mca::SourceRef
std::pair< unsigned, const Instruction & > SourceRef
Definition: SourceMgr.h:25
llvm::mca::InstRef::invalidate
void invalidate()
Invalidate this reference.
Definition: Instruction.h:741
llvm::mca::Stage::moveToTheNextStage
Error moveToTheNextStage(InstRef &IR)
Called when an instruction is ready to move the next pipeline stage.
Definition: Stage.h:73
EntryStage.h
llvm::mca::EntryStage::execute
Error execute(InstRef &IR) override
The primary action that this stage performs on instruction IR.
Definition: EntryStage.cpp:47