blob: f0bd6d1372be79f12649df0875a50d8340d4f421 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Goodwin4a8676e2009-10-28 18:29:54 +000022#include "AntiDepBreaker.h"
David Goodwina8b85962009-10-26 19:32:42 +000023#include "AggressiveAntiDepBreaker.h"
David Goodwinad5789c2009-10-26 16:59:04 +000024#include "CriticalAntiDepBreaker.h"
Dan Gohman98c6cb32009-02-06 17:12:10 +000025#include "ScheduleDAGInstrs.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CodeGen/Passes.h"
Dan Gohmand27a0e02008-11-19 23:18:57 +000027#include "llvm/CodeGen/LatencyPriorityQueue.h"
28#include "llvm/CodeGen/SchedulerRegistry.h"
Dan Gohman6b2ee8f2008-12-16 03:25:46 +000029#include "llvm/CodeGen/MachineDominators.h"
David Goodwincdb56fc2009-10-01 19:45:32 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman6b2ee8f2008-12-16 03:25:46 +000032#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohmanc2c90e22008-11-25 00:52:40 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman99603bb2009-01-16 01:33:36 +000034#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Dan Gohman0a4c09e2009-10-09 23:27:56 +000035#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman75cc17f2009-02-10 23:29:38 +000036#include "llvm/Target/TargetLowering.h"
Dan Gohman96eb47a2009-01-15 19:20:50 +000037#include "llvm/Target/TargetMachine.h"
Dan Gohmanc2c90e22008-11-25 00:52:40 +000038#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetRegisterInfo.h"
David Goodwincf89a602009-09-30 00:10:16 +000040#include "llvm/Target/TargetSubtarget.h"
David Goodwinaa644ea2009-10-26 22:31:16 +000041#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include "llvm/Support/Debug.h"
Edwin Török675d5622009-07-11 20:10:48 +000043#include "llvm/Support/ErrorHandling.h"
David Goodwin0eeb4512009-08-11 01:44:26 +000044#include "llvm/Support/raw_ostream.h"
David Goodwinad5789c2009-10-26 16:59:04 +000045#include "llvm/ADT/BitVector.h"
Dan Gohmand27a0e02008-11-19 23:18:57 +000046#include "llvm/ADT/Statistic.h"
David Goodwina9c16fe2009-08-25 17:03:05 +000047#include <set>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048using namespace llvm;
49
Dan Gohman99603bb2009-01-16 01:33:36 +000050STATISTIC(NumNoops, "Number of noops inserted");
Dan Gohmand27a0e02008-11-19 23:18:57 +000051STATISTIC(NumStalls, "Number of pipeline stalls");
David Goodwinad5789c2009-10-26 16:59:04 +000052STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
Dan Gohmand27a0e02008-11-19 23:18:57 +000053
David Goodwin089aa852009-10-01 21:46:35 +000054// Post-RA scheduling is enabled with
55// TargetSubtarget.enablePostRAScheduler(). This flag can be used to
56// override the target.
57static cl::opt<bool>
58EnablePostRAScheduler("post-RA-scheduler",
59 cl::desc("Enable scheduling after register allocation"),
David Goodwin736fed92009-10-01 22:19:57 +000060 cl::init(false), cl::Hidden);
David Goodwinad5789c2009-10-26 16:59:04 +000061static cl::opt<std::string>
Dan Gohmanc2c90e22008-11-25 00:52:40 +000062EnableAntiDepBreaking("break-anti-dependencies",
David Goodwinad5789c2009-10-26 16:59:04 +000063 cl::desc("Break post-RA scheduling anti-dependencies: "
64 "\"critical\", \"all\", or \"none\""),
65 cl::init("none"), cl::Hidden);
Dan Gohman99603bb2009-01-16 01:33:36 +000066
David Goodwin4b023dd2009-09-01 18:34:03 +000067// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
68static cl::opt<int>
69DebugDiv("postra-sched-debugdiv",
70 cl::desc("Debug control MBBs that are scheduled"),
71 cl::init(0), cl::Hidden);
72static cl::opt<int>
73DebugMod("postra-sched-debugmod",
74 cl::desc("Debug control MBBs that are scheduled"),
75 cl::init(0), cl::Hidden);
76
David Goodwin64948f82009-10-26 19:41:00 +000077AntiDepBreaker::~AntiDepBreaker() { }
78
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000080 class PostRAScheduler : public MachineFunctionPass {
Dan Gohman0a4c09e2009-10-09 23:27:56 +000081 AliasAnalysis *AA;
Evan Cheng24ff0562010-06-18 23:09:54 +000082 const TargetInstrInfo *TII;
Evan Cheng86e24b02009-10-16 21:06:15 +000083 CodeGenOpt::Level OptLevel;
Dan Gohman0a4c09e2009-10-09 23:27:56 +000084
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 public:
86 static char ID;
Evan Cheng86e24b02009-10-16 21:06:15 +000087 PostRAScheduler(CodeGenOpt::Level ol) :
Owen Anderson75693222010-08-06 18:33:48 +000088 MachineFunctionPass(ID), OptLevel(ol) {}
Dan Gohmanc2c90e22008-11-25 00:52:40 +000089
Dan Gohman6b2ee8f2008-12-16 03:25:46 +000090 void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +000091 AU.setPreservesCFG();
Dan Gohman0a4c09e2009-10-09 23:27:56 +000092 AU.addRequired<AliasAnalysis>();
Dan Gohman6b2ee8f2008-12-16 03:25:46 +000093 AU.addRequired<MachineDominatorTree>();
94 AU.addPreserved<MachineDominatorTree>();
95 AU.addRequired<MachineLoopInfo>();
96 AU.addPreserved<MachineLoopInfo>();
97 MachineFunctionPass::getAnalysisUsage(AU);
98 }
99
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 const char *getPassName() const {
Dan Gohmanc2c90e22008-11-25 00:52:40 +0000101 return "Post RA top-down list latency scheduler";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 }
103
104 bool runOnMachineFunction(MachineFunction &Fn);
105 };
Dan Gohmand27a0e02008-11-19 23:18:57 +0000106 char PostRAScheduler::ID = 0;
107
Nick Lewycky492d06e2009-10-25 06:33:48 +0000108 class SchedulePostRATDList : public ScheduleDAGInstrs {
Dan Gohmand27a0e02008-11-19 23:18:57 +0000109 /// AvailableQueue - The priority queue to use for the available SUnits.
Dan Gohmanfb035b42009-10-21 01:44:44 +0000110 ///
Dan Gohmand27a0e02008-11-19 23:18:57 +0000111 LatencyPriorityQueue AvailableQueue;
Jim Grosbached0972f2010-05-14 21:19:48 +0000112
Dan Gohmand27a0e02008-11-19 23:18:57 +0000113 /// PendingQueue - This contains all of the instructions whose operands have
114 /// been issued, but their results are not ready yet (due to the latency of
115 /// the operation). Once the operands becomes available, the instruction is
116 /// added to the AvailableQueue.
117 std::vector<SUnit*> PendingQueue;
118
Dan Gohmanc2c90e22008-11-25 00:52:40 +0000119 /// Topo - A topological ordering for SUnits.
120 ScheduleDAGTopologicalSort Topo;
Dan Gohmand27a0e02008-11-19 23:18:57 +0000121
Dan Gohman99603bb2009-01-16 01:33:36 +0000122 /// HazardRec - The hazard recognizer to use.
123 ScheduleHazardRecognizer *HazardRec;
124
David Goodwinad5789c2009-10-26 16:59:04 +0000125 /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
126 AntiDepBreaker *AntiDepBreak;
127
Dan Gohman0a4c09e2009-10-09 23:27:56 +0000128 /// AA - AliasAnalysis for making memory reference queries.
129 AliasAnalysis *AA;
130
Dan Gohmanfb035b42009-10-21 01:44:44 +0000131 /// KillIndices - The index of the most recent kill (proceding bottom-up),
132 /// or ~0u if the register is not live.
Bill Wendling79fb1202010-07-15 20:01:02 +0000133 std::vector<unsigned> KillIndices;
Dan Gohmana91eb052009-02-10 23:27:53 +0000134
Dan Gohmanc2c90e22008-11-25 00:52:40 +0000135 public:
Dan Gohman96eb47a2009-01-15 19:20:50 +0000136 SchedulePostRATDList(MachineFunction &MF,
Dan Gohman6b2ee8f2008-12-16 03:25:46 +0000137 const MachineLoopInfo &MLI,
Dan Gohman99603bb2009-01-16 01:33:36 +0000138 const MachineDominatorTree &MDT,
Dan Gohman0a4c09e2009-10-09 23:27:56 +0000139 ScheduleHazardRecognizer *HR,
David Goodwinad5789c2009-10-26 16:59:04 +0000140 AntiDepBreaker *ADB,
141 AliasAnalysis *aa)
Dan Gohman96eb47a2009-01-15 19:20:50 +0000142 : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits),
Bill Wendling79fb1202010-07-15 20:01:02 +0000143 HazardRec(HR), AntiDepBreak(ADB), AA(aa),
144 KillIndices(TRI->getNumRegs()) {}
Dan Gohman99603bb2009-01-16 01:33:36 +0000145
146 ~SchedulePostRATDList() {
Dan Gohman99603bb2009-01-16 01:33:36 +0000147 }
Dan Gohmand27a0e02008-11-19 23:18:57 +0000148
Dan Gohmana91eb052009-02-10 23:27:53 +0000149 /// StartBlock - Initialize register live-range state for scheduling in
150 /// this block.
151 ///
152 void StartBlock(MachineBasicBlock *BB);
153
154 /// Schedule - Schedule the instruction range using list scheduling.
155 ///
Dan Gohmand27a0e02008-11-19 23:18:57 +0000156 void Schedule();
Jim Grosbached0972f2010-05-14 21:19:48 +0000157
Dan Gohmanfb035b42009-10-21 01:44:44 +0000158 /// Observe - Update liveness information to account for the current
159 /// instruction, which will not be scheduled.
160 ///
161 void Observe(MachineInstr *MI, unsigned Count);
162
163 /// FinishBlock - Clean up register live-range state.
164 ///
165 void FinishBlock();
166
David Goodwinad5789c2009-10-26 16:59:04 +0000167 /// FixupKills - Fix register kill flags that have been made
168 /// invalid due to scheduling
169 ///
170 void FixupKills(MachineBasicBlock *MBB);
171
Dan Gohmand27a0e02008-11-19 23:18:57 +0000172 private:
David Goodwind34903a2009-11-20 19:32:48 +0000173 void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
174 void ReleaseSuccessors(SUnit *SU);
175 void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
176 void ListScheduleTopDown();
David Goodwin856b38c2009-09-03 22:15:25 +0000177 void StartBlockForKills(MachineBasicBlock *BB);
Jim Grosbached0972f2010-05-14 21:19:48 +0000178
David Goodwin83b6ace2009-09-23 16:35:25 +0000179 // ToggleKillFlag - Toggle a register operand kill flag. Other
180 // adjustments may be made to the instruction if necessary. Return
181 // true if the operand has been deleted, false if not.
182 bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
Dan Gohmand27a0e02008-11-19 23:18:57 +0000183 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184}
185
Dan Gohmand27a0e02008-11-19 23:18:57 +0000186bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
Dan Gohman101611d2009-10-10 00:15:38 +0000187 AA = &getAnalysis<AliasAnalysis>();
Evan Cheng24ff0562010-06-18 23:09:54 +0000188 TII = Fn.getTarget().getInstrInfo();
Dan Gohman101611d2009-10-10 00:15:38 +0000189
David Goodwin089aa852009-10-01 21:46:35 +0000190 // Check for explicit enable/disable of post-ra scheduling.
David Goodwine56e4a62009-10-22 23:19:17 +0000191 TargetSubtarget::AntiDepBreakMode AntiDepMode = TargetSubtarget::ANTIDEP_NONE;
David Goodwine6e30352009-11-13 19:52:48 +0000192 SmallVector<TargetRegisterClass*, 4> CriticalPathRCs;
David Goodwin089aa852009-10-01 21:46:35 +0000193 if (EnablePostRAScheduler.getPosition() > 0) {
194 if (!EnablePostRAScheduler)
Evan Cheng57796902009-10-16 06:10:34 +0000195 return false;
David Goodwin089aa852009-10-01 21:46:35 +0000196 } else {
Evan Cheng57796902009-10-16 06:10:34 +0000197 // Check that post-RA scheduling is enabled for this target.
David Goodwin089aa852009-10-01 21:46:35 +0000198 const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>();
David Goodwine6e30352009-11-13 19:52:48 +0000199 if (!ST.enablePostRAScheduler(OptLevel, AntiDepMode, CriticalPathRCs))
Evan Cheng57796902009-10-16 06:10:34 +0000200 return false;
David Goodwin089aa852009-10-01 21:46:35 +0000201 }
David Goodwincf89a602009-09-30 00:10:16 +0000202
David Goodwine56e4a62009-10-22 23:19:17 +0000203 // Check for antidep breaking override...
204 if (EnableAntiDepBreaking.getPosition() > 0) {
Jim Grosbached0972f2010-05-14 21:19:48 +0000205 AntiDepMode = (EnableAntiDepBreaking == "all") ?
206 TargetSubtarget::ANTIDEP_ALL :
207 (EnableAntiDepBreaking == "critical")
208 ? TargetSubtarget::ANTIDEP_CRITICAL : TargetSubtarget::ANTIDEP_NONE;
David Goodwine56e4a62009-10-22 23:19:17 +0000209 }
210
David Greene2ae85ad2010-01-05 01:26:01 +0000211 DEBUG(dbgs() << "PostRAScheduler\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212
Dan Gohman6b2ee8f2008-12-16 03:25:46 +0000213 const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
214 const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
Evan Chengf4fba532010-06-12 00:12:18 +0000215 const TargetMachine &TM = Fn.getTarget();
216 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
217 ScheduleHazardRecognizer *HR =
218 TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins);
Jim Grosbached0972f2010-05-14 21:19:48 +0000219 AntiDepBreaker *ADB =
David Goodwina8b85962009-10-26 19:32:42 +0000220 ((AntiDepMode == TargetSubtarget::ANTIDEP_ALL) ?
David Goodwine6e30352009-11-13 19:52:48 +0000221 (AntiDepBreaker *)new AggressiveAntiDepBreaker(Fn, CriticalPathRCs) :
Jim Grosbached0972f2010-05-14 21:19:48 +0000222 ((AntiDepMode == TargetSubtarget::ANTIDEP_CRITICAL) ?
David Goodwina8b85962009-10-26 19:32:42 +0000223 (AntiDepBreaker *)new CriticalAntiDepBreaker(Fn) : NULL));
Dan Gohman6b2ee8f2008-12-16 03:25:46 +0000224
David Goodwinad5789c2009-10-26 16:59:04 +0000225 SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR, ADB, AA);
Dan Gohman96eb47a2009-01-15 19:20:50 +0000226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 // Loop over all of the basic blocks
228 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Dan Gohmand27a0e02008-11-19 23:18:57 +0000229 MBB != MBBe; ++MBB) {
David Goodwin4b023dd2009-09-01 18:34:03 +0000230#ifndef NDEBUG
231 // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
232 if (DebugDiv > 0) {
233 static int bbcnt = 0;
234 if (bbcnt++ % DebugDiv != DebugMod)
235 continue;
David Greene2ae85ad2010-01-05 01:26:01 +0000236 dbgs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() <<
Dan Gohman57b31652009-10-31 20:19:03 +0000237 ":BB#" << MBB->getNumber() << " ***\n";
David Goodwin4b023dd2009-09-01 18:34:03 +0000238 }
239#endif
240
Dan Gohmana91eb052009-02-10 23:27:53 +0000241 // Initialize register live-range state for scheduling in this block.
242 Scheduler.StartBlock(MBB);
243
Dan Gohman14bb9922009-01-16 22:10:20 +0000244 // Schedule each sequence of instructions not interrupted by a label
245 // or anything else that effectively needs to shut down scheduling.
Dan Gohmana91eb052009-02-10 23:27:53 +0000246 MachineBasicBlock::iterator Current = MBB->end();
Dan Gohman7c968a82009-02-11 04:27:20 +0000247 unsigned Count = MBB->size(), CurrentCount = Count;
Dan Gohmana91eb052009-02-10 23:27:53 +0000248 for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
Evan Cheng24ff0562010-06-18 23:09:54 +0000249 MachineInstr *MI = llvm::prior(I);
250 if (TII->isSchedulingBoundary(MI, MBB, Fn)) {
Dan Gohman68ade7b2009-03-10 18:10:43 +0000251 Scheduler.Run(MBB, I, Current, CurrentCount);
Dan Gohmane9198cc2010-05-01 00:01:06 +0000252 Scheduler.EmitSchedule();
Dan Gohmana91eb052009-02-10 23:27:53 +0000253 Current = MI;
Dan Gohman7c968a82009-02-11 04:27:20 +0000254 CurrentCount = Count - 1;
Dan Gohman68ade7b2009-03-10 18:10:43 +0000255 Scheduler.Observe(MI, CurrentCount);
Dan Gohman14bb9922009-01-16 22:10:20 +0000256 }
Dan Gohmana91eb052009-02-10 23:27:53 +0000257 I = MI;
Dan Gohman7c968a82009-02-11 04:27:20 +0000258 --Count;
Dan Gohman0dcda312009-02-03 18:57:45 +0000259 }
Dan Gohman7c968a82009-02-11 04:27:20 +0000260 assert(Count == 0 && "Instruction count mismatch!");
Duncan Sands34468542009-03-11 09:04:34 +0000261 assert((MBB->begin() == Current || CurrentCount != 0) &&
Dan Gohman68ade7b2009-03-10 18:10:43 +0000262 "Instruction count mismatch!");
263 Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
Dan Gohmane9198cc2010-05-01 00:01:06 +0000264 Scheduler.EmitSchedule();
Dan Gohmana91eb052009-02-10 23:27:53 +0000265
266 // Clean up register live-range state.
267 Scheduler.FinishBlock();
David Goodwina9c16fe2009-08-25 17:03:05 +0000268
David Goodwin856b38c2009-09-03 22:15:25 +0000269 // Update register kills
David Goodwina9c16fe2009-08-25 17:03:05 +0000270 Scheduler.FixupKills(MBB);
Dan Gohmand27a0e02008-11-19 23:18:57 +0000271 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272
David Goodwinad5789c2009-10-26 16:59:04 +0000273 delete HR;
274 delete ADB;
275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 return true;
277}
Jim Grosbached0972f2010-05-14 21:19:48 +0000278
Dan Gohmana91eb052009-02-10 23:27:53 +0000279/// StartBlock - Initialize register live-range state for scheduling in
280/// this block.
Dan Gohmanc2c90e22008-11-25 00:52:40 +0000281///
Dan Gohmana91eb052009-02-10 23:27:53 +0000282void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
283 // Call the superclass.
284 ScheduleDAGInstrs::StartBlock(BB);
Dan Gohmanc2c90e22008-11-25 00:52:40 +0000285
David Goodwinad5789c2009-10-26 16:59:04 +0000286 // Reset the hazard recognizer and anti-dep breaker.
David Goodwin5ab4fd42009-08-10 15:55:25 +0000287 HazardRec->Reset();
David Goodwinad5789c2009-10-26 16:59:04 +0000288 if (AntiDepBreak != NULL)
289 AntiDepBreak->StartBlock(BB);
Dan Gohmana91eb052009-02-10 23:27:53 +0000290}
291
292/// Schedule - Schedule the instruction range using list scheduling.
293///
294void SchedulePostRATDList::Schedule() {
Dan Gohmana91eb052009-02-10 23:27:53 +0000295 // Build the scheduling graph.
Dan Gohman0a4c09e2009-10-09 23:27:56 +0000296 BuildSchedGraph(AA);
Dan Gohmana91eb052009-02-10 23:27:53 +0000297
David Goodwinad5789c2009-10-26 16:59:04 +0000298 if (AntiDepBreak != NULL) {
Jim Grosbached0972f2010-05-14 21:19:48 +0000299 unsigned Broken =
David Goodwind34903a2009-11-20 19:32:48 +0000300 AntiDepBreak->BreakAntiDependencies(SUnits, Begin, InsertPos,
301 InsertPosIndex);
Jim Grosbached0972f2010-05-14 21:19:48 +0000302
David Goodwind34903a2009-11-20 19:32:48 +0000303 if (Broken != 0) {
Dan Gohmana91eb052009-02-10 23:27:53 +0000304 // We made changes. Update the dependency graph.
305 // Theoretically we could update the graph in place:
306 // When a live range is changed to use a different register, remove
307 // the def's anti-dependence *and* output-dependence edges due to
308 // that register, and add new anti-dependence and output-dependence
309 // edges based on the next live range of the register.
David Goodwind34903a2009-11-20 19:32:48 +0000310 SUnits.clear();
311 Sequence.clear();
312 EntrySU = SUnit();
313 ExitSU = SUnit();
314 BuildSchedGraph(AA);
Jim Grosbached0972f2010-05-14 21:19:48 +0000315
David Goodwinad5789c2009-10-26 16:59:04 +0000316 NumFixedAnti += Broken;
Dan Gohmana91eb052009-02-10 23:27:53 +0000317 }
318 }
319
David Greene2ae85ad2010-01-05 01:26:01 +0000320 DEBUG(dbgs() << "********** List Scheduling **********\n");
David Goodwin5ab4fd42009-08-10 15:55:25 +0000321 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
322 SUnits[su].dumpAll(this));
323
Dan Gohmana91eb052009-02-10 23:27:53 +0000324 AvailableQueue.initNodes(SUnits);
David Goodwind34903a2009-11-20 19:32:48 +0000325 ListScheduleTopDown();
Dan Gohmana91eb052009-02-10 23:27:53 +0000326 AvailableQueue.releaseState();
327}
328
329/// Observe - Update liveness information to account for the current
330/// instruction, which will not be scheduled.
331///
Dan Gohman7c968a82009-02-11 04:27:20 +0000332void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
David Goodwinad5789c2009-10-26 16:59:04 +0000333 if (AntiDepBreak != NULL)
334 AntiDepBreak->Observe(MI, Count, InsertPosIndex);
Dan Gohmana91eb052009-02-10 23:27:53 +0000335}
336
337/// FinishBlock - Clean up register live-range state.
338///
339void SchedulePostRATDList::FinishBlock() {
David Goodwinad5789c2009-10-26 16:59:04 +0000340 if (AntiDepBreak != NULL)
341 AntiDepBreak->FinishBlock();
Dan Gohmana91eb052009-02-10 23:27:53 +0000342
343 // Call the superclass.
344 ScheduleDAGInstrs::FinishBlock();
345}
346
David Goodwin856b38c2009-09-03 22:15:25 +0000347/// StartBlockForKills - Initialize register live-range state for updating kills
348///
349void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
350 // Initialize the indices to indicate that no registers are live.
David Goodwin9b4ae242009-12-09 17:18:22 +0000351 for (unsigned i = 0; i < TRI->getNumRegs(); ++i)
352 KillIndices[i] = ~0u;
David Goodwin856b38c2009-09-03 22:15:25 +0000353
354 // Determine the live-out physregs for this block.
355 if (!BB->empty() && BB->back().getDesc().isReturn()) {
356 // In a return block, examine the function live-out regs.
357 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
358 E = MRI.liveout_end(); I != E; ++I) {
359 unsigned Reg = *I;
360 KillIndices[Reg] = BB->size();
361 // Repeat, for all subregs.
362 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
363 *Subreg; ++Subreg) {
364 KillIndices[*Subreg] = BB->size();
365 }
366 }
367 }
368 else {
369 // In a non-return block, examine the live-in regs of all successors.
370 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
371 SE = BB->succ_end(); SI != SE; ++SI) {
372 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
373 E = (*SI)->livein_end(); I != E; ++I) {
374 unsigned Reg = *I;
375 KillIndices[Reg] = BB->size();
376 // Repeat, for all subregs.
377 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
378 *Subreg; ++Subreg) {
379 KillIndices[*Subreg] = BB->size();
380 }
381 }
382 }
383 }
384}
385
David Goodwin83b6ace2009-09-23 16:35:25 +0000386bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
387 MachineOperand &MO) {
388 // Setting kill flag...
389 if (!MO.isKill()) {
390 MO.setIsKill(true);
391 return false;
392 }
Jim Grosbached0972f2010-05-14 21:19:48 +0000393
David Goodwin83b6ace2009-09-23 16:35:25 +0000394 // If MO itself is live, clear the kill flag...
395 if (KillIndices[MO.getReg()] != ~0u) {
396 MO.setIsKill(false);
397 return false;
398 }
399
400 // If any subreg of MO is live, then create an imp-def for that
401 // subreg and keep MO marked as killed.
Benjamin Kramere3901ce2009-10-02 15:59:52 +0000402 MO.setIsKill(false);
David Goodwin83b6ace2009-09-23 16:35:25 +0000403 bool AllDead = true;
404 const unsigned SuperReg = MO.getReg();
405 for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
406 *Subreg; ++Subreg) {
407 if (KillIndices[*Subreg] != ~0u) {
408 MI->addOperand(MachineOperand::CreateReg(*Subreg,
409 true /*IsDef*/,
410 true /*IsImp*/,
411 false /*IsKill*/,
412 false /*IsDead*/));
413 AllDead = false;
414 }
415 }
416
Dan Gohmanfb035b42009-10-21 01:44:44 +0000417 if(AllDead)
Benjamin Kramere3901ce2009-10-02 15:59:52 +0000418 MO.setIsKill(true);
David Goodwin83b6ace2009-09-23 16:35:25 +0000419 return false;
420}
421
David Goodwina9c16fe2009-08-25 17:03:05 +0000422/// FixupKills - Fix the register kill flags, they may have been made
423/// incorrect by instruction reordering.
424///
425void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
David Greene2ae85ad2010-01-05 01:26:01 +0000426 DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
David Goodwina9c16fe2009-08-25 17:03:05 +0000427
428 std::set<unsigned> killedRegs;
429 BitVector ReservedRegs = TRI->getReservedRegs(MF);
David Goodwin856b38c2009-09-03 22:15:25 +0000430
431 StartBlockForKills(MBB);
Jim Grosbached0972f2010-05-14 21:19:48 +0000432
David Goodwine54c0042009-08-29 00:11:13 +0000433 // Examine block from end to start...
David Goodwina9c16fe2009-08-25 17:03:05 +0000434 unsigned Count = MBB->size();
435 for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
436 I != E; --Count) {
437 MachineInstr *MI = --I;
Dale Johannesen999c1882010-03-05 00:02:59 +0000438 if (MI->isDebugValue())
439 continue;
David Goodwina9c16fe2009-08-25 17:03:05 +0000440
David Goodwine54c0042009-08-29 00:11:13 +0000441 // Update liveness. Registers that are defed but not used in this
442 // instruction are now dead. Mark register and all subregs as they
443 // are completely defined.
444 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
445 MachineOperand &MO = MI->getOperand(i);
446 if (!MO.isReg()) continue;
447 unsigned Reg = MO.getReg();
448 if (Reg == 0) continue;
449 if (!MO.isDef()) continue;
450 // Ignore two-addr defs.
451 if (MI->isRegTiedToUseOperand(i)) continue;
Jim Grosbached0972f2010-05-14 21:19:48 +0000452
David Goodwine54c0042009-08-29 00:11:13 +0000453 KillIndices[Reg] = ~0u;
Jim Grosbached0972f2010-05-14 21:19:48 +0000454
David Goodwine54c0042009-08-29 00:11:13 +0000455 // Repeat for all subregs.
456 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
457 *Subreg; ++Subreg) {
458 KillIndices[*Subreg] = ~0u;
459 }
460 }
David Goodwina9c16fe2009-08-25 17:03:05 +0000461
David Goodwin83b6ace2009-09-23 16:35:25 +0000462 // Examine all used registers and set/clear kill flag. When a
463 // register is used multiple times we only set the kill flag on
464 // the first use.
David Goodwina9c16fe2009-08-25 17:03:05 +0000465 killedRegs.clear();
466 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
467 MachineOperand &MO = MI->getOperand(i);
468 if (!MO.isReg() || !MO.isUse()) continue;
469 unsigned Reg = MO.getReg();
470 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
471
David Goodwine54c0042009-08-29 00:11:13 +0000472 bool kill = false;
473 if (killedRegs.find(Reg) == killedRegs.end()) {
474 kill = true;
475 // A register is not killed if any subregs are live...
476 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
477 *Subreg; ++Subreg) {
478 if (KillIndices[*Subreg] != ~0u) {
479 kill = false;
480 break;
481 }
482 }
483
484 // If subreg is not live, then register is killed if it became
485 // live in this instruction
486 if (kill)
487 kill = (KillIndices[Reg] == ~0u);
488 }
Jim Grosbached0972f2010-05-14 21:19:48 +0000489
David Goodwina9c16fe2009-08-25 17:03:05 +0000490 if (MO.isKill() != kill) {
David Greene2ae85ad2010-01-05 01:26:01 +0000491 DEBUG(dbgs() << "Fixing " << MO << " in ");
Jakob Stoklund Olesen26f2fb72009-12-03 01:49:56 +0000492 // Warning: ToggleKillFlag may invalidate MO.
493 ToggleKillFlag(MI, MO);
David Goodwina9c16fe2009-08-25 17:03:05 +0000494 DEBUG(MI->dump());
495 }
Jim Grosbached0972f2010-05-14 21:19:48 +0000496
David Goodwina9c16fe2009-08-25 17:03:05 +0000497 killedRegs.insert(Reg);
498 }
Jim Grosbached0972f2010-05-14 21:19:48 +0000499
David Goodwin864c4b82009-08-31 20:47:02 +0000500 // Mark any used register (that is not using undef) and subregs as
501 // now live...
David Goodwine54c0042009-08-29 00:11:13 +0000502 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
503 MachineOperand &MO = MI->getOperand(i);
David Goodwin864c4b82009-08-31 20:47:02 +0000504 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
David Goodwine54c0042009-08-29 00:11:13 +0000505 unsigned Reg = MO.getReg();
506 if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
507
David Goodwine54c0042009-08-29 00:11:13 +0000508 KillIndices[Reg] = Count;
Jim Grosbached0972f2010-05-14 21:19:48 +0000509
David Goodwine54c0042009-08-29 00:11:13 +0000510 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
511 *Subreg; ++Subreg) {
512 KillIndices[*Subreg] = Count;
513 }
514 }
David Goodwina9c16fe2009-08-25 17:03:05 +0000515 }
516}
517
Dan Gohmand27a0e02008-11-19 23:18:57 +0000518//===----------------------------------------------------------------------===//
519// Top-Down Scheduling
520//===----------------------------------------------------------------------===//
521
522/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
523/// the PendingQueue if the count reaches zero. Also update its cycle bound.
David Goodwind34903a2009-11-20 19:32:48 +0000524void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
Dan Gohman604394b2008-12-09 22:54:47 +0000525 SUnit *SuccSU = SuccEdge->getSUnit();
Reid Klecknere9b95fd2009-09-30 20:15:38 +0000526
Dan Gohmand27a0e02008-11-19 23:18:57 +0000527#ifndef NDEBUG
Reid Klecknere9b95fd2009-09-30 20:15:38 +0000528 if (SuccSU->NumPredsLeft == 0) {
David Greene2ae85ad2010-01-05 01:26:01 +0000529 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohmand27a0e02008-11-19 23:18:57 +0000530 SuccSU->dump(this);
David Greene2ae85ad2010-01-05 01:26:01 +0000531 dbgs() << " has been released too many times!\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000532 llvm_unreachable(0);
Dan Gohmand27a0e02008-11-19 23:18:57 +0000533 }
534#endif
Reid Klecknere9b95fd2009-09-30 20:15:38 +0000535 --SuccSU->NumPredsLeft;
536
Dan Gohmand27a0e02008-11-19 23:18:57 +0000537 // Compute how many cycles it will be before this actually becomes
538 // available. This is the max of the start time of all predecessors plus
539 // their latencies.
David Goodwind34903a2009-11-20 19:32:48 +0000540 SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
Jim Grosbached0972f2010-05-14 21:19:48 +0000541
Dan Gohmana91eb052009-02-10 23:27:53 +0000542 // If all the node's predecessors are scheduled, this node is ready
543 // to be scheduled. Ignore the special ExitSU node.
544 if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
Dan Gohmand27a0e02008-11-19 23:18:57 +0000545 PendingQueue.push_back(SuccSU);
Dan Gohmana91eb052009-02-10 23:27:53 +0000546}
547
548/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
David Goodwind34903a2009-11-20 19:32:48 +0000549void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
Dan Gohmana91eb052009-02-10 23:27:53 +0000550 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
David Goodwin996892b2009-11-03 20:57:50 +0000551 I != E; ++I) {
David Goodwind34903a2009-11-20 19:32:48 +0000552 ReleaseSucc(SU, &*I);
David Goodwin996892b2009-11-03 20:57:50 +0000553 }
Dan Gohmand27a0e02008-11-19 23:18:57 +0000554}
555
556/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
557/// count of its successors. If a successor pending count is zero, add it to
558/// the Available queue.
David Goodwind34903a2009-11-20 19:32:48 +0000559void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
David Greene2ae85ad2010-01-05 01:26:01 +0000560 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohmand27a0e02008-11-19 23:18:57 +0000561 DEBUG(SU->dump(this));
Jim Grosbached0972f2010-05-14 21:19:48 +0000562
Dan Gohmand27a0e02008-11-19 23:18:57 +0000563 Sequence.push_back(SU);
Jim Grosbached0972f2010-05-14 21:19:48 +0000564 assert(CurCycle >= SU->getDepth() &&
David Goodwin996892b2009-11-03 20:57:50 +0000565 "Node scheduled above its depth!");
David Goodwind34903a2009-11-20 19:32:48 +0000566 SU->setDepthToAtLeast(CurCycle);
Dan Gohmand27a0e02008-11-19 23:18:57 +0000567
David Goodwind34903a2009-11-20 19:32:48 +0000568 ReleaseSuccessors(SU);
Dan Gohmand27a0e02008-11-19 23:18:57 +0000569 SU->isScheduled = true;
570 AvailableQueue.ScheduledNode(SU);
571}
572
573/// ListScheduleTopDown - The main loop of list scheduling for top-down
574/// schedulers.
David Goodwind34903a2009-11-20 19:32:48 +0000575void SchedulePostRATDList::ListScheduleTopDown() {
Dan Gohmand27a0e02008-11-19 23:18:57 +0000576 unsigned CurCycle = 0;
Jim Grosbached0972f2010-05-14 21:19:48 +0000577
David Goodwin996892b2009-11-03 20:57:50 +0000578 // We're scheduling top-down but we're visiting the regions in
579 // bottom-up order, so we don't know the hazards at the start of a
580 // region. So assume no hazards (this should usually be ok as most
581 // blocks are a single region).
582 HazardRec->Reset();
583
Dan Gohmana91eb052009-02-10 23:27:53 +0000584 // Release any successors of the special Entry node.
David Goodwind34903a2009-11-20 19:32:48 +0000585 ReleaseSuccessors(&EntrySU);
Dan Gohmana91eb052009-02-10 23:27:53 +0000586
David Goodwind34903a2009-11-20 19:32:48 +0000587 // Add all leaves to Available queue.
Dan Gohmand27a0e02008-11-19 23:18:57 +0000588 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
589 // It is available if it has no predecessors.
David Goodwin996892b2009-11-03 20:57:50 +0000590 bool available = SUnits[i].Preds.empty();
David Goodwin996892b2009-11-03 20:57:50 +0000591 if (available) {
Dan Gohmand27a0e02008-11-19 23:18:57 +0000592 AvailableQueue.push(&SUnits[i]);
593 SUnits[i].isAvailable = true;
594 }
595 }
Dan Gohmana91eb052009-02-10 23:27:53 +0000596
David Goodwin745d9d32009-08-12 21:47:46 +0000597 // In any cycle where we can't schedule any instructions, we must
598 // stall or emit a noop, depending on the target.
Benjamin Kramer0db71af2009-09-06 12:10:17 +0000599 bool CycleHasInsts = false;
David Goodwin745d9d32009-08-12 21:47:46 +0000600
Dan Gohmand27a0e02008-11-19 23:18:57 +0000601 // While Available queue is not empty, grab the node with the highest
602 // priority. If it is not ready put it back. Schedule the node.
Dan Gohman99603bb2009-01-16 01:33:36 +0000603 std::vector<SUnit*> NotReady;
Dan Gohmand27a0e02008-11-19 23:18:57 +0000604 Sequence.reserve(SUnits.size());
605 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
606 // Check to see if any of the pending instructions are ready to issue. If
607 // so, add them to the available queue.
Dan Gohman6b2ee8f2008-12-16 03:25:46 +0000608 unsigned MinDepth = ~0u;
Dan Gohmand27a0e02008-11-19 23:18:57 +0000609 for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
David Goodwind34903a2009-11-20 19:32:48 +0000610 if (PendingQueue[i]->getDepth() <= CurCycle) {
Dan Gohmand27a0e02008-11-19 23:18:57 +0000611 AvailableQueue.push(PendingQueue[i]);
612 PendingQueue[i]->isAvailable = true;
613 PendingQueue[i] = PendingQueue.back();
614 PendingQueue.pop_back();
615 --i; --e;
David Goodwind34903a2009-11-20 19:32:48 +0000616 } else if (PendingQueue[i]->getDepth() < MinDepth)
617 MinDepth = PendingQueue[i]->getDepth();
Dan Gohmand27a0e02008-11-19 23:18:57 +0000618 }
David Goodwin391efe02009-08-11 17:35:23 +0000619
David Greene2ae85ad2010-01-05 01:26:01 +0000620 DEBUG(dbgs() << "\n*** Examining Available\n";
David Goodwin387579f2009-08-11 17:56:42 +0000621 LatencyPriorityQueue q = AvailableQueue;
622 while (!q.empty()) {
623 SUnit *su = q.pop();
David Greene2ae85ad2010-01-05 01:26:01 +0000624 dbgs() << "Height " << su->getHeight() << ": ";
David Goodwin387579f2009-08-11 17:56:42 +0000625 su->dump(this);
626 });
David Goodwin391efe02009-08-11 17:35:23 +0000627
Dan Gohman99603bb2009-01-16 01:33:36 +0000628 SUnit *FoundSUnit = 0;
Dan Gohman99603bb2009-01-16 01:33:36 +0000629 bool HasNoopHazards = false;
630 while (!AvailableQueue.empty()) {
631 SUnit *CurSUnit = AvailableQueue.pop();
632
633 ScheduleHazardRecognizer::HazardType HT =
634 HazardRec->getHazardType(CurSUnit);
635 if (HT == ScheduleHazardRecognizer::NoHazard) {
636 FoundSUnit = CurSUnit;
637 break;
638 }
639
640 // Remember if this is a noop hazard.
641 HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
642
643 NotReady.push_back(CurSUnit);
644 }
645
646 // Add the nodes that aren't ready back onto the available list.
647 if (!NotReady.empty()) {
648 AvailableQueue.push_all(NotReady);
649 NotReady.clear();
650 }
651
David Goodwin996892b2009-11-03 20:57:50 +0000652 // If we found a node to schedule...
Dan Gohmand27a0e02008-11-19 23:18:57 +0000653 if (FoundSUnit) {
David Goodwin996892b2009-11-03 20:57:50 +0000654 // ... schedule the node...
David Goodwind34903a2009-11-20 19:32:48 +0000655 ScheduleNodeTopDown(FoundSUnit, CurCycle);
Dan Gohman99603bb2009-01-16 01:33:36 +0000656 HazardRec->EmitInstruction(FoundSUnit);
Benjamin Kramer0db71af2009-09-06 12:10:17 +0000657 CycleHasInsts = true;
Dan Gohman99603bb2009-01-16 01:33:36 +0000658 } else {
Benjamin Kramer0db71af2009-09-06 12:10:17 +0000659 if (CycleHasInsts) {
David Greene2ae85ad2010-01-05 01:26:01 +0000660 DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
David Goodwin745d9d32009-08-12 21:47:46 +0000661 HazardRec->AdvanceCycle();
662 } else if (!HasNoopHazards) {
663 // Otherwise, we have a pipeline stall, but no other problem,
664 // just advance the current cycle and try again.
David Greene2ae85ad2010-01-05 01:26:01 +0000665 DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
David Goodwin745d9d32009-08-12 21:47:46 +0000666 HazardRec->AdvanceCycle();
David Goodwind34903a2009-11-20 19:32:48 +0000667 ++NumStalls;
David Goodwin745d9d32009-08-12 21:47:46 +0000668 } else {
669 // Otherwise, we have no instructions to issue and we have instructions
670 // that will fault if we don't do this right. This is the case for
671 // processors without pipeline interlocks and other cases.
David Greene2ae85ad2010-01-05 01:26:01 +0000672 DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
David Goodwin745d9d32009-08-12 21:47:46 +0000673 HazardRec->EmitNoop();
674 Sequence.push_back(0); // NULL here means noop
David Goodwind34903a2009-11-20 19:32:48 +0000675 ++NumNoops;
David Goodwin745d9d32009-08-12 21:47:46 +0000676 }
677
Dan Gohman99603bb2009-01-16 01:33:36 +0000678 ++CurCycle;
Benjamin Kramer0db71af2009-09-06 12:10:17 +0000679 CycleHasInsts = false;
Dan Gohmand27a0e02008-11-19 23:18:57 +0000680 }
681 }
682
683#ifndef NDEBUG
Dan Gohmanf6e4a002008-11-20 01:26:25 +0000684 VerifySchedule(/*isBottomUp=*/false);
Dan Gohmand27a0e02008-11-19 23:18:57 +0000685#endif
686}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687
688//===----------------------------------------------------------------------===//
689// Public Constructor Functions
690//===----------------------------------------------------------------------===//
691
Evan Cheng86e24b02009-10-16 21:06:15 +0000692FunctionPass *llvm::createPostRAScheduler(CodeGenOpt::Level OptLevel) {
693 return new PostRAScheduler(OptLevel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694}