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