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