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) { |
| 34 | // If the begin stage of an itinerary has 0 cycles and units, |
| 35 | // then we have reached the end of the itineraries. |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 36 | const InstrStage *IS = ItinData.beginStage(idx); |
| 37 | const InstrStage *E = ItinData.endStage(idx); |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 38 | if ((IS->getCycles() == 0) && (IS->getUnits() == 0)) |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 39 | break; |
| 40 | |
| 41 | unsigned ItinDepth = 0; |
| 42 | for (; IS != E; ++IS) |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 43 | ItinDepth += IS->getCycles(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 44 | |
| 45 | ScoreboardDepth = std::max(ScoreboardDepth, ItinDepth); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | Scoreboard = new unsigned[ScoreboardDepth]; |
| 50 | ScoreboardHead = 0; |
| 51 | |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 52 | DEBUG(errs() << "Using exact hazard recognizer: ScoreboardDepth = " |
Bill Wendling | fb98a43 | 2009-08-22 20:08:44 +0000 | [diff] [blame] | 53 | << ScoreboardDepth << '\n'); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | ExactHazardRecognizer::~ExactHazardRecognizer() { |
| 57 | delete Scoreboard; |
| 58 | } |
| 59 | |
| 60 | void ExactHazardRecognizer::Reset() { |
| 61 | memset(Scoreboard, 0, ScoreboardDepth * sizeof(unsigned)); |
| 62 | ScoreboardHead = 0; |
| 63 | } |
| 64 | |
| 65 | unsigned ExactHazardRecognizer::getFutureIndex(unsigned offset) { |
| 66 | return (ScoreboardHead + offset) % ScoreboardDepth; |
| 67 | } |
| 68 | |
| 69 | void ExactHazardRecognizer::dumpScoreboard() { |
Daniel Dunbar | d4d415a | 2009-08-11 06:22:47 +0000 | [diff] [blame] | 70 | errs() << "Scoreboard:\n"; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 71 | |
| 72 | unsigned last = ScoreboardDepth - 1; |
| 73 | while ((last > 0) && (Scoreboard[getFutureIndex(last)] == 0)) |
| 74 | last--; |
| 75 | |
| 76 | for (unsigned i = 0; i <= last; i++) { |
| 77 | unsigned FUs = Scoreboard[getFutureIndex(i)]; |
Daniel Dunbar | d4d415a | 2009-08-11 06:22:47 +0000 | [diff] [blame] | 78 | errs() << "\t"; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 79 | for (int j = 31; j >= 0; j--) |
Daniel Dunbar | d4d415a | 2009-08-11 06:22:47 +0000 | [diff] [blame] | 80 | errs() << ((FUs & (1 << j)) ? '1' : '0'); |
| 81 | errs() << '\n'; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) { |
| 86 | unsigned cycle = 0; |
| 87 | |
| 88 | // Use the itinerary for the underlying instruction to check for |
| 89 | // free FU's in the scoreboard at the appropriate future cycles. |
| 90 | unsigned idx = SU->getInstr()->getDesc().getSchedClass(); |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 91 | for (const InstrStage *IS = ItinData.beginStage(idx), |
| 92 | *E = ItinData.endStage(idx); IS != E; ++IS) { |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 93 | // We must find one of the stage's units free for every cycle the |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 94 | // stage is occupied. FIXME it would be more accurate to find the |
| 95 | // same unit free in all the cycles. |
| 96 | for (unsigned int i = 0; i < IS->getCycles(); ++i) { |
| 97 | assert(((cycle + i) < ScoreboardDepth) && |
| 98 | "Scoreboard depth exceeded!"); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 99 | |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 100 | unsigned index = getFutureIndex(cycle + i); |
| 101 | unsigned freeUnits = IS->getUnits() & ~Scoreboard[index]; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 102 | if (!freeUnits) { |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 103 | DEBUG(errs() << "*** Hazard in cycle " << (cycle + i) << ", "); |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 104 | DEBUG(errs() << "SU(" << SU->NodeNum << "): "); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 105 | DEBUG(SU->getInstr()->dump()); |
| 106 | return Hazard; |
| 107 | } |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 108 | } |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 109 | |
| 110 | // Advance the cycle to the next stage. |
| 111 | cycle += IS->getNextCycles(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return NoHazard; |
| 115 | } |
| 116 | |
| 117 | void ExactHazardRecognizer::EmitInstruction(SUnit *SU) { |
| 118 | unsigned cycle = 0; |
| 119 | |
| 120 | // Use the itinerary for the underlying instruction to reserve FU's |
| 121 | // in the scoreboard at the appropriate future cycles. |
| 122 | unsigned idx = SU->getInstr()->getDesc().getSchedClass(); |
David Goodwin | fac8541 | 2009-08-17 16:02:57 +0000 | [diff] [blame] | 123 | for (const InstrStage *IS = ItinData.beginStage(idx), |
| 124 | *E = ItinData.endStage(idx); IS != E; ++IS) { |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 125 | // We must reserve one of the stage's units for every cycle the |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 126 | // stage is occupied. FIXME it would be more accurate to reserve |
| 127 | // the same unit free in all the cycles. |
| 128 | for (unsigned int i = 0; i < IS->getCycles(); ++i) { |
| 129 | assert(((cycle + i) < ScoreboardDepth) && |
| 130 | "Scoreboard depth exceeded!"); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 131 | |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 132 | unsigned index = getFutureIndex(cycle + i); |
| 133 | unsigned freeUnits = IS->getUnits() & ~Scoreboard[index]; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 134 | |
| 135 | // reduce to a single unit |
| 136 | unsigned freeUnit = 0; |
| 137 | do { |
| 138 | freeUnit = freeUnits; |
| 139 | freeUnits = freeUnit & (freeUnit - 1); |
| 140 | } while (freeUnits); |
| 141 | |
| 142 | assert(freeUnit && "No function unit available!"); |
| 143 | Scoreboard[index] |= freeUnit; |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 144 | } |
David Goodwin | 1a8f36e | 2009-08-12 18:31:53 +0000 | [diff] [blame] | 145 | |
| 146 | // Advance the cycle to the next stage. |
| 147 | cycle += IS->getNextCycles(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | DEBUG(dumpScoreboard()); |
| 151 | } |
| 152 | |
| 153 | void ExactHazardRecognizer::AdvanceCycle() { |
| 154 | Scoreboard[ScoreboardHead] = 0; |
| 155 | ScoreboardHead = getFutureIndex(1); |
| 156 | } |