1 //===-- SchedInfo.cpp - Generic code to support target schedulers ----------==//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/alloca.h"
16 #include "llvm/Target/TargetSchedInfo.h"
17 #include "llvm/Target/TargetMachine.h"
22 resourceId_t llvm::CPUResource::nextId = 0;
23 static std::vector<CPUResource*> *CPUResourceMap = 0;
25 CPUResource::CPUResource(const std::string& resourceName, int maxUsers)
26 : rname(resourceName), rid(nextId++), maxNumUsers(maxUsers) {
28 CPUResourceMap = new std::vector<CPUResource*>;
30 //Put Resource in the map
31 CPUResourceMap->push_back(this);
34 ///Get CPUResource if you only have the resource ID
35 CPUResource* CPUResource::getCPUResource(resourceId_t id) {
36 return (*CPUResourceMap)[id];
39 // Check if fromRVec and toRVec have *any* common entries.
40 // Assume the vectors are sorted in increasing order.
41 // Algorithm copied from function set_intersection() for sorted ranges
45 RUConflict(const std::vector<resourceId_t>& fromRVec,
46 const std::vector<resourceId_t>& toRVec)
49 unsigned fN = fromRVec.size(), tN = toRVec.size();
50 unsigned fi = 0, ti = 0;
52 while (fi < fN && ti < tN) {
53 if (fromRVec[fi] < toRVec[ti])
55 else if (toRVec[ti] < fromRVec[fi])
65 ComputeMinGap(const InstrRUsage &fromRU,
66 const InstrRUsage &toRU)
68 CycleCount_t minGap = 0;
70 if (fromRU.numBubbles > 0)
71 minGap = fromRU.numBubbles;
73 if (minGap < fromRU.numCycles) {
74 // only need to check from cycle `minGap' onwards
75 for (CycleCount_t gap=minGap; gap <= fromRU.numCycles-1; gap++) {
76 // check if instr. #2 can start executing `gap' cycles after #1
77 // by checking for resource conflicts in each overlapping cycle
78 CycleCount_t numOverlap =std::min(fromRU.numCycles - gap, toRU.numCycles);
79 for (CycleCount_t c = 0; c <= numOverlap-1; c++)
80 if (RUConflict(fromRU.resourcesByCycle[gap + c],
81 toRU.resourcesByCycle[c])) {
82 // conflict found so minGap must be more than `gap'
93 //---------------------------------------------------------------------------
94 // class TargetSchedInfo
95 // Interface to machine description for instruction scheduling
96 //---------------------------------------------------------------------------
98 TargetSchedInfo::TargetSchedInfo(const TargetMachine& tgt,
100 const InstrClassRUsage* ClassRUsages,
101 const InstrRUsageDelta* UsageDeltas,
102 const InstrIssueDelta* IssueDeltas,
103 unsigned NumUsageDeltas,
104 unsigned NumIssueDeltas)
106 numSchedClasses(NumSchedClasses), mii(tgt.getInstrInfo()),
107 classRUsages(ClassRUsages), usageDeltas(UsageDeltas),
108 issueDeltas(IssueDeltas), numUsageDeltas(NumUsageDeltas),
109 numIssueDeltas(NumIssueDeltas)
113 TargetSchedInfo::initializeResources()
115 assert(MAX_NUM_SLOTS >= (int)getMaxNumIssueTotal() &&
116 "Insufficient slots for static data! Increase MAX_NUM_SLOTS");
118 // First, compute common resource usage info for each class because
119 // most instructions will probably behave the same as their class.
120 // Cannot allocate a vector of InstrRUsage so new each one.
122 std::vector<InstrRUsage> instrRUForClasses;
123 instrRUForClasses.resize(numSchedClasses);
124 for (InstrSchedClass sc = 0; sc < numSchedClasses; sc++) {
125 // instrRUForClasses.push_back(new InstrRUsage);
126 instrRUForClasses[sc].setMaxSlots(getMaxNumIssueTotal());
127 instrRUForClasses[sc].setTo(classRUsages[sc]);
130 computeInstrResources(instrRUForClasses);
131 computeIssueGaps(instrRUForClasses);
136 TargetSchedInfo::computeInstrResources(const std::vector<InstrRUsage>&
138 int numOpCodes = mii->getNumOpcodes();
139 instrRUsages.resize(numOpCodes);
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);
144 assert(sc < numSchedClasses);
145 instrRUsages[op] = instrRUForClasses[sc];
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]);
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]);
165 TargetSchedInfo::computeIssueGaps(const std::vector<InstrRUsage>&
167 int numOpCodes = mii->getNumOpcodes();
168 issueGaps.resize(numOpCodes);
169 conflictLists.resize(numOpCodes);
171 // First, compute issue gaps between pairs of classes based on common
172 // resources usages for each class, because most instruction pairs will
173 // usually behave the same as their class.
176 static_cast<int*>(alloca(sizeof(int) * numSchedClasses * numSchedClasses));
177 for (InstrSchedClass fromSC=0; fromSC < numSchedClasses; fromSC++)
178 for (InstrSchedClass toSC=0; toSC < numSchedClasses; toSC++) {
179 int classPairGap = ComputeMinGap(instrRUForClasses[fromSC],
180 instrRUForClasses[toSC]);
181 classPairGaps[fromSC*numSchedClasses + toSC] = classPairGap;
184 // Now, for each pair of instructions, use the class pair gap if both
185 // instructions have identical resource usage as their respective classes.
186 // If not, recompute the gap for the pair from scratch.
188 longestIssueConflict = 0;
190 for (MachineOpCode fromOp=0; fromOp < numOpCodes; fromOp++)
191 for (MachineOpCode toOp=0; toOp < numOpCodes; toOp++) {
193 (instrRUsages[fromOp].sameAsClass && instrRUsages[toOp].sameAsClass)
194 ? classPairGaps[getSchedClass(fromOp)*numSchedClasses + getSchedClass(toOp)]
195 : ComputeMinGap(instrRUsages[fromOp], instrRUsages[toOp]);
197 if (instrPairGap > 0) {
198 this->setGap(instrPairGap, fromOp, toOp);
199 conflictLists[fromOp].push_back(toOp);
200 longestIssueConflict=std::max(longestIssueConflict, instrPairGap);
206 void InstrRUsage::setTo(const InstrClassRUsage& classRU) {
208 isSingleIssue = classRU.isSingleIssue;
209 breaksGroup = classRU.breaksGroup;
210 numBubbles = classRU.numBubbles;
212 for (unsigned i=0; i < classRU.numSlots; i++) {
213 unsigned slot = classRU.feasibleSlots[i];
214 assert(slot < feasibleSlots.size() && "Invalid slot specified!");
215 this->feasibleSlots[slot] = true;
218 numCycles = classRU.totCycles;
219 resourcesByCycle.resize(this->numCycles);
221 for (unsigned i=0; i < classRU.numRUEntries; i++)
222 for (unsigned c=classRU.V[i].startCycle, NC = c + classRU.V[i].numCycles;
224 this->resourcesByCycle[c].push_back(classRU.V[i].resourceId);
226 // Sort each resource usage vector by resourceId_t to speed up conflict
228 for (unsigned i=0; i < this->resourcesByCycle.size(); i++)
229 std::sort(resourcesByCycle[i].begin(), resourcesByCycle[i].end());
232 // Add the extra resource usage requirements specified in the delta.
233 // Note that a negative value of `numCycles' means one entry for that
234 // resource should be deleted for each cycle.
236 void InstrRUsage::addUsageDelta(const InstrRUsageDelta &delta) {
237 int NC = delta.numCycles;
240 // resize the resources vector if more cycles are specified
241 unsigned maxCycles = this->numCycles;
242 maxCycles = std::max(maxCycles, delta.startCycle + abs(NC) - 1);
243 if (maxCycles > this->numCycles) {
244 this->resourcesByCycle.resize(maxCycles);
245 this->numCycles = maxCycles;
249 for (unsigned c=delta.startCycle, last=c+NC-1; c <= last; c++)
250 this->resourcesByCycle[c].push_back(delta.resourceId);
252 // Remove the resource from all NC cycles.
253 for (unsigned c=delta.startCycle, last=(c-NC)-1; c <= last; c++) {
254 // Look for the resource backwards so we remove the last entry
255 // for that resource in each cycle.
256 std::vector<resourceId_t>& rvec = this->resourcesByCycle[c];
258 for (r = rvec.size() - 1; r >= 0; r--)
259 if (rvec[r] == delta.resourceId) {
260 // found last entry for the resource
261 rvec.erase(rvec.begin() + r);
264 assert(r >= 0 && "Resource to remove was unused in cycle c!");