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