blob: 74913b1586a20f0afee627f9638b12c04e4f7433 [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"
Evan Cheng8264e272011-06-29 01:14:12 +000019#include "llvm/MC/MCInstrItineraries.h"
David Goodwin6021b4d2009-08-10 15:55:25 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/ErrorHandling.h"
David Goodwinf20236a2009-08-11 01:44:26 +000022#include "llvm/Support/raw_ostream.h"
Andrew Trick47ff14b2011-01-21 05:51:33 +000023#include "llvm/Target/TargetInstrInfo.h"
David Goodwin6021b4d2009-08-10 15:55:25 +000024
Bill Wendlinga2c45942009-08-22 20:08:44 +000025using namespace llvm;
David Goodwin6021b4d2009-08-10 15:55:25 +000026
Andrew Trick10ffc2b2010-12-24 05:03:26 +000027#ifndef NDEBUG
28const char *ScoreboardHazardRecognizer::DebugType = "";
29#endif
30
Andrew Trick00067fb2010-12-08 20:04:29 +000031ScoreboardHazardRecognizer::
Andrew Trick10ffc2b2010-12-24 05:03:26 +000032ScoreboardHazardRecognizer(const InstrItineraryData *II,
33 const ScheduleDAG *SchedDAG,
34 const char *ParentDebugType) :
35 ScheduleHazardRecognizer(), ItinData(II), DAG(SchedDAG), IssueWidth(0),
36 IssueCount(0) {
37
38#ifndef NDEBUG
39 DebugType = ParentDebugType;
40#endif
41
David Goodwin6021b4d2009-08-10 15:55:25 +000042 // Determine the maximum depth of any itinerary. This determines the
43 // depth of the scoreboard. We always make the scoreboard at least 1
44 // cycle deep to avoid dealing with the boundary condition.
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000045 unsigned ScoreboardDepth = 1;
Evan Chengbf407072010-09-10 01:29:16 +000046 if (ItinData && !ItinData->isEmpty()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +000047 IssueWidth = ItinData->IssueWidth;
48
David Goodwin6021b4d2009-08-10 15:55:25 +000049 for (unsigned idx = 0; ; ++idx) {
Evan Chengbf407072010-09-10 01:29:16 +000050 if (ItinData->isEndMarker(idx))
David Goodwin6021b4d2009-08-10 15:55:25 +000051 break;
52
Evan Chengbf407072010-09-10 01:29:16 +000053 const InstrStage *IS = ItinData->beginStage(idx);
54 const InstrStage *E = ItinData->endStage(idx);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000055 unsigned CurCycle = 0;
David Goodwin6021b4d2009-08-10 15:55:25 +000056 unsigned ItinDepth = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +000057 for (; IS != E; ++IS) {
58 unsigned StageDepth = CurCycle + IS->getCycles();
59 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
60 CurCycle += IS->getNextCycles();
61 }
David Goodwin6021b4d2009-08-10 15:55:25 +000062
Andrew Trick00067fb2010-12-08 20:04:29 +000063 // Find the next power-of-2 >= ItinDepth
64 while (ItinDepth > ScoreboardDepth) {
65 ScoreboardDepth *= 2;
66 }
David Goodwin6021b4d2009-08-10 15:55:25 +000067 }
Andrew Trick10ffc2b2010-12-24 05:03:26 +000068 MaxLookAhead = ScoreboardDepth;
David Goodwin6021b4d2009-08-10 15:55:25 +000069 }
70
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000071 ReservedScoreboard.reset(ScoreboardDepth);
72 RequiredScoreboard.reset(ScoreboardDepth);
David Goodwin6021b4d2009-08-10 15:55:25 +000073
Andrew Trick00067fb2010-12-08 20:04:29 +000074 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
Bill Wendlinga2c45942009-08-22 20:08:44 +000075 << ScoreboardDepth << '\n');
David Goodwin6021b4d2009-08-10 15:55:25 +000076}
77
Andrew Trick00067fb2010-12-08 20:04:29 +000078void ScoreboardHazardRecognizer::Reset() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +000079 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000080 RequiredScoreboard.reset();
81 ReservedScoreboard.reset();
David Goodwin6021b4d2009-08-10 15:55:25 +000082}
83
Andrew Trick00067fb2010-12-08 20:04:29 +000084void ScoreboardHazardRecognizer::Scoreboard::dump() const {
David Greene964a9822010-01-04 21:26:07 +000085 dbgs() << "Scoreboard:\n";
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000086
87 unsigned last = Depth - 1;
88 while ((last > 0) && ((*this)[last] == 0))
David Goodwin6021b4d2009-08-10 15:55:25 +000089 last--;
90
91 for (unsigned i = 0; i <= last; i++) {
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000092 unsigned FUs = (*this)[i];
David Greene964a9822010-01-04 21:26:07 +000093 dbgs() << "\t";
David Goodwin6021b4d2009-08-10 15:55:25 +000094 for (int j = 31; j >= 0; j--)
David Greene964a9822010-01-04 21:26:07 +000095 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
96 dbgs() << '\n';
David Goodwin6021b4d2009-08-10 15:55:25 +000097 }
98}
99
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000100bool ScoreboardHazardRecognizer::atIssueLimit() const {
101 if (IssueWidth == 0)
102 return false;
103
104 return IssueCount == IssueWidth;
105}
106
Evan Chengf128bdc2010-06-16 07:35:02 +0000107ScheduleHazardRecognizer::HazardType
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000108ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Evan Chengbf407072010-09-10 01:29:16 +0000109 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000110 return NoHazard;
David Goodwin6021b4d2009-08-10 15:55:25 +0000111
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000112 // Note that stalls will be negative for bottom-up scheduling.
113 int cycle = Stalls;
David Goodwin74b79562009-09-22 16:47:52 +0000114
115 // Use the itinerary for the underlying instruction to check for
116 // free FU's in the scoreboard at the appropriate future cycles.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000117
Evan Cheng6cc775f2011-06-28 19:10:37 +0000118 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
119 if (MCID == NULL) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000120 // Don't check hazards for non-machineinstr Nodes.
121 return NoHazard;
122 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000123 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000124 for (const InstrStage *IS = ItinData->beginStage(idx),
125 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000126 // We must find one of the stage's units free for every cycle the
127 // stage is occupied. FIXME it would be more accurate to find the
128 // same unit free in all the cycles.
129 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000130 int StageCycle = cycle + (int)i;
131 if (StageCycle < 0)
132 continue;
133
134 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
135 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
136 "Scoreboard depth exceeded!");
137 // This stage was stalled beyond pipeline depth, so cannot conflict.
138 break;
139 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000140
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000141 unsigned freeUnits = IS->getUnits();
142 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000143 case InstrStage::Required:
144 // Required FUs conflict with both reserved and required ones
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000145 freeUnits &= ~ReservedScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000146 // FALLTHROUGH
147 case InstrStage::Reserved:
148 // Reserved FUs can conflict only with required ones.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000149 freeUnits &= ~RequiredScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000150 break;
151 }
152
David Goodwin74b79562009-09-22 16:47:52 +0000153 if (!freeUnits) {
Andrew Trick4e7f6a72012-05-25 02:02:39 +0000154 DEBUG(dbgs() << "*** Hazard in cycle " << ((cycle+i) < 0 ? "" : "+")
155 << (cycle + i) << ", ");
David Greene964a9822010-01-04 21:26:07 +0000156 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
David Goodwin74b79562009-09-22 16:47:52 +0000173 // Use the itinerary for the underlying instruction to reserve FU's
174 // in the scoreboard at the appropriate future cycles.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000175 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
176 assert(MCID && "The scheduler must filter non-machineinstrs");
177 if (DAG->TII->isZeroCost(MCID->Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +0000178 return;
179
180 ++IssueCount;
181
182 unsigned cycle = 0;
183
Evan Cheng6cc775f2011-06-28 19:10:37 +0000184 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000185 for (const InstrStage *IS = ItinData->beginStage(idx),
186 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000187 // We must reserve one of the stage's units for every cycle the
188 // stage is occupied. FIXME it would be more accurate to reserve
189 // the same unit free in all the cycles.
190 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000191 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
David Goodwin74b79562009-09-22 16:47:52 +0000192 "Scoreboard depth exceeded!");
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000193
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000194 unsigned freeUnits = IS->getUnits();
195 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000196 case InstrStage::Required:
197 // Required FUs conflict with both reserved and required ones
198 freeUnits &= ~ReservedScoreboard[cycle + i];
199 // FALLTHROUGH
200 case InstrStage::Reserved:
201 // Reserved FUs can conflict only with required ones.
202 freeUnits &= ~RequiredScoreboard[cycle + i];
203 break;
204 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000205
David Goodwin74b79562009-09-22 16:47:52 +0000206 // reduce to a single unit
207 unsigned freeUnit = 0;
208 do {
209 freeUnit = freeUnits;
210 freeUnits = freeUnit & (freeUnit - 1);
211 } while (freeUnits);
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000212
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}