blob: e52158cfeb4e778f37207114074f2642505fd639 [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 Gohman21d90032008-11-25 00:52:40 +000059static cl::opt<bool>
60EnableAntiDepBreaking("break-anti-dependencies",
Dan Gohman00dc84a2008-12-16 19:27:52 +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 {
Dan Gohman343f0c02008-11-19 23:18:57 +000079 class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass {
Dan Gohmana70dca12009-10-09 23:27:56 +000080 AliasAnalysis *AA;
81
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000082 public:
83 static char ID;
Dan Gohman343f0c02008-11-19 23:18:57 +000084 PostRAScheduler() : MachineFunctionPass(&ID) {}
Dan Gohman21d90032008-11-25 00:52:40 +000085
Dan Gohman3f237442008-12-16 03:25:46 +000086 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000087 AU.setPreservesCFG();
Dan Gohmana70dca12009-10-09 23:27:56 +000088 AU.addRequired<AliasAnalysis>();
Dan Gohman3f237442008-12-16 03:25:46 +000089 AU.addRequired<MachineDominatorTree>();
90 AU.addPreserved<MachineDominatorTree>();
91 AU.addRequired<MachineLoopInfo>();
92 AU.addPreserved<MachineLoopInfo>();
93 MachineFunctionPass::getAnalysisUsage(AU);
94 }
95
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000096 const char *getPassName() const {
Dan Gohman21d90032008-11-25 00:52:40 +000097 return "Post RA top-down list latency scheduler";
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000098 }
99
100 bool runOnMachineFunction(MachineFunction &Fn);
101 };
Dan Gohman343f0c02008-11-19 23:18:57 +0000102 char PostRAScheduler::ID = 0;
103
104 class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +0000105 /// AvailableQueue - The priority queue to use for the available SUnits.
106 ///
107 LatencyPriorityQueue AvailableQueue;
108
109 /// PendingQueue - This contains all of the instructions whose operands have
110 /// been issued, but their results are not ready yet (due to the latency of
111 /// the operation). Once the operands becomes available, the instruction is
112 /// added to the AvailableQueue.
113 std::vector<SUnit*> PendingQueue;
114
Dan Gohman21d90032008-11-25 00:52:40 +0000115 /// Topo - A topological ordering for SUnits.
116 ScheduleDAGTopologicalSort Topo;
Dan Gohman343f0c02008-11-19 23:18:57 +0000117
Dan Gohman79ce2762009-01-15 19:20:50 +0000118 /// AllocatableSet - The set of allocatable registers.
119 /// We'll be ignoring anti-dependencies on non-allocatable registers,
120 /// because they may not be safe to break.
121 const BitVector AllocatableSet;
122
Dan Gohman2836c282009-01-16 01:33:36 +0000123 /// HazardRec - The hazard recognizer to use.
124 ScheduleHazardRecognizer *HazardRec;
125
Dan Gohmana70dca12009-10-09 23:27:56 +0000126 /// AA - AliasAnalysis for making memory reference queries.
127 AliasAnalysis *AA;
128
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000129 /// Classes - For live regs that are only used in one register class in a
130 /// live range, the register class. If the register is not live, the
131 /// corresponding value is null. If the register is live but used in
132 /// multiple register classes, the corresponding value is -1 casted to a
133 /// pointer.
134 const TargetRegisterClass *
135 Classes[TargetRegisterInfo::FirstVirtualRegister];
136
137 /// RegRegs - Map registers to all their references within a live range.
138 std::multimap<unsigned, MachineOperand *> RegRefs;
139
Evan Cheng714e8bc2009-10-01 08:26:23 +0000140 /// KillIndices - The index of the most recent kill (proceding bottom-up),
141 /// or ~0u if the register is not live.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000142 unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
143
Evan Cheng714e8bc2009-10-01 08:26:23 +0000144 /// DefIndices - The index of the most recent complete def (proceding bottom
145 /// up), or ~0u if the register is live.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000146 unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister];
147
Evan Cheng714e8bc2009-10-01 08:26:23 +0000148 /// KeepRegs - A set of registers which are live and cannot be changed to
149 /// break anti-dependencies.
150 SmallSet<unsigned, 4> KeepRegs;
151
Dan Gohman21d90032008-11-25 00:52:40 +0000152 public:
Dan Gohman79ce2762009-01-15 19:20:50 +0000153 SchedulePostRATDList(MachineFunction &MF,
Dan Gohman3f237442008-12-16 03:25:46 +0000154 const MachineLoopInfo &MLI,
Dan Gohman2836c282009-01-16 01:33:36 +0000155 const MachineDominatorTree &MDT,
Dan Gohmana70dca12009-10-09 23:27:56 +0000156 ScheduleHazardRecognizer *HR,
157 AliasAnalysis *aa)
Dan Gohman79ce2762009-01-15 19:20:50 +0000158 : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits),
Dan Gohman2836c282009-01-16 01:33:36 +0000159 AllocatableSet(TRI->getAllocatableSet(MF)),
Dan Gohmana70dca12009-10-09 23:27:56 +0000160 HazardRec(HR), AA(aa) {}
Dan Gohman2836c282009-01-16 01:33:36 +0000161
162 ~SchedulePostRATDList() {
163 delete HazardRec;
164 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000165
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000166 /// StartBlock - Initialize register live-range state for scheduling in
167 /// this block.
168 ///
169 void StartBlock(MachineBasicBlock *BB);
170
171 /// Schedule - Schedule the instruction range using list scheduling.
172 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000173 void Schedule();
David Goodwin88a589c2009-08-25 17:03:05 +0000174
175 /// FixupKills - Fix register kill flags that have been made
176 /// invalid due to scheduling
177 ///
178 void FixupKills(MachineBasicBlock *MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000179
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000180 /// Observe - Update liveness information to account for the current
181 /// instruction, which will not be scheduled.
182 ///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000183 void Observe(MachineInstr *MI, unsigned Count);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000184
185 /// FinishBlock - Clean up register live-range state.
186 ///
187 void FinishBlock();
188
Dan Gohman343f0c02008-11-19 23:18:57 +0000189 private:
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000190 void PrescanInstruction(MachineInstr *MI);
191 void ScanInstruction(MachineInstr *MI, unsigned Count);
Dan Gohman54e4c362008-12-09 22:54:47 +0000192 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000193 void ReleaseSuccessors(SUnit *SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000194 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
195 void ListScheduleTopDown();
Dan Gohman21d90032008-11-25 00:52:40 +0000196 bool BreakAntiDependencies();
Dan Gohman26255ad2009-08-12 01:33:27 +0000197 unsigned findSuitableFreeRegister(unsigned AntiDepReg,
198 unsigned LastNewReg,
199 const TargetRegisterClass *);
David Goodwin5e411782009-09-03 22:15:25 +0000200 void StartBlockForKills(MachineBasicBlock *BB);
David Goodwin8f909342009-09-23 16:35:25 +0000201
202 // ToggleKillFlag - Toggle a register operand kill flag. Other
203 // adjustments may be made to the instruction if necessary. Return
204 // true if the operand has been deleted, false if not.
205 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Dan Gohman343f0c02008-11-19 23:18:57 +0000206 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000207}
208
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000209/// isSchedulingBoundary - Test if the given instruction should be
210/// considered a scheduling boundary. This primarily includes labels
211/// and terminators.
212///
213static bool isSchedulingBoundary(const MachineInstr *MI,
214 const MachineFunction &MF) {
215 // Terminators and labels can't be scheduled around.
216 if (MI->getDesc().isTerminator() || MI->isLabel())
217 return true;
218
Dan Gohmanbed353d2009-02-10 23:29:38 +0000219 // Don't attempt to schedule around any instruction that modifies
220 // a stack-oriented pointer, as it's unlikely to be profitable. This
221 // saves compile time, because it doesn't require every single
222 // stack slot reference to depend on the instruction that does the
223 // modification.
224 const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
225 if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore()))
226 return true;
227
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000228 return false;
229}
230
Dan Gohman343f0c02008-11-19 23:18:57 +0000231bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Dan Gohman5bf7c2a2009-10-10 00:15:38 +0000232 AA = &getAnalysis<AliasAnalysis>();
233
David Goodwin471850a2009-10-01 21:46:35 +0000234 // Check for explicit enable/disable of post-ra scheduling.
235 if (EnablePostRAScheduler.getPosition() > 0) {
236 if (!EnablePostRAScheduler)
237 return true;
238 } else {
239 // Check that post-RA scheduling is enabled for this function
240 const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>();
241 if (!ST.enablePostRAScheduler())
242 return true;
243 }
David Goodwin0dad89f2009-09-30 00:10:16 +0000244
David Goodwin3a5f0d42009-08-11 01:44:26 +0000245 DEBUG(errs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000246
Dan Gohman3f237442008-12-16 03:25:46 +0000247 const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
248 const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
David Goodwind94a4e52009-08-10 15:55:25 +0000249 const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData();
Dan Gohman2836c282009-01-16 01:33:36 +0000250 ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ?
David Goodwind94a4e52009-08-10 15:55:25 +0000251 (ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) :
252 (ScheduleHazardRecognizer *)new SimpleHazardRecognizer();
Dan Gohman3f237442008-12-16 03:25:46 +0000253
Dan Gohmana70dca12009-10-09 23:27:56 +0000254 SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR, AA);
Dan Gohman79ce2762009-01-15 19:20:50 +0000255
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000256 // Loop over all of the basic blocks
257 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000258 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000259#ifndef NDEBUG
260 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
261 if (DebugDiv > 0) {
262 static int bbcnt = 0;
263 if (bbcnt++ % DebugDiv != DebugMod)
264 continue;
265 errs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() <<
266 ":MBB ID#" << MBB->getNumber() << " ***\n";
267 }
268#endif
269
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000270 // Initialize register live-range state for scheduling in this block.
271 Scheduler.StartBlock(MBB);
272
Dan Gohmanf7119392009-01-16 22:10:20 +0000273 // Schedule each sequence of instructions not interrupted by a label
274 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000275 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000276 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000277 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
278 MachineInstr *MI = prior(I);
279 if (isSchedulingBoundary(MI, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000280 Scheduler.Run(MBB, I, Current, CurrentCount);
Evan Chengfb2e7522009-09-18 21:02:19 +0000281 Scheduler.EmitSchedule(0);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000282 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000283 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000284 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000285 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000286 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000287 --Count;
Dan Gohman43f07fb2009-02-03 18:57:45 +0000288 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000289 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000290 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000291 "Instruction count mismatch!");
292 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Evan Chengfb2e7522009-09-18 21:02:19 +0000293 Scheduler.EmitSchedule(0);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000294
295 // Clean up register live-range state.
296 Scheduler.FinishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000297
David Goodwin5e411782009-09-03 22:15:25 +0000298 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000299 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000300 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000301
302 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 Goodwind94a4e52009-08-10 15:55:25 +0000312 // Reset the hazard recognizer.
313 HazardRec->Reset();
314
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000315 // Clear out the register class data.
316 std::fill(Classes, array_endof(Classes),
317 static_cast<const TargetRegisterClass *>(0));
Dan Gohman21d90032008-11-25 00:52:40 +0000318
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000319 // Initialize the indices to indicate that no registers are live.
Dan Gohman6c3643c2008-12-19 22:23:43 +0000320 std::fill(KillIndices, array_endof(KillIndices), ~0u);
Dan Gohman21d90032008-11-25 00:52:40 +0000321 std::fill(DefIndices, array_endof(DefIndices), BB->size());
322
Evan Cheng714e8bc2009-10-01 08:26:23 +0000323 // Clear "do not change" set.
324 KeepRegs.clear();
325
David Goodwin63bcbb72009-10-01 23:28:47 +0000326 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
327
Dan Gohman21d90032008-11-25 00:52:40 +0000328 // Determine the live-out physregs for this block.
David Goodwin63bcbb72009-10-01 23:28:47 +0000329 if (IsReturnBlock) {
Dan Gohman21d90032008-11-25 00:52:40 +0000330 // In a return block, examine the function live-out regs.
331 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
332 E = MRI.liveout_end(); I != E; ++I) {
333 unsigned Reg = *I;
334 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
335 KillIndices[Reg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000336 DefIndices[Reg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000337 // Repeat, for all aliases.
338 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
339 unsigned AliasReg = *Alias;
340 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
341 KillIndices[AliasReg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000342 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000343 }
344 }
David Goodwinc7951f82009-10-01 19:45:32 +0000345 } else {
Dan Gohman21d90032008-11-25 00:52:40 +0000346 // In a non-return block, examine the live-in regs of all successors.
347 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
Dan Gohman47ac0f02009-02-11 04:27:20 +0000348 SE = BB->succ_end(); SI != SE; ++SI)
Dan Gohman21d90032008-11-25 00:52:40 +0000349 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
350 E = (*SI)->livein_end(); I != E; ++I) {
351 unsigned Reg = *I;
352 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
353 KillIndices[Reg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000354 DefIndices[Reg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000355 // Repeat, for all aliases.
356 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
357 unsigned AliasReg = *Alias;
358 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
359 KillIndices[AliasReg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000360 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000361 }
362 }
David Goodwin63bcbb72009-10-01 23:28:47 +0000363 }
Dan Gohman21d90032008-11-25 00:52:40 +0000364
David Goodwin63bcbb72009-10-01 23:28:47 +0000365 // Mark live-out callee-saved registers. In a return block this is
366 // all callee-saved registers. In non-return this is any
367 // callee-saved register that is not saved in the prolog.
368 const MachineFrameInfo *MFI = MF.getFrameInfo();
369 BitVector Pristine = MFI->getPristineRegs(BB);
370 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
371 unsigned Reg = *I;
372 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
373 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
374 KillIndices[Reg] = BB->size();
375 DefIndices[Reg] = ~0u;
376 // Repeat, for all aliases.
377 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
378 unsigned AliasReg = *Alias;
379 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
380 KillIndices[AliasReg] = BB->size();
381 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000382 }
383 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000384}
385
386/// Schedule - Schedule the instruction range using list scheduling.
387///
388void SchedulePostRATDList::Schedule() {
David Goodwin3a5f0d42009-08-11 01:44:26 +0000389 DEBUG(errs() << "********** List Scheduling **********\n");
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000390
391 // Build the scheduling graph.
Dan Gohmana70dca12009-10-09 23:27:56 +0000392 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000393
394 if (EnableAntiDepBreaking) {
395 if (BreakAntiDependencies()) {
396 // We made changes. Update the dependency graph.
397 // Theoretically we could update the graph in place:
398 // When a live range is changed to use a different register, remove
399 // the def's anti-dependence *and* output-dependence edges due to
400 // that register, and add new anti-dependence and output-dependence
401 // edges based on the next live range of the register.
402 SUnits.clear();
403 EntrySU = SUnit();
404 ExitSU = SUnit();
Dan Gohmana70dca12009-10-09 23:27:56 +0000405 BuildSchedGraph(AA);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000406 }
407 }
408
David Goodwind94a4e52009-08-10 15:55:25 +0000409 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
410 SUnits[su].dumpAll(this));
411
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000412 AvailableQueue.initNodes(SUnits);
413
414 ListScheduleTopDown();
415
416 AvailableQueue.releaseState();
417}
418
419/// Observe - Update liveness information to account for the current
420/// instruction, which will not be scheduled.
421///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000422void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000423 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
424
425 // Any register which was defined within the previous scheduling region
426 // may have been rescheduled and its lifetime may overlap with registers
427 // in ways not reflected in our current liveness state. For each such
428 // register, adjust the liveness state to be conservatively correct.
429 for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg)
430 if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
431 assert(KillIndices[Reg] == ~0u && "Clobbered register is live!");
432 // Mark this register to be non-renamable.
433 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
434 // Move the def index to the end of the previous region, to reflect
435 // that the def could theoretically have been scheduled at the end.
436 DefIndices[Reg] = InsertPosIndex;
437 }
438
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000439 PrescanInstruction(MI);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000440 ScanInstruction(MI, Count);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000441}
442
443/// FinishBlock - Clean up register live-range state.
444///
445void SchedulePostRATDList::FinishBlock() {
446 RegRefs.clear();
447
448 // Call the superclass.
449 ScheduleDAGInstrs::FinishBlock();
450}
451
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000452/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
453/// critical path.
454static SDep *CriticalPathStep(SUnit *SU) {
455 SDep *Next = 0;
456 unsigned NextDepth = 0;
457 // Find the predecessor edge with the greatest depth.
458 for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
459 P != PE; ++P) {
460 SUnit *PredSU = P->getSUnit();
461 unsigned PredLatency = P->getLatency();
462 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
463 // In the case of a latency tie, prefer an anti-dependency edge over
464 // other types of edges.
465 if (NextDepth < PredTotalLatency ||
466 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
467 NextDepth = PredTotalLatency;
468 Next = &*P;
469 }
470 }
471 return Next;
472}
473
474void SchedulePostRATDList::PrescanInstruction(MachineInstr *MI) {
475 // Scan the register operands for this instruction and update
476 // Classes and RegRefs.
477 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
478 MachineOperand &MO = MI->getOperand(i);
479 if (!MO.isReg()) continue;
480 unsigned Reg = MO.getReg();
481 if (Reg == 0) continue;
Chris Lattner2a386882009-07-29 21:36:49 +0000482 const TargetRegisterClass *NewRC = 0;
483
484 if (i < MI->getDesc().getNumOperands())
485 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000486
487 // For now, only allow the register to be changed if its register
488 // class is consistent across all uses.
489 if (!Classes[Reg] && NewRC)
490 Classes[Reg] = NewRC;
491 else if (!NewRC || Classes[Reg] != NewRC)
492 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
493
494 // Now check for aliases.
495 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
496 // If an alias of the reg is used during the live range, give up.
497 // Note that this allows us to skip checking if AntiDepReg
498 // overlaps with any of the aliases, among other things.
499 unsigned AliasReg = *Alias;
500 if (Classes[AliasReg]) {
501 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
502 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
503 }
504 }
505
506 // If we're still willing to consider this register, note the reference.
507 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
508 RegRefs.insert(std::make_pair(Reg, &MO));
David Goodwinc7951f82009-10-01 19:45:32 +0000509
510 // It's not safe to change register allocation for source operands of
511 // that have special allocation requirements.
512 if (MO.isUse() && MI->getDesc().hasExtraSrcRegAllocReq()) {
513 if (KeepRegs.insert(Reg)) {
514 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
515 *Subreg; ++Subreg)
516 KeepRegs.insert(*Subreg);
517 }
518 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000519 }
520}
521
522void SchedulePostRATDList::ScanInstruction(MachineInstr *MI,
523 unsigned Count) {
524 // Update liveness.
525 // Proceding upwards, registers that are defed but not used in this
526 // instruction are now dead.
527 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
528 MachineOperand &MO = MI->getOperand(i);
529 if (!MO.isReg()) continue;
530 unsigned Reg = MO.getReg();
531 if (Reg == 0) continue;
532 if (!MO.isDef()) continue;
533 // Ignore two-addr defs.
Bob Wilsond9df5012009-04-09 17:16:43 +0000534 if (MI->isRegTiedToUseOperand(i)) continue;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000535
536 DefIndices[Reg] = Count;
537 KillIndices[Reg] = ~0u;
Evan Cheng714e8bc2009-10-01 08:26:23 +0000538 assert(((KillIndices[Reg] == ~0u) !=
539 (DefIndices[Reg] == ~0u)) &&
540 "Kill and Def maps aren't consistent for Reg!");
541 KeepRegs.erase(Reg);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000542 Classes[Reg] = 0;
543 RegRefs.erase(Reg);
544 // Repeat, for all subregs.
545 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
546 *Subreg; ++Subreg) {
547 unsigned SubregReg = *Subreg;
548 DefIndices[SubregReg] = Count;
549 KillIndices[SubregReg] = ~0u;
Evan Cheng714e8bc2009-10-01 08:26:23 +0000550 KeepRegs.erase(SubregReg);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000551 Classes[SubregReg] = 0;
552 RegRefs.erase(SubregReg);
553 }
David Goodwin7886cd82009-08-29 00:11:13 +0000554 // Conservatively mark super-registers as unusable.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000555 for (const unsigned *Super = TRI->getSuperRegisters(Reg);
556 *Super; ++Super) {
557 unsigned SuperReg = *Super;
558 Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
559 }
560 }
561 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
562 MachineOperand &MO = MI->getOperand(i);
563 if (!MO.isReg()) continue;
564 unsigned Reg = MO.getReg();
565 if (Reg == 0) continue;
566 if (!MO.isUse()) continue;
567
Chris Lattner2a386882009-07-29 21:36:49 +0000568 const TargetRegisterClass *NewRC = 0;
569 if (i < MI->getDesc().getNumOperands())
570 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000571
572 // For now, only allow the register to be changed if its register
573 // class is consistent across all uses.
574 if (!Classes[Reg] && NewRC)
575 Classes[Reg] = NewRC;
576 else if (!NewRC || Classes[Reg] != NewRC)
577 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
578
579 RegRefs.insert(std::make_pair(Reg, &MO));
580
581 // It wasn't previously live but now it is, this is a kill.
582 if (KillIndices[Reg] == ~0u) {
583 KillIndices[Reg] = Count;
584 DefIndices[Reg] = ~0u;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000585 assert(((KillIndices[Reg] == ~0u) !=
586 (DefIndices[Reg] == ~0u)) &&
587 "Kill and Def maps aren't consistent for Reg!");
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000588 }
589 // Repeat, for all aliases.
590 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
591 unsigned AliasReg = *Alias;
592 if (KillIndices[AliasReg] == ~0u) {
593 KillIndices[AliasReg] = Count;
594 DefIndices[AliasReg] = ~0u;
595 }
596 }
597 }
598}
599
Dan Gohman26255ad2009-08-12 01:33:27 +0000600unsigned
601SchedulePostRATDList::findSuitableFreeRegister(unsigned AntiDepReg,
602 unsigned LastNewReg,
603 const TargetRegisterClass *RC) {
604 for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF),
605 RE = RC->allocation_order_end(MF); R != RE; ++R) {
606 unsigned NewReg = *R;
607 // Don't replace a register with itself.
608 if (NewReg == AntiDepReg) continue;
609 // Don't replace a register with one that was recently used to repair
610 // an anti-dependence with this AntiDepReg, because that would
611 // re-introduce that anti-dependence.
612 if (NewReg == LastNewReg) continue;
613 // If NewReg is dead and NewReg's most recent def is not before
614 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
615 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u)) &&
616 "Kill and Def maps aren't consistent for AntiDepReg!");
617 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u)) &&
618 "Kill and Def maps aren't consistent for NewReg!");
Dan Gohmanda277572009-08-12 01:44:20 +0000619 if (KillIndices[NewReg] != ~0u ||
620 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
621 KillIndices[AntiDepReg] > DefIndices[NewReg])
Dan Gohman26255ad2009-08-12 01:33:27 +0000622 continue;
623 return NewReg;
624 }
625
626 // No registers are free and available!
627 return 0;
628}
629
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000630/// BreakAntiDependencies - Identifiy anti-dependencies along the critical path
631/// of the ScheduleDAG and break them by renaming registers.
632///
633bool SchedulePostRATDList::BreakAntiDependencies() {
634 // The code below assumes that there is at least one instruction,
635 // so just duck out immediately if the block is empty.
636 if (SUnits.empty()) return false;
637
638 // Find the node at the bottom of the critical path.
639 SUnit *Max = 0;
640 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
641 SUnit *SU = &SUnits[i];
642 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
643 Max = SU;
644 }
645
David Goodwind452ea62009-10-13 19:16:03 +0000646#ifndef NDEBUG
647 {
648 DEBUG(errs() << "Critical path has total latency "
649 << (Max->getDepth() + Max->Latency) << "\n");
650 DEBUG(errs() << "Available regs:");
651 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
652 if (KillIndices[Reg] == ~0u)
653 DEBUG(errs() << " " << TRI->getName(Reg));
654 }
655 DEBUG(errs() << '\n');
656 }
657#endif
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000658
659 // Track progress along the critical path through the SUnit graph as we walk
660 // the instructions.
661 SUnit *CriticalPathSU = Max;
662 MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
Dan Gohman21d90032008-11-25 00:52:40 +0000663
664 // Consider this pattern:
665 // A = ...
666 // ... = A
667 // A = ...
668 // ... = A
669 // A = ...
670 // ... = A
671 // A = ...
672 // ... = A
673 // There are three anti-dependencies here, and without special care,
674 // we'd break all of them using the same register:
675 // A = ...
676 // ... = A
677 // B = ...
678 // ... = B
679 // B = ...
680 // ... = B
681 // B = ...
682 // ... = B
683 // because at each anti-dependence, B is the first register that
684 // isn't A which is free. This re-introduces anti-dependencies
685 // at all but one of the original anti-dependencies that we were
686 // trying to break. To avoid this, keep track of the most recent
David Goodwinc93d8372009-08-11 17:35:23 +0000687 // register that each register was replaced with, avoid
Dan Gohman21d90032008-11-25 00:52:40 +0000688 // using it to repair an anti-dependence on the same register.
689 // This lets us produce this:
690 // A = ...
691 // ... = A
692 // B = ...
693 // ... = B
694 // C = ...
695 // ... = C
696 // B = ...
697 // ... = B
698 // This still has an anti-dependence on B, but at least it isn't on the
699 // original critical path.
700 //
701 // TODO: If we tracked more than one register here, we could potentially
702 // fix that remaining critical edge too. This is a little more involved,
703 // because unlike the most recent register, less recent registers should
704 // still be considered, though only if no other registers are available.
705 unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {};
706
Dan Gohman21d90032008-11-25 00:52:40 +0000707 // Attempt to break anti-dependence edges on the critical path. Walk the
708 // instructions from the bottom up, tracking information about liveness
709 // as we go to help determine which registers are available.
710 bool Changed = false;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000711 unsigned Count = InsertPosIndex - 1;
712 for (MachineBasicBlock::iterator I = InsertPos, E = Begin;
Dan Gohman43f07fb2009-02-03 18:57:45 +0000713 I != E; --Count) {
714 MachineInstr *MI = --I;
Dan Gohman21d90032008-11-25 00:52:40 +0000715
Dan Gohman00dc84a2008-12-16 19:27:52 +0000716 // Check if this instruction has a dependence on the critical path that
717 // is an anti-dependence that we may be able to break. If it is, set
718 // AntiDepReg to the non-zero register associated with the anti-dependence.
719 //
720 // We limit our attention to the critical path as a heuristic to avoid
721 // breaking anti-dependence edges that aren't going to significantly
722 // impact the overall schedule. There are a limited number of registers
723 // and we want to save them for the important edges.
724 //
725 // TODO: Instructions with multiple defs could have multiple
726 // anti-dependencies. The current code here only knows how to break one
727 // edge per instruction. Note that we'd have to be able to break all of
728 // the anti-dependencies in an instruction in order to be effective.
729 unsigned AntiDepReg = 0;
730 if (MI == CriticalPathMI) {
731 if (SDep *Edge = CriticalPathStep(CriticalPathSU)) {
732 SUnit *NextSU = Edge->getSUnit();
733
734 // Only consider anti-dependence edges.
735 if (Edge->getKind() == SDep::Anti) {
736 AntiDepReg = Edge->getReg();
737 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Dan Gohman49bb50e2009-01-16 21:57:43 +0000738 if (!AllocatableSet.test(AntiDepReg))
Evan Cheng714e8bc2009-10-01 08:26:23 +0000739 // Don't break anti-dependencies on non-allocatable registers.
740 AntiDepReg = 0;
741 else if (KeepRegs.count(AntiDepReg))
742 // Don't break anti-dependencies if an use down below requires
743 // this exact register.
Dan Gohman49bb50e2009-01-16 21:57:43 +0000744 AntiDepReg = 0;
745 else {
Dan Gohman00dc84a2008-12-16 19:27:52 +0000746 // If the SUnit has other dependencies on the SUnit that it
747 // anti-depends on, don't bother breaking the anti-dependency
748 // since those edges would prevent such units from being
749 // scheduled past each other regardless.
750 //
751 // Also, if there are dependencies on other SUnits with the
752 // same register as the anti-dependency, don't attempt to
753 // break it.
754 for (SUnit::pred_iterator P = CriticalPathSU->Preds.begin(),
755 PE = CriticalPathSU->Preds.end(); P != PE; ++P)
756 if (P->getSUnit() == NextSU ?
757 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
758 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
759 AntiDepReg = 0;
760 break;
761 }
762 }
763 }
764 CriticalPathSU = NextSU;
765 CriticalPathMI = CriticalPathSU->getInstr();
766 } else {
767 // We've reached the end of the critical path.
768 CriticalPathSU = 0;
769 CriticalPathMI = 0;
770 }
771 }
Dan Gohman21d90032008-11-25 00:52:40 +0000772
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000773 PrescanInstruction(MI);
774
Evan Cheng714e8bc2009-10-01 08:26:23 +0000775 if (MI->getDesc().hasExtraDefRegAllocReq())
776 // If this instruction's defs have special allocation requirement, don't
777 // break this anti-dependency.
778 AntiDepReg = 0;
779 else if (AntiDepReg) {
780 // If this instruction has a use of AntiDepReg, breaking it
781 // is invalid.
782 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
783 MachineOperand &MO = MI->getOperand(i);
784 if (!MO.isReg()) continue;
785 unsigned Reg = MO.getReg();
786 if (Reg == 0) continue;
787 if (MO.isUse() && AntiDepReg == Reg) {
788 AntiDepReg = 0;
789 break;
790 }
Dan Gohman21d90032008-11-25 00:52:40 +0000791 }
Dan Gohman21d90032008-11-25 00:52:40 +0000792 }
793
794 // Determine AntiDepReg's register class, if it is live and is
795 // consistently used within a single class.
796 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
Nick Lewyckya89d1022008-11-27 17:29:52 +0000797 assert((AntiDepReg == 0 || RC != NULL) &&
Dan Gohman21d90032008-11-25 00:52:40 +0000798 "Register should be live if it's causing an anti-dependence!");
799 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
800 AntiDepReg = 0;
801
802 // Look for a suitable register to use to break the anti-depenence.
803 //
804 // TODO: Instead of picking the first free register, consider which might
805 // be the best.
806 if (AntiDepReg != 0) {
Dan Gohman26255ad2009-08-12 01:33:27 +0000807 if (unsigned NewReg = findSuitableFreeRegister(AntiDepReg,
808 LastNewReg[AntiDepReg],
809 RC)) {
810 DEBUG(errs() << "Breaking anti-dependence edge on "
811 << TRI->getName(AntiDepReg)
812 << " with " << RegRefs.count(AntiDepReg) << " references"
813 << " using " << TRI->getName(NewReg) << "!\n");
Dan Gohman21d90032008-11-25 00:52:40 +0000814
Dan Gohman26255ad2009-08-12 01:33:27 +0000815 // Update the references to the old register to refer to the new
816 // register.
817 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
818 std::multimap<unsigned, MachineOperand *>::iterator>
819 Range = RegRefs.equal_range(AntiDepReg);
820 for (std::multimap<unsigned, MachineOperand *>::iterator
821 Q = Range.first, QE = Range.second; Q != QE; ++Q)
822 Q->second->setReg(NewReg);
Dan Gohman21d90032008-11-25 00:52:40 +0000823
Dan Gohman26255ad2009-08-12 01:33:27 +0000824 // We just went back in time and modified history; the
825 // liveness information for the anti-depenence reg is now
826 // inconsistent. Set the state as if it were dead.
827 Classes[NewReg] = Classes[AntiDepReg];
828 DefIndices[NewReg] = DefIndices[AntiDepReg];
829 KillIndices[NewReg] = KillIndices[AntiDepReg];
830 assert(((KillIndices[NewReg] == ~0u) !=
831 (DefIndices[NewReg] == ~0u)) &&
832 "Kill and Def maps aren't consistent for NewReg!");
Dan Gohman21d90032008-11-25 00:52:40 +0000833
Dan Gohman26255ad2009-08-12 01:33:27 +0000834 Classes[AntiDepReg] = 0;
835 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
836 KillIndices[AntiDepReg] = ~0u;
837 assert(((KillIndices[AntiDepReg] == ~0u) !=
838 (DefIndices[AntiDepReg] == ~0u)) &&
839 "Kill and Def maps aren't consistent for AntiDepReg!");
Dan Gohman21d90032008-11-25 00:52:40 +0000840
Dan Gohman26255ad2009-08-12 01:33:27 +0000841 RegRefs.erase(AntiDepReg);
842 Changed = true;
843 LastNewReg[AntiDepReg] = NewReg;
Dan Gohman21d90032008-11-25 00:52:40 +0000844 }
845 }
846
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000847 ScanInstruction(MI, Count);
Dan Gohman21d90032008-11-25 00:52:40 +0000848 }
Dan Gohman21d90032008-11-25 00:52:40 +0000849
850 return Changed;
851}
852
David Goodwin5e411782009-09-03 22:15:25 +0000853/// StartBlockForKills - Initialize register live-range state for updating kills
854///
855void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
856 // Initialize the indices to indicate that no registers are live.
857 std::fill(KillIndices, array_endof(KillIndices), ~0u);
858
859 // Determine the live-out physregs for this block.
860 if (!BB->empty() && BB->back().getDesc().isReturn()) {
861 // In a return block, examine the function live-out regs.
862 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
863 E = MRI.liveout_end(); I != E; ++I) {
864 unsigned Reg = *I;
865 KillIndices[Reg] = BB->size();
866 // Repeat, for all subregs.
867 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
868 *Subreg; ++Subreg) {
869 KillIndices[*Subreg] = BB->size();
870 }
871 }
872 }
873 else {
874 // In a non-return block, examine the live-in regs of all successors.
875 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
876 SE = BB->succ_end(); SI != SE; ++SI) {
877 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
878 E = (*SI)->livein_end(); I != E; ++I) {
879 unsigned Reg = *I;
880 KillIndices[Reg] = BB->size();
881 // Repeat, for all subregs.
882 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
883 *Subreg; ++Subreg) {
884 KillIndices[*Subreg] = BB->size();
885 }
886 }
887 }
888 }
889}
890
David Goodwin8f909342009-09-23 16:35:25 +0000891bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
892 MachineOperand &MO) {
893 // Setting kill flag...
894 if (!MO.isKill()) {
895 MO.setIsKill(true);
896 return false;
897 }
898
899 // If MO itself is live, clear the kill flag...
900 if (KillIndices[MO.getReg()] != ~0u) {
901 MO.setIsKill(false);
902 return false;
903 }
904
905 // If any subreg of MO is live, then create an imp-def for that
906 // subreg and keep MO marked as killed.
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000907 MO.setIsKill(false);
David Goodwin8f909342009-09-23 16:35:25 +0000908 bool AllDead = true;
909 const unsigned SuperReg = MO.getReg();
910 for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
911 *Subreg; ++Subreg) {
912 if (KillIndices[*Subreg] != ~0u) {
913 MI->addOperand(MachineOperand::CreateReg(*Subreg,
914 true /*IsDef*/,
915 true /*IsImp*/,
916 false /*IsKill*/,
917 false /*IsDead*/));
918 AllDead = false;
919 }
920 }
921
Benjamin Kramer8bff4af2009-10-02 15:59:52 +0000922 if(AllDead)
923 MO.setIsKill(true);
David Goodwin8f909342009-09-23 16:35:25 +0000924 return false;
925}
926
David Goodwin88a589c2009-08-25 17:03:05 +0000927/// FixupKills - Fix the register kill flags, they may have been made
928/// incorrect by instruction reordering.
929///
930void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
931 DEBUG(errs() << "Fixup kills for BB ID#" << MBB->getNumber() << '\n');
932
933 std::set<unsigned> killedRegs;
934 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000935
936 StartBlockForKills(MBB);
David Goodwin7886cd82009-08-29 00:11:13 +0000937
938 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000939 unsigned Count = MBB->size();
940 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
941 I != E; --Count) {
942 MachineInstr *MI = --I;
943
David Goodwin7886cd82009-08-29 00:11:13 +0000944 // Update liveness. Registers that are defed but not used in this
945 // instruction are now dead. Mark register and all subregs as they
946 // are completely defined.
947 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
948 MachineOperand &MO = MI->getOperand(i);
949 if (!MO.isReg()) continue;
950 unsigned Reg = MO.getReg();
951 if (Reg == 0) continue;
952 if (!MO.isDef()) continue;
953 // Ignore two-addr defs.
954 if (MI->isRegTiedToUseOperand(i)) continue;
955
David Goodwin7886cd82009-08-29 00:11:13 +0000956 KillIndices[Reg] = ~0u;
957
958 // Repeat for all subregs.
959 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
960 *Subreg; ++Subreg) {
961 KillIndices[*Subreg] = ~0u;
962 }
963 }
David Goodwin88a589c2009-08-25 17:03:05 +0000964
David Goodwin8f909342009-09-23 16:35:25 +0000965 // Examine all used registers and set/clear kill flag. When a
966 // register is used multiple times we only set the kill flag on
967 // the first use.
David Goodwin88a589c2009-08-25 17:03:05 +0000968 killedRegs.clear();
969 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
970 MachineOperand &MO = MI->getOperand(i);
971 if (!MO.isReg() || !MO.isUse()) continue;
972 unsigned Reg = MO.getReg();
973 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
974
David Goodwin7886cd82009-08-29 00:11:13 +0000975 bool kill = false;
976 if (killedRegs.find(Reg) == killedRegs.end()) {
977 kill = true;
978 // A register is not killed if any subregs are live...
979 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
980 *Subreg; ++Subreg) {
981 if (KillIndices[*Subreg] != ~0u) {
982 kill = false;
983 break;
984 }
985 }
986
987 // If subreg is not live, then register is killed if it became
988 // live in this instruction
989 if (kill)
990 kill = (KillIndices[Reg] == ~0u);
991 }
992
David Goodwin88a589c2009-08-25 17:03:05 +0000993 if (MO.isKill() != kill) {
David Goodwin8f909342009-09-23 16:35:25 +0000994 bool removed = ToggleKillFlag(MI, MO);
995 if (removed) {
996 DEBUG(errs() << "Fixed <removed> in ");
997 } else {
998 DEBUG(errs() << "Fixed " << MO << " in ");
999 }
David Goodwin88a589c2009-08-25 17:03:05 +00001000 DEBUG(MI->dump());
1001 }
David Goodwin7886cd82009-08-29 00:11:13 +00001002
David Goodwin88a589c2009-08-25 17:03:05 +00001003 killedRegs.insert(Reg);
1004 }
David Goodwin7886cd82009-08-29 00:11:13 +00001005
David Goodwina3251db2009-08-31 20:47:02 +00001006 // Mark any used register (that is not using undef) and subregs as
1007 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +00001008 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1009 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +00001010 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +00001011 unsigned Reg = MO.getReg();
1012 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
1013
David Goodwin7886cd82009-08-29 00:11:13 +00001014 KillIndices[Reg] = Count;
1015
1016 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
1017 *Subreg; ++Subreg) {
1018 KillIndices[*Subreg] = Count;
1019 }
1020 }
David Goodwin88a589c2009-08-25 17:03:05 +00001021 }
1022}
1023
Dan Gohman343f0c02008-11-19 23:18:57 +00001024//===----------------------------------------------------------------------===//
1025// Top-Down Scheduling
1026//===----------------------------------------------------------------------===//
1027
1028/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
1029/// the PendingQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman54e4c362008-12-09 22:54:47 +00001030void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
1031 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +00001032
Dan Gohman343f0c02008-11-19 23:18:57 +00001033#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +00001034 if (SuccSU->NumPredsLeft == 0) {
Chris Lattner103289e2009-08-23 07:19:13 +00001035 errs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +00001036 SuccSU->dump(this);
Chris Lattner103289e2009-08-23 07:19:13 +00001037 errs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001038 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +00001039 }
1040#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +00001041 --SuccSU->NumPredsLeft;
1042
Dan Gohman343f0c02008-11-19 23:18:57 +00001043 // Compute how many cycles it will be before this actually becomes
1044 // available. This is the max of the start time of all predecessors plus
1045 // their latencies.
Dan Gohman3f237442008-12-16 03:25:46 +00001046 SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
Dan Gohman343f0c02008-11-19 23:18:57 +00001047
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001048 // If all the node's predecessors are scheduled, this node is ready
1049 // to be scheduled. Ignore the special ExitSU node.
1050 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +00001051 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001052}
1053
1054/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
1055void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
1056 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1057 I != E; ++I)
1058 ReleaseSucc(SU, &*I);
Dan Gohman343f0c02008-11-19 23:18:57 +00001059}
1060
1061/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1062/// count of its successors. If a successor pending count is zero, add it to
1063/// the Available queue.
1064void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Goodwin3a5f0d42009-08-11 01:44:26 +00001065 DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +00001066 DEBUG(SU->dump(this));
1067
1068 Sequence.push_back(SU);
Dan Gohman3f237442008-12-16 03:25:46 +00001069 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
1070 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +00001071
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001072 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +00001073 SU->isScheduled = true;
1074 AvailableQueue.ScheduledNode(SU);
1075}
1076
1077/// ListScheduleTopDown - The main loop of list scheduling for top-down
1078/// schedulers.
1079void SchedulePostRATDList::ListScheduleTopDown() {
1080 unsigned CurCycle = 0;
1081
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001082 // Release any successors of the special Entry node.
1083 ReleaseSuccessors(&EntrySU);
1084
Dan Gohman343f0c02008-11-19 23:18:57 +00001085 // All leaves to Available queue.
1086 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1087 // It is available if it has no predecessors.
1088 if (SUnits[i].Preds.empty()) {
1089 AvailableQueue.push(&SUnits[i]);
1090 SUnits[i].isAvailable = true;
1091 }
1092 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001093
David Goodwin2ffb0ce2009-08-12 21:47:46 +00001094 // In any cycle where we can't schedule any instructions, we must
1095 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001096 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +00001097
Dan Gohman343f0c02008-11-19 23:18:57 +00001098 // While Available queue is not empty, grab the node with the highest
1099 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +00001100 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +00001101 Sequence.reserve(SUnits.size());
1102 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
1103 // Check to see if any of the pending instructions are ready to issue. If
1104 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +00001105 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +00001106 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohman3f237442008-12-16 03:25:46 +00001107 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +00001108 AvailableQueue.push(PendingQueue[i]);
1109 PendingQueue[i]->isAvailable = true;
1110 PendingQueue[i] = PendingQueue.back();
1111 PendingQueue.pop_back();
1112 --i; --e;
Dan Gohman3f237442008-12-16 03:25:46 +00001113 } else if (PendingQueue[i]->getDepth() < MinDepth)
1114 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +00001115 }
David Goodwinc93d8372009-08-11 17:35:23 +00001116
David Goodwin7cd01182009-08-11 17:56:42 +00001117 DEBUG(errs() << "\n*** Examining Available\n";
1118 LatencyPriorityQueue q = AvailableQueue;
1119 while (!q.empty()) {
1120 SUnit *su = q.pop();
1121 errs() << "Height " << su->getHeight() << ": ";
1122 su->dump(this);
1123 });
David Goodwinc93d8372009-08-11 17:35:23 +00001124
Dan Gohman2836c282009-01-16 01:33:36 +00001125 SUnit *FoundSUnit = 0;
1126
1127 bool HasNoopHazards = false;
1128 while (!AvailableQueue.empty()) {
1129 SUnit *CurSUnit = AvailableQueue.pop();
1130
1131 ScheduleHazardRecognizer::HazardType HT =
1132 HazardRec->getHazardType(CurSUnit);
1133 if (HT == ScheduleHazardRecognizer::NoHazard) {
1134 FoundSUnit = CurSUnit;
1135 break;
1136 }
1137
1138 // Remember if this is a noop hazard.
1139 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
1140
1141 NotReady.push_back(CurSUnit);
1142 }
1143
1144 // Add the nodes that aren't ready back onto the available list.
1145 if (!NotReady.empty()) {
1146 AvailableQueue.push_all(NotReady);
1147 NotReady.clear();
1148 }
1149
Dan Gohman343f0c02008-11-19 23:18:57 +00001150 // If we found a node to schedule, do it now.
1151 if (FoundSUnit) {
1152 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +00001153 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001154 CycleHasInsts = true;
Dan Gohman343f0c02008-11-19 23:18:57 +00001155
David Goodwind94a4e52009-08-10 15:55:25 +00001156 // If we are using the target-specific hazards, then don't
1157 // advance the cycle time just because we schedule a node. If
1158 // the target allows it we can schedule multiple nodes in the
1159 // same cycle.
1160 if (!EnablePostRAHazardAvoidance) {
1161 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
1162 ++CurCycle;
1163 }
Dan Gohman2836c282009-01-16 01:33:36 +00001164 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001165 if (CycleHasInsts) {
David Goodwin2ffb0ce2009-08-12 21:47:46 +00001166 DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n');
1167 HazardRec->AdvanceCycle();
1168 } else if (!HasNoopHazards) {
1169 // Otherwise, we have a pipeline stall, but no other problem,
1170 // just advance the current cycle and try again.
1171 DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n');
1172 HazardRec->AdvanceCycle();
1173 ++NumStalls;
1174 } else {
1175 // Otherwise, we have no instructions to issue and we have instructions
1176 // that will fault if we don't do this right. This is the case for
1177 // processors without pipeline interlocks and other cases.
1178 DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n');
1179 HazardRec->EmitNoop();
1180 Sequence.push_back(0); // NULL here means noop
1181 ++NumNoops;
1182 }
1183
Dan Gohman2836c282009-01-16 01:33:36 +00001184 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001185 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +00001186 }
1187 }
1188
1189#ifndef NDEBUG
Dan Gohmana1e6d362008-11-20 01:26:25 +00001190 VerifySchedule(/*isBottomUp=*/false);
Dan Gohman343f0c02008-11-19 23:18:57 +00001191#endif
1192}
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00001193
1194//===----------------------------------------------------------------------===//
1195// Public Constructor Functions
1196//===----------------------------------------------------------------------===//
1197
1198FunctionPass *llvm::createPostRAScheduler() {
Dan Gohman343f0c02008-11-19 23:18:57 +00001199 return new PostRAScheduler();
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00001200}