Dan Gohman | 23785a1 | 2008-08-12 17:42:33 +0000 | [diff] [blame] | 1 | //===----- ScheduleDAGRRList.cpp - Reg pressure reduction list scheduler --===// |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements bottom-up and top-down register pressure reduction list |
| 11 | // schedulers, using standard algorithms. The basic approach uses a priority |
| 12 | // queue of available nodes to schedule. One at a time, nodes are taken from |
| 13 | // the priority queue (thus in priority order), checked for legality to |
| 14 | // schedule, and emitted if legal. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "pre-RA-sched" |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/ScheduleDAGSDNodes.h" |
Jim Laskey | 29e635d | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetRegisterInfo.h" |
Owen Anderson | 8c2c1e9 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetInstrInfo.h" |
| 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Dan Gohman | a4db335 | 2008-06-21 18:35:25 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/PriorityQueue.h" |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/STLExtras.h" |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 31 | #include <climits> |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
| 33 | using namespace llvm; |
| 34 | |
Dan Gohman | fd227e9 | 2008-03-25 17:10:29 +0000 | [diff] [blame] | 35 | STATISTIC(NumBacktracks, "Number of times scheduler backtracked"); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 36 | STATISTIC(NumUnfolds, "Number of nodes unfolded"); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 37 | STATISTIC(NumDups, "Number of duplicated nodes"); |
| 38 | STATISTIC(NumCCCopies, "Number of cross class copies"); |
| 39 | |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 40 | static RegisterScheduler |
| 41 | burrListDAGScheduler("list-burr", |
Dan Gohman | 9c4b7d5 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 42 | "Bottom-up register reduction list scheduling", |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 43 | createBURRListDAGScheduler); |
| 44 | static RegisterScheduler |
| 45 | tdrListrDAGScheduler("list-tdrr", |
Dan Gohman | 9c4b7d5 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 46 | "Top-down register reduction list scheduling", |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 47 | createTDRRListDAGScheduler); |
| 48 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 49 | namespace { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | /// ScheduleDAGRRList - The actual register reduction list scheduler |
| 52 | /// implementation. This supports both top-down and bottom-up scheduling. |
| 53 | /// |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 54 | class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAGSDNodes { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 55 | private: |
| 56 | /// isBottomUp - This is true if the scheduling problem is bottom-up, false if |
| 57 | /// it is top-down. |
| 58 | bool isBottomUp; |
Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 59 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 60 | /// AvailableQueue - The priority queue to use for the available SUnits. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 61 | SchedulingPriorityQueue *AvailableQueue; |
| 62 | |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 63 | /// LiveRegDefs - A set of physical registers and their definition |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 64 | /// that are "live". These nodes must be scheduled before any other nodes that |
| 65 | /// modifies the registers can be scheduled. |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 66 | unsigned NumLiveRegs; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 67 | std::vector<SUnit*> LiveRegDefs; |
| 68 | std::vector<unsigned> LiveRegCycles; |
| 69 | |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 70 | /// Topo - A topological ordering for SUnits which permits fast IsReachable |
| 71 | /// and similar queries. |
| 72 | ScheduleDAGTopologicalSort Topo; |
| 73 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 74 | public: |
Dan Gohman | 5a390b9 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 75 | ScheduleDAGRRList(SelectionDAG *dag, MachineBasicBlock *bb, |
Dan Gohman | fd08af4 | 2008-11-20 03:11:19 +0000 | [diff] [blame] | 76 | const TargetMachine &tm, bool isbottomup, |
Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 77 | SchedulingPriorityQueue *availqueue) |
Dan Gohman | fd08af4 | 2008-11-20 03:11:19 +0000 | [diff] [blame] | 78 | : ScheduleDAGSDNodes(dag, bb, tm), isBottomUp(isbottomup), |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 79 | AvailableQueue(availqueue), Topo(SUnits) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ~ScheduleDAGRRList() { |
| 83 | delete AvailableQueue; |
| 84 | } |
| 85 | |
| 86 | void Schedule(); |
| 87 | |
Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 88 | /// IsReachable - Checks if SU is reachable from TargetSU. |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 89 | bool IsReachable(const SUnit *SU, const SUnit *TargetSU) { |
| 90 | return Topo.IsReachable(SU, TargetSU); |
| 91 | } |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 92 | |
| 93 | /// willCreateCycle - Returns true if adding an edge from SU to TargetSU will |
| 94 | /// create a cycle. |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 95 | bool WillCreateCycle(SUnit *SU, SUnit *TargetSU) { |
| 96 | return Topo.WillCreateCycle(SU, TargetSU); |
| 97 | } |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 98 | |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 99 | /// AddPred - adds a predecessor edge to SUnit SU. |
Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 100 | /// This returns true if this is a new predecessor. |
| 101 | /// Updates the topological ordering if required. |
Dan Gohman | 17214e6 | 2008-12-16 01:00:55 +0000 | [diff] [blame] | 102 | void AddPred(SUnit *SU, const SDep &D) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 103 | Topo.AddPred(SU, D.getSUnit()); |
Dan Gohman | 17214e6 | 2008-12-16 01:00:55 +0000 | [diff] [blame] | 104 | SU->addPred(D); |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 105 | } |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 106 | |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 107 | /// RemovePred - removes a predecessor edge from SUnit SU. |
| 108 | /// This returns true if an edge was removed. |
| 109 | /// Updates the topological ordering if required. |
Dan Gohman | 17214e6 | 2008-12-16 01:00:55 +0000 | [diff] [blame] | 110 | void RemovePred(SUnit *SU, const SDep &D) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 111 | Topo.RemovePred(SU, D.getSUnit()); |
Dan Gohman | 17214e6 | 2008-12-16 01:00:55 +0000 | [diff] [blame] | 112 | SU->removePred(D); |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 113 | } |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 114 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 115 | private: |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 116 | void ReleasePred(SUnit *SU, SDep *PredEdge); |
| 117 | void ReleaseSucc(SUnit *SU, SDep *SuccEdge); |
| 118 | void CapturePred(SDep *PredEdge); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 119 | void ScheduleNodeBottomUp(SUnit*, unsigned); |
| 120 | void ScheduleNodeTopDown(SUnit*, unsigned); |
| 121 | void UnscheduleNodeBottomUp(SUnit*); |
| 122 | void BacktrackBottomUp(SUnit*, unsigned, unsigned&); |
| 123 | SUnit *CopyAndMoveSuccessors(SUnit*); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 124 | void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned, |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 125 | const TargetRegisterClass*, |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 126 | const TargetRegisterClass*, |
| 127 | SmallVector<SUnit*, 2>&); |
| 128 | bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 129 | void ListScheduleTopDown(); |
| 130 | void ListScheduleBottomUp(); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it. |
Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 134 | /// Updates the topological ordering if required. |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 135 | SUnit *CreateNewSUnit(SDNode *N) { |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 136 | unsigned NumSUnits = SUnits.size(); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 137 | SUnit *NewNode = NewSUnit(N); |
Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 138 | // Update the topological ordering. |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 139 | if (NewNode->NodeNum >= NumSUnits) |
| 140 | Topo.InitDAGTopologicalSorting(); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 141 | return NewNode; |
| 142 | } |
| 143 | |
Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 144 | /// CreateClone - Creates a new SUnit from an existing one. |
| 145 | /// Updates the topological ordering if required. |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 146 | SUnit *CreateClone(SUnit *N) { |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 147 | unsigned NumSUnits = SUnits.size(); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 148 | SUnit *NewNode = Clone(N); |
Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 149 | // Update the topological ordering. |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 150 | if (NewNode->NodeNum >= NumSUnits) |
| 151 | Topo.InitDAGTopologicalSorting(); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 152 | return NewNode; |
| 153 | } |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 154 | |
| 155 | /// ForceUnitLatencies - Return true, since register-pressure-reducing |
| 156 | /// scheduling doesn't need actual latency information. |
| 157 | bool ForceUnitLatencies() const { return true; } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 158 | }; |
| 159 | } // end anonymous namespace |
| 160 | |
| 161 | |
| 162 | /// Schedule - Schedule the DAG using list scheduling. |
| 163 | void ScheduleDAGRRList::Schedule() { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 164 | DOUT << "********** List Scheduling **********\n"; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 165 | |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 166 | NumLiveRegs = 0; |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 167 | LiveRegDefs.resize(TRI->getNumRegs(), NULL); |
| 168 | LiveRegCycles.resize(TRI->getNumRegs(), 0); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 169 | |
Dan Gohman | 04543e7 | 2008-12-23 18:36:58 +0000 | [diff] [blame] | 170 | // Build the scheduling graph. |
| 171 | BuildSchedGraph(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 172 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 173 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
Dan Gohman | 22d07b1 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 174 | SUnits[su].dumpAll(this)); |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 175 | Topo.InitDAGTopologicalSorting(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 176 | |
Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 177 | AvailableQueue->initNodes(SUnits); |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 178 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 179 | // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate. |
| 180 | if (isBottomUp) |
| 181 | ListScheduleBottomUp(); |
| 182 | else |
| 183 | ListScheduleTopDown(); |
| 184 | |
| 185 | AvailableQueue->releaseState(); |
Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 186 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 187 | |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | // Bottom-Up Scheduling |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 192 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 193 | /// the AvailableQueue if the count reaches zero. Also update its cycle bound. |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 194 | void ScheduleDAGRRList::ReleasePred(SUnit *SU, SDep *PredEdge) { |
| 195 | SUnit *PredSU = PredEdge->getSUnit(); |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 196 | --PredSU->NumSuccsLeft; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 197 | |
| 198 | #ifndef NDEBUG |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 199 | if (PredSU->NumSuccsLeft < 0) { |
Dan Gohman | 5ebdb98 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 200 | cerr << "*** Scheduling failed! ***\n"; |
Dan Gohman | 22d07b1 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 201 | PredSU->dump(this); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 202 | cerr << " has been released too many times!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 203 | assert(0); |
| 204 | } |
| 205 | #endif |
| 206 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 207 | if (PredSU->NumSuccsLeft == 0) { |
Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 208 | PredSU->isAvailable = true; |
| 209 | AvailableQueue->push(PredSU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending |
| 214 | /// count of its predecessors. If a predecessor pending count is zero, add it to |
| 215 | /// the Available queue. |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 216 | void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 217 | DOUT << "*** Scheduling [" << CurCycle << "]: "; |
Dan Gohman | 22d07b1 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 218 | DEBUG(SU->dump(this)); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 219 | |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 220 | assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!"); |
| 221 | SU->setHeightToAtLeast(CurCycle); |
Dan Gohman | 6e58726 | 2008-11-18 21:22:20 +0000 | [diff] [blame] | 222 | Sequence.push_back(SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 223 | |
| 224 | // Bottom up: release predecessors |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 225 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 226 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 227 | ReleasePred(SU, &*I); |
| 228 | if (I->isAssignedRegDep()) { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 229 | // This is a physical register dependency and it's impossible or |
| 230 | // expensive to copy the register. Make sure nothing that can |
| 231 | // clobber the register is scheduled between the predecessor and |
| 232 | // this node. |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 233 | if (!LiveRegDefs[I->getReg()]) { |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 234 | ++NumLiveRegs; |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 235 | LiveRegDefs[I->getReg()] = I->getSUnit(); |
| 236 | LiveRegCycles[I->getReg()] = CurCycle; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Release all the implicit physical register defs that are live. |
| 242 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 243 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 244 | if (I->isAssignedRegDep()) { |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 245 | if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) { |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 246 | assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!"); |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 247 | assert(LiveRegDefs[I->getReg()] == SU && |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 248 | "Physical register dependency violated?"); |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 249 | --NumLiveRegs; |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 250 | LiveRegDefs[I->getReg()] = NULL; |
| 251 | LiveRegCycles[I->getReg()] = 0; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 256 | SU->isScheduled = true; |
Dan Gohman | 6e58726 | 2008-11-18 21:22:20 +0000 | [diff] [blame] | 257 | AvailableQueue->ScheduledNode(SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 260 | /// CapturePred - This does the opposite of ReleasePred. Since SU is being |
| 261 | /// unscheduled, incrcease the succ left count of its predecessors. Remove |
| 262 | /// them from AvailableQueue if necessary. |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 263 | void ScheduleDAGRRList::CapturePred(SDep *PredEdge) { |
| 264 | SUnit *PredSU = PredEdge->getSUnit(); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 265 | if (PredSU->isAvailable) { |
| 266 | PredSU->isAvailable = false; |
| 267 | if (!PredSU->isPending) |
| 268 | AvailableQueue->remove(PredSU); |
| 269 | } |
| 270 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 271 | ++PredSU->NumSuccsLeft; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and |
| 275 | /// its predecessor states to reflect the change. |
| 276 | void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) { |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 277 | DOUT << "*** Unscheduling [" << SU->getHeight() << "]: "; |
Dan Gohman | 22d07b1 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 278 | DEBUG(SU->dump(this)); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 279 | |
| 280 | AvailableQueue->UnscheduledNode(SU); |
| 281 | |
| 282 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 283 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 284 | CapturePred(&*I); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 285 | if (I->isAssignedRegDep() && SU->getHeight() == LiveRegCycles[I->getReg()]) { |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 286 | assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!"); |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 287 | assert(LiveRegDefs[I->getReg()] == I->getSUnit() && |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 288 | "Physical register dependency violated?"); |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 289 | --NumLiveRegs; |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 290 | LiveRegDefs[I->getReg()] = NULL; |
| 291 | LiveRegCycles[I->getReg()] = 0; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 296 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 297 | if (I->isAssignedRegDep()) { |
| 298 | if (!LiveRegDefs[I->getReg()]) { |
| 299 | LiveRegDefs[I->getReg()] = SU; |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 300 | ++NumLiveRegs; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 301 | } |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 302 | if (I->getSUnit()->getHeight() < LiveRegCycles[I->getReg()]) |
| 303 | LiveRegCycles[I->getReg()] = I->getSUnit()->getHeight(); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 307 | SU->setHeightDirty(); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 308 | SU->isScheduled = false; |
| 309 | SU->isAvailable = true; |
| 310 | AvailableQueue->push(SU); |
| 311 | } |
| 312 | |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 313 | /// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 314 | /// BTCycle in order to schedule a specific node. Returns the last unscheduled |
| 315 | /// SUnit. Also returns if a successor is unscheduled in the process. |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 316 | void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle, |
| 317 | unsigned &CurCycle) { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 318 | SUnit *OldSU = NULL; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 319 | while (CurCycle > BtCycle) { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 320 | OldSU = Sequence.back(); |
| 321 | Sequence.pop_back(); |
| 322 | if (SU->isSucc(OldSU)) |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 323 | // Don't try to remove SU from AvailableQueue. |
| 324 | SU->isAvailable = false; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 325 | UnscheduleNodeBottomUp(OldSU); |
| 326 | --CurCycle; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | if (SU->isSucc(OldSU)) { |
| 331 | assert(false && "Something is wrong!"); |
| 332 | abort(); |
| 333 | } |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 334 | |
| 335 | ++NumBacktracks; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 338 | /// CopyAndMoveSuccessors - Clone the specified node and move its scheduled |
| 339 | /// successors to the newly created node. |
| 340 | SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) { |
Dan Gohman | 072734e | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 341 | if (SU->getNode()->getFlaggedNode()) |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 342 | return NULL; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 343 | |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 344 | SDNode *N = SU->getNode(); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 345 | if (!N) |
| 346 | return NULL; |
| 347 | |
| 348 | SUnit *NewSU; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 349 | bool TryUnfold = false; |
Evan Cheng | 84d0ebc | 2007-10-05 01:42:35 +0000 | [diff] [blame] | 350 | for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) { |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 351 | MVT VT = N->getValueType(i); |
Evan Cheng | 84d0ebc | 2007-10-05 01:42:35 +0000 | [diff] [blame] | 352 | if (VT == MVT::Flag) |
| 353 | return NULL; |
| 354 | else if (VT == MVT::Other) |
| 355 | TryUnfold = true; |
| 356 | } |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 357 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 358 | const SDValue &Op = N->getOperand(i); |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 359 | MVT VT = Op.getNode()->getValueType(Op.getResNo()); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 360 | if (VT == MVT::Flag) |
| 361 | return NULL; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | if (TryUnfold) { |
Dan Gohman | e6e1348 | 2008-06-21 15:52:51 +0000 | [diff] [blame] | 365 | SmallVector<SDNode*, 2> NewNodes; |
Dan Gohman | 5a390b9 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 366 | if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes)) |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 367 | return NULL; |
| 368 | |
| 369 | DOUT << "Unfolding SU # " << SU->NodeNum << "\n"; |
| 370 | assert(NewNodes.size() == 2 && "Expected a load folding node!"); |
| 371 | |
| 372 | N = NewNodes[1]; |
| 373 | SDNode *LoadNode = NewNodes[0]; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 374 | unsigned NumVals = N->getNumValues(); |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 375 | unsigned OldNumVals = SU->getNode()->getNumValues(); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 376 | for (unsigned i = 0; i != NumVals; ++i) |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 377 | DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i)); |
| 378 | DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1), |
Dan Gohman | 5a390b9 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 379 | SDValue(LoadNode, 1)); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 380 | |
Dan Gohman | e52e089 | 2008-11-11 21:34:44 +0000 | [diff] [blame] | 381 | // LoadNode may already exist. This can happen when there is another |
| 382 | // load from the same location and producing the same type of value |
| 383 | // but it has different alignment or volatileness. |
| 384 | bool isNewLoad = true; |
| 385 | SUnit *LoadSU; |
| 386 | if (LoadNode->getNodeId() != -1) { |
| 387 | LoadSU = &SUnits[LoadNode->getNodeId()]; |
| 388 | isNewLoad = false; |
| 389 | } else { |
| 390 | LoadSU = CreateNewSUnit(LoadNode); |
| 391 | LoadNode->setNodeId(LoadSU->NodeNum); |
Dan Gohman | e52e089 | 2008-11-11 21:34:44 +0000 | [diff] [blame] | 392 | ComputeLatency(LoadSU); |
| 393 | } |
| 394 | |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 395 | SUnit *NewSU = CreateNewSUnit(N); |
Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 396 | assert(N->getNodeId() == -1 && "Node already inserted!"); |
| 397 | N->setNodeId(NewSU->NodeNum); |
Dan Gohman | e6e1348 | 2008-06-21 15:52:51 +0000 | [diff] [blame] | 398 | |
Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 399 | const TargetInstrDesc &TID = TII->get(N->getMachineOpcode()); |
Dan Gohman | 856c012 | 2008-02-16 00:25:40 +0000 | [diff] [blame] | 400 | for (unsigned i = 0; i != TID.getNumOperands(); ++i) { |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 401 | if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) { |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 402 | NewSU->isTwoAddress = true; |
| 403 | break; |
| 404 | } |
| 405 | } |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 406 | if (TID.isCommutable()) |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 407 | NewSU->isCommutable = true; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 408 | ComputeLatency(NewSU); |
| 409 | |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 410 | SDep ChainPred; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 411 | SmallVector<SDep, 4> ChainSuccs; |
| 412 | SmallVector<SDep, 4> LoadPreds; |
| 413 | SmallVector<SDep, 4> NodePreds; |
| 414 | SmallVector<SDep, 4> NodeSuccs; |
| 415 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 416 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 417 | if (I->isCtrl()) |
| 418 | ChainPred = *I; |
| 419 | else if (I->getSUnit()->getNode() && |
| 420 | I->getSUnit()->getNode()->isOperandOf(LoadNode)) |
| 421 | LoadPreds.push_back(*I); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 422 | else |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 423 | NodePreds.push_back(*I); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 424 | } |
| 425 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 426 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 427 | if (I->isCtrl()) |
| 428 | ChainSuccs.push_back(*I); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 429 | else |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 430 | NodeSuccs.push_back(*I); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 433 | if (ChainPred.getSUnit()) { |
| 434 | RemovePred(SU, ChainPred); |
Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 435 | if (isNewLoad) |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 436 | AddPred(LoadSU, ChainPred); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 437 | } |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 438 | for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 439 | const SDep &Pred = LoadPreds[i]; |
| 440 | RemovePred(SU, Pred); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 441 | if (isNewLoad) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 442 | AddPred(LoadSU, Pred); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 443 | } |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 444 | } |
| 445 | for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 446 | const SDep &Pred = NodePreds[i]; |
| 447 | RemovePred(SU, Pred); |
| 448 | AddPred(NewSU, Pred); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 449 | } |
| 450 | for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 451 | SDep D = NodeSuccs[i]; |
| 452 | SUnit *SuccDep = D.getSUnit(); |
| 453 | D.setSUnit(SU); |
| 454 | RemovePred(SuccDep, D); |
| 455 | D.setSUnit(NewSU); |
| 456 | AddPred(SuccDep, D); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 457 | } |
| 458 | for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 459 | SDep D = ChainSuccs[i]; |
| 460 | SUnit *SuccDep = D.getSUnit(); |
| 461 | D.setSUnit(SU); |
| 462 | RemovePred(SuccDep, D); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 463 | if (isNewLoad) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 464 | D.setSUnit(LoadSU); |
| 465 | AddPred(SuccDep, D); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 466 | } |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 467 | } |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 468 | if (isNewLoad) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 469 | AddPred(NewSU, SDep(LoadSU, SDep::Order, LoadSU->Latency)); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 470 | } |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 471 | |
Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 472 | if (isNewLoad) |
| 473 | AvailableQueue->addNode(LoadSU); |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 474 | AvailableQueue->addNode(NewSU); |
| 475 | |
| 476 | ++NumUnfolds; |
| 477 | |
| 478 | if (NewSU->NumSuccsLeft == 0) { |
| 479 | NewSU->isAvailable = true; |
| 480 | return NewSU; |
Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 481 | } |
| 482 | SU = NewSU; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | DOUT << "Duplicating SU # " << SU->NodeNum << "\n"; |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 486 | NewSU = CreateClone(SU); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 487 | |
| 488 | // New SUnit has the exact same predecessors. |
| 489 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 490 | I != E; ++I) |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 491 | if (!I->isArtificial()) |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 492 | AddPred(NewSU, *I); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 493 | |
| 494 | // Only copy scheduled successors. Cut them from old node's successor |
| 495 | // list and move them over. |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 496 | SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 497 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 498 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 499 | if (I->isArtificial()) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 500 | continue; |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 501 | SUnit *SuccSU = I->getSUnit(); |
| 502 | if (SuccSU->isScheduled) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 503 | SDep D = *I; |
| 504 | D.setSUnit(NewSU); |
| 505 | AddPred(SuccSU, D); |
| 506 | D.setSUnit(SU); |
| 507 | DelDeps.push_back(std::make_pair(SuccSU, D)); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 510 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 511 | RemovePred(DelDeps[i].first, DelDeps[i].second); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 512 | |
| 513 | AvailableQueue->updateNode(SU); |
| 514 | AvailableQueue->addNode(NewSU); |
| 515 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 516 | ++NumDups; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 517 | return NewSU; |
| 518 | } |
| 519 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 520 | /// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies |
| 521 | /// and move all scheduled successors of the given SUnit to the last copy. |
| 522 | void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg, |
| 523 | const TargetRegisterClass *DestRC, |
| 524 | const TargetRegisterClass *SrcRC, |
| 525 | SmallVector<SUnit*, 2> &Copies) { |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 526 | SUnit *CopyFromSU = CreateNewSUnit(NULL); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 527 | CopyFromSU->CopySrcRC = SrcRC; |
| 528 | CopyFromSU->CopyDstRC = DestRC; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 529 | |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 530 | SUnit *CopyToSU = CreateNewSUnit(NULL); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 531 | CopyToSU->CopySrcRC = DestRC; |
| 532 | CopyToSU->CopyDstRC = SrcRC; |
| 533 | |
| 534 | // Only copy scheduled successors. Cut them from old node's successor |
| 535 | // list and move them over. |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 536 | SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 537 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 538 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 539 | if (I->isArtificial()) |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 540 | continue; |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 541 | SUnit *SuccSU = I->getSUnit(); |
| 542 | if (SuccSU->isScheduled) { |
| 543 | SDep D = *I; |
| 544 | D.setSUnit(CopyToSU); |
| 545 | AddPred(SuccSU, D); |
| 546 | DelDeps.push_back(std::make_pair(SuccSU, *I)); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 550 | RemovePred(DelDeps[i].first, DelDeps[i].second); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 553 | AddPred(CopyFromSU, SDep(SU, SDep::Data, SU->Latency, Reg)); |
| 554 | AddPred(CopyToSU, SDep(CopyFromSU, SDep::Data, CopyFromSU->Latency, 0)); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 555 | |
| 556 | AvailableQueue->updateNode(SU); |
| 557 | AvailableQueue->addNode(CopyFromSU); |
| 558 | AvailableQueue->addNode(CopyToSU); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 559 | Copies.push_back(CopyFromSU); |
| 560 | Copies.push_back(CopyToSU); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 561 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 562 | ++NumCCCopies; |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /// getPhysicalRegisterVT - Returns the ValueType of the physical register |
| 566 | /// definition of the specified node. |
| 567 | /// FIXME: Move to SelectionDAG? |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 568 | static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg, |
| 569 | const TargetInstrInfo *TII) { |
Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 570 | const TargetInstrDesc &TID = TII->get(N->getMachineOpcode()); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 571 | assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!"); |
Chris Lattner | b0d06b4 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 572 | unsigned NumRes = TID.getNumDefs(); |
| 573 | for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) { |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 574 | if (Reg == *ImpDef) |
| 575 | break; |
| 576 | ++NumRes; |
| 577 | } |
| 578 | return N->getValueType(NumRes); |
| 579 | } |
| 580 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 581 | /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay |
| 582 | /// scheduling of the given node to satisfy live physical register dependencies. |
| 583 | /// If the specific node is the last one that's available to schedule, do |
| 584 | /// whatever is necessary (i.e. backtracking or cloning) to make it possible. |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 585 | bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU, |
| 586 | SmallVector<unsigned, 4> &LRegs){ |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 587 | if (NumLiveRegs == 0) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 588 | return false; |
| 589 | |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 590 | SmallSet<unsigned, 4> RegAdded; |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 591 | // If this node would clobber any "live" register, then it's not ready. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 592 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 593 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 594 | if (I->isAssignedRegDep()) { |
| 595 | unsigned Reg = I->getReg(); |
| 596 | if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->getSUnit()) { |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 597 | if (RegAdded.insert(Reg)) |
| 598 | LRegs.push_back(Reg); |
| 599 | } |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 600 | for (const unsigned *Alias = TRI->getAliasSet(Reg); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 601 | *Alias; ++Alias) |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 602 | if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->getSUnit()) { |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 603 | if (RegAdded.insert(*Alias)) |
| 604 | LRegs.push_back(*Alias); |
| 605 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Dan Gohman | 072734e | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 609 | for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) { |
| 610 | if (!Node->isMachineOpcode()) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 611 | continue; |
Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 612 | const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode()); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 613 | if (!TID.ImplicitDefs) |
| 614 | continue; |
| 615 | for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) { |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 616 | if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) { |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 617 | if (RegAdded.insert(*Reg)) |
| 618 | LRegs.push_back(*Reg); |
| 619 | } |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 620 | for (const unsigned *Alias = TRI->getAliasSet(*Reg); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 621 | *Alias; ++Alias) |
Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 622 | if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) { |
Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 623 | if (RegAdded.insert(*Alias)) |
| 624 | LRegs.push_back(*Alias); |
| 625 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 628 | return !LRegs.empty(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 631 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 632 | /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up |
| 633 | /// schedulers. |
| 634 | void ScheduleDAGRRList::ListScheduleBottomUp() { |
| 635 | unsigned CurCycle = 0; |
| 636 | // Add root to Available queue. |
Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 637 | if (!SUnits.empty()) { |
Dan Gohman | 5a390b9 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 638 | SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()]; |
Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 639 | assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!"); |
| 640 | RootSU->isAvailable = true; |
| 641 | AvailableQueue->push(RootSU); |
| 642 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 643 | |
| 644 | // While Available queue is not empty, grab the node with the highest |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 645 | // priority. If it is not ready put it back. Schedule the node. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 646 | SmallVector<SUnit*, 4> NotReady; |
Dan Gohman | fa63cc4 | 2008-06-23 21:15:00 +0000 | [diff] [blame] | 647 | DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap; |
Dan Gohman | e6e1348 | 2008-06-21 15:52:51 +0000 | [diff] [blame] | 648 | Sequence.reserve(SUnits.size()); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 649 | while (!AvailableQueue->empty()) { |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 650 | bool Delayed = false; |
Dan Gohman | fa63cc4 | 2008-06-23 21:15:00 +0000 | [diff] [blame] | 651 | LRegsMap.clear(); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 652 | SUnit *CurSU = AvailableQueue->pop(); |
| 653 | while (CurSU) { |
Dan Gohman | 63be531 | 2008-11-21 01:30:54 +0000 | [diff] [blame] | 654 | SmallVector<unsigned, 4> LRegs; |
| 655 | if (!DelayForLiveRegsBottomUp(CurSU, LRegs)) |
| 656 | break; |
| 657 | Delayed = true; |
| 658 | LRegsMap.insert(std::make_pair(CurSU, LRegs)); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 659 | |
| 660 | CurSU->isPending = true; // This SU is not in AvailableQueue right now. |
| 661 | NotReady.push_back(CurSU); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 662 | CurSU = AvailableQueue->pop(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 663 | } |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 664 | |
| 665 | // All candidates are delayed due to live physical reg dependencies. |
| 666 | // Try backtracking, code duplication, or inserting cross class copies |
| 667 | // to resolve it. |
| 668 | if (Delayed && !CurSU) { |
| 669 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { |
| 670 | SUnit *TrySU = NotReady[i]; |
| 671 | SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU]; |
| 672 | |
| 673 | // Try unscheduling up to the point where it's safe to schedule |
| 674 | // this node. |
| 675 | unsigned LiveCycle = CurCycle; |
| 676 | for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) { |
| 677 | unsigned Reg = LRegs[j]; |
| 678 | unsigned LCycle = LiveRegCycles[Reg]; |
| 679 | LiveCycle = std::min(LiveCycle, LCycle); |
| 680 | } |
| 681 | SUnit *OldSU = Sequence[LiveCycle]; |
| 682 | if (!WillCreateCycle(TrySU, OldSU)) { |
| 683 | BacktrackBottomUp(TrySU, LiveCycle, CurCycle); |
| 684 | // Force the current node to be scheduled before the node that |
| 685 | // requires the physical reg dep. |
| 686 | if (OldSU->isAvailable) { |
| 687 | OldSU->isAvailable = false; |
| 688 | AvailableQueue->remove(OldSU); |
| 689 | } |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 690 | AddPred(TrySU, SDep(OldSU, SDep::Order, /*Latency=*/1, |
| 691 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 692 | /*isMustAlias=*/false, /*isArtificial=*/true)); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 693 | // If one or more successors has been unscheduled, then the current |
| 694 | // node is no longer avaialable. Schedule a successor that's now |
| 695 | // available instead. |
| 696 | if (!TrySU->isAvailable) |
| 697 | CurSU = AvailableQueue->pop(); |
| 698 | else { |
| 699 | CurSU = TrySU; |
| 700 | TrySU->isPending = false; |
| 701 | NotReady.erase(NotReady.begin()+i); |
| 702 | } |
| 703 | break; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | if (!CurSU) { |
Dan Gohman | fd227e9 | 2008-03-25 17:10:29 +0000 | [diff] [blame] | 708 | // Can't backtrack. Try duplicating the nodes that produces these |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 709 | // "expensive to copy" values to break the dependency. In case even |
| 710 | // that doesn't work, insert cross class copies. |
| 711 | SUnit *TrySU = NotReady[0]; |
| 712 | SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU]; |
| 713 | assert(LRegs.size() == 1 && "Can't handle this yet!"); |
| 714 | unsigned Reg = LRegs[0]; |
| 715 | SUnit *LRDef = LiveRegDefs[Reg]; |
Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 716 | SUnit *NewDef = CopyAndMoveSuccessors(LRDef); |
| 717 | if (!NewDef) { |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 718 | // Issue expensive cross register class copies. |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 719 | MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 720 | const TargetRegisterClass *RC = |
Evan Cheng | e88a625 | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 721 | TRI->getPhysicalRegisterRegClass(Reg, VT); |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 722 | const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 723 | if (!DestRC) { |
| 724 | assert(false && "Don't know how to copy this physical register!"); |
| 725 | abort(); |
| 726 | } |
| 727 | SmallVector<SUnit*, 2> Copies; |
| 728 | InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies); |
| 729 | DOUT << "Adding an edge from SU # " << TrySU->NodeNum |
| 730 | << " to SU #" << Copies.front()->NodeNum << "\n"; |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 731 | AddPred(TrySU, SDep(Copies.front(), SDep::Order, /*Latency=*/1, |
Dan Gohman | bf8e520 | 2009-01-06 01:28:56 +0000 | [diff] [blame] | 732 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 733 | /*isMustAlias=*/false, |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 734 | /*isArtificial=*/true)); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 735 | NewDef = Copies.back(); |
| 736 | } |
| 737 | |
| 738 | DOUT << "Adding an edge from SU # " << NewDef->NodeNum |
| 739 | << " to SU #" << TrySU->NodeNum << "\n"; |
| 740 | LiveRegDefs[Reg] = NewDef; |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 741 | AddPred(NewDef, SDep(TrySU, SDep::Order, /*Latency=*/1, |
Dan Gohman | bf8e520 | 2009-01-06 01:28:56 +0000 | [diff] [blame] | 742 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 743 | /*isMustAlias=*/false, |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 744 | /*isArtificial=*/true)); |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 745 | TrySU->isAvailable = false; |
| 746 | CurSU = NewDef; |
| 747 | } |
| 748 | |
| 749 | if (!CurSU) { |
| 750 | assert(false && "Unable to resolve live physical register dependencies!"); |
| 751 | abort(); |
| 752 | } |
| 753 | } |
| 754 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 755 | // Add the nodes that aren't ready back onto the available list. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 756 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { |
| 757 | NotReady[i]->isPending = false; |
Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 758 | // May no longer be available due to backtracking. |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 759 | if (NotReady[i]->isAvailable) |
| 760 | AvailableQueue->push(NotReady[i]); |
| 761 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 762 | NotReady.clear(); |
| 763 | |
Dan Gohman | c602dd4 | 2008-11-21 00:10:42 +0000 | [diff] [blame] | 764 | if (CurSU) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 765 | ScheduleNodeBottomUp(CurSU, CurCycle); |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 766 | ++CurCycle; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 769 | // Reverse the order if it is bottom up. |
| 770 | std::reverse(Sequence.begin(), Sequence.end()); |
| 771 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 772 | #ifndef NDEBUG |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 773 | VerifySchedule(isBottomUp); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 774 | #endif |
| 775 | } |
| 776 | |
| 777 | //===----------------------------------------------------------------------===// |
| 778 | // Top-Down Scheduling |
| 779 | //===----------------------------------------------------------------------===// |
| 780 | |
| 781 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 782 | /// the AvailableQueue if the count reaches zero. Also update its cycle bound. |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 783 | void ScheduleDAGRRList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { |
| 784 | SUnit *SuccSU = SuccEdge->getSUnit(); |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 785 | --SuccSU->NumPredsLeft; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 786 | |
| 787 | #ifndef NDEBUG |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 788 | if (SuccSU->NumPredsLeft < 0) { |
Dan Gohman | 5ebdb98 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 789 | cerr << "*** Scheduling failed! ***\n"; |
Dan Gohman | 22d07b1 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 790 | SuccSU->dump(this); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 791 | cerr << " has been released too many times!\n"; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 792 | assert(0); |
| 793 | } |
| 794 | #endif |
| 795 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 796 | if (SuccSU->NumPredsLeft == 0) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 797 | SuccSU->isAvailable = true; |
| 798 | AvailableQueue->push(SuccSU); |
| 799 | } |
| 800 | } |
| 801 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 802 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 803 | /// count of its successors. If a successor pending count is zero, add it to |
| 804 | /// the Available queue. |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 805 | void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 806 | DOUT << "*** Scheduling [" << CurCycle << "]: "; |
Dan Gohman | 22d07b1 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 807 | DEBUG(SU->dump(this)); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 808 | |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 809 | assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!"); |
| 810 | SU->setDepthToAtLeast(CurCycle); |
Dan Gohman | 92a36d7 | 2008-11-17 21:31:02 +0000 | [diff] [blame] | 811 | Sequence.push_back(SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 812 | |
| 813 | // Top down: release successors |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 814 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 815 | I != E; ++I) |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 816 | ReleaseSucc(SU, &*I); |
Dan Gohman | 92a36d7 | 2008-11-17 21:31:02 +0000 | [diff] [blame] | 817 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 818 | SU->isScheduled = true; |
Dan Gohman | 92a36d7 | 2008-11-17 21:31:02 +0000 | [diff] [blame] | 819 | AvailableQueue->ScheduledNode(SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 822 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 823 | /// schedulers. |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 824 | void ScheduleDAGRRList::ListScheduleTopDown() { |
| 825 | unsigned CurCycle = 0; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 826 | |
| 827 | // All leaves to Available queue. |
| 828 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 829 | // It is available if it has no predecessors. |
Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 830 | if (SUnits[i].Preds.empty()) { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 831 | AvailableQueue->push(&SUnits[i]); |
| 832 | SUnits[i].isAvailable = true; |
| 833 | } |
| 834 | } |
| 835 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 836 | // While Available queue is not empty, grab the node with the highest |
Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 837 | // priority. If it is not ready put it back. Schedule the node. |
Dan Gohman | e6e1348 | 2008-06-21 15:52:51 +0000 | [diff] [blame] | 838 | Sequence.reserve(SUnits.size()); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 839 | while (!AvailableQueue->empty()) { |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 840 | SUnit *CurSU = AvailableQueue->pop(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 841 | |
Dan Gohman | c602dd4 | 2008-11-21 00:10:42 +0000 | [diff] [blame] | 842 | if (CurSU) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 843 | ScheduleNodeTopDown(CurSU, CurCycle); |
Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 844 | ++CurCycle; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 847 | #ifndef NDEBUG |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 848 | VerifySchedule(isBottomUp); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 849 | #endif |
| 850 | } |
| 851 | |
| 852 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 853 | //===----------------------------------------------------------------------===// |
| 854 | // RegReductionPriorityQueue Implementation |
| 855 | //===----------------------------------------------------------------------===// |
| 856 | // |
| 857 | // This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers |
| 858 | // to reduce register pressure. |
| 859 | // |
| 860 | namespace { |
| 861 | template<class SF> |
| 862 | class RegReductionPriorityQueue; |
| 863 | |
| 864 | /// Sorting functions for the Available queue. |
| 865 | struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 866 | RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ; |
| 867 | bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {} |
| 868 | bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} |
| 869 | |
| 870 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 871 | }; |
| 872 | |
| 873 | struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 874 | RegReductionPriorityQueue<td_ls_rr_sort> *SPQ; |
| 875 | td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {} |
| 876 | td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} |
| 877 | |
| 878 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 879 | }; |
| 880 | } // end anonymous namespace |
| 881 | |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 882 | static inline bool isCopyFromLiveIn(const SUnit *SU) { |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 883 | SDNode *N = SU->getNode(); |
Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 884 | return N && N->getOpcode() == ISD::CopyFromReg && |
Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 885 | N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag; |
| 886 | } |
| 887 | |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 888 | /// CalcNodeSethiUllmanNumber - Compute Sethi Ullman number. |
| 889 | /// Smaller number is the higher priority. |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 890 | static unsigned |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 891 | CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) { |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 892 | unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum]; |
| 893 | if (SethiUllmanNumber != 0) |
| 894 | return SethiUllmanNumber; |
| 895 | |
| 896 | unsigned Extra = 0; |
| 897 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 898 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 899 | if (I->isCtrl()) continue; // ignore chain preds |
| 900 | SUnit *PredSU = I->getSUnit(); |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 901 | unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers); |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 902 | if (PredSethiUllman > SethiUllmanNumber) { |
| 903 | SethiUllmanNumber = PredSethiUllman; |
| 904 | Extra = 0; |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 905 | } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl()) |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 906 | ++Extra; |
| 907 | } |
| 908 | |
| 909 | SethiUllmanNumber += Extra; |
| 910 | |
| 911 | if (SethiUllmanNumber == 0) |
| 912 | SethiUllmanNumber = 1; |
| 913 | |
| 914 | return SethiUllmanNumber; |
| 915 | } |
| 916 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 917 | namespace { |
| 918 | template<class SF> |
Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 919 | class VISIBILITY_HIDDEN RegReductionPriorityQueue |
| 920 | : public SchedulingPriorityQueue { |
Dan Gohman | a4db335 | 2008-06-21 18:35:25 +0000 | [diff] [blame] | 921 | PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue; |
Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 922 | unsigned currentQueueId; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 923 | |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 924 | protected: |
| 925 | // SUnits - The SUnits for the current graph. |
| 926 | std::vector<SUnit> *SUnits; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 927 | |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 928 | const TargetInstrInfo *TII; |
| 929 | const TargetRegisterInfo *TRI; |
| 930 | ScheduleDAGRRList *scheduleDAG; |
| 931 | |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 932 | // SethiUllmanNumbers - The SethiUllman number for each node. |
| 933 | std::vector<unsigned> SethiUllmanNumbers; |
| 934 | |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 935 | public: |
| 936 | RegReductionPriorityQueue(const TargetInstrInfo *tii, |
| 937 | const TargetRegisterInfo *tri) : |
| 938 | Queue(SF(this)), currentQueueId(0), |
| 939 | TII(tii), TRI(tri), scheduleDAG(NULL) {} |
| 940 | |
| 941 | void initNodes(std::vector<SUnit> &sunits) { |
| 942 | SUnits = &sunits; |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 943 | // Add pseudo dependency edges for two-address nodes. |
| 944 | AddPseudoTwoAddrDeps(); |
| 945 | // Calculate node priorities. |
| 946 | CalculateSethiUllmanNumbers(); |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 947 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 948 | |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 949 | void addNode(const SUnit *SU) { |
| 950 | unsigned SUSize = SethiUllmanNumbers.size(); |
| 951 | if (SUnits->size() > SUSize) |
| 952 | SethiUllmanNumbers.resize(SUSize*2, 0); |
| 953 | CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers); |
| 954 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 955 | |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 956 | void updateNode(const SUnit *SU) { |
| 957 | SethiUllmanNumbers[SU->NodeNum] = 0; |
| 958 | CalcNodeSethiUllmanNumber(SU, SethiUllmanNumbers); |
| 959 | } |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 960 | |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 961 | void releaseState() { |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 962 | SUnits = 0; |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 963 | SethiUllmanNumbers.clear(); |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 964 | } |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 965 | |
| 966 | unsigned getNodePriority(const SUnit *SU) const { |
| 967 | assert(SU->NodeNum < SethiUllmanNumbers.size()); |
| 968 | unsigned Opc = SU->getNode() ? SU->getNode()->getOpcode() : 0; |
| 969 | if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU)) |
| 970 | // CopyFromReg should be close to its def because it restricts |
| 971 | // allocation choices. But if it is a livein then perhaps we want it |
| 972 | // closer to its uses so it can be coalesced. |
| 973 | return 0xffff; |
Dan Gohman | 261ee6b | 2009-01-07 22:30:55 +0000 | [diff] [blame^] | 974 | if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg) |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 975 | // CopyToReg should be close to its uses to facilitate coalescing and |
| 976 | // avoid spilling. |
| 977 | return 0; |
Dan Gohman | 261ee6b | 2009-01-07 22:30:55 +0000 | [diff] [blame^] | 978 | if (Opc == TargetInstrInfo::EXTRACT_SUBREG || |
| 979 | Opc == TargetInstrInfo::INSERT_SUBREG) |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 980 | // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to |
| 981 | // facilitate coalescing. |
| 982 | return 0; |
Dan Gohman | 261ee6b | 2009-01-07 22:30:55 +0000 | [diff] [blame^] | 983 | if (SU->NumSuccs == 0) |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 984 | // If SU does not have a use, i.e. it doesn't produce a value that would |
| 985 | // be consumed (e.g. store), then it terminates a chain of computation. |
| 986 | // Give it a large SethiUllman number so it will be scheduled right |
| 987 | // before its predecessors that it doesn't lengthen their live ranges. |
| 988 | return 0xffff; |
Dan Gohman | 261ee6b | 2009-01-07 22:30:55 +0000 | [diff] [blame^] | 989 | if (SU->NumPreds == 0) |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 990 | // If SU does not have a def, schedule it close to its uses because it |
| 991 | // does not lengthen any live ranges. |
| 992 | return 0; |
Dan Gohman | 261ee6b | 2009-01-07 22:30:55 +0000 | [diff] [blame^] | 993 | return SethiUllmanNumbers[SU->NodeNum]; |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 994 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 995 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 996 | unsigned size() const { return Queue.size(); } |
| 997 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 998 | bool empty() const { return Queue.empty(); } |
| 999 | |
| 1000 | void push(SUnit *U) { |
Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1001 | assert(!U->NodeQueueId && "Node in the queue already"); |
| 1002 | U->NodeQueueId = ++currentQueueId; |
Dan Gohman | a4db335 | 2008-06-21 18:35:25 +0000 | [diff] [blame] | 1003 | Queue.push(U); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1004 | } |
Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1005 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1006 | void push_all(const std::vector<SUnit *> &Nodes) { |
| 1007 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1008 | push(Nodes[i]); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | SUnit *pop() { |
Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 1012 | if (empty()) return NULL; |
Dan Gohman | a4db335 | 2008-06-21 18:35:25 +0000 | [diff] [blame] | 1013 | SUnit *V = Queue.top(); |
| 1014 | Queue.pop(); |
Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1015 | V->NodeQueueId = 0; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1016 | return V; |
| 1017 | } |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1018 | |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1019 | void remove(SUnit *SU) { |
Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1020 | assert(!Queue.empty() && "Queue is empty!"); |
Dan Gohman | a4db335 | 2008-06-21 18:35:25 +0000 | [diff] [blame] | 1021 | assert(SU->NodeQueueId != 0 && "Not in queue!"); |
| 1022 | Queue.erase_one(SU); |
Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1023 | SU->NodeQueueId = 0; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1024 | } |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 1025 | |
| 1026 | void setScheduleDAG(ScheduleDAGRRList *scheduleDag) { |
| 1027 | scheduleDAG = scheduleDag; |
| 1028 | } |
| 1029 | |
| 1030 | protected: |
| 1031 | bool canClobber(const SUnit *SU, const SUnit *Op); |
| 1032 | void AddPseudoTwoAddrDeps(); |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1033 | void CalculateSethiUllmanNumbers(); |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1034 | }; |
| 1035 | |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 1036 | typedef RegReductionPriorityQueue<bu_ls_rr_sort> |
| 1037 | BURegReductionPriorityQueue; |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1038 | |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 1039 | typedef RegReductionPriorityQueue<td_ls_rr_sort> |
| 1040 | TDRegReductionPriorityQueue; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1043 | /// closestSucc - Returns the scheduled cycle of the successor which is |
| 1044 | /// closet to the current cycle. |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1045 | static unsigned closestSucc(const SUnit *SU) { |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1046 | unsigned MaxHeight = 0; |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1047 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1048 | I != E; ++I) { |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1049 | unsigned Height = I->getSUnit()->getHeight(); |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1050 | // If there are bunch of CopyToRegs stacked up, they should be considered |
| 1051 | // to be at the same position. |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 1052 | if (I->getSUnit()->getNode() && |
| 1053 | I->getSUnit()->getNode()->getOpcode() == ISD::CopyToReg) |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1054 | Height = closestSucc(I->getSUnit())+1; |
| 1055 | if (Height > MaxHeight) |
| 1056 | MaxHeight = Height; |
Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1057 | } |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1058 | return MaxHeight; |
Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1061 | /// calcMaxScratches - Returns an cost estimate of the worse case requirement |
| 1062 | /// for scratch registers. Live-in operands and live-out results don't count |
| 1063 | /// since they are "fixed". |
| 1064 | static unsigned calcMaxScratches(const SUnit *SU) { |
| 1065 | unsigned Scratches = 0; |
| 1066 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 1067 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 1068 | if (I->isCtrl()) continue; // ignore chain preds |
| 1069 | if (!I->getSUnit()->getNode() || |
| 1070 | I->getSUnit()->getNode()->getOpcode() != ISD::CopyFromReg) |
Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1071 | Scratches++; |
| 1072 | } |
| 1073 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1074 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 1075 | if (I->isCtrl()) continue; // ignore chain succs |
| 1076 | if (!I->getSUnit()->getNode() || |
| 1077 | I->getSUnit()->getNode()->getOpcode() != ISD::CopyToReg) |
Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1078 | Scratches += 10; |
| 1079 | } |
| 1080 | return Scratches; |
| 1081 | } |
| 1082 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1083 | // Bottom up |
| 1084 | bool bu_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const { |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1085 | unsigned LPriority = SPQ->getNodePriority(left); |
| 1086 | unsigned RPriority = SPQ->getNodePriority(right); |
Evan Cheng | 73bdf04 | 2008-03-01 00:39:47 +0000 | [diff] [blame] | 1087 | if (LPriority != RPriority) |
| 1088 | return LPriority > RPriority; |
| 1089 | |
| 1090 | // Try schedule def + use closer when Sethi-Ullman numbers are the same. |
| 1091 | // e.g. |
| 1092 | // t1 = op t2, c1 |
| 1093 | // t3 = op t4, c2 |
| 1094 | // |
| 1095 | // and the following instructions are both ready. |
| 1096 | // t2 = op c3 |
| 1097 | // t4 = op c4 |
| 1098 | // |
| 1099 | // Then schedule t2 = op first. |
| 1100 | // i.e. |
| 1101 | // t4 = op c4 |
| 1102 | // t2 = op c3 |
| 1103 | // t1 = op t2, c1 |
| 1104 | // t3 = op t4, c2 |
| 1105 | // |
| 1106 | // This creates more short live intervals. |
| 1107 | unsigned LDist = closestSucc(left); |
| 1108 | unsigned RDist = closestSucc(right); |
| 1109 | if (LDist != RDist) |
| 1110 | return LDist < RDist; |
| 1111 | |
| 1112 | // Intuitively, it's good to push down instructions whose results are |
| 1113 | // liveout so their long live ranges won't conflict with other values |
| 1114 | // which are needed inside the BB. Further prioritize liveout instructions |
| 1115 | // by the number of operands which are calculated within the BB. |
| 1116 | unsigned LScratch = calcMaxScratches(left); |
| 1117 | unsigned RScratch = calcMaxScratches(right); |
| 1118 | if (LScratch != RScratch) |
| 1119 | return LScratch > RScratch; |
| 1120 | |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1121 | if (left->getHeight() != right->getHeight()) |
| 1122 | return left->getHeight() > right->getHeight(); |
Evan Cheng | 73bdf04 | 2008-03-01 00:39:47 +0000 | [diff] [blame] | 1123 | |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1124 | if (left->getDepth() != right->getDepth()) |
| 1125 | return left->getDepth() < right->getDepth(); |
Evan Cheng | 73bdf04 | 2008-03-01 00:39:47 +0000 | [diff] [blame] | 1126 | |
Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1127 | assert(left->NodeQueueId && right->NodeQueueId && |
| 1128 | "NodeQueueId cannot be zero"); |
| 1129 | return (left->NodeQueueId > right->NodeQueueId); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 1132 | template<class SF> |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1133 | bool |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 1134 | RegReductionPriorityQueue<SF>::canClobber(const SUnit *SU, const SUnit *Op) { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1135 | if (SU->isTwoAddress) { |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 1136 | unsigned Opc = SU->getNode()->getMachineOpcode(); |
Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1137 | const TargetInstrDesc &TID = TII->get(Opc); |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1138 | unsigned NumRes = TID.getNumDefs(); |
Dan Gohman | 0340d1e | 2008-02-15 20:50:13 +0000 | [diff] [blame] | 1139 | unsigned NumOps = TID.getNumOperands() - NumRes; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1140 | for (unsigned i = 0; i != NumOps; ++i) { |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1141 | if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) { |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 1142 | SDNode *DU = SU->getNode()->getOperand(i).getNode(); |
Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 1143 | if (DU->getNodeId() != -1 && |
| 1144 | Op->OrigNode == &(*SUnits)[DU->getNodeId()]) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1145 | return true; |
| 1146 | } |
| 1147 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1148 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1149 | return false; |
| 1150 | } |
| 1151 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1152 | |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1153 | /// hasCopyToRegUse - Return true if SU has a value successor that is a |
| 1154 | /// CopyToReg node. |
Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 1155 | static bool hasCopyToRegUse(const SUnit *SU) { |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1156 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1157 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 1158 | if (I->isCtrl()) continue; |
| 1159 | const SUnit *SuccSU = I->getSUnit(); |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 1160 | if (SuccSU->getNode() && SuccSU->getNode()->getOpcode() == ISD::CopyToReg) |
Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1161 | return true; |
| 1162 | } |
| 1163 | return false; |
| 1164 | } |
| 1165 | |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1166 | /// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's |
Dan Gohman | ea04520 | 2008-06-21 22:05:24 +0000 | [diff] [blame] | 1167 | /// physical register defs. |
Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 1168 | static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU, |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1169 | const TargetInstrInfo *TII, |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1170 | const TargetRegisterInfo *TRI) { |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 1171 | SDNode *N = SuccSU->getNode(); |
Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1172 | unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs(); |
| 1173 | const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs(); |
Dan Gohman | ea04520 | 2008-06-21 22:05:24 +0000 | [diff] [blame] | 1174 | assert(ImpDefs && "Caller should check hasPhysRegDefs"); |
Chris Lattner | b0d06b4 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 1175 | const unsigned *SUImpDefs = |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 1176 | TII->get(SU->getNode()->getMachineOpcode()).getImplicitDefs(); |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1177 | if (!SUImpDefs) |
| 1178 | return false; |
| 1179 | for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) { |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1180 | MVT VT = N->getValueType(i); |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1181 | if (VT == MVT::Flag || VT == MVT::Other) |
| 1182 | continue; |
Dan Gohman | 6ab52a8 | 2008-09-17 15:25:49 +0000 | [diff] [blame] | 1183 | if (!N->hasAnyUseOfValue(i)) |
| 1184 | continue; |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1185 | unsigned Reg = ImpDefs[i - NumDefs]; |
| 1186 | for (;*SUImpDefs; ++SUImpDefs) { |
| 1187 | unsigned SUReg = *SUImpDefs; |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1188 | if (TRI->regsOverlap(Reg, SUReg)) |
Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1189 | return true; |
| 1190 | } |
| 1191 | } |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1195 | /// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses |
| 1196 | /// it as a def&use operand. Add a pseudo control edge from it to the other |
| 1197 | /// 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] | 1198 | /// first (lower in the schedule). If both nodes are two-address, favor the |
| 1199 | /// one that has a CopyToReg use (more likely to be a loop induction update). |
| 1200 | /// If both are two-address, but one is commutable while the other is not |
| 1201 | /// commutable, favor the one that's not commutable. |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 1202 | template<class SF> |
| 1203 | void RegReductionPriorityQueue<SF>::AddPseudoTwoAddrDeps() { |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1204 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) { |
Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 1205 | SUnit *SU = &(*SUnits)[i]; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1206 | if (!SU->isTwoAddress) |
| 1207 | continue; |
| 1208 | |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 1209 | SDNode *Node = SU->getNode(); |
Dan Gohman | 072734e | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 1210 | if (!Node || !Node->isMachineOpcode() || SU->getNode()->getFlaggedNode()) |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1211 | continue; |
| 1212 | |
Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1213 | unsigned Opc = Node->getMachineOpcode(); |
Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1214 | const TargetInstrDesc &TID = TII->get(Opc); |
Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1215 | unsigned NumRes = TID.getNumDefs(); |
Dan Gohman | 0340d1e | 2008-02-15 20:50:13 +0000 | [diff] [blame] | 1216 | unsigned NumOps = TID.getNumOperands() - NumRes; |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1217 | for (unsigned j = 0; j != NumOps; ++j) { |
Dan Gohman | 82016c2 | 2008-11-19 02:00:32 +0000 | [diff] [blame] | 1218 | if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1) |
| 1219 | continue; |
| 1220 | SDNode *DU = SU->getNode()->getOperand(j).getNode(); |
| 1221 | if (DU->getNodeId() == -1) |
| 1222 | continue; |
| 1223 | const SUnit *DUSU = &(*SUnits)[DU->getNodeId()]; |
| 1224 | if (!DUSU) continue; |
| 1225 | for (SUnit::const_succ_iterator I = DUSU->Succs.begin(), |
| 1226 | E = DUSU->Succs.end(); I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 1227 | if (I->isCtrl()) continue; |
| 1228 | SUnit *SuccSU = I->getSUnit(); |
Dan Gohman | 82016c2 | 2008-11-19 02:00:32 +0000 | [diff] [blame] | 1229 | if (SuccSU == SU) |
Evan Cheng | 1bf16631 | 2007-11-09 01:27:11 +0000 | [diff] [blame] | 1230 | continue; |
Dan Gohman | 82016c2 | 2008-11-19 02:00:32 +0000 | [diff] [blame] | 1231 | // Be conservative. Ignore if nodes aren't at roughly the same |
| 1232 | // depth and height. |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1233 | if (SuccSU->getHeight() < SU->getHeight() && |
| 1234 | (SU->getHeight() - SuccSU->getHeight()) > 1) |
Dan Gohman | 82016c2 | 2008-11-19 02:00:32 +0000 | [diff] [blame] | 1235 | continue; |
| 1236 | if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode()) |
| 1237 | continue; |
| 1238 | // Don't constrain nodes with physical register defs if the |
| 1239 | // predecessor can clobber them. |
| 1240 | if (SuccSU->hasPhysRegDefs) { |
| 1241 | if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI)) |
Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1242 | continue; |
Dan Gohman | 82016c2 | 2008-11-19 02:00:32 +0000 | [diff] [blame] | 1243 | } |
| 1244 | // Don't constraint extract_subreg / insert_subreg these may be |
| 1245 | // coalesced away. We don't them close to their uses. |
| 1246 | unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode(); |
| 1247 | if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG || |
| 1248 | SuccOpc == TargetInstrInfo::INSERT_SUBREG) |
| 1249 | continue; |
| 1250 | if ((!canClobber(SuccSU, DUSU) || |
| 1251 | (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) || |
| 1252 | (!SU->isCommutable && SuccSU->isCommutable)) && |
| 1253 | !scheduleDAG->IsReachable(SuccSU, SU)) { |
Dan Gohman | 30cad9c | 2008-12-04 02:14:57 +0000 | [diff] [blame] | 1254 | DOUT << "Adding a pseudo-two-addr edge from SU # " << SU->NodeNum |
Dan Gohman | 82016c2 | 2008-11-19 02:00:32 +0000 | [diff] [blame] | 1255 | << " to SU #" << SuccSU->NodeNum << "\n"; |
Dan Gohman | 79c3516 | 2009-01-06 01:19:04 +0000 | [diff] [blame] | 1256 | scheduleDAG->AddPred(SU, SDep(SuccSU, SDep::Order, /*Latency=*/0, |
Dan Gohman | bf8e520 | 2009-01-06 01:28:56 +0000 | [diff] [blame] | 1257 | /*Reg=*/0, /*isNormalMemory=*/false, |
| 1258 | /*isMustAlias=*/false, |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 1259 | /*isArtificial=*/true)); |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1266 | /// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all |
| 1267 | /// scheduling units. |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 1268 | template<class SF> |
| 1269 | void RegReductionPriorityQueue<SF>::CalculateSethiUllmanNumbers() { |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1270 | SethiUllmanNumbers.assign(SUnits->size(), 0); |
| 1271 | |
| 1272 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
Dan Gohman | 186f65d | 2008-11-20 03:30:37 +0000 | [diff] [blame] | 1273 | CalcNodeSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers); |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1274 | } |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1275 | |
Roman Levenstein | 30d0951 | 2008-03-27 09:44:37 +0000 | [diff] [blame] | 1276 | /// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled |
Roman Levenstein | bc67450 | 2008-03-27 09:14:57 +0000 | [diff] [blame] | 1277 | /// predecessors of the successors of the SUnit SU. Stop when the provided |
| 1278 | /// limit is exceeded. |
Roman Levenstein | bc67450 | 2008-03-27 09:14:57 +0000 | [diff] [blame] | 1279 | static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU, |
| 1280 | unsigned Limit) { |
| 1281 | unsigned Sum = 0; |
| 1282 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1283 | I != E; ++I) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 1284 | const SUnit *SuccSU = I->getSUnit(); |
Roman Levenstein | bc67450 | 2008-03-27 09:14:57 +0000 | [diff] [blame] | 1285 | for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(), |
| 1286 | EE = SuccSU->Preds.end(); II != EE; ++II) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 1287 | SUnit *PredSU = II->getSUnit(); |
Evan Cheng | 16d7207 | 2008-03-29 18:34:22 +0000 | [diff] [blame] | 1288 | if (!PredSU->isScheduled) |
| 1289 | if (++Sum > Limit) |
| 1290 | return Sum; |
Roman Levenstein | bc67450 | 2008-03-27 09:14:57 +0000 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | return Sum; |
| 1294 | } |
| 1295 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1296 | |
| 1297 | // Top down |
| 1298 | bool td_ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const { |
Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1299 | unsigned LPriority = SPQ->getNodePriority(left); |
| 1300 | unsigned RPriority = SPQ->getNodePriority(right); |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 1301 | bool LIsTarget = left->getNode() && left->getNode()->isMachineOpcode(); |
| 1302 | bool RIsTarget = right->getNode() && right->getNode()->isMachineOpcode(); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1303 | bool LIsFloater = LIsTarget && left->NumPreds == 0; |
| 1304 | bool RIsFloater = RIsTarget && right->NumPreds == 0; |
Roman Levenstein | bc67450 | 2008-03-27 09:14:57 +0000 | [diff] [blame] | 1305 | unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0; |
| 1306 | unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1307 | |
| 1308 | if (left->NumSuccs == 0 && right->NumSuccs != 0) |
| 1309 | return false; |
| 1310 | else if (left->NumSuccs != 0 && right->NumSuccs == 0) |
| 1311 | return true; |
| 1312 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1313 | if (LIsFloater) |
| 1314 | LBonus -= 2; |
| 1315 | if (RIsFloater) |
| 1316 | RBonus -= 2; |
| 1317 | if (left->NumSuccs == 1) |
| 1318 | LBonus += 2; |
| 1319 | if (right->NumSuccs == 1) |
| 1320 | RBonus += 2; |
| 1321 | |
Evan Cheng | 73bdf04 | 2008-03-01 00:39:47 +0000 | [diff] [blame] | 1322 | if (LPriority+LBonus != RPriority+RBonus) |
| 1323 | return LPriority+LBonus < RPriority+RBonus; |
Anton Korobeynikov | 035eaac | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 1324 | |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1325 | if (left->getDepth() != right->getDepth()) |
| 1326 | return left->getDepth() < right->getDepth(); |
Evan Cheng | 73bdf04 | 2008-03-01 00:39:47 +0000 | [diff] [blame] | 1327 | |
| 1328 | if (left->NumSuccsLeft != right->NumSuccsLeft) |
| 1329 | return left->NumSuccsLeft > right->NumSuccsLeft; |
| 1330 | |
Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1331 | assert(left->NodeQueueId && right->NodeQueueId && |
| 1332 | "NodeQueueId cannot be zero"); |
| 1333 | return (left->NodeQueueId > right->NodeQueueId); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1336 | //===----------------------------------------------------------------------===// |
| 1337 | // Public Constructor Functions |
| 1338 | //===----------------------------------------------------------------------===// |
| 1339 | |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1340 | llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, |
| 1341 | SelectionDAG *DAG, |
Dan Gohman | 5499e89 | 2008-11-11 17:50:47 +0000 | [diff] [blame] | 1342 | const TargetMachine *TM, |
Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 1343 | MachineBasicBlock *BB, |
Dan Gohman | fd08af4 | 2008-11-20 03:11:19 +0000 | [diff] [blame] | 1344 | bool) { |
Dan Gohman | 5499e89 | 2008-11-11 17:50:47 +0000 | [diff] [blame] | 1345 | const TargetInstrInfo *TII = TM->getInstrInfo(); |
| 1346 | const TargetRegisterInfo *TRI = TM->getRegisterInfo(); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 1347 | |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1348 | BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI); |
Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 1349 | |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1350 | ScheduleDAGRRList *SD = |
Dan Gohman | fd08af4 | 2008-11-20 03:11:19 +0000 | [diff] [blame] | 1351 | new ScheduleDAGRRList(DAG, BB, *TM, true, PQ); |
Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1352 | PQ->setScheduleDAG(SD); |
| 1353 | return SD; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1356 | llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, |
| 1357 | SelectionDAG *DAG, |
Dan Gohman | 5499e89 | 2008-11-11 17:50:47 +0000 | [diff] [blame] | 1358 | const TargetMachine *TM, |
Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 1359 | MachineBasicBlock *BB, |
Dan Gohman | fd08af4 | 2008-11-20 03:11:19 +0000 | [diff] [blame] | 1360 | bool) { |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 1361 | const TargetInstrInfo *TII = TM->getInstrInfo(); |
| 1362 | const TargetRegisterInfo *TRI = TM->getRegisterInfo(); |
| 1363 | |
| 1364 | TDRegReductionPriorityQueue *PQ = new TDRegReductionPriorityQueue(TII, TRI); |
| 1365 | |
Dan Gohman | fd08af4 | 2008-11-20 03:11:19 +0000 | [diff] [blame] | 1366 | ScheduleDAGRRList *SD = new ScheduleDAGRRList(DAG, BB, *TM, false, PQ); |
Dan Gohman | 3f656df | 2008-11-20 02:45:51 +0000 | [diff] [blame] | 1367 | PQ->setScheduleDAG(SD); |
| 1368 | return SD; |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1369 | } |