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