1 //===---------------------- EntryStage.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file defines the Fetch stage of an instruction pipeline. Its sole
12 /// purpose in life is to produce instructions for the rest of the pipeline.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/MCA/Stages/EntryStage.h"
17 #include "llvm/MCA/Instruction.h"
22 bool EntryStage::hasWorkToComplete() const { return CurrentInstruction; }
24 bool EntryStage::isAvailable(const InstRef & /* unused */) const {
25 if (CurrentInstruction)
26 return checkNextStage(CurrentInstruction);
30 void EntryStage::getNextInstruction() {
31 assert(!CurrentInstruction && "There is already an instruction to process!");
34 SourceRef SR = SM.peekNext();
35 std::unique_ptr<Instruction> Inst = llvm::make_unique<Instruction>(SR.second);
36 CurrentInstruction = InstRef(SR.first, Inst.get());
37 Instructions.emplace_back(std::move(Inst));
41 llvm::Error EntryStage::execute(InstRef & /*unused */) {
42 assert(CurrentInstruction && "There is no instruction to process!");
43 if (llvm::Error Val = moveToTheNextStage(CurrentInstruction))
46 // Move the program counter.
47 CurrentInstruction.invalidate();
49 return llvm::ErrorSuccess();
52 llvm::Error EntryStage::cycleStart() {
53 if (!CurrentInstruction)
55 return llvm::ErrorSuccess();
58 llvm::Error EntryStage::cycleEnd() {
59 // Find the first instruction which hasn't been retired.
60 auto Range = make_range(&Instructions[NumRetired], Instructions.end());
61 auto It = find_if(Range, [](const std::unique_ptr<Instruction> &I) {
62 return !I->isRetired();
65 NumRetired = std::distance(Instructions.begin(), It);
66 // Erase instructions up to the first that hasn't been retired.
67 if ((NumRetired * 2) >= Instructions.size()) {
68 Instructions.erase(Instructions.begin(), It);
72 return llvm::ErrorSuccess();