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