blob: 83bc1ba7beb94263167da738a5bf5e7e0d73d072 [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 Trick00067fb2010-12-08 20:04:29 +000016#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Evan Cheng078f4ce2010-06-14 21:06:53 +000017#include "llvm/CodeGen/ScheduleDAG.h"
Evan Cheng8264e272011-06-29 01:14:12 +000018#include "llvm/MC/MCInstrItineraries.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"
Andrew Trick47ff14b2011-01-21 05:51:33 +000022#include "llvm/Target/TargetInstrInfo.h"
David Goodwin6021b4d2009-08-10 15:55:25 +000023
Bill Wendlinga2c45942009-08-22 20:08:44 +000024using namespace llvm;
David Goodwin6021b4d2009-08-10 15:55:25 +000025
Mehdi Aminiea0b1e72016-04-20 00:21:24 +000026#define DEBUG_TYPE DebugType
Chandler Carruth1b9dde02014-04-22 02:02:50 +000027
Mehdi Aminiea0b1e72016-04-20 00:21:24 +000028ScoreboardHazardRecognizer::ScoreboardHazardRecognizer(
29 const InstrItineraryData *II, const ScheduleDAG *SchedDAG,
30 const char *ParentDebugType)
31 : ScheduleHazardRecognizer(), DebugType(ParentDebugType), ItinData(II),
32 DAG(SchedDAG), IssueWidth(0), IssueCount(0) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +000033
Andrew Tricked7c96d2012-06-05 03:44:32 +000034 // Determine the maximum depth of any itinerary. This determines the depth of
35 // the scoreboard. We always make the scoreboard at least 1 cycle deep to
36 // avoid dealing with the boundary condition.
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000037 unsigned ScoreboardDepth = 1;
Evan Chengbf407072010-09-10 01:29:16 +000038 if (ItinData && !ItinData->isEmpty()) {
David Goodwin6021b4d2009-08-10 15:55:25 +000039 for (unsigned idx = 0; ; ++idx) {
Evan Chengbf407072010-09-10 01:29:16 +000040 if (ItinData->isEndMarker(idx))
David Goodwin6021b4d2009-08-10 15:55:25 +000041 break;
42
Evan Chengbf407072010-09-10 01:29:16 +000043 const InstrStage *IS = ItinData->beginStage(idx);
44 const InstrStage *E = ItinData->endStage(idx);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000045 unsigned CurCycle = 0;
David Goodwin6021b4d2009-08-10 15:55:25 +000046 unsigned ItinDepth = 0;
Andrew Trick10ffc2b2010-12-24 05:03:26 +000047 for (; IS != E; ++IS) {
48 unsigned StageDepth = CurCycle + IS->getCycles();
49 if (ItinDepth < StageDepth) ItinDepth = StageDepth;
50 CurCycle += IS->getNextCycles();
51 }
David Goodwin6021b4d2009-08-10 15:55:25 +000052
Andrew Trick00067fb2010-12-08 20:04:29 +000053 // Find the next power-of-2 >= ItinDepth
54 while (ItinDepth > ScoreboardDepth) {
55 ScoreboardDepth *= 2;
Andrew Tricked7c96d2012-06-05 03:44:32 +000056 // Don't set MaxLookAhead until we find at least one nonzero stage.
57 // This way, an itinerary with no stages has MaxLookAhead==0, which
58 // completely bypasses the scoreboard hazard logic.
59 MaxLookAhead = ScoreboardDepth;
Andrew Trick00067fb2010-12-08 20:04:29 +000060 }
David Goodwin6021b4d2009-08-10 15:55:25 +000061 }
62 }
63
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +000064 ReservedScoreboard.reset(ScoreboardDepth);
65 RequiredScoreboard.reset(ScoreboardDepth);
David Goodwin6021b4d2009-08-10 15:55:25 +000066
Andrew Trick87255e32012-07-07 04:00:00 +000067 // If MaxLookAhead is not set above, then we are not enabled.
Andrew Trick73d77362012-06-05 03:44:40 +000068 if (!isEnabled())
Andrew Tricked7c96d2012-06-05 03:44:32 +000069 DEBUG(dbgs() << "Disabled scoreboard hazard recognizer\n");
Andrew Trick73d77362012-06-05 03:44:40 +000070 else {
Andrew Trick87255e32012-07-07 04:00:00 +000071 // A nonempty itinerary must have a SchedModel.
Pete Cooper11759452014-09-02 17:43:54 +000072 IssueWidth = ItinData->SchedModel.IssueWidth;
Andrew Tricked7c96d2012-06-05 03:44:32 +000073 DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = "
74 << ScoreboardDepth << '\n');
Andrew Trick73d77362012-06-05 03:44:40 +000075 }
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
Manman Ren19f49ac2012-09-11 22:23:19 +000084#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000085LLVM_DUMP_METHOD void ScoreboardHazardRecognizer::Scoreboard::dump() const {
David Greene964a9822010-01-04 21:26:07 +000086 dbgs() << "Scoreboard:\n";
Anton Korobeynikov9a348a92010-04-07 18:19:24 +000087
88 unsigned last = Depth - 1;
89 while ((last > 0) && ((*this)[last] == 0))
David Goodwin6021b4d2009-08-10 15:55:25 +000090 last--;
91
92 for (unsigned i = 0; i <= last; i++) {
Hal Finkel8db55472012-06-22 20:27:13 +000093 unsigned FUs = (*this)[i];
David Greene964a9822010-01-04 21:26:07 +000094 dbgs() << "\t";
Hal Finkel8db55472012-06-22 20:27:13 +000095 for (int j = 31; j >= 0; j--)
96 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
David Greene964a9822010-01-04 21:26:07 +000097 dbgs() << '\n';
David Goodwin6021b4d2009-08-10 15:55:25 +000098 }
99}
Manman Ren742534c2012-09-06 19:06:06 +0000100#endif
David Goodwin6021b4d2009-08-10 15:55:25 +0000101
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000102bool ScoreboardHazardRecognizer::atIssueLimit() const {
103 if (IssueWidth == 0)
104 return false;
105
106 return IssueCount == IssueWidth;
107}
108
Evan Chengf128bdc2010-06-16 07:35:02 +0000109ScheduleHazardRecognizer::HazardType
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000110ScoreboardHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Evan Chengbf407072010-09-10 01:29:16 +0000111 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000112 return NoHazard;
David Goodwin6021b4d2009-08-10 15:55:25 +0000113
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000114 // Note that stalls will be negative for bottom-up scheduling.
115 int cycle = Stalls;
David Goodwin74b79562009-09-22 16:47:52 +0000116
117 // Use the itinerary for the underlying instruction to check for
118 // free FU's in the scoreboard at the appropriate future cycles.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000119
Evan Cheng6cc775f2011-06-28 19:10:37 +0000120 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
Craig Topperc0196b12014-04-14 00:51:57 +0000121 if (!MCID) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000122 // Don't check hazards for non-machineinstr Nodes.
123 return NoHazard;
124 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000125 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000126 for (const InstrStage *IS = ItinData->beginStage(idx),
127 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000128 // We must find one of the stage's units free for every cycle the
129 // stage is occupied. FIXME it would be more accurate to find the
130 // same unit free in all the cycles.
131 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000132 int StageCycle = cycle + (int)i;
133 if (StageCycle < 0)
134 continue;
135
136 if (StageCycle >= (int)RequiredScoreboard.getDepth()) {
137 assert((StageCycle - Stalls) < (int)RequiredScoreboard.getDepth() &&
138 "Scoreboard depth exceeded!");
139 // This stage was stalled beyond pipeline depth, so cannot conflict.
140 break;
141 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000142
Hal Finkel8db55472012-06-22 20:27:13 +0000143 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000144 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000145 case InstrStage::Required:
146 // Required FUs conflict with both reserved and required ones
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000147 freeUnits &= ~ReservedScoreboard[StageCycle];
Justin Bognerb03fd122016-08-17 05:10:15 +0000148 LLVM_FALLTHROUGH;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000149 case InstrStage::Reserved:
150 // Reserved FUs can conflict only with required ones.
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000151 freeUnits &= ~RequiredScoreboard[StageCycle];
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000152 break;
153 }
154
David Goodwin74b79562009-09-22 16:47:52 +0000155 if (!freeUnits) {
Benjamin Kramer484f4242012-05-26 11:37:37 +0000156 DEBUG(dbgs() << "*** Hazard in cycle +" << StageCycle << ", ");
David Greene964a9822010-01-04 21:26:07 +0000157 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000158 DEBUG(DAG->dumpNode(SU));
David Goodwin74b79562009-09-22 16:47:52 +0000159 return Hazard;
160 }
David Goodwin6021b4d2009-08-10 15:55:25 +0000161 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000162
David Goodwin74b79562009-09-22 16:47:52 +0000163 // Advance the cycle to the next stage.
164 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000165 }
166
167 return NoHazard;
168}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000169
Andrew Trick00067fb2010-12-08 20:04:29 +0000170void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) {
Evan Chengbf407072010-09-10 01:29:16 +0000171 if (!ItinData || ItinData->isEmpty())
David Goodwin74b79562009-09-22 16:47:52 +0000172 return;
David Goodwin6021b4d2009-08-10 15:55:25 +0000173
David Goodwin74b79562009-09-22 16:47:52 +0000174 // Use the itinerary for the underlying instruction to reserve FU's
175 // in the scoreboard at the appropriate future cycles.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000176 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
177 assert(MCID && "The scheduler must filter non-machineinstrs");
178 if (DAG->TII->isZeroCost(MCID->Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +0000179 return;
180
181 ++IssueCount;
182
183 unsigned cycle = 0;
184
Evan Cheng6cc775f2011-06-28 19:10:37 +0000185 unsigned idx = MCID->getSchedClass();
Evan Chengbf407072010-09-10 01:29:16 +0000186 for (const InstrStage *IS = ItinData->beginStage(idx),
187 *E = ItinData->endStage(idx); IS != E; ++IS) {
David Goodwin74b79562009-09-22 16:47:52 +0000188 // We must reserve one of the stage's units for every cycle the
189 // stage is occupied. FIXME it would be more accurate to reserve
190 // the same unit free in all the cycles.
191 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000192 assert(((cycle + i) < RequiredScoreboard.getDepth()) &&
David Goodwin74b79562009-09-22 16:47:52 +0000193 "Scoreboard depth exceeded!");
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000194
Hal Finkel8db55472012-06-22 20:27:13 +0000195 unsigned freeUnits = IS->getUnits();
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000196 switch (IS->getReservationKind()) {
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000197 case InstrStage::Required:
198 // Required FUs conflict with both reserved and required ones
199 freeUnits &= ~ReservedScoreboard[cycle + i];
Justin Bognerb03fd122016-08-17 05:10:15 +0000200 LLVM_FALLTHROUGH;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000201 case InstrStage::Reserved:
202 // Reserved FUs can conflict only with required ones.
203 freeUnits &= ~RequiredScoreboard[cycle + i];
204 break;
205 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000206
David Goodwin74b79562009-09-22 16:47:52 +0000207 // reduce to a single unit
Hal Finkel8db55472012-06-22 20:27:13 +0000208 unsigned freeUnit = 0;
David Goodwin74b79562009-09-22 16:47:52 +0000209 do {
210 freeUnit = freeUnits;
211 freeUnits = freeUnit & (freeUnit - 1);
212 } while (freeUnits);
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000213
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000214 if (IS->getReservationKind() == InstrStage::Required)
215 RequiredScoreboard[cycle + i] |= freeUnit;
216 else
217 ReservedScoreboard[cycle + i] |= freeUnit;
David Goodwin6021b4d2009-08-10 15:55:25 +0000218 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000219
David Goodwin74b79562009-09-22 16:47:52 +0000220 // Advance the cycle to the next stage.
221 cycle += IS->getNextCycles();
David Goodwin6021b4d2009-08-10 15:55:25 +0000222 }
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000223
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000224 DEBUG(ReservedScoreboard.dump());
225 DEBUG(RequiredScoreboard.dump());
David Goodwin6021b4d2009-08-10 15:55:25 +0000226}
Anton Korobeynikov9a348a92010-04-07 18:19:24 +0000227
Andrew Trick00067fb2010-12-08 20:04:29 +0000228void ScoreboardHazardRecognizer::AdvanceCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000229 IssueCount = 0;
Anton Korobeynikov0bdc6342010-04-07 18:19:32 +0000230 ReservedScoreboard[0] = 0; ReservedScoreboard.advance();
231 RequiredScoreboard[0] = 0; RequiredScoreboard.advance();
David Goodwin6021b4d2009-08-10 15:55:25 +0000232}
Andrew Trick00067fb2010-12-08 20:04:29 +0000233
234void ScoreboardHazardRecognizer::RecedeCycle() {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000235 IssueCount = 0;
Andrew Trick00067fb2010-12-08 20:04:29 +0000236 ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0;
237 ReservedScoreboard.recede();
238 RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0;
239 RequiredScoreboard.recede();
240}