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