blob: b00e0cd0998e8edc026a67ed5502a2325df76a25 [file] [log] [blame]
Andrew Trick00067fb2010-12-08 20:04:29 +00001//===----- ScoreboardHazardRecognizer.cpp - Scheduler Support -------------===//
David Goodwin6021b4d2009-08-10 15:55:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Andrew Trick00067fb2010-12-08 20:04:29 +000010// This file implements the ScoreboardHazardRecognizer class, which
11// encapsultes hazard-avoidance heuristics for scheduling, based on the
12// scheduling itineraries specified for the target.
David Goodwin6021b4d2009-08-10 15:55:25 +000013//
14//===----------------------------------------------------------------------===//
15
Andrew Trick10ffc2b2010-12-24 05:03:26 +000016#define DEBUG_TYPE ::llvm::ScoreboardHazardRecognizer::DebugType
Andrew Trick00067fb2010-12-08 20:04:29 +000017#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Evan Cheng078f4ce2010-06-14 21:06:53 +000018#include "llvm/CodeGen/ScheduleDAG.h"
David Goodwin6021b4d2009-08-10 15:55:25 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/ErrorHandling.h"
David Goodwinf20236a2009-08-11 01:44:26 +000021#include "llvm/Support/raw_ostream.h"
David Goodwin6021b4d2009-08-10 15:55:25 +000022#include "llvm/Target/TargetInstrItineraries.h"
23
Bill Wendlinga2c45942009-08-22 20:08:44 +000024using namespace llvm;
David Goodwin6021b4d2009-08-10 15:55:25 +000025
Andrew Trick10ffc2b2010-12-24 05:03:26 +000026#ifndef NDEBUG
27const char *ScoreboardHazardRecognizer::DebugType = "";
28#endif
29
Andrew Trick00067fb2010-12-08 20:04:29 +000030ScoreboardHazardRecognizer::
Andrew Trick10ffc2b2010-12-24 05:03:26 +000031ScoreboardHazardRecognizer(const InstrItineraryData *II,
32 const ScheduleDAG *SchedDAG,
33 const char *ParentDebugType) :
34 ScheduleHazardRecognizer(), ItinData(II), DAG(SchedDAG), IssueWidth(0),
35 IssueCount(0) {
36
37#ifndef NDEBUG
38 DebugType = ParentDebugType;
39#endif
40
David Goodwin6021b4d2009-08-10 15:55:25 +000041 // Determine the maximum depth of any itinerary. This determines the
42 // depth of the scoreboard. We always make the scoreboard at least 1
43 // cycle deep to avoid dealing with the boundary condition.
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000044 unsigned ScoreboardDepth = 1;
Evan Chengbf407072010-09-10 01:29:16 +000045 if (ItinData && !ItinData->isEmpty()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +000046 IssueWidth = ItinData->IssueWidth;
47
David Goodwin6021b4d2009-08-10 15:55:25 +000048 for (unsigned idx = 0; ; ++idx) {
Evan Chengbf407072010-09-10 01:29:16 +000049 if (ItinData->isEndMarker(idx))
David Goodwin6021b4d2009-08-10 15:55:25 +000050 break;
51
Evan Chengbf407072010-09-10 01:29:16 +000052 const InstrStage *IS = ItinData->beginStage(idx);
53 const InstrStage *E = ItinData->endStage(idx);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000054 unsigned CurCycle = 0;
David Goodwin6021b4d2009-08-10 15:55:25 +000055 unsigned ItinDepth = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +000056 for (; IS != E; ++IS) {
57 unsigned StageDepth = CurCycle + IS->getCycles();
58 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
59 CurCycle += IS->getNextCycles();
60 }
David Goodwin6021b4d2009-08-10 15:55:25 +000061
Andrew Trick00067fb2010-12-08 20:04:29 +000062 // Find the next power-of-2 >= ItinDepth
63 while (ItinDepth > ScoreboardDepth) {
64 ScoreboardDepth *= 2;
65 }
David Goodwin6021b4d2009-08-10 15:55:25 +000066 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +000067 MaxLookAhead = ScoreboardDepth;
David Goodwin6021b4d2009-08-10 15:55:25 +000068 }
69
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000070 ReservedScoreboard.reset(ScoreboardDepth);
71 RequiredScoreboard.reset(ScoreboardDepth);
David Goodwin6021b4d2009-08-10 15:55:25 +000072
Andrew Trick00067fb2010-12-08 20:04:29 +000073 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
Bill Wendlinga2c45942009-08-22 20:08:44 +000074 << ScoreboardDepth << '\n');
David Goodwin6021b4d2009-08-10 15:55:25 +000075}
76
Andrew Trick00067fb2010-12-08 20:04:29 +000077void ScoreboardHazardRecognizer::Reset() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +000078 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000079 RequiredScoreboard.reset();
80 ReservedScoreboard.reset();
David Goodwin6021b4d2009-08-10 15:55:25 +000081}
82
Andrew Trick00067fb2010-12-08 20:04:29 +000083void ScoreboardHazardRecognizer::Scoreboard::dump() const {
David Greene964a9822010-01-04 21:26:07 +000084 dbgs() << "Scoreboard:\n";
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000085
86 unsigned last = Depth - 1;
87 while ((last > 0) && ((*this)[last] == 0))
David Goodwin6021b4d2009-08-10 15:55:25 +000088 last--;
89
90 for (unsigned i = 0; i <= last; i++) {
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000091 unsigned FUs = (*this)[i];
David Greene964a9822010-01-04 21:26:07 +000092 dbgs() << "\t";
David Goodwin6021b4d2009-08-10 15:55:25 +000093 for (int j = 31; j >= 0; j--)
David Greene964a9822010-01-04 21:26:07 +000094 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
95 dbgs() << '\n';
David Goodwin6021b4d2009-08-10 15:55:25 +000096 }
97}
98
Andrew Trick10ffc2b2010-12-24 05:03:26 +000099bool ScoreboardHazardRecognizer::atIssueLimit() const {
100 if (IssueWidth == 0)
101 return false;
102
103 return IssueCount == IssueWidth;
104}
105
Evan Chengf128bdc2010-06-16 07:35:02 +0000106ScheduleHazardRecognizer::HazardType
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000107ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Evan Chengbf407072010-09-10 01:29:16 +0000108 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000109 return NoHazard;
David Goodwin6021b4d2009-08-10 15:55:25 +0000110
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000111 // Note that stalls will be negative for bottom-up scheduling.
112 int cycle = Stalls;
David Goodwin74b79562009-09-22 16:47:52 +0000113
114 // Use the itinerary for the underlying instruction to check for
115 // free FU's in the scoreboard at the appropriate future cycles.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000116
117 const TargetInstrDesc *TID = DAG->getInstrDesc(SU);
118 if (TID == NULL) {
119 // Don't check hazards for non-machineinstr Nodes.
120 return NoHazard;
121 }
122 unsigned idx = TID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000123 for (const InstrStage *IS = ItinData->beginStage(idx),
124 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000125 // We must find one of the stage's units free for every cycle the
126 // stage is occupied. FIXME it would be more accurate to find the
127 // same unit free in all the cycles.
128 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000129 int StageCycle = cycle + (int)i;
130 if (StageCycle < 0)
131 continue;
132
133 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
134 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
135 "Scoreboard depth exceeded!");
136 // This stage was stalled beyond pipeline depth, so cannot conflict.
137 break;
138 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000139
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000140 unsigned freeUnits = IS->getUnits();
141 switch (IS->getReservationKind()) {
142 default:
143 assert(0 && "Invalid FU reservation");
144 case InstrStage::Required:
145 // Required FUs conflict with both reserved and required ones
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000146 freeUnits &= ~ReservedScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000147 // FALLTHROUGH
148 case InstrStage::Reserved:
149 // Reserved FUs can conflict only with required ones.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000150 freeUnits &= ~RequiredScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000151 break;
152 }
153
David Goodwin74b79562009-09-22 16:47:52 +0000154 if (!freeUnits) {
David Greene964a9822010-01-04 21:26:07 +0000155 DEBUG(dbgs() << "*** Hazard in cycle " << (cycle + i) << ", ");
156 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000157 DEBUG(DAG->dumpNode(SU));
David Goodwin74b79562009-09-22 16:47:52 +0000158 return Hazard;
159 }
David Goodwin6021b4d2009-08-10 15:55:25 +0000160 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000161
David Goodwin74b79562009-09-22 16:47:52 +0000162 // Advance the cycle to the next stage.
163 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000164 }
165
166 return NoHazard;
167}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000168
Andrew Trick00067fb2010-12-08 20:04:29 +0000169void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
Evan Chengbf407072010-09-10 01:29:16 +0000170 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000171 return;
David Goodwin6021b4d2009-08-10 15:55:25 +0000172
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000173 ++IssueCount;
174
David Goodwin74b79562009-09-22 16:47:52 +0000175 unsigned cycle = 0;
David Goodwin6021b4d2009-08-10 15:55:25 +0000176
David Goodwin74b79562009-09-22 16:47:52 +0000177 // Use the itinerary for the underlying instruction to reserve FU's
178 // in the scoreboard at the appropriate future cycles.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000179 const TargetInstrDesc *TID = DAG->getInstrDesc(SU);
180 assert(TID && "The scheduler must filter non-machineinstrs");
181 unsigned idx = TID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000182 for (const InstrStage *IS = ItinData->beginStage(idx),
183 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000184 // We must reserve one of the stage's units for every cycle the
185 // stage is occupied. FIXME it would be more accurate to reserve
186 // the same unit free in all the cycles.
187 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000188 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
David Goodwin74b79562009-09-22 16:47:52 +0000189 "Scoreboard depth exceeded!");
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000190
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000191 unsigned freeUnits = IS->getUnits();
192 switch (IS->getReservationKind()) {
193 default:
194 assert(0 && "Invalid FU reservation");
195 case InstrStage::Required:
196 // Required FUs conflict with both reserved and required ones
197 freeUnits &= ~ReservedScoreboard[cycle + i];
198 // FALLTHROUGH
199 case InstrStage::Reserved:
200 // Reserved FUs can conflict only with required ones.
201 freeUnits &= ~RequiredScoreboard[cycle + i];
202 break;
203 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000204
David Goodwin74b79562009-09-22 16:47:52 +0000205 // reduce to a single unit
206 unsigned freeUnit = 0;
207 do {
208 freeUnit = freeUnits;
209 freeUnits = freeUnit & (freeUnit - 1);
210 } while (freeUnits);
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000211
David Goodwin74b79562009-09-22 16:47:52 +0000212 assert(freeUnit && "No function unit available!");
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000213 if (IS->getReservationKind() == InstrStage::Required)
214 RequiredScoreboard[cycle + i] |= freeUnit;
215 else
216 ReservedScoreboard[cycle + i] |= freeUnit;
David Goodwin6021b4d2009-08-10 15:55:25 +0000217 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000218
David Goodwin74b79562009-09-22 16:47:52 +0000219 // Advance the cycle to the next stage.
220 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000221 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000222
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000223 DEBUG(ReservedScoreboard.dump());
224 DEBUG(RequiredScoreboard.dump());
David Goodwin6021b4d2009-08-10 15:55:25 +0000225}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000226
Andrew Trick00067fb2010-12-08 20:04:29 +0000227void ScoreboardHazardRecognizer::AdvanceCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000228 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000229 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
230 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
David Goodwin6021b4d2009-08-10 15:55:25 +0000231}
Andrew Trick00067fb2010-12-08 20:04:29 +0000232
233void ScoreboardHazardRecognizer::RecedeCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000234 IssueCount = 0;
Andrew Trick00067fb2010-12-08 20:04:29 +0000235 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
236 ReservedScoreboard.recede();
237 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
238 RequiredScoreboard.recede();
239}