blob: f9dca290b06460f903f0932f21e7cff6cd796a48 [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
8#include "llvm/Target/MachineSchedInfo.h"
9
10// External object describing the machine instructions
11// Initialized only when the TargetMachine class is created
12// and reset when that class is destroyed.
13//
14const MachineInstrDescriptor* TargetInstrDescriptors = 0;
15
16resourceId_t MachineResource::nextId = 0;
17
18// Check if fromRVec and toRVec have *any* common entries.
19// Assume the vectors are sorted in increasing order.
20// Algorithm copied from function set_intersection() for sorted ranges
21// (stl_algo.h).
22//
23inline static bool
24RUConflict(const vector<resourceId_t>& fromRVec,
25 const vector<resourceId_t>& toRVec)
26{
27
28 unsigned fN = fromRVec.size(), tN = toRVec.size();
29 unsigned fi = 0, ti = 0;
30
31 while (fi < fN && ti < tN)
32 {
33 if (fromRVec[fi] < toRVec[ti])
34 ++fi;
35 else if (toRVec[ti] < fromRVec[fi])
36 ++ti;
37 else
38 return true;
39 }
40 return false;
41}
42
43
44static cycles_t
45ComputeMinGap(const InstrRUsage &fromRU,
46 const InstrRUsage &toRU)
47{
48 cycles_t minGap = 0;
49
50 if (fromRU.numBubbles > 0)
51 minGap = fromRU.numBubbles;
52
53 if (minGap < fromRU.numCycles)
54 {
55 // only need to check from cycle `minGap' onwards
56 for (cycles_t gap=minGap; gap <= fromRU.numCycles-1; gap++)
57 {
58 // check if instr. #2 can start executing `gap' cycles after #1
59 // by checking for resource conflicts in each overlapping cycle
60 cycles_t numOverlap = min(fromRU.numCycles - gap, toRU.numCycles);
61 for (cycles_t c = 0; c <= numOverlap-1; c++)
62 if (RUConflict(fromRU.resourcesByCycle[gap + c],
63 toRU.resourcesByCycle[c]))
64 {
65 // conflict found so minGap must be more than `gap'
66 minGap = gap+1;
67 break;
68 }
69 }
70 }
71
72 return minGap;
73}
74
75
76//---------------------------------------------------------------------------
77// class MachineSchedInfo
78// Interface to machine description for instruction scheduling
79//---------------------------------------------------------------------------
80
Vikram S. Adve7a2f1e72001-11-08 05:15:08 +000081MachineSchedInfo::MachineSchedInfo(const TargetMachine& tgt,
82 int NumSchedClasses,
Vikram S. Adve0799fc42001-09-18 12:58:33 +000083 const InstrClassRUsage* ClassRUsages,
84 const InstrRUsageDelta* UsageDeltas,
85 const InstrIssueDelta* IssueDeltas,
86 unsigned int NumUsageDeltas,
87 unsigned int NumIssueDeltas)
Vikram S. Adve7a2f1e72001-11-08 05:15:08 +000088 : target(tgt),
89 numSchedClasses(NumSchedClasses), mii(& tgt.getInstrInfo()),
Vikram S. Adve0799fc42001-09-18 12:58:33 +000090 classRUsages(ClassRUsages), usageDeltas(UsageDeltas),
91 issueDeltas(IssueDeltas), numUsageDeltas(NumUsageDeltas),
92 numIssueDeltas(NumIssueDeltas)
93{}
94
95void
96MachineSchedInfo::initializeResources()
97{
98 assert(MAX_NUM_SLOTS >= (int)getMaxNumIssueTotal()
99 && "Insufficient slots for static data! Increase MAX_NUM_SLOTS");
100
101 // First, compute common resource usage info for each class because
102 // most instructions will probably behave the same as their class.
103 // Cannot allocate a vector of InstrRUsage so new each one.
104 //
105 vector<InstrRUsage> instrRUForClasses;
106 instrRUForClasses.resize(numSchedClasses);
107 for (InstrSchedClass sc = 0; sc < numSchedClasses; sc++) {
108 // instrRUForClasses.push_back(new InstrRUsage);
109 instrRUForClasses[sc].setMaxSlots(getMaxNumIssueTotal());
110 instrRUForClasses[sc] = classRUsages[sc];
111 }
112
113 computeInstrResources(instrRUForClasses);
114 computeIssueGaps(instrRUForClasses);
115}
116
117
118void
119MachineSchedInfo::computeInstrResources(const vector<InstrRUsage>&
120 instrRUForClasses)
121{
122 int numOpCodes = mii->getNumRealOpCodes();
123 instrRUsages.resize(numOpCodes);
124
125 // First get the resource usage information from the class resource usages.
126 for (MachineOpCode op = 0; op < numOpCodes; ++op) {
127 InstrSchedClass sc = getSchedClass(op);
128 assert(sc >= 0 && sc < numSchedClasses);
129 instrRUsages[op] = instrRUForClasses[sc];
130 }
131
132 // Now, modify the resource usages as specified in the deltas.
133 for (unsigned i = 0; i < numUsageDeltas; ++i) {
134 MachineOpCode op = usageDeltas[i].opCode;
135 assert(op < numOpCodes);
136 instrRUsages[op].addUsageDelta(usageDeltas[i]);
137 }
138
139 // Then modify the issue restrictions as specified in the deltas.
140 for (unsigned i = 0; i < numIssueDeltas; ++i) {
141 MachineOpCode op = issueDeltas[i].opCode;
142 assert(op < numOpCodes);
143 instrRUsages[issueDeltas[i].opCode].addIssueDelta(issueDeltas[i]);
144 }
145}
146
147
148void
149MachineSchedInfo::computeIssueGaps(const vector<InstrRUsage>&
150 instrRUForClasses)
151{
152 int numOpCodes = mii->getNumRealOpCodes();
153 instrRUsages.resize(numOpCodes);
154
155 assert(numOpCodes < (1 << MAX_OPCODE_SIZE) - 1
156 && "numOpCodes invalid for implementation of class OpCodePair!");
157
158 // First, compute issue gaps between pairs of classes based on common
159 // resources usages for each class, because most instruction pairs will
160 // usually behave the same as their class.
161 //
162 int classPairGaps[numSchedClasses][numSchedClasses];
163 for (InstrSchedClass fromSC=0; fromSC < numSchedClasses; fromSC++)
164 for (InstrSchedClass toSC=0; toSC < numSchedClasses; toSC++)
165 {
166 int classPairGap = ComputeMinGap(instrRUForClasses[fromSC],
167 instrRUForClasses[toSC]);
168 classPairGaps[fromSC][toSC] = classPairGap;
169 }
170
171 // Now, for each pair of instructions, use the class pair gap if both
172 // instructions have identical resource usage as their respective classes.
173 // If not, recompute the gap for the pair from scratch.
174
175 longestIssueConflict = 0;
176
177 for (MachineOpCode fromOp=0; fromOp < numOpCodes; fromOp++)
178 for (MachineOpCode toOp=0; toOp < numOpCodes; toOp++)
179 {
180 int instrPairGap =
181 (instrRUsages[fromOp].sameAsClass && instrRUsages[toOp].sameAsClass)
182 ? classPairGaps[getSchedClass(fromOp)][getSchedClass(toOp)]
183 : ComputeMinGap(instrRUsages[fromOp], instrRUsages[toOp]);
184
185 if (instrPairGap > 0)
186 {
187 issueGaps[OpCodePair(fromOp,toOp)] = instrPairGap;
188 conflictLists[fromOp].push_back(toOp);
189 longestIssueConflict = max(longestIssueConflict, instrPairGap);
190 }
191 }
192}
193