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