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