2007-07-13 17:31:29 +00:00
|
|
|
//===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
|
2007-07-13 17:13:54 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-13 17:13:54 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements a top-down list scheduler, using standard algorithms.
|
|
|
|
// The basic approach uses a priority queue of available nodes to schedule.
|
|
|
|
// One at a time, nodes are taken from the priority queue (thus in priority
|
|
|
|
// order), checked for legality to schedule, and emitted if legal.
|
|
|
|
//
|
|
|
|
// Nodes may not be legal to schedule either due to structural hazards (e.g.
|
|
|
|
// pipeline or resource constraints) or because an input to the instruction has
|
|
|
|
// not completed execution.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "post-RA-sched"
|
2009-08-10 15:55:25 +00:00
|
|
|
#include "ExactHazardRecognizer.h"
|
|
|
|
#include "SimpleHazardRecognizer.h"
|
2009-02-06 17:12:10 +00:00
|
|
|
#include "ScheduleDAGInstrs.h"
|
2007-07-13 17:13:54 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2008-11-19 23:18:57 +00:00
|
|
|
#include "llvm/CodeGen/LatencyPriorityQueue.h"
|
|
|
|
#include "llvm/CodeGen/SchedulerRegistry.h"
|
2008-12-16 03:25:46 +00:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2009-10-01 19:45:32 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-07-13 17:13:54 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2008-12-16 03:25:46 +00:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2008-11-25 00:52:40 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2009-01-16 01:33:36 +00:00
|
|
|
#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
|
2009-10-09 23:27:56 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2009-02-10 23:29:38 +00:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2009-01-15 19:20:50 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2008-11-25 00:52:40 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2009-09-30 00:10:16 +00:00
|
|
|
#include "llvm/Target/TargetSubtarget.h"
|
2008-01-14 19:00:06 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-07-13 17:13:54 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-11 01:44:26 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-11-19 23:18:57 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-11-25 00:52:40 +00:00
|
|
|
#include <map>
|
2009-08-25 17:03:05 +00:00
|
|
|
#include <set>
|
2007-07-13 17:13:54 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-01-16 01:33:36 +00:00
|
|
|
STATISTIC(NumNoops, "Number of noops inserted");
|
2008-11-19 23:18:57 +00:00
|
|
|
STATISTIC(NumStalls, "Number of pipeline stalls");
|
|
|
|
|
2009-10-01 21:46:35 +00:00
|
|
|
// Post-RA scheduling is enabled with
|
|
|
|
// TargetSubtarget.enablePostRAScheduler(). This flag can be used to
|
|
|
|
// override the target.
|
|
|
|
static cl::opt<bool>
|
|
|
|
EnablePostRAScheduler("post-RA-scheduler",
|
|
|
|
cl::desc("Enable scheduling after register allocation"),
|
2009-10-01 22:19:57 +00:00
|
|
|
cl::init(false), cl::Hidden);
|
2009-10-21 01:44:44 +00:00
|
|
|
static cl::opt<bool>
|
2008-11-25 00:52:40 +00:00
|
|
|
EnableAntiDepBreaking("break-anti-dependencies",
|
2009-10-21 01:44:44 +00:00
|
|
|
cl::desc("Break post-RA scheduling anti-dependencies"),
|
|
|
|
cl::init(true), cl::Hidden);
|
2009-01-16 01:33:36 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
EnablePostRAHazardAvoidance("avoid-hazards",
|
2009-08-10 15:55:25 +00:00
|
|
|
cl::desc("Enable exact hazard avoidance"),
|
2009-09-03 22:15:25 +00:00
|
|
|
cl::init(true), cl::Hidden);
|
2009-01-16 01:33:36 +00:00
|
|
|
|
2009-09-01 18:34:03 +00:00
|
|
|
// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
|
|
|
|
static cl::opt<int>
|
|
|
|
DebugDiv("postra-sched-debugdiv",
|
|
|
|
cl::desc("Debug control MBBs that are scheduled"),
|
|
|
|
cl::init(0), cl::Hidden);
|
|
|
|
static cl::opt<int>
|
|
|
|
DebugMod("postra-sched-debugmod",
|
|
|
|
cl::desc("Debug control MBBs that are scheduled"),
|
|
|
|
cl::init(0), cl::Hidden);
|
|
|
|
|
2007-07-13 17:13:54 +00:00
|
|
|
namespace {
|
2008-11-19 23:18:57 +00:00
|
|
|
class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass {
|
2009-10-09 23:27:56 +00:00
|
|
|
AliasAnalysis *AA;
|
2009-10-16 21:06:15 +00:00
|
|
|
CodeGenOpt::Level OptLevel;
|
2009-10-09 23:27:56 +00:00
|
|
|
|
2007-07-13 17:13:54 +00:00
|
|
|
public:
|
|
|
|
static char ID;
|
2009-10-16 21:06:15 +00:00
|
|
|
PostRAScheduler(CodeGenOpt::Level ol) :
|
|
|
|
MachineFunctionPass(&ID), OptLevel(ol) {}
|
2008-11-25 00:52:40 +00:00
|
|
|
|
2008-12-16 03:25:46 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const {
|
2009-07-31 23:37:33 +00:00
|
|
|
AU.setPreservesCFG();
|
2009-10-09 23:27:56 +00:00
|
|
|
AU.addRequired<AliasAnalysis>();
|
2008-12-16 03:25:46 +00:00
|
|
|
AU.addRequired<MachineDominatorTree>();
|
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
|
|
|
AU.addRequired<MachineLoopInfo>();
|
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
const char *getPassName() const {
|
2008-11-25 00:52:40 +00:00
|
|
|
return "Post RA top-down list latency scheduler";
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn);
|
|
|
|
};
|
|
|
|
char PostRAScheduler::ID = 0;
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs {
|
|
|
|
/// AvailableQueue - The priority queue to use for the available SUnits.
|
2009-10-21 01:44:44 +00:00
|
|
|
///
|
2008-11-19 23:18:57 +00:00
|
|
|
LatencyPriorityQueue AvailableQueue;
|
|
|
|
|
|
|
|
/// PendingQueue - This contains all of the instructions whose operands have
|
|
|
|
/// been issued, but their results are not ready yet (due to the latency of
|
|
|
|
/// the operation). Once the operands becomes available, the instruction is
|
|
|
|
/// added to the AvailableQueue.
|
|
|
|
std::vector<SUnit*> PendingQueue;
|
|
|
|
|
2008-11-25 00:52:40 +00:00
|
|
|
/// Topo - A topological ordering for SUnits.
|
|
|
|
ScheduleDAGTopologicalSort Topo;
|
2007-07-13 17:13:54 +00:00
|
|
|
|
2009-10-20 19:54:44 +00:00
|
|
|
/// AllocatableSet - The set of allocatable registers.
|
|
|
|
/// We'll be ignoring anti-dependencies on non-allocatable registers,
|
|
|
|
/// because they may not be safe to break.
|
|
|
|
const BitVector AllocatableSet;
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
/// HazardRec - The hazard recognizer to use.
|
|
|
|
ScheduleHazardRecognizer *HazardRec;
|
|
|
|
|
|
|
|
/// AA - AliasAnalysis for making memory reference queries.
|
|
|
|
AliasAnalysis *AA;
|
2009-10-20 19:54:44 +00:00
|
|
|
|
2009-10-22 23:19:17 +00:00
|
|
|
/// AntiDepMode - Anti-dependence breaking mode
|
|
|
|
TargetSubtarget::AntiDepBreakMode AntiDepMode;
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
/// Classes - For live regs that are only used in one register class in a
|
|
|
|
/// live range, the register class. If the register is not live, the
|
|
|
|
/// corresponding value is null. If the register is live but used in
|
|
|
|
/// multiple register classes, the corresponding value is -1 casted to a
|
|
|
|
/// pointer.
|
|
|
|
const TargetRegisterClass *
|
|
|
|
Classes[TargetRegisterInfo::FirstVirtualRegister];
|
2009-02-10 23:27:53 +00:00
|
|
|
|
|
|
|
/// RegRegs - Map registers to all their references within a live range.
|
2009-10-21 01:44:44 +00:00
|
|
|
std::multimap<unsigned, MachineOperand *> RegRefs;
|
2009-02-10 23:27:53 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
/// KillIndices - The index of the most recent kill (proceding bottom-up),
|
|
|
|
/// or ~0u if the register is not live.
|
2009-02-10 23:27:53 +00:00
|
|
|
unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
|
|
|
|
|
2009-10-01 08:26:23 +00:00
|
|
|
/// DefIndices - The index of the most recent complete def (proceding bottom
|
|
|
|
/// up), or ~0u if the register is live.
|
2009-02-10 23:27:53 +00:00
|
|
|
unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister];
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
/// KeepRegs - A set of registers which are live and cannot be changed to
|
|
|
|
/// break anti-dependencies.
|
|
|
|
SmallSet<unsigned, 4> KeepRegs;
|
|
|
|
|
2008-11-25 00:52:40 +00:00
|
|
|
public:
|
2009-01-15 19:20:50 +00:00
|
|
|
SchedulePostRATDList(MachineFunction &MF,
|
2008-12-16 03:25:46 +00:00
|
|
|
const MachineLoopInfo &MLI,
|
2009-01-16 01:33:36 +00:00
|
|
|
const MachineDominatorTree &MDT,
|
2009-10-09 23:27:56 +00:00
|
|
|
ScheduleHazardRecognizer *HR,
|
2009-10-22 23:19:17 +00:00
|
|
|
AliasAnalysis *aa,
|
|
|
|
TargetSubtarget::AntiDepBreakMode adm)
|
2009-01-15 19:20:50 +00:00
|
|
|
: ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits),
|
2009-10-21 01:44:44 +00:00
|
|
|
AllocatableSet(TRI->getAllocatableSet(MF)),
|
2009-10-22 23:19:17 +00:00
|
|
|
HazardRec(HR), AA(aa), AntiDepMode(adm) {}
|
2009-01-16 01:33:36 +00:00
|
|
|
|
|
|
|
~SchedulePostRATDList() {
|
|
|
|
delete HazardRec;
|
|
|
|
}
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
/// StartBlock - Initialize register live-range state for scheduling in
|
|
|
|
/// this block.
|
|
|
|
///
|
|
|
|
void StartBlock(MachineBasicBlock *BB);
|
|
|
|
|
2009-10-20 19:54:44 +00:00
|
|
|
/// Schedule - Schedule the instruction range using list scheduling.
|
2009-02-10 23:27:53 +00:00
|
|
|
///
|
2009-10-20 19:54:44 +00:00
|
|
|
void Schedule();
|
|
|
|
|
|
|
|
/// FixupKills - Fix register kill flags that have been made
|
|
|
|
/// invalid due to scheduling
|
|
|
|
///
|
|
|
|
void FixupKills(MachineBasicBlock *MBB);
|
2009-02-10 23:27:53 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
/// Observe - Update liveness information to account for the current
|
|
|
|
/// instruction, which will not be scheduled.
|
|
|
|
///
|
|
|
|
void Observe(MachineInstr *MI, unsigned Count);
|
2009-10-20 19:54:44 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
/// FinishBlock - Clean up register live-range state.
|
|
|
|
///
|
|
|
|
void FinishBlock();
|
2009-10-20 19:54:44 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
private:
|
|
|
|
void PrescanInstruction(MachineInstr *MI);
|
|
|
|
void ScanInstruction(MachineInstr *MI, unsigned Count);
|
2008-12-09 22:54:47 +00:00
|
|
|
void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
|
2009-02-10 23:27:53 +00:00
|
|
|
void ReleaseSuccessors(SUnit *SU);
|
2008-11-19 23:18:57 +00:00
|
|
|
void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
|
|
|
|
void ListScheduleTopDown();
|
2009-10-21 01:44:44 +00:00
|
|
|
bool BreakAntiDependencies();
|
|
|
|
unsigned findSuitableFreeRegister(unsigned AntiDepReg,
|
|
|
|
unsigned LastNewReg,
|
|
|
|
const TargetRegisterClass *);
|
2009-09-03 22:15:25 +00:00
|
|
|
void StartBlockForKills(MachineBasicBlock *BB);
|
2009-09-23 16:35:25 +00:00
|
|
|
|
|
|
|
// ToggleKillFlag - Toggle a register operand kill flag. Other
|
|
|
|
// adjustments may be made to the instruction if necessary. Return
|
|
|
|
// true if the operand has been deleted, false if not.
|
|
|
|
bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
|
2007-07-13 17:13:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
/// isSchedulingBoundary - Test if the given instruction should be
|
|
|
|
/// considered a scheduling boundary. This primarily includes labels
|
|
|
|
/// and terminators.
|
|
|
|
///
|
|
|
|
static bool isSchedulingBoundary(const MachineInstr *MI,
|
|
|
|
const MachineFunction &MF) {
|
|
|
|
// Terminators and labels can't be scheduled around.
|
|
|
|
if (MI->getDesc().isTerminator() || MI->isLabel())
|
|
|
|
return true;
|
|
|
|
|
2009-02-10 23:29:38 +00:00
|
|
|
// Don't attempt to schedule around any instruction that modifies
|
|
|
|
// a stack-oriented pointer, as it's unlikely to be profitable. This
|
|
|
|
// saves compile time, because it doesn't require every single
|
|
|
|
// stack slot reference to depend on the instruction that does the
|
|
|
|
// modification.
|
|
|
|
const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
|
|
|
|
if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore()))
|
|
|
|
return true;
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
|
2009-10-10 00:15:38 +00:00
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
|
|
|
|
2009-10-01 21:46:35 +00:00
|
|
|
// Check for explicit enable/disable of post-ra scheduling.
|
2009-10-22 23:19:17 +00:00
|
|
|
TargetSubtarget::AntiDepBreakMode AntiDepMode = TargetSubtarget::ANTIDEP_NONE;
|
2009-10-01 21:46:35 +00:00
|
|
|
if (EnablePostRAScheduler.getPosition() > 0) {
|
|
|
|
if (!EnablePostRAScheduler)
|
2009-10-16 06:10:34 +00:00
|
|
|
return false;
|
2009-10-01 21:46:35 +00:00
|
|
|
} else {
|
2009-10-16 06:10:34 +00:00
|
|
|
// Check that post-RA scheduling is enabled for this target.
|
2009-10-01 21:46:35 +00:00
|
|
|
const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>();
|
2009-10-22 23:19:17 +00:00
|
|
|
if (!ST.enablePostRAScheduler(OptLevel, AntiDepMode))
|
2009-10-16 06:10:34 +00:00
|
|
|
return false;
|
2009-10-01 21:46:35 +00:00
|
|
|
}
|
2009-09-30 00:10:16 +00:00
|
|
|
|
2009-10-22 23:19:17 +00:00
|
|
|
// Check for antidep breaking override...
|
|
|
|
if (EnableAntiDepBreaking.getPosition() > 0) {
|
|
|
|
AntiDepMode = (EnableAntiDepBreaking) ?
|
|
|
|
TargetSubtarget::ANTIDEP_CRITICAL : TargetSubtarget::ANTIDEP_NONE;
|
|
|
|
}
|
|
|
|
|
2009-08-11 01:44:26 +00:00
|
|
|
DEBUG(errs() << "PostRAScheduler\n");
|
2007-07-13 17:13:54 +00:00
|
|
|
|
2008-12-16 03:25:46 +00:00
|
|
|
const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
|
|
|
|
const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
|
2009-08-10 15:55:25 +00:00
|
|
|
const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData();
|
2009-01-16 01:33:36 +00:00
|
|
|
ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ?
|
2009-08-10 15:55:25 +00:00
|
|
|
(ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) :
|
|
|
|
(ScheduleHazardRecognizer *)new SimpleHazardRecognizer();
|
2008-12-16 03:25:46 +00:00
|
|
|
|
2009-10-22 23:19:17 +00:00
|
|
|
SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR, AA, AntiDepMode);
|
2009-01-15 19:20:50 +00:00
|
|
|
|
2007-07-13 17:13:54 +00:00
|
|
|
// Loop over all of the basic blocks
|
|
|
|
for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
|
2008-11-19 23:18:57 +00:00
|
|
|
MBB != MBBe; ++MBB) {
|
2009-09-01 18:34:03 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
|
|
|
|
if (DebugDiv > 0) {
|
|
|
|
static int bbcnt = 0;
|
|
|
|
if (bbcnt++ % DebugDiv != DebugMod)
|
|
|
|
continue;
|
|
|
|
errs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() <<
|
|
|
|
":MBB ID#" << MBB->getNumber() << " ***\n";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
// Initialize register live-range state for scheduling in this block.
|
|
|
|
Scheduler.StartBlock(MBB);
|
|
|
|
|
2009-01-16 22:10:20 +00:00
|
|
|
// Schedule each sequence of instructions not interrupted by a label
|
|
|
|
// or anything else that effectively needs to shut down scheduling.
|
2009-02-10 23:27:53 +00:00
|
|
|
MachineBasicBlock::iterator Current = MBB->end();
|
2009-02-11 04:27:20 +00:00
|
|
|
unsigned Count = MBB->size(), CurrentCount = Count;
|
2009-02-10 23:27:53 +00:00
|
|
|
for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
|
|
|
|
MachineInstr *MI = prior(I);
|
|
|
|
if (isSchedulingBoundary(MI, Fn)) {
|
2009-03-10 18:10:43 +00:00
|
|
|
Scheduler.Run(MBB, I, Current, CurrentCount);
|
2009-09-18 21:02:19 +00:00
|
|
|
Scheduler.EmitSchedule(0);
|
2009-02-10 23:27:53 +00:00
|
|
|
Current = MI;
|
2009-02-11 04:27:20 +00:00
|
|
|
CurrentCount = Count - 1;
|
2009-03-10 18:10:43 +00:00
|
|
|
Scheduler.Observe(MI, CurrentCount);
|
2009-01-16 22:10:20 +00:00
|
|
|
}
|
2009-02-10 23:27:53 +00:00
|
|
|
I = MI;
|
2009-02-11 04:27:20 +00:00
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
assert(Count == 0 && "Instruction count mismatch!");
|
2009-03-11 09:04:34 +00:00
|
|
|
assert((MBB->begin() == Current || CurrentCount != 0) &&
|
2009-03-10 18:10:43 +00:00
|
|
|
"Instruction count mismatch!");
|
|
|
|
Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount);
|
2009-09-18 21:02:19 +00:00
|
|
|
Scheduler.EmitSchedule(0);
|
2009-02-10 23:27:53 +00:00
|
|
|
|
|
|
|
// Clean up register live-range state.
|
|
|
|
Scheduler.FinishBlock();
|
2009-08-25 17:03:05 +00:00
|
|
|
|
2009-09-03 22:15:25 +00:00
|
|
|
// Update register kills
|
2009-08-25 17:03:05 +00:00
|
|
|
Scheduler.FixupKills(MBB);
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
2007-07-13 17:13:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
/// StartBlock - Initialize register live-range state for scheduling in
|
|
|
|
/// this block.
|
|
|
|
///
|
|
|
|
void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) {
|
|
|
|
// Call the superclass.
|
|
|
|
ScheduleDAGInstrs::StartBlock(BB);
|
|
|
|
|
2009-08-10 15:55:25 +00:00
|
|
|
// Reset the hazard recognizer.
|
|
|
|
HazardRec->Reset();
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
// Clear out the register class data.
|
|
|
|
std::fill(Classes, array_endof(Classes),
|
|
|
|
static_cast<const TargetRegisterClass *>(0));
|
2009-02-10 23:27:53 +00:00
|
|
|
|
|
|
|
// Initialize the indices to indicate that no registers are live.
|
|
|
|
std::fill(KillIndices, array_endof(KillIndices), ~0u);
|
|
|
|
std::fill(DefIndices, array_endof(DefIndices), BB->size());
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
// Clear "do not change" set.
|
|
|
|
KeepRegs.clear();
|
|
|
|
|
2009-10-01 23:28:47 +00:00
|
|
|
bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
// Determine the live-out physregs for this block.
|
2009-10-01 23:28:47 +00:00
|
|
|
if (IsReturnBlock) {
|
2009-02-10 23:27:53 +00:00
|
|
|
// In a return block, examine the function live-out regs.
|
|
|
|
for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
|
|
|
|
E = MRI.liveout_end(); I != E; ++I) {
|
|
|
|
unsigned Reg = *I;
|
2009-10-21 01:44:44 +00:00
|
|
|
Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
2009-02-10 23:27:53 +00:00
|
|
|
KillIndices[Reg] = BB->size();
|
|
|
|
DefIndices[Reg] = ~0u;
|
|
|
|
// Repeat, for all aliases.
|
|
|
|
for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
|
|
|
|
unsigned AliasReg = *Alias;
|
2009-10-21 01:44:44 +00:00
|
|
|
Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
2009-02-10 23:27:53 +00:00
|
|
|
KillIndices[AliasReg] = BB->size();
|
|
|
|
DefIndices[AliasReg] = ~0u;
|
|
|
|
}
|
|
|
|
}
|
2009-10-01 19:45:32 +00:00
|
|
|
} else {
|
2009-02-10 23:27:53 +00:00
|
|
|
// In a non-return block, examine the live-in regs of all successors.
|
|
|
|
for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
|
2009-02-11 04:27:20 +00:00
|
|
|
SE = BB->succ_end(); SI != SE; ++SI)
|
2009-02-10 23:27:53 +00:00
|
|
|
for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
|
|
|
|
E = (*SI)->livein_end(); I != E; ++I) {
|
|
|
|
unsigned Reg = *I;
|
2009-10-21 01:44:44 +00:00
|
|
|
Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
2009-02-10 23:27:53 +00:00
|
|
|
KillIndices[Reg] = BB->size();
|
|
|
|
DefIndices[Reg] = ~0u;
|
|
|
|
// Repeat, for all aliases.
|
|
|
|
for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
|
|
|
|
unsigned AliasReg = *Alias;
|
2009-10-21 01:44:44 +00:00
|
|
|
Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
2009-02-10 23:27:53 +00:00
|
|
|
KillIndices[AliasReg] = BB->size();
|
|
|
|
DefIndices[AliasReg] = ~0u;
|
|
|
|
}
|
|
|
|
}
|
2009-10-01 23:28:47 +00:00
|
|
|
}
|
2009-02-10 23:27:53 +00:00
|
|
|
|
2009-10-01 23:28:47 +00:00
|
|
|
// Mark live-out callee-saved registers. In a return block this is
|
|
|
|
// all callee-saved registers. In non-return this is any
|
|
|
|
// callee-saved register that is not saved in the prolog.
|
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
BitVector Pristine = MFI->getPristineRegs(BB);
|
|
|
|
for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
|
|
|
|
unsigned Reg = *I;
|
|
|
|
if (!IsReturnBlock && !Pristine.test(Reg)) continue;
|
2009-10-21 01:44:44 +00:00
|
|
|
Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
2009-10-01 23:28:47 +00:00
|
|
|
KillIndices[Reg] = BB->size();
|
|
|
|
DefIndices[Reg] = ~0u;
|
|
|
|
// Repeat, for all aliases.
|
|
|
|
for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
|
|
|
|
unsigned AliasReg = *Alias;
|
2009-10-21 01:44:44 +00:00
|
|
|
Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
2009-10-01 23:28:47 +00:00
|
|
|
KillIndices[AliasReg] = BB->size();
|
|
|
|
DefIndices[AliasReg] = ~0u;
|
2009-02-10 23:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Schedule - Schedule the instruction range using list scheduling.
|
|
|
|
///
|
2008-11-19 23:18:57 +00:00
|
|
|
void SchedulePostRATDList::Schedule() {
|
2009-08-11 01:44:26 +00:00
|
|
|
DEBUG(errs() << "********** List Scheduling **********\n");
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2008-12-23 18:36:58 +00:00
|
|
|
// Build the scheduling graph.
|
2009-10-09 23:27:56 +00:00
|
|
|
BuildSchedGraph(AA);
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2009-10-22 23:19:17 +00:00
|
|
|
if (AntiDepMode != TargetSubtarget::ANTIDEP_NONE) {
|
2009-10-21 01:44:44 +00:00
|
|
|
if (BreakAntiDependencies()) {
|
2008-11-25 00:52:40 +00:00
|
|
|
// We made changes. Update the dependency graph.
|
|
|
|
// Theoretically we could update the graph in place:
|
|
|
|
// When a live range is changed to use a different register, remove
|
|
|
|
// the def's anti-dependence *and* output-dependence edges due to
|
|
|
|
// that register, and add new anti-dependence and output-dependence
|
|
|
|
// edges based on the next live range of the register.
|
|
|
|
SUnits.clear();
|
2009-02-10 23:27:53 +00:00
|
|
|
EntrySU = SUnit();
|
|
|
|
ExitSU = SUnit();
|
2009-10-09 23:27:56 +00:00
|
|
|
BuildSchedGraph(AA);
|
2008-11-25 00:52:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-10 15:55:25 +00:00
|
|
|
DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
|
|
|
|
SUnits[su].dumpAll(this));
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
AvailableQueue.initNodes(SUnits);
|
2008-11-25 00:52:40 +00:00
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
ListScheduleTopDown();
|
|
|
|
|
|
|
|
AvailableQueue.releaseState();
|
|
|
|
}
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
/// Observe - Update liveness information to account for the current
|
|
|
|
/// instruction, which will not be scheduled.
|
|
|
|
///
|
2009-02-11 04:27:20 +00:00
|
|
|
void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
|
2009-03-10 18:10:43 +00:00
|
|
|
assert(Count < InsertPosIndex && "Instruction index out of expected range!");
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
// Any register which was defined within the previous scheduling region
|
|
|
|
// may have been rescheduled and its lifetime may overlap with registers
|
|
|
|
// in ways not reflected in our current liveness state. For each such
|
|
|
|
// register, adjust the liveness state to be conservatively correct.
|
|
|
|
for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg)
|
|
|
|
if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
|
|
|
|
assert(KillIndices[Reg] == ~0u && "Clobbered register is live!");
|
|
|
|
// Mark this register to be non-renamable.
|
|
|
|
Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
|
|
|
// Move the def index to the end of the previous region, to reflect
|
|
|
|
// that the def could theoretically have been scheduled at the end.
|
|
|
|
DefIndices[Reg] = InsertPosIndex;
|
2009-03-10 18:10:43 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
PrescanInstruction(MI);
|
2009-02-11 04:27:20 +00:00
|
|
|
ScanInstruction(MI, Count);
|
2009-02-10 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// FinishBlock - Clean up register live-range state.
|
|
|
|
///
|
|
|
|
void SchedulePostRATDList::FinishBlock() {
|
|
|
|
RegRefs.clear();
|
|
|
|
|
|
|
|
// Call the superclass.
|
|
|
|
ScheduleDAGInstrs::FinishBlock();
|
|
|
|
}
|
|
|
|
|
2008-12-16 03:25:46 +00:00
|
|
|
/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
|
|
|
|
/// critical path.
|
|
|
|
static SDep *CriticalPathStep(SUnit *SU) {
|
|
|
|
SDep *Next = 0;
|
|
|
|
unsigned NextDepth = 0;
|
|
|
|
// Find the predecessor edge with the greatest depth.
|
|
|
|
for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
|
|
|
|
P != PE; ++P) {
|
|
|
|
SUnit *PredSU = P->getSUnit();
|
|
|
|
unsigned PredLatency = P->getLatency();
|
|
|
|
unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
|
|
|
|
// In the case of a latency tie, prefer an anti-dependency edge over
|
|
|
|
// other types of edges.
|
|
|
|
if (NextDepth < PredTotalLatency ||
|
|
|
|
(NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
|
|
|
|
NextDepth = PredTotalLatency;
|
|
|
|
Next = &*P;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Next;
|
|
|
|
}
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
void SchedulePostRATDList::PrescanInstruction(MachineInstr *MI) {
|
|
|
|
// Scan the register operands for this instruction and update
|
|
|
|
// Classes and RegRefs.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg()) continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (Reg == 0) continue;
|
|
|
|
const TargetRegisterClass *NewRC = 0;
|
|
|
|
|
|
|
|
if (i < MI->getDesc().getNumOperands())
|
|
|
|
NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
|
|
|
|
|
|
|
|
// For now, only allow the register to be changed if its register
|
|
|
|
// class is consistent across all uses.
|
|
|
|
if (!Classes[Reg] && NewRC)
|
|
|
|
Classes[Reg] = NewRC;
|
|
|
|
else if (!NewRC || Classes[Reg] != NewRC)
|
|
|
|
Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
|
|
|
|
|
|
|
// Now check for aliases.
|
|
|
|
for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
|
|
|
|
// If an alias of the reg is used during the live range, give up.
|
|
|
|
// Note that this allows us to skip checking if AntiDepReg
|
|
|
|
// overlaps with any of the aliases, among other things.
|
|
|
|
unsigned AliasReg = *Alias;
|
|
|
|
if (Classes[AliasReg]) {
|
|
|
|
Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
|
|
|
Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're still willing to consider this register, note the reference.
|
|
|
|
if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
|
|
|
|
RegRefs.insert(std::make_pair(Reg, &MO));
|
|
|
|
|
|
|
|
// It's not safe to change register allocation for source operands of
|
|
|
|
// that have special allocation requirements.
|
|
|
|
if (MO.isUse() && MI->getDesc().hasExtraSrcRegAllocReq()) {
|
|
|
|
if (KeepRegs.insert(Reg)) {
|
|
|
|
for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
|
|
|
|
*Subreg; ++Subreg)
|
|
|
|
KeepRegs.insert(*Subreg);
|
|
|
|
}
|
2009-10-01 19:45:32 +00:00
|
|
|
}
|
2009-02-10 23:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
void SchedulePostRATDList::ScanInstruction(MachineInstr *MI,
|
|
|
|
unsigned Count) {
|
|
|
|
// Update liveness.
|
|
|
|
// Proceding upwards, registers that are defed but not used in this
|
|
|
|
// instruction are now dead.
|
2009-02-10 23:27:53 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
2009-10-21 01:44:44 +00:00
|
|
|
if (!MO.isReg()) continue;
|
2009-02-10 23:27:53 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (Reg == 0) continue;
|
2009-10-21 01:44:44 +00:00
|
|
|
if (!MO.isDef()) continue;
|
|
|
|
// Ignore two-addr defs.
|
2009-04-09 17:16:43 +00:00
|
|
|
if (MI->isRegTiedToUseOperand(i)) continue;
|
2009-02-10 23:27:53 +00:00
|
|
|
|
|
|
|
DefIndices[Reg] = Count;
|
2009-10-21 01:44:44 +00:00
|
|
|
KillIndices[Reg] = ~0u;
|
|
|
|
assert(((KillIndices[Reg] == ~0u) !=
|
|
|
|
(DefIndices[Reg] == ~0u)) &&
|
|
|
|
"Kill and Def maps aren't consistent for Reg!");
|
|
|
|
KeepRegs.erase(Reg);
|
|
|
|
Classes[Reg] = 0;
|
|
|
|
RegRefs.erase(Reg);
|
|
|
|
// Repeat, for all subregs.
|
2009-02-10 23:27:53 +00:00
|
|
|
for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
|
|
|
|
*Subreg; ++Subreg) {
|
|
|
|
unsigned SubregReg = *Subreg;
|
|
|
|
DefIndices[SubregReg] = Count;
|
2009-10-21 01:44:44 +00:00
|
|
|
KillIndices[SubregReg] = ~0u;
|
|
|
|
KeepRegs.erase(SubregReg);
|
|
|
|
Classes[SubregReg] = 0;
|
|
|
|
RegRefs.erase(SubregReg);
|
|
|
|
}
|
|
|
|
// Conservatively mark super-registers as unusable.
|
|
|
|
for (const unsigned *Super = TRI->getSuperRegisters(Reg);
|
|
|
|
*Super; ++Super) {
|
|
|
|
unsigned SuperReg = *Super;
|
|
|
|
Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
2009-02-10 23:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
2009-10-21 01:44:44 +00:00
|
|
|
if (!MO.isReg()) continue;
|
2009-02-10 23:27:53 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (Reg == 0) continue;
|
2009-10-21 01:44:44 +00:00
|
|
|
if (!MO.isUse()) continue;
|
2009-02-10 23:27:53 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
const TargetRegisterClass *NewRC = 0;
|
2009-10-20 19:54:44 +00:00
|
|
|
if (i < MI->getDesc().getNumOperands())
|
2009-10-21 01:44:44 +00:00
|
|
|
NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
|
2009-02-10 23:27:53 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
// For now, only allow the register to be changed if its register
|
|
|
|
// class is consistent across all uses.
|
|
|
|
if (!Classes[Reg] && NewRC)
|
|
|
|
Classes[Reg] = NewRC;
|
|
|
|
else if (!NewRC || Classes[Reg] != NewRC)
|
|
|
|
Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
|
2009-10-20 22:50:43 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
RegRefs.insert(std::make_pair(Reg, &MO));
|
2009-10-20 22:50:43 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
// It wasn't previously live but now it is, this is a kill.
|
|
|
|
if (KillIndices[Reg] == ~0u) {
|
2009-02-10 23:27:53 +00:00
|
|
|
KillIndices[Reg] = Count;
|
|
|
|
DefIndices[Reg] = ~0u;
|
2009-10-21 01:44:44 +00:00
|
|
|
assert(((KillIndices[Reg] == ~0u) !=
|
|
|
|
(DefIndices[Reg] == ~0u)) &&
|
|
|
|
"Kill and Def maps aren't consistent for Reg!");
|
2009-02-10 23:27:53 +00:00
|
|
|
}
|
2009-10-21 01:44:44 +00:00
|
|
|
// Repeat, for all aliases.
|
|
|
|
for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
|
|
|
|
unsigned AliasReg = *Alias;
|
|
|
|
if (KillIndices[AliasReg] == ~0u) {
|
|
|
|
KillIndices[AliasReg] = Count;
|
|
|
|
DefIndices[AliasReg] = ~0u;
|
2009-02-10 23:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
unsigned
|
|
|
|
SchedulePostRATDList::findSuitableFreeRegister(unsigned AntiDepReg,
|
|
|
|
unsigned LastNewReg,
|
|
|
|
const TargetRegisterClass *RC) {
|
|
|
|
for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF),
|
|
|
|
RE = RC->allocation_order_end(MF); R != RE; ++R) {
|
|
|
|
unsigned NewReg = *R;
|
|
|
|
// Don't replace a register with itself.
|
|
|
|
if (NewReg == AntiDepReg) continue;
|
|
|
|
// Don't replace a register with one that was recently used to repair
|
|
|
|
// an anti-dependence with this AntiDepReg, because that would
|
|
|
|
// re-introduce that anti-dependence.
|
|
|
|
if (NewReg == LastNewReg) continue;
|
|
|
|
// If NewReg is dead and NewReg's most recent def is not before
|
|
|
|
// AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
|
|
|
|
assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u)) &&
|
|
|
|
"Kill and Def maps aren't consistent for AntiDepReg!");
|
|
|
|
assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u)) &&
|
|
|
|
"Kill and Def maps aren't consistent for NewReg!");
|
|
|
|
if (KillIndices[NewReg] != ~0u ||
|
|
|
|
Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
|
|
|
|
KillIndices[AntiDepReg] > DefIndices[NewReg])
|
|
|
|
continue;
|
|
|
|
return NewReg;
|
2009-08-12 01:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No registers are free and available!
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-25 00:52:40 +00:00
|
|
|
/// BreakAntiDependencies - Identifiy anti-dependencies along the critical path
|
|
|
|
/// of the ScheduleDAG and break them by renaming registers.
|
|
|
|
///
|
2009-10-21 01:44:44 +00:00
|
|
|
bool SchedulePostRATDList::BreakAntiDependencies() {
|
2008-11-25 00:52:40 +00:00
|
|
|
// The code below assumes that there is at least one instruction,
|
|
|
|
// so just duck out immediately if the block is empty.
|
2009-01-16 21:37:14 +00:00
|
|
|
if (SUnits.empty()) return false;
|
2008-11-25 00:52:40 +00:00
|
|
|
|
2008-12-16 03:25:46 +00:00
|
|
|
// Find the node at the bottom of the critical path.
|
2009-10-21 01:44:44 +00:00
|
|
|
SUnit *Max = 0;
|
|
|
|
for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
|
|
|
|
SUnit *SU = &SUnits[i];
|
|
|
|
if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
|
|
|
|
Max = SU;
|
2009-10-20 19:54:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
#ifndef NDEBUG
|
2009-10-20 19:54:44 +00:00
|
|
|
{
|
2009-10-21 01:44:44 +00:00
|
|
|
DEBUG(errs() << "Critical path has total latency "
|
|
|
|
<< (Max->getDepth() + Max->Latency) << "\n");
|
2009-10-13 19:16:03 +00:00
|
|
|
DEBUG(errs() << "Available regs:");
|
|
|
|
for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
|
2009-10-21 01:44:44 +00:00
|
|
|
if (KillIndices[Reg] == ~0u)
|
2009-10-13 19:16:03 +00:00
|
|
|
DEBUG(errs() << " " << TRI->getName(Reg));
|
|
|
|
}
|
|
|
|
DEBUG(errs() << '\n');
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-25 00:52:40 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
// Track progress along the critical path through the SUnit graph as we walk
|
|
|
|
// the instructions.
|
|
|
|
SUnit *CriticalPathSU = Max;
|
|
|
|
MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
|
|
|
|
|
|
|
|
// Consider this pattern:
|
|
|
|
// A = ...
|
|
|
|
// ... = A
|
|
|
|
// A = ...
|
|
|
|
// ... = A
|
|
|
|
// A = ...
|
|
|
|
// ... = A
|
|
|
|
// A = ...
|
|
|
|
// ... = A
|
|
|
|
// There are three anti-dependencies here, and without special care,
|
|
|
|
// we'd break all of them using the same register:
|
|
|
|
// A = ...
|
|
|
|
// ... = A
|
|
|
|
// B = ...
|
|
|
|
// ... = B
|
|
|
|
// B = ...
|
|
|
|
// ... = B
|
|
|
|
// B = ...
|
|
|
|
// ... = B
|
|
|
|
// because at each anti-dependence, B is the first register that
|
|
|
|
// isn't A which is free. This re-introduces anti-dependencies
|
|
|
|
// at all but one of the original anti-dependencies that we were
|
|
|
|
// trying to break. To avoid this, keep track of the most recent
|
|
|
|
// register that each register was replaced with, avoid
|
|
|
|
// using it to repair an anti-dependence on the same register.
|
|
|
|
// This lets us produce this:
|
|
|
|
// A = ...
|
|
|
|
// ... = A
|
|
|
|
// B = ...
|
|
|
|
// ... = B
|
|
|
|
// C = ...
|
|
|
|
// ... = C
|
|
|
|
// B = ...
|
|
|
|
// ... = B
|
|
|
|
// This still has an anti-dependence on B, but at least it isn't on the
|
|
|
|
// original critical path.
|
|
|
|
//
|
|
|
|
// TODO: If we tracked more than one register here, we could potentially
|
|
|
|
// fix that remaining critical edge too. This is a little more involved,
|
|
|
|
// because unlike the most recent register, less recent registers should
|
|
|
|
// still be considered, though only if no other registers are available.
|
|
|
|
unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {};
|
|
|
|
|
|
|
|
// Attempt to break anti-dependence edges on the critical path. Walk the
|
|
|
|
// instructions from the bottom up, tracking information about liveness
|
|
|
|
// as we go to help determine which registers are available.
|
2008-11-25 00:52:40 +00:00
|
|
|
bool Changed = false;
|
2009-02-11 04:27:20 +00:00
|
|
|
unsigned Count = InsertPosIndex - 1;
|
|
|
|
for (MachineBasicBlock::iterator I = InsertPos, E = Begin;
|
2009-02-03 18:57:45 +00:00
|
|
|
I != E; --Count) {
|
|
|
|
MachineInstr *MI = --I;
|
2008-11-25 00:52:40 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
// Check if this instruction has a dependence on the critical path that
|
|
|
|
// is an anti-dependence that we may be able to break. If it is, set
|
|
|
|
// AntiDepReg to the non-zero register associated with the anti-dependence.
|
2008-12-16 19:27:52 +00:00
|
|
|
//
|
2009-10-21 01:44:44 +00:00
|
|
|
// We limit our attention to the critical path as a heuristic to avoid
|
2008-12-16 19:27:52 +00:00
|
|
|
// breaking anti-dependence edges that aren't going to significantly
|
|
|
|
// impact the overall schedule. There are a limited number of registers
|
|
|
|
// and we want to save them for the important edges.
|
|
|
|
//
|
|
|
|
// TODO: Instructions with multiple defs could have multiple
|
|
|
|
// anti-dependencies. The current code here only knows how to break one
|
|
|
|
// edge per instruction. Note that we'd have to be able to break all of
|
|
|
|
// the anti-dependencies in an instruction in order to be effective.
|
2009-10-21 01:44:44 +00:00
|
|
|
unsigned AntiDepReg = 0;
|
|
|
|
if (MI == CriticalPathMI) {
|
|
|
|
if (SDep *Edge = CriticalPathStep(CriticalPathSU)) {
|
2008-12-16 19:27:52 +00:00
|
|
|
SUnit *NextSU = Edge->getSUnit();
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
// Only consider anti-dependence edges.
|
|
|
|
if (Edge->getKind() == SDep::Anti) {
|
2008-12-16 19:27:52 +00:00
|
|
|
AntiDepReg = Edge->getReg();
|
|
|
|
assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
|
2009-10-21 01:44:44 +00:00
|
|
|
if (!AllocatableSet.test(AntiDepReg))
|
2009-10-01 08:26:23 +00:00
|
|
|
// Don't break anti-dependencies on non-allocatable registers.
|
|
|
|
AntiDepReg = 0;
|
2009-10-21 01:44:44 +00:00
|
|
|
else if (KeepRegs.count(AntiDepReg))
|
|
|
|
// Don't break anti-dependencies if an use down below requires
|
|
|
|
// this exact register.
|
|
|
|
AntiDepReg = 0;
|
|
|
|
else {
|
|
|
|
// If the SUnit has other dependencies on the SUnit that it
|
|
|
|
// anti-depends on, don't bother breaking the anti-dependency
|
|
|
|
// since those edges would prevent such units from being
|
|
|
|
// scheduled past each other regardless.
|
|
|
|
//
|
|
|
|
// Also, if there are dependencies on other SUnits with the
|
|
|
|
// same register as the anti-dependency, don't attempt to
|
|
|
|
// break it.
|
|
|
|
for (SUnit::pred_iterator P = CriticalPathSU->Preds.begin(),
|
|
|
|
PE = CriticalPathSU->Preds.end(); P != PE; ++P)
|
|
|
|
if (P->getSUnit() == NextSU ?
|
2008-12-16 19:27:52 +00:00
|
|
|
(P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
|
|
|
|
(P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
|
2009-10-21 01:44:44 +00:00
|
|
|
AntiDepReg = 0;
|
|
|
|
break;
|
2008-12-16 19:27:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-21 01:44:44 +00:00
|
|
|
CriticalPathSU = NextSU;
|
|
|
|
CriticalPathMI = CriticalPathSU->getInstr();
|
2008-12-16 19:27:52 +00:00
|
|
|
} else {
|
|
|
|
// We've reached the end of the critical path.
|
|
|
|
CriticalPathSU = 0;
|
|
|
|
CriticalPathMI = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-11-25 00:52:40 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
PrescanInstruction(MI);
|
|
|
|
|
|
|
|
if (MI->getDesc().hasExtraDefRegAllocReq())
|
|
|
|
// If this instruction's defs have special allocation requirement, don't
|
|
|
|
// break this anti-dependency.
|
2009-10-01 08:26:23 +00:00
|
|
|
AntiDepReg = 0;
|
2009-10-21 01:44:44 +00:00
|
|
|
else if (AntiDepReg) {
|
|
|
|
// If this instruction has a use of AntiDepReg, breaking it
|
|
|
|
// is invalid.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg()) continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (Reg == 0) continue;
|
|
|
|
if (MO.isUse() && AntiDepReg == Reg) {
|
|
|
|
AntiDepReg = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-25 00:52:40 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
// Determine AntiDepReg's register class, if it is live and is
|
|
|
|
// consistently used within a single class.
|
|
|
|
const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
|
|
|
|
assert((AntiDepReg == 0 || RC != NULL) &&
|
|
|
|
"Register should be live if it's causing an anti-dependence!");
|
|
|
|
if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
|
|
|
|
AntiDepReg = 0;
|
2008-11-25 00:52:40 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
// Look for a suitable register to use to break the anti-depenence.
|
|
|
|
//
|
|
|
|
// TODO: Instead of picking the first free register, consider which might
|
|
|
|
// be the best.
|
2008-11-25 00:52:40 +00:00
|
|
|
if (AntiDepReg != 0) {
|
2009-10-21 01:44:44 +00:00
|
|
|
if (unsigned NewReg = findSuitableFreeRegister(AntiDepReg,
|
|
|
|
LastNewReg[AntiDepReg],
|
|
|
|
RC)) {
|
|
|
|
DEBUG(errs() << "Breaking anti-dependence edge on "
|
2009-08-12 01:33:27 +00:00
|
|
|
<< TRI->getName(AntiDepReg)
|
|
|
|
<< " with " << RegRefs.count(AntiDepReg) << " references"
|
|
|
|
<< " using " << TRI->getName(NewReg) << "!\n");
|
|
|
|
|
|
|
|
// Update the references to the old register to refer to the new
|
|
|
|
// register.
|
2009-10-21 01:44:44 +00:00
|
|
|
std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
|
|
|
|
std::multimap<unsigned, MachineOperand *>::iterator>
|
2009-08-12 01:33:27 +00:00
|
|
|
Range = RegRefs.equal_range(AntiDepReg);
|
2009-10-21 01:44:44 +00:00
|
|
|
for (std::multimap<unsigned, MachineOperand *>::iterator
|
2009-08-12 01:33:27 +00:00
|
|
|
Q = Range.first, QE = Range.second; Q != QE; ++Q)
|
2009-10-21 01:44:44 +00:00
|
|
|
Q->second->setReg(NewReg);
|
2009-08-12 01:33:27 +00:00
|
|
|
|
|
|
|
// We just went back in time and modified history; the
|
2009-10-21 01:44:44 +00:00
|
|
|
// liveness information for the anti-depenence reg is now
|
2009-08-12 01:33:27 +00:00
|
|
|
// inconsistent. Set the state as if it were dead.
|
2009-10-21 01:44:44 +00:00
|
|
|
Classes[NewReg] = Classes[AntiDepReg];
|
2009-08-12 01:33:27 +00:00
|
|
|
DefIndices[NewReg] = DefIndices[AntiDepReg];
|
|
|
|
KillIndices[NewReg] = KillIndices[AntiDepReg];
|
2009-10-21 01:44:44 +00:00
|
|
|
assert(((KillIndices[NewReg] == ~0u) !=
|
|
|
|
(DefIndices[NewReg] == ~0u)) &&
|
|
|
|
"Kill and Def maps aren't consistent for NewReg!");
|
2009-08-12 01:33:27 +00:00
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
Classes[AntiDepReg] = 0;
|
2009-08-12 01:33:27 +00:00
|
|
|
DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
|
|
|
|
KillIndices[AntiDepReg] = ~0u;
|
|
|
|
assert(((KillIndices[AntiDepReg] == ~0u) !=
|
|
|
|
(DefIndices[AntiDepReg] == ~0u)) &&
|
|
|
|
"Kill and Def maps aren't consistent for AntiDepReg!");
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
RegRefs.erase(AntiDepReg);
|
2009-08-12 01:33:27 +00:00
|
|
|
Changed = true;
|
2009-10-21 01:44:44 +00:00
|
|
|
LastNewReg[AntiDepReg] = NewReg;
|
2008-11-25 00:52:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
ScanInstruction(MI, Count);
|
2008-11-25 00:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2009-09-03 22:15:25 +00:00
|
|
|
/// StartBlockForKills - Initialize register live-range state for updating kills
|
|
|
|
///
|
|
|
|
void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
|
|
|
|
// Initialize the indices to indicate that no registers are live.
|
|
|
|
std::fill(KillIndices, array_endof(KillIndices), ~0u);
|
|
|
|
|
|
|
|
// Determine the live-out physregs for this block.
|
|
|
|
if (!BB->empty() && BB->back().getDesc().isReturn()) {
|
|
|
|
// In a return block, examine the function live-out regs.
|
|
|
|
for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
|
|
|
|
E = MRI.liveout_end(); I != E; ++I) {
|
|
|
|
unsigned Reg = *I;
|
|
|
|
KillIndices[Reg] = BB->size();
|
|
|
|
// Repeat, for all subregs.
|
|
|
|
for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
|
|
|
|
*Subreg; ++Subreg) {
|
|
|
|
KillIndices[*Subreg] = BB->size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// In a non-return block, examine the live-in regs of all successors.
|
|
|
|
for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
|
|
|
|
SE = BB->succ_end(); SI != SE; ++SI) {
|
|
|
|
for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
|
|
|
|
E = (*SI)->livein_end(); I != E; ++I) {
|
|
|
|
unsigned Reg = *I;
|
|
|
|
KillIndices[Reg] = BB->size();
|
|
|
|
// Repeat, for all subregs.
|
|
|
|
for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
|
|
|
|
*Subreg; ++Subreg) {
|
|
|
|
KillIndices[*Subreg] = BB->size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-23 16:35:25 +00:00
|
|
|
bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
|
|
|
|
MachineOperand &MO) {
|
|
|
|
// Setting kill flag...
|
|
|
|
if (!MO.isKill()) {
|
|
|
|
MO.setIsKill(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If MO itself is live, clear the kill flag...
|
|
|
|
if (KillIndices[MO.getReg()] != ~0u) {
|
|
|
|
MO.setIsKill(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any subreg of MO is live, then create an imp-def for that
|
|
|
|
// subreg and keep MO marked as killed.
|
2009-10-02 15:59:52 +00:00
|
|
|
MO.setIsKill(false);
|
2009-09-23 16:35:25 +00:00
|
|
|
bool AllDead = true;
|
|
|
|
const unsigned SuperReg = MO.getReg();
|
|
|
|
for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg);
|
|
|
|
*Subreg; ++Subreg) {
|
|
|
|
if (KillIndices[*Subreg] != ~0u) {
|
|
|
|
MI->addOperand(MachineOperand::CreateReg(*Subreg,
|
|
|
|
true /*IsDef*/,
|
|
|
|
true /*IsImp*/,
|
|
|
|
false /*IsKill*/,
|
|
|
|
false /*IsDead*/));
|
|
|
|
AllDead = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-21 01:44:44 +00:00
|
|
|
if(AllDead)
|
2009-10-02 15:59:52 +00:00
|
|
|
MO.setIsKill(true);
|
2009-09-23 16:35:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-25 17:03:05 +00:00
|
|
|
/// FixupKills - Fix the register kill flags, they may have been made
|
|
|
|
/// incorrect by instruction reordering.
|
|
|
|
///
|
|
|
|
void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
|
|
|
|
DEBUG(errs() << "Fixup kills for BB ID#" << MBB->getNumber() << '\n');
|
|
|
|
|
|
|
|
std::set<unsigned> killedRegs;
|
|
|
|
BitVector ReservedRegs = TRI->getReservedRegs(MF);
|
2009-09-03 22:15:25 +00:00
|
|
|
|
|
|
|
StartBlockForKills(MBB);
|
2009-08-29 00:11:13 +00:00
|
|
|
|
|
|
|
// Examine block from end to start...
|
2009-08-25 17:03:05 +00:00
|
|
|
unsigned Count = MBB->size();
|
|
|
|
for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
|
|
|
|
I != E; --Count) {
|
|
|
|
MachineInstr *MI = --I;
|
|
|
|
|
2009-08-29 00:11:13 +00:00
|
|
|
// Update liveness. Registers that are defed but not used in this
|
|
|
|
// instruction are now dead. Mark register and all subregs as they
|
|
|
|
// are completely defined.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg()) continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (Reg == 0) continue;
|
|
|
|
if (!MO.isDef()) continue;
|
|
|
|
// Ignore two-addr defs.
|
|
|
|
if (MI->isRegTiedToUseOperand(i)) continue;
|
|
|
|
|
|
|
|
KillIndices[Reg] = ~0u;
|
|
|
|
|
|
|
|
// Repeat for all subregs.
|
|
|
|
for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
|
|
|
|
*Subreg; ++Subreg) {
|
|
|
|
KillIndices[*Subreg] = ~0u;
|
|
|
|
}
|
|
|
|
}
|
2009-08-25 17:03:05 +00:00
|
|
|
|
2009-09-23 16:35:25 +00:00
|
|
|
// Examine all used registers and set/clear kill flag. When a
|
|
|
|
// register is used multiple times we only set the kill flag on
|
|
|
|
// the first use.
|
2009-08-25 17:03:05 +00:00
|
|
|
killedRegs.clear();
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg() || !MO.isUse()) continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
|
|
|
|
|
2009-08-29 00:11:13 +00:00
|
|
|
bool kill = false;
|
|
|
|
if (killedRegs.find(Reg) == killedRegs.end()) {
|
|
|
|
kill = true;
|
|
|
|
// A register is not killed if any subregs are live...
|
|
|
|
for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
|
|
|
|
*Subreg; ++Subreg) {
|
|
|
|
if (KillIndices[*Subreg] != ~0u) {
|
|
|
|
kill = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If subreg is not live, then register is killed if it became
|
|
|
|
// live in this instruction
|
|
|
|
if (kill)
|
|
|
|
kill = (KillIndices[Reg] == ~0u);
|
|
|
|
}
|
|
|
|
|
2009-08-25 17:03:05 +00:00
|
|
|
if (MO.isKill() != kill) {
|
2009-09-23 16:35:25 +00:00
|
|
|
bool removed = ToggleKillFlag(MI, MO);
|
|
|
|
if (removed) {
|
|
|
|
DEBUG(errs() << "Fixed <removed> in ");
|
|
|
|
} else {
|
|
|
|
DEBUG(errs() << "Fixed " << MO << " in ");
|
|
|
|
}
|
2009-08-25 17:03:05 +00:00
|
|
|
DEBUG(MI->dump());
|
|
|
|
}
|
2009-08-29 00:11:13 +00:00
|
|
|
|
2009-08-25 17:03:05 +00:00
|
|
|
killedRegs.insert(Reg);
|
|
|
|
}
|
2009-08-29 00:11:13 +00:00
|
|
|
|
2009-08-31 20:47:02 +00:00
|
|
|
// Mark any used register (that is not using undef) and subregs as
|
|
|
|
// now live...
|
2009-08-29 00:11:13 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
2009-08-31 20:47:02 +00:00
|
|
|
if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
|
2009-08-29 00:11:13 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
|
|
|
|
|
|
|
|
KillIndices[Reg] = Count;
|
|
|
|
|
|
|
|
for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
|
|
|
|
*Subreg; ++Subreg) {
|
|
|
|
KillIndices[*Subreg] = Count;
|
|
|
|
}
|
|
|
|
}
|
2009-08-25 17:03:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top-Down Scheduling
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
|
|
|
|
/// the PendingQueue if the count reaches zero. Also update its cycle bound.
|
2008-12-09 22:54:47 +00:00
|
|
|
void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
|
|
|
|
SUnit *SuccSU = SuccEdge->getSUnit();
|
2009-09-30 20:15:38 +00:00
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
#ifndef NDEBUG
|
2009-09-30 20:15:38 +00:00
|
|
|
if (SuccSU->NumPredsLeft == 0) {
|
2009-08-23 07:19:13 +00:00
|
|
|
errs() << "*** Scheduling failed! ***\n";
|
2008-11-19 23:18:57 +00:00
|
|
|
SuccSU->dump(this);
|
2009-08-23 07:19:13 +00:00
|
|
|
errs() << " has been released too many times!\n";
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable(0);
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
2009-09-30 20:15:38 +00:00
|
|
|
--SuccSU->NumPredsLeft;
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
// Compute how many cycles it will be before this actually becomes
|
|
|
|
// available. This is the max of the start time of all predecessors plus
|
|
|
|
// their latencies.
|
2008-12-16 03:25:46 +00:00
|
|
|
SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
// If all the node's predecessors are scheduled, this node is ready
|
|
|
|
// to be scheduled. Ignore the special ExitSU node.
|
|
|
|
if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
|
2008-11-19 23:18:57 +00:00
|
|
|
PendingQueue.push_back(SuccSU);
|
2009-02-10 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
|
|
|
|
void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
|
|
|
|
for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
|
|
|
|
I != E; ++I)
|
|
|
|
ReleaseSucc(SU, &*I);
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
|
|
|
|
/// count of its successors. If a successor pending count is zero, add it to
|
|
|
|
/// the Available queue.
|
|
|
|
void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
|
2009-08-11 01:44:26 +00:00
|
|
|
DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: ");
|
2008-11-19 23:18:57 +00:00
|
|
|
DEBUG(SU->dump(this));
|
|
|
|
|
|
|
|
Sequence.push_back(SU);
|
2008-12-16 03:25:46 +00:00
|
|
|
assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
|
|
|
|
SU->setDepthToAtLeast(CurCycle);
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
ReleaseSuccessors(SU);
|
2008-11-19 23:18:57 +00:00
|
|
|
SU->isScheduled = true;
|
|
|
|
AvailableQueue.ScheduledNode(SU);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ListScheduleTopDown - The main loop of list scheduling for top-down
|
|
|
|
/// schedulers.
|
|
|
|
void SchedulePostRATDList::ListScheduleTopDown() {
|
|
|
|
unsigned CurCycle = 0;
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
// Release any successors of the special Entry node.
|
|
|
|
ReleaseSuccessors(&EntrySU);
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
// All leaves to Available queue.
|
|
|
|
for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
|
|
|
|
// It is available if it has no predecessors.
|
|
|
|
if (SUnits[i].Preds.empty()) {
|
|
|
|
AvailableQueue.push(&SUnits[i]);
|
|
|
|
SUnits[i].isAvailable = true;
|
|
|
|
}
|
|
|
|
}
|
2009-02-10 23:27:53 +00:00
|
|
|
|
2009-08-12 21:47:46 +00:00
|
|
|
// In any cycle where we can't schedule any instructions, we must
|
|
|
|
// stall or emit a noop, depending on the target.
|
2009-09-06 12:10:17 +00:00
|
|
|
bool CycleHasInsts = false;
|
2009-08-12 21:47:46 +00:00
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
// While Available queue is not empty, grab the node with the highest
|
|
|
|
// priority. If it is not ready put it back. Schedule the node.
|
2009-01-16 01:33:36 +00:00
|
|
|
std::vector<SUnit*> NotReady;
|
2008-11-19 23:18:57 +00:00
|
|
|
Sequence.reserve(SUnits.size());
|
|
|
|
while (!AvailableQueue.empty() || !PendingQueue.empty()) {
|
|
|
|
// Check to see if any of the pending instructions are ready to issue. If
|
|
|
|
// so, add them to the available queue.
|
2008-12-16 03:25:46 +00:00
|
|
|
unsigned MinDepth = ~0u;
|
2008-11-19 23:18:57 +00:00
|
|
|
for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
|
2008-12-16 03:25:46 +00:00
|
|
|
if (PendingQueue[i]->getDepth() <= CurCycle) {
|
2008-11-19 23:18:57 +00:00
|
|
|
AvailableQueue.push(PendingQueue[i]);
|
|
|
|
PendingQueue[i]->isAvailable = true;
|
|
|
|
PendingQueue[i] = PendingQueue.back();
|
|
|
|
PendingQueue.pop_back();
|
|
|
|
--i; --e;
|
2008-12-16 03:25:46 +00:00
|
|
|
} else if (PendingQueue[i]->getDepth() < MinDepth)
|
|
|
|
MinDepth = PendingQueue[i]->getDepth();
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
2009-08-11 17:35:23 +00:00
|
|
|
|
2009-08-11 17:56:42 +00:00
|
|
|
DEBUG(errs() << "\n*** Examining Available\n";
|
|
|
|
LatencyPriorityQueue q = AvailableQueue;
|
|
|
|
while (!q.empty()) {
|
|
|
|
SUnit *su = q.pop();
|
|
|
|
errs() << "Height " << su->getHeight() << ": ";
|
|
|
|
su->dump(this);
|
|
|
|
});
|
2009-08-11 17:35:23 +00:00
|
|
|
|
2009-01-16 01:33:36 +00:00
|
|
|
SUnit *FoundSUnit = 0;
|
|
|
|
|
|
|
|
bool HasNoopHazards = false;
|
|
|
|
while (!AvailableQueue.empty()) {
|
|
|
|
SUnit *CurSUnit = AvailableQueue.pop();
|
|
|
|
|
|
|
|
ScheduleHazardRecognizer::HazardType HT =
|
|
|
|
HazardRec->getHazardType(CurSUnit);
|
|
|
|
if (HT == ScheduleHazardRecognizer::NoHazard) {
|
|
|
|
FoundSUnit = CurSUnit;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember if this is a noop hazard.
|
|
|
|
HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
|
|
|
|
|
|
|
|
NotReady.push_back(CurSUnit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the nodes that aren't ready back onto the available list.
|
|
|
|
if (!NotReady.empty()) {
|
|
|
|
AvailableQueue.push_all(NotReady);
|
|
|
|
NotReady.clear();
|
|
|
|
}
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
// If we found a node to schedule, do it now.
|
|
|
|
if (FoundSUnit) {
|
|
|
|
ScheduleNodeTopDown(FoundSUnit, CurCycle);
|
2009-01-16 01:33:36 +00:00
|
|
|
HazardRec->EmitInstruction(FoundSUnit);
|
2009-09-06 12:10:17 +00:00
|
|
|
CycleHasInsts = true;
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2009-08-10 15:55:25 +00:00
|
|
|
// If we are using the target-specific hazards, then don't
|
|
|
|
// advance the cycle time just because we schedule a node. If
|
|
|
|
// the target allows it we can schedule multiple nodes in the
|
|
|
|
// same cycle.
|
|
|
|
if (!EnablePostRAHazardAvoidance) {
|
|
|
|
if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops!
|
|
|
|
++CurCycle;
|
|
|
|
}
|
2009-01-16 01:33:36 +00:00
|
|
|
} else {
|
2009-09-06 12:10:17 +00:00
|
|
|
if (CycleHasInsts) {
|
2009-08-12 21:47:46 +00:00
|
|
|
DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n');
|
|
|
|
HazardRec->AdvanceCycle();
|
|
|
|
} else if (!HasNoopHazards) {
|
|
|
|
// Otherwise, we have a pipeline stall, but no other problem,
|
|
|
|
// just advance the current cycle and try again.
|
|
|
|
DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n');
|
|
|
|
HazardRec->AdvanceCycle();
|
|
|
|
++NumStalls;
|
|
|
|
} else {
|
|
|
|
// Otherwise, we have no instructions to issue and we have instructions
|
|
|
|
// that will fault if we don't do this right. This is the case for
|
|
|
|
// processors without pipeline interlocks and other cases.
|
|
|
|
DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n');
|
|
|
|
HazardRec->EmitNoop();
|
|
|
|
Sequence.push_back(0); // NULL here means noop
|
|
|
|
++NumNoops;
|
|
|
|
}
|
|
|
|
|
2009-01-16 01:33:36 +00:00
|
|
|
++CurCycle;
|
2009-09-06 12:10:17 +00:00
|
|
|
CycleHasInsts = false;
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2008-11-20 01:26:25 +00:00
|
|
|
VerifySchedule(/*isBottomUp=*/false);
|
2008-11-19 23:18:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
2007-07-13 17:13:54 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Public Constructor Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-10-16 21:06:15 +00:00
|
|
|
FunctionPass *llvm::createPostRAScheduler(CodeGenOpt::Level OptLevel) {
|
|
|
|
return new PostRAScheduler(OptLevel);
|
2007-07-13 17:13:54 +00:00
|
|
|
}
|