Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1 | //===----- ScheduleDAGList.cpp - Reg pressure reduction list scheduler ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Evan Cheng and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements bottom-up and top-down register pressure reduction list |
| 11 | // schedulers, using standard algorithms. The basic approach uses a priority |
| 12 | // queue of available nodes to schedule. One at a time, nodes are taken from |
| 13 | // the priority queue (thus in priority order), checked for legality to |
| 14 | // schedule, and emitted if legal. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #define DEBUG_TYPE "sched" |
| 19 | #include "llvm/CodeGen/ScheduleDAG.h" |
Jim Laskey | 29e635d | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SSARegMap.h" |
| 22 | #include "llvm/Target/MRegisterInfo.h" |
Owen Anderson | 8c2c1e9 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
| 25 | #include "llvm/Target/TargetInstrInfo.h" |
| 26 | #include "llvm/Support/Debug.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Compiler.h" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" |
| 29 | #include <climits> |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 30 | #include <queue> |
| 31 | #include "llvm/Support/CommandLine.h" |
| 32 | using namespace llvm; |
| 33 | |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 34 | static RegisterScheduler |
| 35 | burrListDAGScheduler("list-burr", |
| 36 | " Bottom-up register reduction list scheduling", |
| 37 | createBURRListDAGScheduler); |
| 38 | static RegisterScheduler |
| 39 | tdrListrDAGScheduler("list-tdrr", |
| 40 | " Top-down register reduction list scheduling", |
| 41 | createTDRRListDAGScheduler); |
| 42 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 43 | namespace { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
| 45 | /// ScheduleDAGRRList - The actual register reduction list scheduler |
| 46 | /// implementation. This supports both top-down and bottom-up scheduling. |
| 47 | /// |
| 48 | |
Chris Lattner | e097e6f | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 49 | class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAG { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 50 | private: |
| 51 | /// isBottomUp - This is true if the scheduling problem is bottom-up, false if |
| 52 | /// it is top-down. |
| 53 | bool isBottomUp; |
| 54 | |
| 55 | /// AvailableQueue - The priority queue to use for the available SUnits. |
| 56 | /// |
| 57 | SchedulingPriorityQueue *AvailableQueue; |
| 58 | |
| 59 | public: |
| 60 | ScheduleDAGRRList(SelectionDAG &dag, MachineBasicBlock *bb, |
| 61 | const TargetMachine &tm, bool isbottomup, |
| 62 | SchedulingPriorityQueue *availqueue) |
| 63 | : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup), |
| 64 | AvailableQueue(availqueue) { |
| 65 | } |
| 66 | |
| 67 | ~ScheduleDAGRRList() { |
| 68 | delete AvailableQueue; |
| 69 | } |
| 70 | |
| 71 | void Schedule(); |
| 72 | |
| 73 | private: |
| 74 | void ReleasePred(SUnit *PredSU, bool isChain, unsigned CurCycle); |
| 75 | void ReleaseSucc(SUnit *SuccSU, bool isChain, unsigned CurCycle); |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 76 | void ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle); |
| 77 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 78 | void ListScheduleTopDown(); |
| 79 | void ListScheduleBottomUp(); |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 80 | void CommuteNodesToReducePressure(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 81 | }; |
| 82 | } // end anonymous namespace |
| 83 | |
| 84 | |
| 85 | /// Schedule - Schedule the DAG using list scheduling. |
| 86 | void ScheduleDAGRRList::Schedule() { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 87 | DOUT << "********** List Scheduling **********\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 88 | |
| 89 | // Build scheduling units. |
| 90 | BuildSchedUnits(); |
| 91 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 92 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 93 | SUnits[su].dumpAll(&DAG)); |
Evan Cheng | 47fbeda | 2006-10-14 08:34:06 +0000 | [diff] [blame] | 94 | CalculateDepths(); |
| 95 | CalculateHeights(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 96 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 97 | AvailableQueue->initNodes(SUnitMap, SUnits); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 98 | |
| 99 | // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate. |
| 100 | if (isBottomUp) |
| 101 | ListScheduleBottomUp(); |
| 102 | else |
| 103 | ListScheduleTopDown(); |
| 104 | |
| 105 | AvailableQueue->releaseState(); |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 106 | |
Evan Cheng | 009f5f5 | 2006-05-25 08:37:31 +0000 | [diff] [blame] | 107 | CommuteNodesToReducePressure(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 108 | |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 109 | DOUT << "*** Final schedule ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 110 | DEBUG(dumpSchedule()); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 111 | DOUT << "\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 112 | |
| 113 | // Emit in scheduled order |
| 114 | EmitSchedule(); |
| 115 | } |
| 116 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 117 | /// CommuteNodesToReducePressure - If a node is two-address and commutable, and |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 118 | /// it is not the last use of its first operand, add it to the CommuteSet if |
| 119 | /// possible. It will be commuted when it is translated to a MI. |
| 120 | void ScheduleDAGRRList::CommuteNodesToReducePressure() { |
| 121 | std::set<SUnit *> OperandSeen; |
| 122 | for (unsigned i = Sequence.size()-1; i != 0; --i) { // Ignore first node. |
| 123 | SUnit *SU = Sequence[i]; |
| 124 | if (!SU) continue; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 125 | if (SU->isCommutable) { |
| 126 | unsigned Opc = SU->Node->getTargetOpcode(); |
| 127 | unsigned NumRes = CountResults(SU->Node); |
| 128 | unsigned NumOps = CountOperands(SU->Node); |
| 129 | for (unsigned j = 0; j != NumOps; ++j) { |
Evan Cheng | 67fc141 | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 130 | if (TII->getOperandConstraint(Opc, j+NumRes, TOI::TIED_TO) == -1) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 131 | continue; |
| 132 | |
| 133 | SDNode *OpN = SU->Node->getOperand(j).Val; |
| 134 | SUnit *OpSU = SUnitMap[OpN]; |
| 135 | if (OpSU && OperandSeen.count(OpSU) == 1) { |
| 136 | // Ok, so SU is not the last use of OpSU, but SU is two-address so |
| 137 | // it will clobber OpSU. Try to commute SU if no other source operands |
| 138 | // are live below. |
| 139 | bool DoCommute = true; |
| 140 | for (unsigned k = 0; k < NumOps; ++k) { |
| 141 | if (k != j) { |
| 142 | OpN = SU->Node->getOperand(k).Val; |
| 143 | OpSU = SUnitMap[OpN]; |
| 144 | if (OpSU && OperandSeen.count(OpSU) == 1) { |
| 145 | DoCommute = false; |
| 146 | break; |
| 147 | } |
| 148 | } |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 149 | } |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 150 | if (DoCommute) |
| 151 | CommuteSet.insert(SU->Node); |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 152 | } |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 153 | |
| 154 | // Only look at the first use&def node for now. |
| 155 | break; |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 159 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 160 | I != E; ++I) { |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 161 | if (!I->second) |
| 162 | OperandSeen.insert(I->first); |
| 163 | } |
| 164 | } |
| 165 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 166 | |
| 167 | //===----------------------------------------------------------------------===// |
| 168 | // Bottom-Up Scheduling |
| 169 | //===----------------------------------------------------------------------===// |
| 170 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 171 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to |
| 172 | /// the Available queue is the count reaches zero. Also update its cycle bound. |
| 173 | void ScheduleDAGRRList::ReleasePred(SUnit *PredSU, bool isChain, |
| 174 | unsigned CurCycle) { |
| 175 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 176 | // latency. For example, the reader can very well read the register written |
| 177 | // by the predecessor later than the issue cycle. It also depends on the |
| 178 | // interrupt model (drain vs. freeze). |
| 179 | PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency); |
| 180 | |
| 181 | if (!isChain) |
| 182 | PredSU->NumSuccsLeft--; |
| 183 | else |
| 184 | PredSU->NumChainSuccsLeft--; |
| 185 | |
| 186 | #ifndef NDEBUG |
| 187 | if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 188 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 189 | PredSU->dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 190 | cerr << " has been released too many times!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 191 | assert(0); |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) { |
| 196 | // EntryToken has to go last! Special case it here. |
| 197 | if (PredSU->Node->getOpcode() != ISD::EntryToken) { |
| 198 | PredSU->isAvailable = true; |
| 199 | AvailableQueue->push(PredSU); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending |
| 205 | /// count of its predecessors. If a predecessor pending count is zero, add it to |
| 206 | /// the Available queue. |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 207 | void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 208 | DOUT << "*** Scheduling [" << CurCycle << "]: "; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 209 | DEBUG(SU->dump(&DAG)); |
| 210 | SU->Cycle = CurCycle; |
| 211 | |
| 212 | AvailableQueue->ScheduledNode(SU); |
| 213 | Sequence.push_back(SU); |
| 214 | |
| 215 | // Bottom up: release predecessors |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 216 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 217 | I != E; ++I) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 218 | ReleasePred(I->first, I->second, CurCycle); |
| 219 | SU->isScheduled = true; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /// isReady - True if node's lower cycle bound is less or equal to the current |
| 223 | /// scheduling cycle. Always true if all nodes have uniform latency 1. |
| 224 | static inline bool isReady(SUnit *SU, unsigned CurCycle) { |
| 225 | return SU->CycleBound <= CurCycle; |
| 226 | } |
| 227 | |
| 228 | /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up |
| 229 | /// schedulers. |
| 230 | void ScheduleDAGRRList::ListScheduleBottomUp() { |
| 231 | unsigned CurCycle = 0; |
| 232 | // Add root to Available queue. |
| 233 | AvailableQueue->push(SUnitMap[DAG.getRoot().Val]); |
| 234 | |
| 235 | // While Available queue is not empty, grab the node with the highest |
| 236 | // priority. If it is not ready put it back. Schedule the node. |
| 237 | std::vector<SUnit*> NotReady; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 238 | while (!AvailableQueue->empty()) { |
| 239 | SUnit *CurNode = AvailableQueue->pop(); |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 240 | while (CurNode && !isReady(CurNode, CurCycle)) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 241 | NotReady.push_back(CurNode); |
| 242 | CurNode = AvailableQueue->pop(); |
| 243 | } |
| 244 | |
| 245 | // Add the nodes that aren't ready back onto the available list. |
| 246 | AvailableQueue->push_all(NotReady); |
| 247 | NotReady.clear(); |
| 248 | |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 249 | if (CurNode != NULL) |
| 250 | ScheduleNodeBottomUp(CurNode, CurCycle); |
| 251 | CurCycle++; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // Add entry node last |
| 255 | if (DAG.getEntryNode().Val != DAG.getRoot().Val) { |
| 256 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
| 257 | Sequence.push_back(Entry); |
| 258 | } |
| 259 | |
| 260 | // Reverse the order if it is bottom up. |
| 261 | std::reverse(Sequence.begin(), Sequence.end()); |
| 262 | |
| 263 | |
| 264 | #ifndef NDEBUG |
| 265 | // Verify that all SUnits were scheduled. |
| 266 | bool AnyNotSched = false; |
| 267 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 268 | if (SUnits[i].NumSuccsLeft != 0 || SUnits[i].NumChainSuccsLeft != 0) { |
| 269 | if (!AnyNotSched) |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 270 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 271 | SUnits[i].dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 272 | cerr << "has not been scheduled!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 273 | AnyNotSched = true; |
| 274 | } |
| 275 | } |
| 276 | assert(!AnyNotSched); |
| 277 | #endif |
| 278 | } |
| 279 | |
| 280 | //===----------------------------------------------------------------------===// |
| 281 | // Top-Down Scheduling |
| 282 | //===----------------------------------------------------------------------===// |
| 283 | |
| 284 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
| 285 | /// the PendingQueue if the count reaches zero. |
| 286 | void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain, |
| 287 | unsigned CurCycle) { |
| 288 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 289 | // latency. For example, the reader can very well read the register written |
| 290 | // by the predecessor later than the issue cycle. It also depends on the |
| 291 | // interrupt model (drain vs. freeze). |
| 292 | SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency); |
| 293 | |
| 294 | if (!isChain) |
| 295 | SuccSU->NumPredsLeft--; |
| 296 | else |
| 297 | SuccSU->NumChainPredsLeft--; |
| 298 | |
| 299 | #ifndef NDEBUG |
| 300 | if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 301 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 302 | SuccSU->dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 303 | cerr << " has been released too many times!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 304 | assert(0); |
| 305 | } |
| 306 | #endif |
| 307 | |
| 308 | if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0) { |
| 309 | SuccSU->isAvailable = true; |
| 310 | AvailableQueue->push(SuccSU); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 316 | /// count of its successors. If a successor pending count is zero, add it to |
| 317 | /// the Available queue. |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 318 | void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 319 | DOUT << "*** Scheduling [" << CurCycle << "]: "; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 320 | DEBUG(SU->dump(&DAG)); |
| 321 | SU->Cycle = CurCycle; |
| 322 | |
| 323 | AvailableQueue->ScheduledNode(SU); |
| 324 | Sequence.push_back(SU); |
| 325 | |
| 326 | // Top down: release successors |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 327 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 328 | I != E; ++I) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 329 | ReleaseSucc(I->first, I->second, CurCycle); |
| 330 | SU->isScheduled = true; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void ScheduleDAGRRList::ListScheduleTopDown() { |
| 334 | unsigned CurCycle = 0; |
| 335 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
| 336 | |
| 337 | // All leaves to Available queue. |
| 338 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 339 | // It is available if it has no predecessors. |
| 340 | if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) { |
| 341 | AvailableQueue->push(&SUnits[i]); |
| 342 | SUnits[i].isAvailable = true; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // Emit the entry node first. |
| 347 | ScheduleNodeTopDown(Entry, CurCycle); |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 348 | CurCycle++; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 349 | |
| 350 | // While Available queue is not empty, grab the node with the highest |
| 351 | // priority. If it is not ready put it back. Schedule the node. |
| 352 | std::vector<SUnit*> NotReady; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 353 | while (!AvailableQueue->empty()) { |
| 354 | SUnit *CurNode = AvailableQueue->pop(); |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 355 | while (CurNode && !isReady(CurNode, CurCycle)) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 356 | NotReady.push_back(CurNode); |
| 357 | CurNode = AvailableQueue->pop(); |
| 358 | } |
| 359 | |
| 360 | // Add the nodes that aren't ready back onto the available list. |
| 361 | AvailableQueue->push_all(NotReady); |
| 362 | NotReady.clear(); |
| 363 | |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 364 | if (CurNode != NULL) |
| 365 | ScheduleNodeTopDown(CurNode, CurCycle); |
| 366 | CurCycle++; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | |
| 370 | #ifndef NDEBUG |
| 371 | // Verify that all SUnits were scheduled. |
| 372 | bool AnyNotSched = false; |
| 373 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 374 | if (!SUnits[i].isScheduled) { |
| 375 | if (!AnyNotSched) |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 376 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 377 | SUnits[i].dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 378 | cerr << "has not been scheduled!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 379 | AnyNotSched = true; |
| 380 | } |
| 381 | } |
| 382 | assert(!AnyNotSched); |
| 383 | #endif |
| 384 | } |
| 385 | |
| 386 | |
| 387 | |
| 388 | //===----------------------------------------------------------------------===// |
| 389 | // RegReductionPriorityQueue Implementation |
| 390 | //===----------------------------------------------------------------------===// |
| 391 | // |
| 392 | // This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers |
| 393 | // to reduce register pressure. |
| 394 | // |
| 395 | namespace { |
| 396 | template<class SF> |
| 397 | class RegReductionPriorityQueue; |
| 398 | |
| 399 | /// Sorting functions for the Available queue. |
| 400 | struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 401 | RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ; |
| 402 | bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {} |
| 403 | bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} |
| 404 | |
| 405 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 406 | }; |
| 407 | |
| 408 | struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 409 | RegReductionPriorityQueue<td_ls_rr_sort> *SPQ; |
| 410 | td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {} |
| 411 | td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} |
| 412 | |
| 413 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 414 | }; |
| 415 | } // end anonymous namespace |
| 416 | |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 417 | static inline bool isCopyFromLiveIn(const SUnit *SU) { |
| 418 | SDNode *N = SU->Node; |
| 419 | return N->getOpcode() == ISD::CopyFromReg && |
| 420 | N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag; |
| 421 | } |
| 422 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 423 | namespace { |
| 424 | template<class SF> |
Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 425 | class VISIBILITY_HIDDEN RegReductionPriorityQueue |
| 426 | : public SchedulingPriorityQueue { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 427 | std::priority_queue<SUnit*, std::vector<SUnit*>, SF> Queue; |
| 428 | |
| 429 | public: |
| 430 | RegReductionPriorityQueue() : |
| 431 | Queue(SF(this)) {} |
| 432 | |
Chris Lattner | 0a30b1f | 2007-02-03 01:34:13 +0000 | [diff] [blame] | 433 | virtual void initNodes(DenseMap<SDNode*, SUnit*> &sumap, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 434 | std::vector<SUnit> &sunits) {} |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 435 | virtual void releaseState() {} |
| 436 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 437 | virtual unsigned getNodePriority(const SUnit *SU) const { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | bool empty() const { return Queue.empty(); } |
| 442 | |
| 443 | void push(SUnit *U) { |
| 444 | Queue.push(U); |
| 445 | } |
| 446 | void push_all(const std::vector<SUnit *> &Nodes) { |
| 447 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
| 448 | Queue.push(Nodes[i]); |
| 449 | } |
| 450 | |
| 451 | SUnit *pop() { |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 452 | if (empty()) return NULL; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 453 | SUnit *V = Queue.top(); |
| 454 | Queue.pop(); |
| 455 | return V; |
| 456 | } |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 457 | |
| 458 | virtual bool isDUOperand(const SUnit *SU1, const SUnit *SU2) { |
| 459 | return false; |
| 460 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | template<class SF> |
Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 464 | class VISIBILITY_HIDDEN BURegReductionPriorityQueue |
| 465 | : public RegReductionPriorityQueue<SF> { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 466 | // SUnitMap SDNode to SUnit mapping (n -> 1). |
Chris Lattner | 0a30b1f | 2007-02-03 01:34:13 +0000 | [diff] [blame] | 467 | DenseMap<SDNode*, SUnit*> *SUnitMap; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 468 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 469 | // SUnits - The SUnits for the current graph. |
| 470 | const std::vector<SUnit> *SUnits; |
| 471 | |
| 472 | // SethiUllmanNumbers - The SethiUllman number for each node. |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 473 | std::vector<unsigned> SethiUllmanNumbers; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 474 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 475 | const TargetInstrInfo *TII; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 476 | public: |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 477 | BURegReductionPriorityQueue(const TargetInstrInfo *tii) |
| 478 | : TII(tii) {} |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 0a30b1f | 2007-02-03 01:34:13 +0000 | [diff] [blame] | 480 | void initNodes(DenseMap<SDNode*, SUnit*> &sumap, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 481 | std::vector<SUnit> &sunits) { |
| 482 | SUnitMap = &sumap; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 483 | SUnits = &sunits; |
| 484 | // Add pseudo dependency edges for two-address nodes. |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 485 | AddPseudoTwoAddrDeps(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 486 | // Calculate node priorities. |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 487 | CalculateSethiUllmanNumbers(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void releaseState() { |
| 491 | SUnits = 0; |
| 492 | SethiUllmanNumbers.clear(); |
| 493 | } |
| 494 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 495 | unsigned getNodePriority(const SUnit *SU) const { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 496 | assert(SU->NodeNum < SethiUllmanNumbers.size()); |
| 497 | unsigned Opc = SU->Node->getOpcode(); |
| 498 | if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU)) |
| 499 | // CopyFromReg should be close to its def because it restricts |
| 500 | // allocation choices. But if it is a livein then perhaps we want it |
| 501 | // closer to its uses so it can be coalesced. |
| 502 | return 0xffff; |
| 503 | else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg) |
| 504 | // CopyToReg should be close to its uses to facilitate coalescing and |
| 505 | // avoid spilling. |
| 506 | return 0; |
| 507 | else if (SU->NumSuccs == 0) |
| 508 | // If SU does not have a use, i.e. it doesn't produce a value that would |
| 509 | // be consumed (e.g. store), then it terminates a chain of computation. |
| 510 | // Give it a large SethiUllman number so it will be scheduled right |
| 511 | // before its predecessors that it doesn't lengthen their live ranges. |
| 512 | return 0xffff; |
| 513 | else if (SU->NumPreds == 0) |
| 514 | // If SU does not have a def, schedule it close to its uses because it |
| 515 | // does not lengthen any live ranges. |
| 516 | return 0; |
| 517 | else |
| 518 | return SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 521 | bool isDUOperand(const SUnit *SU1, const SUnit *SU2) { |
| 522 | unsigned Opc = SU1->Node->getTargetOpcode(); |
| 523 | unsigned NumRes = ScheduleDAG::CountResults(SU1->Node); |
| 524 | unsigned NumOps = ScheduleDAG::CountOperands(SU1->Node); |
| 525 | for (unsigned i = 0; i != NumOps; ++i) { |
Evan Cheng | 67fc141 | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 526 | if (TII->getOperandConstraint(Opc, i+NumRes, TOI::TIED_TO) == -1) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 527 | continue; |
| 528 | if (SU1->Node->getOperand(i).isOperand(SU2->Node)) |
| 529 | return true; |
| 530 | } |
| 531 | return false; |
| 532 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 533 | private: |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 534 | bool canClobber(SUnit *SU, SUnit *Op); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 535 | void AddPseudoTwoAddrDeps(); |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 536 | void CalculateSethiUllmanNumbers(); |
| 537 | unsigned CalcNodeSethiUllmanNumber(const SUnit *SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
| 540 | |
| 541 | template<class SF> |
| 542 | class TDRegReductionPriorityQueue : public RegReductionPriorityQueue<SF> { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 543 | // SUnitMap SDNode to SUnit mapping (n -> 1). |
Chris Lattner | 0a30b1f | 2007-02-03 01:34:13 +0000 | [diff] [blame] | 544 | DenseMap<SDNode*, SUnit*> *SUnitMap; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 545 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 546 | // SUnits - The SUnits for the current graph. |
| 547 | const std::vector<SUnit> *SUnits; |
| 548 | |
| 549 | // SethiUllmanNumbers - The SethiUllman number for each node. |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 550 | std::vector<unsigned> SethiUllmanNumbers; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 551 | |
| 552 | public: |
| 553 | TDRegReductionPriorityQueue() {} |
| 554 | |
Chris Lattner | 0a30b1f | 2007-02-03 01:34:13 +0000 | [diff] [blame] | 555 | void initNodes(DenseMap<SDNode*, SUnit*> &sumap, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 556 | std::vector<SUnit> &sunits) { |
| 557 | SUnitMap = &sumap; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 558 | SUnits = &sunits; |
| 559 | // Calculate node priorities. |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 560 | CalculateSethiUllmanNumbers(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | void releaseState() { |
| 564 | SUnits = 0; |
| 565 | SethiUllmanNumbers.clear(); |
| 566 | } |
| 567 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 568 | unsigned getNodePriority(const SUnit *SU) const { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 569 | assert(SU->NodeNum < SethiUllmanNumbers.size()); |
| 570 | return SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | private: |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 574 | void CalculateSethiUllmanNumbers(); |
| 575 | unsigned CalcNodeSethiUllmanNumber(const SUnit *SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 576 | }; |
| 577 | } |
| 578 | |
| 579 | // Bottom up |
| 580 | bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 581 | bool LIsTarget = left->Node->isTargetOpcode(); |
| 582 | bool RIsTarget = right->Node->isTargetOpcode(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 583 | |
Evan Cheng | 99f2f79 | 2006-05-13 08:22:24 +0000 | [diff] [blame] | 584 | // Special tie breaker: if two nodes share a operand, the one that use it |
| 585 | // as a def&use operand is preferred. |
| 586 | if (LIsTarget && RIsTarget) { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 587 | if (left->isTwoAddress && !right->isTwoAddress) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 588 | if (SPQ->isDUOperand(left, right)) |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 589 | return false; |
| 590 | if (!left->isTwoAddress && right->isTwoAddress) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 591 | if (SPQ->isDUOperand(right, left)) |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 592 | return true; |
Evan Cheng | 99f2f79 | 2006-05-13 08:22:24 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 595 | unsigned LPriority = SPQ->getNodePriority(left); |
| 596 | unsigned RPriority = SPQ->getNodePriority(right); |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 597 | if (LPriority > RPriority) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 598 | return true; |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 599 | else if (LPriority == RPriority) |
Evan Cheng | 99f2f79 | 2006-05-13 08:22:24 +0000 | [diff] [blame] | 600 | if (left->Height > right->Height) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 601 | return true; |
Evan Cheng | 99f2f79 | 2006-05-13 08:22:24 +0000 | [diff] [blame] | 602 | else if (left->Height == right->Height) |
| 603 | if (left->Depth < right->Depth) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 604 | return true; |
Evan Cheng | 99f2f79 | 2006-05-13 08:22:24 +0000 | [diff] [blame] | 605 | else if (left->Depth == right->Depth) |
| 606 | if (left->CycleBound > right->CycleBound) |
| 607 | return true; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 608 | return false; |
| 609 | } |
| 610 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 611 | // FIXME: This is probably too slow! |
| 612 | static void isReachable(SUnit *SU, SUnit *TargetSU, |
| 613 | std::set<SUnit *> &Visited, bool &Reached) { |
| 614 | if (Reached) return; |
| 615 | if (SU == TargetSU) { |
| 616 | Reached = true; |
| 617 | return; |
| 618 | } |
| 619 | if (!Visited.insert(SU).second) return; |
| 620 | |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 621 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); I != E; |
| 622 | ++I) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 623 | isReachable(I->first, TargetSU, Visited, Reached); |
| 624 | } |
| 625 | |
| 626 | static bool isReachable(SUnit *SU, SUnit *TargetSU) { |
| 627 | std::set<SUnit *> Visited; |
| 628 | bool Reached = false; |
| 629 | isReachable(SU, TargetSU, Visited, Reached); |
| 630 | return Reached; |
| 631 | } |
| 632 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 633 | template<class SF> |
| 634 | bool BURegReductionPriorityQueue<SF>::canClobber(SUnit *SU, SUnit *Op) { |
| 635 | if (SU->isTwoAddress) { |
| 636 | unsigned Opc = SU->Node->getTargetOpcode(); |
| 637 | unsigned NumRes = ScheduleDAG::CountResults(SU->Node); |
| 638 | unsigned NumOps = ScheduleDAG::CountOperands(SU->Node); |
| 639 | for (unsigned i = 0; i != NumOps; ++i) { |
Evan Cheng | 67fc141 | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 640 | if (TII->getOperandConstraint(Opc, i+NumRes, TOI::TIED_TO) != -1) { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 641 | SDNode *DU = SU->Node->getOperand(i).Val; |
| 642 | if (Op == (*SUnitMap)[DU]) |
| 643 | return true; |
| 644 | } |
| 645 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 646 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 647 | return false; |
| 648 | } |
| 649 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 650 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 651 | /// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses |
| 652 | /// it as a def&use operand. Add a pseudo control edge from it to the other |
| 653 | /// node (if it won't create a cycle) so the two-address one will be scheduled |
| 654 | /// first (lower in the schedule). |
| 655 | template<class SF> |
| 656 | void BURegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 657 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) { |
| 658 | SUnit *SU = (SUnit *)&((*SUnits)[i]); |
| 659 | if (!SU->isTwoAddress) |
| 660 | continue; |
| 661 | |
| 662 | SDNode *Node = SU->Node; |
| 663 | if (!Node->isTargetOpcode()) |
| 664 | continue; |
| 665 | |
| 666 | unsigned Opc = Node->getTargetOpcode(); |
| 667 | unsigned NumRes = ScheduleDAG::CountResults(Node); |
| 668 | unsigned NumOps = ScheduleDAG::CountOperands(Node); |
| 669 | for (unsigned j = 0; j != NumOps; ++j) { |
Evan Cheng | 67fc141 | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 670 | if (TII->getOperandConstraint(Opc, j+NumRes, TOI::TIED_TO) != -1) { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 671 | SDNode *DU = SU->Node->getOperand(j).Val; |
| 672 | SUnit *DUSU = (*SUnitMap)[DU]; |
Evan Cheng | f24d15f | 2006-11-06 21:33:46 +0000 | [diff] [blame] | 673 | if (!DUSU) continue; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 674 | for (SUnit::succ_iterator I = DUSU->Succs.begin(),E = DUSU->Succs.end(); |
| 675 | I != E; ++I) { |
| 676 | if (I->second) continue; |
| 677 | SUnit *SuccSU = I->first; |
| 678 | if (SuccSU != SU && |
| 679 | (!canClobber(SuccSU, DUSU) || |
| 680 | (!SU->isCommutable && SuccSU->isCommutable))){ |
| 681 | if (SuccSU->Depth == SU->Depth && !isReachable(SuccSU, SU)) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 682 | DOUT << "Adding an edge from SU # " << SU->NodeNum |
| 683 | << " to SU #" << SuccSU->NodeNum << "\n"; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 684 | if (SU->addPred(SuccSU, true)) |
| 685 | SU->NumChainPredsLeft++; |
| 686 | if (SuccSU->addSucc(SU, true)) |
| 687 | SuccSU->NumChainSuccsLeft++; |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 696 | /// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 697 | /// Smaller number is the higher priority. |
| 698 | template<class SF> |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 699 | unsigned BURegReductionPriorityQueue<SF>:: |
| 700 | CalcNodeSethiUllmanNumber(const SUnit *SU) { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 701 | unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 702 | if (SethiUllmanNumber != 0) |
| 703 | return SethiUllmanNumber; |
| 704 | |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 705 | unsigned Extra = 0; |
| 706 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 707 | I != E; ++I) { |
| 708 | if (I->second) continue; // ignore chain preds |
| 709 | SUnit *PredSU = I->first; |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 710 | unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU); |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 711 | if (PredSethiUllman > SethiUllmanNumber) { |
| 712 | SethiUllmanNumber = PredSethiUllman; |
| 713 | Extra = 0; |
| 714 | } else if (PredSethiUllman == SethiUllmanNumber && !I->second) |
| 715 | Extra++; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 716 | } |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 717 | |
| 718 | SethiUllmanNumber += Extra; |
| 719 | |
| 720 | if (SethiUllmanNumber == 0) |
| 721 | SethiUllmanNumber = 1; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 722 | |
| 723 | return SethiUllmanNumber; |
| 724 | } |
| 725 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 726 | /// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all |
| 727 | /// scheduling units. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 728 | template<class SF> |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 729 | void BURegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 730 | SethiUllmanNumbers.assign(SUnits->size(), 0); |
| 731 | |
| 732 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 733 | CalcNodeSethiUllmanNumber(&(*SUnits)[i]); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static unsigned SumOfUnscheduledPredsOfSuccs(const SUnit *SU) { |
| 737 | unsigned Sum = 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 738 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 739 | I != E; ++I) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 740 | SUnit *SuccSU = I->first; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 741 | for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(), |
| 742 | EE = SuccSU->Preds.end(); II != EE; ++II) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 743 | SUnit *PredSU = II->first; |
| 744 | if (!PredSU->isScheduled) |
| 745 | Sum++; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | return Sum; |
| 750 | } |
| 751 | |
| 752 | |
| 753 | // Top down |
| 754 | bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const { |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 755 | unsigned LPriority = SPQ->getNodePriority(left); |
| 756 | unsigned RPriority = SPQ->getNodePriority(right); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 757 | bool LIsTarget = left->Node->isTargetOpcode(); |
| 758 | bool RIsTarget = right->Node->isTargetOpcode(); |
| 759 | bool LIsFloater = LIsTarget && left->NumPreds == 0; |
| 760 | bool RIsFloater = RIsTarget && right->NumPreds == 0; |
| 761 | unsigned LBonus = (SumOfUnscheduledPredsOfSuccs(left) == 1) ? 2 : 0; |
| 762 | unsigned RBonus = (SumOfUnscheduledPredsOfSuccs(right) == 1) ? 2 : 0; |
| 763 | |
| 764 | if (left->NumSuccs == 0 && right->NumSuccs != 0) |
| 765 | return false; |
| 766 | else if (left->NumSuccs != 0 && right->NumSuccs == 0) |
| 767 | return true; |
| 768 | |
| 769 | // Special tie breaker: if two nodes share a operand, the one that use it |
| 770 | // as a def&use operand is preferred. |
| 771 | if (LIsTarget && RIsTarget) { |
| 772 | if (left->isTwoAddress && !right->isTwoAddress) { |
| 773 | SDNode *DUNode = left->Node->getOperand(0).Val; |
| 774 | if (DUNode->isOperand(right->Node)) |
| 775 | RBonus += 2; |
| 776 | } |
| 777 | if (!left->isTwoAddress && right->isTwoAddress) { |
| 778 | SDNode *DUNode = right->Node->getOperand(0).Val; |
| 779 | if (DUNode->isOperand(left->Node)) |
| 780 | LBonus += 2; |
| 781 | } |
| 782 | } |
| 783 | if (LIsFloater) |
| 784 | LBonus -= 2; |
| 785 | if (RIsFloater) |
| 786 | RBonus -= 2; |
| 787 | if (left->NumSuccs == 1) |
| 788 | LBonus += 2; |
| 789 | if (right->NumSuccs == 1) |
| 790 | RBonus += 2; |
| 791 | |
| 792 | if (LPriority+LBonus < RPriority+RBonus) |
| 793 | return true; |
| 794 | else if (LPriority == RPriority) |
| 795 | if (left->Depth < right->Depth) |
| 796 | return true; |
| 797 | else if (left->Depth == right->Depth) |
| 798 | if (left->NumSuccsLeft > right->NumSuccsLeft) |
| 799 | return true; |
| 800 | else if (left->NumSuccsLeft == right->NumSuccsLeft) |
| 801 | if (left->CycleBound > right->CycleBound) |
| 802 | return true; |
| 803 | return false; |
| 804 | } |
| 805 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 806 | /// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 807 | /// Smaller number is the higher priority. |
| 808 | template<class SF> |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 809 | unsigned TDRegReductionPriorityQueue<SF>:: |
| 810 | CalcNodeSethiUllmanNumber(const SUnit *SU) { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 811 | unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 812 | if (SethiUllmanNumber != 0) |
| 813 | return SethiUllmanNumber; |
| 814 | |
| 815 | unsigned Opc = SU->Node->getOpcode(); |
| 816 | if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg) |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 817 | SethiUllmanNumber = 0xffff; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 818 | else if (SU->NumSuccsLeft == 0) |
| 819 | // If SU does not have a use, i.e. it doesn't produce a value that would |
| 820 | // be consumed (e.g. store), then it terminates a chain of computation. |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 821 | // Give it a small SethiUllman number so it will be scheduled right before |
| 822 | // its predecessors that it doesn't lengthen their live ranges. |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 823 | SethiUllmanNumber = 0; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 824 | else if (SU->NumPredsLeft == 0 && |
| 825 | (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU))) |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 826 | SethiUllmanNumber = 0xffff; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 827 | else { |
| 828 | int Extra = 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 829 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 830 | I != E; ++I) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 831 | if (I->second) continue; // ignore chain preds |
| 832 | SUnit *PredSU = I->first; |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 833 | unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 834 | if (PredSethiUllman > SethiUllmanNumber) { |
| 835 | SethiUllmanNumber = PredSethiUllman; |
| 836 | Extra = 0; |
| 837 | } else if (PredSethiUllman == SethiUllmanNumber && !I->second) |
| 838 | Extra++; |
| 839 | } |
| 840 | |
| 841 | SethiUllmanNumber += Extra; |
| 842 | } |
| 843 | |
| 844 | return SethiUllmanNumber; |
| 845 | } |
| 846 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 847 | /// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all |
| 848 | /// scheduling units. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 849 | template<class SF> |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 850 | void TDRegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 851 | SethiUllmanNumbers.assign(SUnits->size(), 0); |
| 852 | |
| 853 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 854 | CalcNodeSethiUllmanNumber(&(*SUnits)[i]); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | //===----------------------------------------------------------------------===// |
| 858 | // Public Constructor Functions |
| 859 | //===----------------------------------------------------------------------===// |
| 860 | |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 861 | llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, |
| 862 | SelectionDAG *DAG, |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 863 | MachineBasicBlock *BB) { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 864 | const TargetInstrInfo *TII = DAG->getTarget().getInstrInfo(); |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 865 | return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), true, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 866 | new BURegReductionPriorityQueue<bu_ls_rr_sort>(TII)); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 869 | llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, |
| 870 | SelectionDAG *DAG, |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 871 | MachineBasicBlock *BB) { |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 872 | return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), false, |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 873 | new TDRegReductionPriorityQueue<td_ls_rr_sort>()); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 874 | } |
| 875 | |