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