blob: 5aaffbc8282261c7dfe52aeb8d7d96ff9d8d2f6e [file] [log] [blame]
Vikram S. Adve0799fc42001-09-18 12:58:33 +00001//===-- SchedInfo.cpp - Generic code to support target schedulers ----------==//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Vikram S. Adve0799fc42001-09-18 12:58:33 +00009//
10// This file implements the generic part of a Scheduler description for a
11// target. This functionality is defined in the llvm/Target/SchedInfo.h file.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerd0f166a2002-12-29 03:13:05 +000015#include "llvm/Target/TargetSchedInfo.h"
Chris Lattner884f4b52002-02-03 07:47:05 +000016#include "llvm/Target/TargetMachine.h"
Alkis Evlogimenos0ee6e2a2004-09-28 02:47:38 +000017#include <algorithm>
Tanya Lattner6b160502004-05-08 16:12:50 +000018#include <iostream>
19using namespace llvm;
Vikram S. Adve0799fc42001-09-18 12:58:33 +000020
Tanya Lattner6b160502004-05-08 16:12:50 +000021resourceId_t llvm::CPUResource::nextId = 0;
22static std::vector<CPUResource*> *CPUResourceMap = 0;
23
Chris Lattner55a47002004-05-01 11:17:13 +000024CPUResource::CPUResource(const std::string& resourceName, int maxUsers)
Tanya Lattner6b160502004-05-08 16:12:50 +000025 : rname(resourceName), rid(nextId++), maxNumUsers(maxUsers) {
26 if(!CPUResourceMap)
27 CPUResourceMap = new std::vector<CPUResource*>;
28
29 //Put Resource in the map
30 CPUResourceMap->push_back(this);
31}
32
33///Get CPUResource if you only have the resource ID
34CPUResource* CPUResource::getCPUResource(resourceId_t id) {
35 return (*CPUResourceMap)[id];
36}
Chris Lattner55a47002004-05-01 11:17:13 +000037
Vikram S. Adve0799fc42001-09-18 12:58:33 +000038// Check if fromRVec and toRVec have *any* common entries.
39// Assume the vectors are sorted in increasing order.
40// Algorithm copied from function set_intersection() for sorted ranges
41// (stl_algo.h).
42//
43inline static bool
Chris Lattner697954c2002-01-20 22:54:45 +000044RUConflict(const std::vector<resourceId_t>& fromRVec,
45 const std::vector<resourceId_t>& toRVec)
Vikram S. Adve0799fc42001-09-18 12:58:33 +000046{
47
48 unsigned fN = fromRVec.size(), tN = toRVec.size();
49 unsigned fi = 0, ti = 0;
50
Misha Brukmanb10cea82003-08-05 00:02:06 +000051 while (fi < fN && ti < tN) {
52 if (fromRVec[fi] < toRVec[ti])
53 ++fi;
54 else if (toRVec[ti] < fromRVec[fi])
55 ++ti;
56 else
57 return true;
58 }
Vikram S. Adve0799fc42001-09-18 12:58:33 +000059 return false;
60}
61
62
63static cycles_t
64ComputeMinGap(const InstrRUsage &fromRU,
65 const InstrRUsage &toRU)
66{
67 cycles_t minGap = 0;
68
69 if (fromRU.numBubbles > 0)
70 minGap = fromRU.numBubbles;
71
Misha Brukmanb10cea82003-08-05 00:02:06 +000072 if (minGap < fromRU.numCycles) {
73 // only need to check from cycle `minGap' onwards
74 for (cycles_t gap=minGap; gap <= fromRU.numCycles-1; gap++) {
75 // check if instr. #2 can start executing `gap' cycles after #1
76 // by checking for resource conflicts in each overlapping cycle
77 cycles_t numOverlap =std::min(fromRU.numCycles - gap, toRU.numCycles);
78 for (cycles_t c = 0; c <= numOverlap-1; c++)
79 if (RUConflict(fromRU.resourcesByCycle[gap + c],
80 toRU.resourcesByCycle[c])) {
81 // conflict found so minGap must be more than `gap'
82 minGap = gap+1;
83 break;
84 }
Vikram S. Adve0799fc42001-09-18 12:58:33 +000085 }
Misha Brukmanb10cea82003-08-05 00:02:06 +000086 }
Vikram S. Adve0799fc42001-09-18 12:58:33 +000087
88 return minGap;
89}
90
91
92//---------------------------------------------------------------------------
Chris Lattnerd0f166a2002-12-29 03:13:05 +000093// class TargetSchedInfo
Vikram S. Adve0799fc42001-09-18 12:58:33 +000094// Interface to machine description for instruction scheduling
95//---------------------------------------------------------------------------
96
Chris Lattnerd0f166a2002-12-29 03:13:05 +000097TargetSchedInfo::TargetSchedInfo(const TargetMachine& tgt,
98 int NumSchedClasses,
99 const InstrClassRUsage* ClassRUsages,
100 const InstrRUsageDelta* UsageDeltas,
101 const InstrIssueDelta* IssueDeltas,
102 unsigned NumUsageDeltas,
103 unsigned NumIssueDeltas)
Vikram S. Adve7a2f1e72001-11-08 05:15:08 +0000104 : target(tgt),
Chris Lattnerd6ad8472004-06-02 05:56:04 +0000105 numSchedClasses(NumSchedClasses), mii(tgt.getInstrInfo()),
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000106 classRUsages(ClassRUsages), usageDeltas(UsageDeltas),
107 issueDeltas(IssueDeltas), numUsageDeltas(NumUsageDeltas),
108 numIssueDeltas(NumIssueDeltas)
109{}
110
111void
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000112TargetSchedInfo::initializeResources()
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000113{
114 assert(MAX_NUM_SLOTS >= (int)getMaxNumIssueTotal()
115 && "Insufficient slots for static data! Increase MAX_NUM_SLOTS");
116
117 // First, compute common resource usage info for each class because
118 // most instructions will probably behave the same as their class.
119 // Cannot allocate a vector of InstrRUsage so new each one.
120 //
Chris Lattner697954c2002-01-20 22:54:45 +0000121 std::vector<InstrRUsage> instrRUForClasses;
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000122 instrRUForClasses.resize(numSchedClasses);
123 for (InstrSchedClass sc = 0; sc < numSchedClasses; sc++) {
124 // instrRUForClasses.push_back(new InstrRUsage);
125 instrRUForClasses[sc].setMaxSlots(getMaxNumIssueTotal());
Chris Lattner30587612002-02-04 05:56:30 +0000126 instrRUForClasses[sc].setTo(classRUsages[sc]);
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000127 }
128
129 computeInstrResources(instrRUForClasses);
130 computeIssueGaps(instrRUForClasses);
131}
132
133
134void
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000135TargetSchedInfo::computeInstrResources(const std::vector<InstrRUsage>&
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000136 instrRUForClasses)
137{
Chris Lattnerbceb6882004-02-29 06:31:16 +0000138 int numOpCodes = mii->getNumOpcodes();
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000139 instrRUsages.resize(numOpCodes);
140
141 // First get the resource usage information from the class resource usages.
142 for (MachineOpCode op = 0; op < numOpCodes; ++op) {
143 InstrSchedClass sc = getSchedClass(op);
Chris Lattner07541a22002-10-28 04:59:43 +0000144 assert(sc < numSchedClasses);
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000145 instrRUsages[op] = instrRUForClasses[sc];
146 }
147
148 // Now, modify the resource usages as specified in the deltas.
149 for (unsigned i = 0; i < numUsageDeltas; ++i) {
150 MachineOpCode op = usageDeltas[i].opCode;
151 assert(op < numOpCodes);
152 instrRUsages[op].addUsageDelta(usageDeltas[i]);
153 }
154
155 // Then modify the issue restrictions as specified in the deltas.
156 for (unsigned i = 0; i < numIssueDeltas; ++i) {
157 MachineOpCode op = issueDeltas[i].opCode;
158 assert(op < numOpCodes);
159 instrRUsages[issueDeltas[i].opCode].addIssueDelta(issueDeltas[i]);
160 }
161}
162
163
164void
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000165TargetSchedInfo::computeIssueGaps(const std::vector<InstrRUsage>&
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000166 instrRUForClasses)
167{
Chris Lattnerbceb6882004-02-29 06:31:16 +0000168 int numOpCodes = mii->getNumOpcodes();
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000169 issueGaps.resize(numOpCodes);
170 conflictLists.resize(numOpCodes);
171
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000172 assert(numOpCodes < (1 << MAX_OPCODE_SIZE) - 1
173 && "numOpCodes invalid for implementation of class OpCodePair!");
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000174
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000175 // First, compute issue gaps between pairs of classes based on common
176 // resources usages for each class, because most instruction pairs will
177 // usually behave the same as their class.
178 //
Alkis Evlogimenos0ee6e2a2004-09-28 02:47:38 +0000179 int* classPairGaps =
180 static_cast<int*>(alloca(sizeof(int) * numSchedClasses * numSchedClasses));
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000181 for (InstrSchedClass fromSC=0; fromSC < numSchedClasses; fromSC++)
Misha Brukmanb10cea82003-08-05 00:02:06 +0000182 for (InstrSchedClass toSC=0; toSC < numSchedClasses; toSC++) {
183 int classPairGap = ComputeMinGap(instrRUForClasses[fromSC],
184 instrRUForClasses[toSC]);
Alkis Evlogimenos0ee6e2a2004-09-28 02:47:38 +0000185 classPairGaps[fromSC*numSchedClasses + toSC] = classPairGap;
Misha Brukmanb10cea82003-08-05 00:02:06 +0000186 }
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000187
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000188 // Now, for each pair of instructions, use the class pair gap if both
189 // instructions have identical resource usage as their respective classes.
190 // If not, recompute the gap for the pair from scratch.
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000191
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000192 longestIssueConflict = 0;
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000193
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000194 for (MachineOpCode fromOp=0; fromOp < numOpCodes; fromOp++)
Misha Brukmanb10cea82003-08-05 00:02:06 +0000195 for (MachineOpCode toOp=0; toOp < numOpCodes; toOp++) {
196 int instrPairGap =
197 (instrRUsages[fromOp].sameAsClass && instrRUsages[toOp].sameAsClass)
Alkis Evlogimenos0ee6e2a2004-09-28 02:47:38 +0000198 ? classPairGaps[getSchedClass(fromOp)*numSchedClasses + getSchedClass(toOp)]
Misha Brukmanb10cea82003-08-05 00:02:06 +0000199 : ComputeMinGap(instrRUsages[fromOp], instrRUsages[toOp]);
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000200
Misha Brukmanb10cea82003-08-05 00:02:06 +0000201 if (instrPairGap > 0) {
202 this->setGap(instrPairGap, fromOp, toOp);
203 conflictLists[fromOp].push_back(toOp);
204 longestIssueConflict=std::max(longestIssueConflict, instrPairGap);
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000205 }
Misha Brukmanb10cea82003-08-05 00:02:06 +0000206 }
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000207}
208
Chris Lattner30587612002-02-04 05:56:30 +0000209
210void InstrRUsage::setTo(const InstrClassRUsage& classRU) {
211 sameAsClass = true;
212 isSingleIssue = classRU.isSingleIssue;
213 breaksGroup = classRU.breaksGroup;
214 numBubbles = classRU.numBubbles;
215
Misha Brukmanb10cea82003-08-05 00:02:06 +0000216 for (unsigned i=0; i < classRU.numSlots; i++) {
217 unsigned slot = classRU.feasibleSlots[i];
218 assert(slot < feasibleSlots.size() && "Invalid slot specified!");
219 this->feasibleSlots[slot] = true;
220 }
Chris Lattner30587612002-02-04 05:56:30 +0000221
222 numCycles = classRU.totCycles;
223 resourcesByCycle.resize(this->numCycles);
224
225 for (unsigned i=0; i < classRU.numRUEntries; i++)
226 for (unsigned c=classRU.V[i].startCycle, NC = c + classRU.V[i].numCycles;
227 c < NC; c++)
228 this->resourcesByCycle[c].push_back(classRU.V[i].resourceId);
229
Misha Brukmanb10cea82003-08-05 00:02:06 +0000230 // Sort each resource usage vector by resourceId_t to speed up conflict
231 // checking
Chris Lattner30587612002-02-04 05:56:30 +0000232 for (unsigned i=0; i < this->resourcesByCycle.size(); i++)
Alkis Evlogimenos0ee6e2a2004-09-28 02:47:38 +0000233 std::sort(resourcesByCycle[i].begin(), resourcesByCycle[i].end());
Chris Lattner30587612002-02-04 05:56:30 +0000234}
235
236// Add the extra resource usage requirements specified in the delta.
237// Note that a negative value of `numCycles' means one entry for that
238// resource should be deleted for each cycle.
239//
240void InstrRUsage::addUsageDelta(const InstrRUsageDelta &delta) {
241 int NC = delta.numCycles;
242 sameAsClass = false;
243
244 // resize the resources vector if more cycles are specified
245 unsigned maxCycles = this->numCycles;
246 maxCycles = std::max(maxCycles, delta.startCycle + abs(NC) - 1);
Misha Brukmanb10cea82003-08-05 00:02:06 +0000247 if (maxCycles > this->numCycles) {
248 this->resourcesByCycle.resize(maxCycles);
249 this->numCycles = maxCycles;
250 }
Chris Lattner30587612002-02-04 05:56:30 +0000251
252 if (NC >= 0)
253 for (unsigned c=delta.startCycle, last=c+NC-1; c <= last; c++)
254 this->resourcesByCycle[c].push_back(delta.resourceId);
255 else
256 // Remove the resource from all NC cycles.
Misha Brukmanb10cea82003-08-05 00:02:06 +0000257 for (unsigned c=delta.startCycle, last=(c-NC)-1; c <= last; c++) {
258 // Look for the resource backwards so we remove the last entry
259 // for that resource in each cycle.
260 std::vector<resourceId_t>& rvec = this->resourcesByCycle[c];
261 int r;
262 for (r = rvec.size() - 1; r >= 0; r--)
263 if (rvec[r] == delta.resourceId) {
264 // found last entry for the resource
265 rvec.erase(rvec.begin() + r);
266 break;
267 }
268 assert(r >= 0 && "Resource to remove was unused in cycle c!");
269 }
Chris Lattner30587612002-02-04 05:56:30 +0000270}