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