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