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 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 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 | |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "pre-RA-sched" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 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/Target/MRegisterInfo.h" |
Owen Anderson | 8c2c1e9 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetInstrInfo.h" |
| 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include <climits> |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 31 | #include <queue> |
| 32 | #include "llvm/Support/CommandLine.h" |
| 33 | using namespace llvm; |
| 34 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 35 | STATISTIC(NumBacktracks, "Number of times scheduler backtraced"); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 36 | STATISTIC(NumUnfolds, "Number of nodes unfolded"); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 37 | STATISTIC(NumDups, "Number of duplicated nodes"); |
| 38 | STATISTIC(NumCCCopies, "Number of cross class copies"); |
| 39 | |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 40 | static RegisterScheduler |
| 41 | burrListDAGScheduler("list-burr", |
| 42 | " Bottom-up register reduction list scheduling", |
| 43 | createBURRListDAGScheduler); |
| 44 | static RegisterScheduler |
| 45 | tdrListrDAGScheduler("list-tdrr", |
| 46 | " Top-down register reduction list scheduling", |
| 47 | createTDRRListDAGScheduler); |
| 48 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 49 | namespace { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | /// ScheduleDAGRRList - The actual register reduction list scheduler |
| 52 | /// implementation. This supports both top-down and bottom-up scheduling. |
| 53 | /// |
Chris Lattner | e097e6f | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 54 | class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAG { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 55 | private: |
| 56 | /// isBottomUp - This is true if the scheduling problem is bottom-up, false if |
| 57 | /// it is top-down. |
| 58 | bool isBottomUp; |
| 59 | |
| 60 | /// AvailableQueue - The priority queue to use for the available SUnits. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 61 | SchedulingPriorityQueue *AvailableQueue; |
| 62 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 63 | /// LiveRegs / LiveRegDefs - A set of physical registers and their definition |
| 64 | /// that are "live". These nodes must be scheduled before any other nodes that |
| 65 | /// modifies the registers can be scheduled. |
| 66 | SmallSet<unsigned, 4> LiveRegs; |
| 67 | std::vector<SUnit*> LiveRegDefs; |
| 68 | std::vector<unsigned> LiveRegCycles; |
| 69 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 70 | public: |
| 71 | ScheduleDAGRRList(SelectionDAG &dag, MachineBasicBlock *bb, |
| 72 | const TargetMachine &tm, bool isbottomup, |
| 73 | SchedulingPriorityQueue *availqueue) |
| 74 | : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup), |
| 75 | AvailableQueue(availqueue) { |
| 76 | } |
| 77 | |
| 78 | ~ScheduleDAGRRList() { |
| 79 | delete AvailableQueue; |
| 80 | } |
| 81 | |
| 82 | void Schedule(); |
| 83 | |
| 84 | private: |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 85 | void ReleasePred(SUnit*, bool, unsigned); |
| 86 | void ReleaseSucc(SUnit*, bool isChain, unsigned); |
| 87 | void CapturePred(SUnit*, SUnit*, bool); |
| 88 | void ScheduleNodeBottomUp(SUnit*, unsigned); |
| 89 | void ScheduleNodeTopDown(SUnit*, unsigned); |
| 90 | void UnscheduleNodeBottomUp(SUnit*); |
| 91 | void BacktrackBottomUp(SUnit*, unsigned, unsigned&); |
| 92 | SUnit *CopyAndMoveSuccessors(SUnit*); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 93 | void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned, |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 94 | const TargetRegisterClass*, |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 95 | const TargetRegisterClass*, |
| 96 | SmallVector<SUnit*, 2>&); |
| 97 | bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 98 | void ListScheduleTopDown(); |
| 99 | void ListScheduleBottomUp(); |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 100 | void CommuteNodesToReducePressure(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 101 | }; |
| 102 | } // end anonymous namespace |
| 103 | |
| 104 | |
| 105 | /// Schedule - Schedule the DAG using list scheduling. |
| 106 | void ScheduleDAGRRList::Schedule() { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 107 | DOUT << "********** List Scheduling **********\n"; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 108 | |
| 109 | LiveRegDefs.resize(MRI->getNumRegs(), NULL); |
| 110 | LiveRegCycles.resize(MRI->getNumRegs(), 0); |
| 111 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 112 | // Build scheduling units. |
| 113 | BuildSchedUnits(); |
| 114 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 115 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 116 | SUnits[su].dumpAll(&DAG)); |
Evan Cheng | 47fbeda | 2006-10-14 08:34:06 +0000 | [diff] [blame] | 117 | CalculateDepths(); |
| 118 | CalculateHeights(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 119 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 120 | AvailableQueue->initNodes(SUnitMap, SUnits); |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 121 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 122 | // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate. |
| 123 | if (isBottomUp) |
| 124 | ListScheduleBottomUp(); |
| 125 | else |
| 126 | ListScheduleTopDown(); |
| 127 | |
| 128 | AvailableQueue->releaseState(); |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 129 | |
Evan Cheng | 009f5f5 | 2006-05-25 08:37:31 +0000 | [diff] [blame] | 130 | CommuteNodesToReducePressure(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 131 | |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 132 | DOUT << "*** Final schedule ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 133 | DEBUG(dumpSchedule()); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 134 | DOUT << "\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 135 | |
| 136 | // Emit in scheduled order |
| 137 | EmitSchedule(); |
| 138 | } |
| 139 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 140 | /// CommuteNodesToReducePressure - If a node is two-address and commutable, and |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 141 | /// it is not the last use of its first operand, add it to the CommuteSet if |
| 142 | /// possible. It will be commuted when it is translated to a MI. |
| 143 | void ScheduleDAGRRList::CommuteNodesToReducePressure() { |
Evan Cheng | e3c4419 | 2007-06-22 01:35:51 +0000 | [diff] [blame] | 144 | SmallPtrSet<SUnit*, 4> OperandSeen; |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 145 | for (unsigned i = Sequence.size()-1; i != 0; --i) { // Ignore first node. |
| 146 | SUnit *SU = Sequence[i]; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 147 | if (!SU || !SU->Node) continue; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 148 | if (SU->isCommutable) { |
| 149 | unsigned Opc = SU->Node->getTargetOpcode(); |
Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 150 | const TargetInstrDesc &TID = TII->get(Opc); |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 151 | unsigned NumRes = TID.getNumDefs(); |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 152 | unsigned NumOps = CountOperands(SU->Node); |
| 153 | for (unsigned j = 0; j != NumOps; ++j) { |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 154 | if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 155 | continue; |
| 156 | |
| 157 | SDNode *OpN = SU->Node->getOperand(j).Val; |
Evan Cheng | 1bf16631 | 2007-11-09 01:27:11 +0000 | [diff] [blame] | 158 | SUnit *OpSU = isPassiveNode(OpN) ? NULL : SUnitMap[OpN][SU->InstanceNo]; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 159 | if (OpSU && OperandSeen.count(OpSU) == 1) { |
| 160 | // Ok, so SU is not the last use of OpSU, but SU is two-address so |
| 161 | // it will clobber OpSU. Try to commute SU if no other source operands |
| 162 | // are live below. |
| 163 | bool DoCommute = true; |
| 164 | for (unsigned k = 0; k < NumOps; ++k) { |
| 165 | if (k != j) { |
| 166 | OpN = SU->Node->getOperand(k).Val; |
Evan Cheng | 1bf16631 | 2007-11-09 01:27:11 +0000 | [diff] [blame] | 167 | OpSU = isPassiveNode(OpN) ? NULL : SUnitMap[OpN][SU->InstanceNo]; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 168 | if (OpSU && OperandSeen.count(OpSU) == 1) { |
| 169 | DoCommute = false; |
| 170 | break; |
| 171 | } |
| 172 | } |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 173 | } |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 174 | if (DoCommute) |
| 175 | CommuteSet.insert(SU->Node); |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 176 | } |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 177 | |
| 178 | // Only look at the first use&def node for now. |
| 179 | break; |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 183 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 184 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 185 | if (!I->isCtrl) |
| 186 | OperandSeen.insert(I->Dep); |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 190 | |
| 191 | //===----------------------------------------------------------------------===// |
| 192 | // Bottom-Up Scheduling |
| 193 | //===----------------------------------------------------------------------===// |
| 194 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 195 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 196 | /// the AvailableQueue if the count reaches zero. Also update its cycle bound. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 197 | void ScheduleDAGRRList::ReleasePred(SUnit *PredSU, bool isChain, |
| 198 | unsigned CurCycle) { |
| 199 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 200 | // latency. For example, the reader can very well read the register written |
| 201 | // by the predecessor later than the issue cycle. It also depends on the |
| 202 | // interrupt model (drain vs. freeze). |
| 203 | PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency); |
| 204 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 205 | --PredSU->NumSuccsLeft; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 206 | |
| 207 | #ifndef NDEBUG |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 208 | if (PredSU->NumSuccsLeft < 0) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 209 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 210 | PredSU->dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 211 | cerr << " has been released too many times!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 212 | assert(0); |
| 213 | } |
| 214 | #endif |
| 215 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 216 | if (PredSU->NumSuccsLeft == 0) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 217 | // EntryToken has to go last! Special case it here. |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 218 | if (!PredSU->Node || PredSU->Node->getOpcode() != ISD::EntryToken) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 219 | PredSU->isAvailable = true; |
| 220 | AvailableQueue->push(PredSU); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending |
| 226 | /// count of its predecessors. If a predecessor pending count is zero, add it to |
| 227 | /// the Available queue. |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 228 | void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 229 | DOUT << "*** Scheduling [" << CurCycle << "]: "; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 230 | DEBUG(SU->dump(&DAG)); |
| 231 | SU->Cycle = CurCycle; |
| 232 | |
| 233 | AvailableQueue->ScheduledNode(SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 234 | |
| 235 | // Bottom up: release predecessors |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 236 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 237 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 238 | ReleasePred(I->Dep, I->isCtrl, CurCycle); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 239 | if (I->Cost < 0) { |
| 240 | // This is a physical register dependency and it's impossible or |
| 241 | // expensive to copy the register. Make sure nothing that can |
| 242 | // clobber the register is scheduled between the predecessor and |
| 243 | // this node. |
| 244 | if (LiveRegs.insert(I->Reg)) { |
| 245 | LiveRegDefs[I->Reg] = I->Dep; |
| 246 | LiveRegCycles[I->Reg] = CurCycle; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Release all the implicit physical register defs that are live. |
| 252 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 253 | I != E; ++I) { |
| 254 | if (I->Cost < 0) { |
| 255 | if (LiveRegCycles[I->Reg] == I->Dep->Cycle) { |
| 256 | LiveRegs.erase(I->Reg); |
| 257 | assert(LiveRegDefs[I->Reg] == SU && |
| 258 | "Physical register dependency violated?"); |
| 259 | LiveRegDefs[I->Reg] = NULL; |
| 260 | LiveRegCycles[I->Reg] = 0; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 265 | SU->isScheduled = true; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 268 | /// CapturePred - This does the opposite of ReleasePred. Since SU is being |
| 269 | /// unscheduled, incrcease the succ left count of its predecessors. Remove |
| 270 | /// them from AvailableQueue if necessary. |
| 271 | void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) { |
| 272 | PredSU->CycleBound = 0; |
| 273 | for (SUnit::succ_iterator I = PredSU->Succs.begin(), E = PredSU->Succs.end(); |
| 274 | I != E; ++I) { |
| 275 | if (I->Dep == SU) |
| 276 | continue; |
| 277 | PredSU->CycleBound = std::max(PredSU->CycleBound, |
| 278 | I->Dep->Cycle + PredSU->Latency); |
| 279 | } |
| 280 | |
| 281 | if (PredSU->isAvailable) { |
| 282 | PredSU->isAvailable = false; |
| 283 | if (!PredSU->isPending) |
| 284 | AvailableQueue->remove(PredSU); |
| 285 | } |
| 286 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 287 | ++PredSU->NumSuccsLeft; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and |
| 291 | /// its predecessor states to reflect the change. |
| 292 | void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) { |
| 293 | DOUT << "*** Unscheduling [" << SU->Cycle << "]: "; |
| 294 | DEBUG(SU->dump(&DAG)); |
| 295 | |
| 296 | AvailableQueue->UnscheduledNode(SU); |
| 297 | |
| 298 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 299 | I != E; ++I) { |
| 300 | CapturePred(I->Dep, SU, I->isCtrl); |
| 301 | if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg]) { |
| 302 | LiveRegs.erase(I->Reg); |
| 303 | assert(LiveRegDefs[I->Reg] == I->Dep && |
| 304 | "Physical register dependency violated?"); |
| 305 | LiveRegDefs[I->Reg] = NULL; |
| 306 | LiveRegCycles[I->Reg] = 0; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 311 | I != E; ++I) { |
| 312 | if (I->Cost < 0) { |
| 313 | if (LiveRegs.insert(I->Reg)) { |
| 314 | assert(!LiveRegDefs[I->Reg] && |
| 315 | "Physical register dependency violated?"); |
| 316 | LiveRegDefs[I->Reg] = SU; |
| 317 | } |
| 318 | if (I->Dep->Cycle < LiveRegCycles[I->Reg]) |
| 319 | LiveRegCycles[I->Reg] = I->Dep->Cycle; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | SU->Cycle = 0; |
| 324 | SU->isScheduled = false; |
| 325 | SU->isAvailable = true; |
| 326 | AvailableQueue->push(SU); |
| 327 | } |
| 328 | |
Evan Cheng | cfd5f82 | 2007-09-27 00:25:29 +0000 | [diff] [blame] | 329 | // FIXME: This is probably too slow! |
| 330 | static void isReachable(SUnit *SU, SUnit *TargetSU, |
| 331 | SmallPtrSet<SUnit*, 32> &Visited, bool &Reached) { |
| 332 | if (Reached) return; |
| 333 | if (SU == TargetSU) { |
| 334 | Reached = true; |
| 335 | return; |
| 336 | } |
| 337 | if (!Visited.insert(SU)) return; |
| 338 | |
| 339 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); I != E; |
| 340 | ++I) |
| 341 | isReachable(I->Dep, TargetSU, Visited, Reached); |
| 342 | } |
| 343 | |
| 344 | static bool isReachable(SUnit *SU, SUnit *TargetSU) { |
| 345 | SmallPtrSet<SUnit*, 32> Visited; |
| 346 | bool Reached = false; |
| 347 | isReachable(SU, TargetSU, Visited, Reached); |
| 348 | return Reached; |
| 349 | } |
| 350 | |
| 351 | /// willCreateCycle - Returns true if adding an edge from SU to TargetSU will |
| 352 | /// create a cycle. |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 353 | static bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) { |
Evan Cheng | cfd5f82 | 2007-09-27 00:25:29 +0000 | [diff] [blame] | 354 | if (isReachable(TargetSU, SU)) |
| 355 | return true; |
| 356 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 357 | I != E; ++I) |
| 358 | if (I->Cost < 0 && isReachable(TargetSU, I->Dep)) |
| 359 | return true; |
| 360 | return false; |
| 361 | } |
| 362 | |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 363 | /// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 364 | /// BTCycle in order to schedule a specific node. Returns the last unscheduled |
| 365 | /// SUnit. Also returns if a successor is unscheduled in the process. |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 366 | void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle, |
| 367 | unsigned &CurCycle) { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 368 | SUnit *OldSU = NULL; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 369 | while (CurCycle > BtCycle) { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 370 | OldSU = Sequence.back(); |
| 371 | Sequence.pop_back(); |
| 372 | if (SU->isSucc(OldSU)) |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 373 | // Don't try to remove SU from AvailableQueue. |
| 374 | SU->isAvailable = false; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 375 | UnscheduleNodeBottomUp(OldSU); |
| 376 | --CurCycle; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | if (SU->isSucc(OldSU)) { |
| 381 | assert(false && "Something is wrong!"); |
| 382 | abort(); |
| 383 | } |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 384 | |
| 385 | ++NumBacktracks; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 388 | /// CopyAndMoveSuccessors - Clone the specified node and move its scheduled |
| 389 | /// successors to the newly created node. |
| 390 | SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) { |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 391 | if (SU->FlaggedNodes.size()) |
| 392 | return NULL; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 393 | |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 394 | SDNode *N = SU->Node; |
| 395 | if (!N) |
| 396 | return NULL; |
| 397 | |
| 398 | SUnit *NewSU; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 399 | bool TryUnfold = false; |
Evan Cheng | 84d0ebc | 2007-10-05 01:42:35 +0000 | [diff] [blame] | 400 | for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) { |
| 401 | MVT::ValueType VT = N->getValueType(i); |
| 402 | if (VT == MVT::Flag) |
| 403 | return NULL; |
| 404 | else if (VT == MVT::Other) |
| 405 | TryUnfold = true; |
| 406 | } |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 407 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 408 | const SDOperand &Op = N->getOperand(i); |
| 409 | MVT::ValueType VT = Op.Val->getValueType(Op.ResNo); |
| 410 | if (VT == MVT::Flag) |
| 411 | return NULL; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | if (TryUnfold) { |
| 415 | SmallVector<SDNode*, 4> NewNodes; |
Owen Anderson | 0ec92e9 | 2008-01-07 01:35:56 +0000 | [diff] [blame] | 416 | if (!TII->unfoldMemoryOperand(DAG, N, NewNodes)) |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 417 | return NULL; |
| 418 | |
| 419 | DOUT << "Unfolding SU # " << SU->NodeNum << "\n"; |
| 420 | assert(NewNodes.size() == 2 && "Expected a load folding node!"); |
| 421 | |
| 422 | N = NewNodes[1]; |
| 423 | SDNode *LoadNode = NewNodes[0]; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 424 | unsigned NumVals = N->getNumValues(); |
| 425 | unsigned OldNumVals = SU->Node->getNumValues(); |
| 426 | for (unsigned i = 0; i != NumVals; ++i) |
Chris Lattner | 3cfb56d | 2007-10-15 06:10:22 +0000 | [diff] [blame] | 427 | DAG.ReplaceAllUsesOfValueWith(SDOperand(SU->Node, i), SDOperand(N, i)); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 428 | DAG.ReplaceAllUsesOfValueWith(SDOperand(SU->Node, OldNumVals-1), |
Chris Lattner | 3cfb56d | 2007-10-15 06:10:22 +0000 | [diff] [blame] | 429 | SDOperand(LoadNode, 1)); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 430 | |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 431 | SUnit *NewSU = NewSUnit(N); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 432 | SUnitMap[N].push_back(NewSU); |
Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 433 | const TargetInstrDesc &TID = TII->get(N->getTargetOpcode()); |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 434 | for (unsigned i = 0; i != TID.getNumOperands(); ++i) { |
| 435 | if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) { |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 436 | NewSU->isTwoAddress = true; |
| 437 | break; |
| 438 | } |
| 439 | } |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 440 | if (TID.isCommutable()) |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 441 | NewSU->isCommutable = true; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 442 | // FIXME: Calculate height / depth and propagate the changes? |
Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 443 | NewSU->Depth = SU->Depth; |
| 444 | NewSU->Height = SU->Height; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 445 | ComputeLatency(NewSU); |
| 446 | |
Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 447 | // LoadNode may already exist. This can happen when there is another |
| 448 | // load from the same location and producing the same type of value |
| 449 | // but it has different alignment or volatileness. |
| 450 | bool isNewLoad = true; |
| 451 | SUnit *LoadSU; |
| 452 | DenseMap<SDNode*, std::vector<SUnit*> >::iterator SMI = |
| 453 | SUnitMap.find(LoadNode); |
| 454 | if (SMI != SUnitMap.end()) { |
| 455 | LoadSU = SMI->second.front(); |
| 456 | isNewLoad = false; |
| 457 | } else { |
| 458 | LoadSU = NewSUnit(LoadNode); |
| 459 | SUnitMap[LoadNode].push_back(LoadSU); |
| 460 | |
| 461 | LoadSU->Depth = SU->Depth; |
| 462 | LoadSU->Height = SU->Height; |
| 463 | ComputeLatency(LoadSU); |
| 464 | } |
| 465 | |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 466 | SUnit *ChainPred = NULL; |
| 467 | SmallVector<SDep, 4> ChainSuccs; |
| 468 | SmallVector<SDep, 4> LoadPreds; |
| 469 | SmallVector<SDep, 4> NodePreds; |
| 470 | SmallVector<SDep, 4> NodeSuccs; |
| 471 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 472 | I != E; ++I) { |
| 473 | if (I->isCtrl) |
| 474 | ChainPred = I->Dep; |
| 475 | else if (I->Dep->Node && I->Dep->Node->isOperand(LoadNode)) |
| 476 | LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false)); |
| 477 | else |
| 478 | NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false)); |
| 479 | } |
| 480 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 481 | I != E; ++I) { |
| 482 | if (I->isCtrl) |
| 483 | ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost, |
| 484 | I->isCtrl, I->isSpecial)); |
| 485 | else |
| 486 | NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost, |
| 487 | I->isCtrl, I->isSpecial)); |
| 488 | } |
| 489 | |
| 490 | SU->removePred(ChainPred, true, false); |
Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 491 | if (isNewLoad) |
| 492 | LoadSU->addPred(ChainPred, true, false); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 493 | for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) { |
| 494 | SDep *Pred = &LoadPreds[i]; |
| 495 | SU->removePred(Pred->Dep, Pred->isCtrl, Pred->isSpecial); |
Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 496 | if (isNewLoad) |
| 497 | LoadSU->addPred(Pred->Dep, Pred->isCtrl, Pred->isSpecial, |
| 498 | Pred->Reg, Pred->Cost); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 499 | } |
| 500 | for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) { |
| 501 | SDep *Pred = &NodePreds[i]; |
| 502 | SU->removePred(Pred->Dep, Pred->isCtrl, Pred->isSpecial); |
| 503 | NewSU->addPred(Pred->Dep, Pred->isCtrl, Pred->isSpecial, |
| 504 | Pred->Reg, Pred->Cost); |
| 505 | } |
| 506 | for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) { |
| 507 | SDep *Succ = &NodeSuccs[i]; |
| 508 | Succ->Dep->removePred(SU, Succ->isCtrl, Succ->isSpecial); |
| 509 | Succ->Dep->addPred(NewSU, Succ->isCtrl, Succ->isSpecial, |
| 510 | Succ->Reg, Succ->Cost); |
| 511 | } |
| 512 | for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) { |
| 513 | SDep *Succ = &ChainSuccs[i]; |
| 514 | Succ->Dep->removePred(SU, Succ->isCtrl, Succ->isSpecial); |
Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 515 | if (isNewLoad) |
| 516 | Succ->Dep->addPred(LoadSU, Succ->isCtrl, Succ->isSpecial, |
| 517 | Succ->Reg, Succ->Cost); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 518 | } |
Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 519 | if (isNewLoad) |
| 520 | NewSU->addPred(LoadSU, false, false); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 521 | |
Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 522 | if (isNewLoad) |
| 523 | AvailableQueue->addNode(LoadSU); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 524 | AvailableQueue->addNode(NewSU); |
| 525 | |
| 526 | ++NumUnfolds; |
| 527 | |
| 528 | if (NewSU->NumSuccsLeft == 0) { |
| 529 | NewSU->isAvailable = true; |
| 530 | return NewSU; |
Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 531 | } |
| 532 | SU = NewSU; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | DOUT << "Duplicating SU # " << SU->NodeNum << "\n"; |
| 536 | NewSU = Clone(SU); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 537 | |
| 538 | // New SUnit has the exact same predecessors. |
| 539 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 540 | I != E; ++I) |
| 541 | if (!I->isSpecial) { |
| 542 | NewSU->addPred(I->Dep, I->isCtrl, false, I->Reg, I->Cost); |
| 543 | NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1); |
| 544 | } |
| 545 | |
| 546 | // Only copy scheduled successors. Cut them from old node's successor |
| 547 | // list and move them over. |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 548 | SmallVector<std::pair<SUnit*, bool>, 4> DelDeps; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 549 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 550 | I != E; ++I) { |
| 551 | if (I->isSpecial) |
| 552 | continue; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 553 | if (I->Dep->isScheduled) { |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 554 | NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 555 | I->Dep->addPred(NewSU, I->isCtrl, false, I->Reg, I->Cost); |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 556 | DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl)); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 560 | SUnit *Succ = DelDeps[i].first; |
| 561 | bool isCtrl = DelDeps[i].second; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 562 | Succ->removePred(SU, isCtrl, false); |
| 563 | } |
| 564 | |
| 565 | AvailableQueue->updateNode(SU); |
| 566 | AvailableQueue->addNode(NewSU); |
| 567 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 568 | ++NumDups; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 569 | return NewSU; |
| 570 | } |
| 571 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 572 | /// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies |
| 573 | /// and move all scheduled successors of the given SUnit to the last copy. |
| 574 | void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg, |
| 575 | const TargetRegisterClass *DestRC, |
| 576 | const TargetRegisterClass *SrcRC, |
| 577 | SmallVector<SUnit*, 2> &Copies) { |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 578 | SUnit *CopyFromSU = NewSUnit(NULL); |
| 579 | CopyFromSU->CopySrcRC = SrcRC; |
| 580 | CopyFromSU->CopyDstRC = DestRC; |
| 581 | CopyFromSU->Depth = SU->Depth; |
| 582 | CopyFromSU->Height = SU->Height; |
| 583 | |
| 584 | SUnit *CopyToSU = NewSUnit(NULL); |
| 585 | CopyToSU->CopySrcRC = DestRC; |
| 586 | CopyToSU->CopyDstRC = SrcRC; |
| 587 | |
| 588 | // Only copy scheduled successors. Cut them from old node's successor |
| 589 | // list and move them over. |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 590 | SmallVector<std::pair<SUnit*, bool>, 4> DelDeps; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 591 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 592 | I != E; ++I) { |
| 593 | if (I->isSpecial) |
| 594 | continue; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 595 | if (I->Dep->isScheduled) { |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 596 | CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 597 | I->Dep->addPred(CopyToSU, I->isCtrl, false, I->Reg, I->Cost); |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 598 | DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl)); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 602 | SUnit *Succ = DelDeps[i].first; |
| 603 | bool isCtrl = DelDeps[i].second; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 604 | Succ->removePred(SU, isCtrl, false); |
| 605 | } |
| 606 | |
| 607 | CopyFromSU->addPred(SU, false, false, Reg, -1); |
| 608 | CopyToSU->addPred(CopyFromSU, false, false, Reg, 1); |
| 609 | |
| 610 | AvailableQueue->updateNode(SU); |
| 611 | AvailableQueue->addNode(CopyFromSU); |
| 612 | AvailableQueue->addNode(CopyToSU); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 613 | Copies.push_back(CopyFromSU); |
| 614 | Copies.push_back(CopyToSU); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 615 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 616 | ++NumCCCopies; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /// getPhysicalRegisterVT - Returns the ValueType of the physical register |
| 620 | /// definition of the specified node. |
| 621 | /// FIXME: Move to SelectionDAG? |
| 622 | static MVT::ValueType getPhysicalRegisterVT(SDNode *N, unsigned Reg, |
| 623 | const TargetInstrInfo *TII) { |
Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 624 | const TargetInstrDesc &TID = TII->get(N->getTargetOpcode()); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 625 | assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!"); |
Chris Lattner | b0d06b4 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 626 | unsigned NumRes = TID.getNumDefs(); |
| 627 | for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) { |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 628 | if (Reg == *ImpDef) |
| 629 | break; |
| 630 | ++NumRes; |
| 631 | } |
| 632 | return N->getValueType(NumRes); |
| 633 | } |
| 634 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 635 | /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay |
| 636 | /// scheduling of the given node to satisfy live physical register dependencies. |
| 637 | /// If the specific node is the last one that's available to schedule, do |
| 638 | /// whatever is necessary (i.e. backtracking or cloning) to make it possible. |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 639 | bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU, |
| 640 | SmallVector<unsigned, 4> &LRegs){ |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 641 | if (LiveRegs.empty()) |
| 642 | return false; |
| 643 | |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 644 | SmallSet<unsigned, 4> RegAdded; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 645 | // If this node would clobber any "live" register, then it's not ready. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 646 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 647 | I != E; ++I) { |
| 648 | if (I->Cost < 0) { |
| 649 | unsigned Reg = I->Reg; |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 650 | if (LiveRegs.count(Reg) && LiveRegDefs[Reg] != I->Dep) { |
| 651 | if (RegAdded.insert(Reg)) |
| 652 | LRegs.push_back(Reg); |
| 653 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 654 | for (const unsigned *Alias = MRI->getAliasSet(Reg); |
| 655 | *Alias; ++Alias) |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 656 | if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != I->Dep) { |
| 657 | if (RegAdded.insert(*Alias)) |
| 658 | LRegs.push_back(*Alias); |
| 659 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
| 663 | for (unsigned i = 0, e = SU->FlaggedNodes.size()+1; i != e; ++i) { |
| 664 | SDNode *Node = (i == 0) ? SU->Node : SU->FlaggedNodes[i-1]; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 665 | if (!Node || !Node->isTargetOpcode()) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 666 | continue; |
Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 667 | const TargetInstrDesc &TID = TII->get(Node->getTargetOpcode()); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 668 | if (!TID.ImplicitDefs) |
| 669 | continue; |
| 670 | for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) { |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 671 | if (LiveRegs.count(*Reg) && LiveRegDefs[*Reg] != SU) { |
| 672 | if (RegAdded.insert(*Reg)) |
| 673 | LRegs.push_back(*Reg); |
| 674 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 675 | for (const unsigned *Alias = MRI->getAliasSet(*Reg); |
| 676 | *Alias; ++Alias) |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 677 | if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != SU) { |
| 678 | if (RegAdded.insert(*Alias)) |
| 679 | LRegs.push_back(*Alias); |
| 680 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 683 | return !LRegs.empty(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 686 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 687 | /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up |
| 688 | /// schedulers. |
| 689 | void ScheduleDAGRRList::ListScheduleBottomUp() { |
| 690 | unsigned CurCycle = 0; |
| 691 | // Add root to Available queue. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 692 | SUnit *RootSU = SUnitMap[DAG.getRoot().Val].front(); |
| 693 | RootSU->isAvailable = true; |
| 694 | AvailableQueue->push(RootSU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 695 | |
| 696 | // While Available queue is not empty, grab the node with the highest |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 697 | // priority. If it is not ready put it back. Schedule the node. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 698 | SmallVector<SUnit*, 4> NotReady; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 699 | while (!AvailableQueue->empty()) { |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 700 | bool Delayed = false; |
| 701 | DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 702 | SUnit *CurSU = AvailableQueue->pop(); |
| 703 | while (CurSU) { |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 704 | if (CurSU->CycleBound <= CurCycle) { |
| 705 | SmallVector<unsigned, 4> LRegs; |
| 706 | if (!DelayForLiveRegsBottomUp(CurSU, LRegs)) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 707 | break; |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 708 | Delayed = true; |
| 709 | LRegsMap.insert(std::make_pair(CurSU, LRegs)); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 710 | } |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 711 | |
| 712 | CurSU->isPending = true; // This SU is not in AvailableQueue right now. |
| 713 | NotReady.push_back(CurSU); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 714 | CurSU = AvailableQueue->pop(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 715 | } |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 716 | |
| 717 | // All candidates are delayed due to live physical reg dependencies. |
| 718 | // Try backtracking, code duplication, or inserting cross class copies |
| 719 | // to resolve it. |
| 720 | if (Delayed && !CurSU) { |
| 721 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { |
| 722 | SUnit *TrySU = NotReady[i]; |
| 723 | SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU]; |
| 724 | |
| 725 | // Try unscheduling up to the point where it's safe to schedule |
| 726 | // this node. |
| 727 | unsigned LiveCycle = CurCycle; |
| 728 | for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) { |
| 729 | unsigned Reg = LRegs[j]; |
| 730 | unsigned LCycle = LiveRegCycles[Reg]; |
| 731 | LiveCycle = std::min(LiveCycle, LCycle); |
| 732 | } |
| 733 | SUnit *OldSU = Sequence[LiveCycle]; |
| 734 | if (!WillCreateCycle(TrySU, OldSU)) { |
| 735 | BacktrackBottomUp(TrySU, LiveCycle, CurCycle); |
| 736 | // Force the current node to be scheduled before the node that |
| 737 | // requires the physical reg dep. |
| 738 | if (OldSU->isAvailable) { |
| 739 | OldSU->isAvailable = false; |
| 740 | AvailableQueue->remove(OldSU); |
| 741 | } |
| 742 | TrySU->addPred(OldSU, true, true); |
| 743 | // If one or more successors has been unscheduled, then the current |
| 744 | // node is no longer avaialable. Schedule a successor that's now |
| 745 | // available instead. |
| 746 | if (!TrySU->isAvailable) |
| 747 | CurSU = AvailableQueue->pop(); |
| 748 | else { |
| 749 | CurSU = TrySU; |
| 750 | TrySU->isPending = false; |
| 751 | NotReady.erase(NotReady.begin()+i); |
| 752 | } |
| 753 | break; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (!CurSU) { |
| 758 | // Can't backtrace. Try duplicating the nodes that produces these |
| 759 | // "expensive to copy" values to break the dependency. In case even |
| 760 | // that doesn't work, insert cross class copies. |
| 761 | SUnit *TrySU = NotReady[0]; |
| 762 | SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU]; |
| 763 | assert(LRegs.size() == 1 && "Can't handle this yet!"); |
| 764 | unsigned Reg = LRegs[0]; |
| 765 | SUnit *LRDef = LiveRegDefs[Reg]; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 766 | SUnit *NewDef = CopyAndMoveSuccessors(LRDef); |
| 767 | if (!NewDef) { |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 768 | // Issue expensive cross register class copies. |
| 769 | MVT::ValueType VT = getPhysicalRegisterVT(LRDef->Node, Reg, TII); |
| 770 | const TargetRegisterClass *RC = |
| 771 | MRI->getPhysicalRegisterRegClass(VT, Reg); |
| 772 | const TargetRegisterClass *DestRC = MRI->getCrossCopyRegClass(RC); |
| 773 | if (!DestRC) { |
| 774 | assert(false && "Don't know how to copy this physical register!"); |
| 775 | abort(); |
| 776 | } |
| 777 | SmallVector<SUnit*, 2> Copies; |
| 778 | InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies); |
| 779 | DOUT << "Adding an edge from SU # " << TrySU->NodeNum |
| 780 | << " to SU #" << Copies.front()->NodeNum << "\n"; |
| 781 | TrySU->addPred(Copies.front(), true, true); |
| 782 | NewDef = Copies.back(); |
| 783 | } |
| 784 | |
| 785 | DOUT << "Adding an edge from SU # " << NewDef->NodeNum |
| 786 | << " to SU #" << TrySU->NodeNum << "\n"; |
| 787 | LiveRegDefs[Reg] = NewDef; |
| 788 | NewDef->addPred(TrySU, true, true); |
| 789 | TrySU->isAvailable = false; |
| 790 | CurSU = NewDef; |
| 791 | } |
| 792 | |
| 793 | if (!CurSU) { |
| 794 | assert(false && "Unable to resolve live physical register dependencies!"); |
| 795 | abort(); |
| 796 | } |
| 797 | } |
| 798 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 799 | // Add the nodes that aren't ready back onto the available list. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 800 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { |
| 801 | NotReady[i]->isPending = false; |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 802 | // May no longer be available due to backtracking. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 803 | if (NotReady[i]->isAvailable) |
| 804 | AvailableQueue->push(NotReady[i]); |
| 805 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 806 | NotReady.clear(); |
| 807 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 808 | if (!CurSU) |
| 809 | Sequence.push_back(0); |
| 810 | else { |
| 811 | ScheduleNodeBottomUp(CurSU, CurCycle); |
| 812 | Sequence.push_back(CurSU); |
| 813 | } |
| 814 | ++CurCycle; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | // Add entry node last |
| 818 | if (DAG.getEntryNode().Val != DAG.getRoot().Val) { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 819 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val].front(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 820 | Sequence.push_back(Entry); |
| 821 | } |
| 822 | |
| 823 | // Reverse the order if it is bottom up. |
| 824 | std::reverse(Sequence.begin(), Sequence.end()); |
| 825 | |
| 826 | |
| 827 | #ifndef NDEBUG |
| 828 | // Verify that all SUnits were scheduled. |
| 829 | bool AnyNotSched = false; |
| 830 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 831 | if (SUnits[i].NumSuccsLeft != 0) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 832 | if (!AnyNotSched) |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 833 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 834 | SUnits[i].dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 835 | cerr << "has not been scheduled!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 836 | AnyNotSched = true; |
| 837 | } |
| 838 | } |
| 839 | assert(!AnyNotSched); |
| 840 | #endif |
| 841 | } |
| 842 | |
| 843 | //===----------------------------------------------------------------------===// |
| 844 | // Top-Down Scheduling |
| 845 | //===----------------------------------------------------------------------===// |
| 846 | |
| 847 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 848 | /// the AvailableQueue if the count reaches zero. Also update its cycle bound. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 849 | void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain, |
| 850 | unsigned CurCycle) { |
| 851 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 852 | // latency. For example, the reader can very well read the register written |
| 853 | // by the predecessor later than the issue cycle. It also depends on the |
| 854 | // interrupt model (drain vs. freeze). |
| 855 | SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency); |
| 856 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 857 | --SuccSU->NumPredsLeft; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 858 | |
| 859 | #ifndef NDEBUG |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 860 | if (SuccSU->NumPredsLeft < 0) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 861 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 862 | SuccSU->dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 863 | cerr << " has been released too many times!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 864 | assert(0); |
| 865 | } |
| 866 | #endif |
| 867 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 868 | if (SuccSU->NumPredsLeft == 0) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 869 | SuccSU->isAvailable = true; |
| 870 | AvailableQueue->push(SuccSU); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | |
| 875 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 876 | /// count of its successors. If a successor pending count is zero, add it to |
| 877 | /// the Available queue. |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 878 | void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 879 | DOUT << "*** Scheduling [" << CurCycle << "]: "; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 880 | DEBUG(SU->dump(&DAG)); |
| 881 | SU->Cycle = CurCycle; |
| 882 | |
| 883 | AvailableQueue->ScheduledNode(SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 884 | |
| 885 | // Top down: release successors |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 886 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 887 | I != E; ++I) |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 888 | ReleaseSucc(I->Dep, I->isCtrl, CurCycle); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 889 | SU->isScheduled = true; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 892 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 893 | /// schedulers. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 894 | void ScheduleDAGRRList::ListScheduleTopDown() { |
| 895 | unsigned CurCycle = 0; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 896 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val].front(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 897 | |
| 898 | // All leaves to Available queue. |
| 899 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 900 | // It is available if it has no predecessors. |
Dan Gohman | 70de4cb | 2008-01-29 13:02:09 +0000 | [diff] [blame^] | 901 | if (SUnits[i].Preds.empty() && &SUnits[i] != Entry) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 902 | AvailableQueue->push(&SUnits[i]); |
| 903 | SUnits[i].isAvailable = true; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | // Emit the entry node first. |
| 908 | ScheduleNodeTopDown(Entry, CurCycle); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 909 | Sequence.push_back(Entry); |
| 910 | ++CurCycle; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 911 | |
| 912 | // While Available queue is not empty, grab the node with the highest |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 913 | // priority. If it is not ready put it back. Schedule the node. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 914 | std::vector<SUnit*> NotReady; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 915 | while (!AvailableQueue->empty()) { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 916 | SUnit *CurSU = AvailableQueue->pop(); |
| 917 | while (CurSU && CurSU->CycleBound > CurCycle) { |
| 918 | NotReady.push_back(CurSU); |
| 919 | CurSU = AvailableQueue->pop(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | // Add the nodes that aren't ready back onto the available list. |
| 923 | AvailableQueue->push_all(NotReady); |
| 924 | NotReady.clear(); |
| 925 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 926 | if (!CurSU) |
| 927 | Sequence.push_back(0); |
| 928 | else { |
| 929 | ScheduleNodeTopDown(CurSU, CurCycle); |
| 930 | Sequence.push_back(CurSU); |
| 931 | } |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 932 | CurCycle++; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | |
| 936 | #ifndef NDEBUG |
| 937 | // Verify that all SUnits were scheduled. |
| 938 | bool AnyNotSched = false; |
| 939 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 940 | if (!SUnits[i].isScheduled) { |
| 941 | if (!AnyNotSched) |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 942 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 943 | SUnits[i].dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 944 | cerr << "has not been scheduled!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 945 | AnyNotSched = true; |
| 946 | } |
| 947 | } |
| 948 | assert(!AnyNotSched); |
| 949 | #endif |
| 950 | } |
| 951 | |
| 952 | |
| 953 | |
| 954 | //===----------------------------------------------------------------------===// |
| 955 | // RegReductionPriorityQueue Implementation |
| 956 | //===----------------------------------------------------------------------===// |
| 957 | // |
| 958 | // This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers |
| 959 | // to reduce register pressure. |
| 960 | // |
| 961 | namespace { |
| 962 | template<class SF> |
| 963 | class RegReductionPriorityQueue; |
| 964 | |
| 965 | /// Sorting functions for the Available queue. |
| 966 | struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 967 | RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ; |
| 968 | bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {} |
| 969 | bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} |
| 970 | |
| 971 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 972 | }; |
| 973 | |
| 974 | struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 975 | RegReductionPriorityQueue<td_ls_rr_sort> *SPQ; |
| 976 | td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {} |
| 977 | td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} |
| 978 | |
| 979 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 980 | }; |
| 981 | } // end anonymous namespace |
| 982 | |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 983 | static inline bool isCopyFromLiveIn(const SUnit *SU) { |
| 984 | SDNode *N = SU->Node; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 985 | return N && N->getOpcode() == ISD::CopyFromReg && |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 986 | N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag; |
| 987 | } |
| 988 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 989 | namespace { |
| 990 | template<class SF> |
Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 991 | class VISIBILITY_HIDDEN RegReductionPriorityQueue |
| 992 | : public SchedulingPriorityQueue { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 993 | std::priority_queue<SUnit*, std::vector<SUnit*>, SF> Queue; |
| 994 | |
| 995 | public: |
| 996 | RegReductionPriorityQueue() : |
| 997 | Queue(SF(this)) {} |
| 998 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 999 | virtual void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1000 | std::vector<SUnit> &sunits) {} |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1001 | |
| 1002 | virtual void addNode(const SUnit *SU) {} |
| 1003 | |
| 1004 | virtual void updateNode(const SUnit *SU) {} |
| 1005 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1006 | virtual void releaseState() {} |
| 1007 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1008 | virtual unsigned getNodePriority(const SUnit *SU) const { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
| 1011 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1012 | unsigned size() const { return Queue.size(); } |
| 1013 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1014 | bool empty() const { return Queue.empty(); } |
| 1015 | |
| 1016 | void push(SUnit *U) { |
| 1017 | Queue.push(U); |
| 1018 | } |
| 1019 | void push_all(const std::vector<SUnit *> &Nodes) { |
| 1020 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
| 1021 | Queue.push(Nodes[i]); |
| 1022 | } |
| 1023 | |
| 1024 | SUnit *pop() { |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 1025 | if (empty()) return NULL; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1026 | SUnit *V = Queue.top(); |
| 1027 | Queue.pop(); |
| 1028 | return V; |
| 1029 | } |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1030 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1031 | /// remove - This is a really inefficient way to remove a node from a |
| 1032 | /// priority queue. We should roll our own heap to make this better or |
| 1033 | /// something. |
| 1034 | void remove(SUnit *SU) { |
| 1035 | std::vector<SUnit*> Temp; |
| 1036 | |
| 1037 | assert(!Queue.empty() && "Not in queue!"); |
| 1038 | while (Queue.top() != SU) { |
| 1039 | Temp.push_back(Queue.top()); |
| 1040 | Queue.pop(); |
| 1041 | assert(!Queue.empty() && "Not in queue!"); |
| 1042 | } |
| 1043 | |
| 1044 | // Remove the node from the PQ. |
| 1045 | Queue.pop(); |
| 1046 | |
| 1047 | // Add all the other nodes back. |
| 1048 | for (unsigned i = 0, e = Temp.size(); i != e; ++i) |
| 1049 | Queue.push(Temp[i]); |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1050 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1051 | }; |
| 1052 | |
| 1053 | template<class SF> |
Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 1054 | class VISIBILITY_HIDDEN BURegReductionPriorityQueue |
| 1055 | : public RegReductionPriorityQueue<SF> { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1056 | // SUnitMap SDNode to SUnit mapping (n -> n). |
| 1057 | DenseMap<SDNode*, std::vector<SUnit*> > *SUnitMap; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1058 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1059 | // SUnits - The SUnits for the current graph. |
| 1060 | const std::vector<SUnit> *SUnits; |
| 1061 | |
| 1062 | // SethiUllmanNumbers - The SethiUllman number for each node. |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1063 | std::vector<unsigned> SethiUllmanNumbers; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1064 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1065 | const TargetInstrInfo *TII; |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1066 | const MRegisterInfo *MRI; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1067 | public: |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1068 | explicit BURegReductionPriorityQueue(const TargetInstrInfo *tii, |
| 1069 | const MRegisterInfo *mri) |
| 1070 | : TII(tii), MRI(mri) {} |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1071 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1072 | void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1073 | std::vector<SUnit> &sunits) { |
| 1074 | SUnitMap = &sumap; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1075 | SUnits = &sunits; |
| 1076 | // Add pseudo dependency edges for two-address nodes. |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 1077 | AddPseudoTwoAddrDeps(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1078 | // Calculate node priorities. |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1079 | CalculateSethiUllmanNumbers(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1082 | void addNode(const SUnit *SU) { |
| 1083 | SethiUllmanNumbers.resize(SUnits->size(), 0); |
| 1084 | CalcNodeSethiUllmanNumber(SU); |
| 1085 | } |
| 1086 | |
| 1087 | void updateNode(const SUnit *SU) { |
| 1088 | SethiUllmanNumbers[SU->NodeNum] = 0; |
| 1089 | CalcNodeSethiUllmanNumber(SU); |
| 1090 | } |
| 1091 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1092 | void releaseState() { |
| 1093 | SUnits = 0; |
| 1094 | SethiUllmanNumbers.clear(); |
| 1095 | } |
| 1096 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1097 | unsigned getNodePriority(const SUnit *SU) const { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1098 | assert(SU->NodeNum < SethiUllmanNumbers.size()); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 1099 | unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0; |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1100 | if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU)) |
| 1101 | // CopyFromReg should be close to its def because it restricts |
| 1102 | // allocation choices. But if it is a livein then perhaps we want it |
| 1103 | // closer to its uses so it can be coalesced. |
| 1104 | return 0xffff; |
| 1105 | else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg) |
| 1106 | // CopyToReg should be close to its uses to facilitate coalescing and |
| 1107 | // avoid spilling. |
| 1108 | return 0; |
Evan Cheng | aa2d6ef | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1109 | else if (Opc == TargetInstrInfo::EXTRACT_SUBREG || |
| 1110 | Opc == TargetInstrInfo::INSERT_SUBREG) |
| 1111 | // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to |
| 1112 | // facilitate coalescing. |
| 1113 | return 0; |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1114 | else if (SU->NumSuccs == 0) |
| 1115 | // If SU does not have a use, i.e. it doesn't produce a value that would |
| 1116 | // be consumed (e.g. store), then it terminates a chain of computation. |
| 1117 | // Give it a large SethiUllman number so it will be scheduled right |
| 1118 | // before its predecessors that it doesn't lengthen their live ranges. |
| 1119 | return 0xffff; |
| 1120 | else if (SU->NumPreds == 0) |
| 1121 | // If SU does not have a def, schedule it close to its uses because it |
| 1122 | // does not lengthen any live ranges. |
| 1123 | return 0; |
| 1124 | else |
| 1125 | return SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | private: |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1129 | bool canClobber(SUnit *SU, SUnit *Op); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1130 | void AddPseudoTwoAddrDeps(); |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1131 | void CalculateSethiUllmanNumbers(); |
| 1132 | unsigned CalcNodeSethiUllmanNumber(const SUnit *SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1133 | }; |
| 1134 | |
| 1135 | |
| 1136 | template<class SF> |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 1137 | class VISIBILITY_HIDDEN TDRegReductionPriorityQueue |
| 1138 | : public RegReductionPriorityQueue<SF> { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1139 | // SUnitMap SDNode to SUnit mapping (n -> n). |
| 1140 | DenseMap<SDNode*, std::vector<SUnit*> > *SUnitMap; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1141 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1142 | // SUnits - The SUnits for the current graph. |
| 1143 | const std::vector<SUnit> *SUnits; |
| 1144 | |
| 1145 | // SethiUllmanNumbers - The SethiUllman number for each node. |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1146 | std::vector<unsigned> SethiUllmanNumbers; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1147 | |
| 1148 | public: |
| 1149 | TDRegReductionPriorityQueue() {} |
| 1150 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1151 | void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1152 | std::vector<SUnit> &sunits) { |
| 1153 | SUnitMap = &sumap; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1154 | SUnits = &sunits; |
| 1155 | // Calculate node priorities. |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1156 | CalculateSethiUllmanNumbers(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1159 | void addNode(const SUnit *SU) { |
| 1160 | SethiUllmanNumbers.resize(SUnits->size(), 0); |
| 1161 | CalcNodeSethiUllmanNumber(SU); |
| 1162 | } |
| 1163 | |
| 1164 | void updateNode(const SUnit *SU) { |
| 1165 | SethiUllmanNumbers[SU->NodeNum] = 0; |
| 1166 | CalcNodeSethiUllmanNumber(SU); |
| 1167 | } |
| 1168 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1169 | void releaseState() { |
| 1170 | SUnits = 0; |
| 1171 | SethiUllmanNumbers.clear(); |
| 1172 | } |
| 1173 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1174 | unsigned getNodePriority(const SUnit *SU) const { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1175 | assert(SU->NodeNum < SethiUllmanNumbers.size()); |
| 1176 | return SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | private: |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1180 | void CalculateSethiUllmanNumbers(); |
| 1181 | unsigned CalcNodeSethiUllmanNumber(const SUnit *SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1182 | }; |
| 1183 | } |
| 1184 | |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1185 | /// closestSucc - Returns the scheduled cycle of the successor which is |
| 1186 | /// closet to the current cycle. |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1187 | static unsigned closestSucc(const SUnit *SU) { |
| 1188 | unsigned MaxCycle = 0; |
| 1189 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1190 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1191 | unsigned Cycle = I->Dep->Cycle; |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1192 | // If there are bunch of CopyToRegs stacked up, they should be considered |
| 1193 | // to be at the same position. |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 1194 | if (I->Dep->Node && I->Dep->Node->getOpcode() == ISD::CopyToReg) |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1195 | Cycle = closestSucc(I->Dep)+1; |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1196 | if (Cycle > MaxCycle) |
| 1197 | MaxCycle = Cycle; |
| 1198 | } |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1199 | return MaxCycle; |
| 1200 | } |
| 1201 | |
Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1202 | /// calcMaxScratches - Returns an cost estimate of the worse case requirement |
| 1203 | /// for scratch registers. Live-in operands and live-out results don't count |
| 1204 | /// since they are "fixed". |
| 1205 | static unsigned calcMaxScratches(const SUnit *SU) { |
| 1206 | unsigned Scratches = 0; |
| 1207 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 1208 | I != E; ++I) { |
| 1209 | if (I->isCtrl) continue; // ignore chain preds |
Evan Cheng | 0e400d4 | 2008-01-09 23:01:55 +0000 | [diff] [blame] | 1210 | if (!I->Dep->Node || I->Dep->Node->getOpcode() != ISD::CopyFromReg) |
Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1211 | Scratches++; |
| 1212 | } |
| 1213 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1214 | I != E; ++I) { |
| 1215 | if (I->isCtrl) continue; // ignore chain succs |
Evan Cheng | 0e400d4 | 2008-01-09 23:01:55 +0000 | [diff] [blame] | 1216 | if (!I->Dep->Node || I->Dep->Node->getOpcode() != ISD::CopyToReg) |
Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1217 | Scratches += 10; |
| 1218 | } |
| 1219 | return Scratches; |
| 1220 | } |
| 1221 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1222 | // Bottom up |
| 1223 | bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const { |
David Greene | 4c1e6f3 | 2007-06-29 03:42:23 +0000 | [diff] [blame] | 1224 | // There used to be a special tie breaker here that looked for |
David Greene | 5b6f755 | 2007-06-29 02:48:09 +0000 | [diff] [blame] | 1225 | // two-address instructions and preferred the instruction with a |
| 1226 | // def&use operand. The special case triggered diagnostics when |
| 1227 | // _GLIBCXX_DEBUG was enabled because it broke the strict weak |
| 1228 | // ordering that priority_queue requires. It didn't help much anyway |
| 1229 | // because AddPseudoTwoAddrDeps already covers many of the cases |
| 1230 | // where it would have applied. In addition, it's counter-intuitive |
| 1231 | // that a tie breaker would be the first thing attempted. There's a |
| 1232 | // "real" tie breaker below that is the operation of last resort. |
| 1233 | // The fact that the "special tie breaker" would trigger when there |
| 1234 | // wasn't otherwise a tie is what broke the strict weak ordering |
| 1235 | // constraint. |
Evan Cheng | 99f2f79 | 2006-05-13 08:22:24 +0000 | [diff] [blame] | 1236 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1237 | unsigned LPriority = SPQ->getNodePriority(left); |
| 1238 | unsigned RPriority = SPQ->getNodePriority(right); |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1239 | if (LPriority > RPriority) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1240 | return true; |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1241 | else if (LPriority == RPriority) { |
Dan Gohman | e131e3a | 2007-04-26 19:40:56 +0000 | [diff] [blame] | 1242 | // Try schedule def + use closer when Sethi-Ullman numbers are the same. |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1243 | // e.g. |
| 1244 | // t1 = op t2, c1 |
| 1245 | // t3 = op t4, c2 |
| 1246 | // |
| 1247 | // and the following instructions are both ready. |
| 1248 | // t2 = op c3 |
| 1249 | // t4 = op c4 |
| 1250 | // |
| 1251 | // Then schedule t2 = op first. |
| 1252 | // i.e. |
| 1253 | // t4 = op c4 |
| 1254 | // t2 = op c3 |
| 1255 | // t1 = op t2, c1 |
| 1256 | // t3 = op t4, c2 |
| 1257 | // |
| 1258 | // This creates more short live intervals. |
| 1259 | unsigned LDist = closestSucc(left); |
| 1260 | unsigned RDist = closestSucc(right); |
| 1261 | if (LDist < RDist) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1262 | return true; |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1263 | else if (LDist == RDist) { |
Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1264 | // Intuitively, it's good to push down instructions whose results are |
| 1265 | // liveout so their long live ranges won't conflict with other values |
| 1266 | // which are needed inside the BB. Further prioritize liveout instructions |
| 1267 | // by the number of operands which are calculated within the BB. |
| 1268 | unsigned LScratch = calcMaxScratches(left); |
| 1269 | unsigned RScratch = calcMaxScratches(right); |
| 1270 | if (LScratch > RScratch) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1271 | return true; |
Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1272 | else if (LScratch == RScratch) |
| 1273 | if (left->Height > right->Height) |
Evan Cheng | 99f2f79 | 2006-05-13 08:22:24 +0000 | [diff] [blame] | 1274 | return true; |
Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1275 | else if (left->Height == right->Height) |
| 1276 | if (left->Depth < right->Depth) |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1277 | return true; |
Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1278 | else if (left->Depth == right->Depth) |
| 1279 | if (left->CycleBound > right->CycleBound) |
| 1280 | return true; |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1281 | } |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1282 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1283 | return false; |
| 1284 | } |
| 1285 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1286 | template<class SF> |
| 1287 | bool BURegReductionPriorityQueue<SF>::canClobber(SUnit *SU, SUnit *Op) { |
| 1288 | if (SU->isTwoAddress) { |
| 1289 | unsigned Opc = SU->Node->getTargetOpcode(); |
Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1290 | const TargetInstrDesc &TID = TII->get(Opc); |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1291 | unsigned NumRes = TID.getNumDefs(); |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1292 | unsigned NumOps = ScheduleDAG::CountOperands(SU->Node); |
| 1293 | for (unsigned i = 0; i != NumOps; ++i) { |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1294 | if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1295 | SDNode *DU = SU->Node->getOperand(i).Val; |
Evan Cheng | 1bf16631 | 2007-11-09 01:27:11 +0000 | [diff] [blame] | 1296 | if ((*SUnitMap).find(DU) != (*SUnitMap).end() && |
| 1297 | Op == (*SUnitMap)[DU][SU->InstanceNo]) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1298 | return true; |
| 1299 | } |
| 1300 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1301 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1302 | return false; |
| 1303 | } |
| 1304 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1305 | |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1306 | /// hasCopyToRegUse - Return true if SU has a value successor that is a |
| 1307 | /// CopyToReg node. |
| 1308 | static bool hasCopyToRegUse(SUnit *SU) { |
| 1309 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1310 | I != E; ++I) { |
| 1311 | if (I->isCtrl) continue; |
| 1312 | SUnit *SuccSU = I->Dep; |
| 1313 | if (SuccSU->Node && SuccSU->Node->getOpcode() == ISD::CopyToReg) |
| 1314 | return true; |
| 1315 | } |
| 1316 | return false; |
| 1317 | } |
| 1318 | |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1319 | /// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's |
| 1320 | /// physical register def. |
| 1321 | static bool canClobberPhysRegDefs(SUnit *SuccSU, SUnit *SU, |
| 1322 | const TargetInstrInfo *TII, |
| 1323 | const MRegisterInfo *MRI) { |
| 1324 | SDNode *N = SuccSU->Node; |
Chris Lattner | b0d06b4 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 1325 | unsigned NumDefs = TII->get(N->getTargetOpcode()).getNumDefs(); |
| 1326 | const unsigned *ImpDefs = TII->get(N->getTargetOpcode()).getImplicitDefs(); |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1327 | if (!ImpDefs) |
| 1328 | return false; |
Chris Lattner | b0d06b4 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 1329 | const unsigned *SUImpDefs = |
| 1330 | TII->get(SU->Node->getTargetOpcode()).getImplicitDefs(); |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1331 | if (!SUImpDefs) |
| 1332 | return false; |
| 1333 | for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) { |
| 1334 | MVT::ValueType VT = N->getValueType(i); |
| 1335 | if (VT == MVT::Flag || VT == MVT::Other) |
| 1336 | continue; |
| 1337 | unsigned Reg = ImpDefs[i - NumDefs]; |
| 1338 | for (;*SUImpDefs; ++SUImpDefs) { |
| 1339 | unsigned SUReg = *SUImpDefs; |
| 1340 | if (MRI->regsOverlap(Reg, SUReg)) |
| 1341 | return true; |
| 1342 | } |
| 1343 | } |
| 1344 | return false; |
| 1345 | } |
| 1346 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1347 | /// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses |
| 1348 | /// it as a def&use operand. Add a pseudo control edge from it to the other |
| 1349 | /// node (if it won't create a cycle) so the two-address one will be scheduled |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1350 | /// first (lower in the schedule). If both nodes are two-address, favor the |
| 1351 | /// one that has a CopyToReg use (more likely to be a loop induction update). |
| 1352 | /// If both are two-address, but one is commutable while the other is not |
| 1353 | /// commutable, favor the one that's not commutable. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1354 | template<class SF> |
| 1355 | void BURegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1356 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) { |
| 1357 | SUnit *SU = (SUnit *)&((*SUnits)[i]); |
| 1358 | if (!SU->isTwoAddress) |
| 1359 | continue; |
| 1360 | |
| 1361 | SDNode *Node = SU->Node; |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1362 | if (!Node || !Node->isTargetOpcode() || SU->FlaggedNodes.size() > 0) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1363 | continue; |
| 1364 | |
| 1365 | unsigned Opc = Node->getTargetOpcode(); |
Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1366 | const TargetInstrDesc &TID = TII->get(Opc); |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1367 | unsigned NumRes = TID.getNumDefs(); |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1368 | unsigned NumOps = ScheduleDAG::CountOperands(Node); |
| 1369 | for (unsigned j = 0; j != NumOps; ++j) { |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1370 | if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) != -1) { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1371 | SDNode *DU = SU->Node->getOperand(j).Val; |
Evan Cheng | 1bf16631 | 2007-11-09 01:27:11 +0000 | [diff] [blame] | 1372 | if ((*SUnitMap).find(DU) == (*SUnitMap).end()) |
| 1373 | continue; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1374 | SUnit *DUSU = (*SUnitMap)[DU][SU->InstanceNo]; |
Evan Cheng | f24d15f | 2006-11-06 21:33:46 +0000 | [diff] [blame] | 1375 | if (!DUSU) continue; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1376 | for (SUnit::succ_iterator I = DUSU->Succs.begin(),E = DUSU->Succs.end(); |
| 1377 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1378 | if (I->isCtrl) continue; |
| 1379 | SUnit *SuccSU = I->Dep; |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1380 | if (SuccSU == SU) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1381 | continue; |
Evan Cheng | 2dbffa4 | 2007-11-06 08:44:59 +0000 | [diff] [blame] | 1382 | // Be conservative. Ignore if nodes aren't at roughly the same |
| 1383 | // depth and height. |
| 1384 | if (SuccSU->Height < SU->Height && (SU->Height - SuccSU->Height) > 1) |
| 1385 | continue; |
Evan Cheng | aa2d6ef | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1386 | if (!SuccSU->Node || !SuccSU->Node->isTargetOpcode()) |
| 1387 | continue; |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1388 | // Don't constrain nodes with physical register defs if the |
Dan Gohman | cf8827a | 2008-01-29 12:43:50 +0000 | [diff] [blame] | 1389 | // predecessor can clobber them. |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1390 | if (SuccSU->hasPhysRegDefs) { |
| 1391 | if (canClobberPhysRegDefs(SuccSU, SU, TII, MRI)) |
| 1392 | continue; |
| 1393 | } |
Evan Cheng | aa2d6ef | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1394 | // Don't constraint extract_subreg / insert_subreg these may be |
| 1395 | // coalesced away. We don't them close to their uses. |
| 1396 | unsigned SuccOpc = SuccSU->Node->getTargetOpcode(); |
| 1397 | if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG || |
| 1398 | SuccOpc == TargetInstrInfo::INSERT_SUBREG) |
| 1399 | continue; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1400 | if ((!canClobber(SuccSU, DUSU) || |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1401 | (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) || |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1402 | (!SU->isCommutable && SuccSU->isCommutable)) && |
| 1403 | !isReachable(SuccSU, SU)) { |
| 1404 | DOUT << "Adding an edge from SU # " << SU->NodeNum |
| 1405 | << " to SU #" << SuccSU->NodeNum << "\n"; |
| 1406 | SU->addPred(SuccSU, true, true); |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1414 | /// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1415 | /// Smaller number is the higher priority. |
| 1416 | template<class SF> |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1417 | unsigned BURegReductionPriorityQueue<SF>:: |
| 1418 | CalcNodeSethiUllmanNumber(const SUnit *SU) { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1419 | unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1420 | if (SethiUllmanNumber != 0) |
| 1421 | return SethiUllmanNumber; |
| 1422 | |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1423 | unsigned Extra = 0; |
| 1424 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 1425 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1426 | if (I->isCtrl) continue; // ignore chain preds |
| 1427 | SUnit *PredSU = I->Dep; |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1428 | unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU); |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1429 | if (PredSethiUllman > SethiUllmanNumber) { |
| 1430 | SethiUllmanNumber = PredSethiUllman; |
| 1431 | Extra = 0; |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1432 | } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1433 | ++Extra; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1434 | } |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1435 | |
| 1436 | SethiUllmanNumber += Extra; |
| 1437 | |
| 1438 | if (SethiUllmanNumber == 0) |
| 1439 | SethiUllmanNumber = 1; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1440 | |
| 1441 | return SethiUllmanNumber; |
| 1442 | } |
| 1443 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1444 | /// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all |
| 1445 | /// scheduling units. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1446 | template<class SF> |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1447 | void BURegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1448 | SethiUllmanNumbers.assign(SUnits->size(), 0); |
| 1449 | |
| 1450 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1451 | CalcNodeSethiUllmanNumber(&(*SUnits)[i]); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | static unsigned SumOfUnscheduledPredsOfSuccs(const SUnit *SU) { |
| 1455 | unsigned Sum = 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 1456 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1457 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1458 | SUnit *SuccSU = I->Dep; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 1459 | for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(), |
| 1460 | EE = SuccSU->Preds.end(); II != EE; ++II) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1461 | SUnit *PredSU = II->Dep; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1462 | if (!PredSU->isScheduled) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1463 | ++Sum; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | return Sum; |
| 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | // Top down |
| 1472 | 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] | 1473 | unsigned LPriority = SPQ->getNodePriority(left); |
| 1474 | unsigned RPriority = SPQ->getNodePriority(right); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 1475 | bool LIsTarget = left->Node && left->Node->isTargetOpcode(); |
| 1476 | bool RIsTarget = right->Node && right->Node->isTargetOpcode(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1477 | bool LIsFloater = LIsTarget && left->NumPreds == 0; |
| 1478 | bool RIsFloater = RIsTarget && right->NumPreds == 0; |
| 1479 | unsigned LBonus = (SumOfUnscheduledPredsOfSuccs(left) == 1) ? 2 : 0; |
| 1480 | unsigned RBonus = (SumOfUnscheduledPredsOfSuccs(right) == 1) ? 2 : 0; |
| 1481 | |
| 1482 | if (left->NumSuccs == 0 && right->NumSuccs != 0) |
| 1483 | return false; |
| 1484 | else if (left->NumSuccs != 0 && right->NumSuccs == 0) |
| 1485 | return true; |
| 1486 | |
| 1487 | // Special tie breaker: if two nodes share a operand, the one that use it |
| 1488 | // as a def&use operand is preferred. |
| 1489 | if (LIsTarget && RIsTarget) { |
| 1490 | if (left->isTwoAddress && !right->isTwoAddress) { |
| 1491 | SDNode *DUNode = left->Node->getOperand(0).Val; |
| 1492 | if (DUNode->isOperand(right->Node)) |
| 1493 | RBonus += 2; |
| 1494 | } |
| 1495 | if (!left->isTwoAddress && right->isTwoAddress) { |
| 1496 | SDNode *DUNode = right->Node->getOperand(0).Val; |
| 1497 | if (DUNode->isOperand(left->Node)) |
| 1498 | LBonus += 2; |
| 1499 | } |
| 1500 | } |
| 1501 | if (LIsFloater) |
| 1502 | LBonus -= 2; |
| 1503 | if (RIsFloater) |
| 1504 | RBonus -= 2; |
| 1505 | if (left->NumSuccs == 1) |
| 1506 | LBonus += 2; |
| 1507 | if (right->NumSuccs == 1) |
| 1508 | RBonus += 2; |
| 1509 | |
| 1510 | if (LPriority+LBonus < RPriority+RBonus) |
| 1511 | return true; |
| 1512 | else if (LPriority == RPriority) |
| 1513 | if (left->Depth < right->Depth) |
| 1514 | return true; |
| 1515 | else if (left->Depth == right->Depth) |
| 1516 | if (left->NumSuccsLeft > right->NumSuccsLeft) |
| 1517 | return true; |
| 1518 | else if (left->NumSuccsLeft == right->NumSuccsLeft) |
| 1519 | if (left->CycleBound > right->CycleBound) |
| 1520 | return true; |
| 1521 | return false; |
| 1522 | } |
| 1523 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1524 | /// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1525 | /// Smaller number is the higher priority. |
| 1526 | template<class SF> |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1527 | unsigned TDRegReductionPriorityQueue<SF>:: |
| 1528 | CalcNodeSethiUllmanNumber(const SUnit *SU) { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1529 | unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1530 | if (SethiUllmanNumber != 0) |
| 1531 | return SethiUllmanNumber; |
| 1532 | |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 1533 | unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1534 | if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg) |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1535 | SethiUllmanNumber = 0xffff; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1536 | else if (SU->NumSuccsLeft == 0) |
| 1537 | // If SU does not have a use, i.e. it doesn't produce a value that would |
| 1538 | // be consumed (e.g. store), then it terminates a chain of computation. |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1539 | // Give it a small SethiUllman number so it will be scheduled right before |
| 1540 | // its predecessors that it doesn't lengthen their live ranges. |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1541 | SethiUllmanNumber = 0; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1542 | else if (SU->NumPredsLeft == 0 && |
| 1543 | (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU))) |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1544 | SethiUllmanNumber = 0xffff; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1545 | else { |
| 1546 | int Extra = 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 1547 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 1548 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1549 | if (I->isCtrl) continue; // ignore chain preds |
| 1550 | SUnit *PredSU = I->Dep; |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1551 | unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1552 | if (PredSethiUllman > SethiUllmanNumber) { |
| 1553 | SethiUllmanNumber = PredSethiUllman; |
| 1554 | Extra = 0; |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1555 | } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1556 | ++Extra; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | SethiUllmanNumber += Extra; |
| 1560 | } |
| 1561 | |
| 1562 | return SethiUllmanNumber; |
| 1563 | } |
| 1564 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1565 | /// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all |
| 1566 | /// scheduling units. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1567 | template<class SF> |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1568 | void TDRegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1569 | SethiUllmanNumbers.assign(SUnits->size(), 0); |
| 1570 | |
| 1571 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1572 | CalcNodeSethiUllmanNumber(&(*SUnits)[i]); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | //===----------------------------------------------------------------------===// |
| 1576 | // Public Constructor Functions |
| 1577 | //===----------------------------------------------------------------------===// |
| 1578 | |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1579 | llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, |
| 1580 | SelectionDAG *DAG, |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1581 | MachineBasicBlock *BB) { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1582 | const TargetInstrInfo *TII = DAG->getTarget().getInstrInfo(); |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1583 | const MRegisterInfo *MRI = DAG->getTarget().getRegisterInfo(); |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1584 | return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), true, |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1585 | new BURegReductionPriorityQueue<bu_ls_rr_sort>(TII, MRI)); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1588 | llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, |
| 1589 | SelectionDAG *DAG, |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1590 | MachineBasicBlock *BB) { |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1591 | return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), false, |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1592 | new TDRegReductionPriorityQueue<td_ls_rr_sort>()); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |