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 #define DEBUG_TYPE ::llvm::ScoreboardHazardRecognizer::DebugType
17 #include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
18 #include "llvm/CodeGen/ScheduleDAG.h"
19 #include "llvm/MC/MCInstrItineraries.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetInstrInfo.h"
28 const char *ScoreboardHazardRecognizer::DebugType = "";
31 ScoreboardHazardRecognizer::
32 ScoreboardHazardRecognizer(const InstrItineraryData *II,
33 const ScheduleDAG *SchedDAG,
34 const char *ParentDebugType) :
35 ScheduleHazardRecognizer(), ItinData(II), DAG(SchedDAG), IssueWidth(0),
39 DebugType = ParentDebugType;
42 // Determine the maximum depth of any itinerary. This determines the depth of
43 // the scoreboard. We always make the scoreboard at least 1 cycle deep to
44 // avoid dealing with the boundary condition.
45 unsigned ScoreboardDepth = 1;
46 if (ItinData && !ItinData->isEmpty()) {
47 for (unsigned idx = 0; ; ++idx) {
48 if (ItinData->isEndMarker(idx))
51 const InstrStage *IS = ItinData->beginStage(idx);
52 const InstrStage *E = ItinData->endStage(idx);
53 unsigned CurCycle = 0;
54 unsigned ItinDepth = 0;
55 for (; IS != E; ++IS) {
56 unsigned StageDepth = CurCycle + IS->getCycles();
57 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
58 CurCycle += IS->getNextCycles();
61 // Find the next power-of-2 >= ItinDepth
62 while (ItinDepth > ScoreboardDepth) {
64 // Don't set MaxLookAhead until we find at least one nonzero stage.
65 // This way, an itinerary with no stages has MaxLookAhead==0, which
66 // completely bypasses the scoreboard hazard logic.
67 MaxLookAhead = ScoreboardDepth;
72 ReservedScoreboard.reset(ScoreboardDepth);
73 RequiredScoreboard.reset(ScoreboardDepth);
76 DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
78 IssueWidth = ItinData->Props.IssueWidth;
79 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
80 << ScoreboardDepth << '\n');
84 void ScoreboardHazardRecognizer::Reset() {
86 RequiredScoreboard.reset();
87 ReservedScoreboard.reset();
90 void ScoreboardHazardRecognizer::Scoreboard::dump() const {
91 dbgs() << "Scoreboard:\n";
93 unsigned last = Depth - 1;
94 while ((last > 0) && ((*this)[last] == 0))
97 for (unsigned i = 0; i <= last; i++) {
98 unsigned FUs = (*this)[i];
100 for (int j = 31; j >= 0; j--)
101 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
106 bool ScoreboardHazardRecognizer::atIssueLimit() const {
110 return IssueCount == IssueWidth;
113 ScheduleHazardRecognizer::HazardType
114 ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
115 if (!ItinData || ItinData->isEmpty())
118 // Note that stalls will be negative for bottom-up scheduling.
121 // Use the itinerary for the underlying instruction to check for
122 // free FU's in the scoreboard at the appropriate future cycles.
124 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
126 // Don't check hazards for non-machineinstr Nodes.
129 unsigned idx = MCID->getSchedClass();
130 for (const InstrStage *IS = ItinData->beginStage(idx),
131 *E = ItinData->endStage(idx); IS != E; ++IS) {
132 // We must find one of the stage's units free for every cycle the
133 // stage is occupied. FIXME it would be more accurate to find the
134 // same unit free in all the cycles.
135 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
136 int StageCycle = cycle + (int)i;
140 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
141 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
142 "Scoreboard depth exceeded!");
143 // This stage was stalled beyond pipeline depth, so cannot conflict.
147 unsigned freeUnits = IS->getUnits();
148 switch (IS->getReservationKind()) {
149 case InstrStage::Required:
150 // Required FUs conflict with both reserved and required ones
151 freeUnits &= ~ReservedScoreboard[StageCycle];
153 case InstrStage::Reserved:
154 // Reserved FUs can conflict only with required ones.
155 freeUnits &= ~RequiredScoreboard[StageCycle];
160 DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
161 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
162 DEBUG(DAG->dumpNode(SU));
167 // Advance the cycle to the next stage.
168 cycle += IS->getNextCycles();
174 void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
175 if (!ItinData || ItinData->isEmpty())
178 // Use the itinerary for the underlying instruction to reserve FU's
179 // in the scoreboard at the appropriate future cycles.
180 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
181 assert(MCID && "The scheduler must filter non-machineinstrs");
182 if (DAG->TII->isZeroCost(MCID->Opcode))
189 unsigned idx = MCID->getSchedClass();
190 for (const InstrStage *IS = ItinData->beginStage(idx),
191 *E = ItinData->endStage(idx); IS != E; ++IS) {
192 // We must reserve one of the stage's units for every cycle the
193 // stage is occupied. FIXME it would be more accurate to reserve
194 // the same unit free in all the cycles.
195 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
196 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
197 "Scoreboard depth exceeded!");
199 unsigned freeUnits = IS->getUnits();
200 switch (IS->getReservationKind()) {
201 case InstrStage::Required:
202 // Required FUs conflict with both reserved and required ones
203 freeUnits &= ~ReservedScoreboard[cycle + i];
205 case InstrStage::Reserved:
206 // Reserved FUs can conflict only with required ones.
207 freeUnits &= ~RequiredScoreboard[cycle + i];
211 // reduce to a single unit
212 unsigned freeUnit = 0;
214 freeUnit = freeUnits;
215 freeUnits = freeUnit & (freeUnit - 1);
218 if (IS->getReservationKind() == InstrStage::Required)
219 RequiredScoreboard[cycle + i] |= freeUnit;
221 ReservedScoreboard[cycle + i] |= freeUnit;
224 // Advance the cycle to the next stage.
225 cycle += IS->getNextCycles();
228 DEBUG(ReservedScoreboard.dump());
229 DEBUG(RequiredScoreboard.dump());
232 void ScoreboardHazardRecognizer::AdvanceCycle() {
234 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
235 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
238 void ScoreboardHazardRecognizer::RecedeCycle() {
240 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
241 ReservedScoreboard.recede();
242 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
243 RequiredScoreboard.recede();