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