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