Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame^] | 1 | //===-- 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 | // |
| 14 | const MachineInstrDescriptor* TargetInstrDescriptors = 0; |
| 15 | |
| 16 | resourceId_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 | // |
| 23 | inline static bool |
| 24 | RUConflict(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 | |
| 44 | static cycles_t |
| 45 | ComputeMinGap(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 | |
| 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) |
| 92 | {} |
| 93 | |
| 94 | void |
| 95 | MachineSchedInfo::initializeResources() |
| 96 | { |
| 97 | assert(MAX_NUM_SLOTS >= (int)getMaxNumIssueTotal() |
| 98 | && "Insufficient slots for static data! Increase MAX_NUM_SLOTS"); |
| 99 | |
| 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. |
| 103 | // |
| 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]; |
| 110 | } |
| 111 | |
| 112 | computeInstrResources(instrRUForClasses); |
| 113 | computeIssueGaps(instrRUForClasses); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void |
| 118 | MachineSchedInfo::computeInstrResources(const vector<InstrRUsage>& |
| 119 | instrRUForClasses) |
| 120 | { |
| 121 | int numOpCodes = mii->getNumRealOpCodes(); |
| 122 | instrRUsages.resize(numOpCodes); |
| 123 | |
| 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]; |
| 129 | } |
| 130 | |
| 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]); |
| 136 | } |
| 137 | |
| 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]); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void |
| 148 | MachineSchedInfo::computeIssueGaps(const vector<InstrRUsage>& |
| 149 | instrRUForClasses) |
| 150 | { |
| 151 | int numOpCodes = mii->getNumRealOpCodes(); |
| 152 | instrRUsages.resize(numOpCodes); |
| 153 | |
| 154 | assert(numOpCodes < (1 << MAX_OPCODE_SIZE) - 1 |
| 155 | && "numOpCodes invalid for implementation of class OpCodePair!"); |
| 156 | |
| 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. |
| 160 | // |
| 161 | int classPairGaps[numSchedClasses][numSchedClasses]; |
| 162 | for (InstrSchedClass fromSC=0; fromSC < numSchedClasses; fromSC++) |
| 163 | for (InstrSchedClass toSC=0; toSC < numSchedClasses; toSC++) |
| 164 | { |
| 165 | int classPairGap = ComputeMinGap(instrRUForClasses[fromSC], |
| 166 | instrRUForClasses[toSC]); |
| 167 | classPairGaps[fromSC][toSC] = classPairGap; |
| 168 | } |
| 169 | |
| 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. |
| 173 | |
| 174 | longestIssueConflict = 0; |
| 175 | |
| 176 | for (MachineOpCode fromOp=0; fromOp < numOpCodes; fromOp++) |
| 177 | for (MachineOpCode toOp=0; toOp < numOpCodes; toOp++) |
| 178 | { |
| 179 | int instrPairGap = |
| 180 | (instrRUsages[fromOp].sameAsClass && instrRUsages[toOp].sameAsClass) |
| 181 | ? classPairGaps[getSchedClass(fromOp)][getSchedClass(toOp)] |
| 182 | : ComputeMinGap(instrRUsages[fromOp], instrRUsages[toOp]); |
| 183 | |
| 184 | if (instrPairGap > 0) |
| 185 | { |
| 186 | issueGaps[OpCodePair(fromOp,toOp)] = instrPairGap; |
| 187 | conflictLists[fromOp].push_back(toOp); |
| 188 | longestIssueConflict = max(longestIssueConflict, instrPairGap); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |