blob: 52908edefc880cc580d4ad2e44a4beb62c78c02e [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//
Dan Gohmanf451cb82010-02-10 16:03:48 +000010// This implements a hazard recognizer using the instructions itineraries
David Goodwind94a4e52009-08-10 15:55:25 +000011// defined for the current target.
12//
13//===----------------------------------------------------------------------===//
14
David Goodwin4de099d2009-11-03 20:57:50 +000015#define DEBUG_TYPE "post-RA-sched"
David Goodwind94a4e52009-08-10 15:55:25 +000016#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
Bill Wendlingfb98a432009-08-22 20:08:44 +000023using namespace llvm;
David Goodwind94a4e52009-08-10 15:55:25 +000024
Evan Cheng449c25b2009-10-16 05:18:39 +000025ExactHazardRecognizer::
26ExactHazardRecognizer(const InstrItineraryData &LItinData) :
David Goodwind94a4e52009-08-10 15:55:25 +000027 ScheduleHazardRecognizer(), ItinData(LItinData)
28{
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.
Anton Korobeynikov12989482010-04-07 18:19:24 +000032 unsigned ScoreboardDepth = 1;
David Goodwind94a4e52009-08-10 15:55:25 +000033 if (!ItinData.isEmpty()) {
34 for (unsigned idx = 0; ; ++idx) {
David Goodwin1f528952009-09-24 20:22:50 +000035 if (ItinData.isEndMarker(idx))
David Goodwind94a4e52009-08-10 15:55:25 +000036 break;
37
David Goodwin1f528952009-09-24 20:22:50 +000038 const InstrStage *IS = ItinData.beginStage(idx);
39 const InstrStage *E = ItinData.endStage(idx);
David Goodwind94a4e52009-08-10 15:55:25 +000040 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
Anton Korobeynikov12989482010-04-07 18:19:24 +000048 Scoreboard.reset(ScoreboardDepth);
David Goodwind94a4e52009-08-10 15:55:25 +000049
David Greene73242dd2010-01-04 21:26:07 +000050 DEBUG(dbgs() << "Using exact hazard recognizer: ScoreboardDepth = "
Bill Wendlingfb98a432009-08-22 20:08:44 +000051 << ScoreboardDepth << '\n');
David Goodwind94a4e52009-08-10 15:55:25 +000052}
53
David Goodwind94a4e52009-08-10 15:55:25 +000054void ExactHazardRecognizer::Reset() {
Anton Korobeynikov12989482010-04-07 18:19:24 +000055 Scoreboard.reset();
David Goodwind94a4e52009-08-10 15:55:25 +000056}
57
Anton Korobeynikov12989482010-04-07 18:19:24 +000058void ExactHazardRecognizer::ScoreBoard::dump() const {
David Greene73242dd2010-01-04 21:26:07 +000059 dbgs() << "Scoreboard:\n";
Anton Korobeynikov12989482010-04-07 18:19:24 +000060
61 unsigned last = Depth - 1;
62 while ((last > 0) && ((*this)[last] == 0))
David Goodwind94a4e52009-08-10 15:55:25 +000063 last--;
64
65 for (unsigned i = 0; i <= last; i++) {
Anton Korobeynikov12989482010-04-07 18:19:24 +000066 unsigned FUs = (*this)[i];
David Greene73242dd2010-01-04 21:26:07 +000067 dbgs() << "\t";
David Goodwind94a4e52009-08-10 15:55:25 +000068 for (int j = 31; j >= 0; j--)
David Greene73242dd2010-01-04 21:26:07 +000069 dbgs() << ((FUs & (1 << j)) ? '1' : '0');
70 dbgs() << '\n';
David Goodwind94a4e52009-08-10 15:55:25 +000071 }
72}
73
74ExactHazardRecognizer::HazardType ExactHazardRecognizer::getHazardType(SUnit *SU) {
David Goodwin047ae2f2009-09-22 16:47:52 +000075 if (ItinData.isEmpty())
76 return NoHazard;
David Goodwind94a4e52009-08-10 15:55:25 +000077
David Goodwin047ae2f2009-09-22 16:47:52 +000078 unsigned cycle = 0;
79
80 // Use the itinerary for the underlying instruction to check for
81 // free FU's in the scoreboard at the appropriate future cycles.
82 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
83 for (const InstrStage *IS = ItinData.beginStage(idx),
84 *E = ItinData.endStage(idx); IS != E; ++IS) {
85 // We must find one of the stage's units free for every cycle the
86 // stage is occupied. FIXME it would be more accurate to find the
87 // same unit free in all the cycles.
88 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov12989482010-04-07 18:19:24 +000089 assert(((cycle + i) < Scoreboard.getDepth()) &&
David Goodwin047ae2f2009-09-22 16:47:52 +000090 "Scoreboard depth exceeded!");
Anton Korobeynikov12989482010-04-07 18:19:24 +000091
92 unsigned freeUnits = IS->getUnits() & ~Scoreboard[cycle + i];
David Goodwin047ae2f2009-09-22 16:47:52 +000093 if (!freeUnits) {
David Greene73242dd2010-01-04 21:26:07 +000094 DEBUG(dbgs() << "*** Hazard in cycle " << (cycle + i) << ", ");
95 DEBUG(dbgs() << "SU(" << SU->NodeNum << "): ");
David Goodwin047ae2f2009-09-22 16:47:52 +000096 DEBUG(SU->getInstr()->dump());
97 return Hazard;
98 }
David Goodwind94a4e52009-08-10 15:55:25 +000099 }
Anton Korobeynikov12989482010-04-07 18:19:24 +0000100
David Goodwin047ae2f2009-09-22 16:47:52 +0000101 // Advance the cycle to the next stage.
102 cycle += IS->getNextCycles();
David Goodwind94a4e52009-08-10 15:55:25 +0000103 }
104
105 return NoHazard;
106}
Anton Korobeynikov12989482010-04-07 18:19:24 +0000107
David Goodwind94a4e52009-08-10 15:55:25 +0000108void ExactHazardRecognizer::EmitInstruction(SUnit *SU) {
David Goodwin047ae2f2009-09-22 16:47:52 +0000109 if (ItinData.isEmpty())
110 return;
David Goodwind94a4e52009-08-10 15:55:25 +0000111
David Goodwin047ae2f2009-09-22 16:47:52 +0000112 unsigned cycle = 0;
David Goodwind94a4e52009-08-10 15:55:25 +0000113
David Goodwin047ae2f2009-09-22 16:47:52 +0000114 // Use the itinerary for the underlying instruction to reserve FU's
115 // in the scoreboard at the appropriate future cycles.
116 unsigned idx = SU->getInstr()->getDesc().getSchedClass();
117 for (const InstrStage *IS = ItinData.beginStage(idx),
118 *E = ItinData.endStage(idx); IS != E; ++IS) {
119 // We must reserve one of the stage's units for every cycle the
120 // stage is occupied. FIXME it would be more accurate to reserve
121 // the same unit free in all the cycles.
122 for (unsigned int i = 0; i < IS->getCycles(); ++i) {
Anton Korobeynikov12989482010-04-07 18:19:24 +0000123 assert(((cycle + i) < Scoreboard.getDepth()) &&
David Goodwin047ae2f2009-09-22 16:47:52 +0000124 "Scoreboard depth exceeded!");
Anton Korobeynikov12989482010-04-07 18:19:24 +0000125
126 unsigned freeUnits = IS->getUnits() & ~Scoreboard[cycle + i];
127
David Goodwin047ae2f2009-09-22 16:47:52 +0000128 // reduce to a single unit
129 unsigned freeUnit = 0;
130 do {
131 freeUnit = freeUnits;
132 freeUnits = freeUnit & (freeUnit - 1);
133 } while (freeUnits);
Anton Korobeynikov12989482010-04-07 18:19:24 +0000134
David Goodwin047ae2f2009-09-22 16:47:52 +0000135 assert(freeUnit && "No function unit available!");
Anton Korobeynikov12989482010-04-07 18:19:24 +0000136 Scoreboard[cycle + i] |= freeUnit;
David Goodwind94a4e52009-08-10 15:55:25 +0000137 }
Anton Korobeynikov12989482010-04-07 18:19:24 +0000138
David Goodwin047ae2f2009-09-22 16:47:52 +0000139 // Advance the cycle to the next stage.
140 cycle += IS->getNextCycles();
David Goodwind94a4e52009-08-10 15:55:25 +0000141 }
Anton Korobeynikov12989482010-04-07 18:19:24 +0000142
143 DEBUG(Scoreboard.dump());
David Goodwind94a4e52009-08-10 15:55:25 +0000144}
Anton Korobeynikov12989482010-04-07 18:19:24 +0000145
David Goodwind94a4e52009-08-10 15:55:25 +0000146void ExactHazardRecognizer::AdvanceCycle() {
Anton Korobeynikov12989482010-04-07 18:19:24 +0000147 Scoreboard[0] = 0;
148 Scoreboard.advance();
David Goodwind94a4e52009-08-10 15:55:25 +0000149}