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