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