blob: 7110b7566aba0e32c3e6afd81f7e444cf79e4ad5 [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 Trick73d77362012-06-05 03:44:40 +000075 if (!isEnabled())
Andrew Tricked7c96d2012-06-05 03:44:32 +000076 DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
Andrew Trick73d77362012-06-05 03:44:40 +000077 else {
78 IssueWidth = ItinData->Props.IssueWidth;
Andrew Tricked7c96d2012-06-05 03:44:32 +000079 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
80 << ScoreboardDepth << '\n');
Andrew Trick73d77362012-06-05 03:44:40 +000081 }
David Goodwin6021b4d2009-08-10 15:55:25 +000082}
83
Andrew Trick00067fb2010-12-08 20:04:29 +000084void ScoreboardHazardRecognizer::Reset() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +000085 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000086 RequiredScoreboard.reset();
87 ReservedScoreboard.reset();
David Goodwin6021b4d2009-08-10 15:55:25 +000088}
89
Andrew Trick00067fb2010-12-08 20:04:29 +000090void ScoreboardHazardRecognizer::Scoreboard::dump() const {
David Greene964a9822010-01-04 21:26:07 +000091 dbgs() << "Scoreboard:\n";
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000092
93 unsigned last = Depth - 1;
94 while ((last > 0) && ((*this)[last] == 0))
David Goodwin6021b4d2009-08-10 15:55:25 +000095 last--;
96
97 for (unsigned i = 0; i <= last; i++) {
Hal Finkel8db55472012-06-22 20:27:13 +000098 unsigned FUs = (*this)[i];
David Greene964a9822010-01-04 21:26:07 +000099 dbgs() << "\t";
Hal Finkel8db55472012-06-22 20:27:13 +0000100 for (int j = 31; j >= 0; j--)
101 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
David Greene964a9822010-01-04 21:26:07 +0000102 dbgs() << '\n';
David Goodwin6021b4d2009-08-10 15:55:25 +0000103 }
104}
105
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000106bool ScoreboardHazardRecognizer::atIssueLimit() const {
107 if (IssueWidth == 0)
108 return false;
109
110 return IssueCount == IssueWidth;
111}
112
Evan Chengf128bdc2010-06-16 07:35:02 +0000113ScheduleHazardRecognizer::HazardType
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000114ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Evan Chengbf407072010-09-10 01:29:16 +0000115 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000116 return NoHazard;
David Goodwin6021b4d2009-08-10 15:55:25 +0000117
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000118 // Note that stalls will be negative for bottom-up scheduling.
119 int cycle = Stalls;
David Goodwin74b79562009-09-22 16:47:52 +0000120
121 // Use the itinerary for the underlying instruction to check for
122 // free FU's in the scoreboard at the appropriate future cycles.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000123
Evan Cheng6cc775f2011-06-28 19:10:37 +0000124 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
125 if (MCID == NULL) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000126 // Don't check hazards for non-machineinstr Nodes.
127 return NoHazard;
128 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000129 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000130 for (const InstrStage *IS = ItinData->beginStage(idx),
131 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000132 // We must find one of the stage's units free for every cycle the
133 // stage is occupied. FIXME it would be more accurate to find the
134 // same unit free in all the cycles.
135 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000136 int StageCycle = cycle + (int)i;
137 if (StageCycle < 0)
138 continue;
139
140 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
141 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
142 "Scoreboard depth exceeded!");
143 // This stage was stalled beyond pipeline depth, so cannot conflict.
144 break;
145 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000146
Hal Finkel8db55472012-06-22 20:27:13 +0000147 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000148 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000149 case InstrStage::Required:
150 // Required FUs conflict with both reserved and required ones
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000151 freeUnits &= ~ReservedScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000152 // FALLTHROUGH
153 case InstrStage::Reserved:
154 // Reserved FUs can conflict only with required ones.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000155 freeUnits &= ~RequiredScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000156 break;
157 }
158
David Goodwin74b79562009-09-22 16:47:52 +0000159 if (!freeUnits) {
Benjamin Kramer484f4242012-05-26 11:37:37 +0000160 DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
David Greene964a9822010-01-04 21:26:07 +0000161 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000162 DEBUG(DAG->dumpNode(SU));
David Goodwin74b79562009-09-22 16:47:52 +0000163 return Hazard;
164 }
David Goodwin6021b4d2009-08-10 15:55:25 +0000165 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000166
David Goodwin74b79562009-09-22 16:47:52 +0000167 // Advance the cycle to the next stage.
168 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000169 }
170
171 return NoHazard;
172}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000173
Andrew Trick00067fb2010-12-08 20:04:29 +0000174void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
Evan Chengbf407072010-09-10 01:29:16 +0000175 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000176 return;
David Goodwin6021b4d2009-08-10 15:55:25 +0000177
David Goodwin74b79562009-09-22 16:47:52 +0000178 // Use the itinerary for the underlying instruction to reserve FU's
179 // in the scoreboard at the appropriate future cycles.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000180 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
181 assert(MCID && "The scheduler must filter non-machineinstrs");
182 if (DAG->TII->isZeroCost(MCID->Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +0000183 return;
184
185 ++IssueCount;
186
187 unsigned cycle = 0;
188
Evan Cheng6cc775f2011-06-28 19:10:37 +0000189 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000190 for (const InstrStage *IS = ItinData->beginStage(idx),
191 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000192 // We must reserve one of the stage's units for every cycle the
193 // stage is occupied. FIXME it would be more accurate to reserve
194 // the same unit free in all the cycles.
195 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000196 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
David Goodwin74b79562009-09-22 16:47:52 +0000197 "Scoreboard depth exceeded!");
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000198
Hal Finkel8db55472012-06-22 20:27:13 +0000199 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000200 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000201 case InstrStage::Required:
202 // Required FUs conflict with both reserved and required ones
203 freeUnits &= ~ReservedScoreboard[cycle + i];
204 // FALLTHROUGH
205 case InstrStage::Reserved:
206 // Reserved FUs can conflict only with required ones.
207 freeUnits &= ~RequiredScoreboard[cycle + i];
208 break;
209 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000210
David Goodwin74b79562009-09-22 16:47:52 +0000211 // reduce to a single unit
Hal Finkel8db55472012-06-22 20:27:13 +0000212 unsigned freeUnit = 0;
David Goodwin74b79562009-09-22 16:47:52 +0000213 do {
214 freeUnit = freeUnits;
215 freeUnits = freeUnit & (freeUnit - 1);
216 } while (freeUnits);
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000217
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000218 if (IS->getReservationKind() == InstrStage::Required)
219 RequiredScoreboard[cycle + i] |= freeUnit;
220 else
221 ReservedScoreboard[cycle + i] |= freeUnit;
David Goodwin6021b4d2009-08-10 15:55:25 +0000222 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000223
David Goodwin74b79562009-09-22 16:47:52 +0000224 // Advance the cycle to the next stage.
225 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000226 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000227
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000228 DEBUG(ReservedScoreboard.dump());
229 DEBUG(RequiredScoreboard.dump());
David Goodwin6021b4d2009-08-10 15:55:25 +0000230}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000231
Andrew Trick00067fb2010-12-08 20:04:29 +0000232void ScoreboardHazardRecognizer::AdvanceCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000233 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000234 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
235 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
David Goodwin6021b4d2009-08-10 15:55:25 +0000236}
Andrew Trick00067fb2010-12-08 20:04:29 +0000237
238void ScoreboardHazardRecognizer::RecedeCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000239 IssueCount = 0;
Andrew Trick00067fb2010-12-08 20:04:29 +0000240 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
241 ReservedScoreboard.recede();
242 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
243 RequiredScoreboard.recede();
244}