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