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 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "AggressiveAntiDepBreaker.h" |
| 23 | #include "AntiDepBreaker.h" |
| 24 | #include "CriticalAntiDepBreaker.h" |
| 25 | #include "llvm/ADT/BitVector.h" |
| 26 | #include "llvm/ADT/Statistic.h" |
| 27 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/LatencyPriorityQueue.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" |
Andrew Trick | 1525260 | 2012-06-06 20:29:31 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Andrew Trick | ed395c8 | 2012-03-07 23:01:06 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/SchedulerRegistry.h" |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetInstrInfo.h" |
| 43 | #include "llvm/Target/TargetLowering.h" |
| 44 | #include "llvm/Target/TargetMachine.h" |
| 45 | #include "llvm/Target/TargetRegisterInfo.h" |
| 46 | #include "llvm/Target/TargetSubtargetInfo.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 49 | #define DEBUG_TYPE "post-RA-sched" |
| 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 { |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 82 | const TargetInstrInfo *TII; |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 83 | RegisterClassInfo RegClassInfo; |
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; |
Andrew Trick | c7d081b | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 87 | PostRAScheduler() : MachineFunctionPass(ID) {} |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 88 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 89 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 90 | AU.setPreservesCFG(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 91 | AU.addRequired<AliasAnalysis>(); |
Andrew Trick | c7d081b | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 92 | AU.addRequired<TargetPassConfig>(); |
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 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 100 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 101 | }; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 102 | char PostRAScheduler::ID = 0; |
| 103 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 104 | class SchedulePostRATDList : public ScheduleDAGInstrs { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 105 | /// AvailableQueue - The priority queue to use for the available SUnits. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 106 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 107 | LatencyPriorityQueue AvailableQueue; |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 108 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 109 | /// PendingQueue - This contains all of the instructions whose operands have |
| 110 | /// been issued, but their results are not ready yet (due to the latency of |
| 111 | /// the operation). Once the operands becomes available, the instruction is |
| 112 | /// added to the AvailableQueue. |
| 113 | std::vector<SUnit*> PendingQueue; |
| 114 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 115 | /// HazardRec - The hazard recognizer to use. |
| 116 | ScheduleHazardRecognizer *HazardRec; |
| 117 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 118 | /// AntiDepBreak - Anti-dependence breaking object, or NULL if none |
| 119 | AntiDepBreaker *AntiDepBreak; |
| 120 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 121 | /// AA - AliasAnalysis for making memory reference queries. |
| 122 | AliasAnalysis *AA; |
| 123 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 124 | /// The schedule. Null SUnit*'s represent noop instructions. |
| 125 | std::vector<SUnit*> Sequence; |
| 126 | |
Andrew Trick | d2763f6 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 127 | /// The index in BB of RegionEnd. |
| 128 | /// |
| 129 | /// This is the instruction number from the top of the current block, not |
| 130 | /// the SlotIndex. It is only used by the AntiDepBreaker. |
| 131 | unsigned EndIndex; |
| 132 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 133 | public: |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 134 | SchedulePostRATDList( |
| 135 | MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT, |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 136 | AliasAnalysis *AA, const RegisterClassInfo&, |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 137 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode, |
Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 138 | SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 139 | |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 140 | ~SchedulePostRATDList(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 141 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 142 | /// startBlock - Initialize register live-range state for scheduling in |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 143 | /// this block. |
| 144 | /// |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 145 | void startBlock(MachineBasicBlock *BB) override; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 146 | |
Andrew Trick | d2763f6 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 147 | // Set the index of RegionEnd within the current BB. |
| 148 | void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; } |
| 149 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 150 | /// Initialize the scheduler state for the next scheduling region. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 151 | void enterRegion(MachineBasicBlock *bb, |
| 152 | MachineBasicBlock::iterator begin, |
| 153 | MachineBasicBlock::iterator end, |
| 154 | unsigned regioninstrs) override; |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 155 | |
| 156 | /// Notify that the scheduler has finished scheduling the current region. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 157 | void exitRegion() override; |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 158 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 159 | /// Schedule - Schedule the instruction range using list scheduling. |
| 160 | /// |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 161 | void schedule() override; |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 162 | |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 163 | void EmitSchedule(); |
| 164 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 165 | /// Observe - Update liveness information to account for the current |
| 166 | /// instruction, which will not be scheduled. |
| 167 | /// |
| 168 | void Observe(MachineInstr *MI, unsigned Count); |
| 169 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 170 | /// finishBlock - Clean up register live-range state. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 171 | /// |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 172 | void finishBlock() override; |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 173 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 174 | private: |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 175 | void ReleaseSucc(SUnit *SU, SDep *SuccEdge); |
| 176 | void ReleaseSuccessors(SUnit *SU); |
| 177 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
| 178 | void ListScheduleTopDown(); |
Andrew Trick | 73ba69b | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 179 | |
| 180 | void dumpSchedule() const; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 181 | void emitNoop(unsigned CurCycle); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 182 | }; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 185 | char &llvm::PostRASchedulerID = PostRAScheduler::ID; |
| 186 | |
| 187 | INITIALIZE_PASS(PostRAScheduler, "post-RA-sched", |
| 188 | "Post RA top-down list latency scheduler", false, false) |
| 189 | |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 190 | SchedulePostRATDList::SchedulePostRATDList( |
| 191 | MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT, |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 192 | AliasAnalysis *AA, const RegisterClassInfo &RCI, |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 193 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode, |
Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 194 | SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 195 | : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), AA(AA), EndIndex(0) { |
| 196 | |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 197 | const TargetMachine &TM = MF.getTarget(); |
| 198 | const InstrItineraryData *InstrItins = TM.getInstrItineraryData(); |
| 199 | HazardRec = |
| 200 | TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this); |
Preston Gurd | 6a8c7bf | 2012-04-23 21:39:35 +0000 | [diff] [blame] | 201 | |
| 202 | assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE || |
| 203 | MRI.tracksLiveness()) && |
| 204 | "Live-ins must be accurate for anti-dependency breaking"); |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 205 | AntiDepBreak = |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 206 | ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ? |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 207 | (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) : |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 208 | ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ? |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 209 | (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : nullptr)); |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | SchedulePostRATDList::~SchedulePostRATDList() { |
| 213 | delete HazardRec; |
| 214 | delete AntiDepBreak; |
| 215 | } |
| 216 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 217 | /// Initialize state associated with the next scheduling region. |
| 218 | void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb, |
| 219 | MachineBasicBlock::iterator begin, |
| 220 | MachineBasicBlock::iterator end, |
Andrew Trick | d2763f6 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 221 | unsigned regioninstrs) { |
| 222 | ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs); |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 223 | Sequence.clear(); |
| 224 | } |
| 225 | |
| 226 | /// Print the schedule before exiting the region. |
| 227 | void SchedulePostRATDList::exitRegion() { |
| 228 | DEBUG({ |
| 229 | dbgs() << "*** Final schedule ***\n"; |
| 230 | dumpSchedule(); |
| 231 | dbgs() << '\n'; |
| 232 | }); |
| 233 | ScheduleDAGInstrs::exitRegion(); |
| 234 | } |
| 235 | |
Manman Ren | b720be6 | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 236 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Andrew Trick | 73ba69b | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 237 | /// dumpSchedule - dump the scheduled Sequence. |
| 238 | void SchedulePostRATDList::dumpSchedule() const { |
| 239 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 240 | if (SUnit *SU = Sequence[i]) |
| 241 | SU->dump(this); |
| 242 | else |
| 243 | dbgs() << "**** NOOP ****\n"; |
| 244 | } |
| 245 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 246 | #endif |
Andrew Trick | 73ba69b | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 247 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 248 | bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 249 | if (skipOptnoneFunction(*Fn.getFunction())) |
| 250 | return false; |
| 251 | |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 252 | TII = Fn.getTarget().getInstrInfo(); |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 253 | MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 254 | MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
| 255 | AliasAnalysis *AA = &getAnalysis<AliasAnalysis>(); |
Andrew Trick | c7d081b | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 256 | TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>(); |
| 257 | |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 258 | RegClassInfo.runOnMachineFunction(Fn); |
Dan Gohman | 5bf7c2a | 2009-10-10 00:15:38 +0000 | [diff] [blame] | 259 | |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 260 | // Check for explicit enable/disable of post-ra scheduling. |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 261 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode = |
| 262 | TargetSubtargetInfo::ANTIDEP_NONE; |
Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 263 | SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 264 | if (EnablePostRAScheduler.getPosition() > 0) { |
| 265 | if (!EnablePostRAScheduler) |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 266 | return false; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 267 | } else { |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 268 | // Check that post-RA scheduling is enabled for this target. |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 269 | // This may upgrade the AntiDepMode. |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 270 | const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>(); |
Andrew Trick | c7d081b | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 271 | if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode, |
| 272 | CriticalPathRCs)) |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 273 | return false; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 274 | } |
David Goodwin | 0dad89f | 2009-09-30 00:10:16 +0000 | [diff] [blame] | 275 | |
David Goodwin | 4c3715c | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 276 | // Check for antidep breaking override... |
| 277 | if (EnableAntiDepBreaking.getPosition() > 0) { |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 278 | AntiDepMode = (EnableAntiDepBreaking == "all") |
| 279 | ? TargetSubtargetInfo::ANTIDEP_ALL |
| 280 | : ((EnableAntiDepBreaking == "critical") |
| 281 | ? TargetSubtargetInfo::ANTIDEP_CRITICAL |
| 282 | : TargetSubtargetInfo::ANTIDEP_NONE); |
David Goodwin | 4c3715c | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 283 | } |
| 284 | |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 285 | DEBUG(dbgs() << "PostRAScheduler\n"); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 286 | |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 287 | SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode, |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 288 | CriticalPathRCs); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 289 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 290 | // Loop over all of the basic blocks |
| 291 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 292 | MBB != MBBe; ++MBB) { |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 293 | #ifndef NDEBUG |
| 294 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 295 | if (DebugDiv > 0) { |
| 296 | static int bbcnt = 0; |
| 297 | if (bbcnt++ % DebugDiv != DebugMod) |
| 298 | continue; |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 299 | dbgs() << "*** DEBUG scheduling " << Fn.getName() |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 300 | << ":BB#" << MBB->getNumber() << " ***\n"; |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 301 | } |
| 302 | #endif |
| 303 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 304 | // Initialize register live-range state for scheduling in this block. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 305 | Scheduler.startBlock(MBB); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 306 | |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 307 | // Schedule each sequence of instructions not interrupted by a label |
| 308 | // or anything else that effectively needs to shut down scheduling. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 309 | MachineBasicBlock::iterator Current = MBB->end(); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 310 | unsigned Count = MBB->size(), CurrentCount = Count; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 311 | for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 312 | MachineInstr *MI = std::prev(I); |
Andrew Trick | d2763f6 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 313 | --Count; |
Jakob Stoklund Olesen | 976647d | 2012-02-23 17:54:21 +0000 | [diff] [blame] | 314 | // Calls are not scheduling boundaries before register allocation, but |
| 315 | // post-ra we don't gain anything by scheduling across calls since we |
| 316 | // don't need to worry about register pressure. |
| 317 | if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) { |
Andrew Trick | d2763f6 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 318 | Scheduler.enterRegion(MBB, I, Current, CurrentCount - Count); |
| 319 | Scheduler.setEndIndex(CurrentCount); |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 320 | Scheduler.schedule(); |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 321 | Scheduler.exitRegion(); |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 322 | Scheduler.EmitSchedule(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 323 | Current = MI; |
Andrew Trick | d2763f6 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 324 | CurrentCount = Count; |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 325 | Scheduler.Observe(MI, CurrentCount); |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 326 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 327 | I = MI; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 328 | if (MI->isBundle()) |
| 329 | Count -= MI->getBundleSize(); |
Dan Gohman | 43f07fb | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 330 | } |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 331 | assert(Count == 0 && "Instruction count mismatch!"); |
Duncan Sands | 9e8bd0b | 2009-03-11 09:04:34 +0000 | [diff] [blame] | 332 | assert((MBB->begin() == Current || CurrentCount != 0) && |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 333 | "Instruction count mismatch!"); |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 334 | Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount); |
Andrew Trick | d2763f6 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 335 | Scheduler.setEndIndex(CurrentCount); |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 336 | Scheduler.schedule(); |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 337 | Scheduler.exitRegion(); |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 338 | Scheduler.EmitSchedule(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 339 | |
| 340 | // Clean up register live-range state. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 341 | Scheduler.finishBlock(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 342 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 343 | // Update register kills |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 344 | Scheduler.fixupKills(MBB); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 345 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 346 | |
| 347 | return true; |
| 348 | } |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 349 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 350 | /// StartBlock - Initialize register live-range state for scheduling in |
| 351 | /// this block. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 352 | /// |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 353 | void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 354 | // Call the superclass. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 355 | ScheduleDAGInstrs::startBlock(BB); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 356 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 357 | // Reset the hazard recognizer and anti-dep breaker. |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 358 | HazardRec->Reset(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 359 | if (AntiDepBreak) |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 360 | AntiDepBreak->StartBlock(BB); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /// Schedule - Schedule the instruction range using list scheduling. |
| 364 | /// |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 365 | void SchedulePostRATDList::schedule() { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 366 | // Build the scheduling graph. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 367 | buildSchedGraph(AA); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 368 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 369 | if (AntiDepBreak) { |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 370 | unsigned Broken = |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 371 | AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd, |
| 372 | EndIndex, DbgValues); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 373 | |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 374 | if (Broken != 0) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 375 | // We made changes. Update the dependency graph. |
| 376 | // Theoretically we could update the graph in place: |
| 377 | // When a live range is changed to use a different register, remove |
| 378 | // the def's anti-dependence *and* output-dependence edges due to |
| 379 | // that register, and add new anti-dependence and output-dependence |
| 380 | // edges based on the next live range of the register. |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 381 | ScheduleDAG::clearDAG(); |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 382 | buildSchedGraph(AA); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 383 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 384 | NumFixedAnti += Broken; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 388 | DEBUG(dbgs() << "********** List Scheduling **********\n"); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 389 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 390 | SUnits[su].dumpAll(this)); |
| 391 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 392 | AvailableQueue.initNodes(SUnits); |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 393 | ListScheduleTopDown(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 394 | AvailableQueue.releaseState(); |
| 395 | } |
| 396 | |
| 397 | /// Observe - Update liveness information to account for the current |
| 398 | /// instruction, which will not be scheduled. |
| 399 | /// |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 400 | void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 401 | if (AntiDepBreak) |
Andrew Trick | cf46b5a | 2012-03-07 23:00:52 +0000 | [diff] [blame] | 402 | AntiDepBreak->Observe(MI, Count, EndIndex); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /// FinishBlock - Clean up register live-range state. |
| 406 | /// |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 407 | void SchedulePostRATDList::finishBlock() { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 408 | if (AntiDepBreak) |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 409 | AntiDepBreak->FinishBlock(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 410 | |
| 411 | // Call the superclass. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 412 | ScheduleDAGInstrs::finishBlock(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 415 | //===----------------------------------------------------------------------===// |
| 416 | // Top-Down Scheduling |
| 417 | //===----------------------------------------------------------------------===// |
| 418 | |
| 419 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
Andrew Trick | ae692f2 | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 420 | /// the PendingQueue if the count reaches zero. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 421 | void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 422 | SUnit *SuccSU = SuccEdge->getSUnit(); |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 423 | |
Andrew Trick | cf6b613 | 2012-11-13 02:35:06 +0000 | [diff] [blame] | 424 | if (SuccEdge->isWeak()) { |
Andrew Trick | ae692f2 | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 425 | --SuccSU->WeakPredsLeft; |
| 426 | return; |
| 427 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 428 | #ifndef NDEBUG |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 429 | if (SuccSU->NumPredsLeft == 0) { |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 430 | dbgs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 431 | SuccSU->dump(this); |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 432 | dbgs() << " has been released too many times!\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 433 | llvm_unreachable(nullptr); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 434 | } |
| 435 | #endif |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 436 | --SuccSU->NumPredsLeft; |
| 437 | |
Andrew Trick | 89fd437 | 2011-05-06 18:14:32 +0000 | [diff] [blame] | 438 | // Standard scheduler algorithms will recompute the depth of the successor |
Andrew Trick | 15ab359 | 2011-05-06 17:09:08 +0000 | [diff] [blame] | 439 | // here as such: |
| 440 | // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); |
| 441 | // |
| 442 | // However, we lazily compute node depth instead. Note that |
| 443 | // ScheduleNodeTopDown has already updated the depth of this node which causes |
| 444 | // all descendents to be marked dirty. Setting the successor depth explicitly |
| 445 | // here would cause depth to be recomputed for all its ancestors. If the |
| 446 | // successor is not yet ready (because of a transitively redundant edge) then |
| 447 | // 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] | 448 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 449 | // If all the node's predecessors are scheduled, this node is ready |
| 450 | // to be scheduled. Ignore the special ExitSU node. |
| 451 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 452 | PendingQueue.push_back(SuccSU); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 456 | void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 457 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 458 | I != E; ++I) { |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 459 | ReleaseSucc(SU, &*I); |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 460 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 464 | /// count of its successors. If a successor pending count is zero, add it to |
| 465 | /// the Available queue. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 466 | void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 467 | DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: "); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 468 | DEBUG(SU->dump(this)); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 469 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 470 | Sequence.push_back(SU); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 471 | assert(CurCycle >= SU->getDepth() && |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 472 | "Node scheduled above its depth!"); |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 473 | SU->setDepthToAtLeast(CurCycle); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 474 | |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 475 | ReleaseSuccessors(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 476 | SU->isScheduled = true; |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 477 | AvailableQueue.scheduledNode(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 480 | /// emitNoop - Add a noop to the current instruction sequence. |
| 481 | void SchedulePostRATDList::emitNoop(unsigned CurCycle) { |
| 482 | DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n'); |
| 483 | HazardRec->EmitNoop(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 484 | Sequence.push_back(nullptr); // NULL here means noop |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 485 | ++NumNoops; |
| 486 | } |
| 487 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 488 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 489 | /// schedulers. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 490 | void SchedulePostRATDList::ListScheduleTopDown() { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 491 | unsigned CurCycle = 0; |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 492 | |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 493 | // We're scheduling top-down but we're visiting the regions in |
| 494 | // bottom-up order, so we don't know the hazards at the start of a |
| 495 | // region. So assume no hazards (this should usually be ok as most |
| 496 | // blocks are a single region). |
| 497 | HazardRec->Reset(); |
| 498 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 499 | // Release any successors of the special Entry node. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 500 | ReleaseSuccessors(&EntrySU); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 501 | |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 502 | // Add all leaves to Available queue. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 503 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 504 | // It is available if it has no predecessors. |
Andrew Trick | ae692f2 | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 505 | if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 506 | AvailableQueue.push(&SUnits[i]); |
| 507 | SUnits[i].isAvailable = true; |
| 508 | } |
| 509 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 510 | |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 511 | // In any cycle where we can't schedule any instructions, we must |
| 512 | // stall or emit a noop, depending on the target. |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 513 | bool CycleHasInsts = false; |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 514 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 515 | // While Available queue is not empty, grab the node with the highest |
| 516 | // priority. If it is not ready put it back. Schedule the node. |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 517 | std::vector<SUnit*> NotReady; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 518 | Sequence.reserve(SUnits.size()); |
| 519 | while (!AvailableQueue.empty() || !PendingQueue.empty()) { |
| 520 | // Check to see if any of the pending instructions are ready to issue. If |
| 521 | // so, add them to the available queue. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 522 | unsigned MinDepth = ~0u; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 523 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 524 | if (PendingQueue[i]->getDepth() <= CurCycle) { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 525 | AvailableQueue.push(PendingQueue[i]); |
| 526 | PendingQueue[i]->isAvailable = true; |
| 527 | PendingQueue[i] = PendingQueue.back(); |
| 528 | PendingQueue.pop_back(); |
| 529 | --i; --e; |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 530 | } else if (PendingQueue[i]->getDepth() < MinDepth) |
| 531 | MinDepth = PendingQueue[i]->getDepth(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 532 | } |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 533 | |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 534 | DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this)); |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 535 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 536 | SUnit *FoundSUnit = nullptr, *NotPreferredSUnit = nullptr; |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 537 | bool HasNoopHazards = false; |
| 538 | while (!AvailableQueue.empty()) { |
| 539 | SUnit *CurSUnit = AvailableQueue.pop(); |
| 540 | |
| 541 | ScheduleHazardRecognizer::HazardType HT = |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 542 | HazardRec->getHazardType(CurSUnit, 0/*no stalls*/); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 543 | if (HT == ScheduleHazardRecognizer::NoHazard) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 544 | if (HazardRec->ShouldPreferAnother(CurSUnit)) { |
| 545 | if (!NotPreferredSUnit) { |
| 546 | // If this is the first non-preferred node for this cycle, then |
| 547 | // record it and continue searching for a preferred node. If this |
| 548 | // is not the first non-preferred node, then treat it as though |
| 549 | // there had been a hazard. |
| 550 | NotPreferredSUnit = CurSUnit; |
| 551 | continue; |
| 552 | } |
| 553 | } else { |
| 554 | FoundSUnit = CurSUnit; |
| 555 | break; |
| 556 | } |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | // Remember if this is a noop hazard. |
| 560 | HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; |
| 561 | |
| 562 | NotReady.push_back(CurSUnit); |
| 563 | } |
| 564 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 565 | // If we have a non-preferred node, push it back onto the available list. |
| 566 | // If we did not find a preferred node, then schedule this first |
| 567 | // non-preferred node. |
| 568 | if (NotPreferredSUnit) { |
| 569 | if (!FoundSUnit) { |
| 570 | DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n"); |
| 571 | FoundSUnit = NotPreferredSUnit; |
| 572 | } else { |
| 573 | AvailableQueue.push(NotPreferredSUnit); |
| 574 | } |
| 575 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 576 | NotPreferredSUnit = nullptr; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 579 | // Add the nodes that aren't ready back onto the available list. |
| 580 | if (!NotReady.empty()) { |
| 581 | AvailableQueue.push_all(NotReady); |
| 582 | NotReady.clear(); |
| 583 | } |
| 584 | |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 585 | // If we found a node to schedule... |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 586 | if (FoundSUnit) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 587 | // If we need to emit noops prior to this instruction, then do so. |
| 588 | unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit); |
| 589 | for (unsigned i = 0; i != NumPreNoops; ++i) |
| 590 | emitNoop(CurCycle); |
| 591 | |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 592 | // ... schedule the node... |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 593 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 594 | HazardRec->EmitInstruction(FoundSUnit); |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 595 | CycleHasInsts = true; |
Andrew Trick | cf9aa28 | 2011-06-01 03:27:56 +0000 | [diff] [blame] | 596 | if (HazardRec->atIssueLimit()) { |
| 597 | DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n'); |
| 598 | HazardRec->AdvanceCycle(); |
| 599 | ++CurCycle; |
| 600 | CycleHasInsts = false; |
| 601 | } |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 602 | } else { |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 603 | if (CycleHasInsts) { |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 604 | DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n'); |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 605 | HazardRec->AdvanceCycle(); |
| 606 | } else if (!HasNoopHazards) { |
| 607 | // Otherwise, we have a pipeline stall, but no other problem, |
| 608 | // just advance the current cycle and try again. |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 609 | DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n'); |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 610 | HazardRec->AdvanceCycle(); |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 611 | ++NumStalls; |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 612 | } else { |
| 613 | // Otherwise, we have no instructions to issue and we have instructions |
| 614 | // that will fault if we don't do this right. This is the case for |
| 615 | // processors without pipeline interlocks and other cases. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 616 | emitNoop(CurCycle); |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 619 | ++CurCycle; |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 620 | CycleHasInsts = false; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | #ifndef NDEBUG |
Andrew Trick | 4c72720 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 625 | unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false); |
| 626 | unsigned Noops = 0; |
| 627 | for (unsigned i = 0, e = Sequence.size(); i != e; ++i) |
| 628 | if (!Sequence[i]) |
| 629 | ++Noops; |
| 630 | assert(Sequence.size() - Noops == ScheduledNodes && |
| 631 | "The number of nodes scheduled doesn't match the expected number!"); |
| 632 | #endif // NDEBUG |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 633 | } |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 634 | |
| 635 | // EmitSchedule - Emit the machine code in scheduled order. |
| 636 | void SchedulePostRATDList::EmitSchedule() { |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 637 | RegionBegin = RegionEnd; |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 638 | |
| 639 | // If first instruction was a DBG_VALUE then put it back. |
| 640 | if (FirstDbgValue) |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 641 | BB->splice(RegionEnd, BB, FirstDbgValue); |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 642 | |
| 643 | // Then re-insert them according to the given schedule. |
| 644 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 645 | if (SUnit *SU = Sequence[i]) |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 646 | BB->splice(RegionEnd, BB, SU->getInstr()); |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 647 | else |
| 648 | // Null SUnit* is a noop. |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 649 | TII->insertNoop(*BB, RegionEnd); |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 650 | |
| 651 | // Update the Begin iterator, as the first instruction in the block |
| 652 | // may have been scheduled later. |
| 653 | if (i == 0) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 654 | RegionBegin = std::prev(RegionEnd); |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // Reinsert any remaining debug_values. |
| 658 | for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator |
| 659 | DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 660 | std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI); |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 661 | MachineInstr *DbgValue = P.first; |
| 662 | MachineBasicBlock::iterator OrigPrivMI = P.second; |
| 663 | BB->splice(++OrigPrivMI, BB, DbgValue); |
| 664 | } |
| 665 | DbgValues.clear(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 666 | FirstDbgValue = nullptr; |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 667 | } |