Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 1 | //===----- ScoreboardHazardRecognizer.cpp - Scheduler Support -------------===// |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 2 | // |
| 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 Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 10 | // 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 Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "sched-hazard" |
| 17 | #include "llvm/CodeGen/ScoreboardHazardRecognizer.h" |
Evan Cheng | 078f4ce | 2010-06-14 21:06:53 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/ScheduleDAG.h" |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | f20236a | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrItineraries.h" |
| 23 | |
Bill Wendling | a2c4594 | 2009-08-22 20:08:44 +0000 | [diff] [blame] | 24 | using namespace llvm; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 25 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 26 | ScoreboardHazardRecognizer:: |
| 27 | ScoreboardHazardRecognizer(const InstrItineraryData *LItinData) : |
Evan Cheng | 078f4ce | 2010-06-14 21:06:53 +0000 | [diff] [blame] | 28 | ScheduleHazardRecognizer(), ItinData(LItinData) { |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 29 | // Determine the maximum depth of any itinerary. This determines the |
| 30 | // depth of the scoreboard. We always make the scoreboard at least 1 |
| 31 | // cycle deep to avoid dealing with the boundary condition. |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 32 | unsigned ScoreboardDepth = 1; |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 33 | if (ItinData && !ItinData->isEmpty()) { |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 34 | for (unsigned idx = 0; ; ++idx) { |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 35 | if (ItinData->isEndMarker(idx)) |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 36 | break; |
| 37 | |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 38 | const InstrStage *IS = ItinData->beginStage(idx); |
| 39 | const InstrStage *E = ItinData->endStage(idx); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 40 | unsigned ItinDepth = 0; |
| 41 | for (; IS != E; ++IS) |
David Goodwin | b369ee4 | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 42 | ItinDepth += IS->getCycles(); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 43 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 44 | // Find the next power-of-2 >= ItinDepth |
| 45 | while (ItinDepth > ScoreboardDepth) { |
| 46 | ScoreboardDepth *= 2; |
| 47 | } |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 51 | ReservedScoreboard.reset(ScoreboardDepth); |
| 52 | RequiredScoreboard.reset(ScoreboardDepth); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 53 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 54 | DEBUG(dbgs() << "Using scoreboard hazard recognizer: Depth = " |
Bill Wendling | a2c4594 | 2009-08-22 20:08:44 +0000 | [diff] [blame] | 55 | << ScoreboardDepth << '\n'); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 58 | void ScoreboardHazardRecognizer::Reset() { |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 59 | RequiredScoreboard.reset(); |
| 60 | ReservedScoreboard.reset(); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 63 | void ScoreboardHazardRecognizer::Scoreboard::dump() const { |
David Greene | 964a982 | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 64 | dbgs() << "Scoreboard:\n"; |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 65 | |
| 66 | unsigned last = Depth - 1; |
| 67 | while ((last > 0) && ((*this)[last] == 0)) |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 68 | last--; |
| 69 | |
| 70 | for (unsigned i = 0; i <= last; i++) { |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 71 | unsigned FUs = (*this)[i]; |
David Greene | 964a982 | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 72 | dbgs() << "\t"; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 73 | for (int j = 31; j >= 0; j--) |
David Greene | 964a982 | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 74 | dbgs() << ((FUs & (1 << j)) ? '1' : '0'); |
| 75 | dbgs() << '\n'; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 79 | ScheduleHazardRecognizer::HazardType |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 80 | ScoreboardHazardRecognizer::getHazardType(SUnit *SU) { |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 81 | if (!ItinData || ItinData->isEmpty()) |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 82 | return NoHazard; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 83 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 84 | unsigned cycle = 0; |
| 85 | |
| 86 | // Use the itinerary for the underlying instruction to check for |
| 87 | // free FU's in the scoreboard at the appropriate future cycles. |
| 88 | unsigned idx = SU->getInstr()->getDesc().getSchedClass(); |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 89 | for (const InstrStage *IS = ItinData->beginStage(idx), |
| 90 | *E = ItinData->endStage(idx); IS != E; ++IS) { |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 91 | // We must find one of the stage's units free for every cycle the |
| 92 | // stage is occupied. FIXME it would be more accurate to find the |
| 93 | // same unit free in all the cycles. |
| 94 | for (unsigned int i = 0; i < IS->getCycles(); ++i) { |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 95 | assert(((cycle + i) < RequiredScoreboard.getDepth()) && |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 96 | "Scoreboard depth exceeded!"); |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 97 | |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 98 | unsigned freeUnits = IS->getUnits(); |
| 99 | switch (IS->getReservationKind()) { |
| 100 | default: |
| 101 | assert(0 && "Invalid FU reservation"); |
| 102 | case InstrStage::Required: |
| 103 | // Required FUs conflict with both reserved and required ones |
| 104 | freeUnits &= ~ReservedScoreboard[cycle + i]; |
| 105 | // FALLTHROUGH |
| 106 | case InstrStage::Reserved: |
| 107 | // Reserved FUs can conflict only with required ones. |
| 108 | freeUnits &= ~RequiredScoreboard[cycle + i]; |
| 109 | break; |
| 110 | } |
| 111 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 112 | if (!freeUnits) { |
David Greene | 964a982 | 2010-01-04 21:26:07 +0000 | [diff] [blame] | 113 | DEBUG(dbgs() << "*** Hazard in cycle " << (cycle + i) << ", "); |
| 114 | DEBUG(dbgs() << "SU(" << SU->NodeNum << "): "); |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 115 | DEBUG(SU->getInstr()->dump()); |
| 116 | return Hazard; |
| 117 | } |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 118 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 119 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 120 | // Advance the cycle to the next stage. |
| 121 | cycle += IS->getNextCycles(); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | return NoHazard; |
| 125 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 126 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 127 | void ScoreboardHazardRecognizer::EmitInstruction(SUnit *SU) { |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 128 | if (!ItinData || ItinData->isEmpty()) |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 129 | return; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 130 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 131 | unsigned cycle = 0; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 132 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 133 | // Use the itinerary for the underlying instruction to reserve FU's |
| 134 | // in the scoreboard at the appropriate future cycles. |
| 135 | unsigned idx = SU->getInstr()->getDesc().getSchedClass(); |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 136 | for (const InstrStage *IS = ItinData->beginStage(idx), |
| 137 | *E = ItinData->endStage(idx); IS != E; ++IS) { |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 138 | // We must reserve one of the stage's units for every cycle the |
| 139 | // stage is occupied. FIXME it would be more accurate to reserve |
| 140 | // the same unit free in all the cycles. |
| 141 | for (unsigned int i = 0; i < IS->getCycles(); ++i) { |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 142 | assert(((cycle + i) < RequiredScoreboard.getDepth()) && |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 143 | "Scoreboard depth exceeded!"); |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 144 | |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 145 | unsigned freeUnits = IS->getUnits(); |
| 146 | switch (IS->getReservationKind()) { |
| 147 | default: |
| 148 | assert(0 && "Invalid FU reservation"); |
| 149 | case InstrStage::Required: |
| 150 | // Required FUs conflict with both reserved and required ones |
| 151 | freeUnits &= ~ReservedScoreboard[cycle + i]; |
| 152 | // FALLTHROUGH |
| 153 | case InstrStage::Reserved: |
| 154 | // Reserved FUs can conflict only with required ones. |
| 155 | freeUnits &= ~RequiredScoreboard[cycle + i]; |
| 156 | break; |
| 157 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 158 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 159 | // reduce to a single unit |
| 160 | unsigned freeUnit = 0; |
| 161 | do { |
| 162 | freeUnit = freeUnits; |
| 163 | freeUnits = freeUnit & (freeUnit - 1); |
| 164 | } while (freeUnits); |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 165 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 166 | assert(freeUnit && "No function unit available!"); |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 167 | if (IS->getReservationKind() == InstrStage::Required) |
| 168 | RequiredScoreboard[cycle + i] |= freeUnit; |
| 169 | else |
| 170 | ReservedScoreboard[cycle + i] |= freeUnit; |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 171 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 172 | |
David Goodwin | 74b7956 | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 173 | // Advance the cycle to the next stage. |
| 174 | cycle += IS->getNextCycles(); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 175 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 176 | |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 177 | DEBUG(ReservedScoreboard.dump()); |
| 178 | DEBUG(RequiredScoreboard.dump()); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 179 | } |
Anton Korobeynikov | 9a348a9 | 2010-04-07 18:19:24 +0000 | [diff] [blame] | 180 | |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 181 | void ScoreboardHazardRecognizer::AdvanceCycle() { |
Anton Korobeynikov | 0bdc634 | 2010-04-07 18:19:32 +0000 | [diff] [blame] | 182 | ReservedScoreboard[0] = 0; ReservedScoreboard.advance(); |
| 183 | RequiredScoreboard[0] = 0; RequiredScoreboard.advance(); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 184 | } |
Andrew Trick | 00067fb | 2010-12-08 20:04:29 +0000 | [diff] [blame] | 185 | |
| 186 | void ScoreboardHazardRecognizer::RecedeCycle() { |
| 187 | ReservedScoreboard[ReservedScoreboard.getDepth()-1] = 0; |
| 188 | ReservedScoreboard.recede(); |
| 189 | RequiredScoreboard[RequiredScoreboard.getDepth()-1] = 0; |
| 190 | RequiredScoreboard.recede(); |
| 191 | } |