| Dale Johannesen | 4dc35db | 2007-07-13 17:31:29 +0000 | [diff] [blame] | 1 | //===----- SchedulePostRAList.cpp - list scheduler ------------------------===// | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | f3ebc3f | 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 | 2182f06 | 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 |  | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "AggressiveAntiDepBreaker.h" | 
|  | 22 | #include "AntiDepBreaker.h" | 
|  | 23 | #include "CriticalAntiDepBreaker.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" | 
|  | 25 | #include "llvm/Analysis/AliasAnalysis.h" | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LatencyPriorityQueue.h" | 
| Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineDominators.h" | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunctionPass.h" | 
| Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineLoopInfo.h" | 
| Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
| Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Passes.h" | 
| Andrew Trick | 05ff466 | 2012-06-06 20:29:31 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/RegisterClassInfo.h" | 
| Andrew Trick | 9a0c583 | 2012-03-07 23:01:06 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/SchedulerRegistry.h" | 
| David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/TargetInstrInfo.h" | 
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/TargetLowering.h" | 
| Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/TargetPassConfig.h" | 
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/TargetRegisterInfo.h" | 
|  | 40 | #include "llvm/CodeGen/TargetSubtargetInfo.h" | 
| David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 41 | #include "llvm/Support/CommandLine.h" | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Debug.h" | 
| Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ErrorHandling.h" | 
| David Goodwin | f20236a | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 44 | #include "llvm/Support/raw_ostream.h" | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 45 | using namespace llvm; | 
|  | 46 |  | 
| Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 47 | #define DEBUG_TYPE "post-RA-sched" | 
|  | 48 |  | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 49 | STATISTIC(NumNoops, "Number of noops inserted"); | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 50 | STATISTIC(NumStalls, "Number of pipeline stalls"); | 
| David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 51 | STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies"); | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 52 |  | 
| David Goodwin | 9a051a5 | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 53 | // Post-RA scheduling is enabled with | 
| Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 54 | // TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to | 
| David Goodwin | 9a051a5 | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 55 | // override the target. | 
|  | 56 | static cl::opt<bool> | 
|  | 57 | EnablePostRAScheduler("post-RA-scheduler", | 
|  | 58 | cl::desc("Enable scheduling after register allocation"), | 
| David Goodwin | 1cc6dd9 | 2009-10-01 22:19:57 +0000 | [diff] [blame] | 59 | cl::init(false), cl::Hidden); | 
| David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 60 | static cl::opt<std::string> | 
| Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 61 | EnableAntiDepBreaking("break-anti-dependencies", | 
| David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 62 | cl::desc("Break post-RA scheduling anti-dependencies: " | 
|  | 63 | "\"critical\", \"all\", or \"none\""), | 
|  | 64 | cl::init("none"), cl::Hidden); | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 65 |  | 
| David Goodwin | 7f65169 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 66 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod | 
|  | 67 | static cl::opt<int> | 
|  | 68 | DebugDiv("postra-sched-debugdiv", | 
|  | 69 | cl::desc("Debug control MBBs that are scheduled"), | 
|  | 70 | cl::init(0), cl::Hidden); | 
|  | 71 | static cl::opt<int> | 
|  | 72 | DebugMod("postra-sched-debugmod", | 
|  | 73 | cl::desc("Debug control MBBs that are scheduled"), | 
|  | 74 | cl::init(0), cl::Hidden); | 
|  | 75 |  | 
| David Goodwin | 661ea98 | 2009-10-26 19:41:00 +0000 | [diff] [blame] | 76 | AntiDepBreaker::~AntiDepBreaker() { } | 
|  | 77 |  | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 78 | namespace { | 
| Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 79 | class PostRAScheduler : public MachineFunctionPass { | 
| Evan Cheng | 2d51c7c | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 80 | const TargetInstrInfo *TII; | 
| Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 81 | RegisterClassInfo RegClassInfo; | 
| Dan Gohman | 87b02d5 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 82 |  | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 83 | public: | 
|  | 84 | static char ID; | 
| Andrew Trick | df7e376 | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 85 | PostRAScheduler() : MachineFunctionPass(ID) {} | 
| Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 86 |  | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 87 | void getAnalysisUsage(AnalysisUsage &AU) const override { | 
| Dan Gohman | 0402315 | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 88 | AU.setPreservesCFG(); | 
| Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 89 | AU.addRequired<AAResultsWrapperPass>(); | 
| Andrew Trick | df7e376 | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 90 | AU.addRequired<TargetPassConfig>(); | 
| Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 91 | AU.addRequired<MachineDominatorTree>(); | 
|  | 92 | AU.addPreserved<MachineDominatorTree>(); | 
|  | 93 | AU.addRequired<MachineLoopInfo>(); | 
|  | 94 | AU.addPreserved<MachineLoopInfo>(); | 
|  | 95 | MachineFunctionPass::getAnalysisUsage(AU); | 
|  | 96 | } | 
|  | 97 |  | 
| Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 98 | MachineFunctionProperties getRequiredProperties() const override { | 
|  | 99 | return MachineFunctionProperties().set( | 
| Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 100 | MachineFunctionProperties::Property::NoVRegs); | 
| Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 103 | bool runOnMachineFunction(MachineFunction &Fn) override; | 
| NAKAMURA Takumi | f51a34e | 2014-10-29 15:23:11 +0000 | [diff] [blame] | 104 |  | 
| Mitch Bodart | 6453501 | 2016-05-19 16:40:49 +0000 | [diff] [blame] | 105 | private: | 
| Sanjay Patel | a2f658d | 2014-07-15 22:39:58 +0000 | [diff] [blame] | 106 | bool enablePostRAScheduler( | 
|  | 107 | const TargetSubtargetInfo &ST, CodeGenOpt::Level OptLevel, | 
|  | 108 | TargetSubtargetInfo::AntiDepBreakMode &Mode, | 
|  | 109 | TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const; | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 110 | }; | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 111 | char PostRAScheduler::ID = 0; | 
|  | 112 |  | 
| Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 113 | class SchedulePostRATDList : public ScheduleDAGInstrs { | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 114 | /// AvailableQueue - The priority queue to use for the available SUnits. | 
| Dan Gohman | 682a2d1 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 115 | /// | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 116 | LatencyPriorityQueue AvailableQueue; | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 117 |  | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 118 | /// PendingQueue - This contains all of the instructions whose operands have | 
|  | 119 | /// been issued, but their results are not ready yet (due to the latency of | 
|  | 120 | /// the operation).  Once the operands becomes available, the instruction is | 
|  | 121 | /// added to the AvailableQueue. | 
|  | 122 | std::vector<SUnit*> PendingQueue; | 
|  | 123 |  | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 124 | /// HazardRec - The hazard recognizer to use. | 
|  | 125 | ScheduleHazardRecognizer *HazardRec; | 
|  | 126 |  | 
| David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 127 | /// AntiDepBreak - Anti-dependence breaking object, or NULL if none | 
|  | 128 | AntiDepBreaker *AntiDepBreak; | 
|  | 129 |  | 
| Dan Gohman | 87b02d5 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 130 | /// AA - AliasAnalysis for making memory reference queries. | 
|  | 131 | AliasAnalysis *AA; | 
|  | 132 |  | 
| Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 133 | /// The schedule. Null SUnit*'s represent noop instructions. | 
|  | 134 | std::vector<SUnit*> Sequence; | 
|  | 135 |  | 
| Krzysztof Parzyszek | 5c61d11 | 2016-03-05 15:45:23 +0000 | [diff] [blame] | 136 | /// Ordered list of DAG postprocessing steps. | 
|  | 137 | std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations; | 
|  | 138 |  | 
| Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 139 | /// The index in BB of RegionEnd. | 
|  | 140 | /// | 
|  | 141 | /// This is the instruction number from the top of the current block, not | 
|  | 142 | /// the SlotIndex. It is only used by the AntiDepBreaker. | 
|  | 143 | unsigned EndIndex; | 
|  | 144 |  | 
| Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 145 | public: | 
| Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 146 | SchedulePostRATDList( | 
| Alexey Samsonov | ea0aee6 | 2014-08-20 20:57:26 +0000 | [diff] [blame] | 147 | MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA, | 
|  | 148 | const RegisterClassInfo &, | 
|  | 149 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode, | 
|  | 150 | SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs); | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 151 |  | 
| Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 152 | ~SchedulePostRATDList() override; | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 153 |  | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 154 | /// startBlock - Initialize register live-range state for scheduling in | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 155 | /// this block. | 
|  | 156 | /// | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 157 | void startBlock(MachineBasicBlock *BB) override; | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 158 |  | 
| Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 159 | // Set the index of RegionEnd within the current BB. | 
|  | 160 | void setEndIndex(unsigned EndIdx) { EndIndex = EndIdx; } | 
|  | 161 |  | 
| Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 162 | /// Initialize the scheduler state for the next scheduling region. | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 163 | void enterRegion(MachineBasicBlock *bb, | 
|  | 164 | MachineBasicBlock::iterator begin, | 
|  | 165 | MachineBasicBlock::iterator end, | 
|  | 166 | unsigned regioninstrs) override; | 
| Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 167 |  | 
|  | 168 | /// Notify that the scheduler has finished scheduling the current region. | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 169 | void exitRegion() override; | 
| Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 170 |  | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 171 | /// Schedule - Schedule the instruction range using list scheduling. | 
|  | 172 | /// | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 173 | void schedule() override; | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 174 |  | 
| Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 175 | void EmitSchedule(); | 
|  | 176 |  | 
| Dan Gohman | 682a2d1 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 177 | /// Observe - Update liveness information to account for the current | 
|  | 178 | /// instruction, which will not be scheduled. | 
|  | 179 | /// | 
| Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 180 | void Observe(MachineInstr &MI, unsigned Count); | 
| Dan Gohman | 682a2d1 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 181 |  | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 182 | /// finishBlock - Clean up register live-range state. | 
| Dan Gohman | 682a2d1 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 183 | /// | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 184 | void finishBlock() override; | 
| Dan Gohman | 682a2d1 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 185 |  | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 186 | private: | 
| Krzysztof Parzyszek | 5c61d11 | 2016-03-05 15:45:23 +0000 | [diff] [blame] | 187 | /// Apply each ScheduleDAGMutation step in order. | 
|  | 188 | void postprocessDAG(); | 
|  | 189 |  | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 190 | void ReleaseSucc(SUnit *SU, SDep *SuccEdge); | 
|  | 191 | void ReleaseSuccessors(SUnit *SU); | 
|  | 192 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); | 
|  | 193 | void ListScheduleTopDown(); | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 194 |  | 
| Andrew Trick | edee68c | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 195 | void dumpSchedule() const; | 
| Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame] | 196 | void emitNoop(unsigned CurCycle); | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 197 | }; | 
| Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 198 | } | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 199 |  | 
| Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 200 | char &llvm::PostRASchedulerID = PostRAScheduler::ID; | 
|  | 201 |  | 
| Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 202 | INITIALIZE_PASS(PostRAScheduler, DEBUG_TYPE, | 
| Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 203 | "Post RA top-down list latency scheduler", false, false) | 
|  | 204 |  | 
| Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 205 | SchedulePostRATDList::SchedulePostRATDList( | 
| Alexey Samsonov | ea0aee6 | 2014-08-20 20:57:26 +0000 | [diff] [blame] | 206 | MachineFunction &MF, MachineLoopInfo &MLI, AliasAnalysis *AA, | 
|  | 207 | const RegisterClassInfo &RCI, | 
|  | 208 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode, | 
|  | 209 | SmallVectorImpl<const TargetRegisterClass *> &CriticalPathRCs) | 
| Matthias Braun | 93563e7 | 2015-11-03 01:53:29 +0000 | [diff] [blame] | 210 | : ScheduleDAGInstrs(MF, &MLI), AA(AA), EndIndex(0) { | 
| Andrew Trick | 6b104f8 | 2013-12-28 21:56:55 +0000 | [diff] [blame] | 211 |  | 
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 212 | const InstrItineraryData *InstrItins = | 
| Eric Christopher | b66367a | 2014-10-14 07:17:23 +0000 | [diff] [blame] | 213 | MF.getSubtarget().getInstrItineraryData(); | 
| Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 214 | HazardRec = | 
| Eric Christopher | b66367a | 2014-10-14 07:17:23 +0000 | [diff] [blame] | 215 | MF.getSubtarget().getInstrInfo()->CreateTargetPostRAHazardRecognizer( | 
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 216 | InstrItins, this); | 
| Krzysztof Parzyszek | 5c61d11 | 2016-03-05 15:45:23 +0000 | [diff] [blame] | 217 | MF.getSubtarget().getPostRAMutations(Mutations); | 
| Preston Gurd | 9a09147 | 2012-04-23 21:39:35 +0000 | [diff] [blame] | 218 |  | 
|  | 219 | assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE || | 
|  | 220 | MRI.tracksLiveness()) && | 
|  | 221 | "Live-ins must be accurate for anti-dependency breaking"); | 
| Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 222 | AntiDepBreak = | 
| Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 223 | ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ? | 
| Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 224 | (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) : | 
| Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 225 | ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ? | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 226 | (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : nullptr)); | 
| Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 227 | } | 
|  | 228 |  | 
|  | 229 | SchedulePostRATDList::~SchedulePostRATDList() { | 
|  | 230 | delete HazardRec; | 
|  | 231 | delete AntiDepBreak; | 
|  | 232 | } | 
|  | 233 |  | 
| Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 234 | /// Initialize state associated with the next scheduling region. | 
|  | 235 | void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb, | 
|  | 236 | MachineBasicBlock::iterator begin, | 
|  | 237 | MachineBasicBlock::iterator end, | 
| Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 238 | unsigned regioninstrs) { | 
|  | 239 | ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs); | 
| Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 240 | Sequence.clear(); | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | /// Print the schedule before exiting the region. | 
|  | 244 | void SchedulePostRATDList::exitRegion() { | 
|  | 245 | DEBUG({ | 
|  | 246 | dbgs() << "*** Final schedule ***\n"; | 
|  | 247 | dumpSchedule(); | 
|  | 248 | dbgs() << '\n'; | 
|  | 249 | }); | 
|  | 250 | ScheduleDAGInstrs::exitRegion(); | 
|  | 251 | } | 
|  | 252 |  | 
| Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 253 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | 
| Andrew Trick | edee68c | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 254 | /// dumpSchedule - dump the scheduled Sequence. | 
| Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 255 | LLVM_DUMP_METHOD void SchedulePostRATDList::dumpSchedule() const { | 
| Andrew Trick | edee68c | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 256 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { | 
|  | 257 | if (SUnit *SU = Sequence[i]) | 
|  | 258 | SU->dump(this); | 
|  | 259 | else | 
|  | 260 | dbgs() << "**** NOOP ****\n"; | 
|  | 261 | } | 
|  | 262 | } | 
| Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 263 | #endif | 
| Andrew Trick | edee68c | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 264 |  | 
| Sanjay Patel | a2f658d | 2014-07-15 22:39:58 +0000 | [diff] [blame] | 265 | bool PostRAScheduler::enablePostRAScheduler( | 
|  | 266 | const TargetSubtargetInfo &ST, | 
|  | 267 | CodeGenOpt::Level OptLevel, | 
|  | 268 | TargetSubtargetInfo::AntiDepBreakMode &Mode, | 
|  | 269 | TargetSubtargetInfo::RegClassVector &CriticalPathRCs) const { | 
|  | 270 | Mode = ST.getAntiDepBreakMode(); | 
|  | 271 | ST.getCriticalPathRCs(CriticalPathRCs); | 
| Mitch Bodart | 6453501 | 2016-05-19 16:40:49 +0000 | [diff] [blame] | 272 |  | 
|  | 273 | // Check for explicit enable/disable of post-ra scheduling. | 
|  | 274 | if (EnablePostRAScheduler.getPosition() > 0) | 
|  | 275 | return EnablePostRAScheduler; | 
|  | 276 |  | 
| Matthias Braun | 39a2afc | 2015-06-13 03:42:16 +0000 | [diff] [blame] | 277 | return ST.enablePostRAScheduler() && | 
| Sanjay Patel | a2f658d | 2014-07-15 22:39:58 +0000 | [diff] [blame] | 278 | OptLevel >= ST.getOptLevelToEnablePostRAScheduler(); | 
|  | 279 | } | 
|  | 280 |  | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 281 | bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { | 
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 282 | if (skipFunction(Fn.getFunction())) | 
| Paul Robinson | 7c99ec5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 283 | return false; | 
|  | 284 |  | 
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 285 | TII = Fn.getSubtarget().getInstrInfo(); | 
| Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 286 | MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); | 
| Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 287 | AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); | 
| Andrew Trick | df7e376 | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 288 | TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>(); | 
|  | 289 |  | 
| Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 290 | RegClassInfo.runOnMachineFunction(Fn); | 
| Dan Gohman | 26e9b89 | 2009-10-10 00:15:38 +0000 | [diff] [blame] | 291 |  | 
| Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 292 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode = | 
|  | 293 | TargetSubtargetInfo::ANTIDEP_NONE; | 
| Craig Topper | 760b134 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 294 | SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs; | 
| Mitch Bodart | 6453501 | 2016-05-19 16:40:49 +0000 | [diff] [blame] | 295 |  | 
|  | 296 | // Check that post-RA scheduling is enabled for this target. | 
|  | 297 | // This may upgrade the AntiDepMode. | 
|  | 298 | if (!enablePostRAScheduler(Fn.getSubtarget(), PassConfig->getOptLevel(), | 
|  | 299 | AntiDepMode, CriticalPathRCs)) | 
|  | 300 | return false; | 
| David Goodwin | 17199b5 | 2009-09-30 00:10:16 +0000 | [diff] [blame] | 301 |  | 
| David Goodwin | 02ad4cb | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 302 | // Check for antidep breaking override... | 
|  | 303 | if (EnableAntiDepBreaking.getPosition() > 0) { | 
| Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 304 | AntiDepMode = (EnableAntiDepBreaking == "all") | 
|  | 305 | ? TargetSubtargetInfo::ANTIDEP_ALL | 
|  | 306 | : ((EnableAntiDepBreaking == "critical") | 
|  | 307 | ? TargetSubtargetInfo::ANTIDEP_CRITICAL | 
|  | 308 | : TargetSubtargetInfo::ANTIDEP_NONE); | 
| David Goodwin | 02ad4cb | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 309 | } | 
|  | 310 |  | 
| David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 311 | DEBUG(dbgs() << "PostRAScheduler\n"); | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 312 |  | 
| Alexey Samsonov | ea0aee6 | 2014-08-20 20:57:26 +0000 | [diff] [blame] | 313 | SchedulePostRATDList Scheduler(Fn, MLI, AA, RegClassInfo, AntiDepMode, | 
| Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 314 | CriticalPathRCs); | 
| Dan Gohman | 619ef48 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 315 |  | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 316 | // Loop over all of the basic blocks | 
| Duncan P. N. Exon Smith | 1ff4098 | 2015-10-09 21:05:00 +0000 | [diff] [blame] | 317 | for (auto &MBB : Fn) { | 
| David Goodwin | 7f65169 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 318 | #ifndef NDEBUG | 
|  | 319 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod | 
|  | 320 | if (DebugDiv > 0) { | 
|  | 321 | static int bbcnt = 0; | 
|  | 322 | if (bbcnt++ % DebugDiv != DebugMod) | 
|  | 323 | continue; | 
| Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 324 | dbgs() << "*** DEBUG scheduling " << Fn.getName() << ":" | 
|  | 325 | << printMBBReference(MBB) << " ***\n"; | 
| David Goodwin | 7f65169 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 326 | } | 
|  | 327 | #endif | 
|  | 328 |  | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 329 | // Initialize register live-range state for scheduling in this block. | 
| Duncan P. N. Exon Smith | 1ff4098 | 2015-10-09 21:05:00 +0000 | [diff] [blame] | 330 | Scheduler.startBlock(&MBB); | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 331 |  | 
| Dan Gohman | 5f8a259 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 332 | // Schedule each sequence of instructions not interrupted by a label | 
|  | 333 | // or anything else that effectively needs to shut down scheduling. | 
| Duncan P. N. Exon Smith | 1ff4098 | 2015-10-09 21:05:00 +0000 | [diff] [blame] | 334 | MachineBasicBlock::iterator Current = MBB.end(); | 
|  | 335 | unsigned Count = MBB.size(), CurrentCount = Count; | 
|  | 336 | for (MachineBasicBlock::iterator I = Current; I != MBB.begin();) { | 
| Duncan P. N. Exon Smith | 762c5ca | 2016-07-01 01:18:53 +0000 | [diff] [blame] | 337 | MachineInstr &MI = *std::prev(I); | 
| Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 338 | --Count; | 
| Jakob Stoklund Olesen | a793a59 | 2012-02-23 17:54:21 +0000 | [diff] [blame] | 339 | // Calls are not scheduling boundaries before register allocation, but | 
|  | 340 | // post-ra we don't gain anything by scheduling across calls since we | 
|  | 341 | // don't need to worry about register pressure. | 
| Duncan P. N. Exon Smith | 762c5ca | 2016-07-01 01:18:53 +0000 | [diff] [blame] | 342 | if (MI.isCall() || TII->isSchedulingBoundary(MI, &MBB, Fn)) { | 
| Duncan P. N. Exon Smith | 1ff4098 | 2015-10-09 21:05:00 +0000 | [diff] [blame] | 343 | Scheduler.enterRegion(&MBB, I, Current, CurrentCount - Count); | 
| Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 344 | Scheduler.setEndIndex(CurrentCount); | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 345 | Scheduler.schedule(); | 
| Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 346 | Scheduler.exitRegion(); | 
| Dan Gohman | 25c1653 | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 347 | Scheduler.EmitSchedule(); | 
| Duncan P. N. Exon Smith | 762c5ca | 2016-07-01 01:18:53 +0000 | [diff] [blame] | 348 | Current = &MI; | 
| Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 349 | CurrentCount = Count; | 
| Duncan P. N. Exon Smith | 762c5ca | 2016-07-01 01:18:53 +0000 | [diff] [blame] | 350 | Scheduler.Observe(MI, CurrentCount); | 
| Dan Gohman | 5f8a259 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 351 | } | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 352 | I = MI; | 
| Duncan P. N. Exon Smith | 762c5ca | 2016-07-01 01:18:53 +0000 | [diff] [blame] | 353 | if (MI.isBundle()) | 
|  | 354 | Count -= MI.getBundleSize(); | 
| Dan Gohman | d564353 | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 355 | } | 
| Dan Gohman | dfaf646 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 356 | assert(Count == 0 && "Instruction count mismatch!"); | 
| Duncan P. N. Exon Smith | 1ff4098 | 2015-10-09 21:05:00 +0000 | [diff] [blame] | 357 | assert((MBB.begin() == Current || CurrentCount != 0) && | 
| Dan Gohman | 64613ac | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 358 | "Instruction count mismatch!"); | 
| Duncan P. N. Exon Smith | 1ff4098 | 2015-10-09 21:05:00 +0000 | [diff] [blame] | 359 | Scheduler.enterRegion(&MBB, MBB.begin(), Current, CurrentCount); | 
| Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 360 | Scheduler.setEndIndex(CurrentCount); | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 361 | Scheduler.schedule(); | 
| Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 362 | Scheduler.exitRegion(); | 
| Dan Gohman | 25c1653 | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 363 | Scheduler.EmitSchedule(); | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 364 |  | 
|  | 365 | // Clean up register live-range state. | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 366 | Scheduler.finishBlock(); | 
| David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 367 |  | 
| David Goodwin | 6c08cfc | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 368 | // Update register kills | 
| Matthias Braun | 868bbd4 | 2017-05-27 02:50:50 +0000 | [diff] [blame] | 369 | Scheduler.fixupKills(MBB); | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 370 | } | 
| Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 371 |  | 
|  | 372 | return true; | 
|  | 373 | } | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 374 |  | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 375 | /// StartBlock - Initialize register live-range state for scheduling in | 
|  | 376 | /// this block. | 
| Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 377 | /// | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 378 | void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) { | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 379 | // Call the superclass. | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 380 | ScheduleDAGInstrs::startBlock(BB); | 
| Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 381 |  | 
| David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 382 | // Reset the hazard recognizer and anti-dep breaker. | 
| David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 383 | HazardRec->Reset(); | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 384 | if (AntiDepBreak) | 
| David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 385 | AntiDepBreak->StartBlock(BB); | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 386 | } | 
|  | 387 |  | 
|  | 388 | /// Schedule - Schedule the instruction range using list scheduling. | 
|  | 389 | /// | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 390 | void SchedulePostRATDList::schedule() { | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 391 | // Build the scheduling graph. | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 392 | buildSchedGraph(AA); | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 393 |  | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 394 | if (AntiDepBreak) { | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 395 | unsigned Broken = | 
| Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 396 | AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd, | 
|  | 397 | EndIndex, DbgValues); | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 398 |  | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 399 | if (Broken != 0) { | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 400 | // We made changes. Update the dependency graph. | 
|  | 401 | // Theoretically we could update the graph in place: | 
|  | 402 | // When a live range is changed to use a different register, remove | 
|  | 403 | // the def's anti-dependence *and* output-dependence edges due to | 
|  | 404 | // that register, and add new anti-dependence and output-dependence | 
|  | 405 | // edges based on the next live range of the register. | 
| Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 406 | ScheduleDAG::clearDAG(); | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 407 | buildSchedGraph(AA); | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 408 |  | 
| David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 409 | NumFixedAnti += Broken; | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 410 | } | 
|  | 411 | } | 
|  | 412 |  | 
| Krzysztof Parzyszek | cd99e36 | 2016-03-08 16:54:20 +0000 | [diff] [blame] | 413 | postprocessDAG(); | 
|  | 414 |  | 
| David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 415 | DEBUG(dbgs() << "********** List Scheduling **********\n"); | 
| Matthias Braun | 9198c67 | 2015-11-06 20:59:02 +0000 | [diff] [blame] | 416 | DEBUG( | 
|  | 417 | for (const SUnit &SU : SUnits) { | 
|  | 418 | SU.dumpAll(this); | 
|  | 419 | dbgs() << '\n'; | 
|  | 420 | } | 
|  | 421 | ); | 
| David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 422 |  | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 423 | AvailableQueue.initNodes(SUnits); | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 424 | ListScheduleTopDown(); | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 425 | AvailableQueue.releaseState(); | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | /// Observe - Update liveness information to account for the current | 
|  | 429 | /// instruction, which will not be scheduled. | 
|  | 430 | /// | 
| Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 431 | void SchedulePostRATDList::Observe(MachineInstr &MI, unsigned Count) { | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 432 | if (AntiDepBreak) | 
| Andrew Trick | a316faa | 2012-03-07 23:00:52 +0000 | [diff] [blame] | 433 | AntiDepBreak->Observe(MI, Count, EndIndex); | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 434 | } | 
|  | 435 |  | 
|  | 436 | /// FinishBlock - Clean up register live-range state. | 
|  | 437 | /// | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 438 | void SchedulePostRATDList::finishBlock() { | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 439 | if (AntiDepBreak) | 
| David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 440 | AntiDepBreak->FinishBlock(); | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 441 |  | 
|  | 442 | // Call the superclass. | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 443 | ScheduleDAGInstrs::finishBlock(); | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 444 | } | 
|  | 445 |  | 
| Krzysztof Parzyszek | 5c61d11 | 2016-03-05 15:45:23 +0000 | [diff] [blame] | 446 | /// Apply each ScheduleDAGMutation step in order. | 
|  | 447 | void SchedulePostRATDList::postprocessDAG() { | 
|  | 448 | for (auto &M : Mutations) | 
|  | 449 | M->apply(this); | 
|  | 450 | } | 
|  | 451 |  | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 452 | //===----------------------------------------------------------------------===// | 
|  | 453 | //  Top-Down Scheduling | 
|  | 454 | //===----------------------------------------------------------------------===// | 
|  | 455 |  | 
|  | 456 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to | 
| Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 457 | /// the PendingQueue if the count reaches zero. | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 458 | void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { | 
| Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 459 | SUnit *SuccSU = SuccEdge->getSUnit(); | 
| Reid Kleckner | 8ff5c19 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 460 |  | 
| Andrew Trick | 4b1f9e3 | 2012-11-13 02:35:06 +0000 | [diff] [blame] | 461 | if (SuccEdge->isWeak()) { | 
| Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 462 | --SuccSU->WeakPredsLeft; | 
|  | 463 | return; | 
|  | 464 | } | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 465 | #ifndef NDEBUG | 
| Reid Kleckner | 8ff5c19 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 466 | if (SuccSU->NumPredsLeft == 0) { | 
| David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 467 | dbgs() << "*** Scheduling failed! ***\n"; | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 468 | SuccSU->dump(this); | 
| David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 469 | dbgs() << " has been released too many times!\n"; | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 470 | llvm_unreachable(nullptr); | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 471 | } | 
|  | 472 | #endif | 
| Reid Kleckner | 8ff5c19 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 473 | --SuccSU->NumPredsLeft; | 
|  | 474 |  | 
| Andrew Trick | 84f9ad9 | 2011-05-06 18:14:32 +0000 | [diff] [blame] | 475 | // Standard scheduler algorithms will recompute the depth of the successor | 
| Andrew Trick | aab77fe | 2011-05-06 17:09:08 +0000 | [diff] [blame] | 476 | // here as such: | 
|  | 477 | //   SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); | 
|  | 478 | // | 
|  | 479 | // However, we lazily compute node depth instead. Note that | 
|  | 480 | // ScheduleNodeTopDown has already updated the depth of this node which causes | 
|  | 481 | // all descendents to be marked dirty. Setting the successor depth explicitly | 
|  | 482 | // here would cause depth to be recomputed for all its ancestors. If the | 
|  | 483 | // successor is not yet ready (because of a transitively redundant edge) then | 
|  | 484 | // this causes depth computation to be quadratic in the size of the DAG. | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 485 |  | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 486 | // If all the node's predecessors are scheduled, this node is ready | 
|  | 487 | // to be scheduled. Ignore the special ExitSU node. | 
|  | 488 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 489 | PendingQueue.push_back(SuccSU); | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 490 | } | 
|  | 491 |  | 
|  | 492 | /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 493 | void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 494 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); | 
| David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 495 | I != E; ++I) { | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 496 | ReleaseSucc(SU, &*I); | 
| David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 497 | } | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 498 | } | 
|  | 499 |  | 
|  | 500 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending | 
|  | 501 | /// count of its successors. If a successor pending count is zero, add it to | 
|  | 502 | /// the Available queue. | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 503 | void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { | 
| David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 504 | DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: "); | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 505 | DEBUG(SU->dump(this)); | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 506 |  | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 507 | Sequence.push_back(SU); | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 508 | assert(CurCycle >= SU->getDepth() && | 
| David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 509 | "Node scheduled above its depth!"); | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 510 | SU->setDepthToAtLeast(CurCycle); | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 511 |  | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 512 | ReleaseSuccessors(SU); | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 513 | SU->isScheduled = true; | 
| Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 514 | AvailableQueue.scheduledNode(SU); | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 515 | } | 
|  | 516 |  | 
| Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame] | 517 | /// emitNoop - Add a noop to the current instruction sequence. | 
|  | 518 | void SchedulePostRATDList::emitNoop(unsigned CurCycle) { | 
|  | 519 | DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n'); | 
|  | 520 | HazardRec->EmitNoop(); | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 521 | Sequence.push_back(nullptr);   // NULL here means noop | 
| Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame] | 522 | ++NumNoops; | 
|  | 523 | } | 
|  | 524 |  | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 525 | /// ListScheduleTopDown - The main loop of list scheduling for top-down | 
|  | 526 | /// schedulers. | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 527 | void SchedulePostRATDList::ListScheduleTopDown() { | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 528 | unsigned CurCycle = 0; | 
| Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 529 |  | 
| David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 530 | // We're scheduling top-down but we're visiting the regions in | 
|  | 531 | // bottom-up order, so we don't know the hazards at the start of a | 
|  | 532 | // region. So assume no hazards (this should usually be ok as most | 
|  | 533 | // blocks are a single region). | 
|  | 534 | HazardRec->Reset(); | 
|  | 535 |  | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 536 | // Release any successors of the special Entry node. | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 537 | ReleaseSuccessors(&EntrySU); | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 538 |  | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 539 | // Add all leaves to Available queue. | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 540 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { | 
|  | 541 | // It is available if it has no predecessors. | 
| Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 542 | if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) { | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 543 | AvailableQueue.push(&SUnits[i]); | 
|  | 544 | SUnits[i].isAvailable = true; | 
|  | 545 | } | 
|  | 546 | } | 
| Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 547 |  | 
| David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 548 | // In any cycle where we can't schedule any instructions, we must | 
|  | 549 | // stall or emit a noop, depending on the target. | 
| Benjamin Kramer | e3c9d23 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 550 | bool CycleHasInsts = false; | 
| David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 551 |  | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 552 | // While Available queue is not empty, grab the node with the highest | 
|  | 553 | // priority. If it is not ready put it back.  Schedule the node. | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 554 | std::vector<SUnit*> NotReady; | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 555 | Sequence.reserve(SUnits.size()); | 
|  | 556 | while (!AvailableQueue.empty() || !PendingQueue.empty()) { | 
|  | 557 | // Check to see if any of the pending instructions are ready to issue.  If | 
|  | 558 | // so, add them to the available queue. | 
| Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 559 | unsigned MinDepth = ~0u; | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 560 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 561 | if (PendingQueue[i]->getDepth() <= CurCycle) { | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 562 | AvailableQueue.push(PendingQueue[i]); | 
|  | 563 | PendingQueue[i]->isAvailable = true; | 
|  | 564 | PendingQueue[i] = PendingQueue.back(); | 
|  | 565 | PendingQueue.pop_back(); | 
|  | 566 | --i; --e; | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 567 | } else if (PendingQueue[i]->getDepth() < MinDepth) | 
|  | 568 | MinDepth = PendingQueue[i]->getDepth(); | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 569 | } | 
| David Goodwin | ebd694b | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 570 |  | 
| Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 571 | DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this)); | 
| David Goodwin | ebd694b | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 572 |  | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 573 | SUnit *FoundSUnit = nullptr, *NotPreferredSUnit = nullptr; | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 574 | bool HasNoopHazards = false; | 
|  | 575 | while (!AvailableQueue.empty()) { | 
|  | 576 | SUnit *CurSUnit = AvailableQueue.pop(); | 
|  | 577 |  | 
|  | 578 | ScheduleHazardRecognizer::HazardType HT = | 
| Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 579 | HazardRec->getHazardType(CurSUnit, 0/*no stalls*/); | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 580 | if (HT == ScheduleHazardRecognizer::NoHazard) { | 
| Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame] | 581 | if (HazardRec->ShouldPreferAnother(CurSUnit)) { | 
|  | 582 | if (!NotPreferredSUnit) { | 
| NAKAMURA Takumi | f51a34e | 2014-10-29 15:23:11 +0000 | [diff] [blame] | 583 | // If this is the first non-preferred node for this cycle, then | 
|  | 584 | // record it and continue searching for a preferred node. If this | 
|  | 585 | // is not the first non-preferred node, then treat it as though | 
|  | 586 | // there had been a hazard. | 
| Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame] | 587 | NotPreferredSUnit = CurSUnit; | 
|  | 588 | continue; | 
|  | 589 | } | 
|  | 590 | } else { | 
|  | 591 | FoundSUnit = CurSUnit; | 
|  | 592 | break; | 
|  | 593 | } | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 594 | } | 
|  | 595 |  | 
|  | 596 | // Remember if this is a noop hazard. | 
|  | 597 | HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; | 
|  | 598 |  | 
|  | 599 | NotReady.push_back(CurSUnit); | 
|  | 600 | } | 
|  | 601 |  | 
| Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame] | 602 | // If we have a non-preferred node, push it back onto the available list. | 
|  | 603 | // If we did not find a preferred node, then schedule this first | 
|  | 604 | // non-preferred node. | 
|  | 605 | if (NotPreferredSUnit) { | 
|  | 606 | if (!FoundSUnit) { | 
|  | 607 | DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n"); | 
|  | 608 | FoundSUnit = NotPreferredSUnit; | 
|  | 609 | } else { | 
|  | 610 | AvailableQueue.push(NotPreferredSUnit); | 
|  | 611 | } | 
|  | 612 |  | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 613 | NotPreferredSUnit = nullptr; | 
| Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame] | 614 | } | 
|  | 615 |  | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 616 | // Add the nodes that aren't ready back onto the available list. | 
|  | 617 | if (!NotReady.empty()) { | 
|  | 618 | AvailableQueue.push_all(NotReady); | 
|  | 619 | NotReady.clear(); | 
|  | 620 | } | 
|  | 621 |  | 
| David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 622 | // If we found a node to schedule... | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 623 | if (FoundSUnit) { | 
| Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame] | 624 | // If we need to emit noops prior to this instruction, then do so. | 
|  | 625 | unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit); | 
|  | 626 | for (unsigned i = 0; i != NumPreNoops; ++i) | 
|  | 627 | emitNoop(CurCycle); | 
|  | 628 |  | 
| David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 629 | // ... schedule the node... | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 630 | ScheduleNodeTopDown(FoundSUnit, CurCycle); | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 631 | HazardRec->EmitInstruction(FoundSUnit); | 
| Benjamin Kramer | e3c9d23 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 632 | CycleHasInsts = true; | 
| Andrew Trick | 18c9b37 | 2011-06-01 03:27:56 +0000 | [diff] [blame] | 633 | if (HazardRec->atIssueLimit()) { | 
|  | 634 | DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n'); | 
|  | 635 | HazardRec->AdvanceCycle(); | 
|  | 636 | ++CurCycle; | 
|  | 637 | CycleHasInsts = false; | 
|  | 638 | } | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 639 | } else { | 
| Benjamin Kramer | e3c9d23 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 640 | if (CycleHasInsts) { | 
| David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 641 | DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n'); | 
| David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 642 | HazardRec->AdvanceCycle(); | 
|  | 643 | } else if (!HasNoopHazards) { | 
|  | 644 | // Otherwise, we have a pipeline stall, but no other problem, | 
|  | 645 | // just advance the current cycle and try again. | 
| David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 646 | DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n'); | 
| David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 647 | HazardRec->AdvanceCycle(); | 
| David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 648 | ++NumStalls; | 
| David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 649 | } else { | 
|  | 650 | // Otherwise, we have no instructions to issue and we have instructions | 
|  | 651 | // that will fault if we don't do this right.  This is the case for | 
|  | 652 | // processors without pipeline interlocks and other cases. | 
| Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame] | 653 | emitNoop(CurCycle); | 
| David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 654 | } | 
|  | 655 |  | 
| Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 656 | ++CurCycle; | 
| Benjamin Kramer | e3c9d23 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 657 | CycleHasInsts = false; | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 658 | } | 
|  | 659 | } | 
|  | 660 |  | 
|  | 661 | #ifndef NDEBUG | 
| Andrew Trick | 46a5866 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 662 | unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false); | 
|  | 663 | unsigned Noops = 0; | 
|  | 664 | for (unsigned i = 0, e = Sequence.size(); i != e; ++i) | 
|  | 665 | if (!Sequence[i]) | 
|  | 666 | ++Noops; | 
|  | 667 | assert(Sequence.size() - Noops == ScheduledNodes && | 
|  | 668 | "The number of nodes scheduled doesn't match the expected number!"); | 
|  | 669 | #endif // NDEBUG | 
| Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 670 | } | 
| Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 671 |  | 
|  | 672 | // EmitSchedule - Emit the machine code in scheduled order. | 
|  | 673 | void SchedulePostRATDList::EmitSchedule() { | 
| Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 674 | RegionBegin = RegionEnd; | 
| Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 675 |  | 
|  | 676 | // If first instruction was a DBG_VALUE then put it back. | 
|  | 677 | if (FirstDbgValue) | 
| Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 678 | BB->splice(RegionEnd, BB, FirstDbgValue); | 
| Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 679 |  | 
|  | 680 | // Then re-insert them according to the given schedule. | 
|  | 681 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { | 
|  | 682 | if (SUnit *SU = Sequence[i]) | 
| Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 683 | BB->splice(RegionEnd, BB, SU->getInstr()); | 
| Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 684 | else | 
|  | 685 | // Null SUnit* is a noop. | 
| Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 686 | TII->insertNoop(*BB, RegionEnd); | 
| Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 687 |  | 
|  | 688 | // Update the Begin iterator, as the first instruction in the block | 
|  | 689 | // may have been scheduled later. | 
|  | 690 | if (i == 0) | 
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 691 | RegionBegin = std::prev(RegionEnd); | 
| Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 692 | } | 
|  | 693 |  | 
|  | 694 | // Reinsert any remaining debug_values. | 
|  | 695 | for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator | 
|  | 696 | DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) { | 
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 697 | std::pair<MachineInstr *, MachineInstr *> P = *std::prev(DI); | 
| Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 698 | MachineInstr *DbgValue = P.first; | 
|  | 699 | MachineBasicBlock::iterator OrigPrivMI = P.second; | 
|  | 700 | BB->splice(++OrigPrivMI, BB, DbgValue); | 
|  | 701 | } | 
|  | 702 | DbgValues.clear(); | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 703 | FirstDbgValue = nullptr; | 
| Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 704 | } |