Dale Johannesen | 72f1596 | 2007-07-13 17:31:29 +0000 | [diff] [blame] | 1 | //===----- SchedulePostRAList.cpp - list scheduler ------------------------===// |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements a top-down list scheduler, using standard algorithms. |
| 11 | // The basic approach uses a priority queue of available nodes to schedule. |
| 12 | // One at a time, nodes are taken from the priority queue (thus in priority |
| 13 | // order), checked for legality to schedule, and emitted if legal. |
| 14 | // |
| 15 | // Nodes may not be legal to schedule either due to structural hazards (e.g. |
| 16 | // pipeline or resource constraints) or because an input to the instruction has |
| 17 | // not completed execution. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #define DEBUG_TYPE "post-RA-sched" |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 22 | #include "ExactHazardRecognizer.h" |
| 23 | #include "SimpleHazardRecognizer.h" |
Dan Gohman | 6dc75fe | 2009-02-06 17:12:10 +0000 | [diff] [blame] | 24 | #include "ScheduleDAGInstrs.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/Passes.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LatencyPriorityQueue.h" |
| 27 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineDominators.h" |
David Goodwin | c7951f8 | 2009-10-01 19:45:32 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetInstrInfo.h" |
| 38 | #include "llvm/Target/TargetRegisterInfo.h" |
David Goodwin | 0dad89f | 2009-09-30 00:10:16 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetSubtarget.h" |
Chris Lattner | 459525d | 2008-01-14 19:00:06 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Compiler.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 45 | #include <map> |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 46 | #include <set> |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 49 | STATISTIC(NumNoops, "Number of noops inserted"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 50 | STATISTIC(NumStalls, "Number of pipeline stalls"); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 51 | STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 52 | |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 53 | // Post-RA scheduling is enabled with |
| 54 | // TargetSubtarget.enablePostRAScheduler(). This flag can be used to |
| 55 | // override the target. |
| 56 | static cl::opt<bool> |
| 57 | EnablePostRAScheduler("post-RA-scheduler", |
| 58 | cl::desc("Enable scheduling after register allocation"), |
David Goodwin | 9843a93 | 2009-10-01 22:19:57 +0000 | [diff] [blame] | 59 | cl::init(false), cl::Hidden); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 60 | static cl::opt<std::string> |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 61 | EnableAntiDepBreaking("break-anti-dependencies", |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 62 | cl::desc("Break post-RA scheduling anti-dependencies: " |
| 63 | "\"critical\", \"all\", or \"none\""), |
| 64 | cl::init("critical"), cl::Hidden); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 65 | static cl::opt<bool> |
| 66 | EnablePostRAHazardAvoidance("avoid-hazards", |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 67 | cl::desc("Enable exact hazard avoidance"), |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 68 | cl::init(true), cl::Hidden); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 69 | |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 70 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 71 | static cl::opt<int> |
| 72 | DebugDiv("postra-sched-debugdiv", |
| 73 | cl::desc("Debug control MBBs that are scheduled"), |
| 74 | cl::init(0), cl::Hidden); |
| 75 | static cl::opt<int> |
| 76 | DebugMod("postra-sched-debugmod", |
| 77 | cl::desc("Debug control MBBs that are scheduled"), |
| 78 | cl::init(0), cl::Hidden); |
| 79 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 80 | namespace { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 81 | class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass { |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 82 | AliasAnalysis *AA; |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 83 | CodeGenOpt::Level OptLevel; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 84 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 85 | public: |
| 86 | static char ID; |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 87 | PostRAScheduler(CodeGenOpt::Level ol) : |
| 88 | MachineFunctionPass(&ID), OptLevel(ol) {} |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 89 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 90 | void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 91 | AU.setPreservesCFG(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 92 | AU.addRequired<AliasAnalysis>(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 93 | AU.addRequired<MachineDominatorTree>(); |
| 94 | AU.addPreserved<MachineDominatorTree>(); |
| 95 | AU.addRequired<MachineLoopInfo>(); |
| 96 | AU.addPreserved<MachineLoopInfo>(); |
| 97 | MachineFunctionPass::getAnalysisUsage(AU); |
| 98 | } |
| 99 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 100 | const char *getPassName() const { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 101 | return "Post RA top-down list latency scheduler"; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | bool runOnMachineFunction(MachineFunction &Fn); |
| 105 | }; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 106 | char PostRAScheduler::ID = 0; |
| 107 | |
| 108 | class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs { |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 109 | /// RegisterReference - Information about a register reference |
| 110 | /// within a liverange |
| 111 | typedef struct { |
| 112 | /// Operand - The registers operand |
| 113 | MachineOperand *Operand; |
| 114 | /// RC - The register class |
| 115 | const TargetRegisterClass *RC; |
| 116 | } RegisterReference; |
| 117 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 118 | /// AvailableQueue - The priority queue to use for the available SUnits. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 119 | LatencyPriorityQueue AvailableQueue; |
| 120 | |
| 121 | /// PendingQueue - This contains all of the instructions whose operands have |
| 122 | /// been issued, but their results are not ready yet (due to the latency of |
| 123 | /// the operation). Once the operands becomes available, the instruction is |
| 124 | /// added to the AvailableQueue. |
| 125 | std::vector<SUnit*> PendingQueue; |
| 126 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 127 | /// Topo - A topological ordering for SUnits. |
| 128 | ScheduleDAGTopologicalSort Topo; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 129 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 130 | /// HazardRec - The hazard recognizer to use. |
| 131 | ScheduleHazardRecognizer *HazardRec; |
| 132 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 133 | /// AA - AliasAnalysis for making memory reference queries. |
| 134 | AliasAnalysis *AA; |
| 135 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 136 | /// AllocatableSet - The set of allocatable registers. |
| 137 | /// We'll be ignoring anti-dependencies on non-allocatable registers, |
| 138 | /// because they may not be safe to break. |
| 139 | const BitVector AllocatableSet; |
| 140 | |
| 141 | /// GroupNodes - Implements a disjoint-union data structure to |
| 142 | /// form register groups. A node is represented by an index into |
| 143 | /// the vector. A node can "point to" itself to indicate that it |
| 144 | /// is the parent of a group, or point to another node to indicate |
| 145 | /// that it is a member of the same group as that node. |
| 146 | std::vector<unsigned> GroupNodes; |
| 147 | |
| 148 | /// GroupNodeIndices - For each register, the index of the GroupNode |
| 149 | /// currently representing the group that the register belongs to. |
| 150 | /// Register 0 is always represented by the 0 group, a group |
| 151 | /// composed of registers that are not eligible for anti-aliasing. |
| 152 | unsigned GroupNodeIndices[TargetRegisterInfo::FirstVirtualRegister]; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 153 | |
| 154 | /// RegRegs - Map registers to all their references within a live range. |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 155 | std::multimap<unsigned, RegisterReference> RegRefs; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 156 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 157 | /// KillIndices - The index of the most recent kill (proceding |
| 158 | /// bottom-up), or ~0u if no kill of the register has been |
| 159 | /// seen. The register is live if this index != ~0u and DefIndices |
| 160 | /// == ~0u. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 161 | unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 162 | |
Evan Cheng | 714e8bc | 2009-10-01 08:26:23 +0000 | [diff] [blame] | 163 | /// DefIndices - The index of the most recent complete def (proceding bottom |
| 164 | /// up), or ~0u if the register is live. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 165 | unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 166 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 167 | public: |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 168 | SchedulePostRATDList(MachineFunction &MF, |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 169 | const MachineLoopInfo &MLI, |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 170 | const MachineDominatorTree &MDT, |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 171 | ScheduleHazardRecognizer *HR, |
| 172 | AliasAnalysis *aa) |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 173 | : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits), |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 174 | HazardRec(HR), AA(aa), |
| 175 | AllocatableSet(TRI->getAllocatableSet(MF)), |
| 176 | GroupNodes(TargetRegisterInfo::FirstVirtualRegister, 0) {} |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 177 | |
| 178 | ~SchedulePostRATDList() { |
| 179 | delete HazardRec; |
| 180 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 181 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 182 | /// StartBlock - Initialize register live-range state for scheduling in |
| 183 | /// this block. |
| 184 | /// |
| 185 | void StartBlock(MachineBasicBlock *BB); |
| 186 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 187 | /// FinishBlock - Clean up register live-range state. |
| 188 | /// |
| 189 | void FinishBlock(); |
| 190 | |
| 191 | /// Observe - Update liveness information to account for the current |
| 192 | /// instruction, which will not be scheduled. |
| 193 | /// |
| 194 | void Observe(MachineInstr *MI, unsigned Count); |
| 195 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 196 | /// Schedule - Schedule the instruction range using list scheduling. |
| 197 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 198 | void Schedule(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 199 | |
| 200 | /// FixupKills - Fix register kill flags that have been made |
| 201 | /// invalid due to scheduling |
| 202 | /// |
| 203 | void FixupKills(MachineBasicBlock *MBB); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 204 | |
| 205 | private: |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 206 | /// IsLive - Return true if Reg is live |
| 207 | bool IsLive(unsigned Reg); |
| 208 | |
| 209 | void PrescanInstruction(MachineInstr *MI, unsigned Count); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 210 | void ScanInstruction(MachineInstr *MI, unsigned Count); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 211 | bool BreakAntiDependencies(bool CriticalPathOnly); |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 212 | unsigned FindSuitableFreeRegister(unsigned AntiDepReg); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 213 | |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 214 | void ReleaseSucc(SUnit *SU, SDep *SuccEdge); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 215 | void ReleaseSuccessors(SUnit *SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 216 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
| 217 | void ListScheduleTopDown(); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 218 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 219 | void StartBlockForKills(MachineBasicBlock *BB); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 220 | |
| 221 | // ToggleKillFlag - Toggle a register operand kill flag. Other |
| 222 | // adjustments may be made to the instruction if necessary. Return |
| 223 | // true if the operand has been deleted, false if not. |
| 224 | bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 225 | |
| 226 | // GetGroup - Get the group for a register. The returned value is |
| 227 | // the index of the GroupNode representing the group. |
| 228 | unsigned GetGroup(unsigned Reg); |
| 229 | |
| 230 | // GetGroupRegs - Return a vector of the registers belonging to a |
| 231 | // group. |
| 232 | void GetGroupRegs(unsigned Group, std::vector<unsigned> &Regs); |
| 233 | |
| 234 | // UnionGroups - Union Reg1's and Reg2's groups to form a new |
| 235 | // group. Return the index of the GroupNode representing the |
| 236 | // group. |
| 237 | unsigned UnionGroups(unsigned Reg1, unsigned Reg2); |
| 238 | |
| 239 | // LeaveGroup - Remove a register from its current group and place |
| 240 | // it alone in its own group. Return the index of the GroupNode |
| 241 | // representing the registers new group. |
| 242 | unsigned LeaveGroup(unsigned Reg); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 243 | }; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 246 | /// isSchedulingBoundary - Test if the given instruction should be |
| 247 | /// considered a scheduling boundary. This primarily includes labels |
| 248 | /// and terminators. |
| 249 | /// |
| 250 | static bool isSchedulingBoundary(const MachineInstr *MI, |
| 251 | const MachineFunction &MF) { |
| 252 | // Terminators and labels can't be scheduled around. |
| 253 | if (MI->getDesc().isTerminator() || MI->isLabel()) |
| 254 | return true; |
| 255 | |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 256 | // Don't attempt to schedule around any instruction that modifies |
| 257 | // a stack-oriented pointer, as it's unlikely to be profitable. This |
| 258 | // saves compile time, because it doesn't require every single |
| 259 | // stack slot reference to depend on the instruction that does the |
| 260 | // modification. |
| 261 | const TargetLowering &TLI = *MF.getTarget().getTargetLowering(); |
| 262 | if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore())) |
| 263 | return true; |
| 264 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 268 | bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { |
Dan Gohman | 5bf7c2a | 2009-10-10 00:15:38 +0000 | [diff] [blame] | 269 | AA = &getAnalysis<AliasAnalysis>(); |
| 270 | |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 271 | // Check for explicit enable/disable of post-ra scheduling. |
| 272 | if (EnablePostRAScheduler.getPosition() > 0) { |
| 273 | if (!EnablePostRAScheduler) |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 274 | return false; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 275 | } else { |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 276 | // Check that post-RA scheduling is enabled for this target. |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 277 | const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>(); |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 278 | if (!ST.enablePostRAScheduler(OptLevel)) |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 279 | return false; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 280 | } |
David Goodwin | 0dad89f | 2009-09-30 00:10:16 +0000 | [diff] [blame] | 281 | |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 282 | DEBUG(errs() << "PostRAScheduler\n"); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 283 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 284 | const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 285 | const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 286 | const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData(); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 287 | ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ? |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 288 | (ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) : |
| 289 | (ScheduleHazardRecognizer *)new SimpleHazardRecognizer(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 290 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 291 | SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR, AA); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 292 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 293 | // Loop over all of the basic blocks |
| 294 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 295 | MBB != MBBe; ++MBB) { |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 296 | #ifndef NDEBUG |
| 297 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 298 | if (DebugDiv > 0) { |
| 299 | static int bbcnt = 0; |
| 300 | if (bbcnt++ % DebugDiv != DebugMod) |
| 301 | continue; |
| 302 | errs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() << |
| 303 | ":MBB ID#" << MBB->getNumber() << " ***\n"; |
| 304 | } |
| 305 | #endif |
| 306 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 307 | // Initialize register live-range state for scheduling in this block. |
| 308 | Scheduler.StartBlock(MBB); |
| 309 | |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 310 | // Schedule each sequence of instructions not interrupted by a label |
| 311 | // or anything else that effectively needs to shut down scheduling. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 312 | MachineBasicBlock::iterator Current = MBB->end(); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 313 | unsigned Count = MBB->size(), CurrentCount = Count; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 314 | for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) { |
| 315 | MachineInstr *MI = prior(I); |
| 316 | if (isSchedulingBoundary(MI, Fn)) { |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 317 | Scheduler.Run(MBB, I, Current, CurrentCount); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 318 | Scheduler.EmitSchedule(0); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 319 | Current = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 320 | CurrentCount = Count - 1; |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 321 | Scheduler.Observe(MI, CurrentCount); |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 322 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 323 | I = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 324 | --Count; |
Dan Gohman | 43f07fb | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 325 | } |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 326 | assert(Count == 0 && "Instruction count mismatch!"); |
Duncan Sands | 9e8bd0b | 2009-03-11 09:04:34 +0000 | [diff] [blame] | 327 | assert((MBB->begin() == Current || CurrentCount != 0) && |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 328 | "Instruction count mismatch!"); |
| 329 | Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 330 | Scheduler.EmitSchedule(0); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 331 | |
| 332 | // Clean up register live-range state. |
| 333 | Scheduler.FinishBlock(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 334 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 335 | // Update register kills |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 336 | Scheduler.FixupKills(MBB); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 337 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 338 | |
| 339 | return true; |
| 340 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 341 | |
| 342 | unsigned SchedulePostRATDList::GetGroup(unsigned Reg) |
| 343 | { |
| 344 | unsigned Node = GroupNodeIndices[Reg]; |
| 345 | while (GroupNodes[Node] != Node) |
| 346 | Node = GroupNodes[Node]; |
| 347 | |
| 348 | return Node; |
| 349 | } |
| 350 | |
| 351 | void SchedulePostRATDList::GetGroupRegs(unsigned Group, std::vector<unsigned> &Regs) |
| 352 | { |
| 353 | for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg) { |
| 354 | if (GetGroup(Reg) == Group) |
| 355 | Regs.push_back(Reg); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | unsigned SchedulePostRATDList::UnionGroups(unsigned Reg1, unsigned Reg2) |
| 360 | { |
| 361 | assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!"); |
| 362 | assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!"); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 363 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 364 | // find group for each register |
| 365 | unsigned Group1 = GetGroup(Reg1); |
| 366 | unsigned Group2 = GetGroup(Reg2); |
| 367 | |
| 368 | // if either group is 0, then that must become the parent |
| 369 | unsigned Parent = (Group1 == 0) ? Group1 : Group2; |
| 370 | unsigned Other = (Parent == Group1) ? Group2 : Group1; |
| 371 | GroupNodes.at(Other) = Parent; |
| 372 | return Parent; |
| 373 | } |
| 374 | |
| 375 | unsigned SchedulePostRATDList::LeaveGroup(unsigned Reg) |
| 376 | { |
| 377 | // Create a new GroupNode for Reg. Reg's existing GroupNode must |
| 378 | // stay as is because there could be other GroupNodes referring to |
| 379 | // it. |
| 380 | unsigned idx = GroupNodes.size(); |
| 381 | GroupNodes.push_back(idx); |
| 382 | GroupNodeIndices[Reg] = idx; |
| 383 | return idx; |
| 384 | } |
| 385 | |
| 386 | bool SchedulePostRATDList::IsLive(unsigned Reg) |
| 387 | { |
| 388 | // KillIndex must be defined and DefIndex not defined for a register |
| 389 | // to be live. |
| 390 | return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u)); |
| 391 | } |
| 392 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 393 | /// StartBlock - Initialize register live-range state for scheduling in |
| 394 | /// this block. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 395 | /// |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 396 | void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) { |
| 397 | // Call the superclass. |
| 398 | ScheduleDAGInstrs::StartBlock(BB); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 399 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 400 | // Reset the hazard recognizer. |
| 401 | HazardRec->Reset(); |
| 402 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 403 | // Initialize all registers to be in their own group. Initially we |
| 404 | // assign the register to the same-indexed GroupNode. |
| 405 | for (unsigned i = 0; i < TargetRegisterInfo::FirstVirtualRegister; ++i) |
| 406 | GroupNodeIndices[i] = i; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 407 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 408 | // Initialize the indices to indicate that no registers are live. |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 409 | std::fill(KillIndices, array_endof(KillIndices), ~0u); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 410 | std::fill(DefIndices, array_endof(DefIndices), BB->size()); |
| 411 | |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 412 | bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn()); |
| 413 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 414 | // Determine the live-out physregs for this block. |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 415 | if (IsReturnBlock) { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 416 | // In a return block, examine the function live-out regs. |
| 417 | for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), |
| 418 | E = MRI.liveout_end(); I != E; ++I) { |
| 419 | unsigned Reg = *I; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 420 | UnionGroups(Reg, 0); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 421 | KillIndices[Reg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 422 | DefIndices[Reg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 423 | // Repeat, for all aliases. |
| 424 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 425 | unsigned AliasReg = *Alias; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 426 | UnionGroups(AliasReg, 0); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 427 | KillIndices[AliasReg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 428 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
David Goodwin | c7951f8 | 2009-10-01 19:45:32 +0000 | [diff] [blame] | 431 | } else { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 432 | // In a non-return block, examine the live-in regs of all successors. |
| 433 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 434 | SE = BB->succ_end(); SI != SE; ++SI) |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 435 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 436 | E = (*SI)->livein_end(); I != E; ++I) { |
| 437 | unsigned Reg = *I; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 438 | UnionGroups(Reg, 0); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 439 | KillIndices[Reg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 440 | DefIndices[Reg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 441 | // Repeat, for all aliases. |
| 442 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 443 | unsigned AliasReg = *Alias; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 444 | UnionGroups(AliasReg, 0); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 445 | KillIndices[AliasReg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 446 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 449 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 450 | |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 451 | // Mark live-out callee-saved registers. In a return block this is |
| 452 | // all callee-saved registers. In non-return this is any |
| 453 | // callee-saved register that is not saved in the prolog. |
| 454 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 455 | BitVector Pristine = MFI->getPristineRegs(BB); |
| 456 | for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) { |
| 457 | unsigned Reg = *I; |
| 458 | if (!IsReturnBlock && !Pristine.test(Reg)) continue; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 459 | UnionGroups(Reg, 0); |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 460 | KillIndices[Reg] = BB->size(); |
| 461 | DefIndices[Reg] = ~0u; |
| 462 | // Repeat, for all aliases. |
| 463 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 464 | unsigned AliasReg = *Alias; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 465 | UnionGroups(AliasReg, 0); |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 466 | KillIndices[AliasReg] = BB->size(); |
| 467 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /// Schedule - Schedule the instruction range using list scheduling. |
| 473 | /// |
| 474 | void SchedulePostRATDList::Schedule() { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 475 | DEBUG(errs() << "********** List Scheduling **********\n"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 476 | |
| 477 | // Build the scheduling graph. |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 478 | BuildSchedGraph(AA); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 479 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 480 | if (EnableAntiDepBreaking != "none") { |
| 481 | if (BreakAntiDependencies((EnableAntiDepBreaking == "all") ? false : true)) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 482 | // We made changes. Update the dependency graph. |
| 483 | // Theoretically we could update the graph in place: |
| 484 | // When a live range is changed to use a different register, remove |
| 485 | // the def's anti-dependence *and* output-dependence edges due to |
| 486 | // that register, and add new anti-dependence and output-dependence |
| 487 | // edges based on the next live range of the register. |
| 488 | SUnits.clear(); |
| 489 | EntrySU = SUnit(); |
| 490 | ExitSU = SUnit(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 491 | BuildSchedGraph(AA); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 495 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 496 | SUnits[su].dumpAll(this)); |
| 497 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 498 | AvailableQueue.initNodes(SUnits); |
| 499 | |
| 500 | ListScheduleTopDown(); |
| 501 | |
| 502 | AvailableQueue.releaseState(); |
| 503 | } |
| 504 | |
| 505 | /// Observe - Update liveness information to account for the current |
| 506 | /// instruction, which will not be scheduled. |
| 507 | /// |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 508 | void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) { |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 509 | assert(Count < InsertPosIndex && "Instruction index out of expected range!"); |
| 510 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 511 | DEBUG(errs() << "Observe: "); |
| 512 | DEBUG(MI->dump()); |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 513 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 514 | for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg) { |
| 515 | // If Reg is current live, then mark that it can't be renamed as |
| 516 | // we don't know the extent of its live-range anymore (now that it |
| 517 | // has been scheduled). If it is not live but was defined in the |
| 518 | // previous schedule region, then set its def index to the most |
| 519 | // conservative location (i.e. the beginning of the previous |
| 520 | // schedule region). |
| 521 | if (IsLive(Reg)) { |
| 522 | DEBUG(if (GetGroup(Reg) != 0) |
| 523 | errs() << " " << TRI->getName(Reg) << "=g" << |
| 524 | GetGroup(Reg) << "->g0(region live-out)"); |
| 525 | UnionGroups(Reg, 0); |
| 526 | } else if ((DefIndices[Reg] < InsertPosIndex) && (DefIndices[Reg] >= Count)) { |
| 527 | DefIndices[Reg] = Count; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | PrescanInstruction(MI, Count); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 532 | ScanInstruction(MI, Count); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /// FinishBlock - Clean up register live-range state. |
| 536 | /// |
| 537 | void SchedulePostRATDList::FinishBlock() { |
| 538 | RegRefs.clear(); |
| 539 | |
| 540 | // Call the superclass. |
| 541 | ScheduleDAGInstrs::FinishBlock(); |
| 542 | } |
| 543 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 544 | /// CriticalPathStep - Return the next SUnit after SU on the bottom-up |
| 545 | /// critical path. |
| 546 | static SDep *CriticalPathStep(SUnit *SU) { |
| 547 | SDep *Next = 0; |
| 548 | unsigned NextDepth = 0; |
| 549 | // Find the predecessor edge with the greatest depth. |
| 550 | for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
| 551 | P != PE; ++P) { |
| 552 | SUnit *PredSU = P->getSUnit(); |
| 553 | unsigned PredLatency = P->getLatency(); |
| 554 | unsigned PredTotalLatency = PredSU->getDepth() + PredLatency; |
| 555 | // In the case of a latency tie, prefer an anti-dependency edge over |
| 556 | // other types of edges. |
| 557 | if (NextDepth < PredTotalLatency || |
| 558 | (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) { |
| 559 | NextDepth = PredTotalLatency; |
| 560 | Next = &*P; |
| 561 | } |
| 562 | } |
| 563 | return Next; |
| 564 | } |
| 565 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 566 | /// AntiDepPathStep - Return SUnit that SU has an anti-dependence on. |
| 567 | static SDep *AntiDepPathStep(SUnit *SU) { |
| 568 | for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
| 569 | P != PE; ++P) { |
| 570 | if (P->getKind() == SDep::Anti) { |
| 571 | return &*P; |
David Goodwin | c7951f8 | 2009-10-01 19:45:32 +0000 | [diff] [blame] | 572 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 573 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 574 | return 0; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 575 | } |
| 576 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 577 | void SchedulePostRATDList::PrescanInstruction(MachineInstr *MI, unsigned Count) { |
| 578 | // Scan the register defs for this instruction and update |
| 579 | // live-ranges, groups and RegRefs. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 580 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 581 | MachineOperand &MO = MI->getOperand(i); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 582 | if (!MO.isReg() || !MO.isDef()) continue; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 583 | unsigned Reg = MO.getReg(); |
| 584 | if (Reg == 0) continue; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 585 | // Ignore two-addr defs for liveness... |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 586 | if (MI->isRegTiedToUseOperand(i)) continue; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 587 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 588 | // Update Def for Reg and subregs. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 589 | DefIndices[Reg] = Count; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 590 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 591 | *Subreg; ++Subreg) { |
| 592 | unsigned SubregReg = *Subreg; |
| 593 | DefIndices[SubregReg] = Count; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 596 | |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 597 | DEBUG(errs() << "\tDef Groups:"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 598 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 599 | MachineOperand &MO = MI->getOperand(i); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 600 | if (!MO.isReg() || !MO.isDef()) continue; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 601 | unsigned Reg = MO.getReg(); |
| 602 | if (Reg == 0) continue; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 603 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 604 | DEBUG(errs() << " " << TRI->getName(Reg) << "=g" << GetGroup(Reg)); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 605 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 606 | // If MI's defs have special allocation requirement, don't allow |
| 607 | // any def registers to be changed. Also assume all registers |
| 608 | // defined in a call must not be changed (ABI). |
| 609 | if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq()) { |
| 610 | DEBUG(if (GetGroup(Reg) != 0) errs() << "->g0(alloc-req)"); |
| 611 | UnionGroups(Reg, 0); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 612 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 613 | |
| 614 | // Any subregisters that are live at this point are defined here, |
| 615 | // so group those subregisters with Reg. |
| 616 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 617 | *Subreg; ++Subreg) { |
| 618 | unsigned SubregReg = *Subreg; |
| 619 | if (IsLive(SubregReg)) { |
| 620 | UnionGroups(Reg, SubregReg); |
| 621 | DEBUG(errs() << "->g" << GetGroup(Reg) << "(via " << |
| 622 | TRI->getName(SubregReg) << ")"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 625 | |
| 626 | // Note register reference... |
| 627 | const TargetRegisterClass *RC = NULL; |
| 628 | if (i < MI->getDesc().getNumOperands()) |
| 629 | RC = MI->getDesc().OpInfo[i].getRegClass(TRI); |
| 630 | RegisterReference RR = { &MO, RC }; |
| 631 | RegRefs.insert(std::make_pair(Reg, RR)); |
| 632 | } |
| 633 | |
| 634 | DEBUG(errs() << '\n'); |
| 635 | } |
| 636 | |
| 637 | void SchedulePostRATDList::ScanInstruction(MachineInstr *MI, |
| 638 | unsigned Count) { |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 639 | DEBUG(errs() << "\tUse Groups:"); |
| 640 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 641 | // Scan the register uses for this instruction and update |
| 642 | // live-ranges, groups and RegRefs. |
| 643 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 644 | MachineOperand &MO = MI->getOperand(i); |
| 645 | if (!MO.isReg() || !MO.isUse()) continue; |
| 646 | unsigned Reg = MO.getReg(); |
| 647 | if (Reg == 0) continue; |
| 648 | |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 649 | DEBUG(errs() << " " << TRI->getName(Reg) << "=g" << GetGroup(Reg)); |
| 650 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 651 | // It wasn't previously live but now it is, this is a kill. Forget |
| 652 | // the previous live-range information and start a new live-range |
| 653 | // for the register. |
| 654 | if (!IsLive(Reg)) { |
| 655 | KillIndices[Reg] = Count; |
| 656 | DefIndices[Reg] = ~0u; |
| 657 | RegRefs.erase(Reg); |
| 658 | LeaveGroup(Reg); |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 659 | DEBUG(errs() << "->g" << GetGroup(Reg) << "(last-use)"); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 660 | } |
| 661 | // Repeat, for subregisters. |
| 662 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 663 | *Subreg; ++Subreg) { |
| 664 | unsigned SubregReg = *Subreg; |
| 665 | if (!IsLive(SubregReg)) { |
| 666 | KillIndices[SubregReg] = Count; |
| 667 | DefIndices[SubregReg] = ~0u; |
| 668 | RegRefs.erase(SubregReg); |
| 669 | LeaveGroup(SubregReg); |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 670 | DEBUG(errs() << "->g" << GetGroup(SubregReg) << "(last-use)"); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 674 | // If MI's uses have special allocation requirement, don't allow |
| 675 | // any use registers to be changed. Also assume all registers |
| 676 | // used in a call must not be changed (ABI). |
| 677 | if (MI->getDesc().isCall() || MI->getDesc().hasExtraSrcRegAllocReq()) { |
| 678 | DEBUG(if (GetGroup(Reg) != 0) errs() << "->g0(alloc-req)"); |
| 679 | UnionGroups(Reg, 0); |
| 680 | } |
| 681 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 682 | // Note register reference... |
| 683 | const TargetRegisterClass *RC = NULL; |
| 684 | if (i < MI->getDesc().getNumOperands()) |
| 685 | RC = MI->getDesc().OpInfo[i].getRegClass(TRI); |
| 686 | RegisterReference RR = { &MO, RC }; |
| 687 | RegRefs.insert(std::make_pair(Reg, RR)); |
| 688 | } |
| 689 | |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 690 | DEBUG(errs() << '\n'); |
| 691 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 692 | // Form a group of all defs and uses of a KILL instruction to ensure |
| 693 | // that all registers are renamed as a group. |
| 694 | if (MI->getOpcode() == TargetInstrInfo::KILL) { |
| 695 | unsigned FirstReg = 0; |
| 696 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 697 | MachineOperand &MO = MI->getOperand(i); |
| 698 | if (!MO.isReg()) continue; |
| 699 | unsigned Reg = MO.getReg(); |
| 700 | if (Reg == 0) continue; |
| 701 | |
| 702 | if (FirstReg != 0) |
| 703 | UnionGroups(FirstReg, Reg); |
| 704 | FirstReg = Reg; |
| 705 | } |
| 706 | |
| 707 | DEBUG(if (FirstReg != 0) errs() << "\tKill Group: g" << |
| 708 | GetGroup(FirstReg) << '\n'); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 712 | unsigned SchedulePostRATDList::FindSuitableFreeRegister(unsigned AntiDepReg) { |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 713 | // Collect all registers in the same group as AntiDepReg. These all |
| 714 | // need to be renamed together if we are to break the |
| 715 | // anti-dependence. |
| 716 | std::vector<unsigned> Regs; |
| 717 | GetGroupRegs(GetGroup(AntiDepReg), Regs); |
| 718 | |
| 719 | DEBUG(errs() << "\tRename Register Group:"); |
| 720 | DEBUG(for (unsigned i = 0, e = Regs.size(); i != e; ++i) |
| 721 | DEBUG(errs() << " " << TRI->getName(Regs[i]))); |
| 722 | DEBUG(errs() << "\n"); |
| 723 | |
| 724 | // If there is a single register that needs to be renamed then we |
| 725 | // can do it ourselves. |
| 726 | if (Regs.size() == 1) { |
| 727 | assert(Regs[0] == AntiDepReg && "Register group does not contain register!"); |
| 728 | |
| 729 | // Check all references that need rewriting. Gather up all the |
| 730 | // register classes for the register references. |
| 731 | const TargetRegisterClass *FirstRC = NULL; |
| 732 | std::set<const TargetRegisterClass *> RCs; |
| 733 | std::pair<std::multimap<unsigned, RegisterReference>::iterator, |
| 734 | std::multimap<unsigned, RegisterReference>::iterator> |
| 735 | Range = RegRefs.equal_range(AntiDepReg); |
| 736 | for (std::multimap<unsigned, RegisterReference>::iterator |
| 737 | Q = Range.first, QE = Range.second; Q != QE; ++Q) { |
| 738 | const TargetRegisterClass *RC = Q->second.RC; |
| 739 | if (RC == NULL) continue; |
| 740 | if (FirstRC == NULL) |
| 741 | FirstRC = RC; |
| 742 | else if (FirstRC != RC) |
| 743 | RCs.insert(RC); |
| 744 | } |
| 745 | |
| 746 | if (FirstRC == NULL) |
| 747 | return 0; |
| 748 | |
| 749 | DEBUG(errs() << "\tChecking Regclasses: " << FirstRC->getName()); |
| 750 | DEBUG(for (std::set<const TargetRegisterClass *>::iterator S = |
| 751 | RCs.begin(), E = RCs.end(); S != E; ++S) |
| 752 | errs() << " " << (*S)->getName()); |
| 753 | DEBUG(errs() << '\n'); |
| 754 | |
| 755 | // Using the allocation order for one of the register classes, |
| 756 | // find the first register that belongs to all the register |
| 757 | // classes that is available over the liverange of the register. |
| 758 | DEBUG(errs() << "\tFind Register:"); |
| 759 | for (TargetRegisterClass::iterator R = FirstRC->allocation_order_begin(MF), |
| 760 | RE = FirstRC->allocation_order_end(MF); R != RE; ++R) { |
| 761 | unsigned NewReg = *R; |
| 762 | |
| 763 | // Don't replace a register with itself. |
| 764 | if (NewReg == AntiDepReg) continue; |
| 765 | |
| 766 | DEBUG(errs() << " " << TRI->getName(NewReg)); |
| 767 | |
| 768 | // Make sure NewReg is in all required register classes. |
| 769 | for (std::set<const TargetRegisterClass *>::iterator S = |
| 770 | RCs.begin(), E = RCs.end(); S != E; ++S) { |
| 771 | const TargetRegisterClass *RC = *S; |
| 772 | if (!RC->contains(NewReg)) { |
| 773 | DEBUG(errs() << "(not in " << RC->getName() << ")"); |
| 774 | NewReg = 0; |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | // If NewReg is dead and NewReg's most recent def is not before |
| 780 | // AntiDepReg's kill, it's safe to replace AntiDepReg with |
| 781 | // NewReg. We must also check all subregisters of NewReg. |
| 782 | if (IsLive(NewReg) || (KillIndices[AntiDepReg] > DefIndices[NewReg])) { |
| 783 | DEBUG(errs() << "(live)"); |
| 784 | continue; |
| 785 | } |
| 786 | { |
| 787 | bool found = false; |
| 788 | for (const unsigned *Subreg = TRI->getSubRegisters(NewReg); |
| 789 | *Subreg; ++Subreg) { |
| 790 | unsigned SubregReg = *Subreg; |
| 791 | if (IsLive(SubregReg) || (KillIndices[AntiDepReg] > DefIndices[SubregReg])) { |
| 792 | DEBUG(errs() << "(subreg " << TRI->getName(SubregReg) << " live)"); |
| 793 | found = true; |
| 794 | } |
| 795 | } |
| 796 | if (found) |
| 797 | continue; |
| 798 | } |
| 799 | |
| 800 | if (NewReg != 0) { |
| 801 | DEBUG(errs() << '\n'); |
| 802 | return NewReg; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | DEBUG(errs() << '\n'); |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | // No registers are free and available! |
| 810 | return 0; |
| 811 | } |
| 812 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 813 | /// BreakAntiDependencies - Identifiy anti-dependencies along the critical path |
| 814 | /// of the ScheduleDAG and break them by renaming registers. |
| 815 | /// |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 816 | bool SchedulePostRATDList::BreakAntiDependencies(bool CriticalPathOnly) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 817 | // The code below assumes that there is at least one instruction, |
| 818 | // so just duck out immediately if the block is empty. |
| 819 | if (SUnits.empty()) return false; |
| 820 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 821 | // If breaking anti-dependencies only along the critical path, track |
| 822 | // progress along the critical path through the SUnit graph as we |
| 823 | // walk the instructions. |
| 824 | SUnit *CriticalPathSU = 0; |
| 825 | MachineInstr *CriticalPathMI = 0; |
| 826 | |
| 827 | // If breaking all anti-dependencies need a map from MI to SUnit. |
| 828 | std::map<MachineInstr *, SUnit *> MISUnitMap; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 829 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 830 | // Find the node at the bottom of the critical path. |
| 831 | if (CriticalPathOnly) { |
| 832 | SUnit *Max = 0; |
| 833 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 834 | SUnit *SU = &SUnits[i]; |
| 835 | if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency) |
| 836 | Max = SU; |
| 837 | } |
| 838 | |
David Goodwin | d452ea6 | 2009-10-13 19:16:03 +0000 | [diff] [blame] | 839 | DEBUG(errs() << "Critical path has total latency " |
| 840 | << (Max->getDepth() + Max->Latency) << "\n"); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 841 | CriticalPathSU = Max; |
| 842 | CriticalPathMI = CriticalPathSU->getInstr(); |
| 843 | } else { |
| 844 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 845 | SUnit *SU = &SUnits[i]; |
| 846 | MISUnitMap.insert(std::pair<MachineInstr *, SUnit *>(SU->getInstr(), SU)); |
| 847 | } |
| 848 | DEBUG(errs() << "Breaking all anti-dependencies\n"); |
| 849 | } |
| 850 | |
| 851 | #ifndef NDEBUG |
| 852 | { |
David Goodwin | d452ea6 | 2009-10-13 19:16:03 +0000 | [diff] [blame] | 853 | DEBUG(errs() << "Available regs:"); |
| 854 | for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) { |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 855 | if (!IsLive(Reg)) |
David Goodwin | d452ea6 | 2009-10-13 19:16:03 +0000 | [diff] [blame] | 856 | DEBUG(errs() << " " << TRI->getName(Reg)); |
| 857 | } |
| 858 | DEBUG(errs() << '\n'); |
| 859 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 860 | std::string dbgStr; |
David Goodwin | d452ea6 | 2009-10-13 19:16:03 +0000 | [diff] [blame] | 861 | #endif |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 862 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 863 | // Attempt to break anti-dependence edges. Walk the instructions |
| 864 | // from the bottom up, tracking information about liveness as we go |
| 865 | // to help determine which registers are available. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 866 | bool Changed = false; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 867 | unsigned Count = InsertPosIndex - 1; |
| 868 | for (MachineBasicBlock::iterator I = InsertPos, E = Begin; |
Dan Gohman | 43f07fb | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 869 | I != E; --Count) { |
| 870 | MachineInstr *MI = --I; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 871 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 872 | DEBUG(errs() << "Anti: "); |
| 873 | DEBUG(MI->dump()); |
| 874 | |
| 875 | // Process the defs in MI... |
| 876 | PrescanInstruction(MI, Count); |
| 877 | |
| 878 | // Check if this instruction has an anti-dependence that we may be |
| 879 | // able to break. If it is, set AntiDepReg to the non-zero |
| 880 | // register associated with the anti-dependence. |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 881 | // |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 882 | unsigned AntiDepReg = 0; |
| 883 | |
| 884 | // Limiting our attention to the critical path is a heuristic to avoid |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 885 | // breaking anti-dependence edges that aren't going to significantly |
| 886 | // impact the overall schedule. There are a limited number of registers |
| 887 | // and we want to save them for the important edges. |
| 888 | // |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 889 | // We can also break all anti-dependencies because they can |
| 890 | // occur along the non-critical path but are still detrimental for |
| 891 | // scheduling. |
| 892 | // |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 893 | // TODO: Instructions with multiple defs could have multiple |
| 894 | // anti-dependencies. The current code here only knows how to break one |
| 895 | // edge per instruction. Note that we'd have to be able to break all of |
| 896 | // the anti-dependencies in an instruction in order to be effective. |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 897 | if (!CriticalPathOnly || (MI == CriticalPathMI)) { |
| 898 | DEBUG(dbgStr.clear()); |
| 899 | |
| 900 | SUnit *PathSU; |
| 901 | SDep *Edge; |
| 902 | if (CriticalPathOnly) { |
| 903 | PathSU = CriticalPathSU; |
| 904 | Edge = CriticalPathStep(PathSU); |
| 905 | } else { |
| 906 | PathSU = MISUnitMap[MI]; |
| 907 | Edge = (PathSU) ? AntiDepPathStep(PathSU) : 0; |
| 908 | } |
| 909 | |
| 910 | if (Edge) { |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 911 | SUnit *NextSU = Edge->getSUnit(); |
| 912 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 913 | // Only consider anti-dependence edges, and ignore KILL |
| 914 | // instructions (they form a group in ScanInstruction but |
| 915 | // don't cause any anti-dependence breaking themselves) |
| 916 | if ((Edge->getKind() == SDep::Anti) && |
| 917 | (MI->getOpcode() != TargetInstrInfo::KILL)) { |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 918 | AntiDepReg = Edge->getReg(); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 919 | DEBUG(dbgStr += "\tAntidep reg: "); |
| 920 | DEBUG(dbgStr += TRI->getName(AntiDepReg)); |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 921 | assert(AntiDepReg != 0 && "Anti-dependence on reg0?"); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 922 | if (!AllocatableSet.test(AntiDepReg)) { |
Evan Cheng | 714e8bc | 2009-10-01 08:26:23 +0000 | [diff] [blame] | 923 | // Don't break anti-dependencies on non-allocatable registers. |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 924 | DEBUG(dbgStr += " (non-allocatable)"); |
Evan Cheng | 714e8bc | 2009-10-01 08:26:23 +0000 | [diff] [blame] | 925 | AntiDepReg = 0; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 926 | } else { |
| 927 | int OpIdx = MI->findRegisterDefOperandIdx(AntiDepReg); |
| 928 | assert(OpIdx != -1 && "Can't find index for defined register operand"); |
| 929 | if (MI->isRegTiedToUseOperand(OpIdx)) { |
| 930 | // If the anti-dep register is tied to a use, then don't try to |
| 931 | // change it. It will be changed along with the use if required |
| 932 | // to break an earlier antidep. |
| 933 | DEBUG(dbgStr += " (tied-to-use)"); |
| 934 | AntiDepReg = 0; |
| 935 | } else { |
| 936 | // If the SUnit has other dependencies on the SUnit that |
| 937 | // it anti-depends on, don't bother breaking the |
| 938 | // anti-dependency since those edges would prevent such |
| 939 | // units from being scheduled past each other |
| 940 | // regardless. |
| 941 | // |
| 942 | // Also, if there are dependencies on other SUnits with |
| 943 | // the same register as the anti-dependency, don't |
| 944 | // attempt to break it. |
| 945 | for (SUnit::pred_iterator P = PathSU->Preds.begin(), |
| 946 | PE = PathSU->Preds.end(); P != PE; ++P) { |
| 947 | if (P->getSUnit() == NextSU ? |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 948 | (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) : |
| 949 | (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) { |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 950 | DEBUG(dbgStr += " (real dependency)"); |
| 951 | AntiDepReg = 0; |
| 952 | break; |
| 953 | } |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 954 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 955 | } |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 956 | } |
| 957 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 958 | |
| 959 | if (CriticalPathOnly) { |
| 960 | CriticalPathSU = NextSU; |
| 961 | CriticalPathMI = CriticalPathSU->getInstr(); |
| 962 | } |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 963 | } else { |
| 964 | // We've reached the end of the critical path. |
| 965 | CriticalPathSU = 0; |
| 966 | CriticalPathMI = 0; |
| 967 | } |
| 968 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 969 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 970 | // Determine AntiDepReg's register group. |
| 971 | const unsigned GroupIndex = AntiDepReg != 0 ? GetGroup(AntiDepReg) : 0; |
| 972 | if (GroupIndex == 0) { |
| 973 | DEBUG(if (AntiDepReg != 0) dbgStr += " (zero group)"); |
Evan Cheng | 714e8bc | 2009-10-01 08:26:23 +0000 | [diff] [blame] | 974 | AntiDepReg = 0; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 975 | } |
| 976 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 977 | DEBUG(if (!dbgStr.empty()) errs() << dbgStr << '\n'); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 978 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 979 | // Look for a suitable register to use to break the anti-dependence. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 980 | if (AntiDepReg != 0) { |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 981 | if (unsigned NewReg = FindSuitableFreeRegister(AntiDepReg)) { |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 982 | DEBUG(errs() << "\tBreaking anti-dependence edge on " |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 983 | << TRI->getName(AntiDepReg) |
| 984 | << " with " << RegRefs.count(AntiDepReg) << " references" |
| 985 | << " using " << TRI->getName(NewReg) << "!\n"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 986 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 987 | // Update the references to the old register to refer to the new |
| 988 | // register. |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 989 | std::pair<std::multimap<unsigned, RegisterReference>::iterator, |
| 990 | std::multimap<unsigned, RegisterReference>::iterator> |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 991 | Range = RegRefs.equal_range(AntiDepReg); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 992 | for (std::multimap<unsigned, RegisterReference>::iterator |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 993 | Q = Range.first, QE = Range.second; Q != QE; ++Q) |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 994 | Q->second.Operand->setReg(NewReg); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 995 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 996 | // We just went back in time and modified history; the |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 997 | // liveness information for the anti-dependence reg is now |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 998 | // inconsistent. Set the state as if it were dead. |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 999 | // FIXME forall in group |
| 1000 | UnionGroups(NewReg, 0); |
| 1001 | RegRefs.erase(NewReg); |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 1002 | DefIndices[NewReg] = DefIndices[AntiDepReg]; |
| 1003 | KillIndices[NewReg] = KillIndices[AntiDepReg]; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 1004 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 1005 | // FIXME forall in group |
| 1006 | UnionGroups(AntiDepReg, 0); |
| 1007 | RegRefs.erase(AntiDepReg); |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 1008 | DefIndices[AntiDepReg] = KillIndices[AntiDepReg]; |
| 1009 | KillIndices[AntiDepReg] = ~0u; |
| 1010 | assert(((KillIndices[AntiDepReg] == ~0u) != |
| 1011 | (DefIndices[AntiDepReg] == ~0u)) && |
| 1012 | "Kill and Def maps aren't consistent for AntiDepReg!"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 1013 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 1014 | Changed = true; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 1015 | ++NumFixedAnti; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1019 | ScanInstruction(MI, Count); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 1020 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 1021 | |
| 1022 | return Changed; |
| 1023 | } |
| 1024 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 1025 | /// StartBlockForKills - Initialize register live-range state for updating kills |
| 1026 | /// |
| 1027 | void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) { |
| 1028 | // Initialize the indices to indicate that no registers are live. |
| 1029 | std::fill(KillIndices, array_endof(KillIndices), ~0u); |
| 1030 | |
| 1031 | // Determine the live-out physregs for this block. |
| 1032 | if (!BB->empty() && BB->back().getDesc().isReturn()) { |
| 1033 | // In a return block, examine the function live-out regs. |
| 1034 | for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), |
| 1035 | E = MRI.liveout_end(); I != E; ++I) { |
| 1036 | unsigned Reg = *I; |
| 1037 | KillIndices[Reg] = BB->size(); |
| 1038 | // Repeat, for all subregs. |
| 1039 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 1040 | *Subreg; ++Subreg) { |
| 1041 | KillIndices[*Subreg] = BB->size(); |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | else { |
| 1046 | // In a non-return block, examine the live-in regs of all successors. |
| 1047 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 1048 | SE = BB->succ_end(); SI != SE; ++SI) { |
| 1049 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 1050 | E = (*SI)->livein_end(); I != E; ++I) { |
| 1051 | unsigned Reg = *I; |
| 1052 | KillIndices[Reg] = BB->size(); |
| 1053 | // Repeat, for all subregs. |
| 1054 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 1055 | *Subreg; ++Subreg) { |
| 1056 | KillIndices[*Subreg] = BB->size(); |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 1063 | bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI, |
| 1064 | MachineOperand &MO) { |
| 1065 | // Setting kill flag... |
| 1066 | if (!MO.isKill()) { |
| 1067 | MO.setIsKill(true); |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | // If MO itself is live, clear the kill flag... |
| 1072 | if (KillIndices[MO.getReg()] != ~0u) { |
| 1073 | MO.setIsKill(false); |
| 1074 | return false; |
| 1075 | } |
| 1076 | |
| 1077 | // If any subreg of MO is live, then create an imp-def for that |
| 1078 | // subreg and keep MO marked as killed. |
Benjamin Kramer | 8bff4af | 2009-10-02 15:59:52 +0000 | [diff] [blame] | 1079 | MO.setIsKill(false); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 1080 | bool AllDead = true; |
| 1081 | const unsigned SuperReg = MO.getReg(); |
| 1082 | for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg); |
| 1083 | *Subreg; ++Subreg) { |
| 1084 | if (KillIndices[*Subreg] != ~0u) { |
| 1085 | MI->addOperand(MachineOperand::CreateReg(*Subreg, |
| 1086 | true /*IsDef*/, |
| 1087 | true /*IsImp*/, |
| 1088 | false /*IsKill*/, |
| 1089 | false /*IsDead*/)); |
| 1090 | AllDead = false; |
| 1091 | } |
| 1092 | } |
| 1093 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 1094 | if (AllDead) |
Benjamin Kramer | 8bff4af | 2009-10-02 15:59:52 +0000 | [diff] [blame] | 1095 | MO.setIsKill(true); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 1096 | return false; |
| 1097 | } |
| 1098 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1099 | /// FixupKills - Fix the register kill flags, they may have been made |
| 1100 | /// incorrect by instruction reordering. |
| 1101 | /// |
| 1102 | void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) { |
| 1103 | DEBUG(errs() << "Fixup kills for BB ID#" << MBB->getNumber() << '\n'); |
| 1104 | |
| 1105 | std::set<unsigned> killedRegs; |
| 1106 | BitVector ReservedRegs = TRI->getReservedRegs(MF); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 1107 | |
| 1108 | StartBlockForKills(MBB); |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Examine block from end to start... |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1111 | unsigned Count = MBB->size(); |
| 1112 | for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin(); |
| 1113 | I != E; --Count) { |
| 1114 | MachineInstr *MI = --I; |
| 1115 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1116 | // Update liveness. Registers that are defed but not used in this |
| 1117 | // instruction are now dead. Mark register and all subregs as they |
| 1118 | // are completely defined. |
| 1119 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1120 | MachineOperand &MO = MI->getOperand(i); |
| 1121 | if (!MO.isReg()) continue; |
| 1122 | unsigned Reg = MO.getReg(); |
| 1123 | if (Reg == 0) continue; |
| 1124 | if (!MO.isDef()) continue; |
| 1125 | // Ignore two-addr defs. |
| 1126 | if (MI->isRegTiedToUseOperand(i)) continue; |
| 1127 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1128 | KillIndices[Reg] = ~0u; |
| 1129 | |
| 1130 | // Repeat for all subregs. |
| 1131 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 1132 | *Subreg; ++Subreg) { |
| 1133 | KillIndices[*Subreg] = ~0u; |
| 1134 | } |
| 1135 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1136 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 1137 | // Examine all used registers and set/clear kill flag. When a |
| 1138 | // register is used multiple times we only set the kill flag on |
| 1139 | // the first use. |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1140 | killedRegs.clear(); |
| 1141 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1142 | MachineOperand &MO = MI->getOperand(i); |
| 1143 | if (!MO.isReg() || !MO.isUse()) continue; |
| 1144 | unsigned Reg = MO.getReg(); |
| 1145 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 1146 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1147 | bool kill = false; |
| 1148 | if (killedRegs.find(Reg) == killedRegs.end()) { |
| 1149 | kill = true; |
| 1150 | // A register is not killed if any subregs are live... |
| 1151 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 1152 | *Subreg; ++Subreg) { |
| 1153 | if (KillIndices[*Subreg] != ~0u) { |
| 1154 | kill = false; |
| 1155 | break; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | // If subreg is not live, then register is killed if it became |
| 1160 | // live in this instruction |
| 1161 | if (kill) |
| 1162 | kill = (KillIndices[Reg] == ~0u); |
| 1163 | } |
| 1164 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1165 | if (MO.isKill() != kill) { |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 1166 | bool removed = ToggleKillFlag(MI, MO); |
| 1167 | if (removed) { |
| 1168 | DEBUG(errs() << "Fixed <removed> in "); |
| 1169 | } else { |
| 1170 | DEBUG(errs() << "Fixed " << MO << " in "); |
| 1171 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1172 | DEBUG(MI->dump()); |
| 1173 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1174 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1175 | killedRegs.insert(Reg); |
| 1176 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1177 | |
David Goodwin | a3251db | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 1178 | // Mark any used register (that is not using undef) and subregs as |
| 1179 | // now live... |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1180 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1181 | MachineOperand &MO = MI->getOperand(i); |
David Goodwin | a3251db | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 1182 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1183 | unsigned Reg = MO.getReg(); |
| 1184 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 1185 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1186 | KillIndices[Reg] = Count; |
| 1187 | |
| 1188 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 1189 | *Subreg; ++Subreg) { |
| 1190 | KillIndices[*Subreg] = Count; |
| 1191 | } |
| 1192 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1196 | //===----------------------------------------------------------------------===// |
| 1197 | // Top-Down Scheduling |
| 1198 | //===----------------------------------------------------------------------===// |
| 1199 | |
| 1200 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
| 1201 | /// the PendingQueue if the count reaches zero. Also update its cycle bound. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 1202 | void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { |
| 1203 | SUnit *SuccSU = SuccEdge->getSUnit(); |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 1204 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1205 | #ifndef NDEBUG |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 1206 | if (SuccSU->NumPredsLeft == 0) { |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 1207 | errs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1208 | SuccSU->dump(this); |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 1209 | errs() << " has been released too many times!\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1210 | llvm_unreachable(0); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1211 | } |
| 1212 | #endif |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 1213 | --SuccSU->NumPredsLeft; |
| 1214 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1215 | // Compute how many cycles it will be before this actually becomes |
| 1216 | // available. This is the max of the start time of all predecessors plus |
| 1217 | // their latencies. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1218 | SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1219 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1220 | // If all the node's predecessors are scheduled, this node is ready |
| 1221 | // to be scheduled. Ignore the special ExitSU node. |
| 1222 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1223 | PendingQueue.push_back(SuccSU); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. |
| 1227 | void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { |
| 1228 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1229 | I != E; ++I) |
| 1230 | ReleaseSucc(SU, &*I); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 1234 | /// count of its successors. If a successor pending count is zero, add it to |
| 1235 | /// the Available queue. |
| 1236 | void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 1237 | DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: "); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1238 | DEBUG(SU->dump(this)); |
| 1239 | |
| 1240 | Sequence.push_back(SU); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1241 | assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!"); |
| 1242 | SU->setDepthToAtLeast(CurCycle); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1243 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1244 | ReleaseSuccessors(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1245 | SU->isScheduled = true; |
| 1246 | AvailableQueue.ScheduledNode(SU); |
| 1247 | } |
| 1248 | |
| 1249 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 1250 | /// schedulers. |
| 1251 | void SchedulePostRATDList::ListScheduleTopDown() { |
| 1252 | unsigned CurCycle = 0; |
| 1253 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1254 | // Release any successors of the special Entry node. |
| 1255 | ReleaseSuccessors(&EntrySU); |
| 1256 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1257 | // All leaves to Available queue. |
| 1258 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 1259 | // It is available if it has no predecessors. |
| 1260 | if (SUnits[i].Preds.empty()) { |
| 1261 | AvailableQueue.push(&SUnits[i]); |
| 1262 | SUnits[i].isAvailable = true; |
| 1263 | } |
| 1264 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1265 | |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1266 | // In any cycle where we can't schedule any instructions, we must |
| 1267 | // stall or emit a noop, depending on the target. |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1268 | bool CycleHasInsts = false; |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1269 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1270 | // While Available queue is not empty, grab the node with the highest |
| 1271 | // priority. If it is not ready put it back. Schedule the node. |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1272 | std::vector<SUnit*> NotReady; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1273 | Sequence.reserve(SUnits.size()); |
| 1274 | while (!AvailableQueue.empty() || !PendingQueue.empty()) { |
| 1275 | // Check to see if any of the pending instructions are ready to issue. If |
| 1276 | // so, add them to the available queue. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1277 | unsigned MinDepth = ~0u; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1278 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1279 | if (PendingQueue[i]->getDepth() <= CurCycle) { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1280 | AvailableQueue.push(PendingQueue[i]); |
| 1281 | PendingQueue[i]->isAvailable = true; |
| 1282 | PendingQueue[i] = PendingQueue.back(); |
| 1283 | PendingQueue.pop_back(); |
| 1284 | --i; --e; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1285 | } else if (PendingQueue[i]->getDepth() < MinDepth) |
| 1286 | MinDepth = PendingQueue[i]->getDepth(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1287 | } |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 1288 | |
David Goodwin | 7cd0118 | 2009-08-11 17:56:42 +0000 | [diff] [blame] | 1289 | DEBUG(errs() << "\n*** Examining Available\n"; |
| 1290 | LatencyPriorityQueue q = AvailableQueue; |
| 1291 | while (!q.empty()) { |
| 1292 | SUnit *su = q.pop(); |
| 1293 | errs() << "Height " << su->getHeight() << ": "; |
| 1294 | su->dump(this); |
| 1295 | }); |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 1296 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1297 | SUnit *FoundSUnit = 0; |
| 1298 | |
| 1299 | bool HasNoopHazards = false; |
| 1300 | while (!AvailableQueue.empty()) { |
| 1301 | SUnit *CurSUnit = AvailableQueue.pop(); |
| 1302 | |
| 1303 | ScheduleHazardRecognizer::HazardType HT = |
| 1304 | HazardRec->getHazardType(CurSUnit); |
| 1305 | if (HT == ScheduleHazardRecognizer::NoHazard) { |
| 1306 | FoundSUnit = CurSUnit; |
| 1307 | break; |
| 1308 | } |
| 1309 | |
| 1310 | // Remember if this is a noop hazard. |
| 1311 | HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; |
| 1312 | |
| 1313 | NotReady.push_back(CurSUnit); |
| 1314 | } |
| 1315 | |
| 1316 | // Add the nodes that aren't ready back onto the available list. |
| 1317 | if (!NotReady.empty()) { |
| 1318 | AvailableQueue.push_all(NotReady); |
| 1319 | NotReady.clear(); |
| 1320 | } |
| 1321 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1322 | // If we found a node to schedule, do it now. |
| 1323 | if (FoundSUnit) { |
| 1324 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1325 | HazardRec->EmitInstruction(FoundSUnit); |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1326 | CycleHasInsts = true; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1327 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 1328 | // If we are using the target-specific hazards, then don't |
| 1329 | // advance the cycle time just because we schedule a node. If |
| 1330 | // the target allows it we can schedule multiple nodes in the |
| 1331 | // same cycle. |
| 1332 | if (!EnablePostRAHazardAvoidance) { |
| 1333 | if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops! |
| 1334 | ++CurCycle; |
| 1335 | } |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1336 | } else { |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1337 | if (CycleHasInsts) { |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1338 | DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n'); |
| 1339 | HazardRec->AdvanceCycle(); |
| 1340 | } else if (!HasNoopHazards) { |
| 1341 | // Otherwise, we have a pipeline stall, but no other problem, |
| 1342 | // just advance the current cycle and try again. |
| 1343 | DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n'); |
| 1344 | HazardRec->AdvanceCycle(); |
| 1345 | ++NumStalls; |
| 1346 | } else { |
| 1347 | // Otherwise, we have no instructions to issue and we have instructions |
| 1348 | // that will fault if we don't do this right. This is the case for |
| 1349 | // processors without pipeline interlocks and other cases. |
| 1350 | DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n'); |
| 1351 | HazardRec->EmitNoop(); |
| 1352 | Sequence.push_back(0); // NULL here means noop |
| 1353 | ++NumNoops; |
| 1354 | } |
| 1355 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1356 | ++CurCycle; |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1357 | CycleHasInsts = false; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | #ifndef NDEBUG |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 1362 | VerifySchedule(/*isBottomUp=*/false); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1363 | #endif |
| 1364 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 1365 | |
| 1366 | //===----------------------------------------------------------------------===// |
| 1367 | // Public Constructor Functions |
| 1368 | //===----------------------------------------------------------------------===// |
| 1369 | |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 1370 | FunctionPass *llvm::createPostRAScheduler(CodeGenOpt::Level OptLevel) { |
| 1371 | return new PostRAScheduler(OptLevel); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 1372 | } |