| 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" | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/ScheduleDAG.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/BitVector.h" | 
|  | 28 | #include "llvm/ADT/PriorityQueue.h" | 
| Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallPtrSet.h" | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallSet.h" | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Statistic.h" | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 33 | #include <climits> | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" | 
|  | 35 | using namespace llvm; | 
|  | 36 |  | 
| Dan Gohman | fd227e9 | 2008-03-25 17:10:29 +0000 | [diff] [blame] | 37 | STATISTIC(NumBacktracks, "Number of times scheduler backtracked"); | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 38 | STATISTIC(NumUnfolds,    "Number of nodes unfolded"); | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 39 | STATISTIC(NumDups,       "Number of duplicated nodes"); | 
|  | 40 | STATISTIC(NumCCCopies,   "Number of cross class copies"); | 
|  | 41 |  | 
| Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 42 | static RegisterScheduler | 
|  | 43 | burrListDAGScheduler("list-burr", | 
| Dan Gohman | 9c4b7d5 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 44 | "Bottom-up register reduction list scheduling", | 
| Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 45 | createBURRListDAGScheduler); | 
|  | 46 | static RegisterScheduler | 
|  | 47 | tdrListrDAGScheduler("list-tdrr", | 
| Dan Gohman | 9c4b7d5 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 48 | "Top-down register reduction list scheduling", | 
| Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 49 | createTDRRListDAGScheduler); | 
|  | 50 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 51 | namespace { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// | 
|  | 53 | /// ScheduleDAGRRList - The actual register reduction list scheduler | 
|  | 54 | /// implementation.  This supports both top-down and bottom-up scheduling. | 
|  | 55 | /// | 
| Chris Lattner | e097e6f | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 56 | class VISIBILITY_HIDDEN ScheduleDAGRRList : public ScheduleDAG { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 57 | private: | 
|  | 58 | /// isBottomUp - This is true if the scheduling problem is bottom-up, false if | 
|  | 59 | /// it is top-down. | 
|  | 60 | bool isBottomUp; | 
| Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 61 |  | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 62 | /// Fast - True if we are performing fast scheduling. | 
|  | 63 | /// | 
| Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 64 | bool Fast; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 65 |  | 
|  | 66 | /// AvailableQueue - The priority queue to use for the available SUnits. | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 67 | SchedulingPriorityQueue *AvailableQueue; | 
|  | 68 |  | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 69 | /// LiveRegDefs - A set of physical registers and their definition | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 70 | /// that are "live". These nodes must be scheduled before any other nodes that | 
|  | 71 | /// modifies the registers can be scheduled. | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 72 | unsigned NumLiveRegs; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 73 | std::vector<SUnit*> LiveRegDefs; | 
|  | 74 | std::vector<unsigned> LiveRegCycles; | 
|  | 75 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 76 | public: | 
|  | 77 | ScheduleDAGRRList(SelectionDAG &dag, MachineBasicBlock *bb, | 
| Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 78 | const TargetMachine &tm, bool isbottomup, bool f, | 
|  | 79 | SchedulingPriorityQueue *availqueue) | 
|  | 80 | : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup), Fast(f), | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 81 | AvailableQueue(availqueue) { | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | ~ScheduleDAGRRList() { | 
|  | 85 | delete AvailableQueue; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | void Schedule(); | 
|  | 89 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 90 | /// IsReachable - Checks if SU is reachable from TargetSU. | 
| Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 91 | bool IsReachable(const SUnit *SU, const SUnit *TargetSU); | 
| 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. | 
|  | 95 | bool WillCreateCycle(SUnit *SU, SUnit *TargetSU); | 
|  | 96 |  | 
|  | 97 | /// AddPred - This adds the specified node X as a predecessor of | 
|  | 98 | /// the current node Y if not already. | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 99 | /// This returns true if this is a new predecessor. | 
|  | 100 | /// Updates the topological ordering if required. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 101 | bool AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial, | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 102 | unsigned PhyReg = 0, int Cost = 1); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 103 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 104 | /// RemovePred - This removes the specified node N from the predecessors of | 
|  | 105 | /// the current node M. Updates the topological ordering if required. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 106 | bool RemovePred(SUnit *M, SUnit *N, bool isCtrl, bool isSpecial); | 
|  | 107 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 108 | private: | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 109 | void ReleasePred(SUnit*, bool, unsigned); | 
|  | 110 | void ReleaseSucc(SUnit*, bool isChain, unsigned); | 
|  | 111 | void CapturePred(SUnit*, SUnit*, bool); | 
|  | 112 | void ScheduleNodeBottomUp(SUnit*, unsigned); | 
|  | 113 | void ScheduleNodeTopDown(SUnit*, unsigned); | 
|  | 114 | void UnscheduleNodeBottomUp(SUnit*); | 
|  | 115 | void BacktrackBottomUp(SUnit*, unsigned, unsigned&); | 
|  | 116 | SUnit *CopyAndMoveSuccessors(SUnit*); | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 117 | void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned, | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 118 | const TargetRegisterClass*, | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 119 | const TargetRegisterClass*, | 
|  | 120 | SmallVector<SUnit*, 2>&); | 
|  | 121 | bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 122 | void ListScheduleTopDown(); | 
|  | 123 | void ListScheduleBottomUp(); | 
| Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 124 | void CommuteNodesToReducePressure(); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 125 |  | 
|  | 126 |  | 
|  | 127 | /// CreateNewSUnit - Creates a new SUnit and returns a pointer to it. | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 128 | /// Updates the topological ordering if required. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 129 | SUnit *CreateNewSUnit(SDNode *N) { | 
|  | 130 | SUnit *NewNode = NewSUnit(N); | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 131 | // Update the topological ordering. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 132 | if (NewNode->NodeNum >= Node2Index.size()) | 
|  | 133 | InitDAGTopologicalSorting(); | 
|  | 134 | return NewNode; | 
|  | 135 | } | 
|  | 136 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 137 | /// CreateClone - Creates a new SUnit from an existing one. | 
|  | 138 | /// Updates the topological ordering if required. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 139 | SUnit *CreateClone(SUnit *N) { | 
|  | 140 | SUnit *NewNode = Clone(N); | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 141 | // Update the topological ordering. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 142 | if (NewNode->NodeNum >= Node2Index.size()) | 
|  | 143 | InitDAGTopologicalSorting(); | 
|  | 144 | return NewNode; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | /// Functions for preserving the topological ordering | 
|  | 148 | /// even after dynamic insertions of new edges. | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 149 | /// This allows a very fast implementation of IsReachable. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 150 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 151 | /// InitDAGTopologicalSorting - create the initial topological | 
|  | 152 | /// ordering from the DAG to be scheduled. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 153 | void InitDAGTopologicalSorting(); | 
|  | 154 |  | 
|  | 155 | /// DFS - make a DFS traversal and mark all nodes affected by the | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 156 | /// edge insertion. These nodes will later get new topological indexes | 
|  | 157 | /// by means of the Shift method. | 
| Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 158 | void DFS(const SUnit *SU, int UpperBound, bool& HasLoop); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 159 |  | 
|  | 160 | /// Shift - reassign topological indexes for the nodes in the DAG | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 161 | /// to preserve the topological ordering. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 162 | void Shift(BitVector& Visited, int LowerBound, int UpperBound); | 
|  | 163 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 164 | /// Allocate - assign the topological index to the node n. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 165 | void Allocate(int n, int index); | 
|  | 166 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 167 | /// Index2Node - Maps topological index to the node number. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 168 | std::vector<int> Index2Node; | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 169 | /// Node2Index - Maps the node number to its topological index. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 170 | std::vector<int> Node2Index; | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 171 | /// Visited - a set of nodes visited during a DFS traversal. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 172 | BitVector Visited; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 173 | }; | 
|  | 174 | }  // end anonymous namespace | 
|  | 175 |  | 
|  | 176 |  | 
|  | 177 | /// Schedule - Schedule the DAG using list scheduling. | 
|  | 178 | void ScheduleDAGRRList::Schedule() { | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 179 | DOUT << "********** List Scheduling **********\n"; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 180 |  | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 181 | NumLiveRegs = 0; | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 182 | LiveRegDefs.resize(TRI->getNumRegs(), NULL); | 
|  | 183 | LiveRegCycles.resize(TRI->getNumRegs(), 0); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 184 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 185 | // Build scheduling units. | 
|  | 186 | BuildSchedUnits(); | 
|  | 187 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 188 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) | 
| Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 189 | SUnits[su].dumpAll(&DAG)); | 
| Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 190 | if (!Fast) { | 
|  | 191 | CalculateDepths(); | 
|  | 192 | CalculateHeights(); | 
|  | 193 | } | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 194 | InitDAGTopologicalSorting(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 195 |  | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 196 | AvailableQueue->initNodes(SUnits); | 
| Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 197 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 198 | // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate. | 
|  | 199 | if (isBottomUp) | 
|  | 200 | ListScheduleBottomUp(); | 
|  | 201 | else | 
|  | 202 | ListScheduleTopDown(); | 
|  | 203 |  | 
|  | 204 | AvailableQueue->releaseState(); | 
| Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 205 |  | 
|  | 206 | if (!Fast) | 
|  | 207 | CommuteNodesToReducePressure(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 210 | /// CommuteNodesToReducePressure - If a node is two-address and commutable, and | 
| Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 211 | /// it is not the last use of its first operand, add it to the CommuteSet if | 
|  | 212 | /// possible. It will be commuted when it is translated to a MI. | 
|  | 213 | void ScheduleDAGRRList::CommuteNodesToReducePressure() { | 
| Evan Cheng | e3c4419 | 2007-06-22 01:35:51 +0000 | [diff] [blame] | 214 | SmallPtrSet<SUnit*, 4> OperandSeen; | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 215 | for (unsigned i = Sequence.size(); i != 0; ) { | 
|  | 216 | --i; | 
| Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 217 | SUnit *SU = Sequence[i]; | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 218 | if (!SU || !SU->Node) continue; | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 219 | if (SU->isCommutable) { | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 220 | unsigned Opc = SU->Node->getMachineOpcode(); | 
| Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 221 | const TargetInstrDesc &TID = TII->get(Opc); | 
| Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 222 | unsigned NumRes = TID.getNumDefs(); | 
| Dan Gohman | 0340d1e | 2008-02-15 20:50:13 +0000 | [diff] [blame] | 223 | unsigned NumOps = TID.getNumOperands() - NumRes; | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 224 | for (unsigned j = 0; j != NumOps; ++j) { | 
| Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 225 | if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) == -1) | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 226 | continue; | 
|  | 227 |  | 
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 228 | SDNode *OpN = SU->Node->getOperand(j).getNode(); | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 229 | SUnit *OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()]; | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 230 | if (OpSU && OperandSeen.count(OpSU) == 1) { | 
|  | 231 | // Ok, so SU is not the last use of OpSU, but SU is two-address so | 
|  | 232 | // it will clobber OpSU. Try to commute SU if no other source operands | 
|  | 233 | // are live below. | 
|  | 234 | bool DoCommute = true; | 
|  | 235 | for (unsigned k = 0; k < NumOps; ++k) { | 
|  | 236 | if (k != j) { | 
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 237 | OpN = SU->Node->getOperand(k).getNode(); | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 238 | OpSU = isPassiveNode(OpN) ? NULL : &SUnits[OpN->getNodeId()]; | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 239 | if (OpSU && OperandSeen.count(OpSU) == 1) { | 
|  | 240 | DoCommute = false; | 
|  | 241 | break; | 
|  | 242 | } | 
|  | 243 | } | 
| Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 244 | } | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 245 | if (DoCommute) | 
|  | 246 | CommuteSet.insert(SU->Node); | 
| Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 247 | } | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 248 |  | 
|  | 249 | // Only look at the first use&def node for now. | 
|  | 250 | break; | 
| Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 251 | } | 
|  | 252 | } | 
|  | 253 |  | 
| Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 254 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 255 | I != E; ++I) { | 
| Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 256 | if (!I->isCtrl) | 
| Dan Gohman | e6e1348 | 2008-06-21 15:52:51 +0000 | [diff] [blame] | 257 | OperandSeen.insert(I->Dep->OrigNode); | 
| Evan Cheng | afed73e | 2006-05-12 01:58:24 +0000 | [diff] [blame] | 258 | } | 
|  | 259 | } | 
|  | 260 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 261 |  | 
|  | 262 | //===----------------------------------------------------------------------===// | 
|  | 263 | //  Bottom-Up Scheduling | 
|  | 264 | //===----------------------------------------------------------------------===// | 
|  | 265 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 266 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to | 
| Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 267 | /// the AvailableQueue if the count reaches zero. Also update its cycle bound. | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 268 | void ScheduleDAGRRList::ReleasePred(SUnit *PredSU, bool isChain, | 
|  | 269 | unsigned CurCycle) { | 
|  | 270 | // FIXME: the distance between two nodes is not always == the predecessor's | 
|  | 271 | // latency. For example, the reader can very well read the register written | 
|  | 272 | // by the predecessor later than the issue cycle. It also depends on the | 
|  | 273 | // interrupt model (drain vs. freeze). | 
|  | 274 | PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency); | 
|  | 275 |  | 
| Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 276 | --PredSU->NumSuccsLeft; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 277 |  | 
|  | 278 | #ifndef NDEBUG | 
| Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 279 | if (PredSU->NumSuccsLeft < 0) { | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 280 | cerr << "*** List scheduling failed! ***\n"; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 281 | PredSU->dump(&DAG); | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 282 | cerr << " has been released too many times!\n"; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 283 | assert(0); | 
|  | 284 | } | 
|  | 285 | #endif | 
|  | 286 |  | 
| Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 287 | if (PredSU->NumSuccsLeft == 0) { | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 288 | PredSU->isAvailable = true; | 
|  | 289 | AvailableQueue->push(PredSU); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 290 | } | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending | 
|  | 294 | /// count of its predecessors. If a predecessor pending count is zero, add it to | 
|  | 295 | /// the Available queue. | 
| Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 296 | void ScheduleDAGRRList::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) { | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 297 | DOUT << "*** Scheduling [" << CurCycle << "]: "; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 298 | DEBUG(SU->dump(&DAG)); | 
|  | 299 | SU->Cycle = CurCycle; | 
|  | 300 |  | 
|  | 301 | AvailableQueue->ScheduledNode(SU); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 302 |  | 
|  | 303 | // Bottom up: release predecessors | 
| Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 304 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 305 | I != E; ++I) { | 
| Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 306 | ReleasePred(I->Dep, I->isCtrl, CurCycle); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 307 | if (I->Cost < 0)  { | 
|  | 308 | // This is a physical register dependency and it's impossible or | 
|  | 309 | // expensive to copy the register. Make sure nothing that can | 
|  | 310 | // clobber the register is scheduled between the predecessor and | 
|  | 311 | // this node. | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 312 | if (!LiveRegDefs[I->Reg]) { | 
|  | 313 | ++NumLiveRegs; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 314 | LiveRegDefs[I->Reg] = I->Dep; | 
|  | 315 | LiveRegCycles[I->Reg] = CurCycle; | 
|  | 316 | } | 
|  | 317 | } | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | // Release all the implicit physical register defs that are live. | 
|  | 321 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
|  | 322 | I != E; ++I) { | 
|  | 323 | if (I->Cost < 0)  { | 
|  | 324 | if (LiveRegCycles[I->Reg] == I->Dep->Cycle) { | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 325 | assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!"); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 326 | assert(LiveRegDefs[I->Reg] == SU && | 
|  | 327 | "Physical register dependency violated?"); | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 328 | --NumLiveRegs; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 329 | LiveRegDefs[I->Reg] = NULL; | 
|  | 330 | LiveRegCycles[I->Reg] = 0; | 
|  | 331 | } | 
|  | 332 | } | 
|  | 333 | } | 
|  | 334 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 335 | SU->isScheduled = true; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 336 | } | 
|  | 337 |  | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 338 | /// CapturePred - This does the opposite of ReleasePred. Since SU is being | 
|  | 339 | /// unscheduled, incrcease the succ left count of its predecessors. Remove | 
|  | 340 | /// them from AvailableQueue if necessary. | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 341 | void ScheduleDAGRRList::CapturePred(SUnit *PredSU, SUnit *SU, bool isChain) { | 
|  | 342 | unsigned CycleBound = 0; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 343 | for (SUnit::succ_iterator I = PredSU->Succs.begin(), E = PredSU->Succs.end(); | 
|  | 344 | I != E; ++I) { | 
|  | 345 | if (I->Dep == SU) | 
|  | 346 | continue; | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 347 | CycleBound = std::max(CycleBound, | 
|  | 348 | I->Dep->Cycle + PredSU->Latency); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 349 | } | 
|  | 350 |  | 
|  | 351 | if (PredSU->isAvailable) { | 
|  | 352 | PredSU->isAvailable = false; | 
|  | 353 | if (!PredSU->isPending) | 
|  | 354 | AvailableQueue->remove(PredSU); | 
|  | 355 | } | 
|  | 356 |  | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 357 | PredSU->CycleBound = CycleBound; | 
| Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 358 | ++PredSU->NumSuccsLeft; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
|  | 361 | /// UnscheduleNodeBottomUp - Remove the node from the schedule, update its and | 
|  | 362 | /// its predecessor states to reflect the change. | 
|  | 363 | void ScheduleDAGRRList::UnscheduleNodeBottomUp(SUnit *SU) { | 
|  | 364 | DOUT << "*** Unscheduling [" << SU->Cycle << "]: "; | 
|  | 365 | DEBUG(SU->dump(&DAG)); | 
|  | 366 |  | 
|  | 367 | AvailableQueue->UnscheduledNode(SU); | 
|  | 368 |  | 
|  | 369 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 370 | I != E; ++I) { | 
|  | 371 | CapturePred(I->Dep, SU, I->isCtrl); | 
|  | 372 | if (I->Cost < 0 && SU->Cycle == LiveRegCycles[I->Reg])  { | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 373 | assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!"); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 374 | assert(LiveRegDefs[I->Reg] == I->Dep && | 
|  | 375 | "Physical register dependency violated?"); | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 376 | --NumLiveRegs; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 377 | LiveRegDefs[I->Reg] = NULL; | 
|  | 378 | LiveRegCycles[I->Reg] = 0; | 
|  | 379 | } | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
|  | 383 | I != E; ++I) { | 
|  | 384 | if (I->Cost < 0)  { | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 385 | if (!LiveRegDefs[I->Reg]) { | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 386 | LiveRegDefs[I->Reg] = SU; | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 387 | ++NumLiveRegs; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 388 | } | 
|  | 389 | if (I->Dep->Cycle < LiveRegCycles[I->Reg]) | 
|  | 390 | LiveRegCycles[I->Reg] = I->Dep->Cycle; | 
|  | 391 | } | 
|  | 392 | } | 
|  | 393 |  | 
|  | 394 | SU->Cycle = 0; | 
|  | 395 | SU->isScheduled = false; | 
|  | 396 | SU->isAvailable = true; | 
|  | 397 | AvailableQueue->push(SU); | 
|  | 398 | } | 
|  | 399 |  | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 400 | /// IsReachable - Checks if SU is reachable from TargetSU. | 
| Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 401 | bool ScheduleDAGRRList::IsReachable(const SUnit *SU, const SUnit *TargetSU) { | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 402 | // If insertion of the edge SU->TargetSU would create a cycle | 
|  | 403 | // then there is a path from TargetSU to SU. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 404 | int UpperBound, LowerBound; | 
|  | 405 | LowerBound = Node2Index[TargetSU->NodeNum]; | 
|  | 406 | UpperBound = Node2Index[SU->NodeNum]; | 
|  | 407 | bool HasLoop = false; | 
|  | 408 | // Is Ord(TargetSU) < Ord(SU) ? | 
|  | 409 | if (LowerBound < UpperBound) { | 
|  | 410 | Visited.reset(); | 
|  | 411 | // There may be a path from TargetSU to SU. Check for it. | 
|  | 412 | DFS(TargetSU, UpperBound, HasLoop); | 
| Evan Cheng | cfd5f82 | 2007-09-27 00:25:29 +0000 | [diff] [blame] | 413 | } | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 414 | return HasLoop; | 
| Evan Cheng | cfd5f82 | 2007-09-27 00:25:29 +0000 | [diff] [blame] | 415 | } | 
|  | 416 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 417 | /// Allocate - assign the topological index to the node n. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 418 | inline void ScheduleDAGRRList::Allocate(int n, int index) { | 
|  | 419 | Node2Index[n] = index; | 
|  | 420 | Index2Node[index] = n; | 
| Evan Cheng | cfd5f82 | 2007-09-27 00:25:29 +0000 | [diff] [blame] | 421 | } | 
|  | 422 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 423 | /// InitDAGTopologicalSorting - create the initial topological | 
|  | 424 | /// ordering from the DAG to be scheduled. | 
| Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 425 |  | 
|  | 426 | /// The idea of the algorithm is taken from | 
|  | 427 | /// "Online algorithms for managing the topological order of | 
|  | 428 | /// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly | 
|  | 429 | /// This is the MNR algorithm, which was first introduced by | 
|  | 430 | /// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in | 
|  | 431 | /// "Maintaining a topological order under edge insertions". | 
|  | 432 | /// | 
|  | 433 | /// Short description of the algorithm: | 
|  | 434 | /// | 
|  | 435 | /// Topological ordering, ord, of a DAG maps each node to a topological | 
|  | 436 | /// index so that for all edges X->Y it is the case that ord(X) < ord(Y). | 
|  | 437 | /// | 
|  | 438 | /// This means that if there is a path from the node X to the node Z, | 
|  | 439 | /// then ord(X) < ord(Z). | 
|  | 440 | /// | 
|  | 441 | /// This property can be used to check for reachability of nodes: | 
|  | 442 | /// if Z is reachable from X, then an insertion of the edge Z->X would | 
|  | 443 | /// create a cycle. | 
|  | 444 | /// | 
|  | 445 | /// The algorithm first computes a topological ordering for the DAG by | 
|  | 446 | /// initializing the Index2Node and Node2Index arrays and then tries to keep | 
|  | 447 | /// the ordering up-to-date after edge insertions by reordering the DAG. | 
|  | 448 | /// | 
|  | 449 | /// On insertion of the edge X->Y, the algorithm first marks by calling DFS | 
|  | 450 | /// the nodes reachable from Y, and then shifts them using Shift to lie | 
|  | 451 | /// immediately after X in Index2Node. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 452 | void ScheduleDAGRRList::InitDAGTopologicalSorting() { | 
|  | 453 | unsigned DAGSize = SUnits.size(); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 454 | std::vector<SUnit*> WorkList; | 
|  | 455 | WorkList.reserve(DAGSize); | 
| Dan Gohman | 3a3a52d | 2008-08-27 16:29:48 +0000 | [diff] [blame] | 456 |  | 
|  | 457 | Index2Node.resize(DAGSize); | 
|  | 458 | Node2Index.resize(DAGSize); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 459 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 460 | // Initialize the data structures. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 461 | for (unsigned i = 0, e = DAGSize; i != e; ++i) { | 
|  | 462 | SUnit *SU = &SUnits[i]; | 
|  | 463 | int NodeNum = SU->NodeNum; | 
|  | 464 | unsigned Degree = SU->Succs.size(); | 
| Dan Gohman | 3a3a52d | 2008-08-27 16:29:48 +0000 | [diff] [blame] | 465 | // Temporarily use the Node2Index array as scratch space for degree counts. | 
|  | 466 | Node2Index[NodeNum] = Degree; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 467 |  | 
|  | 468 | // Is it a node without dependencies? | 
|  | 469 | if (Degree == 0) { | 
|  | 470 | assert(SU->Succs.empty() && "SUnit should have no successors"); | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 471 | // Collect leaf nodes. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 472 | WorkList.push_back(SU); | 
|  | 473 | } | 
|  | 474 | } | 
|  | 475 |  | 
| Dan Gohman | 3a3a52d | 2008-08-27 16:29:48 +0000 | [diff] [blame] | 476 | int Id = DAGSize; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 477 | while (!WorkList.empty()) { | 
|  | 478 | SUnit *SU = WorkList.back(); | 
|  | 479 | WorkList.pop_back(); | 
| Dan Gohman | 3a3a52d | 2008-08-27 16:29:48 +0000 | [diff] [blame] | 480 | Allocate(SU->NodeNum, --Id); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 481 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 482 | I != E; ++I) { | 
|  | 483 | SUnit *SU = I->Dep; | 
| Dan Gohman | 3a3a52d | 2008-08-27 16:29:48 +0000 | [diff] [blame] | 484 | if (!--Node2Index[SU->NodeNum]) | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 485 | // If all dependencies of the node are processed already, | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 486 | // then the node can be computed now. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 487 | WorkList.push_back(SU); | 
|  | 488 | } | 
|  | 489 | } | 
|  | 490 |  | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 491 | Visited.resize(DAGSize); | 
|  | 492 |  | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 493 | #ifndef NDEBUG | 
|  | 494 | // Check correctness of the ordering | 
|  | 495 | for (unsigned i = 0, e = DAGSize; i != e; ++i) { | 
|  | 496 | SUnit *SU = &SUnits[i]; | 
|  | 497 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 498 | I != E; ++I) { | 
|  | 499 | assert(Node2Index[SU->NodeNum] > Node2Index[I->Dep->NodeNum] && | 
|  | 500 | "Wrong topological sorting"); | 
|  | 501 | } | 
|  | 502 | } | 
|  | 503 | #endif | 
|  | 504 | } | 
|  | 505 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 506 | /// AddPred - adds an edge from SUnit X to SUnit Y. | 
|  | 507 | /// Updates the topological ordering if required. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 508 | bool ScheduleDAGRRList::AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial, | 
|  | 509 | unsigned PhyReg, int Cost) { | 
|  | 510 | int UpperBound, LowerBound; | 
|  | 511 | LowerBound = Node2Index[Y->NodeNum]; | 
|  | 512 | UpperBound = Node2Index[X->NodeNum]; | 
|  | 513 | bool HasLoop = false; | 
|  | 514 | // Is Ord(X) < Ord(Y) ? | 
|  | 515 | if (LowerBound < UpperBound) { | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 516 | // Update the topological order. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 517 | Visited.reset(); | 
|  | 518 | DFS(Y, UpperBound, HasLoop); | 
|  | 519 | assert(!HasLoop && "Inserted edge creates a loop!"); | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 520 | // Recompute topological indexes. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 521 | Shift(Visited, LowerBound, UpperBound); | 
|  | 522 | } | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 523 | // Now really insert the edge. | 
|  | 524 | return Y->addPred(X, isCtrl, isSpecial, PhyReg, Cost); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 525 | } | 
|  | 526 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 527 | /// RemovePred - This removes the specified node N from the predecessors of | 
|  | 528 | /// the current node M. Updates the topological ordering if required. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 529 | bool ScheduleDAGRRList::RemovePred(SUnit *M, SUnit *N, | 
|  | 530 | bool isCtrl, bool isSpecial) { | 
|  | 531 | // InitDAGTopologicalSorting(); | 
|  | 532 | return M->removePred(N, isCtrl, isSpecial); | 
|  | 533 | } | 
|  | 534 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 535 | /// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark | 
|  | 536 | /// all nodes affected by the edge insertion. These nodes will later get new | 
|  | 537 | /// topological indexes by means of the Shift method. | 
| Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 538 | void ScheduleDAGRRList::DFS(const SUnit *SU, int UpperBound, bool& HasLoop) { | 
|  | 539 | std::vector<const SUnit*> WorkList; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 540 | WorkList.reserve(SUnits.size()); | 
|  | 541 |  | 
|  | 542 | WorkList.push_back(SU); | 
|  | 543 | while (!WorkList.empty()) { | 
|  | 544 | SU = WorkList.back(); | 
|  | 545 | WorkList.pop_back(); | 
|  | 546 | Visited.set(SU->NodeNum); | 
|  | 547 | for (int I = SU->Succs.size()-1; I >= 0; --I) { | 
|  | 548 | int s = SU->Succs[I].Dep->NodeNum; | 
|  | 549 | if (Node2Index[s] == UpperBound) { | 
|  | 550 | HasLoop = true; | 
|  | 551 | return; | 
|  | 552 | } | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 553 | // Visit successors if not already and in affected region. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 554 | if (!Visited.test(s) && Node2Index[s] < UpperBound) { | 
|  | 555 | WorkList.push_back(SU->Succs[I].Dep); | 
|  | 556 | } | 
|  | 557 | } | 
|  | 558 | } | 
|  | 559 | } | 
|  | 560 |  | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 561 | /// Shift - Renumber the nodes so that the topological ordering is | 
|  | 562 | /// preserved. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 563 | void ScheduleDAGRRList::Shift(BitVector& Visited, int LowerBound, | 
|  | 564 | int UpperBound) { | 
|  | 565 | std::vector<int> L; | 
|  | 566 | int shift = 0; | 
|  | 567 | int i; | 
|  | 568 |  | 
|  | 569 | for (i = LowerBound; i <= UpperBound; ++i) { | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 570 | // w is node at topological index i. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 571 | int w = Index2Node[i]; | 
|  | 572 | if (Visited.test(w)) { | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 573 | // Unmark. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 574 | Visited.reset(w); | 
|  | 575 | L.push_back(w); | 
|  | 576 | shift = shift + 1; | 
|  | 577 | } else { | 
|  | 578 | Allocate(w, i - shift); | 
|  | 579 | } | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | for (unsigned j = 0; j < L.size(); ++j) { | 
|  | 583 | Allocate(L[j], i - shift); | 
|  | 584 | i = i + 1; | 
|  | 585 | } | 
|  | 586 | } | 
|  | 587 |  | 
|  | 588 |  | 
| Dan Gohman | fd227e9 | 2008-03-25 17:10:29 +0000 | [diff] [blame] | 589 | /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will | 
| Evan Cheng | cfd5f82 | 2007-09-27 00:25:29 +0000 | [diff] [blame] | 590 | /// create a cycle. | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 591 | bool ScheduleDAGRRList::WillCreateCycle(SUnit *SU, SUnit *TargetSU) { | 
|  | 592 | if (IsReachable(TargetSU, SU)) | 
| Evan Cheng | cfd5f82 | 2007-09-27 00:25:29 +0000 | [diff] [blame] | 593 | return true; | 
|  | 594 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 595 | I != E; ++I) | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 596 | if (I->Cost < 0 && IsReachable(TargetSU, I->Dep)) | 
| Evan Cheng | cfd5f82 | 2007-09-27 00:25:29 +0000 | [diff] [blame] | 597 | return true; | 
|  | 598 | return false; | 
|  | 599 | } | 
|  | 600 |  | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 601 | /// BacktrackBottomUp - Backtrack scheduling to a previous cycle specified in | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 602 | /// BTCycle in order to schedule a specific node. Returns the last unscheduled | 
|  | 603 | /// SUnit. Also returns if a successor is unscheduled in the process. | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 604 | void ScheduleDAGRRList::BacktrackBottomUp(SUnit *SU, unsigned BtCycle, | 
|  | 605 | unsigned &CurCycle) { | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 606 | SUnit *OldSU = NULL; | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 607 | while (CurCycle > BtCycle) { | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 608 | OldSU = Sequence.back(); | 
|  | 609 | Sequence.pop_back(); | 
|  | 610 | if (SU->isSucc(OldSU)) | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 611 | // Don't try to remove SU from AvailableQueue. | 
|  | 612 | SU->isAvailable = false; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 613 | UnscheduleNodeBottomUp(OldSU); | 
|  | 614 | --CurCycle; | 
|  | 615 | } | 
|  | 616 |  | 
|  | 617 |  | 
|  | 618 | if (SU->isSucc(OldSU)) { | 
|  | 619 | assert(false && "Something is wrong!"); | 
|  | 620 | abort(); | 
|  | 621 | } | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 622 |  | 
|  | 623 | ++NumBacktracks; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 624 | } | 
|  | 625 |  | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 626 | /// CopyAndMoveSuccessors - Clone the specified node and move its scheduled | 
|  | 627 | /// successors to the newly created node. | 
|  | 628 | SUnit *ScheduleDAGRRList::CopyAndMoveSuccessors(SUnit *SU) { | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 629 | if (SU->FlaggedNodes.size()) | 
|  | 630 | return NULL; | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 631 |  | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 632 | SDNode *N = SU->Node; | 
|  | 633 | if (!N) | 
|  | 634 | return NULL; | 
|  | 635 |  | 
|  | 636 | SUnit *NewSU; | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 637 | bool TryUnfold = false; | 
| Evan Cheng | 84d0ebc | 2007-10-05 01:42:35 +0000 | [diff] [blame] | 638 | for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) { | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 639 | MVT VT = N->getValueType(i); | 
| Evan Cheng | 84d0ebc | 2007-10-05 01:42:35 +0000 | [diff] [blame] | 640 | if (VT == MVT::Flag) | 
|  | 641 | return NULL; | 
|  | 642 | else if (VT == MVT::Other) | 
|  | 643 | TryUnfold = true; | 
|  | 644 | } | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 645 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 646 | const SDValue &Op = N->getOperand(i); | 
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 647 | MVT VT = Op.getNode()->getValueType(Op.getResNo()); | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 648 | if (VT == MVT::Flag) | 
|  | 649 | return NULL; | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 650 | } | 
|  | 651 |  | 
|  | 652 | if (TryUnfold) { | 
| Dan Gohman | e6e1348 | 2008-06-21 15:52:51 +0000 | [diff] [blame] | 653 | SmallVector<SDNode*, 2> NewNodes; | 
| Owen Anderson | 0ec92e9 | 2008-01-07 01:35:56 +0000 | [diff] [blame] | 654 | if (!TII->unfoldMemoryOperand(DAG, N, NewNodes)) | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 655 | return NULL; | 
|  | 656 |  | 
|  | 657 | DOUT << "Unfolding SU # " << SU->NodeNum << "\n"; | 
|  | 658 | assert(NewNodes.size() == 2 && "Expected a load folding node!"); | 
|  | 659 |  | 
|  | 660 | N = NewNodes[1]; | 
|  | 661 | SDNode *LoadNode = NewNodes[0]; | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 662 | unsigned NumVals = N->getNumValues(); | 
|  | 663 | unsigned OldNumVals = SU->Node->getNumValues(); | 
|  | 664 | for (unsigned i = 0; i != NumVals; ++i) | 
| Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 665 | DAG.ReplaceAllUsesOfValueWith(SDValue(SU->Node, i), SDValue(N, i)); | 
|  | 666 | DAG.ReplaceAllUsesOfValueWith(SDValue(SU->Node, OldNumVals-1), | 
|  | 667 | SDValue(LoadNode, 1)); | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 668 |  | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 669 | SUnit *NewSU = CreateNewSUnit(N); | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 670 | assert(N->getNodeId() == -1 && "Node already inserted!"); | 
|  | 671 | N->setNodeId(NewSU->NodeNum); | 
| Dan Gohman | e6e1348 | 2008-06-21 15:52:51 +0000 | [diff] [blame] | 672 |  | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 673 | const TargetInstrDesc &TID = TII->get(N->getMachineOpcode()); | 
| Dan Gohman | 856c012 | 2008-02-16 00:25:40 +0000 | [diff] [blame] | 674 | for (unsigned i = 0; i != TID.getNumOperands(); ++i) { | 
| Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 675 | if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) { | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 676 | NewSU->isTwoAddress = true; | 
|  | 677 | break; | 
|  | 678 | } | 
|  | 679 | } | 
| Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 680 | if (TID.isCommutable()) | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 681 | NewSU->isCommutable = true; | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 682 | // FIXME: Calculate height / depth and propagate the changes? | 
| Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 683 | NewSU->Depth = SU->Depth; | 
|  | 684 | NewSU->Height = SU->Height; | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 685 | ComputeLatency(NewSU); | 
|  | 686 |  | 
| Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 687 | // LoadNode may already exist. This can happen when there is another | 
|  | 688 | // load from the same location and producing the same type of value | 
|  | 689 | // but it has different alignment or volatileness. | 
|  | 690 | bool isNewLoad = true; | 
|  | 691 | SUnit *LoadSU; | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 692 | if (LoadNode->getNodeId() != -1) { | 
|  | 693 | LoadSU = &SUnits[LoadNode->getNodeId()]; | 
| Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 694 | isNewLoad = false; | 
|  | 695 | } else { | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 696 | LoadSU = CreateNewSUnit(LoadNode); | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 697 | LoadNode->setNodeId(LoadSU->NodeNum); | 
| Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 698 |  | 
|  | 699 | LoadSU->Depth = SU->Depth; | 
|  | 700 | LoadSU->Height = SU->Height; | 
|  | 701 | ComputeLatency(LoadSU); | 
|  | 702 | } | 
|  | 703 |  | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 704 | SUnit *ChainPred = NULL; | 
|  | 705 | SmallVector<SDep, 4> ChainSuccs; | 
|  | 706 | SmallVector<SDep, 4> LoadPreds; | 
|  | 707 | SmallVector<SDep, 4> NodePreds; | 
|  | 708 | SmallVector<SDep, 4> NodeSuccs; | 
|  | 709 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 710 | I != E; ++I) { | 
|  | 711 | if (I->isCtrl) | 
|  | 712 | ChainPred = I->Dep; | 
| Evan Cheng | 567d2e5 | 2008-03-04 00:41:45 +0000 | [diff] [blame] | 713 | else if (I->Dep->Node && I->Dep->Node->isOperandOf(LoadNode)) | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 714 | LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false)); | 
|  | 715 | else | 
|  | 716 | NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false)); | 
|  | 717 | } | 
|  | 718 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
|  | 719 | I != E; ++I) { | 
|  | 720 | if (I->isCtrl) | 
|  | 721 | ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost, | 
|  | 722 | I->isCtrl, I->isSpecial)); | 
|  | 723 | else | 
|  | 724 | NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost, | 
|  | 725 | I->isCtrl, I->isSpecial)); | 
|  | 726 | } | 
|  | 727 |  | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 728 | if (ChainPred) { | 
|  | 729 | RemovePred(SU, ChainPred, true, false); | 
|  | 730 | if (isNewLoad) | 
|  | 731 | AddPred(LoadSU, ChainPred, true, false); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 732 | } | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 733 | for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) { | 
|  | 734 | SDep *Pred = &LoadPreds[i]; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 735 | RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial); | 
|  | 736 | if (isNewLoad) { | 
|  | 737 | AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial, | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 738 | Pred->Reg, Pred->Cost); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 739 | } | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 740 | } | 
|  | 741 | for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) { | 
|  | 742 | SDep *Pred = &NodePreds[i]; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 743 | RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial); | 
|  | 744 | AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial, | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 745 | Pred->Reg, Pred->Cost); | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 746 | } | 
|  | 747 | for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) { | 
|  | 748 | SDep *Succ = &NodeSuccs[i]; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 749 | RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial); | 
|  | 750 | AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isSpecial, | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 751 | Succ->Reg, Succ->Cost); | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 752 | } | 
|  | 753 | for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) { | 
|  | 754 | SDep *Succ = &ChainSuccs[i]; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 755 | RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial); | 
|  | 756 | if (isNewLoad) { | 
|  | 757 | AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isSpecial, | 
| Roman Levenstein | 733a4d6 | 2008-03-26 11:23:38 +0000 | [diff] [blame] | 758 | Succ->Reg, Succ->Cost); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 759 | } | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 760 | } | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 761 | if (isNewLoad) { | 
|  | 762 | AddPred(NewSU, LoadSU, false, false); | 
|  | 763 | } | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 764 |  | 
| Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 765 | if (isNewLoad) | 
|  | 766 | AvailableQueue->addNode(LoadSU); | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 767 | AvailableQueue->addNode(NewSU); | 
|  | 768 |  | 
|  | 769 | ++NumUnfolds; | 
|  | 770 |  | 
|  | 771 | if (NewSU->NumSuccsLeft == 0) { | 
|  | 772 | NewSU->isAvailable = true; | 
|  | 773 | return NewSU; | 
| Evan Cheng | 91e0fc9 | 2007-12-18 08:42:10 +0000 | [diff] [blame] | 774 | } | 
|  | 775 | SU = NewSU; | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 776 | } | 
|  | 777 |  | 
|  | 778 | DOUT << "Duplicating SU # " << SU->NodeNum << "\n"; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 779 | NewSU = CreateClone(SU); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 780 |  | 
|  | 781 | // New SUnit has the exact same predecessors. | 
|  | 782 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 783 | I != E; ++I) | 
|  | 784 | if (!I->isSpecial) { | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 785 | AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 786 | NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1); | 
|  | 787 | } | 
|  | 788 |  | 
|  | 789 | // Only copy scheduled successors. Cut them from old node's successor | 
|  | 790 | // list and move them over. | 
| Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 791 | SmallVector<std::pair<SUnit*, bool>, 4> DelDeps; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 792 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
|  | 793 | I != E; ++I) { | 
|  | 794 | if (I->isSpecial) | 
|  | 795 | continue; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 796 | if (I->Dep->isScheduled) { | 
| Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 797 | NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 798 | AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost); | 
| Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 799 | DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl)); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 800 | } | 
|  | 801 | } | 
|  | 802 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { | 
| Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 803 | SUnit *Succ = DelDeps[i].first; | 
|  | 804 | bool isCtrl = DelDeps[i].second; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 805 | RemovePred(Succ, SU, isCtrl, false); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 806 | } | 
|  | 807 |  | 
|  | 808 | AvailableQueue->updateNode(SU); | 
|  | 809 | AvailableQueue->addNode(NewSU); | 
|  | 810 |  | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 811 | ++NumDups; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 812 | return NewSU; | 
|  | 813 | } | 
|  | 814 |  | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 815 | /// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies | 
|  | 816 | /// and move all scheduled successors of the given SUnit to the last copy. | 
|  | 817 | void ScheduleDAGRRList::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg, | 
|  | 818 | const TargetRegisterClass *DestRC, | 
|  | 819 | const TargetRegisterClass *SrcRC, | 
|  | 820 | SmallVector<SUnit*, 2> &Copies) { | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 821 | SUnit *CopyFromSU = CreateNewSUnit(NULL); | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 822 | CopyFromSU->CopySrcRC = SrcRC; | 
|  | 823 | CopyFromSU->CopyDstRC = DestRC; | 
|  | 824 | CopyFromSU->Depth = SU->Depth; | 
|  | 825 | CopyFromSU->Height = SU->Height; | 
|  | 826 |  | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 827 | SUnit *CopyToSU = CreateNewSUnit(NULL); | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 828 | CopyToSU->CopySrcRC = DestRC; | 
|  | 829 | CopyToSU->CopyDstRC = SrcRC; | 
|  | 830 |  | 
|  | 831 | // Only copy scheduled successors. Cut them from old node's successor | 
|  | 832 | // list and move them over. | 
| Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 833 | SmallVector<std::pair<SUnit*, bool>, 4> DelDeps; | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 834 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
|  | 835 | I != E; ++I) { | 
|  | 836 | if (I->isSpecial) | 
|  | 837 | continue; | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 838 | if (I->Dep->isScheduled) { | 
| Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 839 | CopyToSU->Height = std::max(CopyToSU->Height, I->Dep->Height+1); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 840 | AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost); | 
| Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 841 | DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl)); | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 842 | } | 
|  | 843 | } | 
|  | 844 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { | 
| Evan Cheng | bde499b | 2007-09-27 07:29:27 +0000 | [diff] [blame] | 845 | SUnit *Succ = DelDeps[i].first; | 
|  | 846 | bool isCtrl = DelDeps[i].second; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 847 | RemovePred(Succ, SU, isCtrl, false); | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 848 | } | 
|  | 849 |  | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 850 | AddPred(CopyFromSU, SU, false, false, Reg, -1); | 
|  | 851 | AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1); | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 852 |  | 
|  | 853 | AvailableQueue->updateNode(SU); | 
|  | 854 | AvailableQueue->addNode(CopyFromSU); | 
|  | 855 | AvailableQueue->addNode(CopyToSU); | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 856 | Copies.push_back(CopyFromSU); | 
|  | 857 | Copies.push_back(CopyToSU); | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 858 |  | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 859 | ++NumCCCopies; | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 860 | } | 
|  | 861 |  | 
|  | 862 | /// getPhysicalRegisterVT - Returns the ValueType of the physical register | 
|  | 863 | /// definition of the specified node. | 
|  | 864 | /// FIXME: Move to SelectionDAG? | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 865 | static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg, | 
|  | 866 | const TargetInstrInfo *TII) { | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 867 | const TargetInstrDesc &TID = TII->get(N->getMachineOpcode()); | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 868 | assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!"); | 
| Chris Lattner | b0d06b4 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 869 | unsigned NumRes = TID.getNumDefs(); | 
|  | 870 | for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) { | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 871 | if (Reg == *ImpDef) | 
|  | 872 | break; | 
|  | 873 | ++NumRes; | 
|  | 874 | } | 
|  | 875 | return N->getValueType(NumRes); | 
|  | 876 | } | 
|  | 877 |  | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 878 | /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay | 
|  | 879 | /// scheduling of the given node to satisfy live physical register dependencies. | 
|  | 880 | /// If the specific node is the last one that's available to schedule, do | 
|  | 881 | /// whatever is necessary (i.e. backtracking or cloning) to make it possible. | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 882 | bool ScheduleDAGRRList::DelayForLiveRegsBottomUp(SUnit *SU, | 
|  | 883 | SmallVector<unsigned, 4> &LRegs){ | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 884 | if (NumLiveRegs == 0) | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 885 | return false; | 
|  | 886 |  | 
| Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 887 | SmallSet<unsigned, 4> RegAdded; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 888 | // 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] | 889 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 890 | I != E; ++I) { | 
|  | 891 | if (I->Cost < 0)  { | 
|  | 892 | unsigned Reg = I->Reg; | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 893 | if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->Dep) { | 
| Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 894 | if (RegAdded.insert(Reg)) | 
|  | 895 | LRegs.push_back(Reg); | 
|  | 896 | } | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 897 | for (const unsigned *Alias = TRI->getAliasSet(Reg); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 898 | *Alias; ++Alias) | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 899 | if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->Dep) { | 
| Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 900 | if (RegAdded.insert(*Alias)) | 
|  | 901 | LRegs.push_back(*Alias); | 
|  | 902 | } | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 903 | } | 
|  | 904 | } | 
|  | 905 |  | 
|  | 906 | for (unsigned i = 0, e = SU->FlaggedNodes.size()+1; i != e; ++i) { | 
|  | 907 | SDNode *Node = (i == 0) ? SU->Node : SU->FlaggedNodes[i-1]; | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 908 | if (!Node || !Node->isMachineOpcode()) | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 909 | continue; | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 910 | const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode()); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 911 | if (!TID.ImplicitDefs) | 
|  | 912 | continue; | 
|  | 913 | for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) { | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 914 | if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) { | 
| Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 915 | if (RegAdded.insert(*Reg)) | 
|  | 916 | LRegs.push_back(*Reg); | 
|  | 917 | } | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 918 | for (const unsigned *Alias = TRI->getAliasSet(*Reg); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 919 | *Alias; ++Alias) | 
| Dan Gohman | c07f686 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 920 | if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) { | 
| Evan Cheng | e6f9225 | 2007-09-27 18:46:06 +0000 | [diff] [blame] | 921 | if (RegAdded.insert(*Alias)) | 
|  | 922 | LRegs.push_back(*Alias); | 
|  | 923 | } | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 924 | } | 
|  | 925 | } | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 926 | return !LRegs.empty(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 927 | } | 
|  | 928 |  | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 929 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 930 | /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up | 
|  | 931 | /// schedulers. | 
|  | 932 | void ScheduleDAGRRList::ListScheduleBottomUp() { | 
|  | 933 | unsigned CurCycle = 0; | 
|  | 934 | // Add root to Available queue. | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 935 | if (!SUnits.empty()) { | 
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 936 | SUnit *RootSU = &SUnits[DAG.getRoot().getNode()->getNodeId()]; | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 937 | assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!"); | 
|  | 938 | RootSU->isAvailable = true; | 
|  | 939 | AvailableQueue->push(RootSU); | 
|  | 940 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 941 |  | 
|  | 942 | // While Available queue is not empty, grab the node with the highest | 
| Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 943 | // priority. If it is not ready put it back.  Schedule the node. | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 944 | SmallVector<SUnit*, 4> NotReady; | 
| Dan Gohman | fa63cc4 | 2008-06-23 21:15:00 +0000 | [diff] [blame] | 945 | DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap; | 
| Dan Gohman | e6e1348 | 2008-06-21 15:52:51 +0000 | [diff] [blame] | 946 | Sequence.reserve(SUnits.size()); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 947 | while (!AvailableQueue->empty()) { | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 948 | bool Delayed = false; | 
| Dan Gohman | fa63cc4 | 2008-06-23 21:15:00 +0000 | [diff] [blame] | 949 | LRegsMap.clear(); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 950 | SUnit *CurSU = AvailableQueue->pop(); | 
|  | 951 | while (CurSU) { | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 952 | if (CurSU->CycleBound <= CurCycle) { | 
|  | 953 | SmallVector<unsigned, 4> LRegs; | 
|  | 954 | if (!DelayForLiveRegsBottomUp(CurSU, LRegs)) | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 955 | break; | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 956 | Delayed = true; | 
|  | 957 | LRegsMap.insert(std::make_pair(CurSU, LRegs)); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 958 | } | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 959 |  | 
|  | 960 | CurSU->isPending = true;  // This SU is not in AvailableQueue right now. | 
|  | 961 | NotReady.push_back(CurSU); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 962 | CurSU = AvailableQueue->pop(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 963 | } | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 964 |  | 
|  | 965 | // All candidates are delayed due to live physical reg dependencies. | 
|  | 966 | // Try backtracking, code duplication, or inserting cross class copies | 
|  | 967 | // to resolve it. | 
|  | 968 | if (Delayed && !CurSU) { | 
|  | 969 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { | 
|  | 970 | SUnit *TrySU = NotReady[i]; | 
|  | 971 | SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU]; | 
|  | 972 |  | 
|  | 973 | // Try unscheduling up to the point where it's safe to schedule | 
|  | 974 | // this node. | 
|  | 975 | unsigned LiveCycle = CurCycle; | 
|  | 976 | for (unsigned j = 0, ee = LRegs.size(); j != ee; ++j) { | 
|  | 977 | unsigned Reg = LRegs[j]; | 
|  | 978 | unsigned LCycle = LiveRegCycles[Reg]; | 
|  | 979 | LiveCycle = std::min(LiveCycle, LCycle); | 
|  | 980 | } | 
|  | 981 | SUnit *OldSU = Sequence[LiveCycle]; | 
|  | 982 | if (!WillCreateCycle(TrySU, OldSU))  { | 
|  | 983 | BacktrackBottomUp(TrySU, LiveCycle, CurCycle); | 
|  | 984 | // Force the current node to be scheduled before the node that | 
|  | 985 | // requires the physical reg dep. | 
|  | 986 | if (OldSU->isAvailable) { | 
|  | 987 | OldSU->isAvailable = false; | 
|  | 988 | AvailableQueue->remove(OldSU); | 
|  | 989 | } | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 990 | AddPred(TrySU, OldSU, true, true); | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 991 | // If one or more successors has been unscheduled, then the current | 
|  | 992 | // node is no longer avaialable. Schedule a successor that's now | 
|  | 993 | // available instead. | 
|  | 994 | if (!TrySU->isAvailable) | 
|  | 995 | CurSU = AvailableQueue->pop(); | 
|  | 996 | else { | 
|  | 997 | CurSU = TrySU; | 
|  | 998 | TrySU->isPending = false; | 
|  | 999 | NotReady.erase(NotReady.begin()+i); | 
|  | 1000 | } | 
|  | 1001 | break; | 
|  | 1002 | } | 
|  | 1003 | } | 
|  | 1004 |  | 
|  | 1005 | if (!CurSU) { | 
| Dan Gohman | fd227e9 | 2008-03-25 17:10:29 +0000 | [diff] [blame] | 1006 | // Can't backtrack. Try duplicating the nodes that produces these | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 1007 | // "expensive to copy" values to break the dependency. In case even | 
|  | 1008 | // that doesn't work, insert cross class copies. | 
|  | 1009 | SUnit *TrySU = NotReady[0]; | 
|  | 1010 | SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU]; | 
|  | 1011 | assert(LRegs.size() == 1 && "Can't handle this yet!"); | 
|  | 1012 | unsigned Reg = LRegs[0]; | 
|  | 1013 | SUnit *LRDef = LiveRegDefs[Reg]; | 
| Evan Cheng | 79e9713 | 2007-10-05 01:39:18 +0000 | [diff] [blame] | 1014 | SUnit *NewDef = CopyAndMoveSuccessors(LRDef); | 
|  | 1015 | if (!NewDef) { | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 1016 | // Issue expensive cross register class copies. | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1017 | MVT VT = getPhysicalRegisterVT(LRDef->Node, Reg, TII); | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 1018 | const TargetRegisterClass *RC = | 
| Evan Cheng | e88a625 | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 1019 | TRI->getPhysicalRegisterRegClass(Reg, VT); | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1020 | const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC); | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 1021 | if (!DestRC) { | 
|  | 1022 | assert(false && "Don't know how to copy this physical register!"); | 
|  | 1023 | abort(); | 
|  | 1024 | } | 
|  | 1025 | SmallVector<SUnit*, 2> Copies; | 
|  | 1026 | InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies); | 
|  | 1027 | DOUT << "Adding an edge from SU # " << TrySU->NodeNum | 
|  | 1028 | << " to SU #" << Copies.front()->NodeNum << "\n"; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 1029 | AddPred(TrySU, Copies.front(), true, true); | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 1030 | NewDef = Copies.back(); | 
|  | 1031 | } | 
|  | 1032 |  | 
|  | 1033 | DOUT << "Adding an edge from SU # " << NewDef->NodeNum | 
|  | 1034 | << " to SU #" << TrySU->NodeNum << "\n"; | 
|  | 1035 | LiveRegDefs[Reg] = NewDef; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 1036 | AddPred(NewDef, TrySU, true, true); | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 1037 | TrySU->isAvailable = false; | 
|  | 1038 | CurSU = NewDef; | 
|  | 1039 | } | 
|  | 1040 |  | 
|  | 1041 | if (!CurSU) { | 
|  | 1042 | assert(false && "Unable to resolve live physical register dependencies!"); | 
|  | 1043 | abort(); | 
|  | 1044 | } | 
|  | 1045 | } | 
|  | 1046 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1047 | // Add the nodes that aren't ready back onto the available list. | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1048 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { | 
|  | 1049 | NotReady[i]->isPending = false; | 
| Evan Cheng | 1ec79b4 | 2007-09-27 07:09:03 +0000 | [diff] [blame] | 1050 | // May no longer be available due to backtracking. | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1051 | if (NotReady[i]->isAvailable) | 
|  | 1052 | AvailableQueue->push(NotReady[i]); | 
|  | 1053 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1054 | NotReady.clear(); | 
|  | 1055 |  | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1056 | if (!CurSU) | 
|  | 1057 | Sequence.push_back(0); | 
|  | 1058 | else { | 
|  | 1059 | ScheduleNodeBottomUp(CurSU, CurCycle); | 
|  | 1060 | Sequence.push_back(CurSU); | 
|  | 1061 | } | 
|  | 1062 | ++CurCycle; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1063 | } | 
|  | 1064 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1065 | // Reverse the order if it is bottom up. | 
|  | 1066 | std::reverse(Sequence.begin(), Sequence.end()); | 
|  | 1067 |  | 
|  | 1068 |  | 
|  | 1069 | #ifndef NDEBUG | 
|  | 1070 | // Verify that all SUnits were scheduled. | 
|  | 1071 | bool AnyNotSched = false; | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 1072 | unsigned DeadNodes = 0; | 
| Dan Gohman | 82b6673 | 2008-04-15 22:40:14 +0000 | [diff] [blame] | 1073 | unsigned Noops = 0; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1074 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 1075 | if (!SUnits[i].isScheduled) { | 
|  | 1076 | if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) { | 
|  | 1077 | ++DeadNodes; | 
|  | 1078 | continue; | 
|  | 1079 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1080 | if (!AnyNotSched) | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 1081 | cerr << "*** List scheduling failed! ***\n"; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1082 | SUnits[i].dump(&DAG); | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 1083 | cerr << "has not been scheduled!\n"; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1084 | AnyNotSched = true; | 
|  | 1085 | } | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 1086 | if (SUnits[i].NumSuccsLeft != 0) { | 
|  | 1087 | if (!AnyNotSched) | 
|  | 1088 | cerr << "*** List scheduling failed! ***\n"; | 
|  | 1089 | SUnits[i].dump(&DAG); | 
|  | 1090 | cerr << "has successors left!\n"; | 
|  | 1091 | AnyNotSched = true; | 
|  | 1092 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1093 | } | 
| Dan Gohman | 82b6673 | 2008-04-15 22:40:14 +0000 | [diff] [blame] | 1094 | for (unsigned i = 0, e = Sequence.size(); i != e; ++i) | 
|  | 1095 | if (!Sequence[i]) | 
|  | 1096 | ++Noops; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1097 | assert(!AnyNotSched); | 
| Dan Gohman | 82b6673 | 2008-04-15 22:40:14 +0000 | [diff] [blame] | 1098 | assert(Sequence.size() + DeadNodes - Noops == SUnits.size() && | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 1099 | "The number of nodes scheduled doesn't match the expected number!"); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1100 | #endif | 
|  | 1101 | } | 
|  | 1102 |  | 
|  | 1103 | //===----------------------------------------------------------------------===// | 
|  | 1104 | //  Top-Down Scheduling | 
|  | 1105 | //===----------------------------------------------------------------------===// | 
|  | 1106 |  | 
|  | 1107 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to | 
| Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 1108 | /// the AvailableQueue if the count reaches zero. Also update its cycle bound. | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1109 | void ScheduleDAGRRList::ReleaseSucc(SUnit *SuccSU, bool isChain, | 
|  | 1110 | unsigned CurCycle) { | 
|  | 1111 | // FIXME: the distance between two nodes is not always == the predecessor's | 
|  | 1112 | // latency. For example, the reader can very well read the register written | 
|  | 1113 | // by the predecessor later than the issue cycle. It also depends on the | 
|  | 1114 | // interrupt model (drain vs. freeze). | 
|  | 1115 | SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurCycle + SuccSU->Latency); | 
|  | 1116 |  | 
| Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 1117 | --SuccSU->NumPredsLeft; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1118 |  | 
|  | 1119 | #ifndef NDEBUG | 
| Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 1120 | if (SuccSU->NumPredsLeft < 0) { | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 1121 | cerr << "*** List scheduling failed! ***\n"; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1122 | SuccSU->dump(&DAG); | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 1123 | cerr << " has been released too many times!\n"; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1124 | assert(0); | 
|  | 1125 | } | 
|  | 1126 | #endif | 
|  | 1127 |  | 
| Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 1128 | if (SuccSU->NumPredsLeft == 0) { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1129 | SuccSU->isAvailable = true; | 
|  | 1130 | AvailableQueue->push(SuccSU); | 
|  | 1131 | } | 
|  | 1132 | } | 
|  | 1133 |  | 
|  | 1134 |  | 
|  | 1135 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending | 
|  | 1136 | /// count of its successors. If a successor pending count is zero, add it to | 
|  | 1137 | /// the Available queue. | 
| Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 1138 | void ScheduleDAGRRList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 1139 | DOUT << "*** Scheduling [" << CurCycle << "]: "; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1140 | DEBUG(SU->dump(&DAG)); | 
|  | 1141 | SU->Cycle = CurCycle; | 
|  | 1142 |  | 
|  | 1143 | AvailableQueue->ScheduledNode(SU); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1144 |  | 
|  | 1145 | // Top down: release successors | 
| Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 1146 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
|  | 1147 | I != E; ++I) | 
| Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1148 | ReleaseSucc(I->Dep, I->isCtrl, CurCycle); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1149 | SU->isScheduled = true; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1150 | } | 
|  | 1151 |  | 
| Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 1152 | /// ListScheduleTopDown - The main loop of list scheduling for top-down | 
|  | 1153 | /// schedulers. | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1154 | void ScheduleDAGRRList::ListScheduleTopDown() { | 
|  | 1155 | unsigned CurCycle = 0; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1156 |  | 
|  | 1157 | // All leaves to Available queue. | 
|  | 1158 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { | 
|  | 1159 | // It is available if it has no predecessors. | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 1160 | if (SUnits[i].Preds.empty()) { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1161 | AvailableQueue->push(&SUnits[i]); | 
|  | 1162 | SUnits[i].isAvailable = true; | 
|  | 1163 | } | 
|  | 1164 | } | 
|  | 1165 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1166 | // While Available queue is not empty, grab the node with the highest | 
| Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 1167 | // priority. If it is not ready put it back.  Schedule the node. | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1168 | std::vector<SUnit*> NotReady; | 
| Dan Gohman | e6e1348 | 2008-06-21 15:52:51 +0000 | [diff] [blame] | 1169 | Sequence.reserve(SUnits.size()); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1170 | while (!AvailableQueue->empty()) { | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1171 | SUnit *CurSU = AvailableQueue->pop(); | 
|  | 1172 | while (CurSU && CurSU->CycleBound > CurCycle) { | 
|  | 1173 | NotReady.push_back(CurSU); | 
|  | 1174 | CurSU = AvailableQueue->pop(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1175 | } | 
|  | 1176 |  | 
|  | 1177 | // Add the nodes that aren't ready back onto the available list. | 
|  | 1178 | AvailableQueue->push_all(NotReady); | 
|  | 1179 | NotReady.clear(); | 
|  | 1180 |  | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1181 | if (!CurSU) | 
|  | 1182 | Sequence.push_back(0); | 
|  | 1183 | else { | 
|  | 1184 | ScheduleNodeTopDown(CurSU, CurCycle); | 
|  | 1185 | Sequence.push_back(CurSU); | 
|  | 1186 | } | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 1187 | ++CurCycle; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1188 | } | 
|  | 1189 |  | 
|  | 1190 |  | 
|  | 1191 | #ifndef NDEBUG | 
|  | 1192 | // Verify that all SUnits were scheduled. | 
|  | 1193 | bool AnyNotSched = false; | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 1194 | unsigned DeadNodes = 0; | 
| Dan Gohman | 82b6673 | 2008-04-15 22:40:14 +0000 | [diff] [blame] | 1195 | unsigned Noops = 0; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1196 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { | 
|  | 1197 | if (!SUnits[i].isScheduled) { | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 1198 | if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) { | 
|  | 1199 | ++DeadNodes; | 
|  | 1200 | continue; | 
|  | 1201 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1202 | if (!AnyNotSched) | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 1203 | cerr << "*** List scheduling failed! ***\n"; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1204 | SUnits[i].dump(&DAG); | 
| Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 1205 | cerr << "has not been scheduled!\n"; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1206 | AnyNotSched = true; | 
|  | 1207 | } | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 1208 | if (SUnits[i].NumPredsLeft != 0) { | 
|  | 1209 | if (!AnyNotSched) | 
|  | 1210 | cerr << "*** List scheduling failed! ***\n"; | 
|  | 1211 | SUnits[i].dump(&DAG); | 
|  | 1212 | cerr << "has predecessors left!\n"; | 
|  | 1213 | AnyNotSched = true; | 
|  | 1214 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1215 | } | 
| Dan Gohman | 82b6673 | 2008-04-15 22:40:14 +0000 | [diff] [blame] | 1216 | for (unsigned i = 0, e = Sequence.size(); i != e; ++i) | 
|  | 1217 | if (!Sequence[i]) | 
|  | 1218 | ++Noops; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1219 | assert(!AnyNotSched); | 
| Dan Gohman | 82b6673 | 2008-04-15 22:40:14 +0000 | [diff] [blame] | 1220 | assert(Sequence.size() + DeadNodes - Noops == SUnits.size() && | 
| Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 1221 | "The number of nodes scheduled doesn't match the expected number!"); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1222 | #endif | 
|  | 1223 | } | 
|  | 1224 |  | 
|  | 1225 |  | 
|  | 1226 |  | 
|  | 1227 | //===----------------------------------------------------------------------===// | 
|  | 1228 | //                RegReductionPriorityQueue Implementation | 
|  | 1229 | //===----------------------------------------------------------------------===// | 
|  | 1230 | // | 
|  | 1231 | // This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers | 
|  | 1232 | // to reduce register pressure. | 
|  | 1233 | // | 
|  | 1234 | namespace { | 
|  | 1235 | template<class SF> | 
|  | 1236 | class RegReductionPriorityQueue; | 
|  | 1237 |  | 
|  | 1238 | /// Sorting functions for the Available queue. | 
|  | 1239 | struct bu_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { | 
|  | 1240 | RegReductionPriorityQueue<bu_ls_rr_sort> *SPQ; | 
|  | 1241 | bu_ls_rr_sort(RegReductionPriorityQueue<bu_ls_rr_sort> *spq) : SPQ(spq) {} | 
|  | 1242 | bu_ls_rr_sort(const bu_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} | 
|  | 1243 |  | 
|  | 1244 | bool operator()(const SUnit* left, const SUnit* right) const; | 
|  | 1245 | }; | 
|  | 1246 |  | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1247 | struct bu_ls_rr_fast_sort : public std::binary_function<SUnit*, SUnit*, bool>{ | 
|  | 1248 | RegReductionPriorityQueue<bu_ls_rr_fast_sort> *SPQ; | 
|  | 1249 | bu_ls_rr_fast_sort(RegReductionPriorityQueue<bu_ls_rr_fast_sort> *spq) | 
|  | 1250 | : SPQ(spq) {} | 
|  | 1251 | bu_ls_rr_fast_sort(const bu_ls_rr_fast_sort &RHS) : SPQ(RHS.SPQ) {} | 
|  | 1252 |  | 
|  | 1253 | bool operator()(const SUnit* left, const SUnit* right) const; | 
|  | 1254 | }; | 
|  | 1255 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1256 | struct td_ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { | 
|  | 1257 | RegReductionPriorityQueue<td_ls_rr_sort> *SPQ; | 
|  | 1258 | td_ls_rr_sort(RegReductionPriorityQueue<td_ls_rr_sort> *spq) : SPQ(spq) {} | 
|  | 1259 | td_ls_rr_sort(const td_ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} | 
|  | 1260 |  | 
|  | 1261 | bool operator()(const SUnit* left, const SUnit* right) const; | 
|  | 1262 | }; | 
|  | 1263 | }  // end anonymous namespace | 
|  | 1264 |  | 
| Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1265 | static inline bool isCopyFromLiveIn(const SUnit *SU) { | 
|  | 1266 | SDNode *N = SU->Node; | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 1267 | return N && N->getOpcode() == ISD::CopyFromReg && | 
| Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1268 | N->getOperand(N->getNumOperands()-1).getValueType() != MVT::Flag; | 
|  | 1269 | } | 
|  | 1270 |  | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1271 | /// CalcNodeBUSethiUllmanNumber - Compute Sethi Ullman number for bottom up | 
|  | 1272 | /// scheduling. Smaller number is the higher priority. | 
|  | 1273 | static unsigned | 
|  | 1274 | CalcNodeBUSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) { | 
|  | 1275 | unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum]; | 
|  | 1276 | if (SethiUllmanNumber != 0) | 
|  | 1277 | return SethiUllmanNumber; | 
|  | 1278 |  | 
|  | 1279 | unsigned Extra = 0; | 
|  | 1280 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 1281 | I != E; ++I) { | 
|  | 1282 | if (I->isCtrl) continue;  // ignore chain preds | 
|  | 1283 | SUnit *PredSU = I->Dep; | 
|  | 1284 | unsigned PredSethiUllman = CalcNodeBUSethiUllmanNumber(PredSU, SUNumbers); | 
|  | 1285 | if (PredSethiUllman > SethiUllmanNumber) { | 
|  | 1286 | SethiUllmanNumber = PredSethiUllman; | 
|  | 1287 | Extra = 0; | 
|  | 1288 | } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl) | 
|  | 1289 | ++Extra; | 
|  | 1290 | } | 
|  | 1291 |  | 
|  | 1292 | SethiUllmanNumber += Extra; | 
|  | 1293 |  | 
|  | 1294 | if (SethiUllmanNumber == 0) | 
|  | 1295 | SethiUllmanNumber = 1; | 
|  | 1296 |  | 
|  | 1297 | return SethiUllmanNumber; | 
|  | 1298 | } | 
|  | 1299 |  | 
|  | 1300 | /// CalcNodeTDSethiUllmanNumber - Compute Sethi Ullman number for top down | 
|  | 1301 | /// scheduling. Smaller number is the higher priority. | 
|  | 1302 | static unsigned | 
|  | 1303 | CalcNodeTDSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) { | 
|  | 1304 | unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum]; | 
|  | 1305 | if (SethiUllmanNumber != 0) | 
|  | 1306 | return SethiUllmanNumber; | 
|  | 1307 |  | 
|  | 1308 | unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0; | 
|  | 1309 | if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg) | 
|  | 1310 | SethiUllmanNumber = 0xffff; | 
|  | 1311 | else if (SU->NumSuccsLeft == 0) | 
|  | 1312 | // If SU does not have a use, i.e. it doesn't produce a value that would | 
|  | 1313 | // be consumed (e.g. store), then it terminates a chain of computation. | 
|  | 1314 | // Give it a small SethiUllman number so it will be scheduled right before | 
|  | 1315 | // its predecessors that it doesn't lengthen their live ranges. | 
|  | 1316 | SethiUllmanNumber = 0; | 
|  | 1317 | else if (SU->NumPredsLeft == 0 && | 
|  | 1318 | (Opc != ISD::CopyFromReg || isCopyFromLiveIn(SU))) | 
|  | 1319 | SethiUllmanNumber = 0xffff; | 
|  | 1320 | else { | 
|  | 1321 | int Extra = 0; | 
|  | 1322 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 1323 | I != E; ++I) { | 
|  | 1324 | if (I->isCtrl) continue;  // ignore chain preds | 
|  | 1325 | SUnit *PredSU = I->Dep; | 
|  | 1326 | unsigned PredSethiUllman = CalcNodeTDSethiUllmanNumber(PredSU, SUNumbers); | 
|  | 1327 | if (PredSethiUllman > SethiUllmanNumber) { | 
|  | 1328 | SethiUllmanNumber = PredSethiUllman; | 
|  | 1329 | Extra = 0; | 
|  | 1330 | } else if (PredSethiUllman == SethiUllmanNumber && !I->isCtrl) | 
|  | 1331 | ++Extra; | 
|  | 1332 | } | 
|  | 1333 |  | 
|  | 1334 | SethiUllmanNumber += Extra; | 
|  | 1335 | } | 
|  | 1336 |  | 
|  | 1337 | return SethiUllmanNumber; | 
|  | 1338 | } | 
|  | 1339 |  | 
|  | 1340 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1341 | namespace { | 
|  | 1342 | template<class SF> | 
| Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 1343 | class VISIBILITY_HIDDEN RegReductionPriorityQueue | 
|  | 1344 | : public SchedulingPriorityQueue { | 
| Dan Gohman | a4db335 | 2008-06-21 18:35:25 +0000 | [diff] [blame] | 1345 | PriorityQueue<SUnit*, std::vector<SUnit*>, SF> Queue; | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1346 | unsigned currentQueueId; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1347 |  | 
|  | 1348 | public: | 
|  | 1349 | RegReductionPriorityQueue() : | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1350 | Queue(SF(this)), currentQueueId(0) {} | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1351 |  | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 1352 | virtual void initNodes(std::vector<SUnit> &sunits) {} | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1353 |  | 
|  | 1354 | virtual void addNode(const SUnit *SU) {} | 
|  | 1355 |  | 
|  | 1356 | virtual void updateNode(const SUnit *SU) {} | 
|  | 1357 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1358 | virtual void releaseState() {} | 
|  | 1359 |  | 
| Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1360 | virtual unsigned getNodePriority(const SUnit *SU) const { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1361 | return 0; | 
|  | 1362 | } | 
|  | 1363 |  | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1364 | unsigned size() const { return Queue.size(); } | 
|  | 1365 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1366 | bool empty() const { return Queue.empty(); } | 
|  | 1367 |  | 
|  | 1368 | void push(SUnit *U) { | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1369 | assert(!U->NodeQueueId && "Node in the queue already"); | 
|  | 1370 | U->NodeQueueId = ++currentQueueId; | 
| Dan Gohman | a4db335 | 2008-06-21 18:35:25 +0000 | [diff] [blame] | 1371 | Queue.push(U); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1372 | } | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1373 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1374 | void push_all(const std::vector<SUnit *> &Nodes) { | 
|  | 1375 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1376 | push(Nodes[i]); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1377 | } | 
|  | 1378 |  | 
|  | 1379 | SUnit *pop() { | 
| Evan Cheng | d12c97d | 2006-05-30 18:05:39 +0000 | [diff] [blame] | 1380 | if (empty()) return NULL; | 
| Dan Gohman | a4db335 | 2008-06-21 18:35:25 +0000 | [diff] [blame] | 1381 | SUnit *V = Queue.top(); | 
|  | 1382 | Queue.pop(); | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1383 | V->NodeQueueId = 0; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1384 | return V; | 
|  | 1385 | } | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1386 |  | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1387 | void remove(SUnit *SU) { | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1388 | assert(!Queue.empty() && "Queue is empty!"); | 
| Dan Gohman | a4db335 | 2008-06-21 18:35:25 +0000 | [diff] [blame] | 1389 | assert(SU->NodeQueueId != 0 && "Not in queue!"); | 
|  | 1390 | Queue.erase_one(SU); | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1391 | SU->NodeQueueId = 0; | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1392 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1393 | }; | 
|  | 1394 |  | 
| Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 1395 | class VISIBILITY_HIDDEN BURegReductionPriorityQueue | 
| Dan Gohman | 4b49be1 | 2008-06-21 01:08:22 +0000 | [diff] [blame] | 1396 | : public RegReductionPriorityQueue<bu_ls_rr_sort> { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1397 | // SUnits - The SUnits for the current graph. | 
| Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 1398 | std::vector<SUnit> *SUnits; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1399 |  | 
|  | 1400 | // SethiUllmanNumbers - The SethiUllman number for each node. | 
| Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1401 | std::vector<unsigned> SethiUllmanNumbers; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1402 |  | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1403 | const TargetInstrInfo *TII; | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1404 | const TargetRegisterInfo *TRI; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 1405 | ScheduleDAGRRList *scheduleDAG; | 
| Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 1406 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1407 | public: | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1408 | explicit BURegReductionPriorityQueue(const TargetInstrInfo *tii, | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1409 | const TargetRegisterInfo *tri) | 
|  | 1410 | : TII(tii), TRI(tri), scheduleDAG(NULL) {} | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1411 |  | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 1412 | void initNodes(std::vector<SUnit> &sunits) { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1413 | SUnits = &sunits; | 
|  | 1414 | // Add pseudo dependency edges for two-address nodes. | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1415 | AddPseudoTwoAddrDeps(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1416 | // Calculate node priorities. | 
| Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1417 | CalculateSethiUllmanNumbers(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1418 | } | 
|  | 1419 |  | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1420 | void addNode(const SUnit *SU) { | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1421 | unsigned SUSize = SethiUllmanNumbers.size(); | 
|  | 1422 | if (SUnits->size() > SUSize) | 
|  | 1423 | SethiUllmanNumbers.resize(SUSize*2, 0); | 
|  | 1424 | CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1425 | } | 
|  | 1426 |  | 
|  | 1427 | void updateNode(const SUnit *SU) { | 
|  | 1428 | SethiUllmanNumbers[SU->NodeNum] = 0; | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1429 | CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1430 | } | 
|  | 1431 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1432 | void releaseState() { | 
|  | 1433 | SUnits = 0; | 
|  | 1434 | SethiUllmanNumbers.clear(); | 
|  | 1435 | } | 
|  | 1436 |  | 
| Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1437 | unsigned getNodePriority(const SUnit *SU) const { | 
| Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1438 | assert(SU->NodeNum < SethiUllmanNumbers.size()); | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 1439 | unsigned Opc = SU->Node ? SU->Node->getOpcode() : 0; | 
| Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1440 | if (Opc == ISD::CopyFromReg && !isCopyFromLiveIn(SU)) | 
|  | 1441 | // CopyFromReg should be close to its def because it restricts | 
|  | 1442 | // allocation choices. But if it is a livein then perhaps we want it | 
|  | 1443 | // closer to its uses so it can be coalesced. | 
|  | 1444 | return 0xffff; | 
|  | 1445 | else if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg) | 
|  | 1446 | // CopyToReg should be close to its uses to facilitate coalescing and | 
|  | 1447 | // avoid spilling. | 
|  | 1448 | return 0; | 
| Evan Cheng | aa2d6ef | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1449 | else if (Opc == TargetInstrInfo::EXTRACT_SUBREG || | 
|  | 1450 | Opc == TargetInstrInfo::INSERT_SUBREG) | 
|  | 1451 | // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to | 
|  | 1452 | // facilitate coalescing. | 
|  | 1453 | return 0; | 
| Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1454 | else if (SU->NumSuccs == 0) | 
|  | 1455 | // If SU does not have a use, i.e. it doesn't produce a value that would | 
|  | 1456 | // be consumed (e.g. store), then it terminates a chain of computation. | 
|  | 1457 | // Give it a large SethiUllman number so it will be scheduled right | 
|  | 1458 | // before its predecessors that it doesn't lengthen their live ranges. | 
|  | 1459 | return 0xffff; | 
|  | 1460 | else if (SU->NumPreds == 0) | 
|  | 1461 | // If SU does not have a def, schedule it close to its uses because it | 
|  | 1462 | // does not lengthen any live ranges. | 
|  | 1463 | return 0; | 
|  | 1464 | else | 
|  | 1465 | return SethiUllmanNumbers[SU->NodeNum]; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1466 | } | 
|  | 1467 |  | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 1468 | void setScheduleDAG(ScheduleDAGRRList *scheduleDag) { | 
|  | 1469 | scheduleDAG = scheduleDag; | 
|  | 1470 | } | 
|  | 1471 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1472 | private: | 
| Evan Cheng | 73bdf04 | 2008-03-01 00:39:47 +0000 | [diff] [blame] | 1473 | bool canClobber(const SUnit *SU, const SUnit *Op); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1474 | void AddPseudoTwoAddrDeps(); | 
| Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1475 | void CalculateSethiUllmanNumbers(); | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1476 | }; | 
|  | 1477 |  | 
|  | 1478 |  | 
|  | 1479 | class VISIBILITY_HIDDEN BURegReductionFastPriorityQueue | 
|  | 1480 | : public RegReductionPriorityQueue<bu_ls_rr_fast_sort> { | 
|  | 1481 | // SUnits - The SUnits for the current graph. | 
|  | 1482 | const std::vector<SUnit> *SUnits; | 
|  | 1483 |  | 
|  | 1484 | // SethiUllmanNumbers - The SethiUllman number for each node. | 
|  | 1485 | std::vector<unsigned> SethiUllmanNumbers; | 
|  | 1486 | public: | 
|  | 1487 | explicit BURegReductionFastPriorityQueue() {} | 
|  | 1488 |  | 
|  | 1489 | void initNodes(std::vector<SUnit> &sunits) { | 
|  | 1490 | SUnits = &sunits; | 
|  | 1491 | // Calculate node priorities. | 
|  | 1492 | CalculateSethiUllmanNumbers(); | 
|  | 1493 | } | 
|  | 1494 |  | 
|  | 1495 | void addNode(const SUnit *SU) { | 
|  | 1496 | unsigned SUSize = SethiUllmanNumbers.size(); | 
|  | 1497 | if (SUnits->size() > SUSize) | 
|  | 1498 | SethiUllmanNumbers.resize(SUSize*2, 0); | 
|  | 1499 | CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers); | 
|  | 1500 | } | 
|  | 1501 |  | 
|  | 1502 | void updateNode(const SUnit *SU) { | 
|  | 1503 | SethiUllmanNumbers[SU->NodeNum] = 0; | 
|  | 1504 | CalcNodeBUSethiUllmanNumber(SU, SethiUllmanNumbers); | 
|  | 1505 | } | 
|  | 1506 |  | 
|  | 1507 | void releaseState() { | 
|  | 1508 | SUnits = 0; | 
|  | 1509 | SethiUllmanNumbers.clear(); | 
|  | 1510 | } | 
|  | 1511 |  | 
|  | 1512 | unsigned getNodePriority(const SUnit *SU) const { | 
|  | 1513 | return SethiUllmanNumbers[SU->NodeNum]; | 
|  | 1514 | } | 
|  | 1515 |  | 
|  | 1516 | private: | 
|  | 1517 | void CalculateSethiUllmanNumbers(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1518 | }; | 
|  | 1519 |  | 
|  | 1520 |  | 
| Dan Gohman | 54a187e | 2007-08-20 19:28:38 +0000 | [diff] [blame] | 1521 | class VISIBILITY_HIDDEN TDRegReductionPriorityQueue | 
| Dan Gohman | 4b49be1 | 2008-06-21 01:08:22 +0000 | [diff] [blame] | 1522 | : public RegReductionPriorityQueue<td_ls_rr_sort> { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1523 | // SUnits - The SUnits for the current graph. | 
|  | 1524 | const std::vector<SUnit> *SUnits; | 
|  | 1525 |  | 
|  | 1526 | // SethiUllmanNumbers - The SethiUllman number for each node. | 
| Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1527 | std::vector<unsigned> SethiUllmanNumbers; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1528 |  | 
|  | 1529 | public: | 
|  | 1530 | TDRegReductionPriorityQueue() {} | 
|  | 1531 |  | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 1532 | void initNodes(std::vector<SUnit> &sunits) { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1533 | SUnits = &sunits; | 
|  | 1534 | // Calculate node priorities. | 
| Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1535 | CalculateSethiUllmanNumbers(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1536 | } | 
|  | 1537 |  | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1538 | void addNode(const SUnit *SU) { | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1539 | unsigned SUSize = SethiUllmanNumbers.size(); | 
|  | 1540 | if (SUnits->size() > SUSize) | 
|  | 1541 | SethiUllmanNumbers.resize(SUSize*2, 0); | 
|  | 1542 | CalcNodeTDSethiUllmanNumber(SU, SethiUllmanNumbers); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1543 | } | 
|  | 1544 |  | 
|  | 1545 | void updateNode(const SUnit *SU) { | 
|  | 1546 | SethiUllmanNumbers[SU->NodeNum] = 0; | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1547 | CalcNodeTDSethiUllmanNumber(SU, SethiUllmanNumbers); | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1548 | } | 
|  | 1549 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1550 | void releaseState() { | 
|  | 1551 | SUnits = 0; | 
|  | 1552 | SethiUllmanNumbers.clear(); | 
|  | 1553 | } | 
|  | 1554 |  | 
| Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1555 | unsigned getNodePriority(const SUnit *SU) const { | 
| Evan Cheng | 961bbd3 | 2007-01-08 23:50:38 +0000 | [diff] [blame] | 1556 | assert(SU->NodeNum < SethiUllmanNumbers.size()); | 
|  | 1557 | return SethiUllmanNumbers[SU->NodeNum]; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1558 | } | 
|  | 1559 |  | 
|  | 1560 | private: | 
| Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1561 | void CalculateSethiUllmanNumbers(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1562 | }; | 
|  | 1563 | } | 
|  | 1564 |  | 
| Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1565 | /// closestSucc - Returns the scheduled cycle of the successor which is | 
|  | 1566 | /// closet to the current cycle. | 
| Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1567 | static unsigned closestSucc(const SUnit *SU) { | 
|  | 1568 | unsigned MaxCycle = 0; | 
|  | 1569 | 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] | 1570 | I != E; ++I) { | 
| Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1571 | unsigned Cycle = I->Dep->Cycle; | 
| Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1572 | // If there are bunch of CopyToRegs stacked up, they should be considered | 
|  | 1573 | // to be at the same position. | 
| Evan Cheng | 8e136a9 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 1574 | if (I->Dep->Node && I->Dep->Node->getOpcode() == ISD::CopyToReg) | 
| Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1575 | Cycle = closestSucc(I->Dep)+1; | 
| Evan Cheng | b9e3db6 | 2007-03-14 22:43:40 +0000 | [diff] [blame] | 1576 | if (Cycle > MaxCycle) | 
|  | 1577 | MaxCycle = Cycle; | 
|  | 1578 | } | 
| Evan Cheng | 2874855 | 2007-03-13 23:25:11 +0000 | [diff] [blame] | 1579 | return MaxCycle; | 
|  | 1580 | } | 
|  | 1581 |  | 
| Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1582 | /// calcMaxScratches - Returns an cost estimate of the worse case requirement | 
|  | 1583 | /// for scratch registers. Live-in operands and live-out results don't count | 
|  | 1584 | /// since they are "fixed". | 
|  | 1585 | static unsigned calcMaxScratches(const SUnit *SU) { | 
|  | 1586 | unsigned Scratches = 0; | 
|  | 1587 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); | 
|  | 1588 | I != E; ++I) { | 
|  | 1589 | if (I->isCtrl) continue;  // ignore chain preds | 
| Evan Cheng | 0e400d4 | 2008-01-09 23:01:55 +0000 | [diff] [blame] | 1590 | if (!I->Dep->Node || I->Dep->Node->getOpcode() != ISD::CopyFromReg) | 
| Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1591 | Scratches++; | 
|  | 1592 | } | 
|  | 1593 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
|  | 1594 | I != E; ++I) { | 
|  | 1595 | if (I->isCtrl) continue;  // ignore chain succs | 
| Evan Cheng | 0e400d4 | 2008-01-09 23:01:55 +0000 | [diff] [blame] | 1596 | if (!I->Dep->Node || I->Dep->Node->getOpcode() != ISD::CopyToReg) | 
| Evan Cheng | 61bc51e | 2007-12-20 02:22:36 +0000 | [diff] [blame] | 1597 | Scratches += 10; | 
|  | 1598 | } | 
|  | 1599 | return Scratches; | 
|  | 1600 | } | 
|  | 1601 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1602 | // Bottom up | 
|  | 1603 | 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] | 1604 | unsigned LPriority = SPQ->getNodePriority(left); | 
|  | 1605 | unsigned RPriority = SPQ->getNodePriority(right); | 
| Evan Cheng | 73bdf04 | 2008-03-01 00:39:47 +0000 | [diff] [blame] | 1606 | if (LPriority != RPriority) | 
|  | 1607 | return LPriority > RPriority; | 
|  | 1608 |  | 
|  | 1609 | // Try schedule def + use closer when Sethi-Ullman numbers are the same. | 
|  | 1610 | // e.g. | 
|  | 1611 | // t1 = op t2, c1 | 
|  | 1612 | // t3 = op t4, c2 | 
|  | 1613 | // | 
|  | 1614 | // and the following instructions are both ready. | 
|  | 1615 | // t2 = op c3 | 
|  | 1616 | // t4 = op c4 | 
|  | 1617 | // | 
|  | 1618 | // Then schedule t2 = op first. | 
|  | 1619 | // i.e. | 
|  | 1620 | // t4 = op c4 | 
|  | 1621 | // t2 = op c3 | 
|  | 1622 | // t1 = op t2, c1 | 
|  | 1623 | // t3 = op t4, c2 | 
|  | 1624 | // | 
|  | 1625 | // This creates more short live intervals. | 
|  | 1626 | unsigned LDist = closestSucc(left); | 
|  | 1627 | unsigned RDist = closestSucc(right); | 
|  | 1628 | if (LDist != RDist) | 
|  | 1629 | return LDist < RDist; | 
|  | 1630 |  | 
|  | 1631 | // Intuitively, it's good to push down instructions whose results are | 
|  | 1632 | // liveout so their long live ranges won't conflict with other values | 
|  | 1633 | // which are needed inside the BB. Further prioritize liveout instructions | 
|  | 1634 | // by the number of operands which are calculated within the BB. | 
|  | 1635 | unsigned LScratch = calcMaxScratches(left); | 
|  | 1636 | unsigned RScratch = calcMaxScratches(right); | 
|  | 1637 | if (LScratch != RScratch) | 
|  | 1638 | return LScratch > RScratch; | 
|  | 1639 |  | 
|  | 1640 | if (left->Height != right->Height) | 
|  | 1641 | return left->Height > right->Height; | 
|  | 1642 |  | 
|  | 1643 | if (left->Depth != right->Depth) | 
|  | 1644 | return left->Depth < right->Depth; | 
|  | 1645 |  | 
|  | 1646 | if (left->CycleBound != right->CycleBound) | 
|  | 1647 | return left->CycleBound > right->CycleBound; | 
|  | 1648 |  | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1649 | assert(left->NodeQueueId && right->NodeQueueId && | 
|  | 1650 | "NodeQueueId cannot be zero"); | 
|  | 1651 | return (left->NodeQueueId > right->NodeQueueId); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1652 | } | 
|  | 1653 |  | 
| Dan Gohman | 4b49be1 | 2008-06-21 01:08:22 +0000 | [diff] [blame] | 1654 | bool | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1655 | bu_ls_rr_fast_sort::operator()(const SUnit *left, const SUnit *right) const { | 
|  | 1656 | unsigned LPriority = SPQ->getNodePriority(left); | 
|  | 1657 | unsigned RPriority = SPQ->getNodePriority(right); | 
|  | 1658 | if (LPriority != RPriority) | 
|  | 1659 | return LPriority > RPriority; | 
|  | 1660 | assert(left->NodeQueueId && right->NodeQueueId && | 
|  | 1661 | "NodeQueueId cannot be zero"); | 
|  | 1662 | return (left->NodeQueueId > right->NodeQueueId); | 
|  | 1663 | } | 
|  | 1664 |  | 
|  | 1665 | bool | 
| Dan Gohman | 4b49be1 | 2008-06-21 01:08:22 +0000 | [diff] [blame] | 1666 | BURegReductionPriorityQueue::canClobber(const SUnit *SU, const SUnit *Op) { | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1667 | if (SU->isTwoAddress) { | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1668 | unsigned Opc = SU->Node->getMachineOpcode(); | 
| Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1669 | const TargetInstrDesc &TID = TII->get(Opc); | 
| Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1670 | unsigned NumRes = TID.getNumDefs(); | 
| Dan Gohman | 0340d1e | 2008-02-15 20:50:13 +0000 | [diff] [blame] | 1671 | unsigned NumOps = TID.getNumOperands() - NumRes; | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1672 | for (unsigned i = 0; i != NumOps; ++i) { | 
| Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1673 | if (TID.getOperandConstraint(i+NumRes, TOI::TIED_TO) != -1) { | 
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1674 | SDNode *DU = SU->Node->getOperand(i).getNode(); | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 1675 | if (DU->getNodeId() != -1 && | 
|  | 1676 | Op->OrigNode == &(*SUnits)[DU->getNodeId()]) | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1677 | return true; | 
|  | 1678 | } | 
|  | 1679 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1680 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1681 | return false; | 
|  | 1682 | } | 
|  | 1683 |  | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1684 |  | 
| Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1685 | /// hasCopyToRegUse - Return true if SU has a value successor that is a | 
|  | 1686 | /// CopyToReg node. | 
| Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 1687 | static bool hasCopyToRegUse(const SUnit *SU) { | 
| Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1688 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
|  | 1689 | I != E; ++I) { | 
|  | 1690 | if (I->isCtrl) continue; | 
| Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 1691 | const SUnit *SuccSU = I->Dep; | 
| Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1692 | if (SuccSU->Node && SuccSU->Node->getOpcode() == ISD::CopyToReg) | 
|  | 1693 | return true; | 
|  | 1694 | } | 
|  | 1695 | return false; | 
|  | 1696 | } | 
|  | 1697 |  | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1698 | /// canClobberPhysRegDefs - True if SU would clobber one of SuccSU's | 
| Dan Gohman | ea04520 | 2008-06-21 22:05:24 +0000 | [diff] [blame] | 1699 | /// physical register defs. | 
| Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 1700 | static bool canClobberPhysRegDefs(const SUnit *SuccSU, const SUnit *SU, | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1701 | const TargetInstrInfo *TII, | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1702 | const TargetRegisterInfo *TRI) { | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1703 | SDNode *N = SuccSU->Node; | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1704 | unsigned NumDefs = TII->get(N->getMachineOpcode()).getNumDefs(); | 
|  | 1705 | const unsigned *ImpDefs = TII->get(N->getMachineOpcode()).getImplicitDefs(); | 
| Dan Gohman | ea04520 | 2008-06-21 22:05:24 +0000 | [diff] [blame] | 1706 | assert(ImpDefs && "Caller should check hasPhysRegDefs"); | 
| Chris Lattner | b0d06b4 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 1707 | const unsigned *SUImpDefs = | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1708 | TII->get(SU->Node->getMachineOpcode()).getImplicitDefs(); | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1709 | if (!SUImpDefs) | 
|  | 1710 | return false; | 
|  | 1711 | for (unsigned i = NumDefs, e = N->getNumValues(); i != e; ++i) { | 
| Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1712 | MVT VT = N->getValueType(i); | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1713 | if (VT == MVT::Flag || VT == MVT::Other) | 
|  | 1714 | continue; | 
| Dan Gohman | 6ab52a8 | 2008-09-17 15:25:49 +0000 | [diff] [blame] | 1715 | if (!N->hasAnyUseOfValue(i)) | 
|  | 1716 | continue; | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1717 | unsigned Reg = ImpDefs[i - NumDefs]; | 
|  | 1718 | for (;*SUImpDefs; ++SUImpDefs) { | 
|  | 1719 | unsigned SUReg = *SUImpDefs; | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1720 | if (TRI->regsOverlap(Reg, SUReg)) | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1721 | return true; | 
|  | 1722 | } | 
|  | 1723 | } | 
|  | 1724 | return false; | 
|  | 1725 | } | 
|  | 1726 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1727 | /// AddPseudoTwoAddrDeps - If two nodes share an operand and one of them uses | 
|  | 1728 | /// it as a def&use operand. Add a pseudo control edge from it to the other | 
|  | 1729 | /// 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] | 1730 | /// first (lower in the schedule). If both nodes are two-address, favor the | 
|  | 1731 | /// one that has a CopyToReg use (more likely to be a loop induction update). | 
|  | 1732 | /// If both are two-address, but one is commutable while the other is not | 
|  | 1733 | /// commutable, favor the one that's not commutable. | 
| Dan Gohman | 4b49be1 | 2008-06-21 01:08:22 +0000 | [diff] [blame] | 1734 | void BURegReductionPriorityQueue::AddPseudoTwoAddrDeps() { | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1735 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) { | 
| Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 1736 | SUnit *SU = &(*SUnits)[i]; | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1737 | if (!SU->isTwoAddress) | 
|  | 1738 | continue; | 
|  | 1739 |  | 
|  | 1740 | SDNode *Node = SU->Node; | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1741 | if (!Node || !Node->isMachineOpcode() || SU->FlaggedNodes.size() > 0) | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1742 | continue; | 
|  | 1743 |  | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1744 | unsigned Opc = Node->getMachineOpcode(); | 
| Chris Lattner | 03ad885 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1745 | const TargetInstrDesc &TID = TII->get(Opc); | 
| Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1746 | unsigned NumRes = TID.getNumDefs(); | 
| Dan Gohman | 0340d1e | 2008-02-15 20:50:13 +0000 | [diff] [blame] | 1747 | unsigned NumOps = TID.getNumOperands() - NumRes; | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1748 | for (unsigned j = 0; j != NumOps; ++j) { | 
| Chris Lattner | fd2e338 | 2008-01-07 06:47:00 +0000 | [diff] [blame] | 1749 | if (TID.getOperandConstraint(j+NumRes, TOI::TIED_TO) != -1) { | 
| Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1750 | SDNode *DU = SU->Node->getOperand(j).getNode(); | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 1751 | if (DU->getNodeId() == -1) | 
| Evan Cheng | 1bf16631 | 2007-11-09 01:27:11 +0000 | [diff] [blame] | 1752 | continue; | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 1753 | const SUnit *DUSU = &(*SUnits)[DU->getNodeId()]; | 
| Evan Cheng | f24d15f | 2006-11-06 21:33:46 +0000 | [diff] [blame] | 1754 | if (!DUSU) continue; | 
| Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 1755 | for (SUnit::const_succ_iterator I = DUSU->Succs.begin(), | 
|  | 1756 | E = DUSU->Succs.end(); I != E; ++I) { | 
| Evan Cheng | 0effc3a | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 1757 | if (I->isCtrl) continue; | 
|  | 1758 | SUnit *SuccSU = I->Dep; | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1759 | if (SuccSU == SU) | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1760 | continue; | 
| Evan Cheng | 2dbffa4 | 2007-11-06 08:44:59 +0000 | [diff] [blame] | 1761 | // Be conservative. Ignore if nodes aren't at roughly the same | 
|  | 1762 | // depth and height. | 
|  | 1763 | if (SuccSU->Height < SU->Height && (SU->Height - SuccSU->Height) > 1) | 
|  | 1764 | continue; | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1765 | if (!SuccSU->Node || !SuccSU->Node->isMachineOpcode()) | 
| Evan Cheng | aa2d6ef | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1766 | continue; | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1767 | // Don't constrain nodes with physical register defs if the | 
| Dan Gohman | cf8827a | 2008-01-29 12:43:50 +0000 | [diff] [blame] | 1768 | // predecessor can clobber them. | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1769 | if (SuccSU->hasPhysRegDefs) { | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1770 | if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI)) | 
| Evan Cheng | f989141 | 2007-12-20 09:25:31 +0000 | [diff] [blame] | 1771 | continue; | 
|  | 1772 | } | 
| Evan Cheng | aa2d6ef | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1773 | // Don't constraint extract_subreg / insert_subreg these may be | 
|  | 1774 | // coalesced away. We don't them close to their uses. | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1775 | unsigned SuccOpc = SuccSU->Node->getMachineOpcode(); | 
| Evan Cheng | aa2d6ef | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1776 | if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG || | 
|  | 1777 | SuccOpc == TargetInstrInfo::INSERT_SUBREG) | 
|  | 1778 | continue; | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1779 | if ((!canClobber(SuccSU, DUSU) || | 
| Evan Cheng | a5e595d | 2007-09-28 22:32:30 +0000 | [diff] [blame] | 1780 | (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) || | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1781 | (!SU->isCommutable && SuccSU->isCommutable)) && | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 1782 | !scheduleDAG->IsReachable(SuccSU, SU)) { | 
| Evan Cheng | 5924bf7 | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 1783 | DOUT << "Adding an edge from SU # " << SU->NodeNum | 
|  | 1784 | << " to SU #" << SuccSU->NodeNum << "\n"; | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 1785 | scheduleDAG->AddPred(SU, SuccSU, true, true); | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1786 | } | 
|  | 1787 | } | 
|  | 1788 | } | 
|  | 1789 | } | 
|  | 1790 | } | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1791 | } | 
|  | 1792 |  | 
| Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1793 | /// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all | 
|  | 1794 | /// scheduling units. | 
| Dan Gohman | 4b49be1 | 2008-06-21 01:08:22 +0000 | [diff] [blame] | 1795 | void BURegReductionPriorityQueue::CalculateSethiUllmanNumbers() { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1796 | SethiUllmanNumbers.assign(SUnits->size(), 0); | 
|  | 1797 |  | 
|  | 1798 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1799 | CalcNodeBUSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers); | 
|  | 1800 | } | 
|  | 1801 | void BURegReductionFastPriorityQueue::CalculateSethiUllmanNumbers() { | 
|  | 1802 | SethiUllmanNumbers.assign(SUnits->size(), 0); | 
|  | 1803 |  | 
|  | 1804 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) | 
|  | 1805 | CalcNodeBUSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1806 | } | 
|  | 1807 |  | 
| Roman Levenstein | 30d0951 | 2008-03-27 09:44:37 +0000 | [diff] [blame] | 1808 | /// LimitedSumOfUnscheduledPredsOfSuccs - Compute the sum of the unscheduled | 
| Roman Levenstein | bc67450 | 2008-03-27 09:14:57 +0000 | [diff] [blame] | 1809 | /// predecessors of the successors of the SUnit SU. Stop when the provided | 
|  | 1810 | /// limit is exceeded. | 
| Roman Levenstein | bc67450 | 2008-03-27 09:14:57 +0000 | [diff] [blame] | 1811 | static unsigned LimitedSumOfUnscheduledPredsOfSuccs(const SUnit *SU, | 
|  | 1812 | unsigned Limit) { | 
|  | 1813 | unsigned Sum = 0; | 
|  | 1814 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
|  | 1815 | I != E; ++I) { | 
| Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 1816 | const SUnit *SuccSU = I->Dep; | 
| Roman Levenstein | bc67450 | 2008-03-27 09:14:57 +0000 | [diff] [blame] | 1817 | for (SUnit::const_pred_iterator II = SuccSU->Preds.begin(), | 
|  | 1818 | EE = SuccSU->Preds.end(); II != EE; ++II) { | 
|  | 1819 | SUnit *PredSU = II->Dep; | 
| Evan Cheng | 16d7207 | 2008-03-29 18:34:22 +0000 | [diff] [blame] | 1820 | if (!PredSU->isScheduled) | 
|  | 1821 | if (++Sum > Limit) | 
|  | 1822 | return Sum; | 
| Roman Levenstein | bc67450 | 2008-03-27 09:14:57 +0000 | [diff] [blame] | 1823 | } | 
|  | 1824 | } | 
|  | 1825 | return Sum; | 
|  | 1826 | } | 
|  | 1827 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1828 |  | 
|  | 1829 | // Top down | 
|  | 1830 | 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] | 1831 | unsigned LPriority = SPQ->getNodePriority(left); | 
|  | 1832 | unsigned RPriority = SPQ->getNodePriority(right); | 
| Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 1833 | bool LIsTarget = left->Node && left->Node->isMachineOpcode(); | 
|  | 1834 | bool RIsTarget = right->Node && right->Node->isMachineOpcode(); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1835 | bool LIsFloater = LIsTarget && left->NumPreds == 0; | 
|  | 1836 | bool RIsFloater = RIsTarget && right->NumPreds == 0; | 
| Roman Levenstein | bc67450 | 2008-03-27 09:14:57 +0000 | [diff] [blame] | 1837 | unsigned LBonus = (LimitedSumOfUnscheduledPredsOfSuccs(left,1) == 1) ? 2 : 0; | 
|  | 1838 | unsigned RBonus = (LimitedSumOfUnscheduledPredsOfSuccs(right,1) == 1) ? 2 : 0; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1839 |  | 
|  | 1840 | if (left->NumSuccs == 0 && right->NumSuccs != 0) | 
|  | 1841 | return false; | 
|  | 1842 | else if (left->NumSuccs != 0 && right->NumSuccs == 0) | 
|  | 1843 | return true; | 
|  | 1844 |  | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1845 | if (LIsFloater) | 
|  | 1846 | LBonus -= 2; | 
|  | 1847 | if (RIsFloater) | 
|  | 1848 | RBonus -= 2; | 
|  | 1849 | if (left->NumSuccs == 1) | 
|  | 1850 | LBonus += 2; | 
|  | 1851 | if (right->NumSuccs == 1) | 
|  | 1852 | RBonus += 2; | 
|  | 1853 |  | 
| Evan Cheng | 73bdf04 | 2008-03-01 00:39:47 +0000 | [diff] [blame] | 1854 | if (LPriority+LBonus != RPriority+RBonus) | 
|  | 1855 | return LPriority+LBonus < RPriority+RBonus; | 
| Anton Korobeynikov | 035eaac | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 1856 |  | 
| Evan Cheng | 73bdf04 | 2008-03-01 00:39:47 +0000 | [diff] [blame] | 1857 | if (left->Depth != right->Depth) | 
|  | 1858 | return left->Depth < right->Depth; | 
|  | 1859 |  | 
|  | 1860 | if (left->NumSuccsLeft != right->NumSuccsLeft) | 
|  | 1861 | return left->NumSuccsLeft > right->NumSuccsLeft; | 
|  | 1862 |  | 
|  | 1863 | if (left->CycleBound != right->CycleBound) | 
|  | 1864 | return left->CycleBound > right->CycleBound; | 
|  | 1865 |  | 
| Roman Levenstein | 6b37114 | 2008-04-29 09:07:59 +0000 | [diff] [blame] | 1866 | assert(left->NodeQueueId && right->NodeQueueId && | 
|  | 1867 | "NodeQueueId cannot be zero"); | 
|  | 1868 | return (left->NodeQueueId > right->NodeQueueId); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1869 | } | 
|  | 1870 |  | 
| Evan Cheng | 6730f03 | 2007-01-08 23:55:53 +0000 | [diff] [blame] | 1871 | /// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all | 
|  | 1872 | /// scheduling units. | 
| Dan Gohman | 4b49be1 | 2008-06-21 01:08:22 +0000 | [diff] [blame] | 1873 | void TDRegReductionPriorityQueue::CalculateSethiUllmanNumbers() { | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1874 | SethiUllmanNumbers.assign(SUnits->size(), 0); | 
|  | 1875 |  | 
|  | 1876 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1877 | CalcNodeTDSethiUllmanNumber(&(*SUnits)[i], SethiUllmanNumbers); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1878 | } | 
|  | 1879 |  | 
|  | 1880 | //===----------------------------------------------------------------------===// | 
|  | 1881 | //                         Public Constructor Functions | 
|  | 1882 | //===----------------------------------------------------------------------===// | 
|  | 1883 |  | 
| Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1884 | llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAGISel *IS, | 
|  | 1885 | SelectionDAG *DAG, | 
| Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 1886 | MachineBasicBlock *BB, | 
|  | 1887 | bool Fast) { | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1888 | if (Fast) | 
|  | 1889 | return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), true, true, | 
|  | 1890 | new BURegReductionFastPriorityQueue()); | 
|  | 1891 |  | 
| Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 1892 | const TargetInstrInfo *TII = DAG->getTarget().getInstrInfo(); | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1893 | const TargetRegisterInfo *TRI = DAG->getTarget().getRegisterInfo(); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 1894 |  | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1895 | BURegReductionPriorityQueue *PQ = new BURegReductionPriorityQueue(TII, TRI); | 
| Roman Levenstein | 7e71b4b | 2008-03-26 09:18:09 +0000 | [diff] [blame] | 1896 |  | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1897 | ScheduleDAGRRList *SD = | 
|  | 1898 | new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(),true,false, PQ); | 
|  | 1899 | PQ->setScheduleDAG(SD); | 
|  | 1900 | return SD; | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1901 | } | 
|  | 1902 |  | 
| Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1903 | llvm::ScheduleDAG* llvm::createTDRRListDAGScheduler(SelectionDAGISel *IS, | 
|  | 1904 | SelectionDAG *DAG, | 
| Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 1905 | MachineBasicBlock *BB, | 
|  | 1906 | bool Fast) { | 
|  | 1907 | return new ScheduleDAGRRList(*DAG, BB, DAG->getTarget(), false, Fast, | 
| Evan Cheng | 7e4abde | 2008-07-02 09:23:51 +0000 | [diff] [blame] | 1908 | new TDRegReductionPriorityQueue()); | 
| Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 1909 | } |