blob: 905f6932681e929396b9c601a1c6b901e461f5b3 [file] [log] [blame]
Dale Johannesen72f15962007-07-13 17:31:29 +00001//===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00007//
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 Goodwind94a4e52009-08-10 15:55:25 +000022#include "ExactHazardRecognizer.h"
23#include "SimpleHazardRecognizer.h"
Dan Gohman6dc75fe2009-02-06 17:12:10 +000024#include "ScheduleDAGInstrs.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000025#include "llvm/CodeGen/Passes.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000026#include "llvm/CodeGen/LatencyPriorityQueue.h"
27#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3f237442008-12-16 03:25:46 +000028#include "llvm/CodeGen/MachineDominators.h"
David Goodwinc7951f82009-10-01 19:45:32 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman3f237442008-12-16 03:25:46 +000031#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman21d90032008-11-25 00:52:40 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2836c282009-01-16 01:33:36 +000033#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Dan Gohmana70dca12009-10-09 23:27:56 +000034#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohmanbed353d2009-02-10 23:29:38 +000035#include "llvm/Target/TargetLowering.h"
Dan Gohman79ce2762009-01-15 19:20:50 +000036#include "llvm/Target/TargetMachine.h"
Dan Gohman21d90032008-11-25 00:52:40 +000037#include "llvm/Target/TargetInstrInfo.h"
38#include "llvm/Target/TargetRegisterInfo.h"
David Goodwin0dad89f2009-09-30 00:10:16 +000039#include "llvm/Target/TargetSubtarget.h"
Chris Lattner459525d2008-01-14 19:00:06 +000040#include "llvm/Support/Compiler.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000041#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000042#include "llvm/Support/ErrorHandling.h"
David Goodwin3a5f0d42009-08-11 01:44:26 +000043#include "llvm/Support/raw_ostream.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000044#include "llvm/ADT/Statistic.h"
Dan Gohman21d90032008-11-25 00:52:40 +000045#include <map>
David Goodwin88a589c2009-08-25 17:03:05 +000046#include <set>
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000047using namespace llvm;
48
Dan Gohman2836c282009-01-16 01:33:36 +000049STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman343f0c02008-11-19 23:18:57 +000050STATISTIC(NumStalls, "Number of pipeline stalls");
51
David Goodwin471850a2009-10-01 21:46:35 +000052// Post-RA scheduling is enabled with
53// TargetSubtarget.enablePostRAScheduler(). This flag can be used to
54// override the target.
55static cl::opt<bool>
56EnablePostRAScheduler("post-RA-scheduler",
57 cl::desc("Enable scheduling after register allocation"),
David Goodwin9843a932009-10-01 22:19:57 +000058 cl::init(false), cl::Hidden);
Dan Gohmanc1ae8c92009-10-21 01:44:44 +000059static cl::opt<bool>
Dan Gohman21d90032008-11-25 00:52:40 +000060EnableAntiDepBreaking("break-anti-dependencies",
Dan Gohmanc1ae8c92009-10-21 01:44:44 +000061 cl::desc("Break post-RA scheduling anti-dependencies"),
62 cl::init(true), cl::Hidden);
Dan Gohman2836c282009-01-16 01:33:36 +000063static cl::opt<bool>
64EnablePostRAHazardAvoidance("avoid-hazards",
David Goodwind94a4e52009-08-10 15:55:25 +000065 cl::desc("Enable exact hazard avoidance"),
David Goodwin5e411782009-09-03 22:15:25 +000066 cl::init(true), cl::Hidden);
Dan Gohman2836c282009-01-16 01:33:36 +000067
David Goodwin1f152282009-09-01 18:34:03 +000068// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
69static cl::opt<int>
70DebugDiv("postra-sched-debugdiv",
71 cl::desc("Debug control MBBs that are scheduled"),
72 cl::init(0), cl::Hidden);
73static cl::opt<int>
74DebugMod("postra-sched-debugmod",
75 cl::desc("Debug control MBBs that are scheduled"),
76 cl::init(0), cl::Hidden);
77
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000078namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000079 class PostRAScheduler : public MachineFunctionPass {
Dan Gohmana70dca12009-10-09 23:27:56 +000080 AliasAnalysis *AA;
Evan Chengfa163542009-10-16 21:06:15 +000081 CodeGenOpt::Level OptLevel;
Dan Gohmana70dca12009-10-09 23:27:56 +000082
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000083 public:
84 static char ID;
Evan Chengfa163542009-10-16 21:06:15 +000085 PostRAScheduler(CodeGenOpt::Level ol) :
86 MachineFunctionPass(&ID), OptLevel(ol) {}
Dan Gohman21d90032008-11-25 00:52:40 +000087
Dan Gohman3f237442008-12-16 03:25:46 +000088 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000089 AU.setPreservesCFG();
Dan Gohmana70dca12009-10-09 23:27:56 +000090 AU.addRequired<AliasAnalysis>();
Dan Gohman3f237442008-12-16 03:25:46 +000091 AU.addRequired<MachineDominatorTree>();
92 AU.addPreserved<MachineDominatorTree>();
93 AU.addRequired<MachineLoopInfo>();
94 AU.addPreserved<MachineLoopInfo>();
95 MachineFunctionPass::getAnalysisUsage(AU);
96 }
97
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000098 const char *getPassName() const {
Dan Gohman21d90032008-11-25 00:52:40 +000099 return "Post RA top-down list latency scheduler";
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000100 }
101
102 bool runOnMachineFunction(MachineFunction &Fn);
103 };
Dan Gohman343f0c02008-11-19 23:18:57 +0000104 char PostRAScheduler::ID = 0;
105
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000106 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +0000107 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000108 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000109 LatencyPriorityQueue AvailableQueue;
110
111 /// PendingQueue - This contains all of the instructions whose operands have
112 /// been issued, but their results are not ready yet (due to the latency of
113 /// the operation). Once the operands becomes available, the instruction is
114 /// added to the AvailableQueue.
115 std::vector<SUnit*> PendingQueue;
116
Dan Gohman21d90032008-11-25 00:52:40 +0000117 /// Topo - A topological ordering for SUnits.
118 ScheduleDAGTopologicalSort Topo;
Dan Gohman343f0c02008-11-19 23:18:57 +0000119
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000120 /// AllocatableSet - The set of allocatable registers.
121 /// We'll be ignoring anti-dependencies on non-allocatable registers,
122 /// because they may not be safe to break.
123 const BitVector AllocatableSet;
124
Dan Gohman2836c282009-01-16 01:33:36 +0000125 /// HazardRec - The hazard recognizer to use.
126 ScheduleHazardRecognizer *HazardRec;
127
Dan Gohmana70dca12009-10-09 23:27:56 +0000128 /// AA - AliasAnalysis for making memory reference queries.
129 AliasAnalysis *AA;
130
David Goodwin4c3715c2009-10-22 23:19:17 +0000131 /// AntiDepMode - Anti-dependence breaking mode
132 TargetSubtarget::AntiDepBreakMode AntiDepMode;
133
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000134 /// Classes - For live regs that are only used in one register class in a
135 /// live range, the register class. If the register is not live, the
136 /// corresponding value is null. If the register is live but used in
137 /// multiple register classes, the corresponding value is -1 casted to a
138 /// pointer.
139 const TargetRegisterClass *
140 Classes[TargetRegisterInfo::FirstVirtualRegister];
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000141
142 /// RegRegs - Map registers to all their references within a live range.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000143 std::multimap<unsigned, MachineOperand *> RegRefs;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000144
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000145 /// KillIndices - The index of the most recent kill (proceding bottom-up),
146 /// or ~0u if the register is not live.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000147 unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
148
Evan Cheng714e8bc2009-10-01 08:26:23 +0000149 /// DefIndices - The index of the most recent complete def (proceding bottom
150 /// up), or ~0u if the register is live.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000151 unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister];
152
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000153 /// KeepRegs - A set of registers which are live and cannot be changed to
154 /// break anti-dependencies.
155 SmallSet<unsigned, 4> KeepRegs;
156
Dan Gohman21d90032008-11-25 00:52:40 +0000157 public:
Dan Gohman79ce2762009-01-15 19:20:50 +0000158 SchedulePostRATDList(MachineFunction &MF,
Dan Gohman3f237442008-12-16 03:25:46 +0000159 const MachineLoopInfo &MLI,
Dan Gohman2836c282009-01-16 01:33:36 +0000160 const MachineDominatorTree &MDT,
Dan Gohmana70dca12009-10-09 23:27:56 +0000161 ScheduleHazardRecognizer *HR,
David Goodwin4c3715c2009-10-22 23:19:17 +0000162 AliasAnalysis *aa,
163 TargetSubtarget::AntiDepBreakMode adm)
Dan Gohman79ce2762009-01-15 19:20:50 +0000164 : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits),
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000165 AllocatableSet(TRI->getAllocatableSet(MF)),
David Goodwin4c3715c2009-10-22 23:19:17 +0000166 HazardRec(HR), AA(aa), AntiDepMode(adm) {}
Dan Gohman2836c282009-01-16 01:33:36 +0000167
168 ~SchedulePostRATDList() {
169 delete HazardRec;
170 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000171
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000172 /// StartBlock - Initialize register live-range state for scheduling in
173 /// this block.
174 ///
175 void StartBlock(MachineBasicBlock *BB);
176
177 /// Schedule - Schedule the instruction range using list scheduling.
178 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000179 void Schedule();
David Goodwin88a589c2009-08-25 17:03:05 +0000180
181 /// FixupKills - Fix register kill flags that have been made
182 /// invalid due to scheduling
183 ///
184 void FixupKills(MachineBasicBlock *MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000185
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000186 /// Observe - Update liveness information to account for the current
187 /// instruction, which will not be scheduled.
188 ///
189 void Observe(MachineInstr *MI, unsigned Count);
190
191 /// FinishBlock - Clean up register live-range state.
192 ///
193 void FinishBlock();
194
Dan Gohman343f0c02008-11-19 23:18:57 +0000195 private:
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000196 void PrescanInstruction(MachineInstr *MI);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000197 void ScanInstruction(MachineInstr *MI, unsigned Count);
Dan Gohman54e4c362008-12-09 22:54:47 +0000198 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000199 void ReleaseSuccessors(SUnit *SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000200 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
201 void ListScheduleTopDown();
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000202 bool BreakAntiDependencies();
203 unsigned findSuitableFreeRegister(unsigned AntiDepReg,
204 unsigned LastNewReg,
205 const TargetRegisterClass *);
David Goodwin5e411782009-09-03 22:15:25 +0000206 void StartBlockForKills(MachineBasicBlock *BB);
David Goodwin8f909342009-09-23 16:35:25 +0000207
208 // ToggleKillFlag - Toggle a register operand kill flag. Other
209 // adjustments may be made to the instruction if necessary. Return
210 // true if the operand has been deleted, false if not.
211 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Dan Gohman343f0c02008-11-19 23:18:57 +0000212 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000213}
214
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000215/// isSchedulingBoundary - Test if the given instruction should be
216/// considered a scheduling boundary. This primarily includes labels
217/// and terminators.
218///
219static bool isSchedulingBoundary(const MachineInstr *MI,
220 const MachineFunction &MF) {
221 // Terminators and labels can't be scheduled around.
222 if (MI->getDesc().isTerminator() || MI->isLabel())
223 return true;
224
Dan Gohmanbed353d2009-02-10 23:29:38 +0000225 // Don't attempt to schedule around any instruction that modifies
226 // a stack-oriented pointer, as it's unlikely to be profitable. This
227 // saves compile time, because it doesn't require every single
228 // stack slot reference to depend on the instruction that does the
229 // modification.
230 const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
231 if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore()))
232 return true;
233
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000234 return false;
235}
236
Dan Gohman343f0c02008-11-19 23:18:57 +0000237bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000238 AA = &getAnalysis<AliasAnalysis>();
239
David Goodwin471850a2009-10-01 21:46:35 +0000240 // Check for explicit enable/disable of post-ra scheduling.
David Goodwin4c3715c2009-10-22 23:19:17 +0000241 TargetSubtarget::AntiDepBreakMode AntiDepMode = TargetSubtarget::ANTIDEP_NONE;
David Goodwin471850a2009-10-01 21:46:35 +0000242 if (EnablePostRAScheduler.getPosition() > 0) {
243 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000244 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000245 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000246 // Check that post-RA scheduling is enabled for this target.
David Goodwin471850a2009-10-01 21:46:35 +0000247 const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>();
David Goodwin4c3715c2009-10-22 23:19:17 +0000248 if (!ST.enablePostRAScheduler(OptLevel, AntiDepMode))
Evan Chengc83da2f92009-10-16 06:10:34 +0000249 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000250 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000251
David Goodwin4c3715c2009-10-22 23:19:17 +0000252 // Check for antidep breaking override...
253 if (EnableAntiDepBreaking.getPosition() > 0) {
254 AntiDepMode = (EnableAntiDepBreaking) ?
255 TargetSubtarget::ANTIDEP_CRITICAL : TargetSubtarget::ANTIDEP_NONE;
256 }
257
David Goodwin3a5f0d42009-08-11 01:44:26 +0000258 DEBUG(errs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000259
Dan Gohman3f237442008-12-16 03:25:46 +0000260 const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
261 const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
David Goodwind94a4e52009-08-10 15:55:25 +0000262 const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData();
Dan Gohman2836c282009-01-16 01:33:36 +0000263 ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ?
David Goodwind94a4e52009-08-10 15:55:25 +0000264 (ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) :
265 (ScheduleHazardRecognizer *)new SimpleHazardRecognizer();
Dan Gohman3f237442008-12-16 03:25:46 +0000266
David Goodwin4c3715c2009-10-22 23:19:17 +0000267 SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR, AA, AntiDepMode);
Dan Gohman79ce2762009-01-15 19:20:50 +0000268
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000269 // Loop over all of the basic blocks
270 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000271 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000272#ifndef NDEBUG
273 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
274 if (DebugDiv > 0) {
275 static int bbcnt = 0;
276 if (bbcnt++ % DebugDiv != DebugMod)
277 continue;
278 errs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() <<
279 ":MBB ID#" << MBB->getNumber() << " ***\n";
280 }
281#endif
282
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000283 // Initialize register live-range state for scheduling in this block.
284 Scheduler.StartBlock(MBB);
285
Dan Gohmanf7119392009-01-16 22:10:20 +0000286 // Schedule each sequence of instructions not interrupted by a label
287 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000288 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000289 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000290 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
291 MachineInstr *MI = prior(I);
292 if (isSchedulingBoundary(MI, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000293 Scheduler.Run(MBB, I, Current, CurrentCount);
Evan Chengfb2e7522009-09-18 21:02:19 +0000294 Scheduler.EmitSchedule(0);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000295 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000296 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000297 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000298 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000299 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000300 --Count;
Dan Gohman43f07fb2009-02-03 18:57:45 +0000301 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000302 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000303 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000304 "Instruction count mismatch!");
305 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Evan Chengfb2e7522009-09-18 21:02:19 +0000306 Scheduler.EmitSchedule(0);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000307
308 // Clean up register live-range state.
309 Scheduler.FinishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000310
David Goodwin5e411782009-09-03 22:15:25 +0000311 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000312 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000313 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000314
315 return true;
316}
317
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000318/// StartBlock - Initialize register live-range state for scheduling in
319/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000320///
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000321void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
322 // Call the superclass.
323 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000324
David Goodwind94a4e52009-08-10 15:55:25 +0000325 // Reset the hazard recognizer.
326 HazardRec->Reset();
327
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000328 // Clear out the register class data.
329 std::fill(Classes, array_endof(Classes),
330 static_cast<const TargetRegisterClass *>(0));
Dan Gohman21d90032008-11-25 00:52:40 +0000331
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000332 // Initialize the indices to indicate that no registers are live.
Dan Gohman6c3643c2008-12-19 22:23:43 +0000333 std::fill(KillIndices, array_endof(KillIndices), ~0u);
Dan Gohman21d90032008-11-25 00:52:40 +0000334 std::fill(DefIndices, array_endof(DefIndices), BB->size());
335
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000336 // Clear "do not change" set.
337 KeepRegs.clear();
338
David Goodwin63bcbb72009-10-01 23:28:47 +0000339 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
340
Dan Gohman21d90032008-11-25 00:52:40 +0000341 // Determine the live-out physregs for this block.
David Goodwin63bcbb72009-10-01 23:28:47 +0000342 if (IsReturnBlock) {
Dan Gohman21d90032008-11-25 00:52:40 +0000343 // In a return block, examine the function live-out regs.
344 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
345 E = MRI.liveout_end(); I != E; ++I) {
346 unsigned Reg = *I;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000347 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
Dan Gohman21d90032008-11-25 00:52:40 +0000348 KillIndices[Reg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000349 DefIndices[Reg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000350 // Repeat, for all aliases.
351 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
352 unsigned AliasReg = *Alias;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000353 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
Dan Gohman21d90032008-11-25 00:52:40 +0000354 KillIndices[AliasReg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000355 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000356 }
357 }
David Goodwinc7951f82009-10-01 19:45:32 +0000358 } else {
Dan Gohman21d90032008-11-25 00:52:40 +0000359 // In a non-return block, examine the live-in regs of all successors.
360 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
Dan Gohman47ac0f02009-02-11 04:27:20 +0000361 SE = BB->succ_end(); SI != SE; ++SI)
Dan Gohman21d90032008-11-25 00:52:40 +0000362 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
363 E = (*SI)->livein_end(); I != E; ++I) {
364 unsigned Reg = *I;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000365 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
Dan Gohman21d90032008-11-25 00:52:40 +0000366 KillIndices[Reg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000367 DefIndices[Reg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000368 // Repeat, for all aliases.
369 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
370 unsigned AliasReg = *Alias;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000371 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
Dan Gohman21d90032008-11-25 00:52:40 +0000372 KillIndices[AliasReg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000373 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000374 }
375 }
David Goodwin63bcbb72009-10-01 23:28:47 +0000376 }
Dan Gohman21d90032008-11-25 00:52:40 +0000377
David Goodwin63bcbb72009-10-01 23:28:47 +0000378 // Mark live-out callee-saved registers. In a return block this is
379 // all callee-saved registers. In non-return this is any
380 // callee-saved register that is not saved in the prolog.
381 const MachineFrameInfo *MFI = MF.getFrameInfo();
382 BitVector Pristine = MFI->getPristineRegs(BB);
383 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
384 unsigned Reg = *I;
385 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000386 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
David Goodwin63bcbb72009-10-01 23:28:47 +0000387 KillIndices[Reg] = BB->size();
388 DefIndices[Reg] = ~0u;
389 // Repeat, for all aliases.
390 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
391 unsigned AliasReg = *Alias;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000392 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
David Goodwin63bcbb72009-10-01 23:28:47 +0000393 KillIndices[AliasReg] = BB->size();
394 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000395 }
396 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000397}
398
399/// Schedule - Schedule the instruction range using list scheduling.
400///
401void SchedulePostRATDList::Schedule() {
David Goodwin3a5f0d42009-08-11 01:44:26 +0000402 DEBUG(errs() << "********** List Scheduling **********\n");
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000403
404 // Build the scheduling graph.
Dan Gohmana70dca12009-10-09 23:27:56 +0000405 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000406
David Goodwin4c3715c2009-10-22 23:19:17 +0000407 if (AntiDepMode != TargetSubtarget::ANTIDEP_NONE) {
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000408 if (BreakAntiDependencies()) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000409 // We made changes. Update the dependency graph.
410 // Theoretically we could update the graph in place:
411 // When a live range is changed to use a different register, remove
412 // the def's anti-dependence *and* output-dependence edges due to
413 // that register, and add new anti-dependence and output-dependence
414 // edges based on the next live range of the register.
415 SUnits.clear();
416 EntrySU = SUnit();
417 ExitSU = SUnit();
Dan Gohmana70dca12009-10-09 23:27:56 +0000418 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000419 }
420 }
421
David Goodwind94a4e52009-08-10 15:55:25 +0000422 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
423 SUnits[su].dumpAll(this));
424
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000425 AvailableQueue.initNodes(SUnits);
426
427 ListScheduleTopDown();
428
429 AvailableQueue.releaseState();
430}
431
432/// Observe - Update liveness information to account for the current
433/// instruction, which will not be scheduled.
434///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000435void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000436 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
437
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000438 // Any register which was defined within the previous scheduling region
439 // may have been rescheduled and its lifetime may overlap with registers
440 // in ways not reflected in our current liveness state. For each such
441 // register, adjust the liveness state to be conservatively correct.
442 for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg)
443 if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
444 assert(KillIndices[Reg] == ~0u && "Clobbered register is live!");
445 // Mark this register to be non-renamable.
446 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
447 // Move the def index to the end of the previous region, to reflect
448 // that the def could theoretically have been scheduled at the end.
449 DefIndices[Reg] = InsertPosIndex;
David Goodwin480c5292009-10-20 19:54:44 +0000450 }
David Goodwin480c5292009-10-20 19:54:44 +0000451
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000452 PrescanInstruction(MI);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000453 ScanInstruction(MI, Count);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000454}
455
456/// FinishBlock - Clean up register live-range state.
457///
458void SchedulePostRATDList::FinishBlock() {
459 RegRefs.clear();
460
461 // Call the superclass.
462 ScheduleDAGInstrs::FinishBlock();
463}
464
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000465/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
466/// critical path.
467static SDep *CriticalPathStep(SUnit *SU) {
468 SDep *Next = 0;
469 unsigned NextDepth = 0;
470 // Find the predecessor edge with the greatest depth.
471 for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
472 P != PE; ++P) {
473 SUnit *PredSU = P->getSUnit();
474 unsigned PredLatency = P->getLatency();
475 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
476 // In the case of a latency tie, prefer an anti-dependency edge over
477 // other types of edges.
478 if (NextDepth < PredTotalLatency ||
479 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
480 NextDepth = PredTotalLatency;
481 Next = &*P;
482 }
483 }
484 return Next;
485}
486
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000487void SchedulePostRATDList::PrescanInstruction(MachineInstr *MI) {
488 // Scan the register operands for this instruction and update
489 // Classes and RegRefs.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000490 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
491 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000492 if (!MO.isReg()) continue;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000493 unsigned Reg = MO.getReg();
494 if (Reg == 0) continue;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000495 const TargetRegisterClass *NewRC = 0;
496
497 if (i < MI->getDesc().getNumOperands())
498 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000499
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000500 // For now, only allow the register to be changed if its register
501 // class is consistent across all uses.
502 if (!Classes[Reg] && NewRC)
503 Classes[Reg] = NewRC;
504 else if (!NewRC || Classes[Reg] != NewRC)
505 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
David Goodwin480c5292009-10-20 19:54:44 +0000506
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000507 // Now check for aliases.
508 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
509 // If an alias of the reg is used during the live range, give up.
510 // Note that this allows us to skip checking if AntiDepReg
511 // overlaps with any of the aliases, among other things.
512 unsigned AliasReg = *Alias;
513 if (Classes[AliasReg]) {
514 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
515 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000516 }
517 }
David Goodwin480c5292009-10-20 19:54:44 +0000518
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000519 // If we're still willing to consider this register, note the reference.
520 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
521 RegRefs.insert(std::make_pair(Reg, &MO));
522
523 // It's not safe to change register allocation for source operands of
524 // that have special allocation requirements.
525 if (MO.isUse() && MI->getDesc().hasExtraSrcRegAllocReq()) {
526 if (KeepRegs.insert(Reg)) {
527 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
528 *Subreg; ++Subreg)
529 KeepRegs.insert(*Subreg);
530 }
531 }
532 }
David Goodwin480c5292009-10-20 19:54:44 +0000533}
534
535void SchedulePostRATDList::ScanInstruction(MachineInstr *MI,
536 unsigned Count) {
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000537 // Update liveness.
538 // Proceding upwards, registers that are defed but not used in this
539 // instruction are now dead.
David Goodwin480c5292009-10-20 19:54:44 +0000540 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
541 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000542 if (!MO.isReg()) continue;
David Goodwin480c5292009-10-20 19:54:44 +0000543 unsigned Reg = MO.getReg();
544 if (Reg == 0) continue;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000545 if (!MO.isDef()) continue;
546 // Ignore two-addr defs.
547 if (MI->isRegTiedToUseOperand(i)) continue;
David Goodwin7441d142009-10-20 22:50:43 +0000548
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000549 DefIndices[Reg] = Count;
550 KillIndices[Reg] = ~0u;
551 assert(((KillIndices[Reg] == ~0u) !=
552 (DefIndices[Reg] == ~0u)) &&
553 "Kill and Def maps aren't consistent for Reg!");
554 KeepRegs.erase(Reg);
555 Classes[Reg] = 0;
556 RegRefs.erase(Reg);
557 // Repeat, for all subregs.
David Goodwin480c5292009-10-20 19:54:44 +0000558 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
559 *Subreg; ++Subreg) {
560 unsigned SubregReg = *Subreg;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000561 DefIndices[SubregReg] = Count;
562 KillIndices[SubregReg] = ~0u;
563 KeepRegs.erase(SubregReg);
564 Classes[SubregReg] = 0;
565 RegRefs.erase(SubregReg);
566 }
567 // Conservatively mark super-registers as unusable.
568 for (const unsigned *Super = TRI->getSuperRegisters(Reg);
569 *Super; ++Super) {
570 unsigned SuperReg = *Super;
571 Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
572 }
573 }
574 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
575 MachineOperand &MO = MI->getOperand(i);
576 if (!MO.isReg()) continue;
577 unsigned Reg = MO.getReg();
578 if (Reg == 0) continue;
579 if (!MO.isUse()) continue;
580
581 const TargetRegisterClass *NewRC = 0;
582 if (i < MI->getDesc().getNumOperands())
583 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
584
585 // For now, only allow the register to be changed if its register
586 // class is consistent across all uses.
587 if (!Classes[Reg] && NewRC)
588 Classes[Reg] = NewRC;
589 else if (!NewRC || Classes[Reg] != NewRC)
590 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
591
592 RegRefs.insert(std::make_pair(Reg, &MO));
593
594 // It wasn't previously live but now it is, this is a kill.
595 if (KillIndices[Reg] == ~0u) {
596 KillIndices[Reg] = Count;
597 DefIndices[Reg] = ~0u;
598 assert(((KillIndices[Reg] == ~0u) !=
599 (DefIndices[Reg] == ~0u)) &&
600 "Kill and Def maps aren't consistent for Reg!");
601 }
602 // Repeat, for all aliases.
603 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
604 unsigned AliasReg = *Alias;
605 if (KillIndices[AliasReg] == ~0u) {
606 KillIndices[AliasReg] = Count;
607 DefIndices[AliasReg] = ~0u;
David Goodwin480c5292009-10-20 19:54:44 +0000608 }
609 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000610 }
611}
612
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000613unsigned
614SchedulePostRATDList::findSuitableFreeRegister(unsigned AntiDepReg,
615 unsigned LastNewReg,
616 const TargetRegisterClass *RC) {
617 for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF),
618 RE = RC->allocation_order_end(MF); R != RE; ++R) {
619 unsigned NewReg = *R;
620 // Don't replace a register with itself.
621 if (NewReg == AntiDepReg) continue;
622 // Don't replace a register with one that was recently used to repair
623 // an anti-dependence with this AntiDepReg, because that would
624 // re-introduce that anti-dependence.
625 if (NewReg == LastNewReg) continue;
626 // If NewReg is dead and NewReg's most recent def is not before
627 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
628 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u)) &&
629 "Kill and Def maps aren't consistent for AntiDepReg!");
630 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u)) &&
631 "Kill and Def maps aren't consistent for NewReg!");
632 if (KillIndices[NewReg] != ~0u ||
633 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
634 KillIndices[AntiDepReg] > DefIndices[NewReg])
635 continue;
636 return NewReg;
Dan Gohman26255ad2009-08-12 01:33:27 +0000637 }
638
639 // No registers are free and available!
640 return 0;
641}
642
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000643/// BreakAntiDependencies - Identifiy anti-dependencies along the critical path
644/// of the ScheduleDAG and break them by renaming registers.
645///
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000646bool SchedulePostRATDList::BreakAntiDependencies() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000647 // The code below assumes that there is at least one instruction,
648 // so just duck out immediately if the block is empty.
649 if (SUnits.empty()) return false;
650
David Goodwin480c5292009-10-20 19:54:44 +0000651 // Find the node at the bottom of the critical path.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000652 SUnit *Max = 0;
653 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
654 SUnit *SU = &SUnits[i];
655 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
656 Max = SU;
David Goodwin480c5292009-10-20 19:54:44 +0000657 }
658
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000659#ifndef NDEBUG
David Goodwin480c5292009-10-20 19:54:44 +0000660 {
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000661 DEBUG(errs() << "Critical path has total latency "
662 << (Max->getDepth() + Max->Latency) << "\n");
David Goodwind452ea62009-10-13 19:16:03 +0000663 DEBUG(errs() << "Available regs:");
664 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000665 if (KillIndices[Reg] == ~0u)
David Goodwind452ea62009-10-13 19:16:03 +0000666 DEBUG(errs() << " " << TRI->getName(Reg));
667 }
668 DEBUG(errs() << '\n');
669 }
670#endif
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000671
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000672 // Track progress along the critical path through the SUnit graph as we walk
673 // the instructions.
674 SUnit *CriticalPathSU = Max;
675 MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
676
677 // Consider this pattern:
678 // A = ...
679 // ... = A
680 // A = ...
681 // ... = A
682 // A = ...
683 // ... = A
684 // A = ...
685 // ... = A
686 // There are three anti-dependencies here, and without special care,
687 // we'd break all of them using the same register:
688 // A = ...
689 // ... = A
690 // B = ...
691 // ... = B
692 // B = ...
693 // ... = B
694 // B = ...
695 // ... = B
696 // because at each anti-dependence, B is the first register that
697 // isn't A which is free. This re-introduces anti-dependencies
698 // at all but one of the original anti-dependencies that we were
699 // trying to break. To avoid this, keep track of the most recent
700 // register that each register was replaced with, avoid
701 // using it to repair an anti-dependence on the same register.
702 // This lets us produce this:
703 // A = ...
704 // ... = A
705 // B = ...
706 // ... = B
707 // C = ...
708 // ... = C
709 // B = ...
710 // ... = B
711 // This still has an anti-dependence on B, but at least it isn't on the
712 // original critical path.
713 //
714 // TODO: If we tracked more than one register here, we could potentially
715 // fix that remaining critical edge too. This is a little more involved,
716 // because unlike the most recent register, less recent registers should
717 // still be considered, though only if no other registers are available.
718 unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {};
719
720 // Attempt to break anti-dependence edges on the critical path. Walk the
721 // instructions from the bottom up, tracking information about liveness
722 // as we go to help determine which registers are available.
Dan Gohman21d90032008-11-25 00:52:40 +0000723 bool Changed = false;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000724 unsigned Count = InsertPosIndex - 1;
725 for (MachineBasicBlock::iterator I = InsertPos, E = Begin;
Dan Gohman43f07fb2009-02-03 18:57:45 +0000726 I != E; --Count) {
727 MachineInstr *MI = --I;
Dan Gohman21d90032008-11-25 00:52:40 +0000728
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000729 // Check if this instruction has a dependence on the critical path that
730 // is an anti-dependence that we may be able to break. If it is, set
731 // AntiDepReg to the non-zero register associated with the anti-dependence.
Dan Gohman00dc84a2008-12-16 19:27:52 +0000732 //
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000733 // We limit our attention to the critical path as a heuristic to avoid
Dan Gohman00dc84a2008-12-16 19:27:52 +0000734 // breaking anti-dependence edges that aren't going to significantly
735 // impact the overall schedule. There are a limited number of registers
736 // and we want to save them for the important edges.
737 //
738 // TODO: Instructions with multiple defs could have multiple
739 // anti-dependencies. The current code here only knows how to break one
740 // edge per instruction. Note that we'd have to be able to break all of
741 // the anti-dependencies in an instruction in order to be effective.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000742 unsigned AntiDepReg = 0;
743 if (MI == CriticalPathMI) {
744 if (SDep *Edge = CriticalPathStep(CriticalPathSU)) {
Dan Gohman00dc84a2008-12-16 19:27:52 +0000745 SUnit *NextSU = Edge->getSUnit();
746
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000747 // Only consider anti-dependence edges.
748 if (Edge->getKind() == SDep::Anti) {
Dan Gohman00dc84a2008-12-16 19:27:52 +0000749 AntiDepReg = Edge->getReg();
750 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000751 if (!AllocatableSet.test(AntiDepReg))
Evan Cheng714e8bc2009-10-01 08:26:23 +0000752 // Don't break anti-dependencies on non-allocatable registers.
753 AntiDepReg = 0;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000754 else if (KeepRegs.count(AntiDepReg))
755 // Don't break anti-dependencies if an use down below requires
756 // this exact register.
757 AntiDepReg = 0;
758 else {
759 // If the SUnit has other dependencies on the SUnit that it
760 // anti-depends on, don't bother breaking the anti-dependency
761 // since those edges would prevent such units from being
762 // scheduled past each other regardless.
763 //
764 // Also, if there are dependencies on other SUnits with the
765 // same register as the anti-dependency, don't attempt to
766 // break it.
767 for (SUnit::pred_iterator P = CriticalPathSU->Preds.begin(),
768 PE = CriticalPathSU->Preds.end(); P != PE; ++P)
769 if (P->getSUnit() == NextSU ?
Dan Gohman00dc84a2008-12-16 19:27:52 +0000770 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
771 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000772 AntiDepReg = 0;
773 break;
Dan Gohman00dc84a2008-12-16 19:27:52 +0000774 }
775 }
776 }
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000777 CriticalPathSU = NextSU;
778 CriticalPathMI = CriticalPathSU->getInstr();
Dan Gohman00dc84a2008-12-16 19:27:52 +0000779 } else {
780 // We've reached the end of the critical path.
781 CriticalPathSU = 0;
782 CriticalPathMI = 0;
783 }
784 }
Dan Gohman21d90032008-11-25 00:52:40 +0000785
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000786 PrescanInstruction(MI);
787
788 if (MI->getDesc().hasExtraDefRegAllocReq())
789 // If this instruction's defs have special allocation requirement, don't
790 // break this anti-dependency.
Evan Cheng714e8bc2009-10-01 08:26:23 +0000791 AntiDepReg = 0;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000792 else if (AntiDepReg) {
793 // If this instruction has a use of AntiDepReg, breaking it
794 // is invalid.
795 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
796 MachineOperand &MO = MI->getOperand(i);
797 if (!MO.isReg()) continue;
798 unsigned Reg = MO.getReg();
799 if (Reg == 0) continue;
800 if (MO.isUse() && AntiDepReg == Reg) {
801 AntiDepReg = 0;
802 break;
803 }
804 }
Dan Gohman21d90032008-11-25 00:52:40 +0000805 }
806
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000807 // Determine AntiDepReg's register class, if it is live and is
808 // consistently used within a single class.
809 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
810 assert((AntiDepReg == 0 || RC != NULL) &&
811 "Register should be live if it's causing an anti-dependence!");
812 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
813 AntiDepReg = 0;
Dan Gohman21d90032008-11-25 00:52:40 +0000814
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000815 // Look for a suitable register to use to break the anti-depenence.
816 //
817 // TODO: Instead of picking the first free register, consider which might
818 // be the best.
Dan Gohman21d90032008-11-25 00:52:40 +0000819 if (AntiDepReg != 0) {
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000820 if (unsigned NewReg = findSuitableFreeRegister(AntiDepReg,
821 LastNewReg[AntiDepReg],
822 RC)) {
823 DEBUG(errs() << "Breaking anti-dependence edge on "
Dan Gohman26255ad2009-08-12 01:33:27 +0000824 << TRI->getName(AntiDepReg)
825 << " with " << RegRefs.count(AntiDepReg) << " references"
826 << " using " << TRI->getName(NewReg) << "!\n");
Dan Gohman21d90032008-11-25 00:52:40 +0000827
Dan Gohman26255ad2009-08-12 01:33:27 +0000828 // Update the references to the old register to refer to the new
829 // register.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000830 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
831 std::multimap<unsigned, MachineOperand *>::iterator>
Dan Gohman26255ad2009-08-12 01:33:27 +0000832 Range = RegRefs.equal_range(AntiDepReg);
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000833 for (std::multimap<unsigned, MachineOperand *>::iterator
Dan Gohman26255ad2009-08-12 01:33:27 +0000834 Q = Range.first, QE = Range.second; Q != QE; ++Q)
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000835 Q->second->setReg(NewReg);
Dan Gohman21d90032008-11-25 00:52:40 +0000836
Dan Gohman26255ad2009-08-12 01:33:27 +0000837 // We just went back in time and modified history; the
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000838 // liveness information for the anti-depenence reg is now
Dan Gohman26255ad2009-08-12 01:33:27 +0000839 // inconsistent. Set the state as if it were dead.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000840 Classes[NewReg] = Classes[AntiDepReg];
Dan Gohman26255ad2009-08-12 01:33:27 +0000841 DefIndices[NewReg] = DefIndices[AntiDepReg];
842 KillIndices[NewReg] = KillIndices[AntiDepReg];
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000843 assert(((KillIndices[NewReg] == ~0u) !=
844 (DefIndices[NewReg] == ~0u)) &&
845 "Kill and Def maps aren't consistent for NewReg!");
Dan Gohman21d90032008-11-25 00:52:40 +0000846
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000847 Classes[AntiDepReg] = 0;
Dan Gohman26255ad2009-08-12 01:33:27 +0000848 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
849 KillIndices[AntiDepReg] = ~0u;
850 assert(((KillIndices[AntiDepReg] == ~0u) !=
851 (DefIndices[AntiDepReg] == ~0u)) &&
852 "Kill and Def maps aren't consistent for AntiDepReg!");
Dan Gohman21d90032008-11-25 00:52:40 +0000853
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000854 RegRefs.erase(AntiDepReg);
Dan Gohman26255ad2009-08-12 01:33:27 +0000855 Changed = true;
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000856 LastNewReg[AntiDepReg] = NewReg;
Dan Gohman21d90032008-11-25 00:52:40 +0000857 }
858 }
859
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000860 ScanInstruction(MI, Count);
Dan Gohman21d90032008-11-25 00:52:40 +0000861 }
Dan Gohman21d90032008-11-25 00:52:40 +0000862
863 return Changed;
864}
865
David Goodwin5e411782009-09-03 22:15:25 +0000866/// StartBlockForKills - Initialize register live-range state for updating kills
867///
868void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
869 // Initialize the indices to indicate that no registers are live.
870 std::fill(KillIndices, array_endof(KillIndices), ~0u);
871
872 // Determine the live-out physregs for this block.
873 if (!BB->empty() && BB->back().getDesc().isReturn()) {
874 // In a return block, examine the function live-out regs.
875 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
876 E = MRI.liveout_end(); I != E; ++I) {
877 unsigned Reg = *I;
878 KillIndices[Reg] = BB->size();
879 // Repeat, for all subregs.
880 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
881 *Subreg; ++Subreg) {
882 KillIndices[*Subreg] = BB->size();
883 }
884 }
885 }
886 else {
887 // In a non-return block, examine the live-in regs of all successors.
888 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
889 SE = BB->succ_end(); SI != SE; ++SI) {
890 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
891 E = (*SI)->livein_end(); I != E; ++I) {
892 unsigned Reg = *I;
893 KillIndices[Reg] = BB->size();
894 // Repeat, for all subregs.
895 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
896 *Subreg; ++Subreg) {
897 KillIndices[*Subreg] = BB->size();
898 }
899 }
900 }
901 }
902}
903
David Goodwin8f909342009-09-23 16:35:25 +0000904bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
905 MachineOperand &MO) {
906 // Setting kill flag...
907 if (!MO.isKill()) {
908 MO.setIsKill(true);
909 return false;
910 }
911
912 // If MO itself is live, clear the kill flag...
913 if (KillIndices[MO.getReg()] != ~0u) {
914 MO.setIsKill(false);
915 return false;
916 }
917
918 // If any subreg of MO is live, then create an imp-def for that
919 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000920 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000921 bool AllDead = true;
922 const unsigned SuperReg = MO.getReg();
923 for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
924 *Subreg; ++Subreg) {
925 if (KillIndices[*Subreg] != ~0u) {
926 MI->addOperand(MachineOperand::CreateReg(*Subreg,
927 true /*IsDef*/,
928 true /*IsImp*/,
929 false /*IsKill*/,
930 false /*IsDead*/));
931 AllDead = false;
932 }
933 }
934
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000935 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000936 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000937 return false;
938}
939
David Goodwin88a589c2009-08-25 17:03:05 +0000940/// FixupKills - Fix the register kill flags, they may have been made
941/// incorrect by instruction reordering.
942///
943void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
944 DEBUG(errs() << "Fixup kills for BB ID#" << MBB->getNumber() << '\n');
945
946 std::set<unsigned> killedRegs;
947 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000948
949 StartBlockForKills(MBB);
David Goodwin7886cd82009-08-29 00:11:13 +0000950
951 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000952 unsigned Count = MBB->size();
953 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
954 I != E; --Count) {
955 MachineInstr *MI = --I;
956
David Goodwin7886cd82009-08-29 00:11:13 +0000957 // Update liveness. Registers that are defed but not used in this
958 // instruction are now dead. Mark register and all subregs as they
959 // are completely defined.
960 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
961 MachineOperand &MO = MI->getOperand(i);
962 if (!MO.isReg()) continue;
963 unsigned Reg = MO.getReg();
964 if (Reg == 0) continue;
965 if (!MO.isDef()) continue;
966 // Ignore two-addr defs.
967 if (MI->isRegTiedToUseOperand(i)) continue;
968
David Goodwin7886cd82009-08-29 00:11:13 +0000969 KillIndices[Reg] = ~0u;
970
971 // Repeat for all subregs.
972 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
973 *Subreg; ++Subreg) {
974 KillIndices[*Subreg] = ~0u;
975 }
976 }
David Goodwin88a589c2009-08-25 17:03:05 +0000977
David Goodwin8f909342009-09-23 16:35:25 +0000978 // Examine all used registers and set/clear kill flag. When a
979 // register is used multiple times we only set the kill flag on
980 // the first use.
David Goodwin88a589c2009-08-25 17:03:05 +0000981 killedRegs.clear();
982 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
983 MachineOperand &MO = MI->getOperand(i);
984 if (!MO.isReg() || !MO.isUse()) continue;
985 unsigned Reg = MO.getReg();
986 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
987
David Goodwin7886cd82009-08-29 00:11:13 +0000988 bool kill = false;
989 if (killedRegs.find(Reg) == killedRegs.end()) {
990 kill = true;
991 // A register is not killed if any subregs are live...
992 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
993 *Subreg; ++Subreg) {
994 if (KillIndices[*Subreg] != ~0u) {
995 kill = false;
996 break;
997 }
998 }
999
1000 // If subreg is not live, then register is killed if it became
1001 // live in this instruction
1002 if (kill)
1003 kill = (KillIndices[Reg] == ~0u);
1004 }
1005
David Goodwin88a589c2009-08-25 17:03:05 +00001006 if (MO.isKill() != kill) {
David Goodwin8f909342009-09-23 16:35:25 +00001007 bool removed = ToggleKillFlag(MI, MO);
1008 if (removed) {
1009 DEBUG(errs() << "Fixed <removed> in ");
1010 } else {
1011 DEBUG(errs() << "Fixed " << MO << " in ");
1012 }
David Goodwin88a589c2009-08-25 17:03:05 +00001013 DEBUG(MI->dump());
1014 }
David Goodwin7886cd82009-08-29 00:11:13 +00001015
David Goodwin88a589c2009-08-25 17:03:05 +00001016 killedRegs.insert(Reg);
1017 }
David Goodwin7886cd82009-08-29 00:11:13 +00001018
David Goodwina3251db2009-08-31 20:47:02 +00001019 // Mark any used register (that is not using undef) and subregs as
1020 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +00001021 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1022 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +00001023 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +00001024 unsigned Reg = MO.getReg();
1025 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
1026
David Goodwin7886cd82009-08-29 00:11:13 +00001027 KillIndices[Reg] = Count;
1028
1029 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
1030 *Subreg; ++Subreg) {
1031 KillIndices[*Subreg] = Count;
1032 }
1033 }
David Goodwin88a589c2009-08-25 17:03:05 +00001034 }
1035}
1036
Dan Gohman343f0c02008-11-19 23:18:57 +00001037//===----------------------------------------------------------------------===//
1038// Top-Down Scheduling
1039//===----------------------------------------------------------------------===//
1040
1041/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
1042/// the PendingQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman54e4c362008-12-09 22:54:47 +00001043void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
1044 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +00001045
Dan Gohman343f0c02008-11-19 23:18:57 +00001046#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +00001047 if (SuccSU->NumPredsLeft == 0) {
Chris Lattner103289e2009-08-23 07:19:13 +00001048 errs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +00001049 SuccSU->dump(this);
Chris Lattner103289e2009-08-23 07:19:13 +00001050 errs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001051 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +00001052 }
1053#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +00001054 --SuccSU->NumPredsLeft;
1055
Dan Gohman343f0c02008-11-19 23:18:57 +00001056 // Compute how many cycles it will be before this actually becomes
1057 // available. This is the max of the start time of all predecessors plus
1058 // their latencies.
Dan Gohman3f237442008-12-16 03:25:46 +00001059 SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
Dan Gohman343f0c02008-11-19 23:18:57 +00001060
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001061 // If all the node's predecessors are scheduled, this node is ready
1062 // to be scheduled. Ignore the special ExitSU node.
1063 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +00001064 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001065}
1066
1067/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
1068void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
1069 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1070 I != E; ++I)
1071 ReleaseSucc(SU, &*I);
Dan Gohman343f0c02008-11-19 23:18:57 +00001072}
1073
1074/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1075/// count of its successors. If a successor pending count is zero, add it to
1076/// the Available queue.
1077void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Goodwin3a5f0d42009-08-11 01:44:26 +00001078 DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +00001079 DEBUG(SU->dump(this));
1080
1081 Sequence.push_back(SU);
Dan Gohman3f237442008-12-16 03:25:46 +00001082 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
1083 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +00001084
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001085 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +00001086 SU->isScheduled = true;
1087 AvailableQueue.ScheduledNode(SU);
1088}
1089
1090/// ListScheduleTopDown - The main loop of list scheduling for top-down
1091/// schedulers.
1092void SchedulePostRATDList::ListScheduleTopDown() {
1093 unsigned CurCycle = 0;
1094
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001095 // Release any successors of the special Entry node.
1096 ReleaseSuccessors(&EntrySU);
1097
Dan Gohman343f0c02008-11-19 23:18:57 +00001098 // All leaves to Available queue.
1099 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1100 // It is available if it has no predecessors.
1101 if (SUnits[i].Preds.empty()) {
1102 AvailableQueue.push(&SUnits[i]);
1103 SUnits[i].isAvailable = true;
1104 }
1105 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001106
David Goodwin2ffb0ce2009-08-12 21:47:46 +00001107 // In any cycle where we can't schedule any instructions, we must
1108 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001109 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +00001110
Dan Gohman343f0c02008-11-19 23:18:57 +00001111 // While Available queue is not empty, grab the node with the highest
1112 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +00001113 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +00001114 Sequence.reserve(SUnits.size());
1115 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
1116 // Check to see if any of the pending instructions are ready to issue. If
1117 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +00001118 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +00001119 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohman3f237442008-12-16 03:25:46 +00001120 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +00001121 AvailableQueue.push(PendingQueue[i]);
1122 PendingQueue[i]->isAvailable = true;
1123 PendingQueue[i] = PendingQueue.back();
1124 PendingQueue.pop_back();
1125 --i; --e;
Dan Gohman3f237442008-12-16 03:25:46 +00001126 } else if (PendingQueue[i]->getDepth() < MinDepth)
1127 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +00001128 }
David Goodwinc93d8372009-08-11 17:35:23 +00001129
David Goodwin7cd01182009-08-11 17:56:42 +00001130 DEBUG(errs() << "\n*** Examining Available\n";
1131 LatencyPriorityQueue q = AvailableQueue;
1132 while (!q.empty()) {
1133 SUnit *su = q.pop();
1134 errs() << "Height " << su->getHeight() << ": ";
1135 su->dump(this);
1136 });
David Goodwinc93d8372009-08-11 17:35:23 +00001137
Dan Gohman2836c282009-01-16 01:33:36 +00001138 SUnit *FoundSUnit = 0;
1139
1140 bool HasNoopHazards = false;
1141 while (!AvailableQueue.empty()) {
1142 SUnit *CurSUnit = AvailableQueue.pop();
1143
1144 ScheduleHazardRecognizer::HazardType HT =
1145 HazardRec->getHazardType(CurSUnit);
1146 if (HT == ScheduleHazardRecognizer::NoHazard) {
1147 FoundSUnit = CurSUnit;
1148 break;
1149 }
1150
1151 // Remember if this is a noop hazard.
1152 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
1153
1154 NotReady.push_back(CurSUnit);
1155 }
1156
1157 // Add the nodes that aren't ready back onto the available list.
1158 if (!NotReady.empty()) {
1159 AvailableQueue.push_all(NotReady);
1160 NotReady.clear();
1161 }
1162
Dan Gohman343f0c02008-11-19 23:18:57 +00001163 // If we found a node to schedule, do it now.
1164 if (FoundSUnit) {
1165 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +00001166 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001167 CycleHasInsts = true;
Dan Gohman343f0c02008-11-19 23:18:57 +00001168
David Goodwind94a4e52009-08-10 15:55:25 +00001169 // If we are using the target-specific hazards, then don't
1170 // advance the cycle time just because we schedule a node. If
1171 // the target allows it we can schedule multiple nodes in the
1172 // same cycle.
1173 if (!EnablePostRAHazardAvoidance) {
1174 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
1175 ++CurCycle;
1176 }
Dan Gohman2836c282009-01-16 01:33:36 +00001177 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001178 if (CycleHasInsts) {
David Goodwin2ffb0ce2009-08-12 21:47:46 +00001179 DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n');
1180 HazardRec->AdvanceCycle();
1181 } else if (!HasNoopHazards) {
1182 // Otherwise, we have a pipeline stall, but no other problem,
1183 // just advance the current cycle and try again.
1184 DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n');
1185 HazardRec->AdvanceCycle();
1186 ++NumStalls;
1187 } else {
1188 // Otherwise, we have no instructions to issue and we have instructions
1189 // that will fault if we don't do this right. This is the case for
1190 // processors without pipeline interlocks and other cases.
1191 DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n');
1192 HazardRec->EmitNoop();
1193 Sequence.push_back(0); // NULL here means noop
1194 ++NumNoops;
1195 }
1196
Dan Gohman2836c282009-01-16 01:33:36 +00001197 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001198 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +00001199 }
1200 }
1201
1202#ifndef NDEBUG
Dan Gohmana1e6d362008-11-20 01:26:25 +00001203 VerifySchedule(/*isBottomUp=*/false);
Dan Gohman343f0c02008-11-19 23:18:57 +00001204#endif
1205}
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00001206
1207//===----------------------------------------------------------------------===//
1208// Public Constructor Functions
1209//===----------------------------------------------------------------------===//
1210
Evan Chengfa163542009-10-16 21:06:15 +00001211FunctionPass *llvm::createPostRAScheduler(CodeGenOpt::Level OptLevel) {
1212 return new PostRAScheduler(OptLevel);
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00001213}