1 //===-- SchedInfo.cpp - Generic code to support target schedulers ----------==//
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.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Target/MachineSchedInfo.h"
10 // External object describing the machine instructions
11 // Initialized only when the TargetMachine class is created
12 // and reset when that class is destroyed.
14 const MachineInstrDescriptor* TargetInstrDescriptors = 0;
16 resourceId_t MachineResource::nextId = 0;
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
24 RUConflict(const vector<resourceId_t>& fromRVec,
25 const vector<resourceId_t>& toRVec)
28 unsigned fN = fromRVec.size(), tN = toRVec.size();
29 unsigned fi = 0, ti = 0;
31 while (fi < fN && ti < tN)
33 if (fromRVec[fi] < toRVec[ti])
35 else if (toRVec[ti] < fromRVec[fi])
45 ComputeMinGap(const InstrRUsage &fromRU,
46 const InstrRUsage &toRU)
50 if (fromRU.numBubbles > 0)
51 minGap = fromRU.numBubbles;
53 if (minGap < fromRU.numCycles)
55 // only need to check from cycle `minGap' onwards
56 for (cycles_t gap=minGap; gap <= fromRU.numCycles-1; gap++)
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]))
65 // conflict found so minGap must be more than `gap'
76 //---------------------------------------------------------------------------
77 // class MachineSchedInfo
78 // Interface to machine description for instruction scheduling
79 //---------------------------------------------------------------------------
81 MachineSchedInfo::MachineSchedInfo(int NumSchedClasses,
82 const MachineInstrInfo* Mii,
83 const InstrClassRUsage* ClassRUsages,
84 const InstrRUsageDelta* UsageDeltas,
85 const InstrIssueDelta* IssueDeltas,
86 unsigned int NumUsageDeltas,
87 unsigned int NumIssueDeltas)
88 : numSchedClasses(NumSchedClasses), mii(Mii),
89 classRUsages(ClassRUsages), usageDeltas(UsageDeltas),
90 issueDeltas(IssueDeltas), numUsageDeltas(NumUsageDeltas),
91 numIssueDeltas(NumIssueDeltas)
95 MachineSchedInfo::initializeResources()
97 assert(MAX_NUM_SLOTS >= (int)getMaxNumIssueTotal()
98 && "Insufficient slots for static data! Increase MAX_NUM_SLOTS");
100 // First, compute common resource usage info for each class because
101 // most instructions will probably behave the same as their class.
102 // Cannot allocate a vector of InstrRUsage so new each one.
104 vector<InstrRUsage> instrRUForClasses;
105 instrRUForClasses.resize(numSchedClasses);
106 for (InstrSchedClass sc = 0; sc < numSchedClasses; sc++) {
107 // instrRUForClasses.push_back(new InstrRUsage);
108 instrRUForClasses[sc].setMaxSlots(getMaxNumIssueTotal());
109 instrRUForClasses[sc] = classRUsages[sc];
112 computeInstrResources(instrRUForClasses);
113 computeIssueGaps(instrRUForClasses);
118 MachineSchedInfo::computeInstrResources(const vector<InstrRUsage>&
121 int numOpCodes = mii->getNumRealOpCodes();
122 instrRUsages.resize(numOpCodes);
124 // First get the resource usage information from the class resource usages.
125 for (MachineOpCode op = 0; op < numOpCodes; ++op) {
126 InstrSchedClass sc = getSchedClass(op);
127 assert(sc >= 0 && sc < numSchedClasses);
128 instrRUsages[op] = instrRUForClasses[sc];
131 // Now, modify the resource usages as specified in the deltas.
132 for (unsigned i = 0; i < numUsageDeltas; ++i) {
133 MachineOpCode op = usageDeltas[i].opCode;
134 assert(op < numOpCodes);
135 instrRUsages[op].addUsageDelta(usageDeltas[i]);
138 // Then modify the issue restrictions as specified in the deltas.
139 for (unsigned i = 0; i < numIssueDeltas; ++i) {
140 MachineOpCode op = issueDeltas[i].opCode;
141 assert(op < numOpCodes);
142 instrRUsages[issueDeltas[i].opCode].addIssueDelta(issueDeltas[i]);
148 MachineSchedInfo::computeIssueGaps(const vector<InstrRUsage>&
151 int numOpCodes = mii->getNumRealOpCodes();
152 instrRUsages.resize(numOpCodes);
154 assert(numOpCodes < (1 << MAX_OPCODE_SIZE) - 1
155 && "numOpCodes invalid for implementation of class OpCodePair!");
157 // First, compute issue gaps between pairs of classes based on common
158 // resources usages for each class, because most instruction pairs will
159 // usually behave the same as their class.
161 int classPairGaps[numSchedClasses][numSchedClasses];
162 for (InstrSchedClass fromSC=0; fromSC < numSchedClasses; fromSC++)
163 for (InstrSchedClass toSC=0; toSC < numSchedClasses; toSC++)
165 int classPairGap = ComputeMinGap(instrRUForClasses[fromSC],
166 instrRUForClasses[toSC]);
167 classPairGaps[fromSC][toSC] = classPairGap;
170 // Now, for each pair of instructions, use the class pair gap if both
171 // instructions have identical resource usage as their respective classes.
172 // If not, recompute the gap for the pair from scratch.
174 longestIssueConflict = 0;
176 for (MachineOpCode fromOp=0; fromOp < numOpCodes; fromOp++)
177 for (MachineOpCode toOp=0; toOp < numOpCodes; toOp++)
180 (instrRUsages[fromOp].sameAsClass && instrRUsages[toOp].sameAsClass)
181 ? classPairGaps[getSchedClass(fromOp)][getSchedClass(toOp)]
182 : ComputeMinGap(instrRUsages[fromOp], instrRUsages[toOp]);
184 if (instrPairGap > 0)
186 issueGaps[OpCodePair(fromOp,toOp)] = instrPairGap;
187 conflictLists[fromOp].push_back(toOp);
188 longestIssueConflict = max(longestIssueConflict, instrPairGap);