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