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 | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 22 | #include "CriticalAntiDepBreaker.h" |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 23 | #include "ExactHazardRecognizer.h" |
| 24 | #include "SimpleHazardRecognizer.h" |
Dan Gohman | 6dc75fe | 2009-02-06 17:12:10 +0000 | [diff] [blame] | 25 | #include "ScheduleDAGInstrs.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/Passes.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/LatencyPriorityQueue.h" |
| 28 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineDominators.h" |
David Goodwin | c7951f8 | 2009-10-01 19:45:32 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetInstrInfo.h" |
| 39 | #include "llvm/Target/TargetRegisterInfo.h" |
David Goodwin | 0dad89f | 2009-09-30 00:10:16 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetSubtarget.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 44 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 46 | #include <map> |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 47 | #include <set> |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 50 | STATISTIC(NumNoops, "Number of noops inserted"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 51 | STATISTIC(NumStalls, "Number of pipeline stalls"); |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 52 | STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 53 | |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 54 | // Post-RA scheduling is enabled with |
| 55 | // TargetSubtarget.enablePostRAScheduler(). This flag can be used to |
| 56 | // override the target. |
| 57 | static cl::opt<bool> |
| 58 | EnablePostRAScheduler("post-RA-scheduler", |
| 59 | cl::desc("Enable scheduling after register allocation"), |
David Goodwin | 9843a93 | 2009-10-01 22:19:57 +0000 | [diff] [blame] | 60 | cl::init(false), cl::Hidden); |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 61 | static cl::opt<std::string> |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 62 | EnableAntiDepBreaking("break-anti-dependencies", |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 63 | cl::desc("Break post-RA scheduling anti-dependencies: " |
| 64 | "\"critical\", \"all\", or \"none\""), |
| 65 | cl::init("none"), cl::Hidden); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 66 | static cl::opt<bool> |
| 67 | EnablePostRAHazardAvoidance("avoid-hazards", |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 68 | cl::desc("Enable exact hazard avoidance"), |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 69 | cl::init(true), cl::Hidden); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 70 | |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 71 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 72 | static cl::opt<int> |
| 73 | DebugDiv("postra-sched-debugdiv", |
| 74 | cl::desc("Debug control MBBs that are scheduled"), |
| 75 | cl::init(0), cl::Hidden); |
| 76 | static cl::opt<int> |
| 77 | DebugMod("postra-sched-debugmod", |
| 78 | cl::desc("Debug control MBBs that are scheduled"), |
| 79 | cl::init(0), cl::Hidden); |
| 80 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 81 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 82 | class PostRAScheduler : public MachineFunctionPass { |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 83 | AliasAnalysis *AA; |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 84 | CodeGenOpt::Level OptLevel; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 85 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 86 | public: |
| 87 | static char ID; |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 88 | PostRAScheduler(CodeGenOpt::Level ol) : |
| 89 | MachineFunctionPass(&ID), OptLevel(ol) {} |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 90 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 91 | void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 92 | AU.setPreservesCFG(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 93 | AU.addRequired<AliasAnalysis>(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 94 | AU.addRequired<MachineDominatorTree>(); |
| 95 | AU.addPreserved<MachineDominatorTree>(); |
| 96 | AU.addRequired<MachineLoopInfo>(); |
| 97 | AU.addPreserved<MachineLoopInfo>(); |
| 98 | MachineFunctionPass::getAnalysisUsage(AU); |
| 99 | } |
| 100 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 101 | const char *getPassName() const { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 102 | return "Post RA top-down list latency scheduler"; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | bool runOnMachineFunction(MachineFunction &Fn); |
| 106 | }; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 107 | char PostRAScheduler::ID = 0; |
| 108 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 109 | class SchedulePostRATDList : public ScheduleDAGInstrs { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 110 | /// AvailableQueue - The priority queue to use for the available SUnits. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 111 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 112 | LatencyPriorityQueue AvailableQueue; |
| 113 | |
| 114 | /// PendingQueue - This contains all of the instructions whose operands have |
| 115 | /// been issued, but their results are not ready yet (due to the latency of |
| 116 | /// the operation). Once the operands becomes available, the instruction is |
| 117 | /// added to the AvailableQueue. |
| 118 | std::vector<SUnit*> PendingQueue; |
| 119 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 120 | /// Topo - A topological ordering for SUnits. |
| 121 | ScheduleDAGTopologicalSort Topo; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 122 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 123 | /// HazardRec - The hazard recognizer to use. |
| 124 | ScheduleHazardRecognizer *HazardRec; |
| 125 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 126 | /// AntiDepBreak - Anti-dependence breaking object, or NULL if none |
| 127 | AntiDepBreaker *AntiDepBreak; |
| 128 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 129 | /// AA - AliasAnalysis for making memory reference queries. |
| 130 | AliasAnalysis *AA; |
| 131 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 132 | /// KillIndices - The index of the most recent kill (proceding bottom-up), |
| 133 | /// or ~0u if the register is not live. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 134 | unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 135 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 136 | public: |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 137 | SchedulePostRATDList(MachineFunction &MF, |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 138 | const MachineLoopInfo &MLI, |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 139 | const MachineDominatorTree &MDT, |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 140 | ScheduleHazardRecognizer *HR, |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 141 | AntiDepBreaker *ADB, |
| 142 | AliasAnalysis *aa) |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 143 | : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits), |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 144 | HazardRec(HR), AntiDepBreak(ADB), AA(aa) {} |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 145 | |
| 146 | ~SchedulePostRATDList() { |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 147 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 148 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 149 | /// StartBlock - Initialize register live-range state for scheduling in |
| 150 | /// this block. |
| 151 | /// |
| 152 | void StartBlock(MachineBasicBlock *BB); |
| 153 | |
| 154 | /// Schedule - Schedule the instruction range using list scheduling. |
| 155 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 156 | void Schedule(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 157 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 158 | /// Observe - Update liveness information to account for the current |
| 159 | /// instruction, which will not be scheduled. |
| 160 | /// |
| 161 | void Observe(MachineInstr *MI, unsigned Count); |
| 162 | |
| 163 | /// FinishBlock - Clean up register live-range state. |
| 164 | /// |
| 165 | void FinishBlock(); |
| 166 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 167 | /// FixupKills - Fix register kill flags that have been made |
| 168 | /// invalid due to scheduling |
| 169 | /// |
| 170 | void FixupKills(MachineBasicBlock *MBB); |
| 171 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 172 | private: |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 173 | void ReleaseSucc(SUnit *SU, SDep *SuccEdge); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 174 | void ReleaseSuccessors(SUnit *SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 175 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
| 176 | void ListScheduleTopDown(); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 177 | void StartBlockForKills(MachineBasicBlock *BB); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 178 | |
| 179 | // ToggleKillFlag - Toggle a register operand kill flag. Other |
| 180 | // adjustments may be made to the instruction if necessary. Return |
| 181 | // true if the operand has been deleted, false if not. |
| 182 | bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 183 | }; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 186 | /// isSchedulingBoundary - Test if the given instruction should be |
| 187 | /// considered a scheduling boundary. This primarily includes labels |
| 188 | /// and terminators. |
| 189 | /// |
| 190 | static bool isSchedulingBoundary(const MachineInstr *MI, |
| 191 | const MachineFunction &MF) { |
| 192 | // Terminators and labels can't be scheduled around. |
| 193 | if (MI->getDesc().isTerminator() || MI->isLabel()) |
| 194 | return true; |
| 195 | |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 196 | // Don't attempt to schedule around any instruction that modifies |
| 197 | // a stack-oriented pointer, as it's unlikely to be profitable. This |
| 198 | // saves compile time, because it doesn't require every single |
| 199 | // stack slot reference to depend on the instruction that does the |
| 200 | // modification. |
| 201 | const TargetLowering &TLI = *MF.getTarget().getTargetLowering(); |
| 202 | if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore())) |
| 203 | return true; |
| 204 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 205 | return false; |
| 206 | } |
| 207 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 208 | bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { |
Dan Gohman | 5bf7c2a | 2009-10-10 00:15:38 +0000 | [diff] [blame] | 209 | AA = &getAnalysis<AliasAnalysis>(); |
| 210 | |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 211 | // Check for explicit enable/disable of post-ra scheduling. |
David Goodwin | 4c3715c | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 212 | TargetSubtarget::AntiDepBreakMode AntiDepMode = TargetSubtarget::ANTIDEP_NONE; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 213 | if (EnablePostRAScheduler.getPosition() > 0) { |
| 214 | if (!EnablePostRAScheduler) |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 215 | return false; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 216 | } else { |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 217 | // Check that post-RA scheduling is enabled for this target. |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 218 | const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>(); |
David Goodwin | 4c3715c | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 219 | if (!ST.enablePostRAScheduler(OptLevel, AntiDepMode)) |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 220 | return false; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 221 | } |
David Goodwin | 0dad89f | 2009-09-30 00:10:16 +0000 | [diff] [blame] | 222 | |
David Goodwin | 4c3715c | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 223 | // Check for antidep breaking override... |
| 224 | if (EnableAntiDepBreaking.getPosition() > 0) { |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 225 | AntiDepMode = (EnableAntiDepBreaking == "all") ? TargetSubtarget::ANTIDEP_ALL : |
| 226 | (EnableAntiDepBreaking == "critical") ? TargetSubtarget::ANTIDEP_CRITICAL : |
| 227 | TargetSubtarget::ANTIDEP_NONE; |
David Goodwin | 4c3715c | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 228 | } |
| 229 | |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 230 | DEBUG(errs() << "PostRAScheduler\n"); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 231 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 232 | const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 233 | const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 234 | const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData(); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 235 | ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ? |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 236 | (ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) : |
| 237 | (ScheduleHazardRecognizer *)new SimpleHazardRecognizer(); |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 238 | AntiDepBreaker *ADB = |
| 239 | (AntiDepMode == TargetSubtarget::ANTIDEP_ALL) ? NULL /* FIXME */ : |
| 240 | (AntiDepMode == TargetSubtarget::ANTIDEP_CRITICAL) ? |
| 241 | new CriticalAntiDepBreaker(Fn) : NULL; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 242 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 243 | SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR, ADB, AA); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 244 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 245 | // Loop over all of the basic blocks |
| 246 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 247 | MBB != MBBe; ++MBB) { |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 248 | #ifndef NDEBUG |
| 249 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 250 | if (DebugDiv > 0) { |
| 251 | static int bbcnt = 0; |
| 252 | if (bbcnt++ % DebugDiv != DebugMod) |
| 253 | continue; |
| 254 | errs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() << |
| 255 | ":MBB ID#" << MBB->getNumber() << " ***\n"; |
| 256 | } |
| 257 | #endif |
| 258 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 259 | // Initialize register live-range state for scheduling in this block. |
| 260 | Scheduler.StartBlock(MBB); |
| 261 | |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 262 | // Schedule each sequence of instructions not interrupted by a label |
| 263 | // or anything else that effectively needs to shut down scheduling. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 264 | MachineBasicBlock::iterator Current = MBB->end(); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 265 | unsigned Count = MBB->size(), CurrentCount = Count; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 266 | for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) { |
| 267 | MachineInstr *MI = prior(I); |
| 268 | if (isSchedulingBoundary(MI, Fn)) { |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 269 | Scheduler.Run(MBB, I, Current, CurrentCount); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 270 | Scheduler.EmitSchedule(0); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 271 | Current = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 272 | CurrentCount = Count - 1; |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 273 | Scheduler.Observe(MI, CurrentCount); |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 274 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 275 | I = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 276 | --Count; |
Dan Gohman | 43f07fb | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 277 | } |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 278 | assert(Count == 0 && "Instruction count mismatch!"); |
Duncan Sands | 9e8bd0b | 2009-03-11 09:04:34 +0000 | [diff] [blame] | 279 | assert((MBB->begin() == Current || CurrentCount != 0) && |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 280 | "Instruction count mismatch!"); |
| 281 | Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 282 | Scheduler.EmitSchedule(0); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 283 | |
| 284 | // Clean up register live-range state. |
| 285 | Scheduler.FinishBlock(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 286 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 287 | // Update register kills |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 288 | Scheduler.FixupKills(MBB); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 289 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 290 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 291 | delete HR; |
| 292 | delete ADB; |
| 293 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 294 | return true; |
| 295 | } |
| 296 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 297 | /// StartBlock - Initialize register live-range state for scheduling in |
| 298 | /// this block. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 299 | /// |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 300 | void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) { |
| 301 | // Call the superclass. |
| 302 | ScheduleDAGInstrs::StartBlock(BB); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 303 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 304 | // Reset the hazard recognizer and anti-dep breaker. |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 305 | HazardRec->Reset(); |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 306 | if (AntiDepBreak != NULL) |
| 307 | AntiDepBreak->StartBlock(BB); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /// Schedule - Schedule the instruction range using list scheduling. |
| 311 | /// |
| 312 | void SchedulePostRATDList::Schedule() { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 313 | DEBUG(errs() << "********** List Scheduling **********\n"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 314 | |
| 315 | // Build the scheduling graph. |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 316 | BuildSchedGraph(AA); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 317 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 318 | if (AntiDepBreak != NULL) { |
| 319 | unsigned Broken = |
| 320 | AntiDepBreak->BreakAntiDependencies(SUnits, Begin, InsertPos, |
| 321 | InsertPosIndex); |
| 322 | if (Broken > 0) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 323 | // We made changes. Update the dependency graph. |
| 324 | // Theoretically we could update the graph in place: |
| 325 | // When a live range is changed to use a different register, remove |
| 326 | // the def's anti-dependence *and* output-dependence edges due to |
| 327 | // that register, and add new anti-dependence and output-dependence |
| 328 | // edges based on the next live range of the register. |
| 329 | SUnits.clear(); |
| 330 | EntrySU = SUnit(); |
| 331 | ExitSU = SUnit(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 332 | BuildSchedGraph(AA); |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 333 | |
| 334 | NumFixedAnti += Broken; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 338 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 339 | SUnits[su].dumpAll(this)); |
| 340 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 341 | AvailableQueue.initNodes(SUnits); |
| 342 | |
| 343 | ListScheduleTopDown(); |
| 344 | |
| 345 | AvailableQueue.releaseState(); |
| 346 | } |
| 347 | |
| 348 | /// Observe - Update liveness information to account for the current |
| 349 | /// instruction, which will not be scheduled. |
| 350 | /// |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 351 | void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) { |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 352 | if (AntiDepBreak != NULL) |
| 353 | AntiDepBreak->Observe(MI, Count, InsertPosIndex); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /// FinishBlock - Clean up register live-range state. |
| 357 | /// |
| 358 | void SchedulePostRATDList::FinishBlock() { |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame^] | 359 | if (AntiDepBreak != NULL) |
| 360 | AntiDepBreak->FinishBlock(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 361 | |
| 362 | // Call the superclass. |
| 363 | ScheduleDAGInstrs::FinishBlock(); |
| 364 | } |
| 365 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 366 | /// StartBlockForKills - Initialize register live-range state for updating kills |
| 367 | /// |
| 368 | void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) { |
| 369 | // Initialize the indices to indicate that no registers are live. |
| 370 | std::fill(KillIndices, array_endof(KillIndices), ~0u); |
| 371 | |
| 372 | // Determine the live-out physregs for this block. |
| 373 | if (!BB->empty() && BB->back().getDesc().isReturn()) { |
| 374 | // In a return block, examine the function live-out regs. |
| 375 | for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), |
| 376 | E = MRI.liveout_end(); I != E; ++I) { |
| 377 | unsigned Reg = *I; |
| 378 | KillIndices[Reg] = BB->size(); |
| 379 | // Repeat, for all subregs. |
| 380 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 381 | *Subreg; ++Subreg) { |
| 382 | KillIndices[*Subreg] = BB->size(); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | else { |
| 387 | // In a non-return block, examine the live-in regs of all successors. |
| 388 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 389 | SE = BB->succ_end(); SI != SE; ++SI) { |
| 390 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 391 | E = (*SI)->livein_end(); I != E; ++I) { |
| 392 | unsigned Reg = *I; |
| 393 | KillIndices[Reg] = BB->size(); |
| 394 | // Repeat, for all subregs. |
| 395 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 396 | *Subreg; ++Subreg) { |
| 397 | KillIndices[*Subreg] = BB->size(); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 404 | bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI, |
| 405 | MachineOperand &MO) { |
| 406 | // Setting kill flag... |
| 407 | if (!MO.isKill()) { |
| 408 | MO.setIsKill(true); |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | // If MO itself is live, clear the kill flag... |
| 413 | if (KillIndices[MO.getReg()] != ~0u) { |
| 414 | MO.setIsKill(false); |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | // If any subreg of MO is live, then create an imp-def for that |
| 419 | // subreg and keep MO marked as killed. |
Benjamin Kramer | 8bff4af | 2009-10-02 15:59:52 +0000 | [diff] [blame] | 420 | MO.setIsKill(false); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 421 | bool AllDead = true; |
| 422 | const unsigned SuperReg = MO.getReg(); |
| 423 | for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg); |
| 424 | *Subreg; ++Subreg) { |
| 425 | if (KillIndices[*Subreg] != ~0u) { |
| 426 | MI->addOperand(MachineOperand::CreateReg(*Subreg, |
| 427 | true /*IsDef*/, |
| 428 | true /*IsImp*/, |
| 429 | false /*IsKill*/, |
| 430 | false /*IsDead*/)); |
| 431 | AllDead = false; |
| 432 | } |
| 433 | } |
| 434 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 435 | if(AllDead) |
Benjamin Kramer | 8bff4af | 2009-10-02 15:59:52 +0000 | [diff] [blame] | 436 | MO.setIsKill(true); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 437 | return false; |
| 438 | } |
| 439 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 440 | /// FixupKills - Fix the register kill flags, they may have been made |
| 441 | /// incorrect by instruction reordering. |
| 442 | /// |
| 443 | void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) { |
| 444 | DEBUG(errs() << "Fixup kills for BB ID#" << MBB->getNumber() << '\n'); |
| 445 | |
| 446 | std::set<unsigned> killedRegs; |
| 447 | BitVector ReservedRegs = TRI->getReservedRegs(MF); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 448 | |
| 449 | StartBlockForKills(MBB); |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 450 | |
| 451 | // Examine block from end to start... |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 452 | unsigned Count = MBB->size(); |
| 453 | for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin(); |
| 454 | I != E; --Count) { |
| 455 | MachineInstr *MI = --I; |
| 456 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 457 | // Update liveness. Registers that are defed but not used in this |
| 458 | // instruction are now dead. Mark register and all subregs as they |
| 459 | // are completely defined. |
| 460 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 461 | MachineOperand &MO = MI->getOperand(i); |
| 462 | if (!MO.isReg()) continue; |
| 463 | unsigned Reg = MO.getReg(); |
| 464 | if (Reg == 0) continue; |
| 465 | if (!MO.isDef()) continue; |
| 466 | // Ignore two-addr defs. |
| 467 | if (MI->isRegTiedToUseOperand(i)) continue; |
| 468 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 469 | KillIndices[Reg] = ~0u; |
| 470 | |
| 471 | // Repeat for all subregs. |
| 472 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 473 | *Subreg; ++Subreg) { |
| 474 | KillIndices[*Subreg] = ~0u; |
| 475 | } |
| 476 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 477 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 478 | // Examine all used registers and set/clear kill flag. When a |
| 479 | // register is used multiple times we only set the kill flag on |
| 480 | // the first use. |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 481 | killedRegs.clear(); |
| 482 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 483 | MachineOperand &MO = MI->getOperand(i); |
| 484 | if (!MO.isReg() || !MO.isUse()) continue; |
| 485 | unsigned Reg = MO.getReg(); |
| 486 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 487 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 488 | bool kill = false; |
| 489 | if (killedRegs.find(Reg) == killedRegs.end()) { |
| 490 | kill = true; |
| 491 | // A register is not killed if any subregs are live... |
| 492 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 493 | *Subreg; ++Subreg) { |
| 494 | if (KillIndices[*Subreg] != ~0u) { |
| 495 | kill = false; |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | // If subreg is not live, then register is killed if it became |
| 501 | // live in this instruction |
| 502 | if (kill) |
| 503 | kill = (KillIndices[Reg] == ~0u); |
| 504 | } |
| 505 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 506 | if (MO.isKill() != kill) { |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 507 | bool removed = ToggleKillFlag(MI, MO); |
| 508 | if (removed) { |
| 509 | DEBUG(errs() << "Fixed <removed> in "); |
| 510 | } else { |
| 511 | DEBUG(errs() << "Fixed " << MO << " in "); |
| 512 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 513 | DEBUG(MI->dump()); |
| 514 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 515 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 516 | killedRegs.insert(Reg); |
| 517 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 518 | |
David Goodwin | a3251db | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 519 | // Mark any used register (that is not using undef) and subregs as |
| 520 | // now live... |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 521 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 522 | MachineOperand &MO = MI->getOperand(i); |
David Goodwin | a3251db | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 523 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 524 | unsigned Reg = MO.getReg(); |
| 525 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 526 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 527 | KillIndices[Reg] = Count; |
| 528 | |
| 529 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 530 | *Subreg; ++Subreg) { |
| 531 | KillIndices[*Subreg] = Count; |
| 532 | } |
| 533 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 537 | //===----------------------------------------------------------------------===// |
| 538 | // Top-Down Scheduling |
| 539 | //===----------------------------------------------------------------------===// |
| 540 | |
| 541 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
| 542 | /// the PendingQueue if the count reaches zero. Also update its cycle bound. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 543 | void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { |
| 544 | SUnit *SuccSU = SuccEdge->getSUnit(); |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 545 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 546 | #ifndef NDEBUG |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 547 | if (SuccSU->NumPredsLeft == 0) { |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 548 | errs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 549 | SuccSU->dump(this); |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 550 | errs() << " has been released too many times!\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 551 | llvm_unreachable(0); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 552 | } |
| 553 | #endif |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 554 | --SuccSU->NumPredsLeft; |
| 555 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 556 | // Compute how many cycles it will be before this actually becomes |
| 557 | // available. This is the max of the start time of all predecessors plus |
| 558 | // their latencies. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 559 | SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 560 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 561 | // If all the node's predecessors are scheduled, this node is ready |
| 562 | // to be scheduled. Ignore the special ExitSU node. |
| 563 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 564 | PendingQueue.push_back(SuccSU); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. |
| 568 | void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { |
| 569 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 570 | I != E; ++I) |
| 571 | ReleaseSucc(SU, &*I); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 575 | /// count of its successors. If a successor pending count is zero, add it to |
| 576 | /// the Available queue. |
| 577 | void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 578 | DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: "); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 579 | DEBUG(SU->dump(this)); |
| 580 | |
| 581 | Sequence.push_back(SU); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 582 | assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!"); |
| 583 | SU->setDepthToAtLeast(CurCycle); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 584 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 585 | ReleaseSuccessors(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 586 | SU->isScheduled = true; |
| 587 | AvailableQueue.ScheduledNode(SU); |
| 588 | } |
| 589 | |
| 590 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 591 | /// schedulers. |
| 592 | void SchedulePostRATDList::ListScheduleTopDown() { |
| 593 | unsigned CurCycle = 0; |
| 594 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 595 | // Release any successors of the special Entry node. |
| 596 | ReleaseSuccessors(&EntrySU); |
| 597 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 598 | // All leaves to Available queue. |
| 599 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 600 | // It is available if it has no predecessors. |
| 601 | if (SUnits[i].Preds.empty()) { |
| 602 | AvailableQueue.push(&SUnits[i]); |
| 603 | SUnits[i].isAvailable = true; |
| 604 | } |
| 605 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 606 | |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 607 | // In any cycle where we can't schedule any instructions, we must |
| 608 | // stall or emit a noop, depending on the target. |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 609 | bool CycleHasInsts = false; |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 610 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 611 | // While Available queue is not empty, grab the node with the highest |
| 612 | // priority. If it is not ready put it back. Schedule the node. |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 613 | std::vector<SUnit*> NotReady; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 614 | Sequence.reserve(SUnits.size()); |
| 615 | while (!AvailableQueue.empty() || !PendingQueue.empty()) { |
| 616 | // Check to see if any of the pending instructions are ready to issue. If |
| 617 | // so, add them to the available queue. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 618 | unsigned MinDepth = ~0u; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 619 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 620 | if (PendingQueue[i]->getDepth() <= CurCycle) { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 621 | AvailableQueue.push(PendingQueue[i]); |
| 622 | PendingQueue[i]->isAvailable = true; |
| 623 | PendingQueue[i] = PendingQueue.back(); |
| 624 | PendingQueue.pop_back(); |
| 625 | --i; --e; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 626 | } else if (PendingQueue[i]->getDepth() < MinDepth) |
| 627 | MinDepth = PendingQueue[i]->getDepth(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 628 | } |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 629 | |
David Goodwin | 7cd0118 | 2009-08-11 17:56:42 +0000 | [diff] [blame] | 630 | DEBUG(errs() << "\n*** Examining Available\n"; |
| 631 | LatencyPriorityQueue q = AvailableQueue; |
| 632 | while (!q.empty()) { |
| 633 | SUnit *su = q.pop(); |
| 634 | errs() << "Height " << su->getHeight() << ": "; |
| 635 | su->dump(this); |
| 636 | }); |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 637 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 638 | SUnit *FoundSUnit = 0; |
| 639 | |
| 640 | bool HasNoopHazards = false; |
| 641 | while (!AvailableQueue.empty()) { |
| 642 | SUnit *CurSUnit = AvailableQueue.pop(); |
| 643 | |
| 644 | ScheduleHazardRecognizer::HazardType HT = |
| 645 | HazardRec->getHazardType(CurSUnit); |
| 646 | if (HT == ScheduleHazardRecognizer::NoHazard) { |
| 647 | FoundSUnit = CurSUnit; |
| 648 | break; |
| 649 | } |
| 650 | |
| 651 | // Remember if this is a noop hazard. |
| 652 | HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; |
| 653 | |
| 654 | NotReady.push_back(CurSUnit); |
| 655 | } |
| 656 | |
| 657 | // Add the nodes that aren't ready back onto the available list. |
| 658 | if (!NotReady.empty()) { |
| 659 | AvailableQueue.push_all(NotReady); |
| 660 | NotReady.clear(); |
| 661 | } |
| 662 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 663 | // If we found a node to schedule, do it now. |
| 664 | if (FoundSUnit) { |
| 665 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 666 | HazardRec->EmitInstruction(FoundSUnit); |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 667 | CycleHasInsts = true; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 668 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 669 | // If we are using the target-specific hazards, then don't |
| 670 | // advance the cycle time just because we schedule a node. If |
| 671 | // the target allows it we can schedule multiple nodes in the |
| 672 | // same cycle. |
| 673 | if (!EnablePostRAHazardAvoidance) { |
| 674 | if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops! |
| 675 | ++CurCycle; |
| 676 | } |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 677 | } else { |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 678 | if (CycleHasInsts) { |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 679 | DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n'); |
| 680 | HazardRec->AdvanceCycle(); |
| 681 | } else if (!HasNoopHazards) { |
| 682 | // Otherwise, we have a pipeline stall, but no other problem, |
| 683 | // just advance the current cycle and try again. |
| 684 | DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n'); |
| 685 | HazardRec->AdvanceCycle(); |
| 686 | ++NumStalls; |
| 687 | } else { |
| 688 | // Otherwise, we have no instructions to issue and we have instructions |
| 689 | // that will fault if we don't do this right. This is the case for |
| 690 | // processors without pipeline interlocks and other cases. |
| 691 | DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n'); |
| 692 | HazardRec->EmitNoop(); |
| 693 | Sequence.push_back(0); // NULL here means noop |
| 694 | ++NumNoops; |
| 695 | } |
| 696 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 697 | ++CurCycle; |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 698 | CycleHasInsts = false; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
| 702 | #ifndef NDEBUG |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 703 | VerifySchedule(/*isBottomUp=*/false); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 704 | #endif |
| 705 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 706 | |
| 707 | //===----------------------------------------------------------------------===// |
| 708 | // Public Constructor Functions |
| 709 | //===----------------------------------------------------------------------===// |
| 710 | |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 711 | FunctionPass *llvm::createPostRAScheduler(CodeGenOpt::Level OptLevel) { |
| 712 | return new PostRAScheduler(OptLevel); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 713 | } |