David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 1 | //===----- ExactHazardRecognizer.cpp - hazard recognizer -------- ---------===// |
| 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 | // |
| 10 | // This implements a a hazard recognizer using the instructions itineraries |
| 11 | // defined for the current target. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "exact-hazards" |
| 16 | #include "ExactHazardRecognizer.h" |
| 17 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
| 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetInstrItineraries.h" |
| 22 | |
Bill Wendling | fb98a43 | 2009-08-22 20:08:44 +0000 | [diff] [blame] | 23 | using namespace llvm; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 24 | |
| 25 | ExactHazardRecognizer::ExactHazardRecognizer(const InstrItineraryData &LItinData) : |
| 26 | ScheduleHazardRecognizer(), ItinData(LItinData) |
| 27 | { |
| 28 | // Determine the maximum depth of any itinerary. This determines the |
| 29 | // depth of the scoreboard. We always make the scoreboard at least 1 |
| 30 | // cycle deep to avoid dealing with the boundary condition. |
| 31 | ScoreboardDepth = 1; |
| 32 | if (!ItinData.isEmpty()) { |
| 33 | for (unsigned idx = 0; ; ++idx) { |
David Goodwin | 1f52895 | 2009-09-24 20:22:50 +0000 | [diff] [blame] | 34 | if (ItinData.isEndMarker(idx)) |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 35 | break; |
| 36 | |
David Goodwin | 1f52895 | 2009-09-24 20:22:50 +0000 | [diff] [blame] | 37 | const InstrStage *IS = ItinData.beginStage(idx); |
| 38 | const InstrStage *E = ItinData.endStage(idx); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 39 | unsigned ItinDepth = 0; |
| 40 | for (; IS != E; ++IS) |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 41 | ItinDepth += IS->getCycles(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 42 | |
| 43 | ScoreboardDepth = std::max(ScoreboardDepth, ItinDepth); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | Scoreboard = new unsigned[ScoreboardDepth]; |
| 48 | ScoreboardHead = 0; |
| 49 | |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 50 | DEBUG(errs() << "Using exact hazard recognizer: ScoreboardDepth = " |
Bill Wendling | fb98a43 | 2009-08-22 20:08:44 +0000 | [diff] [blame] | 51 | << ScoreboardDepth << '\n'); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | ExactHazardRecognizer::~ExactHazardRecognizer() { |
Duncan Sands | 70327da | 2009-09-04 11:59:43 +0000 | [diff] [blame] | 55 | delete [] Scoreboard; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void ExactHazardRecognizer::Reset() { |
| 59 | memset(Scoreboard, 0, ScoreboardDepth * sizeof(unsigned)); |
| 60 | ScoreboardHead = 0; |
| 61 | } |
| 62 | |
| 63 | unsigned ExactHazardRecognizer::getFutureIndex(unsigned offset) { |
| 64 | return (ScoreboardHead + offset) % ScoreboardDepth; |
| 65 | } |
| 66 | |
| 67 | void ExactHazardRecognizer::dumpScoreboard() { |
Daniel Dunbar | d4d415a | 2009-08-11 06:22:47 +0000 | [diff] [blame] | 68 | errs() << "Scoreboard:\n"; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 69 | |
| 70 | unsigned last = ScoreboardDepth - 1; |
| 71 | while ((last > 0) && (Scoreboard[getFutureIndex(last)] == 0)) |
| 72 | last--; |
| 73 | |
| 74 | for (unsigned i = 0; i <= last; i++) { |
| 75 | unsigned FUs = Scoreboard[getFutureIndex(i)]; |
Daniel Dunbar | d4d415a | 2009-08-11 06:22:47 +0000 | [diff] [blame] | 76 | errs() << "\t"; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 77 | for (int j = 31; j >= 0; j--) |
Daniel Dunbar | d4d415a | 2009-08-11 06:22:47 +0000 | [diff] [blame] | 78 | errs() << ((FUs & (1 << j)) ? '1' : '0'); |
| 79 | errs() << '\n'; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) { |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 84 | if (ItinData.isEmpty()) |
| 85 | return NoHazard; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 86 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 87 | unsigned cycle = 0; |
| 88 | |
| 89 | // Use the itinerary for the underlying instruction to check for |
| 90 | // free FU's in the scoreboard at the appropriate future cycles. |
| 91 | unsigned idx = SU->getInstr()->getDesc().getSchedClass(); |
| 92 | for (const InstrStage *IS = ItinData.beginStage(idx), |
| 93 | *E = ItinData.endStage(idx); IS != E; ++IS) { |
| 94 | // We must find one of the stage's units free for every cycle the |
| 95 | // stage is occupied. FIXME it would be more accurate to find the |
| 96 | // same unit free in all the cycles. |
| 97 | for (unsigned int i = 0; i < IS->getCycles(); ++i) { |
| 98 | assert(((cycle + i) < ScoreboardDepth) && |
| 99 | "Scoreboard depth exceeded!"); |
David Goodwin | 4f7228f | 2009-09-03 22:48:51 +0000 | [diff] [blame] | 100 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 101 | unsigned index = getFutureIndex(cycle + i); |
| 102 | unsigned freeUnits = IS->getUnits() & ~Scoreboard[index]; |
| 103 | if (!freeUnits) { |
| 104 | DEBUG(errs() << "*** Hazard in cycle " << (cycle + i) << ", "); |
| 105 | DEBUG(errs() << "SU(" << SU->NodeNum << "): "); |
| 106 | DEBUG(SU->getInstr()->dump()); |
| 107 | return Hazard; |
| 108 | } |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 109 | } |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 110 | |
| 111 | // Advance the cycle to the next stage. |
| 112 | cycle += IS->getNextCycles(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return NoHazard; |
| 116 | } |
| 117 | |
| 118 | void ExactHazardRecognizer::EmitInstruction(SUnit *SU) { |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 119 | if (ItinData.isEmpty()) |
| 120 | return; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 121 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 122 | unsigned cycle = 0; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 123 | |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 124 | // Use the itinerary for the underlying instruction to reserve FU's |
| 125 | // in the scoreboard at the appropriate future cycles. |
| 126 | unsigned idx = SU->getInstr()->getDesc().getSchedClass(); |
| 127 | for (const InstrStage *IS = ItinData.beginStage(idx), |
| 128 | *E = ItinData.endStage(idx); IS != E; ++IS) { |
| 129 | // We must reserve one of the stage's units for every cycle the |
| 130 | // stage is occupied. FIXME it would be more accurate to reserve |
| 131 | // the same unit free in all the cycles. |
| 132 | for (unsigned int i = 0; i < IS->getCycles(); ++i) { |
| 133 | assert(((cycle + i) < ScoreboardDepth) && |
| 134 | "Scoreboard depth exceeded!"); |
| 135 | |
| 136 | unsigned index = getFutureIndex(cycle + i); |
| 137 | unsigned freeUnits = IS->getUnits() & ~Scoreboard[index]; |
| 138 | |
| 139 | // reduce to a single unit |
| 140 | unsigned freeUnit = 0; |
| 141 | do { |
| 142 | freeUnit = freeUnits; |
| 143 | freeUnits = freeUnit & (freeUnit - 1); |
| 144 | } while (freeUnits); |
| 145 | |
| 146 | assert(freeUnit && "No function unit available!"); |
| 147 | Scoreboard[index] |= freeUnit; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 148 | } |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 149 | |
| 150 | // Advance the cycle to the next stage. |
| 151 | cycle += IS->getNextCycles(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 152 | } |
David Goodwin | 047ae2f | 2009-09-22 16:47:52 +0000 | [diff] [blame] | 153 | |
| 154 | DEBUG(dumpScoreboard()); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void ExactHazardRecognizer::AdvanceCycle() { |
| 158 | Scoreboard[ScoreboardHead] = 0; |
| 159 | ScoreboardHead = getFutureIndex(1); |
| 160 | } |