1 //===----- ExactHazardRecognizer.cpp - hazard recognizer -------- ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements a a hazard recognizer using the instructions itineraries
11 // defined for the current target.
13 //===----------------------------------------------------------------------===//
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"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Target/TargetInstrItineraries.h"
25 ExactHazardRecognizer::
26 ExactHazardRecognizer(const InstrItineraryData &LItinData) :
27 ScheduleHazardRecognizer(), ItinData(LItinData)
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.
33 if (!ItinData.isEmpty()) {
34 for (unsigned idx = 0; ; ++idx) {
35 if (ItinData.isEndMarker(idx))
38 const InstrStage *IS = ItinData.beginStage(idx);
39 const InstrStage *E = ItinData.endStage(idx);
40 unsigned ItinDepth = 0;
42 ItinDepth += IS->getCycles();
44 ScoreboardDepth = std::max(ScoreboardDepth, ItinDepth);
48 Scoreboard = new unsigned[ScoreboardDepth];
51 DEBUG(errs() << "Using exact hazard recognizer: ScoreboardDepth = "
52 << ScoreboardDepth << '\n');
55 ExactHazardRecognizer::~ExactHazardRecognizer() {
59 void ExactHazardRecognizer::Reset() {
60 memset(Scoreboard, 0, ScoreboardDepth * sizeof(unsigned));
64 unsigned ExactHazardRecognizer::getFutureIndex(unsigned offset) {
65 return (ScoreboardHead + offset) % ScoreboardDepth;
68 void ExactHazardRecognizer::dumpScoreboard() {
69 errs() << "Scoreboard:\n";
71 unsigned last = ScoreboardDepth - 1;
72 while ((last > 0) && (Scoreboard[getFutureIndex(last)] == 0))
75 for (unsigned i = 0; i <= last; i++) {
76 unsigned FUs = Scoreboard[getFutureIndex(i)];
78 for (int j = 31; j >= 0; j--)
79 errs() << ((FUs & (1 << j)) ? '1' : '0');
84 ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) {
85 if (ItinData.isEmpty())
90 // Use the itinerary for the underlying instruction to check for
91 // free FU's in the scoreboard at the appropriate future cycles.
92 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
93 for (const InstrStage *IS = ItinData.beginStage(idx),
94 *E = ItinData.endStage(idx); IS != E; ++IS) {
95 // We must find one of the stage's units free for every cycle the
96 // stage is occupied. FIXME it would be more accurate to find the
97 // same unit free in all the cycles.
98 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
99 assert(((cycle + i) < ScoreboardDepth) &&
100 "Scoreboard depth exceeded!");
102 unsigned index = getFutureIndex(cycle + i);
103 unsigned freeUnits = IS->getUnits() & ~Scoreboard[index];
105 DEBUG(errs() << "*** Hazard in cycle " << (cycle + i) << ", ");
106 DEBUG(errs() << "SU(" << SU->NodeNum << "): ");
107 DEBUG(SU->getInstr()->dump());
112 // Advance the cycle to the next stage.
113 cycle += IS->getNextCycles();
119 void ExactHazardRecognizer::EmitInstruction(SUnit *SU) {
120 if (ItinData.isEmpty())
125 // Use the itinerary for the underlying instruction to reserve FU's
126 // in the scoreboard at the appropriate future cycles.
127 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
128 for (const InstrStage *IS = ItinData.beginStage(idx),
129 *E = ItinData.endStage(idx); IS != E; ++IS) {
130 // We must reserve one of the stage's units for every cycle the
131 // stage is occupied. FIXME it would be more accurate to reserve
132 // the same unit free in all the cycles.
133 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
134 assert(((cycle + i) < ScoreboardDepth) &&
135 "Scoreboard depth exceeded!");
137 unsigned index = getFutureIndex(cycle + i);
138 unsigned freeUnits = IS->getUnits() & ~Scoreboard[index];
140 // reduce to a single unit
141 unsigned freeUnit = 0;
143 freeUnit = freeUnits;
144 freeUnits = freeUnit & (freeUnit - 1);
147 assert(freeUnit && "No function unit available!");
148 Scoreboard[index] |= freeUnit;
151 // Advance the cycle to the next stage.
152 cycle += IS->getNextCycles();
155 DEBUG(dumpScoreboard());
158 void ExactHazardRecognizer::AdvanceCycle() {
159 Scoreboard[ScoreboardHead] = 0;
160 ScoreboardHead = getFutureIndex(1);