blob: 5917e76004ab1bebe21baeb4cae06c2e079cb90b [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 Goodwin82c72482009-10-28 18:29:54 +000022#include "AntiDepBreaker.h"
David Goodwin34877712009-10-26 19:32:42 +000023#include "AggressiveAntiDepBreaker.h"
David Goodwin2e7be612009-10-26 16:59:04 +000024#include "CriticalAntiDepBreaker.h"
David Goodwind94a4e52009-08-10 15:55:25 +000025#include "ExactHazardRecognizer.h"
26#include "SimpleHazardRecognizer.h"
Dan Gohman6dc75fe2009-02-06 17:12:10 +000027#include "ScheduleDAGInstrs.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000028#include "llvm/CodeGen/Passes.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000029#include "llvm/CodeGen/LatencyPriorityQueue.h"
30#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman3f237442008-12-16 03:25:46 +000031#include "llvm/CodeGen/MachineDominators.h"
David Goodwinc7951f82009-10-01 19:45:32 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000033#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman3f237442008-12-16 03:25:46 +000034#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman21d90032008-11-25 00:52:40 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2836c282009-01-16 01:33:36 +000036#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Dan Gohmana70dca12009-10-09 23:27:56 +000037#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohmanbed353d2009-02-10 23:29:38 +000038#include "llvm/Target/TargetLowering.h"
Dan Gohman79ce2762009-01-15 19:20:50 +000039#include "llvm/Target/TargetMachine.h"
Dan Gohman21d90032008-11-25 00:52:40 +000040#include "llvm/Target/TargetInstrInfo.h"
41#include "llvm/Target/TargetRegisterInfo.h"
David Goodwin0dad89f2009-09-30 00:10:16 +000042#include "llvm/Target/TargetSubtarget.h"
David Goodwine10deca2009-10-26 22:31:16 +000043#include "llvm/Support/CommandLine.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000044#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000045#include "llvm/Support/ErrorHandling.h"
David Goodwin3a5f0d42009-08-11 01:44:26 +000046#include "llvm/Support/raw_ostream.h"
David Goodwin2e7be612009-10-26 16:59:04 +000047#include "llvm/ADT/BitVector.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000048#include "llvm/ADT/Statistic.h"
Dan Gohman21d90032008-11-25 00:52:40 +000049#include <map>
David Goodwin88a589c2009-08-25 17:03:05 +000050#include <set>
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000051using namespace llvm;
52
Dan Gohman2836c282009-01-16 01:33:36 +000053STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman343f0c02008-11-19 23:18:57 +000054STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwin2e7be612009-10-26 16:59:04 +000055STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohman343f0c02008-11-19 23:18:57 +000056
David Goodwin471850a2009-10-01 21:46:35 +000057// Post-RA scheduling is enabled with
58// TargetSubtarget.enablePostRAScheduler(). This flag can be used to
59// override the target.
60static cl::opt<bool>
61EnablePostRAScheduler("post-RA-scheduler",
62 cl::desc("Enable scheduling after register allocation"),
David Goodwin9843a932009-10-01 22:19:57 +000063 cl::init(false), cl::Hidden);
David Goodwin2e7be612009-10-26 16:59:04 +000064static cl::opt<std::string>
Dan Gohman21d90032008-11-25 00:52:40 +000065EnableAntiDepBreaking("break-anti-dependencies",
David Goodwin2e7be612009-10-26 16:59:04 +000066 cl::desc("Break post-RA scheduling anti-dependencies: "
67 "\"critical\", \"all\", or \"none\""),
68 cl::init("none"), cl::Hidden);
Dan Gohman2836c282009-01-16 01:33:36 +000069static cl::opt<bool>
70EnablePostRAHazardAvoidance("avoid-hazards",
David Goodwind94a4e52009-08-10 15:55:25 +000071 cl::desc("Enable exact hazard avoidance"),
David Goodwin5e411782009-09-03 22:15:25 +000072 cl::init(true), cl::Hidden);
Dan Gohman2836c282009-01-16 01:33:36 +000073
David Goodwin1f152282009-09-01 18:34:03 +000074// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
75static cl::opt<int>
76DebugDiv("postra-sched-debugdiv",
77 cl::desc("Debug control MBBs that are scheduled"),
78 cl::init(0), cl::Hidden);
79static cl::opt<int>
80DebugMod("postra-sched-debugmod",
81 cl::desc("Debug control MBBs that are scheduled"),
82 cl::init(0), cl::Hidden);
83
David Goodwinada0ef82009-10-26 19:41:00 +000084AntiDepBreaker::~AntiDepBreaker() { }
85
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000086namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000087 class PostRAScheduler : public MachineFunctionPass {
Dan Gohmana70dca12009-10-09 23:27:56 +000088 AliasAnalysis *AA;
Evan Chengfa163542009-10-16 21:06:15 +000089 CodeGenOpt::Level OptLevel;
Dan Gohmana70dca12009-10-09 23:27:56 +000090
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000091 public:
92 static char ID;
Evan Chengfa163542009-10-16 21:06:15 +000093 PostRAScheduler(CodeGenOpt::Level ol) :
94 MachineFunctionPass(&ID), OptLevel(ol) {}
Dan Gohman21d90032008-11-25 00:52:40 +000095
Dan Gohman3f237442008-12-16 03:25:46 +000096 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000097 AU.setPreservesCFG();
Dan Gohmana70dca12009-10-09 23:27:56 +000098 AU.addRequired<AliasAnalysis>();
Dan Gohman3f237442008-12-16 03:25:46 +000099 AU.addRequired<MachineDominatorTree>();
100 AU.addPreserved<MachineDominatorTree>();
101 AU.addRequired<MachineLoopInfo>();
102 AU.addPreserved<MachineLoopInfo>();
103 MachineFunctionPass::getAnalysisUsage(AU);
104 }
105
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000106 const char *getPassName() const {
Dan Gohman21d90032008-11-25 00:52:40 +0000107 return "Post RA top-down list latency scheduler";
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000108 }
109
110 bool runOnMachineFunction(MachineFunction &Fn);
111 };
Dan Gohman343f0c02008-11-19 23:18:57 +0000112 char PostRAScheduler::ID = 0;
113
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000114 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +0000115 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000116 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000117 LatencyPriorityQueue AvailableQueue;
118
119 /// PendingQueue - This contains all of the instructions whose operands have
120 /// been issued, but their results are not ready yet (due to the latency of
121 /// the operation). Once the operands becomes available, the instruction is
122 /// added to the AvailableQueue.
123 std::vector<SUnit*> PendingQueue;
124
Dan Gohman21d90032008-11-25 00:52:40 +0000125 /// Topo - A topological ordering for SUnits.
126 ScheduleDAGTopologicalSort Topo;
Dan Gohman343f0c02008-11-19 23:18:57 +0000127
Dan Gohman2836c282009-01-16 01:33:36 +0000128 /// HazardRec - The hazard recognizer to use.
129 ScheduleHazardRecognizer *HazardRec;
130
David Goodwin2e7be612009-10-26 16:59:04 +0000131 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
132 AntiDepBreaker *AntiDepBreak;
133
Dan Gohmana70dca12009-10-09 23:27:56 +0000134 /// AA - AliasAnalysis for making memory reference queries.
135 AliasAnalysis *AA;
136
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000137 /// KillIndices - The index of the most recent kill (proceding bottom-up),
138 /// or ~0u if the register is not live.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000139 unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
140
Dan Gohman21d90032008-11-25 00:52:40 +0000141 public:
Dan Gohman79ce2762009-01-15 19:20:50 +0000142 SchedulePostRATDList(MachineFunction &MF,
Dan Gohman3f237442008-12-16 03:25:46 +0000143 const MachineLoopInfo &MLI,
Dan Gohman2836c282009-01-16 01:33:36 +0000144 const MachineDominatorTree &MDT,
Dan Gohmana70dca12009-10-09 23:27:56 +0000145 ScheduleHazardRecognizer *HR,
David Goodwin2e7be612009-10-26 16:59:04 +0000146 AntiDepBreaker *ADB,
147 AliasAnalysis *aa)
Dan Gohman79ce2762009-01-15 19:20:50 +0000148 : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits),
David Goodwin2e7be612009-10-26 16:59:04 +0000149 HazardRec(HR), AntiDepBreak(ADB), AA(aa) {}
Dan Gohman2836c282009-01-16 01:33:36 +0000150
151 ~SchedulePostRATDList() {
Dan Gohman2836c282009-01-16 01:33:36 +0000152 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000153
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000154 /// StartBlock - Initialize register live-range state for scheduling in
155 /// this block.
156 ///
157 void StartBlock(MachineBasicBlock *BB);
158
159 /// Schedule - Schedule the instruction range using list scheduling.
160 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000161 void Schedule();
David Goodwin88a589c2009-08-25 17:03:05 +0000162
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000163 /// Observe - Update liveness information to account for the current
164 /// instruction, which will not be scheduled.
165 ///
166 void Observe(MachineInstr *MI, unsigned Count);
167
168 /// FinishBlock - Clean up register live-range state.
169 ///
170 void FinishBlock();
171
David Goodwin2e7be612009-10-26 16:59:04 +0000172 /// FixupKills - Fix register kill flags that have been made
173 /// invalid due to scheduling
174 ///
175 void FixupKills(MachineBasicBlock *MBB);
176
Dan Gohman343f0c02008-11-19 23:18:57 +0000177 private:
David Goodwin4de099d2009-11-03 20:57:50 +0000178 void ReleaseSucc(SUnit *SU, SDep *SuccEdge, bool IgnoreAntiDep);
179 void ReleaseSuccessors(SUnit *SU, bool IgnoreAntiDep);
180 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle, bool IgnoreAntiDep);
181 void ListScheduleTopDown(
182 AntiDepBreaker::CandidateMap *AntiDepCandidates);
David Goodwin5e411782009-09-03 22:15:25 +0000183 void StartBlockForKills(MachineBasicBlock *BB);
David Goodwin8f909342009-09-23 16:35:25 +0000184
185 // ToggleKillFlag - Toggle a register operand kill flag. Other
186 // adjustments may be made to the instruction if necessary. Return
187 // true if the operand has been deleted, false if not.
188 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Dan Gohman343f0c02008-11-19 23:18:57 +0000189 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000190}
191
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000192/// isSchedulingBoundary - Test if the given instruction should be
193/// considered a scheduling boundary. This primarily includes labels
194/// and terminators.
195///
196static bool isSchedulingBoundary(const MachineInstr *MI,
197 const MachineFunction &MF) {
198 // Terminators and labels can't be scheduled around.
199 if (MI->getDesc().isTerminator() || MI->isLabel())
200 return true;
201
Dan Gohmanbed353d2009-02-10 23:29:38 +0000202 // Don't attempt to schedule around any instruction that modifies
203 // a stack-oriented pointer, as it's unlikely to be profitable. This
204 // saves compile time, because it doesn't require every single
205 // stack slot reference to depend on the instruction that does the
206 // modification.
207 const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
208 if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore()))
209 return true;
210
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000211 return false;
212}
213
Dan Gohman343f0c02008-11-19 23:18:57 +0000214bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000215 AA = &getAnalysis<AliasAnalysis>();
216
David Goodwin471850a2009-10-01 21:46:35 +0000217 // Check for explicit enable/disable of post-ra scheduling.
David Goodwin4c3715c2009-10-22 23:19:17 +0000218 TargetSubtarget::AntiDepBreakMode AntiDepMode = TargetSubtarget::ANTIDEP_NONE;
David Goodwin0855dee2009-11-10 00:15:47 +0000219 TargetSubtarget::ExcludedRCVector ExcludedRCs;
David Goodwin471850a2009-10-01 21:46:35 +0000220 if (EnablePostRAScheduler.getPosition() > 0) {
221 if (!EnablePostRAScheduler)
Evan Chengc83da2f92009-10-16 06:10:34 +0000222 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000223 } else {
Evan Chengc83da2f92009-10-16 06:10:34 +0000224 // Check that post-RA scheduling is enabled for this target.
David Goodwin471850a2009-10-01 21:46:35 +0000225 const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>();
David Goodwin0855dee2009-11-10 00:15:47 +0000226 if (!ST.enablePostRAScheduler(OptLevel, AntiDepMode, ExcludedRCs))
Evan Chengc83da2f92009-10-16 06:10:34 +0000227 return false;
David Goodwin471850a2009-10-01 21:46:35 +0000228 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000229
David Goodwin4c3715c2009-10-22 23:19:17 +0000230 // Check for antidep breaking override...
231 if (EnableAntiDepBreaking.getPosition() > 0) {
David Goodwin2e7be612009-10-26 16:59:04 +0000232 AntiDepMode = (EnableAntiDepBreaking == "all") ? TargetSubtarget::ANTIDEP_ALL :
233 (EnableAntiDepBreaking == "critical") ? TargetSubtarget::ANTIDEP_CRITICAL :
234 TargetSubtarget::ANTIDEP_NONE;
David Goodwin4c3715c2009-10-22 23:19:17 +0000235 }
236
David Goodwin3a5f0d42009-08-11 01:44:26 +0000237 DEBUG(errs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000238
Dan Gohman3f237442008-12-16 03:25:46 +0000239 const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
240 const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
David Goodwind94a4e52009-08-10 15:55:25 +0000241 const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData();
Dan Gohman2836c282009-01-16 01:33:36 +0000242 ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ?
David Goodwind94a4e52009-08-10 15:55:25 +0000243 (ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) :
244 (ScheduleHazardRecognizer *)new SimpleHazardRecognizer();
David Goodwin2e7be612009-10-26 16:59:04 +0000245 AntiDepBreaker *ADB =
David Goodwin34877712009-10-26 19:32:42 +0000246 ((AntiDepMode == TargetSubtarget::ANTIDEP_ALL) ?
David Goodwin0855dee2009-11-10 00:15:47 +0000247 (AntiDepBreaker *)new AggressiveAntiDepBreaker(Fn, ExcludedRCs) :
David Goodwin34877712009-10-26 19:32:42 +0000248 ((AntiDepMode == TargetSubtarget::ANTIDEP_CRITICAL) ?
249 (AntiDepBreaker *)new CriticalAntiDepBreaker(Fn) : NULL));
Dan Gohman3f237442008-12-16 03:25:46 +0000250
David Goodwin2e7be612009-10-26 16:59:04 +0000251 SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR, ADB, AA);
Dan Gohman79ce2762009-01-15 19:20:50 +0000252
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000253 // Loop over all of the basic blocks
254 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000255 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000256#ifndef NDEBUG
257 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
258 if (DebugDiv > 0) {
259 static int bbcnt = 0;
260 if (bbcnt++ % DebugDiv != DebugMod)
261 continue;
262 errs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() <<
Dan Gohman0ba90f32009-10-31 20:19:03 +0000263 ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin1f152282009-09-01 18:34:03 +0000264 }
265#endif
266
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000267 // Initialize register live-range state for scheduling in this block.
268 Scheduler.StartBlock(MBB);
269
Dan Gohmanf7119392009-01-16 22:10:20 +0000270 // Schedule each sequence of instructions not interrupted by a label
271 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000272 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000273 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000274 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
275 MachineInstr *MI = prior(I);
276 if (isSchedulingBoundary(MI, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000277 Scheduler.Run(MBB, I, Current, CurrentCount);
Evan Chengfb2e7522009-09-18 21:02:19 +0000278 Scheduler.EmitSchedule(0);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000279 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000280 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000281 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000282 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000283 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000284 --Count;
Dan Gohman43f07fb2009-02-03 18:57:45 +0000285 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000286 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000287 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000288 "Instruction count mismatch!");
289 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Evan Chengfb2e7522009-09-18 21:02:19 +0000290 Scheduler.EmitSchedule(0);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000291
292 // Clean up register live-range state.
293 Scheduler.FinishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000294
David Goodwin5e411782009-09-03 22:15:25 +0000295 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000296 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000297 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000298
David Goodwin2e7be612009-10-26 16:59:04 +0000299 delete HR;
300 delete ADB;
301
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000302 return true;
303}
304
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000305/// StartBlock - Initialize register live-range state for scheduling in
306/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000307///
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000308void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
309 // Call the superclass.
310 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000311
David Goodwin2e7be612009-10-26 16:59:04 +0000312 // Reset the hazard recognizer and anti-dep breaker.
David Goodwind94a4e52009-08-10 15:55:25 +0000313 HazardRec->Reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000314 if (AntiDepBreak != NULL)
315 AntiDepBreak->StartBlock(BB);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000316}
317
318/// Schedule - Schedule the instruction range using list scheduling.
319///
320void SchedulePostRATDList::Schedule() {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000321 // Build the scheduling graph.
Dan Gohmana70dca12009-10-09 23:27:56 +0000322 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000323
David Goodwin2e7be612009-10-26 16:59:04 +0000324 if (AntiDepBreak != NULL) {
David Goodwin4de099d2009-11-03 20:57:50 +0000325 AntiDepBreaker::CandidateMap AntiDepCandidates;
326 const bool NeedCandidates = AntiDepBreak->NeedCandidates();
327
David Goodwine10deca2009-10-26 22:31:16 +0000328 for (unsigned i = 0, Trials = AntiDepBreak->GetMaxTrials();
329 i < Trials; ++i) {
David Goodwin4de099d2009-11-03 20:57:50 +0000330 DEBUG(errs() << "\n********** Break Anti-Deps, Trial " <<
David Goodwine10deca2009-10-26 22:31:16 +0000331 i << " **********\n");
David Goodwin4de099d2009-11-03 20:57:50 +0000332
333 // If candidates are required, then schedule forward ignoring
334 // anti-dependencies to collect the candidate operands for
335 // anti-dependence breaking. The candidates will be the def
336 // operands for the anti-dependencies that if broken would allow
337 // an improved schedule
338 if (NeedCandidates) {
339 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
340 SUnits[su].dumpAll(this));
341
342 AntiDepCandidates.clear();
343 AvailableQueue.initNodes(SUnits);
344 ListScheduleTopDown(&AntiDepCandidates);
345 AvailableQueue.releaseState();
346 }
347
David Goodwine10deca2009-10-26 22:31:16 +0000348 unsigned Broken =
David Goodwin4de099d2009-11-03 20:57:50 +0000349 AntiDepBreak->BreakAntiDependencies(SUnits, AntiDepCandidates,
350 Begin, InsertPos, InsertPosIndex);
David Goodwine10deca2009-10-26 22:31:16 +0000351
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000352 // We made changes. Update the dependency graph.
353 // Theoretically we could update the graph in place:
354 // When a live range is changed to use a different register, remove
355 // the def's anti-dependence *and* output-dependence edges due to
356 // that register, and add new anti-dependence and output-dependence
357 // edges based on the next live range of the register.
David Goodwin4de099d2009-11-03 20:57:50 +0000358 if ((Broken != 0) || NeedCandidates) {
359 SUnits.clear();
360 Sequence.clear();
361 EntrySU = SUnit();
362 ExitSU = SUnit();
363 BuildSchedGraph(AA);
364 }
David Goodwin2e7be612009-10-26 16:59:04 +0000365
366 NumFixedAnti += Broken;
David Goodwin4de099d2009-11-03 20:57:50 +0000367 if (Broken == 0)
368 break;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000369 }
370 }
371
David Goodwine10deca2009-10-26 22:31:16 +0000372 DEBUG(errs() << "********** List Scheduling **********\n");
David Goodwind94a4e52009-08-10 15:55:25 +0000373 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
374 SUnits[su].dumpAll(this));
375
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000376 AvailableQueue.initNodes(SUnits);
David Goodwin4de099d2009-11-03 20:57:50 +0000377 ListScheduleTopDown(NULL);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000378 AvailableQueue.releaseState();
379}
380
381/// Observe - Update liveness information to account for the current
382/// instruction, which will not be scheduled.
383///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000384void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwin2e7be612009-10-26 16:59:04 +0000385 if (AntiDepBreak != NULL)
386 AntiDepBreak->Observe(MI, Count, InsertPosIndex);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000387}
388
389/// FinishBlock - Clean up register live-range state.
390///
391void SchedulePostRATDList::FinishBlock() {
David Goodwin2e7be612009-10-26 16:59:04 +0000392 if (AntiDepBreak != NULL)
393 AntiDepBreak->FinishBlock();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000394
395 // Call the superclass.
396 ScheduleDAGInstrs::FinishBlock();
397}
398
David Goodwin5e411782009-09-03 22:15:25 +0000399/// StartBlockForKills - Initialize register live-range state for updating kills
400///
401void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
402 // Initialize the indices to indicate that no registers are live.
403 std::fill(KillIndices, array_endof(KillIndices), ~0u);
404
405 // Determine the live-out physregs for this block.
406 if (!BB->empty() && BB->back().getDesc().isReturn()) {
407 // In a return block, examine the function live-out regs.
408 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
409 E = MRI.liveout_end(); I != E; ++I) {
410 unsigned Reg = *I;
411 KillIndices[Reg] = BB->size();
412 // Repeat, for all subregs.
413 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
414 *Subreg; ++Subreg) {
415 KillIndices[*Subreg] = BB->size();
416 }
417 }
418 }
419 else {
420 // In a non-return block, examine the live-in regs of all successors.
421 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
422 SE = BB->succ_end(); SI != SE; ++SI) {
423 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
424 E = (*SI)->livein_end(); I != E; ++I) {
425 unsigned Reg = *I;
426 KillIndices[Reg] = BB->size();
427 // Repeat, for all subregs.
428 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
429 *Subreg; ++Subreg) {
430 KillIndices[*Subreg] = BB->size();
431 }
432 }
433 }
434 }
435}
436
David Goodwin8f909342009-09-23 16:35:25 +0000437bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
438 MachineOperand &MO) {
439 // Setting kill flag...
440 if (!MO.isKill()) {
441 MO.setIsKill(true);
442 return false;
443 }
444
445 // If MO itself is live, clear the kill flag...
446 if (KillIndices[MO.getReg()] != ~0u) {
447 MO.setIsKill(false);
448 return false;
449 }
450
451 // If any subreg of MO is live, then create an imp-def for that
452 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000453 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000454 bool AllDead = true;
455 const unsigned SuperReg = MO.getReg();
456 for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
457 *Subreg; ++Subreg) {
458 if (KillIndices[*Subreg] != ~0u) {
459 MI->addOperand(MachineOperand::CreateReg(*Subreg,
460 true /*IsDef*/,
461 true /*IsImp*/,
462 false /*IsKill*/,
463 false /*IsDead*/));
464 AllDead = false;
465 }
466 }
467
Dan Gohmanc1ae8c92009-10-21 01:44:44 +0000468 if(AllDead)
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000469 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000470 return false;
471}
472
David Goodwin88a589c2009-08-25 17:03:05 +0000473/// FixupKills - Fix the register kill flags, they may have been made
474/// incorrect by instruction reordering.
475///
476void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000477 DEBUG(errs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwin88a589c2009-08-25 17:03:05 +0000478
479 std::set<unsigned> killedRegs;
480 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000481
482 StartBlockForKills(MBB);
David Goodwin7886cd82009-08-29 00:11:13 +0000483
484 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000485 unsigned Count = MBB->size();
486 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
487 I != E; --Count) {
488 MachineInstr *MI = --I;
489
David Goodwin7886cd82009-08-29 00:11:13 +0000490 // Update liveness. Registers that are defed but not used in this
491 // instruction are now dead. Mark register and all subregs as they
492 // are completely defined.
493 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
494 MachineOperand &MO = MI->getOperand(i);
495 if (!MO.isReg()) continue;
496 unsigned Reg = MO.getReg();
497 if (Reg == 0) continue;
498 if (!MO.isDef()) continue;
499 // Ignore two-addr defs.
500 if (MI->isRegTiedToUseOperand(i)) continue;
501
David Goodwin7886cd82009-08-29 00:11:13 +0000502 KillIndices[Reg] = ~0u;
503
504 // Repeat for all subregs.
505 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
506 *Subreg; ++Subreg) {
507 KillIndices[*Subreg] = ~0u;
508 }
509 }
David Goodwin88a589c2009-08-25 17:03:05 +0000510
David Goodwin8f909342009-09-23 16:35:25 +0000511 // Examine all used registers and set/clear kill flag. When a
512 // register is used multiple times we only set the kill flag on
513 // the first use.
David Goodwin88a589c2009-08-25 17:03:05 +0000514 killedRegs.clear();
515 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
516 MachineOperand &MO = MI->getOperand(i);
517 if (!MO.isReg() || !MO.isUse()) continue;
518 unsigned Reg = MO.getReg();
519 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
520
David Goodwin7886cd82009-08-29 00:11:13 +0000521 bool kill = false;
522 if (killedRegs.find(Reg) == killedRegs.end()) {
523 kill = true;
524 // A register is not killed if any subregs are live...
525 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
526 *Subreg; ++Subreg) {
527 if (KillIndices[*Subreg] != ~0u) {
528 kill = false;
529 break;
530 }
531 }
532
533 // If subreg is not live, then register is killed if it became
534 // live in this instruction
535 if (kill)
536 kill = (KillIndices[Reg] == ~0u);
537 }
538
David Goodwin88a589c2009-08-25 17:03:05 +0000539 if (MO.isKill() != kill) {
David Goodwin8f909342009-09-23 16:35:25 +0000540 bool removed = ToggleKillFlag(MI, MO);
541 if (removed) {
542 DEBUG(errs() << "Fixed <removed> in ");
543 } else {
544 DEBUG(errs() << "Fixed " << MO << " in ");
545 }
David Goodwin88a589c2009-08-25 17:03:05 +0000546 DEBUG(MI->dump());
547 }
David Goodwin7886cd82009-08-29 00:11:13 +0000548
David Goodwin88a589c2009-08-25 17:03:05 +0000549 killedRegs.insert(Reg);
550 }
David Goodwin7886cd82009-08-29 00:11:13 +0000551
David Goodwina3251db2009-08-31 20:47:02 +0000552 // Mark any used register (that is not using undef) and subregs as
553 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000554 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
555 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000556 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000557 unsigned Reg = MO.getReg();
558 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
559
David Goodwin7886cd82009-08-29 00:11:13 +0000560 KillIndices[Reg] = Count;
561
562 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
563 *Subreg; ++Subreg) {
564 KillIndices[*Subreg] = Count;
565 }
566 }
David Goodwin88a589c2009-08-25 17:03:05 +0000567 }
568}
569
Dan Gohman343f0c02008-11-19 23:18:57 +0000570//===----------------------------------------------------------------------===//
571// Top-Down Scheduling
572//===----------------------------------------------------------------------===//
573
574/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
575/// the PendingQueue if the count reaches zero. Also update its cycle bound.
David Goodwin4de099d2009-11-03 20:57:50 +0000576void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge,
577 bool IgnoreAntiDep) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000578 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +0000579
Dan Gohman343f0c02008-11-19 23:18:57 +0000580#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +0000581 if (SuccSU->NumPredsLeft == 0) {
Chris Lattner103289e2009-08-23 07:19:13 +0000582 errs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +0000583 SuccSU->dump(this);
Chris Lattner103289e2009-08-23 07:19:13 +0000584 errs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000585 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +0000586 }
587#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +0000588 --SuccSU->NumPredsLeft;
589
Dan Gohman343f0c02008-11-19 23:18:57 +0000590 // Compute how many cycles it will be before this actually becomes
591 // available. This is the max of the start time of all predecessors plus
592 // their latencies.
David Goodwin4de099d2009-11-03 20:57:50 +0000593 SuccSU->setDepthToAtLeast(SU->getDepth(IgnoreAntiDep) +
594 SuccEdge->getLatency(), IgnoreAntiDep);
Dan Gohman343f0c02008-11-19 23:18:57 +0000595
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000596 // If all the node's predecessors are scheduled, this node is ready
597 // to be scheduled. Ignore the special ExitSU node.
598 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +0000599 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000600}
601
602/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwin4de099d2009-11-03 20:57:50 +0000603void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU, bool IgnoreAntiDep) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000604 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin4de099d2009-11-03 20:57:50 +0000605 I != E; ++I) {
606 if (IgnoreAntiDep && (I->getKind() == SDep::Anti)) continue;
607 ReleaseSucc(SU, &*I, IgnoreAntiDep);
608 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000609}
610
611/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
612/// count of its successors. If a successor pending count is zero, add it to
613/// the Available queue.
David Goodwin4de099d2009-11-03 20:57:50 +0000614void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle,
615 bool IgnoreAntiDep) {
David Goodwin3a5f0d42009-08-11 01:44:26 +0000616 DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +0000617 DEBUG(SU->dump(this));
618
619 Sequence.push_back(SU);
David Goodwin4de099d2009-11-03 20:57:50 +0000620 assert(CurCycle >= SU->getDepth(IgnoreAntiDep) &&
621 "Node scheduled above its depth!");
622 SU->setDepthToAtLeast(CurCycle, IgnoreAntiDep);
Dan Gohman343f0c02008-11-19 23:18:57 +0000623
David Goodwin4de099d2009-11-03 20:57:50 +0000624 ReleaseSuccessors(SU, IgnoreAntiDep);
Dan Gohman343f0c02008-11-19 23:18:57 +0000625 SU->isScheduled = true;
626 AvailableQueue.ScheduledNode(SU);
627}
628
629/// ListScheduleTopDown - The main loop of list scheduling for top-down
630/// schedulers.
David Goodwin4de099d2009-11-03 20:57:50 +0000631void SchedulePostRATDList::ListScheduleTopDown(
632 AntiDepBreaker::CandidateMap *AntiDepCandidates) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000633 unsigned CurCycle = 0;
David Goodwin4de099d2009-11-03 20:57:50 +0000634 const bool IgnoreAntiDep = (AntiDepCandidates != NULL);
635
636 // We're scheduling top-down but we're visiting the regions in
637 // bottom-up order, so we don't know the hazards at the start of a
638 // region. So assume no hazards (this should usually be ok as most
639 // blocks are a single region).
640 HazardRec->Reset();
641
642 // If ignoring anti-dependencies, the Schedule DAG still has Anti
643 // dep edges, but we ignore them for scheduling purposes
644 AvailableQueue.setIgnoreAntiDep(IgnoreAntiDep);
Dan Gohman343f0c02008-11-19 23:18:57 +0000645
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000646 // Release any successors of the special Entry node.
David Goodwin4de099d2009-11-03 20:57:50 +0000647 ReleaseSuccessors(&EntrySU, IgnoreAntiDep);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000648
David Goodwin4de099d2009-11-03 20:57:50 +0000649 // Add all leaves to Available queue. If ignoring antideps we also
650 // adjust the predecessor count for each node to not include antidep
651 // edges.
Dan Gohman343f0c02008-11-19 23:18:57 +0000652 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
653 // It is available if it has no predecessors.
David Goodwin4de099d2009-11-03 20:57:50 +0000654 bool available = SUnits[i].Preds.empty();
655 // If we are ignoring anti-dependencies then a node that has only
656 // anti-dep predecessors is available.
657 if (!available && IgnoreAntiDep) {
658 available = true;
659 for (SUnit::const_pred_iterator I = SUnits[i].Preds.begin(),
660 E = SUnits[i].Preds.end(); I != E; ++I) {
661 if (I->getKind() != SDep::Anti) {
662 available = false;
663 } else {
664 SUnits[i].NumPredsLeft -= 1;
665 }
666 }
667 }
668
669 if (available) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000670 AvailableQueue.push(&SUnits[i]);
671 SUnits[i].isAvailable = true;
672 }
673 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000674
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000675 // In any cycle where we can't schedule any instructions, we must
676 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000677 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000678
Dan Gohman343f0c02008-11-19 23:18:57 +0000679 // While Available queue is not empty, grab the node with the highest
680 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +0000681 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +0000682 Sequence.reserve(SUnits.size());
683 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
684 // Check to see if any of the pending instructions are ready to issue. If
685 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +0000686 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +0000687 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwin4de099d2009-11-03 20:57:50 +0000688 if (PendingQueue[i]->getDepth(IgnoreAntiDep) <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000689 AvailableQueue.push(PendingQueue[i]);
690 PendingQueue[i]->isAvailable = true;
691 PendingQueue[i] = PendingQueue.back();
692 PendingQueue.pop_back();
693 --i; --e;
David Goodwin4de099d2009-11-03 20:57:50 +0000694 } else if (PendingQueue[i]->getDepth(IgnoreAntiDep) < MinDepth)
695 MinDepth = PendingQueue[i]->getDepth(IgnoreAntiDep);
Dan Gohman343f0c02008-11-19 23:18:57 +0000696 }
David Goodwinc93d8372009-08-11 17:35:23 +0000697
David Goodwin7cd01182009-08-11 17:56:42 +0000698 DEBUG(errs() << "\n*** Examining Available\n";
699 LatencyPriorityQueue q = AvailableQueue;
700 while (!q.empty()) {
701 SUnit *su = q.pop();
David Goodwin4de099d2009-11-03 20:57:50 +0000702 errs() << "Height " << su->getHeight(IgnoreAntiDep) << ": ";
David Goodwin7cd01182009-08-11 17:56:42 +0000703 su->dump(this);
704 });
David Goodwinc93d8372009-08-11 17:35:23 +0000705
Dan Gohman2836c282009-01-16 01:33:36 +0000706 SUnit *FoundSUnit = 0;
Dan Gohman2836c282009-01-16 01:33:36 +0000707 bool HasNoopHazards = false;
708 while (!AvailableQueue.empty()) {
709 SUnit *CurSUnit = AvailableQueue.pop();
710
711 ScheduleHazardRecognizer::HazardType HT =
712 HazardRec->getHazardType(CurSUnit);
713 if (HT == ScheduleHazardRecognizer::NoHazard) {
714 FoundSUnit = CurSUnit;
715 break;
716 }
717
718 // Remember if this is a noop hazard.
719 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
720
721 NotReady.push_back(CurSUnit);
722 }
723
724 // Add the nodes that aren't ready back onto the available list.
725 if (!NotReady.empty()) {
726 AvailableQueue.push_all(NotReady);
727 NotReady.clear();
728 }
729
David Goodwin4de099d2009-11-03 20:57:50 +0000730 // If we found a node to schedule...
Dan Gohman343f0c02008-11-19 23:18:57 +0000731 if (FoundSUnit) {
David Goodwin4de099d2009-11-03 20:57:50 +0000732 // If we are ignoring anti-dependencies and the SUnit we are
733 // scheduling has an antidep predecessor that has not been
734 // scheduled, then we will need to break that antidep if we want
735 // to get this schedule when not ignoring anti-dependencies.
736 if (IgnoreAntiDep) {
737 AntiDepBreaker::AntiDepRegVector AntiDepRegs;
738 for (SUnit::const_pred_iterator I = FoundSUnit->Preds.begin(),
739 E = FoundSUnit->Preds.end(); I != E; ++I) {
740 if ((I->getKind() == SDep::Anti) && !I->getSUnit()->isScheduled)
741 AntiDepRegs.push_back(I->getReg());
742 }
743
744 if (AntiDepRegs.size() > 0) {
745 DEBUG(errs() << "*** AntiDep Candidate: ");
746 DEBUG(FoundSUnit->dump(this));
747 AntiDepCandidates->insert(
748 AntiDepBreaker::CandidateMap::value_type(FoundSUnit, AntiDepRegs));
749 }
750 }
751
752 // ... schedule the node...
753 ScheduleNodeTopDown(FoundSUnit, CurCycle, IgnoreAntiDep);
Dan Gohman2836c282009-01-16 01:33:36 +0000754 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000755 CycleHasInsts = true;
Dan Gohman343f0c02008-11-19 23:18:57 +0000756
David Goodwind94a4e52009-08-10 15:55:25 +0000757 // If we are using the target-specific hazards, then don't
758 // advance the cycle time just because we schedule a node. If
759 // the target allows it we can schedule multiple nodes in the
760 // same cycle.
761 if (!EnablePostRAHazardAvoidance) {
762 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
763 ++CurCycle;
764 }
Dan Gohman2836c282009-01-16 01:33:36 +0000765 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000766 if (CycleHasInsts) {
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000767 DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n');
768 HazardRec->AdvanceCycle();
769 } else if (!HasNoopHazards) {
770 // Otherwise, we have a pipeline stall, but no other problem,
771 // just advance the current cycle and try again.
772 DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n');
773 HazardRec->AdvanceCycle();
David Goodwin54097832009-11-05 01:19:35 +0000774 if (!IgnoreAntiDep)
775 ++NumStalls;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000776 } else {
777 // Otherwise, we have no instructions to issue and we have instructions
778 // that will fault if we don't do this right. This is the case for
779 // processors without pipeline interlocks and other cases.
780 DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n');
781 HazardRec->EmitNoop();
782 Sequence.push_back(0); // NULL here means noop
David Goodwin54097832009-11-05 01:19:35 +0000783 if (!IgnoreAntiDep)
784 ++NumNoops;
David Goodwin2ffb0ce2009-08-12 21:47:46 +0000785 }
786
Dan Gohman2836c282009-01-16 01:33:36 +0000787 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +0000788 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +0000789 }
790 }
791
792#ifndef NDEBUG
Dan Gohmana1e6d362008-11-20 01:26:25 +0000793 VerifySchedule(/*isBottomUp=*/false);
Dan Gohman343f0c02008-11-19 23:18:57 +0000794#endif
795}
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000796
797//===----------------------------------------------------------------------===//
798// Public Constructor Functions
799//===----------------------------------------------------------------------===//
800
Evan Chengfa163542009-10-16 21:06:15 +0000801FunctionPass *llvm::createPostRAScheduler(CodeGenOpt::Level OptLevel) {
802 return new PostRAScheduler(OptLevel);
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000803}