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