1 //===----------------------- DispatchStage.h --------------------*- 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 models the dispatch component of an instruction pipeline.
13 /// The DispatchStage is responsible for updating instruction dependencies
14 /// and communicating to the simulated instruction scheduler that an instruction
15 /// is ready to be scheduled for execution.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_TOOLS_LLVM_MCA_DISPATCH_STAGE_H
20 #define LLVM_TOOLS_LLVM_MCA_DISPATCH_STAGE_H
22 #include "HWEventListener.h"
23 #include "Instruction.h"
24 #include "RegisterFile.h"
25 #include "RetireControlUnit.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCSubtargetInfo.h"
34 // Implements the hardware dispatch logic.
36 // This class is responsible for the dispatch stage, in which instructions are
37 // dispatched in groups to the Scheduler. An instruction can be dispatched if
38 // the following conditions are met:
39 // 1) There are enough entries in the reorder buffer (see class
40 // RetireControlUnit) to write the opcodes associated with the instruction.
41 // 2) There are enough physical registers to rename output register operands.
42 // 3) There are enough entries available in the used buffered resource(s).
44 // The number of micro opcodes that can be dispatched in one cycle is limited by
45 // the value of field 'DispatchWidth'. A "dynamic dispatch stall" occurs when
46 // processor resources are not available. Dispatch stall events are counted
47 // during the entire execution of the code, and displayed by the performance
48 // report when flag '-dispatch-stats' is specified.
50 // If the number of micro opcodes exceedes DispatchWidth, then the instruction
51 // is dispatched in multiple cycles.
52 class DispatchStage : public Stage {
53 unsigned DispatchWidth;
54 unsigned AvailableEntries;
56 const llvm::MCSubtargetInfo &STI;
57 RetireControlUnit &RCU;
61 bool checkRCU(const InstRef &IR);
62 bool checkPRF(const InstRef &IR);
63 bool checkScheduler(const InstRef &IR);
64 void dispatch(InstRef IR);
65 void updateRAWDependencies(ReadState &RS, const llvm::MCSubtargetInfo &STI);
67 void notifyInstructionDispatched(const InstRef &IR,
68 llvm::ArrayRef<unsigned> UsedPhysRegs);
70 bool isAvailable(unsigned NumEntries) const {
71 return NumEntries <= AvailableEntries || AvailableEntries == DispatchWidth;
74 bool canDispatch(const InstRef &IR) {
75 assert(isAvailable(IR.getInstruction()->getDesc().NumMicroOps));
76 return checkRCU(IR) && checkPRF(IR) && checkScheduler(IR);
79 void collectWrites(llvm::SmallVectorImpl<WriteRef> &Vec,
80 unsigned RegID) const {
81 return PRF.collectWrites(Vec, RegID);
85 DispatchStage(const llvm::MCSubtargetInfo &Subtarget,
86 const llvm::MCRegisterInfo &MRI, unsigned RegisterFileSize,
87 unsigned MaxDispatchWidth, RetireControlUnit &R,
88 RegisterFile &F, Scheduler &Sched)
89 : DispatchWidth(MaxDispatchWidth), AvailableEntries(MaxDispatchWidth),
90 CarryOver(0U), STI(Subtarget), RCU(R), PRF(F), SC(Sched) {}
92 // We can always try to dispatch, so returning false is okay in this case.
93 // The retire stage, which controls the RCU, might have items to complete but
94 // RetireStage::hasWorkToComplete will check for that case.
95 virtual bool hasWorkToComplete() const override final { return false; }
96 virtual void cycleStart() override final;
97 virtual bool execute(InstRef &IR) override final;
98 void notifyDispatchStall(const InstRef &IR, unsigned EventType);
106 #endif // LLVM_TOOLS_LLVM_MCA_DISPATCH_STAGE_H