blob: f70f374dab843f717b64e0ff555805c1bd063ecb [file] [log] [blame]
Vikram S. Adve0799fc42001-09-18 12:58:33 +00001//===-- SchedInfo.cpp - Generic code to support target schedulers ----------==//
2//
3// This file implements the generic part of a Scheduler description for a
4// target. This functionality is defined in the llvm/Target/SchedInfo.h file.
5//
6//===----------------------------------------------------------------------===//
7
Chris Lattnerd0f166a2002-12-29 03:13:05 +00008#include "llvm/Target/TargetSchedInfo.h"
Chris Lattner884f4b52002-02-03 07:47:05 +00009#include "llvm/Target/TargetMachine.h"
Vikram S. Adve0799fc42001-09-18 12:58:33 +000010
Vikram S. Adve0799fc42001-09-18 12:58:33 +000011resourceId_t MachineResource::nextId = 0;
12
13// Check if fromRVec and toRVec have *any* common entries.
14// Assume the vectors are sorted in increasing order.
15// Algorithm copied from function set_intersection() for sorted ranges
16// (stl_algo.h).
17//
18inline static bool
Chris Lattner697954c2002-01-20 22:54:45 +000019RUConflict(const std::vector<resourceId_t>& fromRVec,
20 const std::vector<resourceId_t>& toRVec)
Vikram S. Adve0799fc42001-09-18 12:58:33 +000021{
22
23 unsigned fN = fromRVec.size(), tN = toRVec.size();
24 unsigned fi = 0, ti = 0;
25
Misha Brukmanb10cea82003-08-05 00:02:06 +000026 while (fi < fN && ti < tN) {
27 if (fromRVec[fi] < toRVec[ti])
28 ++fi;
29 else if (toRVec[ti] < fromRVec[fi])
30 ++ti;
31 else
32 return true;
33 }
Vikram S. Adve0799fc42001-09-18 12:58:33 +000034 return false;
35}
36
37
38static cycles_t
39ComputeMinGap(const InstrRUsage &fromRU,
40 const InstrRUsage &toRU)
41{
42 cycles_t minGap = 0;
43
44 if (fromRU.numBubbles > 0)
45 minGap = fromRU.numBubbles;
46
Misha Brukmanb10cea82003-08-05 00:02:06 +000047 if (minGap < fromRU.numCycles) {
48 // only need to check from cycle `minGap' onwards
49 for (cycles_t gap=minGap; gap <= fromRU.numCycles-1; gap++) {
50 // check if instr. #2 can start executing `gap' cycles after #1
51 // by checking for resource conflicts in each overlapping cycle
52 cycles_t numOverlap =std::min(fromRU.numCycles - gap, toRU.numCycles);
53 for (cycles_t c = 0; c <= numOverlap-1; c++)
54 if (RUConflict(fromRU.resourcesByCycle[gap + c],
55 toRU.resourcesByCycle[c])) {
56 // conflict found so minGap must be more than `gap'
57 minGap = gap+1;
58 break;
59 }
Vikram S. Adve0799fc42001-09-18 12:58:33 +000060 }
Misha Brukmanb10cea82003-08-05 00:02:06 +000061 }
Vikram S. Adve0799fc42001-09-18 12:58:33 +000062
63 return minGap;
64}
65
66
67//---------------------------------------------------------------------------
Chris Lattnerd0f166a2002-12-29 03:13:05 +000068// class TargetSchedInfo
Vikram S. Adve0799fc42001-09-18 12:58:33 +000069// Interface to machine description for instruction scheduling
70//---------------------------------------------------------------------------
71
Chris Lattnerd0f166a2002-12-29 03:13:05 +000072TargetSchedInfo::TargetSchedInfo(const TargetMachine& tgt,
73 int NumSchedClasses,
74 const InstrClassRUsage* ClassRUsages,
75 const InstrRUsageDelta* UsageDeltas,
76 const InstrIssueDelta* IssueDeltas,
77 unsigned NumUsageDeltas,
78 unsigned NumIssueDeltas)
Vikram S. Adve7a2f1e72001-11-08 05:15:08 +000079 : target(tgt),
80 numSchedClasses(NumSchedClasses), mii(& tgt.getInstrInfo()),
Vikram S. Adve0799fc42001-09-18 12:58:33 +000081 classRUsages(ClassRUsages), usageDeltas(UsageDeltas),
82 issueDeltas(IssueDeltas), numUsageDeltas(NumUsageDeltas),
83 numIssueDeltas(NumIssueDeltas)
84{}
85
86void
Chris Lattnerd0f166a2002-12-29 03:13:05 +000087TargetSchedInfo::initializeResources()
Vikram S. Adve0799fc42001-09-18 12:58:33 +000088{
89 assert(MAX_NUM_SLOTS >= (int)getMaxNumIssueTotal()
90 && "Insufficient slots for static data! Increase MAX_NUM_SLOTS");
91
92 // First, compute common resource usage info for each class because
93 // most instructions will probably behave the same as their class.
94 // Cannot allocate a vector of InstrRUsage so new each one.
95 //
Chris Lattner697954c2002-01-20 22:54:45 +000096 std::vector<InstrRUsage> instrRUForClasses;
Vikram S. Adve0799fc42001-09-18 12:58:33 +000097 instrRUForClasses.resize(numSchedClasses);
98 for (InstrSchedClass sc = 0; sc < numSchedClasses; sc++) {
99 // instrRUForClasses.push_back(new InstrRUsage);
100 instrRUForClasses[sc].setMaxSlots(getMaxNumIssueTotal());
Chris Lattner30587612002-02-04 05:56:30 +0000101 instrRUForClasses[sc].setTo(classRUsages[sc]);
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000102 }
103
104 computeInstrResources(instrRUForClasses);
105 computeIssueGaps(instrRUForClasses);
106}
107
108
109void
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000110TargetSchedInfo::computeInstrResources(const std::vector<InstrRUsage>&
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000111 instrRUForClasses)
112{
113 int numOpCodes = mii->getNumRealOpCodes();
114 instrRUsages.resize(numOpCodes);
115
116 // First get the resource usage information from the class resource usages.
117 for (MachineOpCode op = 0; op < numOpCodes; ++op) {
118 InstrSchedClass sc = getSchedClass(op);
Chris Lattner07541a22002-10-28 04:59:43 +0000119 assert(sc < numSchedClasses);
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000120 instrRUsages[op] = instrRUForClasses[sc];
121 }
122
123 // Now, modify the resource usages as specified in the deltas.
124 for (unsigned i = 0; i < numUsageDeltas; ++i) {
125 MachineOpCode op = usageDeltas[i].opCode;
126 assert(op < numOpCodes);
127 instrRUsages[op].addUsageDelta(usageDeltas[i]);
128 }
129
130 // Then modify the issue restrictions as specified in the deltas.
131 for (unsigned i = 0; i < numIssueDeltas; ++i) {
132 MachineOpCode op = issueDeltas[i].opCode;
133 assert(op < numOpCodes);
134 instrRUsages[issueDeltas[i].opCode].addIssueDelta(issueDeltas[i]);
135 }
136}
137
138
139void
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000140TargetSchedInfo::computeIssueGaps(const std::vector<InstrRUsage>&
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000141 instrRUForClasses)
142{
143 int numOpCodes = mii->getNumRealOpCodes();
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000144 issueGaps.resize(numOpCodes);
145 conflictLists.resize(numOpCodes);
146
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000147 assert(numOpCodes < (1 << MAX_OPCODE_SIZE) - 1
148 && "numOpCodes invalid for implementation of class OpCodePair!");
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000149
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000150 // First, compute issue gaps between pairs of classes based on common
151 // resources usages for each class, because most instruction pairs will
152 // usually behave the same as their class.
153 //
154 int classPairGaps[numSchedClasses][numSchedClasses];
155 for (InstrSchedClass fromSC=0; fromSC < numSchedClasses; fromSC++)
Misha Brukmanb10cea82003-08-05 00:02:06 +0000156 for (InstrSchedClass toSC=0; toSC < numSchedClasses; toSC++) {
157 int classPairGap = ComputeMinGap(instrRUForClasses[fromSC],
158 instrRUForClasses[toSC]);
159 classPairGaps[fromSC][toSC] = classPairGap;
160 }
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000161
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000162 // Now, for each pair of instructions, use the class pair gap if both
163 // instructions have identical resource usage as their respective classes.
164 // If not, recompute the gap for the pair from scratch.
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000165
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000166 longestIssueConflict = 0;
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000167
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000168 for (MachineOpCode fromOp=0; fromOp < numOpCodes; fromOp++)
Misha Brukmanb10cea82003-08-05 00:02:06 +0000169 for (MachineOpCode toOp=0; toOp < numOpCodes; toOp++) {
170 int instrPairGap =
171 (instrRUsages[fromOp].sameAsClass && instrRUsages[toOp].sameAsClass)
172 ? classPairGaps[getSchedClass(fromOp)][getSchedClass(toOp)]
173 : ComputeMinGap(instrRUsages[fromOp], instrRUsages[toOp]);
Vikram S. Adve5aefcad2002-10-13 00:37:46 +0000174
Misha Brukmanb10cea82003-08-05 00:02:06 +0000175 if (instrPairGap > 0) {
176 this->setGap(instrPairGap, fromOp, toOp);
177 conflictLists[fromOp].push_back(toOp);
178 longestIssueConflict=std::max(longestIssueConflict, instrPairGap);
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000179 }
Misha Brukmanb10cea82003-08-05 00:02:06 +0000180 }
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000181}
182
Chris Lattner30587612002-02-04 05:56:30 +0000183
184void InstrRUsage::setTo(const InstrClassRUsage& classRU) {
185 sameAsClass = true;
186 isSingleIssue = classRU.isSingleIssue;
187 breaksGroup = classRU.breaksGroup;
188 numBubbles = classRU.numBubbles;
189
Misha Brukmanb10cea82003-08-05 00:02:06 +0000190 for (unsigned i=0; i < classRU.numSlots; i++) {
191 unsigned slot = classRU.feasibleSlots[i];
192 assert(slot < feasibleSlots.size() && "Invalid slot specified!");
193 this->feasibleSlots[slot] = true;
194 }
Chris Lattner30587612002-02-04 05:56:30 +0000195
196 numCycles = classRU.totCycles;
197 resourcesByCycle.resize(this->numCycles);
198
199 for (unsigned i=0; i < classRU.numRUEntries; i++)
200 for (unsigned c=classRU.V[i].startCycle, NC = c + classRU.V[i].numCycles;
201 c < NC; c++)
202 this->resourcesByCycle[c].push_back(classRU.V[i].resourceId);
203
Misha Brukmanb10cea82003-08-05 00:02:06 +0000204 // Sort each resource usage vector by resourceId_t to speed up conflict
205 // checking
Chris Lattner30587612002-02-04 05:56:30 +0000206 for (unsigned i=0; i < this->resourcesByCycle.size(); i++)
207 sort(resourcesByCycle[i].begin(), resourcesByCycle[i].end());
Chris Lattner30587612002-02-04 05:56:30 +0000208}
209
210// Add the extra resource usage requirements specified in the delta.
211// Note that a negative value of `numCycles' means one entry for that
212// resource should be deleted for each cycle.
213//
214void InstrRUsage::addUsageDelta(const InstrRUsageDelta &delta) {
215 int NC = delta.numCycles;
216 sameAsClass = false;
217
218 // resize the resources vector if more cycles are specified
219 unsigned maxCycles = this->numCycles;
220 maxCycles = std::max(maxCycles, delta.startCycle + abs(NC) - 1);
Misha Brukmanb10cea82003-08-05 00:02:06 +0000221 if (maxCycles > this->numCycles) {
222 this->resourcesByCycle.resize(maxCycles);
223 this->numCycles = maxCycles;
224 }
Chris Lattner30587612002-02-04 05:56:30 +0000225
226 if (NC >= 0)
227 for (unsigned c=delta.startCycle, last=c+NC-1; c <= last; c++)
228 this->resourcesByCycle[c].push_back(delta.resourceId);
229 else
230 // Remove the resource from all NC cycles.
Misha Brukmanb10cea82003-08-05 00:02:06 +0000231 for (unsigned c=delta.startCycle, last=(c-NC)-1; c <= last; c++) {
232 // Look for the resource backwards so we remove the last entry
233 // for that resource in each cycle.
234 std::vector<resourceId_t>& rvec = this->resourcesByCycle[c];
235 int r;
236 for (r = rvec.size() - 1; r >= 0; r--)
237 if (rvec[r] == delta.resourceId) {
238 // found last entry for the resource
239 rvec.erase(rvec.begin() + r);
240 break;
241 }
242 assert(r >= 0 && "Resource to remove was unused in cycle c!");
243 }
Chris Lattner30587612002-02-04 05:56:30 +0000244}