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