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