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