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/CodeGen/TargetInstrInfo.h"
19 #include "llvm/MC/MCInstrDesc.h"
20 #include "llvm/MC/MCInstrItineraries.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
28 #define DEBUG_TYPE DebugType
30 ScoreboardHazardRecognizer::ScoreboardHazardRecognizer(
31 const InstrItineraryData *II, const ScheduleDAG *SchedDAG,
32 const char *ParentDebugType)
33 : ScheduleHazardRecognizer(), DebugType(ParentDebugType), ItinData(II),
36 // Determine the maximum depth of any itinerary. This determines the depth of
37 // the scoreboard. We always make the scoreboard at least 1 cycle deep to
38 // avoid dealing with the boundary condition.
39 unsigned ScoreboardDepth = 1;
40 if (ItinData && !ItinData->isEmpty()) {
41 for (unsigned idx = 0; ; ++idx) {
42 if (ItinData->isEndMarker(idx))
45 const InstrStage *IS = ItinData->beginStage(idx);
46 const InstrStage *E = ItinData->endStage(idx);
47 unsigned CurCycle = 0;
48 unsigned ItinDepth = 0;
49 for (; IS != E; ++IS) {
50 unsigned StageDepth = CurCycle + IS->getCycles();
51 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
52 CurCycle += IS->getNextCycles();
55 // Find the next power-of-2 >= ItinDepth
56 while (ItinDepth > ScoreboardDepth) {
58 // Don't set MaxLookAhead until we find at least one nonzero stage.
59 // This way, an itinerary with no stages has MaxLookAhead==0, which
60 // completely bypasses the scoreboard hazard logic.
61 MaxLookAhead = ScoreboardDepth;
66 ReservedScoreboard.reset(ScoreboardDepth);
67 RequiredScoreboard.reset(ScoreboardDepth);
69 // If MaxLookAhead is not set above, then we are not enabled.
71 DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
73 // A nonempty itinerary must have a SchedModel.
74 IssueWidth = ItinData->SchedModel.IssueWidth;
75 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
76 << ScoreboardDepth << '\n');
80 void ScoreboardHazardRecognizer::Reset() {
82 RequiredScoreboard.reset();
83 ReservedScoreboard.reset();
86 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
87 LLVM_DUMP_METHOD void ScoreboardHazardRecognizer::Scoreboard::dump() const {
88 dbgs() << "Scoreboard:\n";
90 unsigned last = Depth - 1;
91 while ((last > 0) && ((*this)[last] == 0))
94 for (unsigned i = 0; i <= last; i++) {
95 unsigned FUs = (*this)[i];
97 for (int j = 31; j >= 0; j--)
98 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
104 bool ScoreboardHazardRecognizer::atIssueLimit() const {
108 return IssueCount == IssueWidth;
111 ScheduleHazardRecognizer::HazardType
112 ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
113 if (!ItinData || ItinData->isEmpty())
116 // Note that stalls will be negative for bottom-up scheduling.
119 // Use the itinerary for the underlying instruction to check for
120 // free FU's in the scoreboard at the appropriate future cycles.
122 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
124 // Don't check hazards for non-machineinstr Nodes.
127 unsigned idx = MCID->getSchedClass();
128 for (const InstrStage *IS = ItinData->beginStage(idx),
129 *E = ItinData->endStage(idx); IS != E; ++IS) {
130 // We must find one of the stage's units free for every cycle the
131 // stage is occupied. FIXME it would be more accurate to find the
132 // same unit free in all the cycles.
133 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
134 int StageCycle = cycle + (int)i;
138 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
139 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
140 "Scoreboard depth exceeded!");
141 // This stage was stalled beyond pipeline depth, so cannot conflict.
145 unsigned freeUnits = IS->getUnits();
146 switch (IS->getReservationKind()) {
147 case InstrStage::Required:
148 // Required FUs conflict with both reserved and required ones
149 freeUnits &= ~ReservedScoreboard[StageCycle];
151 case InstrStage::Reserved:
152 // Reserved FUs can conflict only with required ones.
153 freeUnits &= ~RequiredScoreboard[StageCycle];
158 DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
159 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
160 DEBUG(DAG->dumpNode(SU));
165 // Advance the cycle to the next stage.
166 cycle += IS->getNextCycles();
172 void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
173 if (!ItinData || ItinData->isEmpty())
176 // Use the itinerary for the underlying instruction to reserve FU's
177 // in the scoreboard at the appropriate future cycles.
178 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
179 assert(MCID && "The scheduler must filter non-machineinstrs");
180 if (DAG->TII->isZeroCost(MCID->Opcode))
187 unsigned idx = MCID->getSchedClass();
188 for (const InstrStage *IS = ItinData->beginStage(idx),
189 *E = ItinData->endStage(idx); IS != E; ++IS) {
190 // We must reserve one of the stage's units for every cycle the
191 // stage is occupied. FIXME it would be more accurate to reserve
192 // the same unit free in all the cycles.
193 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
194 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
195 "Scoreboard depth exceeded!");
197 unsigned freeUnits = IS->getUnits();
198 switch (IS->getReservationKind()) {
199 case InstrStage::Required:
200 // Required FUs conflict with both reserved and required ones
201 freeUnits &= ~ReservedScoreboard[cycle + i];
203 case InstrStage::Reserved:
204 // Reserved FUs can conflict only with required ones.
205 freeUnits &= ~RequiredScoreboard[cycle + i];
209 // reduce to a single unit
210 unsigned freeUnit = 0;
212 freeUnit = freeUnits;
213 freeUnits = freeUnit & (freeUnit - 1);
216 if (IS->getReservationKind() == InstrStage::Required)
217 RequiredScoreboard[cycle + i] |= freeUnit;
219 ReservedScoreboard[cycle + i] |= freeUnit;
222 // Advance the cycle to the next stage.
223 cycle += IS->getNextCycles();
226 DEBUG(ReservedScoreboard.dump());
227 DEBUG(RequiredScoreboard.dump());
230 void ScoreboardHazardRecognizer::AdvanceCycle() {
232 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
233 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
236 void ScoreboardHazardRecognizer::RecedeCycle() {
238 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
239 ReservedScoreboard.recede();
240 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
241 RequiredScoreboard.recede();