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