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