Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 1 | //===-- SchedInfo.cpp - Generic code to support target schedulers ----------==// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 9 | // |
| 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. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | d0f166a | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetSchedInfo.h" |
Chris Lattner | 884f4b5 | 2002-02-03 07:47:05 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetMachine.h" |
Tanya Lattner | 6b16050 | 2004-05-08 16:12:50 +0000 | [diff] [blame^] | 17 | #include <iostream> |
| 18 | using namespace llvm; |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 19 | |
Tanya Lattner | 6b16050 | 2004-05-08 16:12:50 +0000 | [diff] [blame^] | 20 | resourceId_t llvm::CPUResource::nextId = 0; |
| 21 | static std::vector<CPUResource*> *CPUResourceMap = 0; |
| 22 | |
Chris Lattner | 55a4700 | 2004-05-01 11:17:13 +0000 | [diff] [blame] | 23 | CPUResource::CPUResource(const std::string& resourceName, int maxUsers) |
Tanya Lattner | 6b16050 | 2004-05-08 16:12:50 +0000 | [diff] [blame^] | 24 | : rname(resourceName), rid(nextId++), maxNumUsers(maxUsers) { |
| 25 | if(!CPUResourceMap) |
| 26 | CPUResourceMap = new std::vector<CPUResource*>; |
| 27 | |
| 28 | //Put Resource in the map |
| 29 | CPUResourceMap->push_back(this); |
| 30 | } |
| 31 | |
| 32 | ///Get CPUResource if you only have the resource ID |
| 33 | CPUResource* CPUResource::getCPUResource(resourceId_t id) { |
| 34 | return (*CPUResourceMap)[id]; |
| 35 | } |
Chris Lattner | 55a4700 | 2004-05-01 11:17:13 +0000 | [diff] [blame] | 36 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 37 | // Check if fromRVec and toRVec have *any* common entries. |
| 38 | // Assume the vectors are sorted in increasing order. |
| 39 | // Algorithm copied from function set_intersection() for sorted ranges |
| 40 | // (stl_algo.h). |
| 41 | // |
| 42 | inline static bool |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 43 | RUConflict(const std::vector<resourceId_t>& fromRVec, |
| 44 | const std::vector<resourceId_t>& toRVec) |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 45 | { |
| 46 | |
| 47 | unsigned fN = fromRVec.size(), tN = toRVec.size(); |
| 48 | unsigned fi = 0, ti = 0; |
| 49 | |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 50 | while (fi < fN && ti < tN) { |
| 51 | if (fromRVec[fi] < toRVec[ti]) |
| 52 | ++fi; |
| 53 | else if (toRVec[ti] < fromRVec[fi]) |
| 54 | ++ti; |
| 55 | else |
| 56 | return true; |
| 57 | } |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | static cycles_t |
| 63 | ComputeMinGap(const InstrRUsage &fromRU, |
| 64 | const InstrRUsage &toRU) |
| 65 | { |
| 66 | cycles_t minGap = 0; |
| 67 | |
| 68 | if (fromRU.numBubbles > 0) |
| 69 | minGap = fromRU.numBubbles; |
| 70 | |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 71 | if (minGap < fromRU.numCycles) { |
| 72 | // only need to check from cycle `minGap' onwards |
| 73 | for (cycles_t gap=minGap; gap <= fromRU.numCycles-1; gap++) { |
| 74 | // check if instr. #2 can start executing `gap' cycles after #1 |
| 75 | // by checking for resource conflicts in each overlapping cycle |
| 76 | cycles_t numOverlap =std::min(fromRU.numCycles - gap, toRU.numCycles); |
| 77 | for (cycles_t c = 0; c <= numOverlap-1; c++) |
| 78 | if (RUConflict(fromRU.resourcesByCycle[gap + c], |
| 79 | toRU.resourcesByCycle[c])) { |
| 80 | // conflict found so minGap must be more than `gap' |
| 81 | minGap = gap+1; |
| 82 | break; |
| 83 | } |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 84 | } |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 85 | } |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 86 | |
| 87 | return minGap; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | //--------------------------------------------------------------------------- |
Chris Lattner | d0f166a | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 92 | // class TargetSchedInfo |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 93 | // Interface to machine description for instruction scheduling |
| 94 | //--------------------------------------------------------------------------- |
| 95 | |
Chris Lattner | d0f166a | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 96 | TargetSchedInfo::TargetSchedInfo(const TargetMachine& tgt, |
| 97 | int NumSchedClasses, |
| 98 | const InstrClassRUsage* ClassRUsages, |
| 99 | const InstrRUsageDelta* UsageDeltas, |
| 100 | const InstrIssueDelta* IssueDeltas, |
| 101 | unsigned NumUsageDeltas, |
| 102 | unsigned NumIssueDeltas) |
Vikram S. Adve | 7a2f1e7 | 2001-11-08 05:15:08 +0000 | [diff] [blame] | 103 | : target(tgt), |
| 104 | numSchedClasses(NumSchedClasses), mii(& tgt.getInstrInfo()), |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 105 | classRUsages(ClassRUsages), usageDeltas(UsageDeltas), |
| 106 | issueDeltas(IssueDeltas), numUsageDeltas(NumUsageDeltas), |
| 107 | numIssueDeltas(NumIssueDeltas) |
| 108 | {} |
| 109 | |
| 110 | void |
Chris Lattner | d0f166a | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 111 | TargetSchedInfo::initializeResources() |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 112 | { |
| 113 | assert(MAX_NUM_SLOTS >= (int)getMaxNumIssueTotal() |
| 114 | && "Insufficient slots for static data! Increase MAX_NUM_SLOTS"); |
| 115 | |
| 116 | // First, compute common resource usage info for each class because |
| 117 | // most instructions will probably behave the same as their class. |
| 118 | // Cannot allocate a vector of InstrRUsage so new each one. |
| 119 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 120 | std::vector<InstrRUsage> instrRUForClasses; |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 121 | instrRUForClasses.resize(numSchedClasses); |
| 122 | for (InstrSchedClass sc = 0; sc < numSchedClasses; sc++) { |
| 123 | // instrRUForClasses.push_back(new InstrRUsage); |
| 124 | instrRUForClasses[sc].setMaxSlots(getMaxNumIssueTotal()); |
Chris Lattner | 3058761 | 2002-02-04 05:56:30 +0000 | [diff] [blame] | 125 | instrRUForClasses[sc].setTo(classRUsages[sc]); |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | computeInstrResources(instrRUForClasses); |
| 129 | computeIssueGaps(instrRUForClasses); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | void |
Chris Lattner | d0f166a | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 134 | TargetSchedInfo::computeInstrResources(const std::vector<InstrRUsage>& |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 135 | instrRUForClasses) |
| 136 | { |
Chris Lattner | bceb688 | 2004-02-29 06:31:16 +0000 | [diff] [blame] | 137 | int numOpCodes = mii->getNumOpcodes(); |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 138 | instrRUsages.resize(numOpCodes); |
| 139 | |
| 140 | // First get the resource usage information from the class resource usages. |
| 141 | for (MachineOpCode op = 0; op < numOpCodes; ++op) { |
| 142 | InstrSchedClass sc = getSchedClass(op); |
Chris Lattner | 07541a2 | 2002-10-28 04:59:43 +0000 | [diff] [blame] | 143 | assert(sc < numSchedClasses); |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 144 | instrRUsages[op] = instrRUForClasses[sc]; |
| 145 | } |
| 146 | |
| 147 | // Now, modify the resource usages as specified in the deltas. |
| 148 | for (unsigned i = 0; i < numUsageDeltas; ++i) { |
| 149 | MachineOpCode op = usageDeltas[i].opCode; |
| 150 | assert(op < numOpCodes); |
| 151 | instrRUsages[op].addUsageDelta(usageDeltas[i]); |
| 152 | } |
| 153 | |
| 154 | // Then modify the issue restrictions as specified in the deltas. |
| 155 | for (unsigned i = 0; i < numIssueDeltas; ++i) { |
| 156 | MachineOpCode op = issueDeltas[i].opCode; |
| 157 | assert(op < numOpCodes); |
| 158 | instrRUsages[issueDeltas[i].opCode].addIssueDelta(issueDeltas[i]); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | void |
Chris Lattner | d0f166a | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 164 | TargetSchedInfo::computeIssueGaps(const std::vector<InstrRUsage>& |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 165 | instrRUForClasses) |
| 166 | { |
Chris Lattner | bceb688 | 2004-02-29 06:31:16 +0000 | [diff] [blame] | 167 | int numOpCodes = mii->getNumOpcodes(); |
Vikram S. Adve | 5aefcad | 2002-10-13 00:37:46 +0000 | [diff] [blame] | 168 | issueGaps.resize(numOpCodes); |
| 169 | conflictLists.resize(numOpCodes); |
| 170 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 171 | assert(numOpCodes < (1 << MAX_OPCODE_SIZE) - 1 |
| 172 | && "numOpCodes invalid for implementation of class OpCodePair!"); |
Vikram S. Adve | 5aefcad | 2002-10-13 00:37:46 +0000 | [diff] [blame] | 173 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 174 | // First, compute issue gaps between pairs of classes based on common |
| 175 | // resources usages for each class, because most instruction pairs will |
| 176 | // usually behave the same as their class. |
| 177 | // |
| 178 | int classPairGaps[numSchedClasses][numSchedClasses]; |
| 179 | for (InstrSchedClass fromSC=0; fromSC < numSchedClasses; fromSC++) |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 180 | for (InstrSchedClass toSC=0; toSC < numSchedClasses; toSC++) { |
| 181 | int classPairGap = ComputeMinGap(instrRUForClasses[fromSC], |
| 182 | instrRUForClasses[toSC]); |
| 183 | classPairGaps[fromSC][toSC] = classPairGap; |
| 184 | } |
Vikram S. Adve | 5aefcad | 2002-10-13 00:37:46 +0000 | [diff] [blame] | 185 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 186 | // Now, for each pair of instructions, use the class pair gap if both |
| 187 | // instructions have identical resource usage as their respective classes. |
| 188 | // If not, recompute the gap for the pair from scratch. |
Vikram S. Adve | 5aefcad | 2002-10-13 00:37:46 +0000 | [diff] [blame] | 189 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 190 | longestIssueConflict = 0; |
Vikram S. Adve | 5aefcad | 2002-10-13 00:37:46 +0000 | [diff] [blame] | 191 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 192 | for (MachineOpCode fromOp=0; fromOp < numOpCodes; fromOp++) |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 193 | for (MachineOpCode toOp=0; toOp < numOpCodes; toOp++) { |
| 194 | int instrPairGap = |
| 195 | (instrRUsages[fromOp].sameAsClass && instrRUsages[toOp].sameAsClass) |
| 196 | ? classPairGaps[getSchedClass(fromOp)][getSchedClass(toOp)] |
| 197 | : ComputeMinGap(instrRUsages[fromOp], instrRUsages[toOp]); |
Vikram S. Adve | 5aefcad | 2002-10-13 00:37:46 +0000 | [diff] [blame] | 198 | |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 199 | if (instrPairGap > 0) { |
| 200 | this->setGap(instrPairGap, fromOp, toOp); |
| 201 | conflictLists[fromOp].push_back(toOp); |
| 202 | longestIssueConflict=std::max(longestIssueConflict, instrPairGap); |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 203 | } |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 204 | } |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Chris Lattner | 3058761 | 2002-02-04 05:56:30 +0000 | [diff] [blame] | 207 | |
| 208 | void InstrRUsage::setTo(const InstrClassRUsage& classRU) { |
| 209 | sameAsClass = true; |
| 210 | isSingleIssue = classRU.isSingleIssue; |
| 211 | breaksGroup = classRU.breaksGroup; |
| 212 | numBubbles = classRU.numBubbles; |
| 213 | |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 214 | for (unsigned i=0; i < classRU.numSlots; i++) { |
| 215 | unsigned slot = classRU.feasibleSlots[i]; |
| 216 | assert(slot < feasibleSlots.size() && "Invalid slot specified!"); |
| 217 | this->feasibleSlots[slot] = true; |
| 218 | } |
Chris Lattner | 3058761 | 2002-02-04 05:56:30 +0000 | [diff] [blame] | 219 | |
| 220 | numCycles = classRU.totCycles; |
| 221 | resourcesByCycle.resize(this->numCycles); |
| 222 | |
| 223 | for (unsigned i=0; i < classRU.numRUEntries; i++) |
| 224 | for (unsigned c=classRU.V[i].startCycle, NC = c + classRU.V[i].numCycles; |
| 225 | c < NC; c++) |
| 226 | this->resourcesByCycle[c].push_back(classRU.V[i].resourceId); |
| 227 | |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 228 | // Sort each resource usage vector by resourceId_t to speed up conflict |
| 229 | // checking |
Chris Lattner | 3058761 | 2002-02-04 05:56:30 +0000 | [diff] [blame] | 230 | for (unsigned i=0; i < this->resourcesByCycle.size(); i++) |
| 231 | sort(resourcesByCycle[i].begin(), resourcesByCycle[i].end()); |
Chris Lattner | 3058761 | 2002-02-04 05:56:30 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // Add the extra resource usage requirements specified in the delta. |
| 235 | // Note that a negative value of `numCycles' means one entry for that |
| 236 | // resource should be deleted for each cycle. |
| 237 | // |
| 238 | void InstrRUsage::addUsageDelta(const InstrRUsageDelta &delta) { |
| 239 | int NC = delta.numCycles; |
| 240 | sameAsClass = false; |
| 241 | |
| 242 | // resize the resources vector if more cycles are specified |
| 243 | unsigned maxCycles = this->numCycles; |
| 244 | maxCycles = std::max(maxCycles, delta.startCycle + abs(NC) - 1); |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 245 | if (maxCycles > this->numCycles) { |
| 246 | this->resourcesByCycle.resize(maxCycles); |
| 247 | this->numCycles = maxCycles; |
| 248 | } |
Chris Lattner | 3058761 | 2002-02-04 05:56:30 +0000 | [diff] [blame] | 249 | |
| 250 | if (NC >= 0) |
| 251 | for (unsigned c=delta.startCycle, last=c+NC-1; c <= last; c++) |
| 252 | this->resourcesByCycle[c].push_back(delta.resourceId); |
| 253 | else |
| 254 | // Remove the resource from all NC cycles. |
Misha Brukman | b10cea8 | 2003-08-05 00:02:06 +0000 | [diff] [blame] | 255 | for (unsigned c=delta.startCycle, last=(c-NC)-1; c <= last; c++) { |
| 256 | // Look for the resource backwards so we remove the last entry |
| 257 | // for that resource in each cycle. |
| 258 | std::vector<resourceId_t>& rvec = this->resourcesByCycle[c]; |
| 259 | int r; |
| 260 | for (r = rvec.size() - 1; r >= 0; r--) |
| 261 | if (rvec[r] == delta.resourceId) { |
| 262 | // found last entry for the resource |
| 263 | rvec.erase(rvec.begin() + r); |
| 264 | break; |
| 265 | } |
| 266 | assert(r >= 0 && "Resource to remove was unused in cycle c!"); |
| 267 | } |
Chris Lattner | 3058761 | 2002-02-04 05:56:30 +0000 | [diff] [blame] | 268 | } |