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