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