Dale Johannesen | 72f1596 | 2007-07-13 17:31:29 +0000 | [diff] [blame] | 1 | //===----- SchedulePostRAList.cpp - list scheduler ------------------------===// |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements a top-down list scheduler, using standard algorithms. |
| 11 | // The basic approach uses a priority queue of available nodes to schedule. |
| 12 | // One at a time, nodes are taken from the priority queue (thus in priority |
| 13 | // order), checked for legality to schedule, and emitted if legal. |
| 14 | // |
| 15 | // Nodes may not be legal to schedule either due to structural hazards (e.g. |
| 16 | // pipeline or resource constraints) or because an input to the instruction has |
| 17 | // not completed execution. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #define DEBUG_TYPE "post-RA-sched" |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 22 | #include "ExactHazardRecognizer.h" |
| 23 | #include "SimpleHazardRecognizer.h" |
Dan Gohman | 6dc75fe | 2009-02-06 17:12:10 +0000 | [diff] [blame] | 24 | #include "ScheduleDAGInstrs.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/Passes.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LatencyPriorityQueue.h" |
| 27 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineDominators.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetInstrInfo.h" |
| 36 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 459525d | 2008-01-14 19:00:06 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Compiler.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 42 | #include <map> |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 43 | #include <set> |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 46 | STATISTIC(NumNoops, "Number of noops inserted"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 47 | STATISTIC(NumStalls, "Number of pipeline stalls"); |
| 48 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 49 | static cl::opt<bool> |
| 50 | EnableAntiDepBreaking("break-anti-dependencies", |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 51 | cl::desc("Break post-RA scheduling anti-dependencies"), |
| 52 | cl::init(true), cl::Hidden); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 53 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 54 | static cl::opt<bool> |
| 55 | EnablePostRAHazardAvoidance("avoid-hazards", |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 56 | cl::desc("Enable exact hazard avoidance"), |
| 57 | cl::init(false), cl::Hidden); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 58 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 59 | namespace { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 60 | class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass { |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 61 | public: |
| 62 | static char ID; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 63 | PostRAScheduler() : MachineFunctionPass(&ID) {} |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 64 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 65 | void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 66 | AU.setPreservesCFG(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 67 | AU.addRequired<MachineDominatorTree>(); |
| 68 | AU.addPreserved<MachineDominatorTree>(); |
| 69 | AU.addRequired<MachineLoopInfo>(); |
| 70 | AU.addPreserved<MachineLoopInfo>(); |
| 71 | MachineFunctionPass::getAnalysisUsage(AU); |
| 72 | } |
| 73 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 74 | const char *getPassName() const { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 75 | return "Post RA top-down list latency scheduler"; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | bool runOnMachineFunction(MachineFunction &Fn); |
| 79 | }; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 80 | char PostRAScheduler::ID = 0; |
| 81 | |
| 82 | class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 83 | /// AvailableQueue - The priority queue to use for the available SUnits. |
| 84 | /// |
| 85 | LatencyPriorityQueue AvailableQueue; |
| 86 | |
| 87 | /// PendingQueue - This contains all of the instructions whose operands have |
| 88 | /// been issued, but their results are not ready yet (due to the latency of |
| 89 | /// the operation). Once the operands becomes available, the instruction is |
| 90 | /// added to the AvailableQueue. |
| 91 | std::vector<SUnit*> PendingQueue; |
| 92 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 93 | /// Topo - A topological ordering for SUnits. |
| 94 | ScheduleDAGTopologicalSort Topo; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 95 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 96 | /// AllocatableSet - The set of allocatable registers. |
| 97 | /// We'll be ignoring anti-dependencies on non-allocatable registers, |
| 98 | /// because they may not be safe to break. |
| 99 | const BitVector AllocatableSet; |
| 100 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 101 | /// HazardRec - The hazard recognizer to use. |
| 102 | ScheduleHazardRecognizer *HazardRec; |
| 103 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 104 | /// Classes - For live regs that are only used in one register class in a |
| 105 | /// live range, the register class. If the register is not live, the |
| 106 | /// corresponding value is null. If the register is live but used in |
| 107 | /// multiple register classes, the corresponding value is -1 casted to a |
| 108 | /// pointer. |
| 109 | const TargetRegisterClass * |
| 110 | Classes[TargetRegisterInfo::FirstVirtualRegister]; |
| 111 | |
| 112 | /// RegRegs - Map registers to all their references within a live range. |
| 113 | std::multimap<unsigned, MachineOperand *> RegRefs; |
| 114 | |
| 115 | /// The index of the most recent kill (proceding bottom-up), or ~0u if |
| 116 | /// the register is not live. |
| 117 | unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 118 | |
| 119 | /// The index of the most recent complete def (proceding bottom up), or ~0u |
| 120 | /// if the register is live. |
| 121 | unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 122 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 123 | public: |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 124 | SchedulePostRATDList(MachineFunction &MF, |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 125 | const MachineLoopInfo &MLI, |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 126 | const MachineDominatorTree &MDT, |
| 127 | ScheduleHazardRecognizer *HR) |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 128 | : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits), |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 129 | AllocatableSet(TRI->getAllocatableSet(MF)), |
| 130 | HazardRec(HR) {} |
| 131 | |
| 132 | ~SchedulePostRATDList() { |
| 133 | delete HazardRec; |
| 134 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 135 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 136 | /// StartBlock - Initialize register live-range state for scheduling in |
| 137 | /// this block. |
| 138 | /// |
| 139 | void StartBlock(MachineBasicBlock *BB); |
| 140 | |
| 141 | /// Schedule - Schedule the instruction range using list scheduling. |
| 142 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 143 | void Schedule(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 144 | |
| 145 | /// FixupKills - Fix register kill flags that have been made |
| 146 | /// invalid due to scheduling |
| 147 | /// |
| 148 | void FixupKills(MachineBasicBlock *MBB); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 149 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 150 | /// Observe - Update liveness information to account for the current |
| 151 | /// instruction, which will not be scheduled. |
| 152 | /// |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 153 | void Observe(MachineInstr *MI, unsigned Count); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 154 | |
| 155 | /// FinishBlock - Clean up register live-range state. |
| 156 | /// |
| 157 | void FinishBlock(); |
| 158 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 159 | /// GenerateLivenessForKills - If true then generate Def/Kill |
| 160 | /// information for use in updating register kill. If false then |
| 161 | /// generate Def/Kill information for anti-dependence breaking. |
| 162 | bool GenerateLivenessForKills; |
| 163 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 164 | private: |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 165 | void PrescanInstruction(MachineInstr *MI); |
| 166 | void ScanInstruction(MachineInstr *MI, unsigned Count); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 167 | void ReleaseSucc(SUnit *SU, SDep *SuccEdge); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 168 | void ReleaseSuccessors(SUnit *SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 169 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
| 170 | void ListScheduleTopDown(); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 171 | bool BreakAntiDependencies(); |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 172 | unsigned findSuitableFreeRegister(unsigned AntiDepReg, |
| 173 | unsigned LastNewReg, |
| 174 | const TargetRegisterClass *); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 175 | }; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 178 | /// isSchedulingBoundary - Test if the given instruction should be |
| 179 | /// considered a scheduling boundary. This primarily includes labels |
| 180 | /// and terminators. |
| 181 | /// |
| 182 | static bool isSchedulingBoundary(const MachineInstr *MI, |
| 183 | const MachineFunction &MF) { |
| 184 | // Terminators and labels can't be scheduled around. |
| 185 | if (MI->getDesc().isTerminator() || MI->isLabel()) |
| 186 | return true; |
| 187 | |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 188 | // Don't attempt to schedule around any instruction that modifies |
| 189 | // a stack-oriented pointer, as it's unlikely to be profitable. This |
| 190 | // saves compile time, because it doesn't require every single |
| 191 | // stack slot reference to depend on the instruction that does the |
| 192 | // modification. |
| 193 | const TargetLowering &TLI = *MF.getTarget().getTargetLowering(); |
| 194 | if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore())) |
| 195 | return true; |
| 196 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 200 | bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 201 | DEBUG(errs() << "PostRAScheduler\n"); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 202 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 203 | const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 204 | const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 205 | const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData(); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 206 | ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ? |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 207 | (ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) : |
| 208 | (ScheduleHazardRecognizer *)new SimpleHazardRecognizer(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 209 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 210 | SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 211 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 212 | // Loop over all of the basic blocks |
| 213 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 214 | MBB != MBBe; ++MBB) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 215 | // Initialize register live-range state for scheduling in this block. |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 216 | Scheduler.GenerateLivenessForKills = false; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 217 | Scheduler.StartBlock(MBB); |
| 218 | |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 219 | // Schedule each sequence of instructions not interrupted by a label |
| 220 | // or anything else that effectively needs to shut down scheduling. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 221 | MachineBasicBlock::iterator Current = MBB->end(); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 222 | unsigned Count = MBB->size(), CurrentCount = Count; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 223 | for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) { |
| 224 | MachineInstr *MI = prior(I); |
| 225 | if (isSchedulingBoundary(MI, Fn)) { |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 226 | Scheduler.Run(MBB, I, Current, CurrentCount); |
| 227 | Scheduler.EmitSchedule(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 228 | Current = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 229 | CurrentCount = Count - 1; |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 230 | Scheduler.Observe(MI, CurrentCount); |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 231 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 232 | I = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 233 | --Count; |
Dan Gohman | 43f07fb | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 234 | } |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 235 | assert(Count == 0 && "Instruction count mismatch!"); |
Duncan Sands | 9e8bd0b | 2009-03-11 09:04:34 +0000 | [diff] [blame] | 236 | assert((MBB->begin() == Current || CurrentCount != 0) && |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 237 | "Instruction count mismatch!"); |
| 238 | Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 239 | Scheduler.EmitSchedule(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 240 | |
| 241 | // Clean up register live-range state. |
| 242 | Scheduler.FinishBlock(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 243 | |
| 244 | // Initialize register live-range state again and update register kills |
| 245 | Scheduler.GenerateLivenessForKills = true; |
| 246 | Scheduler.StartBlock(MBB); |
| 247 | Scheduler.FixupKills(MBB); |
| 248 | Scheduler.FinishBlock(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 249 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 254 | /// StartBlock - Initialize register live-range state for scheduling in |
| 255 | /// this block. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 256 | /// |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 257 | void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) { |
| 258 | // Call the superclass. |
| 259 | ScheduleDAGInstrs::StartBlock(BB); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 260 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 261 | // Reset the hazard recognizer. |
| 262 | HazardRec->Reset(); |
| 263 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 264 | // Clear out the register class data. |
| 265 | std::fill(Classes, array_endof(Classes), |
| 266 | static_cast<const TargetRegisterClass *>(0)); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 267 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 268 | // Initialize the indices to indicate that no registers are live. |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 269 | std::fill(KillIndices, array_endof(KillIndices), ~0u); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 270 | std::fill(DefIndices, array_endof(DefIndices), BB->size()); |
| 271 | |
| 272 | // Determine the live-out physregs for this block. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 273 | if (!BB->empty() && BB->back().getDesc().isReturn()) |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 274 | // In a return block, examine the function live-out regs. |
| 275 | for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), |
| 276 | E = MRI.liveout_end(); I != E; ++I) { |
| 277 | unsigned Reg = *I; |
| 278 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 279 | KillIndices[Reg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 280 | DefIndices[Reg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 281 | // Repeat, for all aliases. |
| 282 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 283 | unsigned AliasReg = *Alias; |
| 284 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 285 | KillIndices[AliasReg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 286 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | else |
| 290 | // In a non-return block, examine the live-in regs of all successors. |
| 291 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 292 | SE = BB->succ_end(); SI != SE; ++SI) |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 293 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 294 | E = (*SI)->livein_end(); I != E; ++I) { |
| 295 | unsigned Reg = *I; |
| 296 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 297 | KillIndices[Reg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 298 | DefIndices[Reg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 299 | // Repeat, for all aliases. |
| 300 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 301 | unsigned AliasReg = *Alias; |
| 302 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 303 | KillIndices[AliasReg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 304 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 308 | if (!GenerateLivenessForKills) { |
| 309 | // Consider callee-saved registers as live-out, since we're running after |
| 310 | // prologue/epilogue insertion so there's no way to add additional |
| 311 | // saved registers. |
| 312 | // |
| 313 | // TODO: If the callee saves and restores these, then we can potentially |
| 314 | // use them between the save and the restore. To do that, we could scan |
| 315 | // the exit blocks to see which of these registers are defined. |
| 316 | // Alternatively, callee-saved registers that aren't saved and restored |
| 317 | // could be marked live-in in every block. |
| 318 | for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) { |
| 319 | unsigned Reg = *I; |
| 320 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 321 | KillIndices[Reg] = BB->size(); |
| 322 | DefIndices[Reg] = ~0u; |
| 323 | // Repeat, for all aliases. |
| 324 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 325 | unsigned AliasReg = *Alias; |
| 326 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 327 | KillIndices[AliasReg] = BB->size(); |
| 328 | DefIndices[AliasReg] = ~0u; |
| 329 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /// Schedule - Schedule the instruction range using list scheduling. |
| 335 | /// |
| 336 | void SchedulePostRATDList::Schedule() { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 337 | DEBUG(errs() << "********** List Scheduling **********\n"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 338 | |
| 339 | // Build the scheduling graph. |
| 340 | BuildSchedGraph(); |
| 341 | |
| 342 | if (EnableAntiDepBreaking) { |
| 343 | if (BreakAntiDependencies()) { |
| 344 | // We made changes. Update the dependency graph. |
| 345 | // Theoretically we could update the graph in place: |
| 346 | // When a live range is changed to use a different register, remove |
| 347 | // the def's anti-dependence *and* output-dependence edges due to |
| 348 | // that register, and add new anti-dependence and output-dependence |
| 349 | // edges based on the next live range of the register. |
| 350 | SUnits.clear(); |
| 351 | EntrySU = SUnit(); |
| 352 | ExitSU = SUnit(); |
| 353 | BuildSchedGraph(); |
| 354 | } |
| 355 | } |
| 356 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 357 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 358 | SUnits[su].dumpAll(this)); |
| 359 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 360 | AvailableQueue.initNodes(SUnits); |
| 361 | |
| 362 | ListScheduleTopDown(); |
| 363 | |
| 364 | AvailableQueue.releaseState(); |
| 365 | } |
| 366 | |
| 367 | /// Observe - Update liveness information to account for the current |
| 368 | /// instruction, which will not be scheduled. |
| 369 | /// |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 370 | void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) { |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 371 | assert(Count < InsertPosIndex && "Instruction index out of expected range!"); |
| 372 | |
| 373 | // Any register which was defined within the previous scheduling region |
| 374 | // may have been rescheduled and its lifetime may overlap with registers |
| 375 | // in ways not reflected in our current liveness state. For each such |
| 376 | // register, adjust the liveness state to be conservatively correct. |
| 377 | for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg) |
| 378 | if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) { |
| 379 | assert(KillIndices[Reg] == ~0u && "Clobbered register is live!"); |
| 380 | // Mark this register to be non-renamable. |
| 381 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 382 | // Move the def index to the end of the previous region, to reflect |
| 383 | // that the def could theoretically have been scheduled at the end. |
| 384 | DefIndices[Reg] = InsertPosIndex; |
| 385 | } |
| 386 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 387 | PrescanInstruction(MI); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 388 | ScanInstruction(MI, Count); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /// FinishBlock - Clean up register live-range state. |
| 392 | /// |
| 393 | void SchedulePostRATDList::FinishBlock() { |
| 394 | RegRefs.clear(); |
| 395 | |
| 396 | // Call the superclass. |
| 397 | ScheduleDAGInstrs::FinishBlock(); |
| 398 | } |
| 399 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 400 | /// CriticalPathStep - Return the next SUnit after SU on the bottom-up |
| 401 | /// critical path. |
| 402 | static SDep *CriticalPathStep(SUnit *SU) { |
| 403 | SDep *Next = 0; |
| 404 | unsigned NextDepth = 0; |
| 405 | // Find the predecessor edge with the greatest depth. |
| 406 | for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
| 407 | P != PE; ++P) { |
| 408 | SUnit *PredSU = P->getSUnit(); |
| 409 | unsigned PredLatency = P->getLatency(); |
| 410 | unsigned PredTotalLatency = PredSU->getDepth() + PredLatency; |
| 411 | // In the case of a latency tie, prefer an anti-dependency edge over |
| 412 | // other types of edges. |
| 413 | if (NextDepth < PredTotalLatency || |
| 414 | (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) { |
| 415 | NextDepth = PredTotalLatency; |
| 416 | Next = &*P; |
| 417 | } |
| 418 | } |
| 419 | return Next; |
| 420 | } |
| 421 | |
| 422 | void SchedulePostRATDList::PrescanInstruction(MachineInstr *MI) { |
| 423 | // Scan the register operands for this instruction and update |
| 424 | // Classes and RegRefs. |
| 425 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 426 | MachineOperand &MO = MI->getOperand(i); |
| 427 | if (!MO.isReg()) continue; |
| 428 | unsigned Reg = MO.getReg(); |
| 429 | if (Reg == 0) continue; |
Chris Lattner | 2a38688 | 2009-07-29 21:36:49 +0000 | [diff] [blame] | 430 | const TargetRegisterClass *NewRC = 0; |
| 431 | |
| 432 | if (i < MI->getDesc().getNumOperands()) |
| 433 | NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 434 | |
| 435 | // For now, only allow the register to be changed if its register |
| 436 | // class is consistent across all uses. |
| 437 | if (!Classes[Reg] && NewRC) |
| 438 | Classes[Reg] = NewRC; |
| 439 | else if (!NewRC || Classes[Reg] != NewRC) |
| 440 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 441 | |
| 442 | // Now check for aliases. |
| 443 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 444 | // If an alias of the reg is used during the live range, give up. |
| 445 | // Note that this allows us to skip checking if AntiDepReg |
| 446 | // overlaps with any of the aliases, among other things. |
| 447 | unsigned AliasReg = *Alias; |
| 448 | if (Classes[AliasReg]) { |
| 449 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 450 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // If we're still willing to consider this register, note the reference. |
| 455 | if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1)) |
| 456 | RegRefs.insert(std::make_pair(Reg, &MO)); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void SchedulePostRATDList::ScanInstruction(MachineInstr *MI, |
| 461 | unsigned Count) { |
| 462 | // Update liveness. |
| 463 | // Proceding upwards, registers that are defed but not used in this |
| 464 | // instruction are now dead. |
| 465 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 466 | MachineOperand &MO = MI->getOperand(i); |
| 467 | if (!MO.isReg()) continue; |
| 468 | unsigned Reg = MO.getReg(); |
| 469 | if (Reg == 0) continue; |
| 470 | if (!MO.isDef()) continue; |
| 471 | // Ignore two-addr defs. |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 472 | if (MI->isRegTiedToUseOperand(i)) continue; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 473 | |
| 474 | DefIndices[Reg] = Count; |
| 475 | KillIndices[Reg] = ~0u; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 476 | assert(((KillIndices[Reg] == ~0u) != |
| 477 | (DefIndices[Reg] == ~0u)) && |
| 478 | "Kill and Def maps aren't consistent for Reg!"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 479 | Classes[Reg] = 0; |
| 480 | RegRefs.erase(Reg); |
| 481 | // Repeat, for all subregs. |
| 482 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 483 | *Subreg; ++Subreg) { |
| 484 | unsigned SubregReg = *Subreg; |
| 485 | DefIndices[SubregReg] = Count; |
| 486 | KillIndices[SubregReg] = ~0u; |
| 487 | Classes[SubregReg] = 0; |
| 488 | RegRefs.erase(SubregReg); |
| 489 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 490 | // Conservatively mark super-registers as unusable. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 491 | for (const unsigned *Super = TRI->getSuperRegisters(Reg); |
| 492 | *Super; ++Super) { |
| 493 | unsigned SuperReg = *Super; |
| 494 | Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 495 | } |
| 496 | } |
| 497 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 498 | MachineOperand &MO = MI->getOperand(i); |
| 499 | if (!MO.isReg()) continue; |
| 500 | unsigned Reg = MO.getReg(); |
| 501 | if (Reg == 0) continue; |
| 502 | if (!MO.isUse()) continue; |
| 503 | |
Chris Lattner | 2a38688 | 2009-07-29 21:36:49 +0000 | [diff] [blame] | 504 | const TargetRegisterClass *NewRC = 0; |
| 505 | if (i < MI->getDesc().getNumOperands()) |
| 506 | NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 507 | |
| 508 | // For now, only allow the register to be changed if its register |
| 509 | // class is consistent across all uses. |
| 510 | if (!Classes[Reg] && NewRC) |
| 511 | Classes[Reg] = NewRC; |
| 512 | else if (!NewRC || Classes[Reg] != NewRC) |
| 513 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 514 | |
| 515 | RegRefs.insert(std::make_pair(Reg, &MO)); |
| 516 | |
| 517 | // It wasn't previously live but now it is, this is a kill. |
| 518 | if (KillIndices[Reg] == ~0u) { |
| 519 | KillIndices[Reg] = Count; |
| 520 | DefIndices[Reg] = ~0u; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 521 | assert(((KillIndices[Reg] == ~0u) != |
| 522 | (DefIndices[Reg] == ~0u)) && |
| 523 | "Kill and Def maps aren't consistent for Reg!"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 524 | } |
| 525 | // Repeat, for all aliases. |
| 526 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 527 | unsigned AliasReg = *Alias; |
| 528 | if (KillIndices[AliasReg] == ~0u) { |
| 529 | KillIndices[AliasReg] = Count; |
| 530 | DefIndices[AliasReg] = ~0u; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 536 | unsigned |
| 537 | SchedulePostRATDList::findSuitableFreeRegister(unsigned AntiDepReg, |
| 538 | unsigned LastNewReg, |
| 539 | const TargetRegisterClass *RC) { |
| 540 | for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF), |
| 541 | RE = RC->allocation_order_end(MF); R != RE; ++R) { |
| 542 | unsigned NewReg = *R; |
| 543 | // Don't replace a register with itself. |
| 544 | if (NewReg == AntiDepReg) continue; |
| 545 | // Don't replace a register with one that was recently used to repair |
| 546 | // an anti-dependence with this AntiDepReg, because that would |
| 547 | // re-introduce that anti-dependence. |
| 548 | if (NewReg == LastNewReg) continue; |
| 549 | // If NewReg is dead and NewReg's most recent def is not before |
| 550 | // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg. |
| 551 | assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u)) && |
| 552 | "Kill and Def maps aren't consistent for AntiDepReg!"); |
| 553 | assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u)) && |
| 554 | "Kill and Def maps aren't consistent for NewReg!"); |
Dan Gohman | da27757 | 2009-08-12 01:44:20 +0000 | [diff] [blame] | 555 | if (KillIndices[NewReg] != ~0u || |
| 556 | Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) || |
| 557 | KillIndices[AntiDepReg] > DefIndices[NewReg]) |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 558 | continue; |
| 559 | return NewReg; |
| 560 | } |
| 561 | |
| 562 | // No registers are free and available! |
| 563 | return 0; |
| 564 | } |
| 565 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 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 | |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 582 | DEBUG(errs() << "Critical path has total latency " |
| 583 | << (Max->getDepth() + Max->Latency) << "\n"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 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 |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 613 | // register that each register was replaced with, avoid |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 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 | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 730 | if (unsigned NewReg = findSuitableFreeRegister(AntiDepReg, |
| 731 | LastNewReg[AntiDepReg], |
| 732 | RC)) { |
| 733 | DEBUG(errs() << "Breaking anti-dependence edge on " |
| 734 | << TRI->getName(AntiDepReg) |
| 735 | << " with " << RegRefs.count(AntiDepReg) << " references" |
| 736 | << " using " << TRI->getName(NewReg) << "!\n"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 737 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 738 | // Update the references to the old register to refer to the new |
| 739 | // register. |
| 740 | std::pair<std::multimap<unsigned, MachineOperand *>::iterator, |
| 741 | std::multimap<unsigned, MachineOperand *>::iterator> |
| 742 | Range = RegRefs.equal_range(AntiDepReg); |
| 743 | for (std::multimap<unsigned, MachineOperand *>::iterator |
| 744 | Q = Range.first, QE = Range.second; Q != QE; ++Q) |
| 745 | Q->second->setReg(NewReg); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 746 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 747 | // We just went back in time and modified history; the |
| 748 | // liveness information for the anti-depenence reg is now |
| 749 | // inconsistent. Set the state as if it were dead. |
| 750 | Classes[NewReg] = Classes[AntiDepReg]; |
| 751 | DefIndices[NewReg] = DefIndices[AntiDepReg]; |
| 752 | KillIndices[NewReg] = KillIndices[AntiDepReg]; |
| 753 | assert(((KillIndices[NewReg] == ~0u) != |
| 754 | (DefIndices[NewReg] == ~0u)) && |
| 755 | "Kill and Def maps aren't consistent for NewReg!"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 756 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 757 | Classes[AntiDepReg] = 0; |
| 758 | DefIndices[AntiDepReg] = KillIndices[AntiDepReg]; |
| 759 | KillIndices[AntiDepReg] = ~0u; |
| 760 | assert(((KillIndices[AntiDepReg] == ~0u) != |
| 761 | (DefIndices[AntiDepReg] == ~0u)) && |
| 762 | "Kill and Def maps aren't consistent for AntiDepReg!"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 763 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 764 | RegRefs.erase(AntiDepReg); |
| 765 | Changed = true; |
| 766 | LastNewReg[AntiDepReg] = NewReg; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 770 | ScanInstruction(MI, Count); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 771 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 772 | |
| 773 | return Changed; |
| 774 | } |
| 775 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 776 | /// FixupKills - Fix the register kill flags, they may have been made |
| 777 | /// incorrect by instruction reordering. |
| 778 | /// |
| 779 | void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) { |
| 780 | DEBUG(errs() << "Fixup kills for BB ID#" << MBB->getNumber() << '\n'); |
| 781 | |
| 782 | std::set<unsigned> killedRegs; |
| 783 | BitVector ReservedRegs = TRI->getReservedRegs(MF); |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 784 | |
| 785 | // Examine block from end to start... |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 786 | unsigned Count = MBB->size(); |
| 787 | for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin(); |
| 788 | I != E; --Count) { |
| 789 | MachineInstr *MI = --I; |
| 790 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 791 | DEBUG(MI->dump()); |
| 792 | // Update liveness. Registers that are defed but not used in this |
| 793 | // instruction are now dead. Mark register and all subregs as they |
| 794 | // are completely defined. |
| 795 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 796 | MachineOperand &MO = MI->getOperand(i); |
| 797 | if (!MO.isReg()) continue; |
| 798 | unsigned Reg = MO.getReg(); |
| 799 | if (Reg == 0) continue; |
| 800 | if (!MO.isDef()) continue; |
| 801 | // Ignore two-addr defs. |
| 802 | if (MI->isRegTiedToUseOperand(i)) continue; |
| 803 | |
| 804 | DEBUG(errs() << "*** Handling Defs " << TM.getRegisterInfo()->get(Reg).Name << '\n'); |
| 805 | |
| 806 | KillIndices[Reg] = ~0u; |
| 807 | |
| 808 | // Repeat for all subregs. |
| 809 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 810 | *Subreg; ++Subreg) { |
| 811 | KillIndices[*Subreg] = ~0u; |
| 812 | } |
| 813 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 814 | |
| 815 | // Examine all used registers and set kill flag. When a register |
| 816 | // is used multiple times we only set the kill flag on the first |
| 817 | // use. |
| 818 | killedRegs.clear(); |
| 819 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 820 | MachineOperand &MO = MI->getOperand(i); |
| 821 | if (!MO.isReg() || !MO.isUse()) continue; |
| 822 | unsigned Reg = MO.getReg(); |
| 823 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 824 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 825 | DEBUG(errs() << "*** Handling Uses " << TM.getRegisterInfo()->get(Reg).Name << '\n'); |
| 826 | |
| 827 | bool kill = false; |
| 828 | if (killedRegs.find(Reg) == killedRegs.end()) { |
| 829 | kill = true; |
| 830 | // A register is not killed if any subregs are live... |
| 831 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 832 | *Subreg; ++Subreg) { |
| 833 | if (KillIndices[*Subreg] != ~0u) { |
| 834 | kill = false; |
| 835 | break; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | // If subreg is not live, then register is killed if it became |
| 840 | // live in this instruction |
| 841 | if (kill) |
| 842 | kill = (KillIndices[Reg] == ~0u); |
| 843 | } |
| 844 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 845 | if (MO.isKill() != kill) { |
| 846 | MO.setIsKill(kill); |
| 847 | DEBUG(errs() << "Fixed " << MO << " in "); |
| 848 | DEBUG(MI->dump()); |
| 849 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 850 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 851 | killedRegs.insert(Reg); |
| 852 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 853 | |
| 854 | // Mark any used register and subregs as now live... |
| 855 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 856 | MachineOperand &MO = MI->getOperand(i); |
| 857 | if (!MO.isReg() || !MO.isUse()) continue; |
| 858 | unsigned Reg = MO.getReg(); |
| 859 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 860 | |
| 861 | DEBUG(errs() << "Killing " << TM.getRegisterInfo()->get(Reg).Name << '\n'); |
| 862 | KillIndices[Reg] = Count; |
| 863 | |
| 864 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 865 | *Subreg; ++Subreg) { |
| 866 | KillIndices[*Subreg] = Count; |
| 867 | } |
| 868 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 872 | //===----------------------------------------------------------------------===// |
| 873 | // Top-Down Scheduling |
| 874 | //===----------------------------------------------------------------------===// |
| 875 | |
| 876 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
| 877 | /// the PendingQueue if the count reaches zero. Also update its cycle bound. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 878 | void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { |
| 879 | SUnit *SuccSU = SuccEdge->getSUnit(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 880 | --SuccSU->NumPredsLeft; |
| 881 | |
| 882 | #ifndef NDEBUG |
| 883 | if (SuccSU->NumPredsLeft < 0) { |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 884 | errs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 885 | SuccSU->dump(this); |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 886 | errs() << " has been released too many times!\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 887 | llvm_unreachable(0); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 888 | } |
| 889 | #endif |
| 890 | |
| 891 | // Compute how many cycles it will be before this actually becomes |
| 892 | // available. This is the max of the start time of all predecessors plus |
| 893 | // their latencies. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 894 | SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 895 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 896 | // If all the node's predecessors are scheduled, this node is ready |
| 897 | // to be scheduled. Ignore the special ExitSU node. |
| 898 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 899 | PendingQueue.push_back(SuccSU); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. |
| 903 | void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { |
| 904 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 905 | I != E; ++I) |
| 906 | ReleaseSucc(SU, &*I); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 910 | /// count of its successors. If a successor pending count is zero, add it to |
| 911 | /// the Available queue. |
| 912 | void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 913 | DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: "); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 914 | DEBUG(SU->dump(this)); |
| 915 | |
| 916 | Sequence.push_back(SU); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 917 | assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!"); |
| 918 | SU->setDepthToAtLeast(CurCycle); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 919 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 920 | ReleaseSuccessors(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 921 | SU->isScheduled = true; |
| 922 | AvailableQueue.ScheduledNode(SU); |
| 923 | } |
| 924 | |
| 925 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 926 | /// schedulers. |
| 927 | void SchedulePostRATDList::ListScheduleTopDown() { |
| 928 | unsigned CurCycle = 0; |
| 929 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 930 | // Release any successors of the special Entry node. |
| 931 | ReleaseSuccessors(&EntrySU); |
| 932 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 933 | // All leaves to Available queue. |
| 934 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 935 | // It is available if it has no predecessors. |
| 936 | if (SUnits[i].Preds.empty()) { |
| 937 | AvailableQueue.push(&SUnits[i]); |
| 938 | SUnits[i].isAvailable = true; |
| 939 | } |
| 940 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 941 | |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 942 | // In any cycle where we can't schedule any instructions, we must |
| 943 | // stall or emit a noop, depending on the target. |
| 944 | bool CycleInstCnt = 0; |
| 945 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 946 | // While Available queue is not empty, grab the node with the highest |
| 947 | // priority. If it is not ready put it back. Schedule the node. |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 948 | std::vector<SUnit*> NotReady; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 949 | Sequence.reserve(SUnits.size()); |
| 950 | while (!AvailableQueue.empty() || !PendingQueue.empty()) { |
| 951 | // Check to see if any of the pending instructions are ready to issue. If |
| 952 | // so, add them to the available queue. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 953 | unsigned MinDepth = ~0u; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 954 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 955 | if (PendingQueue[i]->getDepth() <= CurCycle) { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 956 | AvailableQueue.push(PendingQueue[i]); |
| 957 | PendingQueue[i]->isAvailable = true; |
| 958 | PendingQueue[i] = PendingQueue.back(); |
| 959 | PendingQueue.pop_back(); |
| 960 | --i; --e; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 961 | } else if (PendingQueue[i]->getDepth() < MinDepth) |
| 962 | MinDepth = PendingQueue[i]->getDepth(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 963 | } |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 964 | |
David Goodwin | 7cd0118 | 2009-08-11 17:56:42 +0000 | [diff] [blame] | 965 | DEBUG(errs() << "\n*** Examining Available\n"; |
| 966 | LatencyPriorityQueue q = AvailableQueue; |
| 967 | while (!q.empty()) { |
| 968 | SUnit *su = q.pop(); |
| 969 | errs() << "Height " << su->getHeight() << ": "; |
| 970 | su->dump(this); |
| 971 | }); |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 972 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 973 | SUnit *FoundSUnit = 0; |
| 974 | |
| 975 | bool HasNoopHazards = false; |
| 976 | while (!AvailableQueue.empty()) { |
| 977 | SUnit *CurSUnit = AvailableQueue.pop(); |
| 978 | |
| 979 | ScheduleHazardRecognizer::HazardType HT = |
| 980 | HazardRec->getHazardType(CurSUnit); |
| 981 | if (HT == ScheduleHazardRecognizer::NoHazard) { |
| 982 | FoundSUnit = CurSUnit; |
| 983 | break; |
| 984 | } |
| 985 | |
| 986 | // Remember if this is a noop hazard. |
| 987 | HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; |
| 988 | |
| 989 | NotReady.push_back(CurSUnit); |
| 990 | } |
| 991 | |
| 992 | // Add the nodes that aren't ready back onto the available list. |
| 993 | if (!NotReady.empty()) { |
| 994 | AvailableQueue.push_all(NotReady); |
| 995 | NotReady.clear(); |
| 996 | } |
| 997 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 998 | // If we found a node to schedule, do it now. |
| 999 | if (FoundSUnit) { |
| 1000 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1001 | HazardRec->EmitInstruction(FoundSUnit); |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1002 | CycleInstCnt++; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1003 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 1004 | // If we are using the target-specific hazards, then don't |
| 1005 | // advance the cycle time just because we schedule a node. If |
| 1006 | // the target allows it we can schedule multiple nodes in the |
| 1007 | // same cycle. |
| 1008 | if (!EnablePostRAHazardAvoidance) { |
| 1009 | if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops! |
| 1010 | ++CurCycle; |
| 1011 | } |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1012 | } else { |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1013 | if (CycleInstCnt > 0) { |
| 1014 | DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n'); |
| 1015 | HazardRec->AdvanceCycle(); |
| 1016 | } else if (!HasNoopHazards) { |
| 1017 | // Otherwise, we have a pipeline stall, but no other problem, |
| 1018 | // just advance the current cycle and try again. |
| 1019 | DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n'); |
| 1020 | HazardRec->AdvanceCycle(); |
| 1021 | ++NumStalls; |
| 1022 | } else { |
| 1023 | // Otherwise, we have no instructions to issue and we have instructions |
| 1024 | // that will fault if we don't do this right. This is the case for |
| 1025 | // processors without pipeline interlocks and other cases. |
| 1026 | DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n'); |
| 1027 | HazardRec->EmitNoop(); |
| 1028 | Sequence.push_back(0); // NULL here means noop |
| 1029 | ++NumNoops; |
| 1030 | } |
| 1031 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1032 | ++CurCycle; |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1033 | CycleInstCnt = 0; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | #ifndef NDEBUG |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 1038 | VerifySchedule(/*isBottomUp=*/false); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1039 | #endif |
| 1040 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 1041 | |
| 1042 | //===----------------------------------------------------------------------===// |
| 1043 | // Public Constructor Functions |
| 1044 | //===----------------------------------------------------------------------===// |
| 1045 | |
| 1046 | FunctionPass *llvm::createPostRAScheduler() { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1047 | return new PostRAScheduler(); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 1048 | } |