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 ::llvm::ScoreboardHazardRecognizer::DebugType
29 const char *ScoreboardHazardRecognizer::DebugType = "";
32 ScoreboardHazardRecognizer::
33 ScoreboardHazardRecognizer(const InstrItineraryData *II,
34 const ScheduleDAG *SchedDAG,
35 const char *ParentDebugType) :
36 ScheduleHazardRecognizer(), ItinData(II), DAG(SchedDAG), IssueWidth(0),
40 DebugType = ParentDebugType;
43 // Determine the maximum depth of any itinerary. This determines the depth of
44 // the scoreboard. We always make the scoreboard at least 1 cycle deep to
45 // avoid dealing with the boundary condition.
46 unsigned ScoreboardDepth = 1;
47 if (ItinData && !ItinData->isEmpty()) {
48 for (unsigned idx = 0; ; ++idx) {
49 if (ItinData->isEndMarker(idx))
52 const InstrStage *IS = ItinData->beginStage(idx);
53 const InstrStage *E = ItinData->endStage(idx);
54 unsigned CurCycle = 0;
55 unsigned ItinDepth = 0;
56 for (; IS != E; ++IS) {
57 unsigned StageDepth = CurCycle + IS->getCycles();
58 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
59 CurCycle += IS->getNextCycles();
62 // Find the next power-of-2 >= ItinDepth
63 while (ItinDepth > ScoreboardDepth) {
65 // Don't set MaxLookAhead until we find at least one nonzero stage.
66 // This way, an itinerary with no stages has MaxLookAhead==0, which
67 // completely bypasses the scoreboard hazard logic.
68 MaxLookAhead = ScoreboardDepth;
73 ReservedScoreboard.reset(ScoreboardDepth);
74 RequiredScoreboard.reset(ScoreboardDepth);
76 // If MaxLookAhead is not set above, then we are not enabled.
78 DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
80 // A nonempty itinerary must have a SchedModel.
81 IssueWidth = ItinData->SchedModel->IssueWidth;
82 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
83 << ScoreboardDepth << '\n');
87 void ScoreboardHazardRecognizer::Reset() {
89 RequiredScoreboard.reset();
90 ReservedScoreboard.reset();
93 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
94 void ScoreboardHazardRecognizer::Scoreboard::dump() const {
95 dbgs() << "Scoreboard:\n";
97 unsigned last = Depth - 1;
98 while ((last > 0) && ((*this)[last] == 0))
101 for (unsigned i = 0; i <= last; i++) {
102 unsigned FUs = (*this)[i];
104 for (int j = 31; j >= 0; j--)
105 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
111 bool ScoreboardHazardRecognizer::atIssueLimit() const {
115 return IssueCount == IssueWidth;
118 ScheduleHazardRecognizer::HazardType
119 ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
120 if (!ItinData || ItinData->isEmpty())
123 // Note that stalls will be negative for bottom-up scheduling.
126 // Use the itinerary for the underlying instruction to check for
127 // free FU's in the scoreboard at the appropriate future cycles.
129 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
131 // Don't check hazards for non-machineinstr Nodes.
134 unsigned idx = MCID->getSchedClass();
135 for (const InstrStage *IS = ItinData->beginStage(idx),
136 *E = ItinData->endStage(idx); IS != E; ++IS) {
137 // We must find one of the stage's units free for every cycle the
138 // stage is occupied. FIXME it would be more accurate to find the
139 // same unit free in all the cycles.
140 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
141 int StageCycle = cycle + (int)i;
145 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
146 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
147 "Scoreboard depth exceeded!");
148 // This stage was stalled beyond pipeline depth, so cannot conflict.
152 unsigned freeUnits = IS->getUnits();
153 switch (IS->getReservationKind()) {
154 case InstrStage::Required:
155 // Required FUs conflict with both reserved and required ones
156 freeUnits &= ~ReservedScoreboard[StageCycle];
158 case InstrStage::Reserved:
159 // Reserved FUs can conflict only with required ones.
160 freeUnits &= ~RequiredScoreboard[StageCycle];
165 DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
166 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
167 DEBUG(DAG->dumpNode(SU));
172 // Advance the cycle to the next stage.
173 cycle += IS->getNextCycles();
179 void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
180 if (!ItinData || ItinData->isEmpty())
183 // Use the itinerary for the underlying instruction to reserve FU's
184 // in the scoreboard at the appropriate future cycles.
185 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
186 assert(MCID && "The scheduler must filter non-machineinstrs");
187 if (DAG->TII->isZeroCost(MCID->Opcode))
194 unsigned idx = MCID->getSchedClass();
195 for (const InstrStage *IS = ItinData->beginStage(idx),
196 *E = ItinData->endStage(idx); IS != E; ++IS) {
197 // We must reserve one of the stage's units for every cycle the
198 // stage is occupied. FIXME it would be more accurate to reserve
199 // the same unit free in all the cycles.
200 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
201 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
202 "Scoreboard depth exceeded!");
204 unsigned freeUnits = IS->getUnits();
205 switch (IS->getReservationKind()) {
206 case InstrStage::Required:
207 // Required FUs conflict with both reserved and required ones
208 freeUnits &= ~ReservedScoreboard[cycle + i];
210 case InstrStage::Reserved:
211 // Reserved FUs can conflict only with required ones.
212 freeUnits &= ~RequiredScoreboard[cycle + i];
216 // reduce to a single unit
217 unsigned freeUnit = 0;
219 freeUnit = freeUnits;
220 freeUnits = freeUnit & (freeUnit - 1);
223 if (IS->getReservationKind() == InstrStage::Required)
224 RequiredScoreboard[cycle + i] |= freeUnit;
226 ReservedScoreboard[cycle + i] |= freeUnit;
229 // Advance the cycle to the next stage.
230 cycle += IS->getNextCycles();
233 DEBUG(ReservedScoreboard.dump());
234 DEBUG(RequiredScoreboard.dump());
237 void ScoreboardHazardRecognizer::AdvanceCycle() {
239 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
240 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
243 void ScoreboardHazardRecognizer::RecedeCycle() {
245 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
246 ReservedScoreboard.recede();
247 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
248 RequiredScoreboard.recede();