Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 1 | //===-- MSSchedule.cpp Schedule ---------------------------------*- C++ -*-===// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #define DEBUG_TYPE "ModuloSched" |
| 14 | |
| 15 | #include "MSSchedule.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetSchedInfo.h" |
Misha Brukman | 9da1134b | 2004-10-10 23:34:50 +0000 | [diff] [blame] | 18 | #include "../SparcV9Internals.h" |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
Tanya Lattner | a31ad51 | 2005-02-23 02:01:42 +0000 | [diff] [blame] | 22 | //Returns a boolean indicating if the start cycle needs to be increased/decreased |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 23 | bool MSSchedule::insert(MSchedGraphNode *node, int cycle) { |
| 24 | |
| 25 | //First, check if the cycle has a spot free to start |
| 26 | if(schedule.find(cycle) != schedule.end()) { |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 27 | //Check if we have a free issue slot at this cycle |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 28 | if (schedule[cycle].size() < numIssue) { |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 29 | //Now check if all the resources in their respective cycles are available |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 30 | if(resourcesFree(node, cycle)) { |
Tanya Lattner | 341828e | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 31 | //Insert to preserve dependencies |
| 32 | addToSchedule(cycle,node); |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 33 | DEBUG(std::cerr << "Found spot in map, and there is an issue slot\n"); |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | //Not in the map yet so put it in |
| 39 | else { |
| 40 | if(resourcesFree(node,cycle)) { |
| 41 | std::vector<MSchedGraphNode*> nodes; |
| 42 | nodes.push_back(node); |
| 43 | schedule[cycle] = nodes; |
| 44 | DEBUG(std::cerr << "Nothing in map yet so taking an issue slot\n"); |
| 45 | return false; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | DEBUG(std::cerr << "All issue slots taken\n"); |
| 50 | return true; |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 51 | |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Tanya Lattner | 341828e | 2004-11-28 23:36:15 +0000 | [diff] [blame] | 54 | void MSSchedule::addToSchedule(int cycle, MSchedGraphNode *node) { |
| 55 | std::vector<MSchedGraphNode*> nodesAtCycle = schedule[cycle]; |
| 56 | |
| 57 | std::map<unsigned, MSchedGraphNode*> indexMap; |
| 58 | for(unsigned i=0; i < nodesAtCycle.size(); ++i) { |
| 59 | indexMap[nodesAtCycle[i]->getIndex()] = nodesAtCycle[i]; |
| 60 | } |
| 61 | |
| 62 | indexMap[node->getIndex()] = node; |
| 63 | |
| 64 | std::vector<MSchedGraphNode*> nodes; |
| 65 | for(std::map<unsigned, MSchedGraphNode*>::iterator I = indexMap.begin(), E = indexMap.end(); I != E; ++I) |
| 66 | nodes.push_back(I->second); |
| 67 | |
| 68 | schedule[cycle] = nodes; |
| 69 | } |
| 70 | |
| 71 | |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 72 | bool MSSchedule::resourcesFree(MSchedGraphNode *node, int cycle) { |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 73 | |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 74 | //Get Resource usage for this instruction |
Tanya Lattner | 081fbd1 | 2004-07-30 23:36:10 +0000 | [diff] [blame] | 75 | const TargetSchedInfo *msi = node->getParent()->getTarget()->getSchedInfo(); |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 76 | int currentCycle = cycle; |
| 77 | bool success = true; |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 78 | |
| 79 | //Get resource usage for this instruction |
| 80 | InstrRUsage rUsage = msi->getInstrRUsage(node->getInst()->getOpcode()); |
| 81 | std::vector<std::vector<resourceId_t> > resources = rUsage.resourcesByCycle; |
| 82 | |
| 83 | //Loop over resources in each cycle and increments their usage count |
| 84 | for(unsigned i=0; i < resources.size(); ++i) { |
| 85 | for(unsigned j=0; j < resources[i].size(); ++j) { |
| 86 | |
| 87 | //Get Resource to check its availability |
| 88 | int resourceNum = resources[i][j]; |
| 89 | |
| 90 | DEBUG(std::cerr << "Attempting to schedule Resource Num: " << resourceNum << " in cycle: " << currentCycle << "\n"); |
| 91 | |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 92 | //Check if this resource is available for this cycle |
| 93 | std::map<int, std::map<int,int> >::iterator resourcesForCycle = resourceNumPerCycle.find(currentCycle); |
| 94 | |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 95 | //First check if map exists for this cycle |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 96 | if(resourcesForCycle != resourceNumPerCycle.end()) { |
| 97 | //A map exists for this cycle, so lets check for the resource |
| 98 | std::map<int, int>::iterator resourceUse = resourcesForCycle->second.find(resourceNum); |
| 99 | if(resourceUse != resourcesForCycle->second.end()) { |
| 100 | //Check if there are enough of this resource and if so, increase count and move on |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 101 | if(resourceUse->second < CPUResource::getCPUResource(resourceNum)->maxNumUsers) |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 102 | ++resourceUse->second; |
Tanya Lattner | ab9cf27 | 2004-11-22 20:41:24 +0000 | [diff] [blame] | 103 | |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 104 | else { |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 105 | DEBUG(std::cerr << "No resource num " << resourceNum << " available for cycle " << currentCycle << "\n"); |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 106 | success = false; |
| 107 | } |
| 108 | } |
| 109 | //Not in the map yet, so put it |
| 110 | else |
| 111 | resourcesForCycle->second[resourceNum] = 1; |
| 112 | |
| 113 | } |
| 114 | else { |
| 115 | //Create a new map and put in our resource |
| 116 | std::map<int, int> resourceMap; |
| 117 | resourceMap[resourceNum] = 1; |
Tanya Lattner | ab9cf27 | 2004-11-22 20:41:24 +0000 | [diff] [blame] | 118 | resourceNumPerCycle[currentCycle] = resourceMap; |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 119 | } |
| 120 | if(!success) |
| 121 | break; |
| 122 | } |
| 123 | if(!success) |
| 124 | break; |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 125 | |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 126 | |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 127 | //Increase cycle |
| 128 | currentCycle++; |
| 129 | } |
| 130 | |
| 131 | if(!success) { |
| 132 | int oldCycle = cycle; |
| 133 | DEBUG(std::cerr << "Backtrack\n"); |
| 134 | //Get resource usage for this instruction |
| 135 | InstrRUsage rUsage = msi->getInstrRUsage(node->getInst()->getOpcode()); |
| 136 | std::vector<std::vector<resourceId_t> > resources = rUsage.resourcesByCycle; |
| 137 | |
| 138 | //Loop over resources in each cycle and increments their usage count |
| 139 | for(unsigned i=0; i < resources.size(); ++i) { |
| 140 | if(oldCycle < currentCycle) { |
| 141 | |
| 142 | //Check if this resource is available for this cycle |
| 143 | std::map<int, std::map<int,int> >::iterator resourcesForCycle = resourceNumPerCycle.find(oldCycle); |
| 144 | if(resourcesForCycle != resourceNumPerCycle.end()) { |
| 145 | for(unsigned j=0; j < resources[i].size(); ++j) { |
| 146 | int resourceNum = resources[i][j]; |
| 147 | //remove from map |
| 148 | std::map<int, int>::iterator resourceUse = resourcesForCycle->second.find(resourceNum); |
| 149 | //assert if not in the map.. since it should be! |
| 150 | //assert(resourceUse != resourcesForCycle.end() && "Resource should be in map!"); |
Tanya Lattner | a31ad51 | 2005-02-23 02:01:42 +0000 | [diff] [blame] | 151 | DEBUG(std::cerr << "Removing resource num " << resourceNum << " from cycle " << oldCycle << "\n"); |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 152 | --resourceUse->second; |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 155 | } |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 156 | else |
| 157 | break; |
| 158 | oldCycle++; |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 159 | } |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 160 | return false; |
| 161 | |
| 162 | } |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 163 | |
Tanya Lattner | 13c71ca | 2004-11-24 01:49:10 +0000 | [diff] [blame] | 164 | return true; |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 165 | |
| 166 | } |
| 167 | |
Tanya Lattner | 13417b5 | 2005-03-23 01:47:20 +0000 | [diff] [blame^] | 168 | bool MSSchedule::constructKernel(int II, std::vector<MSchedGraphNode*> &branches, std::map<const MachineInstr*, unsigned> &indVar) { |
Tanya Lattner | 201e972 | 2004-12-02 07:22:15 +0000 | [diff] [blame] | 169 | |
Tanya Lattner | 13417b5 | 2005-03-23 01:47:20 +0000 | [diff] [blame^] | 170 | //Our schedule is allowed to have negative numbers, so lets calculate this offset |
| 171 | int offset = schedule.begin()->first; |
| 172 | if(offset > 0) |
| 173 | offset = 0; |
| 174 | |
| 175 | DEBUG(std::cerr << "Offset: " << offset << "\n"); |
| 176 | |
| 177 | //Not sure what happens in this case, but assert if offset is > II |
| 178 | //assert(offset > -II && "Offset can not be more then II"); |
| 179 | |
| 180 | std::vector<std::pair<MSchedGraphNode*, int> > tempKernel; |
| 181 | |
| 182 | |
| 183 | int stageNum = ((schedule.rbegin()->first-offset)+1)/ II; |
| 184 | int maxSN = 0; |
| 185 | |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 186 | DEBUG(std::cerr << "Number of Stages: " << stageNum << "\n"); |
| 187 | |
Tanya Lattner | 13417b5 | 2005-03-23 01:47:20 +0000 | [diff] [blame^] | 188 | for(int index = offset; index < (II+offset); ++index) { |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 189 | int count = 0; |
| 190 | for(int i = index; i <= (schedule.rbegin()->first); i+=II) { |
| 191 | if(schedule.count(i)) { |
| 192 | for(std::vector<MSchedGraphNode*>::iterator I = schedule[i].begin(), |
| 193 | E = schedule[i].end(); I != E; ++I) { |
| 194 | //Check if its a branch |
| 195 | if((*I)->isBranch()) { |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 196 | assert(count == 0 && "Branch can not be from a previous iteration"); |
Tanya Lattner | 13417b5 | 2005-03-23 01:47:20 +0000 | [diff] [blame^] | 197 | tempKernel.push_back(std::make_pair(*I, count)); |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 198 | } |
Tanya Lattner | 13417b5 | 2005-03-23 01:47:20 +0000 | [diff] [blame^] | 199 | else { |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 200 | //FIXME: Check if the instructions in the earlier stage conflict |
Tanya Lattner | 13417b5 | 2005-03-23 01:47:20 +0000 | [diff] [blame^] | 201 | tempKernel.push_back(std::make_pair(*I, count)); |
| 202 | maxSN = std::max(maxSN, count); |
| 203 | } |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | ++count; |
| 207 | } |
| 208 | } |
Tanya Lattner | 13417b5 | 2005-03-23 01:47:20 +0000 | [diff] [blame^] | 209 | |
| 210 | //Add in induction var code |
| 211 | for(std::vector<std::pair<MSchedGraphNode*, int> >::iterator I = tempKernel.begin(), IE = tempKernel.end(); |
| 212 | I != IE; ++I) { |
| 213 | //Add indVar instructions before this one for the current iteration |
| 214 | if(I->second == 0) { |
| 215 | std::map<unsigned, MachineInstr*> tmpMap; |
| 216 | |
| 217 | //Loop over induction variable instructions in the map that come before this instr |
| 218 | for(std::map<const MachineInstr*, unsigned>::iterator N = indVar.begin(), NE = indVar.end(); N != NE; ++N) { |
| 219 | |
| 220 | |
| 221 | if(N->second < I->first->getIndex()) |
| 222 | tmpMap[N->second] = (MachineInstr*) N->first; |
| 223 | } |
| 224 | |
| 225 | //Add to kernel, and delete from indVar |
| 226 | for(std::map<unsigned, MachineInstr*>::iterator N = tmpMap.begin(), NE = tmpMap.end(); N != NE; ++N) { |
| 227 | kernel.push_back(std::make_pair(N->second, 0)); |
| 228 | indVar.erase(N->second); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | kernel.push_back(std::make_pair((MachineInstr*) I->first->getInst(), I->second)); |
| 233 | |
Tanya Lattner | a31ad51 | 2005-02-23 02:01:42 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Tanya Lattner | 13417b5 | 2005-03-23 01:47:20 +0000 | [diff] [blame^] | 236 | std::map<unsigned, MachineInstr*> tmpMap; |
| 237 | |
| 238 | //Add remaining invar instructions |
| 239 | for(std::map<const MachineInstr*, unsigned>::iterator N = indVar.begin(), NE = indVar.end(); N != NE; ++N) { |
| 240 | tmpMap[N->second] = (MachineInstr*) N->first; |
| 241 | } |
| 242 | |
| 243 | //Add to kernel, and delete from indVar |
| 244 | for(std::map<unsigned, MachineInstr*>::iterator N = tmpMap.begin(), NE = tmpMap.end(); N != NE; ++N) { |
| 245 | kernel.push_back(std::make_pair(N->second, 0)); |
| 246 | indVar.erase(N->second); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | maxStage = maxSN; |
| 251 | |
Tanya Lattner | dbac0cb | 2004-10-10 22:44:35 +0000 | [diff] [blame] | 252 | |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void MSSchedule::print(std::ostream &os) const { |
| 258 | os << "Schedule:\n"; |
| 259 | |
| 260 | for(schedule_const_iterator I = schedule.begin(), E = schedule.end(); I != E; ++I) { |
| 261 | os << "Cycle: " << I->first << "\n"; |
| 262 | for(std::vector<MSchedGraphNode*>::const_iterator node = I->second.begin(), nodeEnd = I->second.end(); node != nodeEnd; ++node) |
| 263 | os << **node << "\n"; |
| 264 | } |
| 265 | |
| 266 | os << "Kernel:\n"; |
Tanya Lattner | 13417b5 | 2005-03-23 01:47:20 +0000 | [diff] [blame^] | 267 | for(std::vector<std::pair<MachineInstr*, int> >::const_iterator I = kernel.begin(), |
Tanya Lattner | 642685a | 2004-05-26 06:27:36 +0000 | [diff] [blame] | 268 | E = kernel.end(); I != E; ++I) |
| 269 | os << "Node: " << *(I->first) << " Stage: " << I->second << "\n"; |
| 270 | } |
| 271 | |