Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1 | //===----- ScheduleDAGList.cpp - Reg pressure reduction list scheduler ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Evan Cheng and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements bottom-up and top-down register pressure reduction list |
| 11 | // schedulers, using standard algorithms. The basic approach uses a priority |
| 12 | // queue of available nodes to schedule. One at a time, nodes are taken from |
| 13 | // the priority queue (thus in priority order), checked for legality to |
| 14 | // schedule, and emitted if legal. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
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/CodeGen/SSARegMap.h" |
| 22 | #include "llvm/Target/MRegisterInfo.h" |
Owen Anderson | 8c2c1e9 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
| 25 | #include "llvm/Target/TargetInstrInfo.h" |
| 26 | #include "llvm/Support/Debug.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Compiler.h" |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/Statistic.h" |
| 31 | #include <climits> |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 32 | #include <queue> |
| 33 | #include "llvm/Support/CommandLine.h" |
| 34 | using namespace llvm; |
| 35 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 36 | STATISTIC(NumBacktracks, "Number of times scheduler backtraced"); |
| 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 | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 61 | ///a |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 62 | SchedulingPriorityQueue *AvailableQueue; |
| 63 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 64 | /// LiveRegs / LiveRegDefs - A set of physical registers and their definition |
| 65 | /// that are "live". These nodes must be scheduled before any other nodes that |
| 66 | /// modifies the registers can be scheduled. |
| 67 | SmallSet<unsigned, 4> LiveRegs; |
| 68 | std::vector<SUnit*> LiveRegDefs; |
| 69 | std::vector<unsigned> LiveRegCycles; |
| 70 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 71 | public: |
| 72 | ScheduleDAGRRList(SelectionDAG &dag, MachineBasicBlock *bb, |
| 73 | const TargetMachine &tm, bool isbottomup, |
| 74 | SchedulingPriorityQueue *availqueue) |
| 75 | : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup), |
| 76 | AvailableQueue(availqueue) { |
| 77 | } |
| 78 | |
| 79 | ~ScheduleDAGRRList() { |
| 80 | delete AvailableQueue; |
| 81 | } |
| 82 | |
| 83 | void Schedule(); |
| 84 | |
| 85 | private: |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 86 | void ReleasePred(SUnit*, bool, unsigned); |
| 87 | void ReleaseSucc(SUnit*, bool isChain, unsigned); |
| 88 | void CapturePred(SUnit*, SUnit*, bool); |
| 89 | void ScheduleNodeBottomUp(SUnit*, unsigned); |
| 90 | void ScheduleNodeTopDown(SUnit*, unsigned); |
| 91 | void UnscheduleNodeBottomUp(SUnit*); |
| 92 | void BacktrackBottomUp(SUnit*, unsigned, unsigned&); |
| 93 | SUnit *CopyAndMoveSuccessors(SUnit*); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 94 | void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned, |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 95 | const TargetRegisterClass*, |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 96 | const TargetRegisterClass*, |
| 97 | SmallVector<SUnit*, 2>&); |
| 98 | bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 99 | void ListScheduleTopDown(); |
| 100 | void ListScheduleBottomUp(); |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 101 | void CommuteNodesToReducePressure(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 102 | }; |
| 103 | } // end anonymous namespace |
| 104 | |
| 105 | |
| 106 | /// Schedule - Schedule the DAG using list scheduling. |
| 107 | void ScheduleDAGRRList::Schedule() { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 108 | DOUT << "********** List Scheduling **********\n"; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 109 | |
| 110 | LiveRegDefs.resize(MRI->getNumRegs(), NULL); |
| 111 | LiveRegCycles.resize(MRI->getNumRegs(), 0); |
| 112 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 113 | // Build scheduling units. |
| 114 | BuildSchedUnits(); |
| 115 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 116 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 117 | SUnits[su].dumpAll(&DAG)); |
Evan Cheng | 47fbeda | 2006-10-14 08:34:06 +0000 | [diff] [blame] | 118 | CalculateDepths(); |
| 119 | CalculateHeights(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 120 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 121 | AvailableQueue->initNodes(SUnitMap, SUnits); |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 122 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 123 | // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate. |
| 124 | if (isBottomUp) |
| 125 | ListScheduleBottomUp(); |
| 126 | else |
| 127 | ListScheduleTopDown(); |
| 128 | |
| 129 | AvailableQueue->releaseState(); |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 130 | |
Evan Cheng | 009f5f5 | 2006-05-25 08:37:31 +0000 | [diff] [blame] | 131 | CommuteNodesToReducePressure(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 132 | |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 133 | DOUT << "*** Final schedule ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 134 | DEBUG(dumpSchedule()); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 135 | DOUT << "\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 136 | |
| 137 | // Emit in scheduled order |
| 138 | EmitSchedule(); |
| 139 | } |
| 140 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 141 | /// CommuteNodesToReducePressure - If a node is two-address and commutable, and |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 142 | /// it is not the last use of its first operand, add it to the CommuteSet if |
| 143 | /// possible. It will be commuted when it is translated to a MI. |
| 144 | void ScheduleDAGRRList::CommuteNodesToReducePressure() { |
Evan Cheng | e3c4419 | 2007-06-22 01:35:51 +0000 | [diff] [blame] | 145 | SmallPtrSet<SUnit*, 4> OperandSeen; |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 146 | for (unsigned i = Sequence.size()-1; i != 0; --i) { // Ignore first node. |
| 147 | SUnit *SU = Sequence[i]; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 148 | if (!SU || !SU->Node) continue; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 149 | if (SU->isCommutable) { |
| 150 | unsigned Opc = SU->Node->getTargetOpcode(); |
Evan Cheng | 100c8d6 | 2007-09-13 00:06:00 +0000 | [diff] [blame] | 151 | unsigned NumRes = TII->getNumDefs(Opc); |
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) { |
Evan Cheng | 67fc141 | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 154 | if (TII->getOperandConstraint(Opc, 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 | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 158 | SUnit *OpSU = 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 | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 167 | OpSU = 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 | |
| 388 | /// isSafeToCopy - True if the SUnit for the given SDNode can safely cloned, |
| 389 | /// i.e. the node does not produce a flag, it does not read a flag and it does |
| 390 | /// not have an incoming chain. |
| 391 | static bool isSafeToCopy(SDNode *N) { |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 392 | if (!N) |
| 393 | return true; |
| 394 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 395 | for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) |
| 396 | if (N->getValueType(i) == MVT::Flag) |
| 397 | return false; |
| 398 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 399 | const SDOperand &Op = N->getOperand(i); |
| 400 | MVT::ValueType VT = Op.Val->getValueType(Op.ResNo); |
| 401 | if (VT == MVT::Other || VT == MVT::Flag) |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | /// CopyAndMoveSuccessors - Clone the specified node and move its scheduled |
| 409 | /// successors to the newly created node. |
| 410 | SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) { |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 411 | DOUT << "Duplicating SU # " << SU->NodeNum << "\n"; |
| 412 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 413 | SUnit *NewSU = Clone(SU); |
| 414 | |
| 415 | // New SUnit has the exact same predecessors. |
| 416 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 417 | I != E; ++I) |
| 418 | if (!I->isSpecial) { |
| 419 | NewSU->addPred(I->Dep, I->isCtrl, false, I->Reg, I->Cost); |
| 420 | NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1); |
| 421 | } |
| 422 | |
| 423 | // Only copy scheduled successors. Cut them from old node's successor |
| 424 | // list and move them over. |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 425 | SmallVector<std::pair<SUnit*, bool>, 4> DelDeps; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 426 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 427 | I != E; ++I) { |
| 428 | if (I->isSpecial) |
| 429 | continue; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 430 | if (I->Dep->isScheduled) { |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 431 | NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 432 | I->Dep->addPred(NewSU, I->isCtrl, false, I->Reg, I->Cost); |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 433 | DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl)); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 437 | SUnit *Succ = DelDeps[i].first; |
| 438 | bool isCtrl = DelDeps[i].second; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 439 | Succ->removePred(SU, isCtrl, false); |
| 440 | } |
| 441 | |
| 442 | AvailableQueue->updateNode(SU); |
| 443 | AvailableQueue->addNode(NewSU); |
| 444 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 445 | ++NumDups; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 446 | return NewSU; |
| 447 | } |
| 448 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 449 | /// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies |
| 450 | /// and move all scheduled successors of the given SUnit to the last copy. |
| 451 | void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg, |
| 452 | const TargetRegisterClass *DestRC, |
| 453 | const TargetRegisterClass *SrcRC, |
| 454 | SmallVector<SUnit*, 2> &Copies) { |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 455 | SUnit *CopyFromSU = NewSUnit(NULL); |
| 456 | CopyFromSU->CopySrcRC = SrcRC; |
| 457 | CopyFromSU->CopyDstRC = DestRC; |
| 458 | CopyFromSU->Depth = SU->Depth; |
| 459 | CopyFromSU->Height = SU->Height; |
| 460 | |
| 461 | SUnit *CopyToSU = NewSUnit(NULL); |
| 462 | CopyToSU->CopySrcRC = DestRC; |
| 463 | CopyToSU->CopyDstRC = SrcRC; |
| 464 | |
| 465 | // Only copy scheduled successors. Cut them from old node's successor |
| 466 | // list and move them over. |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 467 | SmallVector<std::pair<SUnit*, bool>, 4> DelDeps; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 468 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 469 | I != E; ++I) { |
| 470 | if (I->isSpecial) |
| 471 | continue; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 472 | if (I->Dep->isScheduled) { |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 473 | CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 474 | I->Dep->addPred(CopyToSU, I->isCtrl, false, I->Reg, I->Cost); |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 475 | DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl)); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { |
Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 479 | SUnit *Succ = DelDeps[i].first; |
| 480 | bool isCtrl = DelDeps[i].second; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 481 | Succ->removePred(SU, isCtrl, false); |
| 482 | } |
| 483 | |
| 484 | CopyFromSU->addPred(SU, false, false, Reg, -1); |
| 485 | CopyToSU->addPred(CopyFromSU, false, false, Reg, 1); |
| 486 | |
| 487 | AvailableQueue->updateNode(SU); |
| 488 | AvailableQueue->addNode(CopyFromSU); |
| 489 | AvailableQueue->addNode(CopyToSU); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 490 | Copies.push_back(CopyFromSU); |
| 491 | Copies.push_back(CopyToSU); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 492 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 493 | ++NumCCCopies; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /// getPhysicalRegisterVT - Returns the ValueType of the physical register |
| 497 | /// definition of the specified node. |
| 498 | /// FIXME: Move to SelectionDAG? |
| 499 | static MVT::ValueType getPhysicalRegisterVT(SDNode *N, unsigned Reg, |
| 500 | const TargetInstrInfo *TII) { |
| 501 | const TargetInstrDescriptor &TID = TII->get(N->getTargetOpcode()); |
| 502 | assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!"); |
| 503 | unsigned NumRes = TID.numDefs; |
| 504 | for (const unsigned *ImpDef = TID.ImplicitDefs; *ImpDef; ++ImpDef) { |
| 505 | if (Reg == *ImpDef) |
| 506 | break; |
| 507 | ++NumRes; |
| 508 | } |
| 509 | return N->getValueType(NumRes); |
| 510 | } |
| 511 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 512 | /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay |
| 513 | /// scheduling of the given node to satisfy live physical register dependencies. |
| 514 | /// If the specific node is the last one that's available to schedule, do |
| 515 | /// whatever is necessary (i.e. backtracking or cloning) to make it possible. |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 516 | bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU, |
| 517 | SmallVector<unsigned, 4> &LRegs){ |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 518 | if (LiveRegs.empty()) |
| 519 | return false; |
| 520 | |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 521 | SmallSet<unsigned, 4> RegAdded; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 522 | // 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] | 523 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 524 | I != E; ++I) { |
| 525 | if (I->Cost < 0) { |
| 526 | unsigned Reg = I->Reg; |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 527 | if (LiveRegs.count(Reg) && LiveRegDefs[Reg] != I->Dep) { |
| 528 | if (RegAdded.insert(Reg)) |
| 529 | LRegs.push_back(Reg); |
| 530 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 531 | for (const unsigned *Alias = MRI->getAliasSet(Reg); |
| 532 | *Alias; ++Alias) |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 533 | if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != I->Dep) { |
| 534 | if (RegAdded.insert(*Alias)) |
| 535 | LRegs.push_back(*Alias); |
| 536 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | for (unsigned i = 0, e = SU->FlaggedNodes.size()+1; i != e; ++i) { |
| 541 | SDNode *Node = (i == 0) ? SU->Node : SU->FlaggedNodes[i-1]; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 542 | if (!Node || !Node->isTargetOpcode()) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 543 | continue; |
| 544 | const TargetInstrDescriptor &TID = TII->get(Node->getTargetOpcode()); |
| 545 | if (!TID.ImplicitDefs) |
| 546 | continue; |
| 547 | for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) { |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 548 | if (LiveRegs.count(*Reg) && LiveRegDefs[*Reg] != SU) { |
| 549 | if (RegAdded.insert(*Reg)) |
| 550 | LRegs.push_back(*Reg); |
| 551 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 552 | for (const unsigned *Alias = MRI->getAliasSet(*Reg); |
| 553 | *Alias; ++Alias) |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 554 | if (LiveRegs.count(*Alias) && LiveRegDefs[*Alias] != SU) { |
| 555 | if (RegAdded.insert(*Alias)) |
| 556 | LRegs.push_back(*Alias); |
| 557 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 560 | return !LRegs.empty(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 563 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 564 | /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up |
| 565 | /// schedulers. |
| 566 | void ScheduleDAGRRList::ListScheduleBottomUp() { |
| 567 | unsigned CurCycle = 0; |
| 568 | // Add root to Available queue. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 569 | SUnit *RootSU = SUnitMap[DAG.getRoot().Val].front(); |
| 570 | RootSU->isAvailable = true; |
| 571 | AvailableQueue->push(RootSU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 572 | |
| 573 | // While Available queue is not empty, grab the node with the highest |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 574 | // priority. If it is not ready put it back. Schedule the node. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 575 | SmallVector<SUnit*, 4> NotReady; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 576 | while (!AvailableQueue->empty()) { |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 577 | bool Delayed = false; |
| 578 | DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 579 | SUnit *CurSU = AvailableQueue->pop(); |
| 580 | while (CurSU) { |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 581 | if (CurSU->CycleBound <= CurCycle) { |
| 582 | SmallVector<unsigned, 4> LRegs; |
| 583 | if (!DelayForLiveRegsBottomUp(CurSU, LRegs)) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 584 | break; |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 585 | Delayed = true; |
| 586 | LRegsMap.insert(std::make_pair(CurSU, LRegs)); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 587 | } |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 588 | |
| 589 | CurSU->isPending = true; // This SU is not in AvailableQueue right now. |
| 590 | NotReady.push_back(CurSU); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 591 | CurSU = AvailableQueue->pop(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 592 | } |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 593 | |
| 594 | // All candidates are delayed due to live physical reg dependencies. |
| 595 | // Try backtracking, code duplication, or inserting cross class copies |
| 596 | // to resolve it. |
| 597 | if (Delayed && !CurSU) { |
| 598 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { |
| 599 | SUnit *TrySU = NotReady[i]; |
| 600 | SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU]; |
| 601 | |
| 602 | // Try unscheduling up to the point where it's safe to schedule |
| 603 | // this node. |
| 604 | unsigned LiveCycle = CurCycle; |
| 605 | for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) { |
| 606 | unsigned Reg = LRegs[j]; |
| 607 | unsigned LCycle = LiveRegCycles[Reg]; |
| 608 | LiveCycle = std::min(LiveCycle, LCycle); |
| 609 | } |
| 610 | SUnit *OldSU = Sequence[LiveCycle]; |
| 611 | if (!WillCreateCycle(TrySU, OldSU)) { |
| 612 | BacktrackBottomUp(TrySU, LiveCycle, CurCycle); |
| 613 | // Force the current node to be scheduled before the node that |
| 614 | // requires the physical reg dep. |
| 615 | if (OldSU->isAvailable) { |
| 616 | OldSU->isAvailable = false; |
| 617 | AvailableQueue->remove(OldSU); |
| 618 | } |
| 619 | TrySU->addPred(OldSU, true, true); |
| 620 | // If one or more successors has been unscheduled, then the current |
| 621 | // node is no longer avaialable. Schedule a successor that's now |
| 622 | // available instead. |
| 623 | if (!TrySU->isAvailable) |
| 624 | CurSU = AvailableQueue->pop(); |
| 625 | else { |
| 626 | CurSU = TrySU; |
| 627 | TrySU->isPending = false; |
| 628 | NotReady.erase(NotReady.begin()+i); |
| 629 | } |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | if (!CurSU) { |
| 635 | // Can't backtrace. Try duplicating the nodes that produces these |
| 636 | // "expensive to copy" values to break the dependency. In case even |
| 637 | // that doesn't work, insert cross class copies. |
| 638 | SUnit *TrySU = NotReady[0]; |
| 639 | SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU]; |
| 640 | assert(LRegs.size() == 1 && "Can't handle this yet!"); |
| 641 | unsigned Reg = LRegs[0]; |
| 642 | SUnit *LRDef = LiveRegDefs[Reg]; |
| 643 | SUnit *NewDef; |
| 644 | if (isSafeToCopy(LRDef->Node)) |
| 645 | NewDef = CopyAndMoveSuccessors(LRDef); |
| 646 | else { |
| 647 | // Issue expensive cross register class copies. |
| 648 | MVT::ValueType VT = getPhysicalRegisterVT(LRDef->Node, Reg, TII); |
| 649 | const TargetRegisterClass *RC = |
| 650 | MRI->getPhysicalRegisterRegClass(VT, Reg); |
| 651 | const TargetRegisterClass *DestRC = MRI->getCrossCopyRegClass(RC); |
| 652 | if (!DestRC) { |
| 653 | assert(false && "Don't know how to copy this physical register!"); |
| 654 | abort(); |
| 655 | } |
| 656 | SmallVector<SUnit*, 2> Copies; |
| 657 | InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies); |
| 658 | DOUT << "Adding an edge from SU # " << TrySU->NodeNum |
| 659 | << " to SU #" << Copies.front()->NodeNum << "\n"; |
| 660 | TrySU->addPred(Copies.front(), true, true); |
| 661 | NewDef = Copies.back(); |
| 662 | } |
| 663 | |
| 664 | DOUT << "Adding an edge from SU # " << NewDef->NodeNum |
| 665 | << " to SU #" << TrySU->NodeNum << "\n"; |
| 666 | LiveRegDefs[Reg] = NewDef; |
| 667 | NewDef->addPred(TrySU, true, true); |
| 668 | TrySU->isAvailable = false; |
| 669 | CurSU = NewDef; |
| 670 | } |
| 671 | |
| 672 | if (!CurSU) { |
| 673 | assert(false && "Unable to resolve live physical register dependencies!"); |
| 674 | abort(); |
| 675 | } |
| 676 | } |
| 677 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 678 | // Add the nodes that aren't ready back onto the available list. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 679 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { |
| 680 | NotReady[i]->isPending = false; |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 681 | // May no longer be available due to backtracking. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 682 | if (NotReady[i]->isAvailable) |
| 683 | AvailableQueue->push(NotReady[i]); |
| 684 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 685 | NotReady.clear(); |
| 686 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 687 | if (!CurSU) |
| 688 | Sequence.push_back(0); |
| 689 | else { |
| 690 | ScheduleNodeBottomUp(CurSU, CurCycle); |
| 691 | Sequence.push_back(CurSU); |
| 692 | } |
| 693 | ++CurCycle; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | // Add entry node last |
| 697 | if (DAG.getEntryNode().Val != DAG.getRoot().Val) { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 698 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val].front(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 699 | Sequence.push_back(Entry); |
| 700 | } |
| 701 | |
| 702 | // Reverse the order if it is bottom up. |
| 703 | std::reverse(Sequence.begin(), Sequence.end()); |
| 704 | |
| 705 | |
| 706 | #ifndef NDEBUG |
| 707 | // Verify that all SUnits were scheduled. |
| 708 | bool AnyNotSched = false; |
| 709 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 710 | if (SUnits[i].NumSuccsLeft != 0) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 711 | if (!AnyNotSched) |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 712 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 713 | SUnits[i].dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 714 | cerr << "has not been scheduled!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 715 | AnyNotSched = true; |
| 716 | } |
| 717 | } |
| 718 | assert(!AnyNotSched); |
| 719 | #endif |
| 720 | } |
| 721 | |
| 722 | //===----------------------------------------------------------------------===// |
| 723 | // Top-Down Scheduling |
| 724 | //===----------------------------------------------------------------------===// |
| 725 | |
| 726 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 727 | /// the AvailableQueue if the count reaches zero. Also update its cycle bound. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 728 | void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain, |
| 729 | unsigned CurCycle) { |
| 730 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 731 | // latency. For example, the reader can very well read the register written |
| 732 | // by the predecessor later than the issue cycle. It also depends on the |
| 733 | // interrupt model (drain vs. freeze). |
| 734 | SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency); |
| 735 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 736 | --SuccSU->NumPredsLeft; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 737 | |
| 738 | #ifndef NDEBUG |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 739 | if (SuccSU->NumPredsLeft < 0) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 740 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 741 | SuccSU->dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 742 | cerr << " has been released too many times!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 743 | assert(0); |
| 744 | } |
| 745 | #endif |
| 746 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 747 | if (SuccSU->NumPredsLeft == 0) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 748 | SuccSU->isAvailable = true; |
| 749 | AvailableQueue->push(SuccSU); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | |
| 754 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 755 | /// count of its successors. If a successor pending count is zero, add it to |
| 756 | /// the Available queue. |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 757 | void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 758 | DOUT << "*** Scheduling [" << CurCycle << "]: "; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 759 | DEBUG(SU->dump(&DAG)); |
| 760 | SU->Cycle = CurCycle; |
| 761 | |
| 762 | AvailableQueue->ScheduledNode(SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 763 | |
| 764 | // Top down: release successors |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 765 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 766 | I != E; ++I) |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 767 | ReleaseSucc(I->Dep, I->isCtrl, CurCycle); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 768 | SU->isScheduled = true; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 771 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 772 | /// schedulers. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 773 | void ScheduleDAGRRList::ListScheduleTopDown() { |
| 774 | unsigned CurCycle = 0; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 775 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val].front(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 776 | |
| 777 | // All leaves to Available queue. |
| 778 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 779 | // It is available if it has no predecessors. |
| 780 | if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) { |
| 781 | AvailableQueue->push(&SUnits[i]); |
| 782 | SUnits[i].isAvailable = true; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | // Emit the entry node first. |
| 787 | ScheduleNodeTopDown(Entry, CurCycle); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 788 | Sequence.push_back(Entry); |
| 789 | ++CurCycle; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 790 | |
| 791 | // While Available queue is not empty, grab the node with the highest |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 792 | // priority. If it is not ready put it back. Schedule the node. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 793 | std::vector<SUnit*> NotReady; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 794 | while (!AvailableQueue->empty()) { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 795 | SUnit *CurSU = AvailableQueue->pop(); |
| 796 | while (CurSU && CurSU->CycleBound > CurCycle) { |
| 797 | NotReady.push_back(CurSU); |
| 798 | CurSU = AvailableQueue->pop(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | // Add the nodes that aren't ready back onto the available list. |
| 802 | AvailableQueue->push_all(NotReady); |
| 803 | NotReady.clear(); |
| 804 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 805 | if (!CurSU) |
| 806 | Sequence.push_back(0); |
| 807 | else { |
| 808 | ScheduleNodeTopDown(CurSU, CurCycle); |
| 809 | Sequence.push_back(CurSU); |
| 810 | } |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 811 | CurCycle++; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | |
| 815 | #ifndef NDEBUG |
| 816 | // Verify that all SUnits were scheduled. |
| 817 | bool AnyNotSched = false; |
| 818 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 819 | if (!SUnits[i].isScheduled) { |
| 820 | if (!AnyNotSched) |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 821 | cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 822 | SUnits[i].dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 823 | cerr << "has not been scheduled!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 824 | AnyNotSched = true; |
| 825 | } |
| 826 | } |
| 827 | assert(!AnyNotSched); |
| 828 | #endif |
| 829 | } |
| 830 | |
| 831 | |
| 832 | |
| 833 | //===----------------------------------------------------------------------===// |
| 834 | // RegReductionPriorityQueue Implementation |
| 835 | //===----------------------------------------------------------------------===// |
| 836 | // |
| 837 | // This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers |
| 838 | // to reduce register pressure. |
| 839 | // |
| 840 | namespace { |
| 841 | template<class SF> |
| 842 | class RegReductionPriorityQueue; |
| 843 | |
| 844 | /// Sorting functions for the Available queue. |
| 845 | struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 846 | RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ; |
| 847 | bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {} |
| 848 | bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} |
| 849 | |
| 850 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 851 | }; |
| 852 | |
| 853 | struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 854 | RegReductionPriorityQueue<td_ls_rr_sort> *SPQ; |
| 855 | td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {} |
| 856 | td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} |
| 857 | |
| 858 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 859 | }; |
| 860 | } // end anonymous namespace |
| 861 | |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 862 | static inline bool isCopyFromLiveIn(const SUnit *SU) { |
| 863 | SDNode *N = SU->Node; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 864 | return N && N->getOpcode() == ISD::CopyFromReg && |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 865 | N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag; |
| 866 | } |
| 867 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 868 | namespace { |
| 869 | template<class SF> |
Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 870 | class VISIBILITY_HIDDEN RegReductionPriorityQueue |
| 871 | : public SchedulingPriorityQueue { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 872 | std::priority_queue<SUnit*, std::vector<SUnit*>, SF> Queue; |
| 873 | |
| 874 | public: |
| 875 | RegReductionPriorityQueue() : |
| 876 | Queue(SF(this)) {} |
| 877 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 878 | virtual void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 879 | std::vector<SUnit> &sunits) {} |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 880 | |
| 881 | virtual void addNode(const SUnit *SU) {} |
| 882 | |
| 883 | virtual void updateNode(const SUnit *SU) {} |
| 884 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 885 | virtual void releaseState() {} |
| 886 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 887 | virtual unsigned getNodePriority(const SUnit *SU) const { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 888 | return 0; |
| 889 | } |
| 890 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 891 | unsigned size() const { return Queue.size(); } |
| 892 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 893 | bool empty() const { return Queue.empty(); } |
| 894 | |
| 895 | void push(SUnit *U) { |
| 896 | Queue.push(U); |
| 897 | } |
| 898 | void push_all(const std::vector<SUnit *> &Nodes) { |
| 899 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
| 900 | Queue.push(Nodes[i]); |
| 901 | } |
| 902 | |
| 903 | SUnit *pop() { |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 904 | if (empty()) return NULL; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 905 | SUnit *V = Queue.top(); |
| 906 | Queue.pop(); |
| 907 | return V; |
| 908 | } |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 909 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 910 | /// remove - This is a really inefficient way to remove a node from a |
| 911 | /// priority queue. We should roll our own heap to make this better or |
| 912 | /// something. |
| 913 | void remove(SUnit *SU) { |
| 914 | std::vector<SUnit*> Temp; |
| 915 | |
| 916 | assert(!Queue.empty() && "Not in queue!"); |
| 917 | while (Queue.top() != SU) { |
| 918 | Temp.push_back(Queue.top()); |
| 919 | Queue.pop(); |
| 920 | assert(!Queue.empty() && "Not in queue!"); |
| 921 | } |
| 922 | |
| 923 | // Remove the node from the PQ. |
| 924 | Queue.pop(); |
| 925 | |
| 926 | // Add all the other nodes back. |
| 927 | for (unsigned i = 0, e = Temp.size(); i != e; ++i) |
| 928 | Queue.push(Temp[i]); |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 929 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 930 | }; |
| 931 | |
| 932 | template<class SF> |
Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 933 | class VISIBILITY_HIDDEN BURegReductionPriorityQueue |
| 934 | : public RegReductionPriorityQueue<SF> { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 935 | // SUnitMap SDNode to SUnit mapping (n -> n). |
| 936 | DenseMap<SDNode*, std::vector<SUnit*> > *SUnitMap; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 937 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 938 | // SUnits - The SUnits for the current graph. |
| 939 | const std::vector<SUnit> *SUnits; |
| 940 | |
| 941 | // SethiUllmanNumbers - The SethiUllman number for each node. |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 942 | std::vector<unsigned> SethiUllmanNumbers; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 943 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 944 | const TargetInstrInfo *TII; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 945 | public: |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 946 | explicit BURegReductionPriorityQueue(const TargetInstrInfo *tii) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 947 | : TII(tii) {} |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 948 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 949 | void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 950 | std::vector<SUnit> &sunits) { |
| 951 | SUnitMap = &sumap; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 952 | SUnits = &sunits; |
| 953 | // Add pseudo dependency edges for two-address nodes. |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 954 | AddPseudoTwoAddrDeps(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 955 | // Calculate node priorities. |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 956 | CalculateSethiUllmanNumbers(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 959 | void addNode(const SUnit *SU) { |
| 960 | SethiUllmanNumbers.resize(SUnits->size(), 0); |
| 961 | CalcNodeSethiUllmanNumber(SU); |
| 962 | } |
| 963 | |
| 964 | void updateNode(const SUnit *SU) { |
| 965 | SethiUllmanNumbers[SU->NodeNum] = 0; |
| 966 | CalcNodeSethiUllmanNumber(SU); |
| 967 | } |
| 968 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 969 | void releaseState() { |
| 970 | SUnits = 0; |
| 971 | SethiUllmanNumbers.clear(); |
| 972 | } |
| 973 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 974 | unsigned getNodePriority(const SUnit *SU) const { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 975 | assert(SU->NodeNum < SethiUllmanNumbers.size()); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 976 | unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0; |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 977 | if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU)) |
| 978 | // CopyFromReg should be close to its def because it restricts |
| 979 | // allocation choices. But if it is a livein then perhaps we want it |
| 980 | // closer to its uses so it can be coalesced. |
| 981 | return 0xffff; |
| 982 | else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg) |
| 983 | // CopyToReg should be close to its uses to facilitate coalescing and |
| 984 | // avoid spilling. |
| 985 | return 0; |
| 986 | else if (SU->NumSuccs == 0) |
| 987 | // If SU does not have a use, i.e. it doesn't produce a value that would |
| 988 | // be consumed (e.g. store), then it terminates a chain of computation. |
| 989 | // Give it a large SethiUllman number so it will be scheduled right |
| 990 | // before its predecessors that it doesn't lengthen their live ranges. |
| 991 | return 0xffff; |
| 992 | else if (SU->NumPreds == 0) |
| 993 | // If SU does not have a def, schedule it close to its uses because it |
| 994 | // does not lengthen any live ranges. |
| 995 | return 0; |
| 996 | else |
| 997 | return SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | private: |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1001 | bool canClobber(SUnit *SU, SUnit *Op); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1002 | void AddPseudoTwoAddrDeps(); |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1003 | void CalculateSethiUllmanNumbers(); |
| 1004 | unsigned CalcNodeSethiUllmanNumber(const SUnit *SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1005 | }; |
| 1006 | |
| 1007 | |
| 1008 | template<class SF> |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 1009 | class VISIBILITY_HIDDEN TDRegReductionPriorityQueue |
| 1010 | : public RegReductionPriorityQueue<SF> { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1011 | // SUnitMap SDNode to SUnit mapping (n -> n). |
| 1012 | DenseMap<SDNode*, std::vector<SUnit*> > *SUnitMap; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1013 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1014 | // SUnits - The SUnits for the current graph. |
| 1015 | const std::vector<SUnit> *SUnits; |
| 1016 | |
| 1017 | // SethiUllmanNumbers - The SethiUllman number for each node. |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1018 | std::vector<unsigned> SethiUllmanNumbers; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1019 | |
| 1020 | public: |
| 1021 | TDRegReductionPriorityQueue() {} |
| 1022 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1023 | void initNodes(DenseMap<SDNode*, std::vector<SUnit*> > &sumap, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1024 | std::vector<SUnit> &sunits) { |
| 1025 | SUnitMap = &sumap; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1026 | SUnits = &sunits; |
| 1027 | // Calculate node priorities. |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1028 | CalculateSethiUllmanNumbers(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1031 | void addNode(const SUnit *SU) { |
| 1032 | SethiUllmanNumbers.resize(SUnits->size(), 0); |
| 1033 | CalcNodeSethiUllmanNumber(SU); |
| 1034 | } |
| 1035 | |
| 1036 | void updateNode(const SUnit *SU) { |
| 1037 | SethiUllmanNumbers[SU->NodeNum] = 0; |
| 1038 | CalcNodeSethiUllmanNumber(SU); |
| 1039 | } |
| 1040 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1041 | void releaseState() { |
| 1042 | SUnits = 0; |
| 1043 | SethiUllmanNumbers.clear(); |
| 1044 | } |
| 1045 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1046 | unsigned getNodePriority(const SUnit *SU) const { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1047 | assert(SU->NodeNum < SethiUllmanNumbers.size()); |
| 1048 | return SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | private: |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1052 | void CalculateSethiUllmanNumbers(); |
| 1053 | unsigned CalcNodeSethiUllmanNumber(const SUnit *SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1054 | }; |
| 1055 | } |
| 1056 | |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1057 | /// closestSucc - Returns the scheduled cycle of the successor which is |
| 1058 | /// closet to the current cycle. |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1059 | static unsigned closestSucc(const SUnit *SU) { |
| 1060 | unsigned MaxCycle = 0; |
| 1061 | 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] | 1062 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1063 | unsigned Cycle = I->Dep->Cycle; |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1064 | // If there are bunch of CopyToRegs stacked up, they should be considered |
| 1065 | // to be at the same position. |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 1066 | if (I->Dep->Node && I->Dep->Node->getOpcode() == ISD::CopyToReg) |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1067 | Cycle = closestSucc(I->Dep)+1; |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1068 | if (Cycle > MaxCycle) |
| 1069 | MaxCycle = Cycle; |
| 1070 | } |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1071 | return MaxCycle; |
| 1072 | } |
| 1073 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1074 | // Bottom up |
| 1075 | 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] | 1076 | // There used to be a special tie breaker here that looked for |
David Greene | 5b6f755 | 2007-06-29 02:48:09 +0000 | [diff] [blame] | 1077 | // two-address instructions and preferred the instruction with a |
| 1078 | // def&use operand. The special case triggered diagnostics when |
| 1079 | // _GLIBCXX_DEBUG was enabled because it broke the strict weak |
| 1080 | // ordering that priority_queue requires. It didn't help much anyway |
| 1081 | // because AddPseudoTwoAddrDeps already covers many of the cases |
| 1082 | // where it would have applied. In addition, it's counter-intuitive |
| 1083 | // that a tie breaker would be the first thing attempted. There's a |
| 1084 | // "real" tie breaker below that is the operation of last resort. |
| 1085 | // The fact that the "special tie breaker" would trigger when there |
| 1086 | // wasn't otherwise a tie is what broke the strict weak ordering |
| 1087 | // constraint. |
Evan Cheng | 99f2f79 | 2006-05-13 08:22:24 +0000 | [diff] [blame] | 1088 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1089 | unsigned LPriority = SPQ->getNodePriority(left); |
| 1090 | unsigned RPriority = SPQ->getNodePriority(right); |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1091 | if (LPriority > RPriority) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1092 | return true; |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1093 | else if (LPriority == RPriority) { |
Dan Gohman | e131e3a | 2007-04-26 19:40:56 +0000 | [diff] [blame] | 1094 | // Try schedule def + use closer when Sethi-Ullman numbers are the same. |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1095 | // e.g. |
| 1096 | // t1 = op t2, c1 |
| 1097 | // t3 = op t4, c2 |
| 1098 | // |
| 1099 | // and the following instructions are both ready. |
| 1100 | // t2 = op c3 |
| 1101 | // t4 = op c4 |
| 1102 | // |
| 1103 | // Then schedule t2 = op first. |
| 1104 | // i.e. |
| 1105 | // t4 = op c4 |
| 1106 | // t2 = op c3 |
| 1107 | // t1 = op t2, c1 |
| 1108 | // t3 = op t4, c2 |
| 1109 | // |
| 1110 | // This creates more short live intervals. |
| 1111 | unsigned LDist = closestSucc(left); |
| 1112 | unsigned RDist = closestSucc(right); |
| 1113 | if (LDist < RDist) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1114 | return true; |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1115 | else if (LDist == RDist) { |
Evan Cheng | f72693f | 2007-09-28 19:37:35 +0000 | [diff] [blame] | 1116 | if (left->Height > right->Height) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1117 | return true; |
Evan Cheng | f72693f | 2007-09-28 19:37:35 +0000 | [diff] [blame] | 1118 | else if (left->Height == right->Height) |
| 1119 | if (left->Depth < right->Depth) |
Evan Cheng | 99f2f79 | 2006-05-13 08:22:24 +0000 | [diff] [blame] | 1120 | return true; |
Evan Cheng | f72693f | 2007-09-28 19:37:35 +0000 | [diff] [blame] | 1121 | else if (left->Depth == right->Depth) |
| 1122 | if (left->CycleBound > right->CycleBound) |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1123 | return true; |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1124 | } |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1125 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1126 | return false; |
| 1127 | } |
| 1128 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1129 | template<class SF> |
| 1130 | bool BURegReductionPriorityQueue<SF>::canClobber(SUnit *SU, SUnit *Op) { |
| 1131 | if (SU->isTwoAddress) { |
| 1132 | unsigned Opc = SU->Node->getTargetOpcode(); |
Evan Cheng | 100c8d6 | 2007-09-13 00:06:00 +0000 | [diff] [blame] | 1133 | unsigned NumRes = TII->getNumDefs(Opc); |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1134 | unsigned NumOps = ScheduleDAG::CountOperands(SU->Node); |
| 1135 | for (unsigned i = 0; i != NumOps; ++i) { |
Evan Cheng | 67fc141 | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 1136 | if (TII->getOperandConstraint(Opc, i+NumRes, TOI::TIED_TO) != -1) { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1137 | SDNode *DU = SU->Node->getOperand(i).Val; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1138 | if (Op == (*SUnitMap)[DU][SU->InstanceNo]) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1139 | return true; |
| 1140 | } |
| 1141 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1142 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1143 | return false; |
| 1144 | } |
| 1145 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1146 | |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame^] | 1147 | /// hasCopyToRegUse - Return true if SU has a value successor that is a |
| 1148 | /// CopyToReg node. |
| 1149 | static bool hasCopyToRegUse(SUnit *SU) { |
| 1150 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1151 | I != E; ++I) { |
| 1152 | if (I->isCtrl) continue; |
| 1153 | SUnit *SuccSU = I->Dep; |
| 1154 | if (SuccSU->Node && SuccSU->Node->getOpcode() == ISD::CopyToReg) |
| 1155 | return true; |
| 1156 | } |
| 1157 | return false; |
| 1158 | } |
| 1159 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1160 | /// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses |
| 1161 | /// it as a def&use operand. Add a pseudo control edge from it to the other |
| 1162 | /// 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^] | 1163 | /// first (lower in the schedule). If both nodes are two-address, favor the |
| 1164 | /// one that has a CopyToReg use (more likely to be a loop induction update). |
| 1165 | /// If both are two-address, but one is commutable while the other is not |
| 1166 | /// commutable, favor the one that's not commutable. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1167 | template<class SF> |
| 1168 | void BURegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1169 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) { |
| 1170 | SUnit *SU = (SUnit *)&((*SUnits)[i]); |
| 1171 | if (!SU->isTwoAddress) |
| 1172 | continue; |
| 1173 | |
| 1174 | SDNode *Node = SU->Node; |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame^] | 1175 | if (!Node || !Node->isTargetOpcode() || SU->FlaggedNodes.size() > 0) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1176 | continue; |
| 1177 | |
| 1178 | unsigned Opc = Node->getTargetOpcode(); |
Evan Cheng | 100c8d6 | 2007-09-13 00:06:00 +0000 | [diff] [blame] | 1179 | unsigned NumRes = TII->getNumDefs(Opc); |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1180 | unsigned NumOps = ScheduleDAG::CountOperands(Node); |
| 1181 | for (unsigned j = 0; j != NumOps; ++j) { |
Evan Cheng | 67fc141 | 2006-12-01 21:52:58 +0000 | [diff] [blame] | 1182 | if (TII->getOperandConstraint(Opc, j+NumRes, TOI::TIED_TO) != -1) { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1183 | SDNode *DU = SU->Node->getOperand(j).Val; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1184 | SUnit *DUSU = (*SUnitMap)[DU][SU->InstanceNo]; |
Evan Cheng | f24d15f | 2006-11-06 21:33:46 +0000 | [diff] [blame] | 1185 | if (!DUSU) continue; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1186 | for (SUnit::succ_iterator I = DUSU->Succs.begin(),E = DUSU->Succs.end(); |
| 1187 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1188 | if (I->isCtrl) continue; |
| 1189 | SUnit *SuccSU = I->Dep; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1190 | // Don't constraint nodes with implicit defs. It can create cycles |
| 1191 | // plus it may increase register pressures. |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame^] | 1192 | if (SuccSU == SU || SuccSU->hasPhysRegDefs) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1193 | continue; |
| 1194 | // Be conservative. Ignore if nodes aren't at the same depth. |
| 1195 | if (SuccSU->Depth != SU->Depth) |
| 1196 | continue; |
| 1197 | if ((!canClobber(SuccSU, DUSU) || |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame^] | 1198 | (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) || |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1199 | (!SU->isCommutable && SuccSU->isCommutable)) && |
| 1200 | !isReachable(SuccSU, SU)) { |
| 1201 | DOUT << "Adding an edge from SU # " << SU->NodeNum |
| 1202 | << " to SU #" << SuccSU->NodeNum << "\n"; |
| 1203 | SU->addPred(SuccSU, true, true); |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1211 | /// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1212 | /// Smaller number is the higher priority. |
| 1213 | template<class SF> |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1214 | unsigned BURegReductionPriorityQueue<SF>:: |
| 1215 | CalcNodeSethiUllmanNumber(const SUnit *SU) { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1216 | unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1217 | if (SethiUllmanNumber != 0) |
| 1218 | return SethiUllmanNumber; |
| 1219 | |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1220 | unsigned Extra = 0; |
| 1221 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 1222 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1223 | if (I->isCtrl) continue; // ignore chain preds |
| 1224 | SUnit *PredSU = I->Dep; |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1225 | unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU); |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1226 | if (PredSethiUllman > SethiUllmanNumber) { |
| 1227 | SethiUllmanNumber = PredSethiUllman; |
| 1228 | Extra = 0; |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1229 | } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1230 | ++Extra; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1231 | } |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1232 | |
| 1233 | SethiUllmanNumber += Extra; |
| 1234 | |
| 1235 | if (SethiUllmanNumber == 0) |
| 1236 | SethiUllmanNumber = 1; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1237 | |
| 1238 | return SethiUllmanNumber; |
| 1239 | } |
| 1240 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1241 | /// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all |
| 1242 | /// scheduling units. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1243 | template<class SF> |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1244 | void BURegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1245 | SethiUllmanNumbers.assign(SUnits->size(), 0); |
| 1246 | |
| 1247 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1248 | CalcNodeSethiUllmanNumber(&(*SUnits)[i]); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | static unsigned SumOfUnscheduledPredsOfSuccs(const SUnit *SU) { |
| 1252 | unsigned Sum = 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 1253 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1254 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1255 | SUnit *SuccSU = I->Dep; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 1256 | for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(), |
| 1257 | EE = SuccSU->Preds.end(); II != EE; ++II) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1258 | SUnit *PredSU = II->Dep; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1259 | if (!PredSU->isScheduled) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1260 | ++Sum; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | return Sum; |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | // Top down |
| 1269 | 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] | 1270 | unsigned LPriority = SPQ->getNodePriority(left); |
| 1271 | unsigned RPriority = SPQ->getNodePriority(right); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 1272 | bool LIsTarget = left->Node && left->Node->isTargetOpcode(); |
| 1273 | bool RIsTarget = right->Node && right->Node->isTargetOpcode(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1274 | bool LIsFloater = LIsTarget && left->NumPreds == 0; |
| 1275 | bool RIsFloater = RIsTarget && right->NumPreds == 0; |
| 1276 | unsigned LBonus = (SumOfUnscheduledPredsOfSuccs(left) == 1) ? 2 : 0; |
| 1277 | unsigned RBonus = (SumOfUnscheduledPredsOfSuccs(right) == 1) ? 2 : 0; |
| 1278 | |
| 1279 | if (left->NumSuccs == 0 && right->NumSuccs != 0) |
| 1280 | return false; |
| 1281 | else if (left->NumSuccs != 0 && right->NumSuccs == 0) |
| 1282 | return true; |
| 1283 | |
| 1284 | // Special tie breaker: if two nodes share a operand, the one that use it |
| 1285 | // as a def&use operand is preferred. |
| 1286 | if (LIsTarget && RIsTarget) { |
| 1287 | if (left->isTwoAddress && !right->isTwoAddress) { |
| 1288 | SDNode *DUNode = left->Node->getOperand(0).Val; |
| 1289 | if (DUNode->isOperand(right->Node)) |
| 1290 | RBonus += 2; |
| 1291 | } |
| 1292 | if (!left->isTwoAddress && right->isTwoAddress) { |
| 1293 | SDNode *DUNode = right->Node->getOperand(0).Val; |
| 1294 | if (DUNode->isOperand(left->Node)) |
| 1295 | LBonus += 2; |
| 1296 | } |
| 1297 | } |
| 1298 | if (LIsFloater) |
| 1299 | LBonus -= 2; |
| 1300 | if (RIsFloater) |
| 1301 | RBonus -= 2; |
| 1302 | if (left->NumSuccs == 1) |
| 1303 | LBonus += 2; |
| 1304 | if (right->NumSuccs == 1) |
| 1305 | RBonus += 2; |
| 1306 | |
| 1307 | if (LPriority+LBonus < RPriority+RBonus) |
| 1308 | return true; |
| 1309 | else if (LPriority == RPriority) |
| 1310 | if (left->Depth < right->Depth) |
| 1311 | return true; |
| 1312 | else if (left->Depth == right->Depth) |
| 1313 | if (left->NumSuccsLeft > right->NumSuccsLeft) |
| 1314 | return true; |
| 1315 | else if (left->NumSuccsLeft == right->NumSuccsLeft) |
| 1316 | if (left->CycleBound > right->CycleBound) |
| 1317 | return true; |
| 1318 | return false; |
| 1319 | } |
| 1320 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1321 | /// CalcNodeSethiUllmanNumber - Priority is the Sethi Ullman number. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1322 | /// Smaller number is the higher priority. |
| 1323 | template<class SF> |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1324 | unsigned TDRegReductionPriorityQueue<SF>:: |
| 1325 | CalcNodeSethiUllmanNumber(const SUnit *SU) { |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1326 | unsigned &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1327 | if (SethiUllmanNumber != 0) |
| 1328 | return SethiUllmanNumber; |
| 1329 | |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 1330 | unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1331 | if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg) |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1332 | SethiUllmanNumber = 0xffff; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1333 | else if (SU->NumSuccsLeft == 0) |
| 1334 | // If SU does not have a use, i.e. it doesn't produce a value that would |
| 1335 | // be consumed (e.g. store), then it terminates a chain of computation. |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1336 | // Give it a small SethiUllman number so it will be scheduled right before |
| 1337 | // its predecessors that it doesn't lengthen their live ranges. |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1338 | SethiUllmanNumber = 0; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1339 | else if (SU->NumPredsLeft == 0 && |
| 1340 | (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU))) |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1341 | SethiUllmanNumber = 0xffff; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1342 | else { |
| 1343 | int Extra = 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 1344 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 1345 | I != E; ++I) { |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1346 | if (I->isCtrl) continue; // ignore chain preds |
| 1347 | SUnit *PredSU = I->Dep; |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1348 | unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1349 | if (PredSethiUllman > SethiUllmanNumber) { |
| 1350 | SethiUllmanNumber = PredSethiUllman; |
| 1351 | Extra = 0; |
Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1352 | } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1353 | ++Extra; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | SethiUllmanNumber += Extra; |
| 1357 | } |
| 1358 | |
| 1359 | return SethiUllmanNumber; |
| 1360 | } |
| 1361 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1362 | /// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all |
| 1363 | /// scheduling units. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1364 | template<class SF> |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1365 | void TDRegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1366 | SethiUllmanNumbers.assign(SUnits->size(), 0); |
| 1367 | |
| 1368 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1369 | CalcNodeSethiUllmanNumber(&(*SUnits)[i]); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | //===----------------------------------------------------------------------===// |
| 1373 | // Public Constructor Functions |
| 1374 | //===----------------------------------------------------------------------===// |
| 1375 | |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1376 | llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, |
| 1377 | SelectionDAG *DAG, |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1378 | MachineBasicBlock *BB) { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1379 | const TargetInstrInfo *TII = DAG->getTarget().getInstrInfo(); |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1380 | return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), true, |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1381 | new BURegReductionPriorityQueue<bu_ls_rr_sort>(TII)); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1384 | llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, |
| 1385 | SelectionDAG *DAG, |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1386 | MachineBasicBlock *BB) { |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1387 | return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), false, |
Chris Lattner | 296a83c | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1388 | new TDRegReductionPriorityQueue<td_ls_rr_sort>()); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |