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