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