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" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetInstrInfo.h" |
| 36 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 459525d | 2008-01-14 19:00:06 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Compiler.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 42 | #include <map> |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 43 | #include <set> |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 46 | STATISTIC(NumNoops, "Number of noops inserted"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 47 | STATISTIC(NumStalls, "Number of pipeline stalls"); |
| 48 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 49 | static cl::opt<bool> |
| 50 | EnableAntiDepBreaking("break-anti-dependencies", |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 51 | cl::desc("Break post-RA scheduling anti-dependencies"), |
| 52 | cl::init(true), cl::Hidden); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 53 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 54 | static cl::opt<bool> |
| 55 | EnablePostRAHazardAvoidance("avoid-hazards", |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 56 | cl::desc("Enable exact hazard avoidance"), |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 57 | cl::init(true), cl::Hidden); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 58 | |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 59 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 60 | static cl::opt<int> |
| 61 | DebugDiv("postra-sched-debugdiv", |
| 62 | cl::desc("Debug control MBBs that are scheduled"), |
| 63 | cl::init(0), cl::Hidden); |
| 64 | static cl::opt<int> |
| 65 | DebugMod("postra-sched-debugmod", |
| 66 | cl::desc("Debug control MBBs that are scheduled"), |
| 67 | cl::init(0), cl::Hidden); |
| 68 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 69 | namespace { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 70 | class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass { |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 71 | public: |
| 72 | static char ID; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 73 | PostRAScheduler() : MachineFunctionPass(&ID) {} |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 74 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 75 | void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 76 | AU.setPreservesCFG(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 77 | AU.addRequired<MachineDominatorTree>(); |
| 78 | AU.addPreserved<MachineDominatorTree>(); |
| 79 | AU.addRequired<MachineLoopInfo>(); |
| 80 | AU.addPreserved<MachineLoopInfo>(); |
| 81 | MachineFunctionPass::getAnalysisUsage(AU); |
| 82 | } |
| 83 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 84 | const char *getPassName() const { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 85 | return "Post RA top-down list latency scheduler"; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | bool runOnMachineFunction(MachineFunction &Fn); |
| 89 | }; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 90 | char PostRAScheduler::ID = 0; |
| 91 | |
| 92 | class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 93 | /// AvailableQueue - The priority queue to use for the available SUnits. |
| 94 | /// |
| 95 | LatencyPriorityQueue AvailableQueue; |
| 96 | |
| 97 | /// PendingQueue - This contains all of the instructions whose operands have |
| 98 | /// been issued, but their results are not ready yet (due to the latency of |
| 99 | /// the operation). Once the operands becomes available, the instruction is |
| 100 | /// added to the AvailableQueue. |
| 101 | std::vector<SUnit*> PendingQueue; |
| 102 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 103 | /// Topo - A topological ordering for SUnits. |
| 104 | ScheduleDAGTopologicalSort Topo; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 105 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 106 | /// AllocatableSet - The set of allocatable registers. |
| 107 | /// We'll be ignoring anti-dependencies on non-allocatable registers, |
| 108 | /// because they may not be safe to break. |
| 109 | const BitVector AllocatableSet; |
| 110 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 111 | /// HazardRec - The hazard recognizer to use. |
| 112 | ScheduleHazardRecognizer *HazardRec; |
| 113 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 114 | /// Classes - For live regs that are only used in one register class in a |
| 115 | /// live range, the register class. If the register is not live, the |
| 116 | /// corresponding value is null. If the register is live but used in |
| 117 | /// multiple register classes, the corresponding value is -1 casted to a |
| 118 | /// pointer. |
| 119 | const TargetRegisterClass * |
| 120 | Classes[TargetRegisterInfo::FirstVirtualRegister]; |
| 121 | |
| 122 | /// RegRegs - Map registers to all their references within a live range. |
| 123 | std::multimap<unsigned, MachineOperand *> RegRefs; |
| 124 | |
| 125 | /// The index of the most recent kill (proceding bottom-up), or ~0u if |
| 126 | /// the register is not live. |
| 127 | unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 128 | |
| 129 | /// The index of the most recent complete def (proceding bottom up), or ~0u |
| 130 | /// if the register is live. |
| 131 | unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 132 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 133 | public: |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 134 | SchedulePostRATDList(MachineFunction &MF, |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 135 | const MachineLoopInfo &MLI, |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 136 | const MachineDominatorTree &MDT, |
| 137 | ScheduleHazardRecognizer *HR) |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 138 | : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits), |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 139 | AllocatableSet(TRI->getAllocatableSet(MF)), |
| 140 | HazardRec(HR) {} |
| 141 | |
| 142 | ~SchedulePostRATDList() { |
| 143 | delete HazardRec; |
| 144 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 145 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 146 | /// StartBlock - Initialize register live-range state for scheduling in |
| 147 | /// this block. |
| 148 | /// |
| 149 | void StartBlock(MachineBasicBlock *BB); |
| 150 | |
| 151 | /// Schedule - Schedule the instruction range using list scheduling. |
| 152 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 153 | void Schedule(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 154 | |
| 155 | /// FixupKills - Fix register kill flags that have been made |
| 156 | /// invalid due to scheduling |
| 157 | /// |
| 158 | void FixupKills(MachineBasicBlock *MBB); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 159 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 160 | /// Observe - Update liveness information to account for the current |
| 161 | /// instruction, which will not be scheduled. |
| 162 | /// |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 163 | void Observe(MachineInstr *MI, unsigned Count); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 164 | |
| 165 | /// FinishBlock - Clean up register live-range state. |
| 166 | /// |
| 167 | void FinishBlock(); |
| 168 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 169 | private: |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 170 | void PrescanInstruction(MachineInstr *MI); |
| 171 | void ScanInstruction(MachineInstr *MI, unsigned Count); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 172 | void ReleaseSucc(SUnit *SU, SDep *SuccEdge); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 173 | void ReleaseSuccessors(SUnit *SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 174 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
| 175 | void ListScheduleTopDown(); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 176 | bool BreakAntiDependencies(); |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 177 | unsigned findSuitableFreeRegister(unsigned AntiDepReg, |
| 178 | unsigned LastNewReg, |
| 179 | const TargetRegisterClass *); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 180 | void StartBlockForKills(MachineBasicBlock *BB); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 181 | |
| 182 | // ToggleKillFlag - Toggle a register operand kill flag. Other |
| 183 | // adjustments may be made to the instruction if necessary. Return |
| 184 | // true if the operand has been deleted, false if not. |
| 185 | bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 186 | }; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 189 | /// isSchedulingBoundary - Test if the given instruction should be |
| 190 | /// considered a scheduling boundary. This primarily includes labels |
| 191 | /// and terminators. |
| 192 | /// |
| 193 | static bool isSchedulingBoundary(const MachineInstr *MI, |
| 194 | const MachineFunction &MF) { |
| 195 | // Terminators and labels can't be scheduled around. |
| 196 | if (MI->getDesc().isTerminator() || MI->isLabel()) |
| 197 | return true; |
| 198 | |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 199 | // Don't attempt to schedule around any instruction that modifies |
| 200 | // a stack-oriented pointer, as it's unlikely to be profitable. This |
| 201 | // saves compile time, because it doesn't require every single |
| 202 | // stack slot reference to depend on the instruction that does the |
| 203 | // modification. |
| 204 | const TargetLowering &TLI = *MF.getTarget().getTargetLowering(); |
| 205 | if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore())) |
| 206 | return true; |
| 207 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 211 | bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 212 | DEBUG(errs() << "PostRAScheduler\n"); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 213 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 214 | const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 215 | const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 216 | const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData(); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 217 | ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ? |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 218 | (ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) : |
| 219 | (ScheduleHazardRecognizer *)new SimpleHazardRecognizer(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 220 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 221 | SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 222 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 223 | // Loop over all of the basic blocks |
| 224 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 225 | MBB != MBBe; ++MBB) { |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 226 | #ifndef NDEBUG |
| 227 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 228 | if (DebugDiv > 0) { |
| 229 | static int bbcnt = 0; |
| 230 | if (bbcnt++ % DebugDiv != DebugMod) |
| 231 | continue; |
| 232 | errs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() << |
| 233 | ":MBB ID#" << MBB->getNumber() << " ***\n"; |
| 234 | } |
| 235 | #endif |
| 236 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 237 | // Initialize register live-range state for scheduling in this block. |
| 238 | Scheduler.StartBlock(MBB); |
| 239 | |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 240 | // Schedule each sequence of instructions not interrupted by a label |
| 241 | // or anything else that effectively needs to shut down scheduling. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 242 | MachineBasicBlock::iterator Current = MBB->end(); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 243 | unsigned Count = MBB->size(), CurrentCount = Count; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 244 | for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) { |
| 245 | MachineInstr *MI = prior(I); |
| 246 | if (isSchedulingBoundary(MI, Fn)) { |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 247 | Scheduler.Run(MBB, I, Current, CurrentCount); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 248 | Scheduler.EmitSchedule(0); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 249 | Current = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 250 | CurrentCount = Count - 1; |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 251 | Scheduler.Observe(MI, CurrentCount); |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 252 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 253 | I = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 254 | --Count; |
Dan Gohman | 43f07fb | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 255 | } |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 256 | assert(Count == 0 && "Instruction count mismatch!"); |
Duncan Sands | 9e8bd0b | 2009-03-11 09:04:34 +0000 | [diff] [blame] | 257 | assert((MBB->begin() == Current || CurrentCount != 0) && |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 258 | "Instruction count mismatch!"); |
| 259 | Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 260 | Scheduler.EmitSchedule(0); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 261 | |
| 262 | // Clean up register live-range state. |
| 263 | Scheduler.FinishBlock(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 264 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 265 | // Update register kills |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 266 | Scheduler.FixupKills(MBB); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 267 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 272 | /// StartBlock - Initialize register live-range state for scheduling in |
| 273 | /// this block. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 274 | /// |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 275 | void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) { |
| 276 | // Call the superclass. |
| 277 | ScheduleDAGInstrs::StartBlock(BB); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 278 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 279 | // Reset the hazard recognizer. |
| 280 | HazardRec->Reset(); |
| 281 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 282 | // Clear out the register class data. |
| 283 | std::fill(Classes, array_endof(Classes), |
| 284 | static_cast<const TargetRegisterClass *>(0)); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 285 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 286 | // Initialize the indices to indicate that no registers are live. |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 287 | std::fill(KillIndices, array_endof(KillIndices), ~0u); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 288 | std::fill(DefIndices, array_endof(DefIndices), BB->size()); |
| 289 | |
| 290 | // Determine the live-out physregs for this block. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 291 | if (!BB->empty() && BB->back().getDesc().isReturn()) |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 292 | // In a return block, examine the function live-out regs. |
| 293 | for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), |
| 294 | E = MRI.liveout_end(); I != E; ++I) { |
| 295 | unsigned Reg = *I; |
| 296 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 297 | KillIndices[Reg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 298 | DefIndices[Reg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 299 | // Repeat, for all aliases. |
| 300 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 301 | unsigned AliasReg = *Alias; |
| 302 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 303 | KillIndices[AliasReg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 304 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | else |
| 308 | // In a non-return block, examine the live-in regs of all successors. |
| 309 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 310 | SE = BB->succ_end(); SI != SE; ++SI) |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 311 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 312 | E = (*SI)->livein_end(); I != E; ++I) { |
| 313 | unsigned Reg = *I; |
| 314 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 315 | KillIndices[Reg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 316 | DefIndices[Reg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 317 | // Repeat, for all aliases. |
| 318 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 319 | unsigned AliasReg = *Alias; |
| 320 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 321 | KillIndices[AliasReg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 322 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 326 | // Consider callee-saved registers as live-out, since we're running after |
| 327 | // prologue/epilogue insertion so there's no way to add additional |
| 328 | // saved registers. |
| 329 | // |
| 330 | // TODO: there is a new method |
| 331 | // MachineFrameInfo::getPristineRegs(MBB). It gives you a list of |
| 332 | // CSRs that have not been saved when entering the MBB. The |
| 333 | // remaining CSRs have been saved and can be treated like call |
| 334 | // clobbered registers. |
| 335 | for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) { |
| 336 | unsigned Reg = *I; |
| 337 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 338 | KillIndices[Reg] = BB->size(); |
| 339 | DefIndices[Reg] = ~0u; |
| 340 | // Repeat, for all aliases. |
| 341 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 342 | unsigned AliasReg = *Alias; |
| 343 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 344 | KillIndices[AliasReg] = BB->size(); |
| 345 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /// Schedule - Schedule the instruction range using list scheduling. |
| 351 | /// |
| 352 | void SchedulePostRATDList::Schedule() { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 353 | DEBUG(errs() << "********** List Scheduling **********\n"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 354 | |
| 355 | // Build the scheduling graph. |
| 356 | BuildSchedGraph(); |
| 357 | |
| 358 | if (EnableAntiDepBreaking) { |
| 359 | if (BreakAntiDependencies()) { |
| 360 | // We made changes. Update the dependency graph. |
| 361 | // Theoretically we could update the graph in place: |
| 362 | // When a live range is changed to use a different register, remove |
| 363 | // the def's anti-dependence *and* output-dependence edges due to |
| 364 | // that register, and add new anti-dependence and output-dependence |
| 365 | // edges based on the next live range of the register. |
| 366 | SUnits.clear(); |
| 367 | EntrySU = SUnit(); |
| 368 | ExitSU = SUnit(); |
| 369 | BuildSchedGraph(); |
| 370 | } |
| 371 | } |
| 372 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 373 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 374 | SUnits[su].dumpAll(this)); |
| 375 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 376 | AvailableQueue.initNodes(SUnits); |
| 377 | |
| 378 | ListScheduleTopDown(); |
| 379 | |
| 380 | AvailableQueue.releaseState(); |
| 381 | } |
| 382 | |
| 383 | /// Observe - Update liveness information to account for the current |
| 384 | /// instruction, which will not be scheduled. |
| 385 | /// |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 386 | void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) { |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 387 | assert(Count < InsertPosIndex && "Instruction index out of expected range!"); |
| 388 | |
| 389 | // Any register which was defined within the previous scheduling region |
| 390 | // may have been rescheduled and its lifetime may overlap with registers |
| 391 | // in ways not reflected in our current liveness state. For each such |
| 392 | // register, adjust the liveness state to be conservatively correct. |
| 393 | for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg) |
| 394 | if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) { |
| 395 | assert(KillIndices[Reg] == ~0u && "Clobbered register is live!"); |
| 396 | // Mark this register to be non-renamable. |
| 397 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 398 | // Move the def index to the end of the previous region, to reflect |
| 399 | // that the def could theoretically have been scheduled at the end. |
| 400 | DefIndices[Reg] = InsertPosIndex; |
| 401 | } |
| 402 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 403 | PrescanInstruction(MI); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 404 | ScanInstruction(MI, Count); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /// FinishBlock - Clean up register live-range state. |
| 408 | /// |
| 409 | void SchedulePostRATDList::FinishBlock() { |
| 410 | RegRefs.clear(); |
| 411 | |
| 412 | // Call the superclass. |
| 413 | ScheduleDAGInstrs::FinishBlock(); |
| 414 | } |
| 415 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 416 | /// CriticalPathStep - Return the next SUnit after SU on the bottom-up |
| 417 | /// critical path. |
| 418 | static SDep *CriticalPathStep(SUnit *SU) { |
| 419 | SDep *Next = 0; |
| 420 | unsigned NextDepth = 0; |
| 421 | // Find the predecessor edge with the greatest depth. |
| 422 | for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
| 423 | P != PE; ++P) { |
| 424 | SUnit *PredSU = P->getSUnit(); |
| 425 | unsigned PredLatency = P->getLatency(); |
| 426 | unsigned PredTotalLatency = PredSU->getDepth() + PredLatency; |
| 427 | // In the case of a latency tie, prefer an anti-dependency edge over |
| 428 | // other types of edges. |
| 429 | if (NextDepth < PredTotalLatency || |
| 430 | (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) { |
| 431 | NextDepth = PredTotalLatency; |
| 432 | Next = &*P; |
| 433 | } |
| 434 | } |
| 435 | return Next; |
| 436 | } |
| 437 | |
| 438 | void SchedulePostRATDList::PrescanInstruction(MachineInstr *MI) { |
| 439 | // Scan the register operands for this instruction and update |
| 440 | // Classes and RegRefs. |
| 441 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 442 | MachineOperand &MO = MI->getOperand(i); |
| 443 | if (!MO.isReg()) continue; |
| 444 | unsigned Reg = MO.getReg(); |
| 445 | if (Reg == 0) continue; |
Chris Lattner | 2a38688 | 2009-07-29 21:36:49 +0000 | [diff] [blame] | 446 | const TargetRegisterClass *NewRC = 0; |
| 447 | |
| 448 | if (i < MI->getDesc().getNumOperands()) |
| 449 | NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 450 | |
| 451 | // For now, only allow the register to be changed if its register |
| 452 | // class is consistent across all uses. |
| 453 | if (!Classes[Reg] && NewRC) |
| 454 | Classes[Reg] = NewRC; |
| 455 | else if (!NewRC || Classes[Reg] != NewRC) |
| 456 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 457 | |
| 458 | // Now check for aliases. |
| 459 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 460 | // If an alias of the reg is used during the live range, give up. |
| 461 | // Note that this allows us to skip checking if AntiDepReg |
| 462 | // overlaps with any of the aliases, among other things. |
| 463 | unsigned AliasReg = *Alias; |
| 464 | if (Classes[AliasReg]) { |
| 465 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 466 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // If we're still willing to consider this register, note the reference. |
| 471 | if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1)) |
| 472 | RegRefs.insert(std::make_pair(Reg, &MO)); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | void SchedulePostRATDList::ScanInstruction(MachineInstr *MI, |
| 477 | unsigned Count) { |
| 478 | // Update liveness. |
| 479 | // Proceding upwards, registers that are defed but not used in this |
| 480 | // instruction are now dead. |
| 481 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 482 | MachineOperand &MO = MI->getOperand(i); |
| 483 | if (!MO.isReg()) continue; |
| 484 | unsigned Reg = MO.getReg(); |
| 485 | if (Reg == 0) continue; |
| 486 | if (!MO.isDef()) continue; |
| 487 | // Ignore two-addr defs. |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 488 | if (MI->isRegTiedToUseOperand(i)) continue; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 489 | |
| 490 | DefIndices[Reg] = Count; |
| 491 | KillIndices[Reg] = ~0u; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 492 | assert(((KillIndices[Reg] == ~0u) != |
| 493 | (DefIndices[Reg] == ~0u)) && |
| 494 | "Kill and Def maps aren't consistent for Reg!"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 495 | Classes[Reg] = 0; |
| 496 | RegRefs.erase(Reg); |
| 497 | // Repeat, for all subregs. |
| 498 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 499 | *Subreg; ++Subreg) { |
| 500 | unsigned SubregReg = *Subreg; |
| 501 | DefIndices[SubregReg] = Count; |
| 502 | KillIndices[SubregReg] = ~0u; |
| 503 | Classes[SubregReg] = 0; |
| 504 | RegRefs.erase(SubregReg); |
| 505 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 506 | // Conservatively mark super-registers as unusable. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 507 | for (const unsigned *Super = TRI->getSuperRegisters(Reg); |
| 508 | *Super; ++Super) { |
| 509 | unsigned SuperReg = *Super; |
| 510 | Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 511 | } |
| 512 | } |
| 513 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 514 | MachineOperand &MO = MI->getOperand(i); |
| 515 | if (!MO.isReg()) continue; |
| 516 | unsigned Reg = MO.getReg(); |
| 517 | if (Reg == 0) continue; |
| 518 | if (!MO.isUse()) continue; |
| 519 | |
Chris Lattner | 2a38688 | 2009-07-29 21:36:49 +0000 | [diff] [blame] | 520 | const TargetRegisterClass *NewRC = 0; |
| 521 | if (i < MI->getDesc().getNumOperands()) |
| 522 | NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 523 | |
| 524 | // For now, only allow the register to be changed if its register |
| 525 | // class is consistent across all uses. |
| 526 | if (!Classes[Reg] && NewRC) |
| 527 | Classes[Reg] = NewRC; |
| 528 | else if (!NewRC || Classes[Reg] != NewRC) |
| 529 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 530 | |
| 531 | RegRefs.insert(std::make_pair(Reg, &MO)); |
| 532 | |
| 533 | // It wasn't previously live but now it is, this is a kill. |
| 534 | if (KillIndices[Reg] == ~0u) { |
| 535 | KillIndices[Reg] = Count; |
| 536 | DefIndices[Reg] = ~0u; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 537 | assert(((KillIndices[Reg] == ~0u) != |
| 538 | (DefIndices[Reg] == ~0u)) && |
| 539 | "Kill and Def maps aren't consistent for Reg!"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 540 | } |
| 541 | // Repeat, for all aliases. |
| 542 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 543 | unsigned AliasReg = *Alias; |
| 544 | if (KillIndices[AliasReg] == ~0u) { |
| 545 | KillIndices[AliasReg] = Count; |
| 546 | DefIndices[AliasReg] = ~0u; |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 552 | unsigned |
| 553 | SchedulePostRATDList::findSuitableFreeRegister(unsigned AntiDepReg, |
| 554 | unsigned LastNewReg, |
| 555 | const TargetRegisterClass *RC) { |
| 556 | for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF), |
| 557 | RE = RC->allocation_order_end(MF); R != RE; ++R) { |
| 558 | unsigned NewReg = *R; |
| 559 | // Don't replace a register with itself. |
| 560 | if (NewReg == AntiDepReg) continue; |
| 561 | // Don't replace a register with one that was recently used to repair |
| 562 | // an anti-dependence with this AntiDepReg, because that would |
| 563 | // re-introduce that anti-dependence. |
| 564 | if (NewReg == LastNewReg) continue; |
| 565 | // If NewReg is dead and NewReg's most recent def is not before |
| 566 | // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg. |
| 567 | assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u)) && |
| 568 | "Kill and Def maps aren't consistent for AntiDepReg!"); |
| 569 | assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u)) && |
| 570 | "Kill and Def maps aren't consistent for NewReg!"); |
Dan Gohman | da27757 | 2009-08-12 01:44:20 +0000 | [diff] [blame] | 571 | if (KillIndices[NewReg] != ~0u || |
| 572 | Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) || |
| 573 | KillIndices[AntiDepReg] > DefIndices[NewReg]) |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 574 | continue; |
| 575 | return NewReg; |
| 576 | } |
| 577 | |
| 578 | // No registers are free and available! |
| 579 | return 0; |
| 580 | } |
| 581 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 582 | /// BreakAntiDependencies - Identifiy anti-dependencies along the critical path |
| 583 | /// of the ScheduleDAG and break them by renaming registers. |
| 584 | /// |
| 585 | bool SchedulePostRATDList::BreakAntiDependencies() { |
| 586 | // The code below assumes that there is at least one instruction, |
| 587 | // so just duck out immediately if the block is empty. |
| 588 | if (SUnits.empty()) return false; |
| 589 | |
| 590 | // Find the node at the bottom of the critical path. |
| 591 | SUnit *Max = 0; |
| 592 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 593 | SUnit *SU = &SUnits[i]; |
| 594 | if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency) |
| 595 | Max = SU; |
| 596 | } |
| 597 | |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 598 | DEBUG(errs() << "Critical path has total latency " |
| 599 | << (Max->getDepth() + Max->Latency) << "\n"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 600 | |
| 601 | // Track progress along the critical path through the SUnit graph as we walk |
| 602 | // the instructions. |
| 603 | SUnit *CriticalPathSU = Max; |
| 604 | MachineInstr *CriticalPathMI = CriticalPathSU->getInstr(); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 605 | |
| 606 | // Consider this pattern: |
| 607 | // A = ... |
| 608 | // ... = A |
| 609 | // A = ... |
| 610 | // ... = A |
| 611 | // A = ... |
| 612 | // ... = A |
| 613 | // A = ... |
| 614 | // ... = A |
| 615 | // There are three anti-dependencies here, and without special care, |
| 616 | // we'd break all of them using the same register: |
| 617 | // A = ... |
| 618 | // ... = A |
| 619 | // B = ... |
| 620 | // ... = B |
| 621 | // B = ... |
| 622 | // ... = B |
| 623 | // B = ... |
| 624 | // ... = B |
| 625 | // because at each anti-dependence, B is the first register that |
| 626 | // isn't A which is free. This re-introduces anti-dependencies |
| 627 | // at all but one of the original anti-dependencies that we were |
| 628 | // trying to break. To avoid this, keep track of the most recent |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 629 | // register that each register was replaced with, avoid |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 630 | // using it to repair an anti-dependence on the same register. |
| 631 | // This lets us produce this: |
| 632 | // A = ... |
| 633 | // ... = A |
| 634 | // B = ... |
| 635 | // ... = B |
| 636 | // C = ... |
| 637 | // ... = C |
| 638 | // B = ... |
| 639 | // ... = B |
| 640 | // This still has an anti-dependence on B, but at least it isn't on the |
| 641 | // original critical path. |
| 642 | // |
| 643 | // TODO: If we tracked more than one register here, we could potentially |
| 644 | // fix that remaining critical edge too. This is a little more involved, |
| 645 | // because unlike the most recent register, less recent registers should |
| 646 | // still be considered, though only if no other registers are available. |
| 647 | unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {}; |
| 648 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 649 | // Attempt to break anti-dependence edges on the critical path. Walk the |
| 650 | // instructions from the bottom up, tracking information about liveness |
| 651 | // as we go to help determine which registers are available. |
| 652 | bool Changed = false; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 653 | unsigned Count = InsertPosIndex - 1; |
| 654 | for (MachineBasicBlock::iterator I = InsertPos, E = Begin; |
Dan Gohman | 43f07fb | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 655 | I != E; --Count) { |
| 656 | MachineInstr *MI = --I; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 657 | |
Jakob Stoklund Olesen | 544df36 | 2009-09-28 20:32:46 +0000 | [diff] [blame] | 658 | // After regalloc, KILL instructions aren't safe to treat as |
| 659 | // dependence-breaking. In the case of an INSERT_SUBREG, the KILL |
Dan Gohman | 490b183 | 2008-12-05 05:30:02 +0000 | [diff] [blame] | 660 | // is left behind appearing to clobber the super-register, while the |
| 661 | // subregister needs to remain live. So we just ignore them. |
Jakob Stoklund Olesen | 544df36 | 2009-09-28 20:32:46 +0000 | [diff] [blame] | 662 | if (MI->getOpcode() == TargetInstrInfo::KILL) |
Dan Gohman | 490b183 | 2008-12-05 05:30:02 +0000 | [diff] [blame] | 663 | continue; |
| 664 | |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 665 | // Check if this instruction has a dependence on the critical path that |
| 666 | // is an anti-dependence that we may be able to break. If it is, set |
| 667 | // AntiDepReg to the non-zero register associated with the anti-dependence. |
| 668 | // |
| 669 | // We limit our attention to the critical path as a heuristic to avoid |
| 670 | // breaking anti-dependence edges that aren't going to significantly |
| 671 | // impact the overall schedule. There are a limited number of registers |
| 672 | // and we want to save them for the important edges. |
| 673 | // |
| 674 | // TODO: Instructions with multiple defs could have multiple |
| 675 | // anti-dependencies. The current code here only knows how to break one |
| 676 | // edge per instruction. Note that we'd have to be able to break all of |
| 677 | // the anti-dependencies in an instruction in order to be effective. |
| 678 | unsigned AntiDepReg = 0; |
| 679 | if (MI == CriticalPathMI) { |
| 680 | if (SDep *Edge = CriticalPathStep(CriticalPathSU)) { |
| 681 | SUnit *NextSU = Edge->getSUnit(); |
| 682 | |
| 683 | // Only consider anti-dependence edges. |
| 684 | if (Edge->getKind() == SDep::Anti) { |
| 685 | AntiDepReg = Edge->getReg(); |
| 686 | assert(AntiDepReg != 0 && "Anti-dependence on reg0?"); |
| 687 | // Don't break anti-dependencies on non-allocatable registers. |
Dan Gohman | 49bb50e | 2009-01-16 21:57:43 +0000 | [diff] [blame] | 688 | if (!AllocatableSet.test(AntiDepReg)) |
| 689 | AntiDepReg = 0; |
| 690 | else { |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 691 | // If the SUnit has other dependencies on the SUnit that it |
| 692 | // anti-depends on, don't bother breaking the anti-dependency |
| 693 | // since those edges would prevent such units from being |
| 694 | // scheduled past each other regardless. |
| 695 | // |
| 696 | // Also, if there are dependencies on other SUnits with the |
| 697 | // same register as the anti-dependency, don't attempt to |
| 698 | // break it. |
| 699 | for (SUnit::pred_iterator P = CriticalPathSU->Preds.begin(), |
| 700 | PE = CriticalPathSU->Preds.end(); P != PE; ++P) |
| 701 | if (P->getSUnit() == NextSU ? |
| 702 | (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) : |
| 703 | (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) { |
| 704 | AntiDepReg = 0; |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | CriticalPathSU = NextSU; |
| 710 | CriticalPathMI = CriticalPathSU->getInstr(); |
| 711 | } else { |
| 712 | // We've reached the end of the critical path. |
| 713 | CriticalPathSU = 0; |
| 714 | CriticalPathMI = 0; |
| 715 | } |
| 716 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 717 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 718 | PrescanInstruction(MI); |
| 719 | |
| 720 | // If this instruction has a use of AntiDepReg, breaking it |
| 721 | // is invalid. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 722 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 723 | MachineOperand &MO = MI->getOperand(i); |
| 724 | if (!MO.isReg()) continue; |
| 725 | unsigned Reg = MO.getReg(); |
| 726 | if (Reg == 0) continue; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 727 | if (MO.isUse() && AntiDepReg == Reg) { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 728 | AntiDepReg = 0; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 729 | break; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 730 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | // Determine AntiDepReg's register class, if it is live and is |
| 734 | // consistently used within a single class. |
| 735 | const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0; |
Nick Lewycky | a89d102 | 2008-11-27 17:29:52 +0000 | [diff] [blame] | 736 | assert((AntiDepReg == 0 || RC != NULL) && |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 737 | "Register should be live if it's causing an anti-dependence!"); |
| 738 | if (RC == reinterpret_cast<TargetRegisterClass *>(-1)) |
| 739 | AntiDepReg = 0; |
| 740 | |
| 741 | // Look for a suitable register to use to break the anti-depenence. |
| 742 | // |
| 743 | // TODO: Instead of picking the first free register, consider which might |
| 744 | // be the best. |
| 745 | if (AntiDepReg != 0) { |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 746 | if (unsigned NewReg = findSuitableFreeRegister(AntiDepReg, |
| 747 | LastNewReg[AntiDepReg], |
| 748 | RC)) { |
| 749 | DEBUG(errs() << "Breaking anti-dependence edge on " |
| 750 | << TRI->getName(AntiDepReg) |
| 751 | << " with " << RegRefs.count(AntiDepReg) << " references" |
| 752 | << " using " << TRI->getName(NewReg) << "!\n"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 753 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 754 | // Update the references to the old register to refer to the new |
| 755 | // register. |
| 756 | std::pair<std::multimap<unsigned, MachineOperand *>::iterator, |
| 757 | std::multimap<unsigned, MachineOperand *>::iterator> |
| 758 | Range = RegRefs.equal_range(AntiDepReg); |
| 759 | for (std::multimap<unsigned, MachineOperand *>::iterator |
| 760 | Q = Range.first, QE = Range.second; Q != QE; ++Q) |
| 761 | Q->second->setReg(NewReg); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 762 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 763 | // We just went back in time and modified history; the |
| 764 | // liveness information for the anti-depenence reg is now |
| 765 | // inconsistent. Set the state as if it were dead. |
| 766 | Classes[NewReg] = Classes[AntiDepReg]; |
| 767 | DefIndices[NewReg] = DefIndices[AntiDepReg]; |
| 768 | KillIndices[NewReg] = KillIndices[AntiDepReg]; |
| 769 | assert(((KillIndices[NewReg] == ~0u) != |
| 770 | (DefIndices[NewReg] == ~0u)) && |
| 771 | "Kill and Def maps aren't consistent for NewReg!"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 772 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 773 | Classes[AntiDepReg] = 0; |
| 774 | DefIndices[AntiDepReg] = KillIndices[AntiDepReg]; |
| 775 | KillIndices[AntiDepReg] = ~0u; |
| 776 | assert(((KillIndices[AntiDepReg] == ~0u) != |
| 777 | (DefIndices[AntiDepReg] == ~0u)) && |
| 778 | "Kill and Def maps aren't consistent for AntiDepReg!"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 779 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 780 | RegRefs.erase(AntiDepReg); |
| 781 | Changed = true; |
| 782 | LastNewReg[AntiDepReg] = NewReg; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 786 | ScanInstruction(MI, Count); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 787 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 788 | |
| 789 | return Changed; |
| 790 | } |
| 791 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 792 | /// StartBlockForKills - Initialize register live-range state for updating kills |
| 793 | /// |
| 794 | void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) { |
| 795 | // Initialize the indices to indicate that no registers are live. |
| 796 | std::fill(KillIndices, array_endof(KillIndices), ~0u); |
| 797 | |
| 798 | // Determine the live-out physregs for this block. |
| 799 | if (!BB->empty() && BB->back().getDesc().isReturn()) { |
| 800 | // In a return block, examine the function live-out regs. |
| 801 | for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), |
| 802 | E = MRI.liveout_end(); I != E; ++I) { |
| 803 | unsigned Reg = *I; |
| 804 | KillIndices[Reg] = BB->size(); |
| 805 | // Repeat, for all subregs. |
| 806 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 807 | *Subreg; ++Subreg) { |
| 808 | KillIndices[*Subreg] = BB->size(); |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | else { |
| 813 | // In a non-return block, examine the live-in regs of all successors. |
| 814 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 815 | SE = BB->succ_end(); SI != SE; ++SI) { |
| 816 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 817 | E = (*SI)->livein_end(); I != E; ++I) { |
| 818 | unsigned Reg = *I; |
| 819 | KillIndices[Reg] = BB->size(); |
| 820 | // Repeat, for all subregs. |
| 821 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 822 | *Subreg; ++Subreg) { |
| 823 | KillIndices[*Subreg] = BB->size(); |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 830 | bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI, |
| 831 | MachineOperand &MO) { |
| 832 | // Setting kill flag... |
| 833 | if (!MO.isKill()) { |
| 834 | MO.setIsKill(true); |
| 835 | return false; |
| 836 | } |
| 837 | |
| 838 | // If MO itself is live, clear the kill flag... |
| 839 | if (KillIndices[MO.getReg()] != ~0u) { |
| 840 | MO.setIsKill(false); |
| 841 | return false; |
| 842 | } |
| 843 | |
| 844 | // If any subreg of MO is live, then create an imp-def for that |
| 845 | // subreg and keep MO marked as killed. |
| 846 | bool AllDead = true; |
| 847 | const unsigned SuperReg = MO.getReg(); |
| 848 | for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg); |
| 849 | *Subreg; ++Subreg) { |
| 850 | if (KillIndices[*Subreg] != ~0u) { |
| 851 | MI->addOperand(MachineOperand::CreateReg(*Subreg, |
| 852 | true /*IsDef*/, |
| 853 | true /*IsImp*/, |
| 854 | false /*IsKill*/, |
| 855 | false /*IsDead*/)); |
| 856 | AllDead = false; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | MO.setIsKill(AllDead); |
| 861 | return false; |
| 862 | } |
| 863 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 864 | /// FixupKills - Fix the register kill flags, they may have been made |
| 865 | /// incorrect by instruction reordering. |
| 866 | /// |
| 867 | void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) { |
| 868 | DEBUG(errs() << "Fixup kills for BB ID#" << MBB->getNumber() << '\n'); |
| 869 | |
| 870 | std::set<unsigned> killedRegs; |
| 871 | BitVector ReservedRegs = TRI->getReservedRegs(MF); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 872 | |
| 873 | StartBlockForKills(MBB); |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 874 | |
| 875 | // Examine block from end to start... |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 876 | unsigned Count = MBB->size(); |
| 877 | for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin(); |
| 878 | I != E; --Count) { |
| 879 | MachineInstr *MI = --I; |
| 880 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 881 | // Update liveness. Registers that are defed but not used in this |
| 882 | // instruction are now dead. Mark register and all subregs as they |
| 883 | // are completely defined. |
| 884 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 885 | MachineOperand &MO = MI->getOperand(i); |
| 886 | if (!MO.isReg()) continue; |
| 887 | unsigned Reg = MO.getReg(); |
| 888 | if (Reg == 0) continue; |
| 889 | if (!MO.isDef()) continue; |
| 890 | // Ignore two-addr defs. |
| 891 | if (MI->isRegTiedToUseOperand(i)) continue; |
| 892 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 893 | KillIndices[Reg] = ~0u; |
| 894 | |
| 895 | // Repeat for all subregs. |
| 896 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 897 | *Subreg; ++Subreg) { |
| 898 | KillIndices[*Subreg] = ~0u; |
| 899 | } |
| 900 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 901 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 902 | // Examine all used registers and set/clear kill flag. When a |
| 903 | // register is used multiple times we only set the kill flag on |
| 904 | // the first use. |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 905 | killedRegs.clear(); |
| 906 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 907 | MachineOperand &MO = MI->getOperand(i); |
| 908 | if (!MO.isReg() || !MO.isUse()) continue; |
| 909 | unsigned Reg = MO.getReg(); |
| 910 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 911 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 912 | bool kill = false; |
| 913 | if (killedRegs.find(Reg) == killedRegs.end()) { |
| 914 | kill = true; |
| 915 | // A register is not killed if any subregs are live... |
| 916 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 917 | *Subreg; ++Subreg) { |
| 918 | if (KillIndices[*Subreg] != ~0u) { |
| 919 | kill = false; |
| 920 | break; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | // If subreg is not live, then register is killed if it became |
| 925 | // live in this instruction |
| 926 | if (kill) |
| 927 | kill = (KillIndices[Reg] == ~0u); |
| 928 | } |
| 929 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 930 | if (MO.isKill() != kill) { |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 931 | bool removed = ToggleKillFlag(MI, MO); |
| 932 | if (removed) { |
| 933 | DEBUG(errs() << "Fixed <removed> in "); |
| 934 | } else { |
| 935 | DEBUG(errs() << "Fixed " << MO << " in "); |
| 936 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 937 | DEBUG(MI->dump()); |
| 938 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 939 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 940 | killedRegs.insert(Reg); |
| 941 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 942 | |
David Goodwin | a3251db | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 943 | // Mark any used register (that is not using undef) and subregs as |
| 944 | // now live... |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 945 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 946 | MachineOperand &MO = MI->getOperand(i); |
David Goodwin | a3251db | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 947 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 948 | unsigned Reg = MO.getReg(); |
| 949 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 950 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 951 | KillIndices[Reg] = Count; |
| 952 | |
| 953 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 954 | *Subreg; ++Subreg) { |
| 955 | KillIndices[*Subreg] = Count; |
| 956 | } |
| 957 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 961 | //===----------------------------------------------------------------------===// |
| 962 | // Top-Down Scheduling |
| 963 | //===----------------------------------------------------------------------===// |
| 964 | |
| 965 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
| 966 | /// the PendingQueue if the count reaches zero. Also update its cycle bound. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 967 | void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { |
| 968 | SUnit *SuccSU = SuccEdge->getSUnit(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 969 | --SuccSU->NumPredsLeft; |
| 970 | |
| 971 | #ifndef NDEBUG |
| 972 | if (SuccSU->NumPredsLeft < 0) { |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 973 | errs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 974 | SuccSU->dump(this); |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 975 | errs() << " has been released too many times!\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 976 | llvm_unreachable(0); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 977 | } |
| 978 | #endif |
| 979 | |
| 980 | // Compute how many cycles it will be before this actually becomes |
| 981 | // available. This is the max of the start time of all predecessors plus |
| 982 | // their latencies. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 983 | SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 984 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 985 | // If all the node's predecessors are scheduled, this node is ready |
| 986 | // to be scheduled. Ignore the special ExitSU node. |
| 987 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 988 | PendingQueue.push_back(SuccSU); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. |
| 992 | void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { |
| 993 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 994 | I != E; ++I) |
| 995 | ReleaseSucc(SU, &*I); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 999 | /// count of its successors. If a successor pending count is zero, add it to |
| 1000 | /// the Available queue. |
| 1001 | void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 1002 | DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: "); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1003 | DEBUG(SU->dump(this)); |
| 1004 | |
| 1005 | Sequence.push_back(SU); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1006 | assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!"); |
| 1007 | SU->setDepthToAtLeast(CurCycle); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1008 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1009 | ReleaseSuccessors(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1010 | SU->isScheduled = true; |
| 1011 | AvailableQueue.ScheduledNode(SU); |
| 1012 | } |
| 1013 | |
| 1014 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 1015 | /// schedulers. |
| 1016 | void SchedulePostRATDList::ListScheduleTopDown() { |
| 1017 | unsigned CurCycle = 0; |
| 1018 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1019 | // Release any successors of the special Entry node. |
| 1020 | ReleaseSuccessors(&EntrySU); |
| 1021 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1022 | // All leaves to Available queue. |
| 1023 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 1024 | // It is available if it has no predecessors. |
| 1025 | if (SUnits[i].Preds.empty()) { |
| 1026 | AvailableQueue.push(&SUnits[i]); |
| 1027 | SUnits[i].isAvailable = true; |
| 1028 | } |
| 1029 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1030 | |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1031 | // In any cycle where we can't schedule any instructions, we must |
| 1032 | // stall or emit a noop, depending on the target. |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1033 | bool CycleHasInsts = false; |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1034 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1035 | // While Available queue is not empty, grab the node with the highest |
| 1036 | // priority. If it is not ready put it back. Schedule the node. |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1037 | std::vector<SUnit*> NotReady; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1038 | Sequence.reserve(SUnits.size()); |
| 1039 | while (!AvailableQueue.empty() || !PendingQueue.empty()) { |
| 1040 | // Check to see if any of the pending instructions are ready to issue. If |
| 1041 | // so, add them to the available queue. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1042 | unsigned MinDepth = ~0u; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1043 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1044 | if (PendingQueue[i]->getDepth() <= CurCycle) { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1045 | AvailableQueue.push(PendingQueue[i]); |
| 1046 | PendingQueue[i]->isAvailable = true; |
| 1047 | PendingQueue[i] = PendingQueue.back(); |
| 1048 | PendingQueue.pop_back(); |
| 1049 | --i; --e; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1050 | } else if (PendingQueue[i]->getDepth() < MinDepth) |
| 1051 | MinDepth = PendingQueue[i]->getDepth(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1052 | } |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 1053 | |
David Goodwin | 7cd0118 | 2009-08-11 17:56:42 +0000 | [diff] [blame] | 1054 | DEBUG(errs() << "\n*** Examining Available\n"; |
| 1055 | LatencyPriorityQueue q = AvailableQueue; |
| 1056 | while (!q.empty()) { |
| 1057 | SUnit *su = q.pop(); |
| 1058 | errs() << "Height " << su->getHeight() << ": "; |
| 1059 | su->dump(this); |
| 1060 | }); |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 1061 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1062 | SUnit *FoundSUnit = 0; |
| 1063 | |
| 1064 | bool HasNoopHazards = false; |
| 1065 | while (!AvailableQueue.empty()) { |
| 1066 | SUnit *CurSUnit = AvailableQueue.pop(); |
| 1067 | |
| 1068 | ScheduleHazardRecognizer::HazardType HT = |
| 1069 | HazardRec->getHazardType(CurSUnit); |
| 1070 | if (HT == ScheduleHazardRecognizer::NoHazard) { |
| 1071 | FoundSUnit = CurSUnit; |
| 1072 | break; |
| 1073 | } |
| 1074 | |
| 1075 | // Remember if this is a noop hazard. |
| 1076 | HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; |
| 1077 | |
| 1078 | NotReady.push_back(CurSUnit); |
| 1079 | } |
| 1080 | |
| 1081 | // Add the nodes that aren't ready back onto the available list. |
| 1082 | if (!NotReady.empty()) { |
| 1083 | AvailableQueue.push_all(NotReady); |
| 1084 | NotReady.clear(); |
| 1085 | } |
| 1086 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1087 | // If we found a node to schedule, do it now. |
| 1088 | if (FoundSUnit) { |
| 1089 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1090 | HazardRec->EmitInstruction(FoundSUnit); |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1091 | CycleHasInsts = true; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1092 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 1093 | // If we are using the target-specific hazards, then don't |
| 1094 | // advance the cycle time just because we schedule a node. If |
| 1095 | // the target allows it we can schedule multiple nodes in the |
| 1096 | // same cycle. |
| 1097 | if (!EnablePostRAHazardAvoidance) { |
| 1098 | if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops! |
| 1099 | ++CurCycle; |
| 1100 | } |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1101 | } else { |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1102 | if (CycleHasInsts) { |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1103 | DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n'); |
| 1104 | HazardRec->AdvanceCycle(); |
| 1105 | } else if (!HasNoopHazards) { |
| 1106 | // Otherwise, we have a pipeline stall, but no other problem, |
| 1107 | // just advance the current cycle and try again. |
| 1108 | DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n'); |
| 1109 | HazardRec->AdvanceCycle(); |
| 1110 | ++NumStalls; |
| 1111 | } else { |
| 1112 | // Otherwise, we have no instructions to issue and we have instructions |
| 1113 | // that will fault if we don't do this right. This is the case for |
| 1114 | // processors without pipeline interlocks and other cases. |
| 1115 | DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n'); |
| 1116 | HazardRec->EmitNoop(); |
| 1117 | Sequence.push_back(0); // NULL here means noop |
| 1118 | ++NumNoops; |
| 1119 | } |
| 1120 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1121 | ++CurCycle; |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1122 | CycleHasInsts = false; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | #ifndef NDEBUG |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 1127 | VerifySchedule(/*isBottomUp=*/false); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1128 | #endif |
| 1129 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 1130 | |
| 1131 | //===----------------------------------------------------------------------===// |
| 1132 | // Public Constructor Functions |
| 1133 | //===----------------------------------------------------------------------===// |
| 1134 | |
| 1135 | FunctionPass *llvm::createPostRAScheduler() { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1136 | return new PostRAScheduler(); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 1137 | } |