1 //===----- ScoreboardHazardRecognizer.cpp - Scheduler Support -------------===//
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 //===----------------------------------------------------------------------===//
10 // This file implements the ScoreboardHazardRecognizer class, which
11 // encapsultes hazard-avoidance heuristics for scheduling, based on the
12 // scheduling itineraries specified for the target.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
17 #include "llvm/CodeGen/ScheduleDAG.h"
18 #include "llvm/MC/MCInstrItineraries.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetInstrInfo.h"
26 #define DEBUG_TYPE DebugType
28 ScoreboardHazardRecognizer::ScoreboardHazardRecognizer(
29 const InstrItineraryData *II, const ScheduleDAG *SchedDAG,
30 const char *ParentDebugType)
31 : ScheduleHazardRecognizer(), DebugType(ParentDebugType), ItinData(II),
32 DAG(SchedDAG), IssueWidth(0), IssueCount(0) {
34 // Determine the maximum depth of any itinerary. This determines the depth of
35 // the scoreboard. We always make the scoreboard at least 1 cycle deep to
36 // avoid dealing with the boundary condition.
37 unsigned ScoreboardDepth = 1;
38 if (ItinData && !ItinData->isEmpty()) {
39 for (unsigned idx = 0; ; ++idx) {
40 if (ItinData->isEndMarker(idx))
43 const InstrStage *IS = ItinData->beginStage(idx);
44 const InstrStage *E = ItinData->endStage(idx);
45 unsigned CurCycle = 0;
46 unsigned ItinDepth = 0;
47 for (; IS != E; ++IS) {
48 unsigned StageDepth = CurCycle + IS->getCycles();
49 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
50 CurCycle += IS->getNextCycles();
53 // Find the next power-of-2 >= ItinDepth
54 while (ItinDepth > ScoreboardDepth) {
56 // Don't set MaxLookAhead until we find at least one nonzero stage.
57 // This way, an itinerary with no stages has MaxLookAhead==0, which
58 // completely bypasses the scoreboard hazard logic.
59 MaxLookAhead = ScoreboardDepth;
64 ReservedScoreboard.reset(ScoreboardDepth);
65 RequiredScoreboard.reset(ScoreboardDepth);
67 // If MaxLookAhead is not set above, then we are not enabled.
69 DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
71 // A nonempty itinerary must have a SchedModel.
72 IssueWidth = ItinData->SchedModel.IssueWidth;
73 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
74 << ScoreboardDepth << '\n');
78 void ScoreboardHazardRecognizer::Reset() {
80 RequiredScoreboard.reset();
81 ReservedScoreboard.reset();
84 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
85 LLVM_DUMP_METHOD void ScoreboardHazardRecognizer::Scoreboard::dump() const {
86 dbgs() << "Scoreboard:\n";
88 unsigned last = Depth - 1;
89 while ((last > 0) && ((*this)[last] == 0))
92 for (unsigned i = 0; i <= last; i++) {
93 unsigned FUs = (*this)[i];
95 for (int j = 31; j >= 0; j--)
96 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
102 bool ScoreboardHazardRecognizer::atIssueLimit() const {
106 return IssueCount == IssueWidth;
109 ScheduleHazardRecognizer::HazardType
110 ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
111 if (!ItinData || ItinData->isEmpty())
114 // Note that stalls will be negative for bottom-up scheduling.
117 // Use the itinerary for the underlying instruction to check for
118 // free FU's in the scoreboard at the appropriate future cycles.
120 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
122 // Don't check hazards for non-machineinstr Nodes.
125 unsigned idx = MCID->getSchedClass();
126 for (const InstrStage *IS = ItinData->beginStage(idx),
127 *E = ItinData->endStage(idx); IS != E; ++IS) {
128 // We must find one of the stage's units free for every cycle the
129 // stage is occupied. FIXME it would be more accurate to find the
130 // same unit free in all the cycles.
131 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
132 int StageCycle = cycle + (int)i;
136 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
137 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
138 "Scoreboard depth exceeded!");
139 // This stage was stalled beyond pipeline depth, so cannot conflict.
143 unsigned freeUnits = IS->getUnits();
144 switch (IS->getReservationKind()) {
145 case InstrStage::Required:
146 // Required FUs conflict with both reserved and required ones
147 freeUnits &= ~ReservedScoreboard[StageCycle];
149 case InstrStage::Reserved:
150 // Reserved FUs can conflict only with required ones.
151 freeUnits &= ~RequiredScoreboard[StageCycle];
156 DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
157 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
158 DEBUG(DAG->dumpNode(SU));
163 // Advance the cycle to the next stage.
164 cycle += IS->getNextCycles();
170 void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
171 if (!ItinData || ItinData->isEmpty())
174 // Use the itinerary for the underlying instruction to reserve FU's
175 // in the scoreboard at the appropriate future cycles.
176 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
177 assert(MCID && "The scheduler must filter non-machineinstrs");
178 if (DAG->TII->isZeroCost(MCID->Opcode))
185 unsigned idx = MCID->getSchedClass();
186 for (const InstrStage *IS = ItinData->beginStage(idx),
187 *E = ItinData->endStage(idx); IS != E; ++IS) {
188 // We must reserve one of the stage's units for every cycle the
189 // stage is occupied. FIXME it would be more accurate to reserve
190 // the same unit free in all the cycles.
191 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
192 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
193 "Scoreboard depth exceeded!");
195 unsigned freeUnits = IS->getUnits();
196 switch (IS->getReservationKind()) {
197 case InstrStage::Required:
198 // Required FUs conflict with both reserved and required ones
199 freeUnits &= ~ReservedScoreboard[cycle + i];
201 case InstrStage::Reserved:
202 // Reserved FUs can conflict only with required ones.
203 freeUnits &= ~RequiredScoreboard[cycle + i];
207 // reduce to a single unit
208 unsigned freeUnit = 0;
210 freeUnit = freeUnits;
211 freeUnits = freeUnit & (freeUnit - 1);
214 if (IS->getReservationKind() == InstrStage::Required)
215 RequiredScoreboard[cycle + i] |= freeUnit;
217 ReservedScoreboard[cycle + i] |= freeUnit;
220 // Advance the cycle to the next stage.
221 cycle += IS->getNextCycles();
224 DEBUG(ReservedScoreboard.dump());
225 DEBUG(RequiredScoreboard.dump());
228 void ScoreboardHazardRecognizer::AdvanceCycle() {
230 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
231 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
234 void ScoreboardHazardRecognizer::RecedeCycle() {
236 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
237 ReservedScoreboard.recede();
238 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
239 RequiredScoreboard.recede();