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