blob: 8d746142d1eb0e97b41713ecbdc03b7389563497 [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"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman3f237442008-12-16 03:25:46 +000030#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman21d90032008-11-25 00:52:40 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2836c282009-01-16 01:33:36 +000032#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Dan Gohmanbed353d2009-02-10 23:29:38 +000033#include "llvm/Target/TargetLowering.h"
Dan Gohman79ce2762009-01-15 19:20:50 +000034#include "llvm/Target/TargetMachine.h"
Dan Gohman21d90032008-11-25 00:52:40 +000035#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetRegisterInfo.h"
David Goodwin0dad89f2009-09-30 00:10:16 +000037#include "llvm/Target/TargetSubtarget.h"
Chris Lattner459525d2008-01-14 19:00:06 +000038#include "llvm/Support/Compiler.h"
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000039#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
David Goodwin3a5f0d42009-08-11 01:44:26 +000041#include "llvm/Support/raw_ostream.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000042#include "llvm/ADT/Statistic.h"
Dan Gohman21d90032008-11-25 00:52:40 +000043#include <map>
David Goodwin88a589c2009-08-25 17:03:05 +000044#include <set>
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000045using namespace llvm;
46
Dan Gohman2836c282009-01-16 01:33:36 +000047STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohman343f0c02008-11-19 23:18:57 +000048STATISTIC(NumStalls, "Number of pipeline stalls");
49
Dan Gohman21d90032008-11-25 00:52:40 +000050static cl::opt<bool>
51EnableAntiDepBreaking("break-anti-dependencies",
Dan Gohman00dc84a2008-12-16 19:27:52 +000052 cl::desc("Break post-RA scheduling anti-dependencies"),
53 cl::init(true), cl::Hidden);
Dan Gohman21d90032008-11-25 00:52:40 +000054
Dan Gohman2836c282009-01-16 01:33:36 +000055static cl::opt<bool>
56EnablePostRAHazardAvoidance("avoid-hazards",
David Goodwind94a4e52009-08-10 15:55:25 +000057 cl::desc("Enable exact hazard avoidance"),
David Goodwin5e411782009-09-03 22:15:25 +000058 cl::init(true), cl::Hidden);
Dan Gohman2836c282009-01-16 01:33:36 +000059
David Goodwin1f152282009-09-01 18:34:03 +000060// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
61static cl::opt<int>
62DebugDiv("postra-sched-debugdiv",
63 cl::desc("Debug control MBBs that are scheduled"),
64 cl::init(0), cl::Hidden);
65static cl::opt<int>
66DebugMod("postra-sched-debugmod",
67 cl::desc("Debug control MBBs that are scheduled"),
68 cl::init(0), cl::Hidden);
69
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000070namespace {
Dan Gohman343f0c02008-11-19 23:18:57 +000071 class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass {
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000072 public:
73 static char ID;
Dan Gohman343f0c02008-11-19 23:18:57 +000074 PostRAScheduler() : MachineFunctionPass(&ID) {}
Dan Gohman21d90032008-11-25 00:52:40 +000075
Dan Gohman3f237442008-12-16 03:25:46 +000076 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000077 AU.setPreservesCFG();
Dan Gohman3f237442008-12-16 03:25:46 +000078 AU.addRequired<MachineDominatorTree>();
79 AU.addPreserved<MachineDominatorTree>();
80 AU.addRequired<MachineLoopInfo>();
81 AU.addPreserved<MachineLoopInfo>();
82 MachineFunctionPass::getAnalysisUsage(AU);
83 }
84
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000085 const char *getPassName() const {
Dan Gohman21d90032008-11-25 00:52:40 +000086 return "Post RA top-down list latency scheduler";
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000087 }
88
89 bool runOnMachineFunction(MachineFunction &Fn);
90 };
Dan Gohman343f0c02008-11-19 23:18:57 +000091 char PostRAScheduler::ID = 0;
92
93 class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohman343f0c02008-11-19 23:18:57 +000094 /// AvailableQueue - The priority queue to use for the available SUnits.
95 ///
96 LatencyPriorityQueue AvailableQueue;
97
98 /// PendingQueue - This contains all of the instructions whose operands have
99 /// been issued, but their results are not ready yet (due to the latency of
100 /// the operation). Once the operands becomes available, the instruction is
101 /// added to the AvailableQueue.
102 std::vector<SUnit*> PendingQueue;
103
Dan Gohman21d90032008-11-25 00:52:40 +0000104 /// Topo - A topological ordering for SUnits.
105 ScheduleDAGTopologicalSort Topo;
Dan Gohman343f0c02008-11-19 23:18:57 +0000106
Dan Gohman79ce2762009-01-15 19:20:50 +0000107 /// AllocatableSet - The set of allocatable registers.
108 /// We'll be ignoring anti-dependencies on non-allocatable registers,
109 /// because they may not be safe to break.
110 const BitVector AllocatableSet;
111
Dan Gohman2836c282009-01-16 01:33:36 +0000112 /// HazardRec - The hazard recognizer to use.
113 ScheduleHazardRecognizer *HazardRec;
114
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000115 /// Classes - For live regs that are only used in one register class in a
116 /// live range, the register class. If the register is not live, the
117 /// corresponding value is null. If the register is live but used in
118 /// multiple register classes, the corresponding value is -1 casted to a
119 /// pointer.
120 const TargetRegisterClass *
121 Classes[TargetRegisterInfo::FirstVirtualRegister];
122
123 /// RegRegs - Map registers to all their references within a live range.
124 std::multimap<unsigned, MachineOperand *> RegRefs;
125
Evan Cheng714e8bc2009-10-01 08:26:23 +0000126 /// KillIndices - The index of the most recent kill (proceding bottom-up),
127 /// or ~0u if the register is not live.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000128 unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
129
Evan Cheng714e8bc2009-10-01 08:26:23 +0000130 /// DefIndices - The index of the most recent complete def (proceding bottom
131 /// up), or ~0u if the register is live.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000132 unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister];
133
Evan Cheng714e8bc2009-10-01 08:26:23 +0000134 /// KeepRegs - A set of registers which are live and cannot be changed to
135 /// break anti-dependencies.
136 SmallSet<unsigned, 4> KeepRegs;
137
Dan Gohman21d90032008-11-25 00:52:40 +0000138 public:
Dan Gohman79ce2762009-01-15 19:20:50 +0000139 SchedulePostRATDList(MachineFunction &MF,
Dan Gohman3f237442008-12-16 03:25:46 +0000140 const MachineLoopInfo &MLI,
Dan Gohman2836c282009-01-16 01:33:36 +0000141 const MachineDominatorTree &MDT,
142 ScheduleHazardRecognizer *HR)
Dan Gohman79ce2762009-01-15 19:20:50 +0000143 : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits),
Dan Gohman2836c282009-01-16 01:33:36 +0000144 AllocatableSet(TRI->getAllocatableSet(MF)),
145 HazardRec(HR) {}
146
147 ~SchedulePostRATDList() {
148 delete HazardRec;
149 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000150
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000151 /// StartBlock - Initialize register live-range state for scheduling in
152 /// this block.
153 ///
154 void StartBlock(MachineBasicBlock *BB);
155
156 /// Schedule - Schedule the instruction range using list scheduling.
157 ///
Dan Gohman343f0c02008-11-19 23:18:57 +0000158 void Schedule();
David Goodwin88a589c2009-08-25 17:03:05 +0000159
160 /// FixupKills - Fix register kill flags that have been made
161 /// invalid due to scheduling
162 ///
163 void FixupKills(MachineBasicBlock *MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000164
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000165 /// Observe - Update liveness information to account for the current
166 /// instruction, which will not be scheduled.
167 ///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000168 void Observe(MachineInstr *MI, unsigned Count);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000169
170 /// FinishBlock - Clean up register live-range state.
171 ///
172 void FinishBlock();
173
Dan Gohman343f0c02008-11-19 23:18:57 +0000174 private:
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000175 void PrescanInstruction(MachineInstr *MI);
176 void ScanInstruction(MachineInstr *MI, unsigned Count);
Dan Gohman54e4c362008-12-09 22:54:47 +0000177 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000178 void ReleaseSuccessors(SUnit *SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000179 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
180 void ListScheduleTopDown();
Dan Gohman21d90032008-11-25 00:52:40 +0000181 bool BreakAntiDependencies();
Dan Gohman26255ad2009-08-12 01:33:27 +0000182 unsigned findSuitableFreeRegister(unsigned AntiDepReg,
183 unsigned LastNewReg,
184 const TargetRegisterClass *);
David Goodwin5e411782009-09-03 22:15:25 +0000185 void StartBlockForKills(MachineBasicBlock *BB);
David Goodwin8f909342009-09-23 16:35:25 +0000186
187 // ToggleKillFlag - Toggle a register operand kill flag. Other
188 // adjustments may be made to the instruction if necessary. Return
189 // true if the operand has been deleted, false if not.
190 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Dan Gohman343f0c02008-11-19 23:18:57 +0000191 };
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000192}
193
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000194/// isSchedulingBoundary - Test if the given instruction should be
195/// considered a scheduling boundary. This primarily includes labels
196/// and terminators.
197///
198static bool isSchedulingBoundary(const MachineInstr *MI,
199 const MachineFunction &MF) {
200 // Terminators and labels can't be scheduled around.
201 if (MI->getDesc().isTerminator() || MI->isLabel())
202 return true;
203
Dan Gohmanbed353d2009-02-10 23:29:38 +0000204 // Don't attempt to schedule around any instruction that modifies
205 // a stack-oriented pointer, as it's unlikely to be profitable. This
206 // saves compile time, because it doesn't require every single
207 // stack slot reference to depend on the instruction that does the
208 // modification.
209 const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
210 if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore()))
211 return true;
212
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000213 return false;
214}
215
Dan Gohman343f0c02008-11-19 23:18:57 +0000216bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
David Goodwin0dad89f2009-09-30 00:10:16 +0000217 // Check that post-RA scheduling is enabled for this function
218 const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>();
219 if (!ST.enablePostRAScheduler())
220 return true;
221
David Goodwin3a5f0d42009-08-11 01:44:26 +0000222 DEBUG(errs() << "PostRAScheduler\n");
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000223
Dan Gohman3f237442008-12-16 03:25:46 +0000224 const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
225 const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
David Goodwind94a4e52009-08-10 15:55:25 +0000226 const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData();
Dan Gohman2836c282009-01-16 01:33:36 +0000227 ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ?
David Goodwind94a4e52009-08-10 15:55:25 +0000228 (ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) :
229 (ScheduleHazardRecognizer *)new SimpleHazardRecognizer();
Dan Gohman3f237442008-12-16 03:25:46 +0000230
Dan Gohman2836c282009-01-16 01:33:36 +0000231 SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR);
Dan Gohman79ce2762009-01-15 19:20:50 +0000232
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000233 // Loop over all of the basic blocks
234 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohman343f0c02008-11-19 23:18:57 +0000235 MBB != MBBe; ++MBB) {
David Goodwin1f152282009-09-01 18:34:03 +0000236#ifndef NDEBUG
237 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
238 if (DebugDiv > 0) {
239 static int bbcnt = 0;
240 if (bbcnt++ % DebugDiv != DebugMod)
241 continue;
242 errs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() <<
243 ":MBB ID#" << MBB->getNumber() << " ***\n";
244 }
245#endif
246
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000247 // Initialize register live-range state for scheduling in this block.
248 Scheduler.StartBlock(MBB);
249
Dan Gohmanf7119392009-01-16 22:10:20 +0000250 // Schedule each sequence of instructions not interrupted by a label
251 // or anything else that effectively needs to shut down scheduling.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000252 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman47ac0f02009-02-11 04:27:20 +0000253 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000254 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
255 MachineInstr *MI = prior(I);
256 if (isSchedulingBoundary(MI, Fn)) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000257 Scheduler.Run(MBB, I, Current, CurrentCount);
Evan Chengfb2e7522009-09-18 21:02:19 +0000258 Scheduler.EmitSchedule(0);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000259 Current = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000260 CurrentCount = Count - 1;
Dan Gohman1274ced2009-03-10 18:10:43 +0000261 Scheduler.Observe(MI, CurrentCount);
Dan Gohmanf7119392009-01-16 22:10:20 +0000262 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000263 I = MI;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000264 --Count;
Dan Gohman43f07fb2009-02-03 18:57:45 +0000265 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000266 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands9e8bd0b2009-03-11 09:04:34 +0000267 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman1274ced2009-03-10 18:10:43 +0000268 "Instruction count mismatch!");
269 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Evan Chengfb2e7522009-09-18 21:02:19 +0000270 Scheduler.EmitSchedule(0);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000271
272 // Clean up register live-range state.
273 Scheduler.FinishBlock();
David Goodwin88a589c2009-08-25 17:03:05 +0000274
David Goodwin5e411782009-09-03 22:15:25 +0000275 // Update register kills
David Goodwin88a589c2009-08-25 17:03:05 +0000276 Scheduler.FixupKills(MBB);
Dan Gohman343f0c02008-11-19 23:18:57 +0000277 }
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000278
279 return true;
280}
281
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000282/// StartBlock - Initialize register live-range state for scheduling in
283/// this block.
Dan Gohman21d90032008-11-25 00:52:40 +0000284///
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000285void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
286 // Call the superclass.
287 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohman21d90032008-11-25 00:52:40 +0000288
David Goodwind94a4e52009-08-10 15:55:25 +0000289 // Reset the hazard recognizer.
290 HazardRec->Reset();
291
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000292 // Clear out the register class data.
293 std::fill(Classes, array_endof(Classes),
294 static_cast<const TargetRegisterClass *>(0));
Dan Gohman21d90032008-11-25 00:52:40 +0000295
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000296 // Initialize the indices to indicate that no registers are live.
Dan Gohman6c3643c2008-12-19 22:23:43 +0000297 std::fill(KillIndices, array_endof(KillIndices), ~0u);
Dan Gohman21d90032008-11-25 00:52:40 +0000298 std::fill(DefIndices, array_endof(DefIndices), BB->size());
299
Evan Cheng714e8bc2009-10-01 08:26:23 +0000300 // Clear "do not change" set.
301 KeepRegs.clear();
302
Dan Gohman21d90032008-11-25 00:52:40 +0000303 // Determine the live-out physregs for this block.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000304 if (!BB->empty() && BB->back().getDesc().isReturn())
Dan Gohman21d90032008-11-25 00:52:40 +0000305 // In a return block, examine the function live-out regs.
306 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
307 E = MRI.liveout_end(); I != E; ++I) {
308 unsigned Reg = *I;
309 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
310 KillIndices[Reg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000311 DefIndices[Reg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000312 // Repeat, for all aliases.
313 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
314 unsigned AliasReg = *Alias;
315 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
316 KillIndices[AliasReg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000317 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000318 }
319 }
320 else
321 // In a non-return block, examine the live-in regs of all successors.
322 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
Dan Gohman47ac0f02009-02-11 04:27:20 +0000323 SE = BB->succ_end(); SI != SE; ++SI)
Dan Gohman21d90032008-11-25 00:52:40 +0000324 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
325 E = (*SI)->livein_end(); I != E; ++I) {
326 unsigned Reg = *I;
327 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
328 KillIndices[Reg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000329 DefIndices[Reg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000330 // Repeat, for all aliases.
331 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
332 unsigned AliasReg = *Alias;
333 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
334 KillIndices[AliasReg] = BB->size();
Dan Gohman6c3643c2008-12-19 22:23:43 +0000335 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000336 }
337 }
338
David Goodwin5e411782009-09-03 22:15:25 +0000339 // Consider callee-saved registers as live-out, since we're running after
340 // prologue/epilogue insertion so there's no way to add additional
341 // saved registers.
342 //
343 // TODO: there is a new method
344 // MachineFrameInfo::getPristineRegs(MBB). It gives you a list of
345 // CSRs that have not been saved when entering the MBB. The
346 // remaining CSRs have been saved and can be treated like call
347 // clobbered registers.
348 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
349 unsigned Reg = *I;
350 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
351 KillIndices[Reg] = BB->size();
352 DefIndices[Reg] = ~0u;
353 // Repeat, for all aliases.
354 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
355 unsigned AliasReg = *Alias;
356 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
357 KillIndices[AliasReg] = BB->size();
358 DefIndices[AliasReg] = ~0u;
Dan Gohman21d90032008-11-25 00:52:40 +0000359 }
360 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000361}
362
363/// Schedule - Schedule the instruction range using list scheduling.
364///
365void SchedulePostRATDList::Schedule() {
David Goodwin3a5f0d42009-08-11 01:44:26 +0000366 DEBUG(errs() << "********** List Scheduling **********\n");
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000367
368 // Build the scheduling graph.
369 BuildSchedGraph();
370
371 if (EnableAntiDepBreaking) {
372 if (BreakAntiDependencies()) {
373 // We made changes. Update the dependency graph.
374 // Theoretically we could update the graph in place:
375 // When a live range is changed to use a different register, remove
376 // the def's anti-dependence *and* output-dependence edges due to
377 // that register, and add new anti-dependence and output-dependence
378 // edges based on the next live range of the register.
379 SUnits.clear();
380 EntrySU = SUnit();
381 ExitSU = SUnit();
382 BuildSchedGraph();
383 }
384 }
385
David Goodwind94a4e52009-08-10 15:55:25 +0000386 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
387 SUnits[su].dumpAll(this));
388
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000389 AvailableQueue.initNodes(SUnits);
390
391 ListScheduleTopDown();
392
393 AvailableQueue.releaseState();
394}
395
396/// Observe - Update liveness information to account for the current
397/// instruction, which will not be scheduled.
398///
Dan Gohman47ac0f02009-02-11 04:27:20 +0000399void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
Dan Gohman1274ced2009-03-10 18:10:43 +0000400 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
401
402 // Any register which was defined within the previous scheduling region
403 // may have been rescheduled and its lifetime may overlap with registers
404 // in ways not reflected in our current liveness state. For each such
405 // register, adjust the liveness state to be conservatively correct.
406 for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg)
407 if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
408 assert(KillIndices[Reg] == ~0u && "Clobbered register is live!");
409 // Mark this register to be non-renamable.
410 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
411 // Move the def index to the end of the previous region, to reflect
412 // that the def could theoretically have been scheduled at the end.
413 DefIndices[Reg] = InsertPosIndex;
414 }
415
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000416 PrescanInstruction(MI);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000417 ScanInstruction(MI, Count);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000418}
419
420/// FinishBlock - Clean up register live-range state.
421///
422void SchedulePostRATDList::FinishBlock() {
423 RegRefs.clear();
424
425 // Call the superclass.
426 ScheduleDAGInstrs::FinishBlock();
427}
428
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000429/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
430/// critical path.
431static SDep *CriticalPathStep(SUnit *SU) {
432 SDep *Next = 0;
433 unsigned NextDepth = 0;
434 // Find the predecessor edge with the greatest depth.
435 for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
436 P != PE; ++P) {
437 SUnit *PredSU = P->getSUnit();
438 unsigned PredLatency = P->getLatency();
439 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
440 // In the case of a latency tie, prefer an anti-dependency edge over
441 // other types of edges.
442 if (NextDepth < PredTotalLatency ||
443 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
444 NextDepth = PredTotalLatency;
445 Next = &*P;
446 }
447 }
448 return Next;
449}
450
451void SchedulePostRATDList::PrescanInstruction(MachineInstr *MI) {
452 // Scan the register operands for this instruction and update
453 // Classes and RegRefs.
454 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
455 MachineOperand &MO = MI->getOperand(i);
456 if (!MO.isReg()) continue;
457 unsigned Reg = MO.getReg();
458 if (Reg == 0) continue;
Chris Lattner2a386882009-07-29 21:36:49 +0000459 const TargetRegisterClass *NewRC = 0;
460
461 if (i < MI->getDesc().getNumOperands())
462 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000463
464 // For now, only allow the register to be changed if its register
465 // class is consistent across all uses.
466 if (!Classes[Reg] && NewRC)
467 Classes[Reg] = NewRC;
468 else if (!NewRC || Classes[Reg] != NewRC)
469 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
470
471 // Now check for aliases.
472 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
473 // If an alias of the reg is used during the live range, give up.
474 // Note that this allows us to skip checking if AntiDepReg
475 // overlaps with any of the aliases, among other things.
476 unsigned AliasReg = *Alias;
477 if (Classes[AliasReg]) {
478 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
479 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
480 }
481 }
482
483 // If we're still willing to consider this register, note the reference.
484 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
485 RegRefs.insert(std::make_pair(Reg, &MO));
486 }
487}
488
489void SchedulePostRATDList::ScanInstruction(MachineInstr *MI,
490 unsigned Count) {
491 // Update liveness.
492 // Proceding upwards, registers that are defed but not used in this
493 // instruction are now dead.
494 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
495 MachineOperand &MO = MI->getOperand(i);
496 if (!MO.isReg()) continue;
497 unsigned Reg = MO.getReg();
498 if (Reg == 0) continue;
499 if (!MO.isDef()) continue;
500 // Ignore two-addr defs.
Bob Wilsond9df5012009-04-09 17:16:43 +0000501 if (MI->isRegTiedToUseOperand(i)) continue;
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000502
503 DefIndices[Reg] = Count;
504 KillIndices[Reg] = ~0u;
Evan Cheng714e8bc2009-10-01 08:26:23 +0000505 assert(((KillIndices[Reg] == ~0u) !=
506 (DefIndices[Reg] == ~0u)) &&
507 "Kill and Def maps aren't consistent for Reg!");
508 KeepRegs.erase(Reg);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000509 Classes[Reg] = 0;
510 RegRefs.erase(Reg);
511 // Repeat, for all subregs.
512 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
513 *Subreg; ++Subreg) {
514 unsigned SubregReg = *Subreg;
515 DefIndices[SubregReg] = Count;
516 KillIndices[SubregReg] = ~0u;
Evan Cheng714e8bc2009-10-01 08:26:23 +0000517 KeepRegs.erase(SubregReg);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000518 Classes[SubregReg] = 0;
519 RegRefs.erase(SubregReg);
520 }
David Goodwin7886cd82009-08-29 00:11:13 +0000521 // Conservatively mark super-registers as unusable.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000522 for (const unsigned *Super = TRI->getSuperRegisters(Reg);
523 *Super; ++Super) {
524 unsigned SuperReg = *Super;
525 Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
526 }
527 }
528 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
529 MachineOperand &MO = MI->getOperand(i);
530 if (!MO.isReg()) continue;
531 unsigned Reg = MO.getReg();
532 if (Reg == 0) continue;
533 if (!MO.isUse()) continue;
534
Chris Lattner2a386882009-07-29 21:36:49 +0000535 const TargetRegisterClass *NewRC = 0;
536 if (i < MI->getDesc().getNumOperands())
537 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000538
539 // For now, only allow the register to be changed if its register
540 // class is consistent across all uses.
541 if (!Classes[Reg] && NewRC)
542 Classes[Reg] = NewRC;
543 else if (!NewRC || Classes[Reg] != NewRC)
544 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
545
546 RegRefs.insert(std::make_pair(Reg, &MO));
547
548 // It wasn't previously live but now it is, this is a kill.
549 if (KillIndices[Reg] == ~0u) {
550 KillIndices[Reg] = Count;
551 DefIndices[Reg] = ~0u;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000552 assert(((KillIndices[Reg] == ~0u) !=
553 (DefIndices[Reg] == ~0u)) &&
554 "Kill and Def maps aren't consistent for Reg!");
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000555 }
556 // Repeat, for all aliases.
557 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
558 unsigned AliasReg = *Alias;
559 if (KillIndices[AliasReg] == ~0u) {
560 KillIndices[AliasReg] = Count;
561 DefIndices[AliasReg] = ~0u;
562 }
563 }
564 }
565}
566
Dan Gohman26255ad2009-08-12 01:33:27 +0000567unsigned
568SchedulePostRATDList::findSuitableFreeRegister(unsigned AntiDepReg,
569 unsigned LastNewReg,
570 const TargetRegisterClass *RC) {
571 for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF),
572 RE = RC->allocation_order_end(MF); R != RE; ++R) {
573 unsigned NewReg = *R;
574 // Don't replace a register with itself.
575 if (NewReg == AntiDepReg) continue;
576 // Don't replace a register with one that was recently used to repair
577 // an anti-dependence with this AntiDepReg, because that would
578 // re-introduce that anti-dependence.
579 if (NewReg == LastNewReg) continue;
580 // If NewReg is dead and NewReg's most recent def is not before
581 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
582 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u)) &&
583 "Kill and Def maps aren't consistent for AntiDepReg!");
584 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u)) &&
585 "Kill and Def maps aren't consistent for NewReg!");
Dan Gohmanda277572009-08-12 01:44:20 +0000586 if (KillIndices[NewReg] != ~0u ||
587 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
588 KillIndices[AntiDepReg] > DefIndices[NewReg])
Dan Gohman26255ad2009-08-12 01:33:27 +0000589 continue;
590 return NewReg;
591 }
592
593 // No registers are free and available!
594 return 0;
595}
596
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000597/// BreakAntiDependencies - Identifiy anti-dependencies along the critical path
598/// of the ScheduleDAG and break them by renaming registers.
599///
600bool SchedulePostRATDList::BreakAntiDependencies() {
601 // The code below assumes that there is at least one instruction,
602 // so just duck out immediately if the block is empty.
603 if (SUnits.empty()) return false;
604
605 // Find the node at the bottom of the critical path.
606 SUnit *Max = 0;
607 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
608 SUnit *SU = &SUnits[i];
609 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
610 Max = SU;
611 }
612
David Goodwin3a5f0d42009-08-11 01:44:26 +0000613 DEBUG(errs() << "Critical path has total latency "
614 << (Max->getDepth() + Max->Latency) << "\n");
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000615
616 // Track progress along the critical path through the SUnit graph as we walk
617 // the instructions.
618 SUnit *CriticalPathSU = Max;
619 MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
Dan Gohman21d90032008-11-25 00:52:40 +0000620
621 // Consider this pattern:
622 // A = ...
623 // ... = A
624 // A = ...
625 // ... = A
626 // A = ...
627 // ... = A
628 // A = ...
629 // ... = A
630 // There are three anti-dependencies here, and without special care,
631 // we'd break all of them using the same register:
632 // A = ...
633 // ... = A
634 // B = ...
635 // ... = B
636 // B = ...
637 // ... = B
638 // B = ...
639 // ... = B
640 // because at each anti-dependence, B is the first register that
641 // isn't A which is free. This re-introduces anti-dependencies
642 // at all but one of the original anti-dependencies that we were
643 // trying to break. To avoid this, keep track of the most recent
David Goodwinc93d8372009-08-11 17:35:23 +0000644 // register that each register was replaced with, avoid
Dan Gohman21d90032008-11-25 00:52:40 +0000645 // using it to repair an anti-dependence on the same register.
646 // This lets us produce this:
647 // A = ...
648 // ... = A
649 // B = ...
650 // ... = B
651 // C = ...
652 // ... = C
653 // B = ...
654 // ... = B
655 // This still has an anti-dependence on B, but at least it isn't on the
656 // original critical path.
657 //
658 // TODO: If we tracked more than one register here, we could potentially
659 // fix that remaining critical edge too. This is a little more involved,
660 // because unlike the most recent register, less recent registers should
661 // still be considered, though only if no other registers are available.
662 unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {};
663
Dan Gohman21d90032008-11-25 00:52:40 +0000664 // Attempt to break anti-dependence edges on the critical path. Walk the
665 // instructions from the bottom up, tracking information about liveness
666 // as we go to help determine which registers are available.
667 bool Changed = false;
Dan Gohman47ac0f02009-02-11 04:27:20 +0000668 unsigned Count = InsertPosIndex - 1;
669 for (MachineBasicBlock::iterator I = InsertPos, E = Begin;
Dan Gohman43f07fb2009-02-03 18:57:45 +0000670 I != E; --Count) {
671 MachineInstr *MI = --I;
Dan Gohman21d90032008-11-25 00:52:40 +0000672
Jakob Stoklund Olesen544df362009-09-28 20:32:46 +0000673 // After regalloc, KILL instructions aren't safe to treat as
674 // dependence-breaking. In the case of an INSERT_SUBREG, the KILL
Dan Gohman490b1832008-12-05 05:30:02 +0000675 // is left behind appearing to clobber the super-register, while the
676 // subregister needs to remain live. So we just ignore them.
Jakob Stoklund Olesen544df362009-09-28 20:32:46 +0000677 if (MI->getOpcode() == TargetInstrInfo::KILL)
Dan Gohman490b1832008-12-05 05:30:02 +0000678 continue;
679
Dan Gohman00dc84a2008-12-16 19:27:52 +0000680 // Check if this instruction has a dependence on the critical path that
681 // is an anti-dependence that we may be able to break. If it is, set
682 // AntiDepReg to the non-zero register associated with the anti-dependence.
683 //
684 // We limit our attention to the critical path as a heuristic to avoid
685 // breaking anti-dependence edges that aren't going to significantly
686 // impact the overall schedule. There are a limited number of registers
687 // and we want to save them for the important edges.
688 //
689 // TODO: Instructions with multiple defs could have multiple
690 // anti-dependencies. The current code here only knows how to break one
691 // edge per instruction. Note that we'd have to be able to break all of
692 // the anti-dependencies in an instruction in order to be effective.
693 unsigned AntiDepReg = 0;
694 if (MI == CriticalPathMI) {
695 if (SDep *Edge = CriticalPathStep(CriticalPathSU)) {
696 SUnit *NextSU = Edge->getSUnit();
697
698 // Only consider anti-dependence edges.
699 if (Edge->getKind() == SDep::Anti) {
700 AntiDepReg = Edge->getReg();
701 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Dan Gohman49bb50e2009-01-16 21:57:43 +0000702 if (!AllocatableSet.test(AntiDepReg))
Evan Cheng714e8bc2009-10-01 08:26:23 +0000703 // Don't break anti-dependencies on non-allocatable registers.
704 AntiDepReg = 0;
705 else if (KeepRegs.count(AntiDepReg))
706 // Don't break anti-dependencies if an use down below requires
707 // this exact register.
Dan Gohman49bb50e2009-01-16 21:57:43 +0000708 AntiDepReg = 0;
709 else {
Dan Gohman00dc84a2008-12-16 19:27:52 +0000710 // If the SUnit has other dependencies on the SUnit that it
711 // anti-depends on, don't bother breaking the anti-dependency
712 // since those edges would prevent such units from being
713 // scheduled past each other regardless.
714 //
715 // Also, if there are dependencies on other SUnits with the
716 // same register as the anti-dependency, don't attempt to
717 // break it.
718 for (SUnit::pred_iterator P = CriticalPathSU->Preds.begin(),
719 PE = CriticalPathSU->Preds.end(); P != PE; ++P)
720 if (P->getSUnit() == NextSU ?
721 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
722 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
723 AntiDepReg = 0;
724 break;
725 }
726 }
727 }
728 CriticalPathSU = NextSU;
729 CriticalPathMI = CriticalPathSU->getInstr();
730 } else {
731 // We've reached the end of the critical path.
732 CriticalPathSU = 0;
733 CriticalPathMI = 0;
734 }
735 }
Dan Gohman21d90032008-11-25 00:52:40 +0000736
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000737 PrescanInstruction(MI);
738
Evan Cheng714e8bc2009-10-01 08:26:23 +0000739 if (MI->getDesc().hasExtraSrcRegAllocReq()) {
740 // It's not safe to change register allocation for source operands of
741 // that have special allocation requirements.
742 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
743 MachineOperand &MO = MI->getOperand(i);
744 if (!MO.isReg()) continue;
745 unsigned Reg = MO.getReg();
746 if (Reg == 0) continue;
747 if (MO.isUse()) {
748 if (KeepRegs.insert(Reg)) {
749 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
750 *Subreg; ++Subreg)
751 KeepRegs.insert(*Subreg);
752 }
753 }
754 }
755 }
756
757 if (MI->getDesc().hasExtraDefRegAllocReq())
758 // If this instruction's defs have special allocation requirement, don't
759 // break this anti-dependency.
760 AntiDepReg = 0;
761 else if (AntiDepReg) {
762 // If this instruction has a use of AntiDepReg, breaking it
763 // is invalid.
764 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
765 MachineOperand &MO = MI->getOperand(i);
766 if (!MO.isReg()) continue;
767 unsigned Reg = MO.getReg();
768 if (Reg == 0) continue;
769 if (MO.isUse() && AntiDepReg == Reg) {
770 AntiDepReg = 0;
771 break;
772 }
Dan Gohman21d90032008-11-25 00:52:40 +0000773 }
Dan Gohman21d90032008-11-25 00:52:40 +0000774 }
775
776 // Determine AntiDepReg's register class, if it is live and is
777 // consistently used within a single class.
778 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
Nick Lewyckya89d1022008-11-27 17:29:52 +0000779 assert((AntiDepReg == 0 || RC != NULL) &&
Dan Gohman21d90032008-11-25 00:52:40 +0000780 "Register should be live if it's causing an anti-dependence!");
781 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
782 AntiDepReg = 0;
783
784 // Look for a suitable register to use to break the anti-depenence.
785 //
786 // TODO: Instead of picking the first free register, consider which might
787 // be the best.
788 if (AntiDepReg != 0) {
Dan Gohman26255ad2009-08-12 01:33:27 +0000789 if (unsigned NewReg = findSuitableFreeRegister(AntiDepReg,
790 LastNewReg[AntiDepReg],
791 RC)) {
792 DEBUG(errs() << "Breaking anti-dependence edge on "
793 << TRI->getName(AntiDepReg)
794 << " with " << RegRefs.count(AntiDepReg) << " references"
795 << " using " << TRI->getName(NewReg) << "!\n");
Dan Gohman21d90032008-11-25 00:52:40 +0000796
Dan Gohman26255ad2009-08-12 01:33:27 +0000797 // Update the references to the old register to refer to the new
798 // register.
799 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
800 std::multimap<unsigned, MachineOperand *>::iterator>
801 Range = RegRefs.equal_range(AntiDepReg);
802 for (std::multimap<unsigned, MachineOperand *>::iterator
803 Q = Range.first, QE = Range.second; Q != QE; ++Q)
804 Q->second->setReg(NewReg);
Dan Gohman21d90032008-11-25 00:52:40 +0000805
Dan Gohman26255ad2009-08-12 01:33:27 +0000806 // We just went back in time and modified history; the
807 // liveness information for the anti-depenence reg is now
808 // inconsistent. Set the state as if it were dead.
809 Classes[NewReg] = Classes[AntiDepReg];
810 DefIndices[NewReg] = DefIndices[AntiDepReg];
811 KillIndices[NewReg] = KillIndices[AntiDepReg];
812 assert(((KillIndices[NewReg] == ~0u) !=
813 (DefIndices[NewReg] == ~0u)) &&
814 "Kill and Def maps aren't consistent for NewReg!");
Dan Gohman21d90032008-11-25 00:52:40 +0000815
Dan Gohman26255ad2009-08-12 01:33:27 +0000816 Classes[AntiDepReg] = 0;
817 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
818 KillIndices[AntiDepReg] = ~0u;
819 assert(((KillIndices[AntiDepReg] == ~0u) !=
820 (DefIndices[AntiDepReg] == ~0u)) &&
821 "Kill and Def maps aren't consistent for AntiDepReg!");
Dan Gohman21d90032008-11-25 00:52:40 +0000822
Dan Gohman26255ad2009-08-12 01:33:27 +0000823 RegRefs.erase(AntiDepReg);
824 Changed = true;
825 LastNewReg[AntiDepReg] = NewReg;
Dan Gohman21d90032008-11-25 00:52:40 +0000826 }
827 }
828
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000829 ScanInstruction(MI, Count);
Dan Gohman21d90032008-11-25 00:52:40 +0000830 }
Dan Gohman21d90032008-11-25 00:52:40 +0000831
832 return Changed;
833}
834
David Goodwin5e411782009-09-03 22:15:25 +0000835/// StartBlockForKills - Initialize register live-range state for updating kills
836///
837void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
838 // Initialize the indices to indicate that no registers are live.
839 std::fill(KillIndices, array_endof(KillIndices), ~0u);
840
841 // Determine the live-out physregs for this block.
842 if (!BB->empty() && BB->back().getDesc().isReturn()) {
843 // In a return block, examine the function live-out regs.
844 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
845 E = MRI.liveout_end(); I != E; ++I) {
846 unsigned Reg = *I;
847 KillIndices[Reg] = BB->size();
848 // Repeat, for all subregs.
849 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
850 *Subreg; ++Subreg) {
851 KillIndices[*Subreg] = BB->size();
852 }
853 }
854 }
855 else {
856 // In a non-return block, examine the live-in regs of all successors.
857 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
858 SE = BB->succ_end(); SI != SE; ++SI) {
859 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
860 E = (*SI)->livein_end(); I != E; ++I) {
861 unsigned Reg = *I;
862 KillIndices[Reg] = BB->size();
863 // Repeat, for all subregs.
864 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
865 *Subreg; ++Subreg) {
866 KillIndices[*Subreg] = BB->size();
867 }
868 }
869 }
870 }
871}
872
David Goodwin8f909342009-09-23 16:35:25 +0000873bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
874 MachineOperand &MO) {
875 // Setting kill flag...
876 if (!MO.isKill()) {
877 MO.setIsKill(true);
878 return false;
879 }
880
881 // If MO itself is live, clear the kill flag...
882 if (KillIndices[MO.getReg()] != ~0u) {
883 MO.setIsKill(false);
884 return false;
885 }
886
887 // If any subreg of MO is live, then create an imp-def for that
888 // subreg and keep MO marked as killed.
889 bool AllDead = true;
890 const unsigned SuperReg = MO.getReg();
891 for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
892 *Subreg; ++Subreg) {
893 if (KillIndices[*Subreg] != ~0u) {
894 MI->addOperand(MachineOperand::CreateReg(*Subreg,
895 true /*IsDef*/,
896 true /*IsImp*/,
897 false /*IsKill*/,
898 false /*IsDead*/));
899 AllDead = false;
900 }
901 }
902
903 MO.setIsKill(AllDead);
904 return false;
905}
906
David Goodwin88a589c2009-08-25 17:03:05 +0000907/// FixupKills - Fix the register kill flags, they may have been made
908/// incorrect by instruction reordering.
909///
910void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
911 DEBUG(errs() << "Fixup kills for BB ID#" << MBB->getNumber() << '\n');
912
913 std::set<unsigned> killedRegs;
914 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin5e411782009-09-03 22:15:25 +0000915
916 StartBlockForKills(MBB);
David Goodwin7886cd82009-08-29 00:11:13 +0000917
918 // Examine block from end to start...
David Goodwin88a589c2009-08-25 17:03:05 +0000919 unsigned Count = MBB->size();
920 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
921 I != E; --Count) {
922 MachineInstr *MI = --I;
923
David Goodwin7886cd82009-08-29 00:11:13 +0000924 // Update liveness. Registers that are defed but not used in this
925 // instruction are now dead. Mark register and all subregs as they
926 // are completely defined.
927 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
928 MachineOperand &MO = MI->getOperand(i);
929 if (!MO.isReg()) continue;
930 unsigned Reg = MO.getReg();
931 if (Reg == 0) continue;
932 if (!MO.isDef()) continue;
933 // Ignore two-addr defs.
934 if (MI->isRegTiedToUseOperand(i)) continue;
935
David Goodwin7886cd82009-08-29 00:11:13 +0000936 KillIndices[Reg] = ~0u;
937
938 // Repeat for all subregs.
939 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
940 *Subreg; ++Subreg) {
941 KillIndices[*Subreg] = ~0u;
942 }
943 }
David Goodwin88a589c2009-08-25 17:03:05 +0000944
David Goodwin8f909342009-09-23 16:35:25 +0000945 // Examine all used registers and set/clear kill flag. When a
946 // register is used multiple times we only set the kill flag on
947 // the first use.
David Goodwin88a589c2009-08-25 17:03:05 +0000948 killedRegs.clear();
949 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
950 MachineOperand &MO = MI->getOperand(i);
951 if (!MO.isReg() || !MO.isUse()) continue;
952 unsigned Reg = MO.getReg();
953 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
954
David Goodwin7886cd82009-08-29 00:11:13 +0000955 bool kill = false;
956 if (killedRegs.find(Reg) == killedRegs.end()) {
957 kill = true;
958 // A register is not killed if any subregs are live...
959 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
960 *Subreg; ++Subreg) {
961 if (KillIndices[*Subreg] != ~0u) {
962 kill = false;
963 break;
964 }
965 }
966
967 // If subreg is not live, then register is killed if it became
968 // live in this instruction
969 if (kill)
970 kill = (KillIndices[Reg] == ~0u);
971 }
972
David Goodwin88a589c2009-08-25 17:03:05 +0000973 if (MO.isKill() != kill) {
David Goodwin8f909342009-09-23 16:35:25 +0000974 bool removed = ToggleKillFlag(MI, MO);
975 if (removed) {
976 DEBUG(errs() << "Fixed <removed> in ");
977 } else {
978 DEBUG(errs() << "Fixed " << MO << " in ");
979 }
David Goodwin88a589c2009-08-25 17:03:05 +0000980 DEBUG(MI->dump());
981 }
David Goodwin7886cd82009-08-29 00:11:13 +0000982
David Goodwin88a589c2009-08-25 17:03:05 +0000983 killedRegs.insert(Reg);
984 }
David Goodwin7886cd82009-08-29 00:11:13 +0000985
David Goodwina3251db2009-08-31 20:47:02 +0000986 // Mark any used register (that is not using undef) and subregs as
987 // now live...
David Goodwin7886cd82009-08-29 00:11:13 +0000988 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
989 MachineOperand &MO = MI->getOperand(i);
David Goodwina3251db2009-08-31 20:47:02 +0000990 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwin7886cd82009-08-29 00:11:13 +0000991 unsigned Reg = MO.getReg();
992 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
993
David Goodwin7886cd82009-08-29 00:11:13 +0000994 KillIndices[Reg] = Count;
995
996 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
997 *Subreg; ++Subreg) {
998 KillIndices[*Subreg] = Count;
999 }
1000 }
David Goodwin88a589c2009-08-25 17:03:05 +00001001 }
1002}
1003
Dan Gohman343f0c02008-11-19 23:18:57 +00001004//===----------------------------------------------------------------------===//
1005// Top-Down Scheduling
1006//===----------------------------------------------------------------------===//
1007
1008/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
1009/// the PendingQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman54e4c362008-12-09 22:54:47 +00001010void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
1011 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknerc277ab02009-09-30 20:15:38 +00001012
Dan Gohman343f0c02008-11-19 23:18:57 +00001013#ifndef NDEBUG
Reid Klecknerc277ab02009-09-30 20:15:38 +00001014 if (SuccSU->NumPredsLeft == 0) {
Chris Lattner103289e2009-08-23 07:19:13 +00001015 errs() << "*** Scheduling failed! ***\n";
Dan Gohman343f0c02008-11-19 23:18:57 +00001016 SuccSU->dump(this);
Chris Lattner103289e2009-08-23 07:19:13 +00001017 errs() << " has been released too many times!\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001018 llvm_unreachable(0);
Dan Gohman343f0c02008-11-19 23:18:57 +00001019 }
1020#endif
Reid Klecknerc277ab02009-09-30 20:15:38 +00001021 --SuccSU->NumPredsLeft;
1022
Dan Gohman343f0c02008-11-19 23:18:57 +00001023 // Compute how many cycles it will be before this actually becomes
1024 // available. This is the max of the start time of all predecessors plus
1025 // their latencies.
Dan Gohman3f237442008-12-16 03:25:46 +00001026 SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
Dan Gohman343f0c02008-11-19 23:18:57 +00001027
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001028 // If all the node's predecessors are scheduled, this node is ready
1029 // to be scheduled. Ignore the special ExitSU node.
1030 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohman343f0c02008-11-19 23:18:57 +00001031 PendingQueue.push_back(SuccSU);
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001032}
1033
1034/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
1035void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
1036 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
1037 I != E; ++I)
1038 ReleaseSucc(SU, &*I);
Dan Gohman343f0c02008-11-19 23:18:57 +00001039}
1040
1041/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
1042/// count of its successors. If a successor pending count is zero, add it to
1043/// the Available queue.
1044void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Goodwin3a5f0d42009-08-11 01:44:26 +00001045 DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohman343f0c02008-11-19 23:18:57 +00001046 DEBUG(SU->dump(this));
1047
1048 Sequence.push_back(SU);
Dan Gohman3f237442008-12-16 03:25:46 +00001049 assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
1050 SU->setDepthToAtLeast(CurCycle);
Dan Gohman343f0c02008-11-19 23:18:57 +00001051
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001052 ReleaseSuccessors(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +00001053 SU->isScheduled = true;
1054 AvailableQueue.ScheduledNode(SU);
1055}
1056
1057/// ListScheduleTopDown - The main loop of list scheduling for top-down
1058/// schedulers.
1059void SchedulePostRATDList::ListScheduleTopDown() {
1060 unsigned CurCycle = 0;
1061
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001062 // Release any successors of the special Entry node.
1063 ReleaseSuccessors(&EntrySU);
1064
Dan Gohman343f0c02008-11-19 23:18:57 +00001065 // All leaves to Available queue.
1066 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1067 // It is available if it has no predecessors.
1068 if (SUnits[i].Preds.empty()) {
1069 AvailableQueue.push(&SUnits[i]);
1070 SUnits[i].isAvailable = true;
1071 }
1072 }
Dan Gohman9e64bbb2009-02-10 23:27:53 +00001073
David Goodwin2ffb0ce2009-08-12 21:47:46 +00001074 // In any cycle where we can't schedule any instructions, we must
1075 // stall or emit a noop, depending on the target.
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001076 bool CycleHasInsts = false;
David Goodwin2ffb0ce2009-08-12 21:47:46 +00001077
Dan Gohman343f0c02008-11-19 23:18:57 +00001078 // While Available queue is not empty, grab the node with the highest
1079 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman2836c282009-01-16 01:33:36 +00001080 std::vector<SUnit*> NotReady;
Dan Gohman343f0c02008-11-19 23:18:57 +00001081 Sequence.reserve(SUnits.size());
1082 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
1083 // Check to see if any of the pending instructions are ready to issue. If
1084 // so, add them to the available queue.
Dan Gohman3f237442008-12-16 03:25:46 +00001085 unsigned MinDepth = ~0u;
Dan Gohman343f0c02008-11-19 23:18:57 +00001086 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
Dan Gohman3f237442008-12-16 03:25:46 +00001087 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohman343f0c02008-11-19 23:18:57 +00001088 AvailableQueue.push(PendingQueue[i]);
1089 PendingQueue[i]->isAvailable = true;
1090 PendingQueue[i] = PendingQueue.back();
1091 PendingQueue.pop_back();
1092 --i; --e;
Dan Gohman3f237442008-12-16 03:25:46 +00001093 } else if (PendingQueue[i]->getDepth() < MinDepth)
1094 MinDepth = PendingQueue[i]->getDepth();
Dan Gohman343f0c02008-11-19 23:18:57 +00001095 }
David Goodwinc93d8372009-08-11 17:35:23 +00001096
David Goodwin7cd01182009-08-11 17:56:42 +00001097 DEBUG(errs() << "\n*** Examining Available\n";
1098 LatencyPriorityQueue q = AvailableQueue;
1099 while (!q.empty()) {
1100 SUnit *su = q.pop();
1101 errs() << "Height " << su->getHeight() << ": ";
1102 su->dump(this);
1103 });
David Goodwinc93d8372009-08-11 17:35:23 +00001104
Dan Gohman2836c282009-01-16 01:33:36 +00001105 SUnit *FoundSUnit = 0;
1106
1107 bool HasNoopHazards = false;
1108 while (!AvailableQueue.empty()) {
1109 SUnit *CurSUnit = AvailableQueue.pop();
1110
1111 ScheduleHazardRecognizer::HazardType HT =
1112 HazardRec->getHazardType(CurSUnit);
1113 if (HT == ScheduleHazardRecognizer::NoHazard) {
1114 FoundSUnit = CurSUnit;
1115 break;
1116 }
1117
1118 // Remember if this is a noop hazard.
1119 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
1120
1121 NotReady.push_back(CurSUnit);
1122 }
1123
1124 // Add the nodes that aren't ready back onto the available list.
1125 if (!NotReady.empty()) {
1126 AvailableQueue.push_all(NotReady);
1127 NotReady.clear();
1128 }
1129
Dan Gohman343f0c02008-11-19 23:18:57 +00001130 // If we found a node to schedule, do it now.
1131 if (FoundSUnit) {
1132 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman2836c282009-01-16 01:33:36 +00001133 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001134 CycleHasInsts = true;
Dan Gohman343f0c02008-11-19 23:18:57 +00001135
David Goodwind94a4e52009-08-10 15:55:25 +00001136 // If we are using the target-specific hazards, then don't
1137 // advance the cycle time just because we schedule a node. If
1138 // the target allows it we can schedule multiple nodes in the
1139 // same cycle.
1140 if (!EnablePostRAHazardAvoidance) {
1141 if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
1142 ++CurCycle;
1143 }
Dan Gohman2836c282009-01-16 01:33:36 +00001144 } else {
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001145 if (CycleHasInsts) {
David Goodwin2ffb0ce2009-08-12 21:47:46 +00001146 DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n');
1147 HazardRec->AdvanceCycle();
1148 } else if (!HasNoopHazards) {
1149 // Otherwise, we have a pipeline stall, but no other problem,
1150 // just advance the current cycle and try again.
1151 DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n');
1152 HazardRec->AdvanceCycle();
1153 ++NumStalls;
1154 } else {
1155 // Otherwise, we have no instructions to issue and we have instructions
1156 // that will fault if we don't do this right. This is the case for
1157 // processors without pipeline interlocks and other cases.
1158 DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n');
1159 HazardRec->EmitNoop();
1160 Sequence.push_back(0); // NULL here means noop
1161 ++NumNoops;
1162 }
1163
Dan Gohman2836c282009-01-16 01:33:36 +00001164 ++CurCycle;
Benjamin Kramerbe441c02009-09-06 12:10:17 +00001165 CycleHasInsts = false;
Dan Gohman343f0c02008-11-19 23:18:57 +00001166 }
1167 }
1168
1169#ifndef NDEBUG
Dan Gohmana1e6d362008-11-20 01:26:25 +00001170 VerifySchedule(/*isBottomUp=*/false);
Dan Gohman343f0c02008-11-19 23:18:57 +00001171#endif
1172}
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00001173
1174//===----------------------------------------------------------------------===//
1175// Public Constructor Functions
1176//===----------------------------------------------------------------------===//
1177
1178FunctionPass *llvm::createPostRAScheduler() {
Dan Gohman343f0c02008-11-19 23:18:57 +00001179 return new PostRAScheduler();
Dale Johannesene7e7d0d2007-07-13 17:13:54 +00001180}