add some missing quotes in debug output
[llvm/avr.git] / lib / CodeGen / ExactHazardRecognizer.cpp
blob057d7ef6b926aaff867858278912dae5978296cd
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.
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"
23 using namespace llvm;
25 ExactHazardRecognizer::ExactHazardRecognizer(const InstrItineraryData &LItinData) :
26 ScheduleHazardRecognizer(), ItinData(LItinData)
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.
36 const InstrStage *IS = ItinData.beginStage(idx);
37 const InstrStage *E = ItinData.endStage(idx);
38 if ((IS->getCycles() == 0) && (IS->getUnits() == 0))
39 break;
41 unsigned ItinDepth = 0;
42 for (; IS != E; ++IS)
43 ItinDepth += IS->getCycles();
45 ScoreboardDepth = std::max(ScoreboardDepth, ItinDepth);
49 Scoreboard = new unsigned[ScoreboardDepth];
50 ScoreboardHead = 0;
52 DEBUG(errs() << "Using exact hazard recognizer: ScoreboardDepth = "
53 << ScoreboardDepth << '\n');
56 ExactHazardRecognizer::~ExactHazardRecognizer() {
57 delete [] Scoreboard;
60 void ExactHazardRecognizer::Reset() {
61 memset(Scoreboard, 0, ScoreboardDepth * sizeof(unsigned));
62 ScoreboardHead = 0;
65 unsigned ExactHazardRecognizer::getFutureIndex(unsigned offset) {
66 return (ScoreboardHead + offset) % ScoreboardDepth;
69 void ExactHazardRecognizer::dumpScoreboard() {
70 errs() << "Scoreboard:\n";
72 unsigned last = ScoreboardDepth - 1;
73 while ((last > 0) && (Scoreboard[getFutureIndex(last)] == 0))
74 last--;
76 for (unsigned i = 0; i <= last; i++) {
77 unsigned FUs = Scoreboard[getFutureIndex(i)];
78 errs() << "\t";
79 for (int j = 31; j >= 0; j--)
80 errs() << ((FUs & (1 << j)) ? '1' : '0');
81 errs() << '\n';
85 ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) {
86 if (!ItinData.isEmpty()) {
87 unsigned cycle = 0;
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!");
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;
111 // Advance the cycle to the next stage.
112 cycle += IS->getNextCycles();
116 return NoHazard;
119 void ExactHazardRecognizer::EmitInstruction(SUnit *SU) {
120 if (!ItinData.isEmpty()) {
121 unsigned cycle = 0;
123 // Use the itinerary for the underlying instruction to reserve FU's
124 // in the scoreboard at the appropriate future cycles.
125 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
126 for (const InstrStage *IS = ItinData.beginStage(idx),
127 *E = ItinData.endStage(idx); IS != E; ++IS) {
128 // We must reserve one of the stage's units for every cycle the
129 // stage is occupied. FIXME it would be more accurate to reserve
130 // the same unit free in all the cycles.
131 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
132 assert(((cycle + i) < ScoreboardDepth) &&
133 "Scoreboard depth exceeded!");
135 unsigned index = getFutureIndex(cycle + i);
136 unsigned freeUnits = IS->getUnits() & ~Scoreboard[index];
138 // reduce to a single unit
139 unsigned freeUnit = 0;
140 do {
141 freeUnit = freeUnits;
142 freeUnits = freeUnit & (freeUnit - 1);
143 } while (freeUnits);
145 assert(freeUnit && "No function unit available!");
146 Scoreboard[index] |= freeUnit;
149 // Advance the cycle to the next stage.
150 cycle += IS->getNextCycles();
153 DEBUG(dumpScoreboard());
157 void ExactHazardRecognizer::AdvanceCycle() {
158 Scoreboard[ScoreboardHead] = 0;
159 ScoreboardHead = getFutureIndex(1);