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