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