blob: 8bac08a4a05667be51715160e4f6167f1a921849 [file] [log] [blame]
David Goodwind94a4e52009-08-10 15:55:25 +00001//===----- 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 Goodwin3a5f0d42009-08-11 01:44:26 +000020#include "llvm/Support/raw_ostream.h"
David Goodwind94a4e52009-08-10 15:55:25 +000021#include "llvm/Target/TargetInstrItineraries.h"
22
23namespace llvm {
24
25ExactHazardRecognizer::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.
36 const InstrStage *IS = ItinData.begin(idx), *E = ItinData.end(idx);
David Goodwin1a8f36e2009-08-12 18:31:53 +000037 if ((IS->getCycles() == 0) && (IS->getUnits() == 0))
David Goodwind94a4e52009-08-10 15:55:25 +000038 break;
39
40 unsigned ItinDepth = 0;
41 for (; IS != E; ++IS)
David Goodwin1a8f36e2009-08-12 18:31:53 +000042 ItinDepth += IS->getCycles();
David Goodwind94a4e52009-08-10 15:55:25 +000043
44 ScoreboardDepth = std::max(ScoreboardDepth, ItinDepth);
45 }
46 }
47
48 Scoreboard = new unsigned[ScoreboardDepth];
49 ScoreboardHead = 0;
50
David Goodwin3a5f0d42009-08-11 01:44:26 +000051 DEBUG(errs() << "Using exact hazard recognizer: ScoreboardDepth = "
52 << ScoreboardDepth << '\n');
David Goodwind94a4e52009-08-10 15:55:25 +000053}
54
55ExactHazardRecognizer::~ExactHazardRecognizer() {
56 delete Scoreboard;
57}
58
59void ExactHazardRecognizer::Reset() {
60 memset(Scoreboard, 0, ScoreboardDepth * sizeof(unsigned));
61 ScoreboardHead = 0;
62}
63
64unsigned ExactHazardRecognizer::getFutureIndex(unsigned offset) {
65 return (ScoreboardHead + offset) % ScoreboardDepth;
66}
67
68void ExactHazardRecognizer::dumpScoreboard() {
Daniel Dunbard4d415a2009-08-11 06:22:47 +000069 errs() << "Scoreboard:\n";
David Goodwind94a4e52009-08-10 15:55:25 +000070
71 unsigned last = ScoreboardDepth - 1;
72 while ((last > 0) && (Scoreboard[getFutureIndex(last)] == 0))
73 last--;
74
75 for (unsigned i = 0; i <= last; i++) {
76 unsigned FUs = Scoreboard[getFutureIndex(i)];
Daniel Dunbard4d415a2009-08-11 06:22:47 +000077 errs() << "\t";
David Goodwind94a4e52009-08-10 15:55:25 +000078 for (int j = 31; j >= 0; j--)
Daniel Dunbard4d415a2009-08-11 06:22:47 +000079 errs() << ((FUs & (1 << j)) ? '1' : '0');
80 errs() << '\n';
David Goodwind94a4e52009-08-10 15:55:25 +000081 }
82}
83
84ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) {
85 unsigned cycle = 0;
86
87 // Use the itinerary for the underlying instruction to check for
88 // free FU's in the scoreboard at the appropriate future cycles.
89 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
90 for (const InstrStage *IS = ItinData.begin(idx), *E = ItinData.end(idx);
91 IS != E; ++IS) {
92 // We must find one of the stage's units free for every cycle the
David Goodwin1a8f36e2009-08-12 18:31:53 +000093 // stage is occupied. FIXME it would be more accurate to find the
94 // same unit free in all the cycles.
95 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
96 assert(((cycle + i) < ScoreboardDepth) &&
97 "Scoreboard depth exceeded!");
David Goodwind94a4e52009-08-10 15:55:25 +000098
David Goodwin1a8f36e2009-08-12 18:31:53 +000099 unsigned index = getFutureIndex(cycle + i);
100 unsigned freeUnits = IS->getUnits() & ~Scoreboard[index];
David Goodwind94a4e52009-08-10 15:55:25 +0000101 if (!freeUnits) {
David Goodwin1a8f36e2009-08-12 18:31:53 +0000102 DEBUG(errs() << "*** Hazard in cycle " << (cycle + i) << ", ");
David Goodwin3a5f0d42009-08-11 01:44:26 +0000103 DEBUG(errs() << "SU(" << SU->NodeNum << "): ");
David Goodwind94a4e52009-08-10 15:55:25 +0000104 DEBUG(SU->getInstr()->dump());
105 return Hazard;
106 }
David Goodwind94a4e52009-08-10 15:55:25 +0000107 }
David Goodwin1a8f36e2009-08-12 18:31:53 +0000108
109 // Advance the cycle to the next stage.
110 cycle += IS->getNextCycles();
David Goodwind94a4e52009-08-10 15:55:25 +0000111 }
112
113 return NoHazard;
114}
115
116void ExactHazardRecognizer::EmitInstruction(SUnit *SU) {
117 unsigned cycle = 0;
118
119 // Use the itinerary for the underlying instruction to reserve FU's
120 // in the scoreboard at the appropriate future cycles.
121 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
122 for (const InstrStage *IS = ItinData.begin(idx), *E = ItinData.end(idx);
123 IS != E; ++IS) {
124 // We must reserve one of the stage's units for every cycle the
David Goodwin1a8f36e2009-08-12 18:31:53 +0000125 // stage is occupied. FIXME it would be more accurate to reserve
126 // the same unit free in all the cycles.
127 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
128 assert(((cycle + i) < ScoreboardDepth) &&
129 "Scoreboard depth exceeded!");
David Goodwind94a4e52009-08-10 15:55:25 +0000130
David Goodwin1a8f36e2009-08-12 18:31:53 +0000131 unsigned index = getFutureIndex(cycle + i);
132 unsigned freeUnits = IS->getUnits() & ~Scoreboard[index];
David Goodwind94a4e52009-08-10 15:55:25 +0000133
134 // reduce to a single unit
135 unsigned freeUnit = 0;
136 do {
137 freeUnit = freeUnits;
138 freeUnits = freeUnit & (freeUnit - 1);
139 } while (freeUnits);
140
141 assert(freeUnit && "No function unit available!");
142 Scoreboard[index] |= freeUnit;
David Goodwind94a4e52009-08-10 15:55:25 +0000143 }
David Goodwin1a8f36e2009-08-12 18:31:53 +0000144
145 // Advance the cycle to the next stage.
146 cycle += IS->getNextCycles();
David Goodwind94a4e52009-08-10 15:55:25 +0000147 }
148
149 DEBUG(dumpScoreboard());
150}
151
152void ExactHazardRecognizer::AdvanceCycle() {
153 Scoreboard[ScoreboardHead] = 0;
154 ScoreboardHead = getFutureIndex(1);
155}
156
157} /* namespace llvm */