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 | |
| 21 | #define DEBUG_TYPE "post-RA-sched" |
| 22 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | ed0881b | 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 | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/LatencyPriorityQueue.h" |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineDominators.h" |
David Goodwin | be3039e | 2009-10-01 19:45:32 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | f623e98 | 2012-12-20 18:08:06 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Andrew Trick | 05ff466 | 2012-06-06 20:29:31 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Andrew Trick | 9a0c583 | 2012-03-07 23:01:06 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/SchedulerRegistry.h" |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | f20236a | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 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 | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 49 | using namespace llvm; |
| 50 | |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 51 | STATISTIC(NumNoops, "Number of noops inserted"); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 52 | STATISTIC(NumStalls, "Number of pipeline stalls"); |
David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 53 | STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies"); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 54 | |
David Goodwin | 9a051a5 | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 55 | // Post-RA scheduling is enabled with |
Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 56 | // TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to |
David Goodwin | 9a051a5 | 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 | 1cc6dd9 | 2009-10-01 22:19:57 +0000 | [diff] [blame] | 61 | cl::init(false), cl::Hidden); |
David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 62 | static cl::opt<std::string> |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 63 | EnableAntiDepBreaking("break-anti-dependencies", |
David Goodwin | 8370485 | 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 | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 67 | |
David Goodwin | 7f65169 | 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 | 661ea98 | 2009-10-26 19:41:00 +0000 | [diff] [blame] | 78 | AntiDepBreaker::~AntiDepBreaker() { } |
| 79 | |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 80 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 81 | class PostRAScheduler : public MachineFunctionPass { |
Evan Cheng | 2d51c7c | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 82 | const TargetInstrInfo *TII; |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 83 | RegisterClassInfo RegClassInfo; |
Dan Gohman | 87b02d5 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 84 | |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 85 | public: |
| 86 | static char ID; |
Andrew Trick | df7e376 | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 87 | PostRAScheduler() : MachineFunctionPass(ID) {} |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 88 | |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 89 | void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 0402315 | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 90 | AU.setPreservesCFG(); |
Dan Gohman | 87b02d5 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 91 | AU.addRequired<AliasAnalysis>(); |
Andrew Trick | df7e376 | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 92 | AU.addRequired<TargetPassConfig>(); |
Dan Gohman | dddc1ac | 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 | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 100 | bool runOnMachineFunction(MachineFunction &Fn); |
| 101 | }; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 102 | char PostRAScheduler::ID = 0; |
| 103 | |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 104 | class SchedulePostRATDList : public ScheduleDAGInstrs { |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 105 | /// AvailableQueue - The priority queue to use for the available SUnits. |
Dan Gohman | 682a2d1 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 106 | /// |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 107 | LatencyPriorityQueue AvailableQueue; |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 108 | |
Dan Gohman | 60cb69e | 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 | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 115 | /// HazardRec - The hazard recognizer to use. |
| 116 | ScheduleHazardRecognizer *HazardRec; |
| 117 | |
David Goodwin | 8370485 | 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 | 87b02d5 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 121 | /// AA - AliasAnalysis for making memory reference queries. |
| 122 | AliasAnalysis *AA; |
| 123 | |
Benjamin Kramer | 796fd46 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 124 | /// LiveRegs - true if the register is live. |
| 125 | BitVector LiveRegs; |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 126 | |
Andrew Trick | 60cf03e | 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 | a53e101 | 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 | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 136 | public: |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 137 | SchedulePostRATDList( |
| 138 | MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT, |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 139 | AliasAnalysis *AA, const RegisterClassInfo&, |
Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 140 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode, |
Craig Topper | 760b134 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 141 | SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs); |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 142 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 143 | ~SchedulePostRATDList(); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 144 | |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 145 | /// startBlock - Initialize register live-range state for scheduling in |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 146 | /// this block. |
| 147 | /// |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 148 | void startBlock(MachineBasicBlock *BB); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 149 | |
Andrew Trick | a53e101 | 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 | 60cf03e | 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 | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 157 | unsigned regioninstrs); |
Andrew Trick | 60cf03e | 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 | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 162 | /// Schedule - Schedule the instruction range using list scheduling. |
| 163 | /// |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 164 | void schedule(); |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 165 | |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 166 | void EmitSchedule(); |
| 167 | |
Dan Gohman | 682a2d1 | 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 | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 173 | /// finishBlock - Clean up register live-range state. |
Dan Gohman | 682a2d1 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 174 | /// |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 175 | void finishBlock(); |
Dan Gohman | 682a2d1 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 176 | |
David Goodwin | 8370485 | 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 | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 182 | private: |
David Goodwin | 80a03cc | 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 | 6c08cfc | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 187 | void StartBlockForKills(MachineBasicBlock *BB); |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 188 | |
David Goodwin | a4c98a3 | 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 | edee68c | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 193 | |
| 194 | void dumpSchedule() const; |
Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame^] | 195 | void emitNoop(unsigned CurCycle); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 196 | }; |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 199 | char &llvm::PostRASchedulerID = PostRAScheduler::ID; |
| 200 | |
| 201 | INITIALIZE_PASS(PostRAScheduler, "post-RA-sched", |
| 202 | "Post RA top-down list latency scheduler", false, false) |
| 203 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 204 | SchedulePostRATDList::SchedulePostRATDList( |
| 205 | MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT, |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 206 | AliasAnalysis *AA, const RegisterClassInfo &RCI, |
Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 207 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode, |
Craig Topper | 760b134 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 208 | SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs) |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 209 | : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), AA(AA), |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 210 | LiveRegs(TRI->getNumRegs()), EndIndex(0) |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 211 | { |
| 212 | const TargetMachine &TM = MF.getTarget(); |
| 213 | const InstrItineraryData *InstrItins = TM.getInstrItineraryData(); |
| 214 | HazardRec = |
| 215 | TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this); |
Preston Gurd | 9a09147 | 2012-04-23 21:39:35 +0000 | [diff] [blame] | 216 | |
| 217 | assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE || |
| 218 | MRI.tracksLiveness()) && |
| 219 | "Live-ins must be accurate for anti-dependency breaking"); |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 220 | AntiDepBreak = |
Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 221 | ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ? |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 222 | (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) : |
Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 223 | ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ? |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 224 | (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL)); |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | SchedulePostRATDList::~SchedulePostRATDList() { |
| 228 | delete HazardRec; |
| 229 | delete AntiDepBreak; |
| 230 | } |
| 231 | |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 232 | /// Initialize state associated with the next scheduling region. |
| 233 | void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb, |
| 234 | MachineBasicBlock::iterator begin, |
| 235 | MachineBasicBlock::iterator end, |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 236 | unsigned regioninstrs) { |
| 237 | ScheduleDAGInstrs::enterRegion(bb, begin, end, regioninstrs); |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 238 | Sequence.clear(); |
| 239 | } |
| 240 | |
| 241 | /// Print the schedule before exiting the region. |
| 242 | void SchedulePostRATDList::exitRegion() { |
| 243 | DEBUG({ |
| 244 | dbgs() << "*** Final schedule ***\n"; |
| 245 | dumpSchedule(); |
| 246 | dbgs() << '\n'; |
| 247 | }); |
| 248 | ScheduleDAGInstrs::exitRegion(); |
| 249 | } |
| 250 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 251 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Andrew Trick | edee68c | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 252 | /// dumpSchedule - dump the scheduled Sequence. |
| 253 | void SchedulePostRATDList::dumpSchedule() const { |
| 254 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 255 | if (SUnit *SU = Sequence[i]) |
| 256 | SU->dump(this); |
| 257 | else |
| 258 | dbgs() << "**** NOOP ****\n"; |
| 259 | } |
| 260 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 261 | #endif |
Andrew Trick | edee68c | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 262 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 263 | bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { |
Evan Cheng | 2d51c7c | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 264 | TII = Fn.getTarget().getInstrInfo(); |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 265 | MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 266 | MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
| 267 | AliasAnalysis *AA = &getAnalysis<AliasAnalysis>(); |
Andrew Trick | df7e376 | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 268 | TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>(); |
| 269 | |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 270 | RegClassInfo.runOnMachineFunction(Fn); |
Dan Gohman | 26e9b89 | 2009-10-10 00:15:38 +0000 | [diff] [blame] | 271 | |
David Goodwin | 9a051a5 | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 272 | // Check for explicit enable/disable of post-ra scheduling. |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 273 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode = |
| 274 | TargetSubtargetInfo::ANTIDEP_NONE; |
Craig Topper | 760b134 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 275 | SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs; |
David Goodwin | 9a051a5 | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 276 | if (EnablePostRAScheduler.getPosition() > 0) { |
| 277 | if (!EnablePostRAScheduler) |
Evan Cheng | 8b61476 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 278 | return false; |
David Goodwin | 9a051a5 | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 279 | } else { |
Evan Cheng | 8b61476 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 280 | // Check that post-RA scheduling is enabled for this target. |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 281 | // This may upgrade the AntiDepMode. |
Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 282 | const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>(); |
Andrew Trick | df7e376 | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 283 | if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode, |
| 284 | CriticalPathRCs)) |
Evan Cheng | 8b61476 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 285 | return false; |
David Goodwin | 9a051a5 | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 286 | } |
David Goodwin | 17199b5 | 2009-09-30 00:10:16 +0000 | [diff] [blame] | 287 | |
David Goodwin | 02ad4cb | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 288 | // Check for antidep breaking override... |
| 289 | if (EnableAntiDepBreaking.getPosition() > 0) { |
Evan Cheng | 0d639a2 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 290 | AntiDepMode = (EnableAntiDepBreaking == "all") |
| 291 | ? TargetSubtargetInfo::ANTIDEP_ALL |
| 292 | : ((EnableAntiDepBreaking == "critical") |
| 293 | ? TargetSubtargetInfo::ANTIDEP_CRITICAL |
| 294 | : TargetSubtargetInfo::ANTIDEP_NONE); |
David Goodwin | 02ad4cb | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 295 | } |
| 296 | |
David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 297 | DEBUG(dbgs() << "PostRAScheduler\n"); |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 298 | |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 299 | SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode, |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 300 | CriticalPathRCs); |
Dan Gohman | 619ef48 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 301 | |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 302 | // Loop over all of the basic blocks |
| 303 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 304 | MBB != MBBe; ++MBB) { |
David Goodwin | 7f65169 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 305 | #ifndef NDEBUG |
| 306 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 307 | if (DebugDiv > 0) { |
| 308 | static int bbcnt = 0; |
| 309 | if (bbcnt++ % DebugDiv != DebugMod) |
| 310 | continue; |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 311 | dbgs() << "*** DEBUG scheduling " << Fn.getName() |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 312 | << ":BB#" << MBB->getNumber() << " ***\n"; |
David Goodwin | 7f65169 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 313 | } |
| 314 | #endif |
| 315 | |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 316 | // Initialize register live-range state for scheduling in this block. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 317 | Scheduler.startBlock(MBB); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 318 | |
Dan Gohman | 5f8a259 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 319 | // Schedule each sequence of instructions not interrupted by a label |
| 320 | // or anything else that effectively needs to shut down scheduling. |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 321 | MachineBasicBlock::iterator Current = MBB->end(); |
Dan Gohman | dfaf646 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 322 | unsigned Count = MBB->size(), CurrentCount = Count; |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 323 | for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) { |
Evan Cheng | 2d51c7c | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 324 | MachineInstr *MI = llvm::prior(I); |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 325 | --Count; |
Jakob Stoklund Olesen | a793a59 | 2012-02-23 17:54:21 +0000 | [diff] [blame] | 326 | // Calls are not scheduling boundaries before register allocation, but |
| 327 | // post-ra we don't gain anything by scheduling across calls since we |
| 328 | // don't need to worry about register pressure. |
| 329 | if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) { |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 330 | Scheduler.enterRegion(MBB, I, Current, CurrentCount - Count); |
| 331 | Scheduler.setEndIndex(CurrentCount); |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 332 | Scheduler.schedule(); |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 333 | Scheduler.exitRegion(); |
Dan Gohman | 25c1653 | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 334 | Scheduler.EmitSchedule(); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 335 | Current = MI; |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 336 | CurrentCount = Count; |
Dan Gohman | 64613ac | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 337 | Scheduler.Observe(MI, CurrentCount); |
Dan Gohman | 5f8a259 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 338 | } |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 339 | I = MI; |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 340 | if (MI->isBundle()) |
| 341 | Count -= MI->getBundleSize(); |
Dan Gohman | d564353 | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 342 | } |
Dan Gohman | dfaf646 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 343 | assert(Count == 0 && "Instruction count mismatch!"); |
Duncan Sands | be69d60 | 2009-03-11 09:04:34 +0000 | [diff] [blame] | 344 | assert((MBB->begin() == Current || CurrentCount != 0) && |
Dan Gohman | 64613ac | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 345 | "Instruction count mismatch!"); |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 346 | Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount); |
Andrew Trick | a53e101 | 2013-08-23 17:48:33 +0000 | [diff] [blame] | 347 | Scheduler.setEndIndex(CurrentCount); |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 348 | Scheduler.schedule(); |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 349 | Scheduler.exitRegion(); |
Dan Gohman | 25c1653 | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 350 | Scheduler.EmitSchedule(); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 351 | |
| 352 | // Clean up register live-range state. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 353 | Scheduler.finishBlock(); |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 354 | |
David Goodwin | 6c08cfc | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 355 | // Update register kills |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 356 | Scheduler.FixupKills(MBB); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 357 | } |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 358 | |
| 359 | return true; |
| 360 | } |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 361 | |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 362 | /// StartBlock - Initialize register live-range state for scheduling in |
| 363 | /// this block. |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 364 | /// |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 365 | void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) { |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 366 | // Call the superclass. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 367 | ScheduleDAGInstrs::startBlock(BB); |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 368 | |
David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 369 | // Reset the hazard recognizer and anti-dep breaker. |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 370 | HazardRec->Reset(); |
David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 371 | if (AntiDepBreak != NULL) |
| 372 | AntiDepBreak->StartBlock(BB); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /// Schedule - Schedule the instruction range using list scheduling. |
| 376 | /// |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 377 | void SchedulePostRATDList::schedule() { |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 378 | // Build the scheduling graph. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 379 | buildSchedGraph(AA); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 380 | |
David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 381 | if (AntiDepBreak != NULL) { |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 382 | unsigned Broken = |
Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 383 | AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd, |
| 384 | EndIndex, DbgValues); |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 385 | |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 386 | if (Broken != 0) { |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 387 | // We made changes. Update the dependency graph. |
| 388 | // Theoretically we could update the graph in place: |
| 389 | // When a live range is changed to use a different register, remove |
| 390 | // the def's anti-dependence *and* output-dependence edges due to |
| 391 | // that register, and add new anti-dependence and output-dependence |
| 392 | // edges based on the next live range of the register. |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 393 | ScheduleDAG::clearDAG(); |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 394 | buildSchedGraph(AA); |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 395 | |
David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 396 | NumFixedAnti += Broken; |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 400 | DEBUG(dbgs() << "********** List Scheduling **********\n"); |
David Goodwin | 6021b4d | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 401 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 402 | SUnits[su].dumpAll(this)); |
| 403 | |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 404 | AvailableQueue.initNodes(SUnits); |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 405 | ListScheduleTopDown(); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 406 | AvailableQueue.releaseState(); |
| 407 | } |
| 408 | |
| 409 | /// Observe - Update liveness information to account for the current |
| 410 | /// instruction, which will not be scheduled. |
| 411 | /// |
Dan Gohman | dfaf646 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 412 | void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) { |
David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 413 | if (AntiDepBreak != NULL) |
Andrew Trick | a316faa | 2012-03-07 23:00:52 +0000 | [diff] [blame] | 414 | AntiDepBreak->Observe(MI, Count, EndIndex); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /// FinishBlock - Clean up register live-range state. |
| 418 | /// |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 419 | void SchedulePostRATDList::finishBlock() { |
David Goodwin | 8370485 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 420 | if (AntiDepBreak != NULL) |
| 421 | AntiDepBreak->FinishBlock(); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 422 | |
| 423 | // Call the superclass. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 424 | ScheduleDAGInstrs::finishBlock(); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 425 | } |
| 426 | |
David Goodwin | 6c08cfc | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 427 | /// StartBlockForKills - Initialize register live-range state for updating kills |
| 428 | /// |
| 429 | void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) { |
Benjamin Kramer | 796fd46 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 430 | // Start with no live registers. |
| 431 | LiveRegs.reset(); |
David Goodwin | 6c08cfc | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 432 | |
Jakob Stoklund Olesen | c338679 | 2013-02-05 18:21:52 +0000 | [diff] [blame] | 433 | // Examine the live-in regs of all successors. |
| 434 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 435 | SE = BB->succ_end(); SI != SE; ++SI) { |
| 436 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 437 | E = (*SI)->livein_end(); I != E; ++I) { |
David Goodwin | 6c08cfc | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 438 | unsigned Reg = *I; |
Chad Rosier | abdb1d6 | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 439 | // Repeat, for reg and all subregs. |
| 440 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 441 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 442 | LiveRegs.set(*SubRegs); |
David Goodwin | 6c08cfc | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
David Goodwin | 6c08cfc | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 445 | } |
| 446 | |
David Goodwin | a4c98a3 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 447 | bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI, |
| 448 | MachineOperand &MO) { |
| 449 | // Setting kill flag... |
| 450 | if (!MO.isKill()) { |
| 451 | MO.setIsKill(true); |
| 452 | return false; |
| 453 | } |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 454 | |
David Goodwin | a4c98a3 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 455 | // If MO itself is live, clear the kill flag... |
Benjamin Kramer | 796fd46 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 456 | if (LiveRegs.test(MO.getReg())) { |
David Goodwin | a4c98a3 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 457 | MO.setIsKill(false); |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | // If any subreg of MO is live, then create an imp-def for that |
| 462 | // subreg and keep MO marked as killed. |
Benjamin Kramer | 3b008a3 | 2009-10-02 15:59:52 +0000 | [diff] [blame] | 463 | MO.setIsKill(false); |
David Goodwin | a4c98a3 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 464 | bool AllDead = true; |
| 465 | const unsigned SuperReg = MO.getReg(); |
Jakob Stoklund Olesen | f623e98 | 2012-12-20 18:08:06 +0000 | [diff] [blame] | 466 | MachineInstrBuilder MIB(MF, MI); |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 467 | for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 468 | if (LiveRegs.test(*SubRegs)) { |
Jakob Stoklund Olesen | f623e98 | 2012-12-20 18:08:06 +0000 | [diff] [blame] | 469 | MIB.addReg(*SubRegs, RegState::ImplicitDefine); |
David Goodwin | a4c98a3 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 470 | AllDead = false; |
| 471 | } |
| 472 | } |
| 473 | |
Dan Gohman | 682a2d1 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 474 | if(AllDead) |
Benjamin Kramer | 3b008a3 | 2009-10-02 15:59:52 +0000 | [diff] [blame] | 475 | MO.setIsKill(true); |
David Goodwin | a4c98a3 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 479 | /// FixupKills - Fix the register kill flags, they may have been made |
| 480 | /// incorrect by instruction reordering. |
| 481 | /// |
| 482 | void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) { |
David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 483 | DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n'); |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 484 | |
Benjamin Kramer | 21974b1 | 2012-02-23 18:28:32 +0000 | [diff] [blame] | 485 | BitVector killedRegs(TRI->getNumRegs()); |
David Goodwin | 6c08cfc | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 486 | |
| 487 | StartBlockForKills(MBB); |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 488 | |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 489 | // Examine block from end to start... |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 490 | unsigned Count = MBB->size(); |
| 491 | for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin(); |
| 492 | I != E; --Count) { |
| 493 | MachineInstr *MI = --I; |
Dale Johannesen | 2061c84 | 2010-03-05 00:02:59 +0000 | [diff] [blame] | 494 | if (MI->isDebugValue()) |
| 495 | continue; |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 496 | |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 497 | // Update liveness. Registers that are defed but not used in this |
| 498 | // instruction are now dead. Mark register and all subregs as they |
| 499 | // are completely defined. |
| 500 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 501 | MachineOperand &MO = MI->getOperand(i); |
Jakob Stoklund Olesen | 28d4803 | 2012-02-23 01:22:15 +0000 | [diff] [blame] | 502 | if (MO.isRegMask()) |
Benjamin Kramer | ef8bf39 | 2012-02-23 19:29:25 +0000 | [diff] [blame] | 503 | LiveRegs.clearBitsNotInMask(MO.getRegMask()); |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 504 | if (!MO.isReg()) continue; |
| 505 | unsigned Reg = MO.getReg(); |
| 506 | if (Reg == 0) continue; |
| 507 | if (!MO.isDef()) continue; |
| 508 | // Ignore two-addr defs. |
| 509 | if (MI->isRegTiedToUseOperand(i)) continue; |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 510 | |
Chad Rosier | abdb1d6 | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 511 | // Repeat for reg and all subregs. |
| 512 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 513 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 514 | LiveRegs.reset(*SubRegs); |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 515 | } |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 516 | |
David Goodwin | a4c98a3 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 517 | // Examine all used registers and set/clear kill flag. When a |
| 518 | // register is used multiple times we only set the kill flag on |
Andrew Trick | 811a2ef | 2013-10-16 18:30:23 +0000 | [diff] [blame] | 519 | // the first use. Don't set kill flags on undef operands. |
Benjamin Kramer | 21974b1 | 2012-02-23 18:28:32 +0000 | [diff] [blame] | 520 | killedRegs.reset(); |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 521 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 522 | MachineOperand &MO = MI->getOperand(i); |
Andrew Trick | 811a2ef | 2013-10-16 18:30:23 +0000 | [diff] [blame] | 523 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 524 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c30a9af | 2012-10-15 21:57:41 +0000 | [diff] [blame] | 525 | if ((Reg == 0) || MRI.isReserved(Reg)) continue; |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 526 | |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 527 | bool kill = false; |
Benjamin Kramer | 21974b1 | 2012-02-23 18:28:32 +0000 | [diff] [blame] | 528 | if (!killedRegs.test(Reg)) { |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 529 | kill = true; |
| 530 | // A register is not killed if any subregs are live... |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 531 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 532 | if (LiveRegs.test(*SubRegs)) { |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 533 | kill = false; |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // If subreg is not live, then register is killed if it became |
| 539 | // live in this instruction |
| 540 | if (kill) |
Benjamin Kramer | 796fd46 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 541 | kill = !LiveRegs.test(Reg); |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 542 | } |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 543 | |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 544 | if (MO.isKill() != kill) { |
David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 545 | DEBUG(dbgs() << "Fixing " << MO << " in "); |
Jakob Stoklund Olesen | 8392456 | 2009-12-03 01:49:56 +0000 | [diff] [blame] | 546 | // Warning: ToggleKillFlag may invalidate MO. |
| 547 | ToggleKillFlag(MI, MO); |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 548 | DEBUG(MI->dump()); |
| 549 | } |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 550 | |
Benjamin Kramer | 21974b1 | 2012-02-23 18:28:32 +0000 | [diff] [blame] | 551 | killedRegs.set(Reg); |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 552 | } |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 553 | |
David Goodwin | c898520 | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 554 | // Mark any used register (that is not using undef) and subregs as |
| 555 | // now live... |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 556 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 557 | MachineOperand &MO = MI->getOperand(i); |
David Goodwin | c898520 | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 558 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 559 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c30a9af | 2012-10-15 21:57:41 +0000 | [diff] [blame] | 560 | if ((Reg == 0) || MRI.isReserved(Reg)) continue; |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 561 | |
Chad Rosier | abdb1d6 | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 562 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 563 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 564 | LiveRegs.set(*SubRegs); |
David Goodwin | 7cb103d | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 565 | } |
David Goodwin | ae6bc82 | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 569 | //===----------------------------------------------------------------------===// |
| 570 | // Top-Down Scheduling |
| 571 | //===----------------------------------------------------------------------===// |
| 572 | |
| 573 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 574 | /// the PendingQueue if the count reaches zero. |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 575 | void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 576 | SUnit *SuccSU = SuccEdge->getSUnit(); |
Reid Kleckner | 8ff5c19 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 577 | |
Andrew Trick | 4b1f9e3 | 2012-11-13 02:35:06 +0000 | [diff] [blame] | 578 | if (SuccEdge->isWeak()) { |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 579 | --SuccSU->WeakPredsLeft; |
| 580 | return; |
| 581 | } |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 582 | #ifndef NDEBUG |
Reid Kleckner | 8ff5c19 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 583 | if (SuccSU->NumPredsLeft == 0) { |
David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 584 | dbgs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 585 | SuccSU->dump(this); |
David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 586 | dbgs() << " has been released too many times!\n"; |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 587 | llvm_unreachable(0); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 588 | } |
| 589 | #endif |
Reid Kleckner | 8ff5c19 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 590 | --SuccSU->NumPredsLeft; |
| 591 | |
Andrew Trick | 84f9ad9 | 2011-05-06 18:14:32 +0000 | [diff] [blame] | 592 | // Standard scheduler algorithms will recompute the depth of the successor |
Andrew Trick | aab77fe | 2011-05-06 17:09:08 +0000 | [diff] [blame] | 593 | // here as such: |
| 594 | // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); |
| 595 | // |
| 596 | // However, we lazily compute node depth instead. Note that |
| 597 | // ScheduleNodeTopDown has already updated the depth of this node which causes |
| 598 | // all descendents to be marked dirty. Setting the successor depth explicitly |
| 599 | // here would cause depth to be recomputed for all its ancestors. If the |
| 600 | // successor is not yet ready (because of a transitively redundant edge) then |
| 601 | // 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] | 602 | |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 603 | // If all the node's predecessors are scheduled, this node is ready |
| 604 | // to be scheduled. Ignore the special ExitSU node. |
| 605 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 606 | PendingQueue.push_back(SuccSU); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 610 | void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 611 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 612 | I != E; ++I) { |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 613 | ReleaseSucc(SU, &*I); |
David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 614 | } |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 618 | /// count of its successors. If a successor pending count is zero, add it to |
| 619 | /// the Available queue. |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 620 | void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 621 | DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: "); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 622 | DEBUG(SU->dump(this)); |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 623 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 624 | Sequence.push_back(SU); |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 625 | assert(CurCycle >= SU->getDepth() && |
David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 626 | "Node scheduled above its depth!"); |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 627 | SU->setDepthToAtLeast(CurCycle); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 628 | |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 629 | ReleaseSuccessors(SU); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 630 | SU->isScheduled = true; |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 631 | AvailableQueue.scheduledNode(SU); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame^] | 634 | /// emitNoop - Add a noop to the current instruction sequence. |
| 635 | void SchedulePostRATDList::emitNoop(unsigned CurCycle) { |
| 636 | DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n'); |
| 637 | HazardRec->EmitNoop(); |
| 638 | Sequence.push_back(0); // NULL here means noop |
| 639 | ++NumNoops; |
| 640 | } |
| 641 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 642 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 643 | /// schedulers. |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 644 | void SchedulePostRATDList::ListScheduleTopDown() { |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 645 | unsigned CurCycle = 0; |
Jim Grosbach | d772bde | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 646 | |
David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 647 | // We're scheduling top-down but we're visiting the regions in |
| 648 | // bottom-up order, so we don't know the hazards at the start of a |
| 649 | // region. So assume no hazards (this should usually be ok as most |
| 650 | // blocks are a single region). |
| 651 | HazardRec->Reset(); |
| 652 | |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 653 | // Release any successors of the special Entry node. |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 654 | ReleaseSuccessors(&EntrySU); |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 655 | |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 656 | // Add all leaves to Available queue. |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 657 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 658 | // It is available if it has no predecessors. |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 659 | if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) { |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 660 | AvailableQueue.push(&SUnits[i]); |
| 661 | SUnits[i].isAvailable = true; |
| 662 | } |
| 663 | } |
Dan Gohman | b954343 | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 664 | |
David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 665 | // In any cycle where we can't schedule any instructions, we must |
| 666 | // stall or emit a noop, depending on the target. |
Benjamin Kramer | e3c9d23 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 667 | bool CycleHasInsts = false; |
David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 668 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 669 | // While Available queue is not empty, grab the node with the highest |
| 670 | // priority. If it is not ready put it back. Schedule the node. |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 671 | std::vector<SUnit*> NotReady; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 672 | Sequence.reserve(SUnits.size()); |
| 673 | while (!AvailableQueue.empty() || !PendingQueue.empty()) { |
| 674 | // Check to see if any of the pending instructions are ready to issue. If |
| 675 | // so, add them to the available queue. |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 676 | unsigned MinDepth = ~0u; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 677 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 678 | if (PendingQueue[i]->getDepth() <= CurCycle) { |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 679 | AvailableQueue.push(PendingQueue[i]); |
| 680 | PendingQueue[i]->isAvailable = true; |
| 681 | PendingQueue[i] = PendingQueue.back(); |
| 682 | PendingQueue.pop_back(); |
| 683 | --i; --e; |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 684 | } else if (PendingQueue[i]->getDepth() < MinDepth) |
| 685 | MinDepth = PendingQueue[i]->getDepth(); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 686 | } |
David Goodwin | ebd694b | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 687 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 688 | DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this)); |
David Goodwin | ebd694b | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 689 | |
Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame^] | 690 | SUnit *FoundSUnit = 0, *NotPreferredSUnit = 0; |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 691 | bool HasNoopHazards = false; |
| 692 | while (!AvailableQueue.empty()) { |
| 693 | SUnit *CurSUnit = AvailableQueue.pop(); |
| 694 | |
| 695 | ScheduleHazardRecognizer::HazardType HT = |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 696 | HazardRec->getHazardType(CurSUnit, 0/*no stalls*/); |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 697 | if (HT == ScheduleHazardRecognizer::NoHazard) { |
Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame^] | 698 | if (HazardRec->ShouldPreferAnother(CurSUnit)) { |
| 699 | if (!NotPreferredSUnit) { |
| 700 | // If this is the first non-preferred node for this cycle, then |
| 701 | // record it and continue searching for a preferred node. If this |
| 702 | // is not the first non-preferred node, then treat it as though |
| 703 | // there had been a hazard. |
| 704 | NotPreferredSUnit = CurSUnit; |
| 705 | continue; |
| 706 | } |
| 707 | } else { |
| 708 | FoundSUnit = CurSUnit; |
| 709 | break; |
| 710 | } |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | // Remember if this is a noop hazard. |
| 714 | HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; |
| 715 | |
| 716 | NotReady.push_back(CurSUnit); |
| 717 | } |
| 718 | |
Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame^] | 719 | // If we have a non-preferred node, push it back onto the available list. |
| 720 | // If we did not find a preferred node, then schedule this first |
| 721 | // non-preferred node. |
| 722 | if (NotPreferredSUnit) { |
| 723 | if (!FoundSUnit) { |
| 724 | DEBUG(dbgs() << "*** Will schedule a non-preferred instruction...\n"); |
| 725 | FoundSUnit = NotPreferredSUnit; |
| 726 | } else { |
| 727 | AvailableQueue.push(NotPreferredSUnit); |
| 728 | } |
| 729 | |
| 730 | NotPreferredSUnit = 0; |
| 731 | } |
| 732 | |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 733 | // Add the nodes that aren't ready back onto the available list. |
| 734 | if (!NotReady.empty()) { |
| 735 | AvailableQueue.push_all(NotReady); |
| 736 | NotReady.clear(); |
| 737 | } |
| 738 | |
David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 739 | // If we found a node to schedule... |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 740 | if (FoundSUnit) { |
Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame^] | 741 | // If we need to emit noops prior to this instruction, then do so. |
| 742 | unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit); |
| 743 | for (unsigned i = 0; i != NumPreNoops; ++i) |
| 744 | emitNoop(CurCycle); |
| 745 | |
David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 746 | // ... schedule the node... |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 747 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 748 | HazardRec->EmitInstruction(FoundSUnit); |
Benjamin Kramer | e3c9d23 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 749 | CycleHasInsts = true; |
Andrew Trick | 18c9b37 | 2011-06-01 03:27:56 +0000 | [diff] [blame] | 750 | if (HazardRec->atIssueLimit()) { |
| 751 | DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n'); |
| 752 | HazardRec->AdvanceCycle(); |
| 753 | ++CurCycle; |
| 754 | CycleHasInsts = false; |
| 755 | } |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 756 | } else { |
Benjamin Kramer | e3c9d23 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 757 | if (CycleHasInsts) { |
David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 758 | DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n'); |
David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 759 | HazardRec->AdvanceCycle(); |
| 760 | } else if (!HasNoopHazards) { |
| 761 | // Otherwise, we have a pipeline stall, but no other problem, |
| 762 | // just advance the current cycle and try again. |
David Greene | aa8ce38 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 763 | DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n'); |
David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 764 | HazardRec->AdvanceCycle(); |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 765 | ++NumStalls; |
David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 766 | } else { |
| 767 | // Otherwise, we have no instructions to issue and we have instructions |
| 768 | // that will fault if we don't do this right. This is the case for |
| 769 | // processors without pipeline interlocks and other cases. |
Hal Finkel | 4fd3b1d | 2013-12-11 22:33:43 +0000 | [diff] [blame^] | 770 | emitNoop(CurCycle); |
David Goodwin | 1f8c7a7 | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Dan Gohman | ceac7c3 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 773 | ++CurCycle; |
Benjamin Kramer | e3c9d23 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 774 | CycleHasInsts = false; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
| 778 | #ifndef NDEBUG |
Andrew Trick | 46a5866 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 779 | unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false); |
| 780 | unsigned Noops = 0; |
| 781 | for (unsigned i = 0, e = Sequence.size(); i != e; ++i) |
| 782 | if (!Sequence[i]) |
| 783 | ++Noops; |
| 784 | assert(Sequence.size() - Noops == ScheduledNodes && |
| 785 | "The number of nodes scheduled doesn't match the expected number!"); |
| 786 | #endif // NDEBUG |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 787 | } |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 788 | |
| 789 | // EmitSchedule - Emit the machine code in scheduled order. |
| 790 | void SchedulePostRATDList::EmitSchedule() { |
Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 791 | RegionBegin = RegionEnd; |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 792 | |
| 793 | // If first instruction was a DBG_VALUE then put it back. |
| 794 | if (FirstDbgValue) |
Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 795 | BB->splice(RegionEnd, BB, FirstDbgValue); |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 796 | |
| 797 | // Then re-insert them according to the given schedule. |
| 798 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 799 | if (SUnit *SU = Sequence[i]) |
Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 800 | BB->splice(RegionEnd, BB, SU->getInstr()); |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 801 | else |
| 802 | // Null SUnit* is a noop. |
Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 803 | TII->insertNoop(*BB, RegionEnd); |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 804 | |
| 805 | // Update the Begin iterator, as the first instruction in the block |
| 806 | // may have been scheduled later. |
| 807 | if (i == 0) |
Andrew Trick | 8c207e4 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 808 | RegionBegin = prior(RegionEnd); |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | // Reinsert any remaining debug_values. |
| 812 | for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator |
| 813 | DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) { |
| 814 | std::pair<MachineInstr *, MachineInstr *> P = *prior(DI); |
| 815 | MachineInstr *DbgValue = P.first; |
| 816 | MachineBasicBlock::iterator OrigPrivMI = P.second; |
| 817 | BB->splice(++OrigPrivMI, BB, DbgValue); |
| 818 | } |
| 819 | DbgValues.clear(); |
| 820 | FirstDbgValue = NULL; |
| 821 | } |