blob: e6753664850a7803e6c156862021a3ed8068c998 [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
Andrew Tricked7c96d2012-06-05 03:44:32 +000042 // 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.
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000045 unsigned ScoreboardDepth = 1;
Evan Chengbf407072010-09-10 01:29:16 +000046 if (ItinData && !ItinData->isEmpty()) {
David Goodwin6021b4d2009-08-10 15:55:25 +000047 for (unsigned idx = 0; ; ++idx) {
Evan Chengbf407072010-09-10 01:29:16 +000048 if (ItinData->isEndMarker(idx))
David Goodwin6021b4d2009-08-10 15:55:25 +000049 break;
50
Evan Chengbf407072010-09-10 01:29:16 +000051 const InstrStage *IS = ItinData->beginStage(idx);
52 const InstrStage *E = ItinData->endStage(idx);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000053 unsigned CurCycle = 0;
David Goodwin6021b4d2009-08-10 15:55:25 +000054 unsigned ItinDepth = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +000055 for (; IS != E; ++IS) {
56 unsigned StageDepth = CurCycle + IS->getCycles();
57 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
58 CurCycle += IS->getNextCycles();
59 }
David Goodwin6021b4d2009-08-10 15:55:25 +000060
Andrew Trick00067fb2010-12-08 20:04:29 +000061 // Find the next power-of-2 >= ItinDepth
62 while (ItinDepth > ScoreboardDepth) {
63 ScoreboardDepth *= 2;
Andrew Tricked7c96d2012-06-05 03:44:32 +000064 // 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;
Andrew Trick00067fb2010-12-08 20:04:29 +000068 }
David Goodwin6021b4d2009-08-10 15:55:25 +000069 }
70 }
71
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000072 ReservedScoreboard.reset(ScoreboardDepth);
73 RequiredScoreboard.reset(ScoreboardDepth);
David Goodwin6021b4d2009-08-10 15:55:25 +000074
Andrew Trick87255e32012-07-07 04:00:00 +000075 // If MaxLookAhead is not set above, then we are not enabled.
Andrew Trick73d77362012-06-05 03:44:40 +000076 if (!isEnabled())
Andrew Tricked7c96d2012-06-05 03:44:32 +000077 DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
Andrew Trick73d77362012-06-05 03:44:40 +000078 else {
Andrew Trick87255e32012-07-07 04:00:00 +000079 // A nonempty itinerary must have a SchedModel.
80 IssueWidth = ItinData->SchedModel->IssueWidth;
Andrew Tricked7c96d2012-06-05 03:44:32 +000081 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
82 << ScoreboardDepth << '\n');
Andrew Trick73d77362012-06-05 03:44:40 +000083 }
David Goodwin6021b4d2009-08-10 15:55:25 +000084}
85
Andrew Trick00067fb2010-12-08 20:04:29 +000086void ScoreboardHazardRecognizer::Reset() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +000087 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000088 RequiredScoreboard.reset();
89 ReservedScoreboard.reset();
David Goodwin6021b4d2009-08-10 15:55:25 +000090}
91
Andrew Trick00067fb2010-12-08 20:04:29 +000092void ScoreboardHazardRecognizer::Scoreboard::dump() const {
David Greene964a9822010-01-04 21:26:07 +000093 dbgs() << "Scoreboard:\n";
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000094
95 unsigned last = Depth - 1;
96 while ((last > 0) && ((*this)[last] == 0))
David Goodwin6021b4d2009-08-10 15:55:25 +000097 last--;
98
99 for (unsigned i = 0; i <= last; i++) {
Hal Finkel8db55472012-06-22 20:27:13 +0000100 unsigned FUs = (*this)[i];
David Greene964a9822010-01-04 21:26:07 +0000101 dbgs() << "\t";
Hal Finkel8db55472012-06-22 20:27:13 +0000102 for (int j = 31; j >= 0; j--)
103 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
David Greene964a9822010-01-04 21:26:07 +0000104 dbgs() << '\n';
David Goodwin6021b4d2009-08-10 15:55:25 +0000105 }
106}
107
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000108bool ScoreboardHazardRecognizer::atIssueLimit() const {
109 if (IssueWidth == 0)
110 return false;
111
112 return IssueCount == IssueWidth;
113}
114
Evan Chengf128bdc2010-06-16 07:35:02 +0000115ScheduleHazardRecognizer::HazardType
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000116ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Evan Chengbf407072010-09-10 01:29:16 +0000117 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000118 return NoHazard;
David Goodwin6021b4d2009-08-10 15:55:25 +0000119
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000120 // Note that stalls will be negative for bottom-up scheduling.
121 int cycle = Stalls;
David Goodwin74b79562009-09-22 16:47:52 +0000122
123 // Use the itinerary for the underlying instruction to check for
124 // free FU's in the scoreboard at the appropriate future cycles.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000125
Evan Cheng6cc775f2011-06-28 19:10:37 +0000126 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
127 if (MCID == NULL) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000128 // Don't check hazards for non-machineinstr Nodes.
129 return NoHazard;
130 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000131 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000132 for (const InstrStage *IS = ItinData->beginStage(idx),
133 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000134 // We must find one of the stage's units free for every cycle the
135 // stage is occupied. FIXME it would be more accurate to find the
136 // same unit free in all the cycles.
137 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000138 int StageCycle = cycle + (int)i;
139 if (StageCycle < 0)
140 continue;
141
142 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
143 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
144 "Scoreboard depth exceeded!");
145 // This stage was stalled beyond pipeline depth, so cannot conflict.
146 break;
147 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000148
Hal Finkel8db55472012-06-22 20:27:13 +0000149 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000150 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000151 case InstrStage::Required:
152 // Required FUs conflict with both reserved and required ones
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000153 freeUnits &= ~ReservedScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000154 // FALLTHROUGH
155 case InstrStage::Reserved:
156 // Reserved FUs can conflict only with required ones.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000157 freeUnits &= ~RequiredScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000158 break;
159 }
160
David Goodwin74b79562009-09-22 16:47:52 +0000161 if (!freeUnits) {
Benjamin Kramer484f4242012-05-26 11:37:37 +0000162 DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
David Greene964a9822010-01-04 21:26:07 +0000163 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000164 DEBUG(DAG->dumpNode(SU));
David Goodwin74b79562009-09-22 16:47:52 +0000165 return Hazard;
166 }
David Goodwin6021b4d2009-08-10 15:55:25 +0000167 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000168
David Goodwin74b79562009-09-22 16:47:52 +0000169 // Advance the cycle to the next stage.
170 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000171 }
172
173 return NoHazard;
174}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000175
Andrew Trick00067fb2010-12-08 20:04:29 +0000176void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
Evan Chengbf407072010-09-10 01:29:16 +0000177 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000178 return;
David Goodwin6021b4d2009-08-10 15:55:25 +0000179
David Goodwin74b79562009-09-22 16:47:52 +0000180 // Use the itinerary for the underlying instruction to reserve FU's
181 // in the scoreboard at the appropriate future cycles.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000182 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
183 assert(MCID && "The scheduler must filter non-machineinstrs");
184 if (DAG->TII->isZeroCost(MCID->Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +0000185 return;
186
187 ++IssueCount;
188
189 unsigned cycle = 0;
190
Evan Cheng6cc775f2011-06-28 19:10:37 +0000191 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000192 for (const InstrStage *IS = ItinData->beginStage(idx),
193 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000194 // We must reserve one of the stage's units for every cycle the
195 // stage is occupied. FIXME it would be more accurate to reserve
196 // the same unit free in all the cycles.
197 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000198 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
David Goodwin74b79562009-09-22 16:47:52 +0000199 "Scoreboard depth exceeded!");
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000200
Hal Finkel8db55472012-06-22 20:27:13 +0000201 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000202 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000203 case InstrStage::Required:
204 // Required FUs conflict with both reserved and required ones
205 freeUnits &= ~ReservedScoreboard[cycle + i];
206 // FALLTHROUGH
207 case InstrStage::Reserved:
208 // Reserved FUs can conflict only with required ones.
209 freeUnits &= ~RequiredScoreboard[cycle + i];
210 break;
211 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000212
David Goodwin74b79562009-09-22 16:47:52 +0000213 // reduce to a single unit
Hal Finkel8db55472012-06-22 20:27:13 +0000214 unsigned freeUnit = 0;
David Goodwin74b79562009-09-22 16:47:52 +0000215 do {
216 freeUnit = freeUnits;
217 freeUnits = freeUnit & (freeUnit - 1);
218 } while (freeUnits);
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000219
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000220 if (IS->getReservationKind() == InstrStage::Required)
221 RequiredScoreboard[cycle + i] |= freeUnit;
222 else
223 ReservedScoreboard[cycle + i] |= freeUnit;
David Goodwin6021b4d2009-08-10 15:55:25 +0000224 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000225
David Goodwin74b79562009-09-22 16:47:52 +0000226 // Advance the cycle to the next stage.
227 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000228 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000229
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000230 DEBUG(ReservedScoreboard.dump());
231 DEBUG(RequiredScoreboard.dump());
David Goodwin6021b4d2009-08-10 15:55:25 +0000232}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000233
Andrew Trick00067fb2010-12-08 20:04:29 +0000234void ScoreboardHazardRecognizer::AdvanceCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000235 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000236 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
237 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
David Goodwin6021b4d2009-08-10 15:55:25 +0000238}
Andrew Trick00067fb2010-12-08 20:04:29 +0000239
240void ScoreboardHazardRecognizer::RecedeCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000241 IssueCount = 0;
Andrew Trick00067fb2010-12-08 20:04:29 +0000242 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
243 ReservedScoreboard.recede();
244 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
245 RequiredScoreboard.recede();
246}