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