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 | |
15 | #include "llvm/MCA/Stages/EntryStage.h" |
16 | #include "llvm/MCA/Instruction.h" |
17 | |
18 | namespace llvm { |
19 | namespace mca { |
20 | |
21 | bool EntryStage::hasWorkToComplete() const { |
22 | return static_cast<bool>(CurrentInstruction) || !SM.isEnd(); |
23 | } |
24 | |
25 | bool EntryStage::isAvailable(const InstRef & /* unused */) const { |
26 | if (CurrentInstruction) |
27 | return checkNextStage(IR: 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>(args: SR.second); |
41 | CurrentInstruction = InstRef(SR.first, Inst.get()); |
42 | Instructions.emplace_back(Args: std::move(Inst)); |
43 | SM.updateNext(); |
44 | return llvm::ErrorSuccess(); |
45 | } |
46 | |
47 | llvm::Error EntryStage::execute(InstRef & /*unused */) { |
48 | assert(CurrentInstruction && "There is no instruction to process!"); |
49 | if (llvm::Error Val = moveToTheNextStage(IR&: CurrentInstruction)) |
50 | return Val; |
51 | |
52 | // Move the program counter. |
53 | CurrentInstruction.invalidate(); |
54 | return getNextInstruction(); |
55 | } |
56 | |
57 | llvm::Error EntryStage::cycleStart() { |
58 | if (!CurrentInstruction) |
59 | return getNextInstruction(); |
60 | return llvm::ErrorSuccess(); |
61 | } |
62 | |
63 | llvm::Error EntryStage::cycleResume() { |
64 | assert(!CurrentInstruction); |
65 | return getNextInstruction(); |
66 | } |
67 | |
68 | llvm::Error EntryStage::cycleEnd() { |
69 | // Find the first instruction which hasn't been retired. |
70 | auto Range = drop_begin(RangeOrContainer&: Instructions, N: NumRetired); |
71 | auto It = find_if(Range, P: [](const std::unique_ptr<Instruction> &I) { |
72 | return !I->isRetired(); |
73 | }); |
74 | |
75 | NumRetired = std::distance(first: Instructions.begin(), last: It); |
76 | // Erase instructions up to the first that hasn't been retired. |
77 | if ((NumRetired * 2) >= Instructions.size()) { |
78 | Instructions.erase(CS: Instructions.begin(), CE: It); |
79 | NumRetired = 0; |
80 | } |
81 | |
82 | return llvm::ErrorSuccess(); |
83 | } |
84 | |
85 | } // namespace mca |
86 | } // namespace llvm |
87 |