Do a scheduling pass ignoring anti-dependencies to identify candidate registers that should be renamed.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85939 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Goodwin 2009-11-03 20:57:50 +00:00
parent abf67ef548
commit 4de099d8ca
11 changed files with 216 additions and 83 deletions

View File

@ -40,9 +40,18 @@ namespace llvm {
/// mobility. /// mobility.
std::vector<unsigned> NumNodesSolelyBlocking; std::vector<unsigned> NumNodesSolelyBlocking;
/// IgnoreAntiDep - Ignore anti-dependencies
bool IgnoreAntiDep;
/// Queue - The queue.
PriorityQueue<SUnit*, std::vector<SUnit*>, latency_sort> Queue; PriorityQueue<SUnit*, std::vector<SUnit*>, latency_sort> Queue;
public: public:
LatencyPriorityQueue() : Queue(latency_sort(this)) { LatencyPriorityQueue() : IgnoreAntiDep(false), Queue(latency_sort(this)) {
}
void setIgnoreAntiDep(bool ignore) {
IgnoreAntiDep = ignore;
} }
void initNodes(std::vector<SUnit> &sunits) { void initNodes(std::vector<SUnit> &sunits) {
@ -63,7 +72,7 @@ public:
unsigned getLatency(unsigned NodeNum) const { unsigned getLatency(unsigned NodeNum) const {
assert(NodeNum < (*SUnits).size()); assert(NodeNum < (*SUnits).size());
return (*SUnits)[NodeNum].getHeight(); return (*SUnits)[NodeNum].getHeight(IgnoreAntiDep);
} }
unsigned getNumSolelyBlockNodes(unsigned NodeNum) const { unsigned getNumSolelyBlockNodes(unsigned NodeNum) const {

View File

@ -340,28 +340,34 @@ namespace llvm {
void removePred(const SDep &D); void removePred(const SDep &D);
/// getDepth - Return the depth of this node, which is the length of the /// getDepth - Return the depth of this node, which is the length of the
/// maximum path up to any node with has no predecessors. /// maximum path up to any node with has no predecessors. If IgnoreAntiDep
unsigned getDepth() const { /// is true, ignore anti-dependence edges.
if (!isDepthCurrent) const_cast<SUnit *>(this)->ComputeDepth(); unsigned getDepth(bool IgnoreAntiDep=false) const {
if (!isDepthCurrent)
const_cast<SUnit *>(this)->ComputeDepth(IgnoreAntiDep);
return Depth; return Depth;
} }
/// getHeight - Return the height of this node, which is the length of the /// getHeight - Return the height of this node, which is the length of the
/// maximum path down to any node with has no successors. /// maximum path down to any node with has no successors. If IgnoreAntiDep
unsigned getHeight() const { /// is true, ignore anti-dependence edges.
if (!isHeightCurrent) const_cast<SUnit *>(this)->ComputeHeight(); unsigned getHeight(bool IgnoreAntiDep=false) const {
if (!isHeightCurrent)
const_cast<SUnit *>(this)->ComputeHeight(IgnoreAntiDep);
return Height; return Height;
} }
/// setDepthToAtLeast - If NewDepth is greater than this node's depth /// setDepthToAtLeast - If NewDepth is greater than this node's
/// value, set it to be the new depth value. This also recursively /// depth value, set it to be the new depth value. This also
/// marks successor nodes dirty. /// recursively marks successor nodes dirty. If IgnoreAntiDep is
void setDepthToAtLeast(unsigned NewDepth); /// true, ignore anti-dependence edges.
void setDepthToAtLeast(unsigned NewDepth, bool IgnoreAntiDep=false);
/// setDepthToAtLeast - If NewDepth is greater than this node's depth /// setDepthToAtLeast - If NewDepth is greater than this node's
/// value, set it to be the new height value. This also recursively /// depth value, set it to be the new height value. This also
/// marks predecessor nodes dirty. /// recursively marks predecessor nodes dirty. If IgnoreAntiDep is
void setHeightToAtLeast(unsigned NewHeight); /// true, ignore anti-dependence edges.
void setHeightToAtLeast(unsigned NewHeight, bool IgnoreAntiDep=false);
/// setDepthDirty - Set a flag in this node to indicate that its /// setDepthDirty - Set a flag in this node to indicate that its
/// stored Depth value will require recomputation the next time /// stored Depth value will require recomputation the next time
@ -394,8 +400,8 @@ namespace llvm {
void print(raw_ostream &O, const ScheduleDAG *G) const; void print(raw_ostream &O, const ScheduleDAG *G) const;
private: private:
void ComputeDepth(); void ComputeDepth(bool IgnoreAntiDep);
void ComputeHeight(); void ComputeHeight(bool IgnoreAntiDep);
}; };
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//

View File

@ -14,7 +14,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#define DEBUG_TYPE "aggressive-antidep" #define DEBUG_TYPE "post-RA-sched"
#include "AggressiveAntiDepBreaker.h" #include "AggressiveAntiDepBreaker.h"
#include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFrameInfo.h"
@ -31,7 +31,7 @@ using namespace llvm;
static cl::opt<int> static cl::opt<int>
AntiDepTrials("agg-antidep-trials", AntiDepTrials("agg-antidep-trials",
cl::desc("Maximum number of anti-dependency breaking passes"), cl::desc("Maximum number of anti-dependency breaking passes"),
cl::init(2), cl::Hidden); cl::init(1), cl::Hidden);
AggressiveAntiDepState::AggressiveAntiDepState(MachineBasicBlock *BB) : AggressiveAntiDepState::AggressiveAntiDepState(MachineBasicBlock *BB) :
GroupNodes(TargetRegisterInfo::FirstVirtualRegister, 0) { GroupNodes(TargetRegisterInfo::FirstVirtualRegister, 0) {
@ -265,18 +265,24 @@ void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
} }
/// AntiDepPathStep - Return SUnit that SU has an anti-dependence on. /// AntiDepPathStep - Return SUnit that SU has an anti-dependence on.
static void AntiDepPathStep(SUnit *SU, std::vector<SDep*>& Edges) { static void AntiDepPathStep(SUnit *SU, AntiDepBreaker::AntiDepRegVector& Regs,
SmallSet<unsigned, 8> Dups; std::vector<SDep*>& Edges) {
AntiDepBreaker::AntiDepRegSet RegSet;
for (unsigned i = 0, e = Regs.size(); i < e; ++i)
RegSet.insert(Regs[i]);
for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
P != PE; ++P) { P != PE; ++P) {
if (P->getKind() == SDep::Anti) { if (P->getKind() == SDep::Anti) {
unsigned Reg = P->getReg(); unsigned Reg = P->getReg();
if (Dups.count(Reg) == 0) { if (RegSet.count(Reg) != 0) {
Edges.push_back(&*P); Edges.push_back(&*P);
Dups.insert(Reg); RegSet.erase(Reg);
} }
} }
} }
assert(RegSet.empty() && "Expected all antidep registers to be found");
} }
void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx, void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
@ -593,6 +599,7 @@ bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
/// ///
unsigned AggressiveAntiDepBreaker::BreakAntiDependencies( unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
std::vector<SUnit>& SUnits, std::vector<SUnit>& SUnits,
CandidateMap& Candidates,
MachineBasicBlock::iterator& Begin, MachineBasicBlock::iterator& Begin,
MachineBasicBlock::iterator& End, MachineBasicBlock::iterator& End,
unsigned InsertPosIndex) { unsigned InsertPosIndex) {
@ -601,9 +608,15 @@ unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
RegRefs = State->GetRegRefs(); RegRefs = State->GetRegRefs();
// Nothing to do if no candidates.
if (Candidates.empty()) {
DEBUG(errs() << "\n===== No anti-dependency candidates\n");
return 0;
}
// The code below assumes that there is at least one instruction, // The code below assumes that there is at least one instruction,
// so just duck out immediately if the block is empty. // so just duck out immediately if the block is empty.
if (SUnits.empty()) return false; if (SUnits.empty()) return 0;
// Manage saved state to enable multiple passes... // Manage saved state to enable multiple passes...
if (AntiDepTrials > 1) { if (AntiDepTrials > 1) {
@ -618,7 +631,8 @@ unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
// ...need a map from MI to SUnit. // ...need a map from MI to SUnit.
std::map<MachineInstr *, SUnit *> MISUnitMap; std::map<MachineInstr *, SUnit *> MISUnitMap;
DEBUG(errs() << "Breaking all anti-dependencies\n"); DEBUG(errs() << "\n===== Attempting to break " << Candidates.size() <<
" anti-dependencies\n");
for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
SUnit *SU = &SUnits[i]; SUnit *SU = &SUnits[i];
MISUnitMap.insert(std::pair<MachineInstr *, SUnit *>(SU->getInstr(), SU)); MISUnitMap.insert(std::pair<MachineInstr *, SUnit *>(SU->getInstr(), SU));
@ -655,8 +669,10 @@ unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
std::vector<SDep*> Edges; std::vector<SDep*> Edges;
SUnit *PathSU = MISUnitMap[MI]; SUnit *PathSU = MISUnitMap[MI];
if (PathSU) AntiDepBreaker::CandidateMap::iterator
AntiDepPathStep(PathSU, Edges); citer = Candidates.find(PathSU);
if (citer != Candidates.end())
AntiDepPathStep(PathSU, citer->second, Edges);
// Ignore KILL instructions (they form a group in ScanInstruction // Ignore KILL instructions (they form a group in ScanInstruction
// but don't cause any anti-dependence breaking themselves) // but don't cause any anti-dependence breaking themselves)

View File

@ -131,6 +131,9 @@ namespace llvm {
/// dependencies may be exposed, so multiple passes are required. /// dependencies may be exposed, so multiple passes are required.
unsigned GetMaxTrials(); unsigned GetMaxTrials();
/// NeedCandidates - Candidates required.
bool NeedCandidates() { return true; }
/// Start - Initialize anti-dep breaking for a new basic block. /// Start - Initialize anti-dep breaking for a new basic block.
void StartBlock(MachineBasicBlock *BB); void StartBlock(MachineBasicBlock *BB);
@ -138,6 +141,7 @@ namespace llvm {
/// of the ScheduleDAG and break them by renaming registers. /// of the ScheduleDAG and break them by renaming registers.
/// ///
unsigned BreakAntiDependencies(std::vector<SUnit>& SUnits, unsigned BreakAntiDependencies(std::vector<SUnit>& SUnits,
CandidateMap& Candidates,
MachineBasicBlock::iterator& Begin, MachineBasicBlock::iterator& Begin,
MachineBasicBlock::iterator& End, MachineBasicBlock::iterator& End,
unsigned InsertPosIndex); unsigned InsertPosIndex);

View File

@ -21,6 +21,8 @@
#include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/ScheduleDAG.h" #include "llvm/CodeGen/ScheduleDAG.h"
#include "llvm/Target/TargetRegisterInfo.h" #include "llvm/Target/TargetRegisterInfo.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
namespace llvm { namespace llvm {
@ -29,12 +31,20 @@ namespace llvm {
/// anti-dependencies. /// anti-dependencies.
class AntiDepBreaker { class AntiDepBreaker {
public: public:
typedef SmallSet<unsigned, 4> AntiDepRegSet;
typedef SmallVector<unsigned, 4> AntiDepRegVector;
typedef std::map<SUnit *, AntiDepRegVector> CandidateMap;
virtual ~AntiDepBreaker(); virtual ~AntiDepBreaker();
/// GetMaxTrials - Return the maximum number of anti-dependence /// GetMaxTrials - Return the maximum number of anti-dependence
/// breaking attempts that will be made for a block. /// breaking attempts that will be made for a block.
virtual unsigned GetMaxTrials() =0; virtual unsigned GetMaxTrials() =0;
/// NeedCandidates - Return true if the schedule must provide
/// candidates with BreakAntiDependencies().
virtual bool NeedCandidates() =0;
/// Start - Initialize anti-dep breaking for a new basic block. /// Start - Initialize anti-dep breaking for a new basic block.
virtual void StartBlock(MachineBasicBlock *BB) =0; virtual void StartBlock(MachineBasicBlock *BB) =0;
@ -43,6 +53,7 @@ public:
/// the number of anti-dependencies broken. /// the number of anti-dependencies broken.
/// ///
virtual unsigned BreakAntiDependencies(std::vector<SUnit>& SUnits, virtual unsigned BreakAntiDependencies(std::vector<SUnit>& SUnits,
CandidateMap& Candidates,
MachineBasicBlock::iterator& Begin, MachineBasicBlock::iterator& Begin,
MachineBasicBlock::iterator& End, MachineBasicBlock::iterator& End,
unsigned InsertPosIndex) =0; unsigned InsertPosIndex) =0;

View File

@ -13,7 +13,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#define DEBUG_TYPE "critical-antidep" #define DEBUG_TYPE "post-RA-sched"
#include "CriticalAntiDepBreaker.h" #include "CriticalAntiDepBreaker.h"
#include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFrameInfo.h"
@ -316,6 +316,7 @@ CriticalAntiDepBreaker::findSuitableFreeRegister(unsigned AntiDepReg,
unsigned CriticalAntiDepBreaker:: unsigned CriticalAntiDepBreaker::
BreakAntiDependencies(std::vector<SUnit>& SUnits, BreakAntiDependencies(std::vector<SUnit>& SUnits,
CandidateMap& Candidates,
MachineBasicBlock::iterator& Begin, MachineBasicBlock::iterator& Begin,
MachineBasicBlock::iterator& End, MachineBasicBlock::iterator& End,
unsigned InsertPosIndex) { unsigned InsertPosIndex) {

View File

@ -68,6 +68,9 @@ namespace llvm {
/// only a single pass /// only a single pass
unsigned GetMaxTrials() { return 1; } unsigned GetMaxTrials() { return 1; }
/// NeedCandidates - Candidates not needed.
bool NeedCandidates() { return false; }
/// Start - Initialize anti-dep breaking for a new basic block. /// Start - Initialize anti-dep breaking for a new basic block.
void StartBlock(MachineBasicBlock *BB); void StartBlock(MachineBasicBlock *BB);
@ -75,6 +78,7 @@ namespace llvm {
/// of the ScheduleDAG and break them by renaming registers. /// of the ScheduleDAG and break them by renaming registers.
/// ///
unsigned BreakAntiDependencies(std::vector<SUnit>& SUnits, unsigned BreakAntiDependencies(std::vector<SUnit>& SUnits,
CandidateMap& Candidates,
MachineBasicBlock::iterator& Begin, MachineBasicBlock::iterator& Begin,
MachineBasicBlock::iterator& End, MachineBasicBlock::iterator& End,
unsigned InsertPosIndex); unsigned InsertPosIndex);

View File

@ -12,7 +12,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#define DEBUG_TYPE "exact-hazards" #define DEBUG_TYPE "post-RA-sched"
#include "ExactHazardRecognizer.h" #include "ExactHazardRecognizer.h"
#include "llvm/CodeGen/ScheduleHazardRecognizer.h" #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"

View File

@ -55,6 +55,7 @@ SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) {
SUnit *OnlyAvailablePred = 0; SUnit *OnlyAvailablePred = 0;
for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
I != E; ++I) { I != E; ++I) {
if (IgnoreAntiDep && (I->getKind() == SDep::Anti)) continue;
SUnit &Pred = *I->getSUnit(); SUnit &Pred = *I->getSUnit();
if (!Pred.isScheduled) { if (!Pred.isScheduled) {
// We found an available, but not scheduled, predecessor. If it's the // We found an available, but not scheduled, predecessor. If it's the
@ -73,9 +74,11 @@ void LatencyPriorityQueue::push_impl(SUnit *SU) {
// this node is the sole unscheduled node for. // this node is the sole unscheduled node for.
unsigned NumNodesBlocking = 0; unsigned NumNodesBlocking = 0;
for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
I != E; ++I) I != E; ++I) {
if (IgnoreAntiDep && (I->getKind() == SDep::Anti)) continue;
if (getSingleUnscheduledPred(I->getSUnit()) == SU) if (getSingleUnscheduledPred(I->getSUnit()) == SU)
++NumNodesBlocking; ++NumNodesBlocking;
}
NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking; NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking;
Queue.push(SU); Queue.push(SU);
@ -88,9 +91,11 @@ void LatencyPriorityQueue::push_impl(SUnit *SU) {
// the node available. // the node available.
void LatencyPriorityQueue::ScheduledNode(SUnit *SU) { void LatencyPriorityQueue::ScheduledNode(SUnit *SU) {
for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
I != E; ++I) I != E; ++I) {
if (IgnoreAntiDep && (I->getKind() == SDep::Anti)) continue;
AdjustPriorityOfUnscheduledPreds(I->getSUnit()); AdjustPriorityOfUnscheduledPreds(I->getSUnit());
} }
}
/// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just /// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just
/// scheduled. If SU is not itself available, then there is at least one /// scheduled. If SU is not itself available, then there is at least one

View File

@ -175,10 +175,11 @@ namespace {
void FixupKills(MachineBasicBlock *MBB); void FixupKills(MachineBasicBlock *MBB);
private: private:
void ReleaseSucc(SUnit *SU, SDep *SuccEdge); void ReleaseSucc(SUnit *SU, SDep *SuccEdge, bool IgnoreAntiDep);
void ReleaseSuccessors(SUnit *SU); void ReleaseSuccessors(SUnit *SU, bool IgnoreAntiDep);
void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle, bool IgnoreAntiDep);
void ListScheduleTopDown(); void ListScheduleTopDown(
AntiDepBreaker::CandidateMap *AntiDepCandidates);
void StartBlockForKills(MachineBasicBlock *BB); void StartBlockForKills(MachineBasicBlock *BB);
// ToggleKillFlag - Toggle a register operand kill flag. Other // ToggleKillFlag - Toggle a register operand kill flag. Other
@ -320,15 +321,32 @@ void SchedulePostRATDList::Schedule() {
BuildSchedGraph(AA); BuildSchedGraph(AA);
if (AntiDepBreak != NULL) { if (AntiDepBreak != NULL) {
AntiDepBreaker::CandidateMap AntiDepCandidates;
const bool NeedCandidates = AntiDepBreak->NeedCandidates();
for (unsigned i = 0, Trials = AntiDepBreak->GetMaxTrials(); for (unsigned i = 0, Trials = AntiDepBreak->GetMaxTrials();
i < Trials; ++i) { i < Trials; ++i) {
DEBUG(errs() << "********** Break Anti-Deps, Trial " << DEBUG(errs() << "\n********** Break Anti-Deps, Trial " <<
i << " **********\n"); i << " **********\n");
// If candidates are required, then schedule forward ignoring
// anti-dependencies to collect the candidate operands for
// anti-dependence breaking. The candidates will be the def
// operands for the anti-dependencies that if broken would allow
// an improved schedule
if (NeedCandidates) {
DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
SUnits[su].dumpAll(this));
AntiDepCandidates.clear();
AvailableQueue.initNodes(SUnits);
ListScheduleTopDown(&AntiDepCandidates);
AvailableQueue.releaseState();
}
unsigned Broken = unsigned Broken =
AntiDepBreak->BreakAntiDependencies(SUnits, Begin, InsertPos, AntiDepBreak->BreakAntiDependencies(SUnits, AntiDepCandidates,
InsertPosIndex); Begin, InsertPos, InsertPosIndex);
if (Broken == 0)
break;
// We made changes. Update the dependency graph. // We made changes. Update the dependency graph.
// Theoretically we could update the graph in place: // Theoretically we could update the graph in place:
@ -336,24 +354,26 @@ void SchedulePostRATDList::Schedule() {
// the def's anti-dependence *and* output-dependence edges due to // the def's anti-dependence *and* output-dependence edges due to
// that register, and add new anti-dependence and output-dependence // that register, and add new anti-dependence and output-dependence
// edges based on the next live range of the register. // edges based on the next live range of the register.
if ((Broken != 0) || NeedCandidates) {
SUnits.clear(); SUnits.clear();
Sequence.clear();
EntrySU = SUnit(); EntrySU = SUnit();
ExitSU = SUnit(); ExitSU = SUnit();
BuildSchedGraph(AA); BuildSchedGraph(AA);
}
NumFixedAnti += Broken; NumFixedAnti += Broken;
if (Broken == 0)
break;
} }
} }
DEBUG(errs() << "********** List Scheduling **********\n"); DEBUG(errs() << "********** List Scheduling **********\n");
DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
SUnits[su].dumpAll(this)); SUnits[su].dumpAll(this));
AvailableQueue.initNodes(SUnits); AvailableQueue.initNodes(SUnits);
ListScheduleTopDown(NULL);
ListScheduleTopDown();
AvailableQueue.releaseState(); AvailableQueue.releaseState();
} }
@ -552,7 +572,8 @@ void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
/// the PendingQueue if the count reaches zero. Also update its cycle bound. /// the PendingQueue if the count reaches zero. Also update its cycle bound.
void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge,
bool IgnoreAntiDep) {
SUnit *SuccSU = SuccEdge->getSUnit(); SUnit *SuccSU = SuccEdge->getSUnit();
#ifndef NDEBUG #ifndef NDEBUG
@ -568,7 +589,8 @@ void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
// Compute how many cycles it will be before this actually becomes // Compute how many cycles it will be before this actually becomes
// available. This is the max of the start time of all predecessors plus // available. This is the max of the start time of all predecessors plus
// their latencies. // their latencies.
SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); SuccSU->setDepthToAtLeast(SU->getDepth(IgnoreAntiDep) +
SuccEdge->getLatency(), IgnoreAntiDep);
// If all the node's predecessors are scheduled, this node is ready // If all the node's predecessors are scheduled, this node is ready
// to be scheduled. Ignore the special ExitSU node. // to be scheduled. Ignore the special ExitSU node.
@ -577,40 +599,73 @@ void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
} }
/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU, bool IgnoreAntiDep) {
for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
I != E; ++I) I != E; ++I) {
ReleaseSucc(SU, &*I); if (IgnoreAntiDep && (I->getKind() == SDep::Anti)) continue;
ReleaseSucc(SU, &*I, IgnoreAntiDep);
}
} }
/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
/// count of its successors. If a successor pending count is zero, add it to /// count of its successors. If a successor pending count is zero, add it to
/// the Available queue. /// the Available queue.
void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle,
bool IgnoreAntiDep) {
DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: "); DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: ");
DEBUG(SU->dump(this)); DEBUG(SU->dump(this));
Sequence.push_back(SU); Sequence.push_back(SU);
assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!"); assert(CurCycle >= SU->getDepth(IgnoreAntiDep) &&
SU->setDepthToAtLeast(CurCycle); "Node scheduled above its depth!");
SU->setDepthToAtLeast(CurCycle, IgnoreAntiDep);
ReleaseSuccessors(SU); ReleaseSuccessors(SU, IgnoreAntiDep);
SU->isScheduled = true; SU->isScheduled = true;
AvailableQueue.ScheduledNode(SU); AvailableQueue.ScheduledNode(SU);
} }
/// ListScheduleTopDown - The main loop of list scheduling for top-down /// ListScheduleTopDown - The main loop of list scheduling for top-down
/// schedulers. /// schedulers.
void SchedulePostRATDList::ListScheduleTopDown() { void SchedulePostRATDList::ListScheduleTopDown(
AntiDepBreaker::CandidateMap *AntiDepCandidates) {
unsigned CurCycle = 0; unsigned CurCycle = 0;
const bool IgnoreAntiDep = (AntiDepCandidates != NULL);
// We're scheduling top-down but we're visiting the regions in
// bottom-up order, so we don't know the hazards at the start of a
// region. So assume no hazards (this should usually be ok as most
// blocks are a single region).
HazardRec->Reset();
// If ignoring anti-dependencies, the Schedule DAG still has Anti
// dep edges, but we ignore them for scheduling purposes
AvailableQueue.setIgnoreAntiDep(IgnoreAntiDep);
// Release any successors of the special Entry node. // Release any successors of the special Entry node.
ReleaseSuccessors(&EntrySU); ReleaseSuccessors(&EntrySU, IgnoreAntiDep);
// All leaves to Available queue. // Add all leaves to Available queue. If ignoring antideps we also
// adjust the predecessor count for each node to not include antidep
// edges.
for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
// It is available if it has no predecessors. // It is available if it has no predecessors.
if (SUnits[i].Preds.empty()) { bool available = SUnits[i].Preds.empty();
// If we are ignoring anti-dependencies then a node that has only
// anti-dep predecessors is available.
if (!available && IgnoreAntiDep) {
available = true;
for (SUnit::const_pred_iterator I = SUnits[i].Preds.begin(),
E = SUnits[i].Preds.end(); I != E; ++I) {
if (I->getKind() != SDep::Anti) {
available = false;
} else {
SUnits[i].NumPredsLeft -= 1;
}
}
}
if (available) {
AvailableQueue.push(&SUnits[i]); AvailableQueue.push(&SUnits[i]);
SUnits[i].isAvailable = true; SUnits[i].isAvailable = true;
} }
@ -629,26 +684,25 @@ void SchedulePostRATDList::ListScheduleTopDown() {
// so, add them to the available queue. // so, add them to the available queue.
unsigned MinDepth = ~0u; unsigned MinDepth = ~0u;
for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
if (PendingQueue[i]->getDepth() <= CurCycle) { if (PendingQueue[i]->getDepth(IgnoreAntiDep) <= CurCycle) {
AvailableQueue.push(PendingQueue[i]); AvailableQueue.push(PendingQueue[i]);
PendingQueue[i]->isAvailable = true; PendingQueue[i]->isAvailable = true;
PendingQueue[i] = PendingQueue.back(); PendingQueue[i] = PendingQueue.back();
PendingQueue.pop_back(); PendingQueue.pop_back();
--i; --e; --i; --e;
} else if (PendingQueue[i]->getDepth() < MinDepth) } else if (PendingQueue[i]->getDepth(IgnoreAntiDep) < MinDepth)
MinDepth = PendingQueue[i]->getDepth(); MinDepth = PendingQueue[i]->getDepth(IgnoreAntiDep);
} }
DEBUG(errs() << "\n*** Examining Available\n"; DEBUG(errs() << "\n*** Examining Available\n";
LatencyPriorityQueue q = AvailableQueue; LatencyPriorityQueue q = AvailableQueue;
while (!q.empty()) { while (!q.empty()) {
SUnit *su = q.pop(); SUnit *su = q.pop();
errs() << "Height " << su->getHeight() << ": "; errs() << "Height " << su->getHeight(IgnoreAntiDep) << ": ";
su->dump(this); su->dump(this);
}); });
SUnit *FoundSUnit = 0; SUnit *FoundSUnit = 0;
bool HasNoopHazards = false; bool HasNoopHazards = false;
while (!AvailableQueue.empty()) { while (!AvailableQueue.empty()) {
SUnit *CurSUnit = AvailableQueue.pop(); SUnit *CurSUnit = AvailableQueue.pop();
@ -672,9 +726,30 @@ void SchedulePostRATDList::ListScheduleTopDown() {
NotReady.clear(); NotReady.clear();
} }
// If we found a node to schedule, do it now. // If we found a node to schedule...
if (FoundSUnit) { if (FoundSUnit) {
ScheduleNodeTopDown(FoundSUnit, CurCycle); // If we are ignoring anti-dependencies and the SUnit we are
// scheduling has an antidep predecessor that has not been
// scheduled, then we will need to break that antidep if we want
// to get this schedule when not ignoring anti-dependencies.
if (IgnoreAntiDep) {
AntiDepBreaker::AntiDepRegVector AntiDepRegs;
for (SUnit::const_pred_iterator I = FoundSUnit->Preds.begin(),
E = FoundSUnit->Preds.end(); I != E; ++I) {
if ((I->getKind() == SDep::Anti) && !I->getSUnit()->isScheduled)
AntiDepRegs.push_back(I->getReg());
}
if (AntiDepRegs.size() > 0) {
DEBUG(errs() << "*** AntiDep Candidate: ");
DEBUG(FoundSUnit->dump(this));
AntiDepCandidates->insert(
AntiDepBreaker::CandidateMap::value_type(FoundSUnit, AntiDepRegs));
}
}
// ... schedule the node...
ScheduleNodeTopDown(FoundSUnit, CurCycle, IgnoreAntiDep);
HazardRec->EmitInstruction(FoundSUnit); HazardRec->EmitInstruction(FoundSUnit);
CycleHasInsts = true; CycleHasInsts = true;

View File

@ -183,8 +183,8 @@ void SUnit::setHeightDirty() {
/// setDepthToAtLeast - Update this node's successors to reflect the /// setDepthToAtLeast - Update this node's successors to reflect the
/// fact that this node's depth just increased. /// fact that this node's depth just increased.
/// ///
void SUnit::setDepthToAtLeast(unsigned NewDepth) { void SUnit::setDepthToAtLeast(unsigned NewDepth, bool IgnoreAntiDep) {
if (NewDepth <= getDepth()) if (NewDepth <= getDepth(IgnoreAntiDep))
return; return;
setDepthDirty(); setDepthDirty();
Depth = NewDepth; Depth = NewDepth;
@ -194,8 +194,8 @@ void SUnit::setDepthToAtLeast(unsigned NewDepth) {
/// setHeightToAtLeast - Update this node's predecessors to reflect the /// setHeightToAtLeast - Update this node's predecessors to reflect the
/// fact that this node's height just increased. /// fact that this node's height just increased.
/// ///
void SUnit::setHeightToAtLeast(unsigned NewHeight) { void SUnit::setHeightToAtLeast(unsigned NewHeight, bool IgnoreAntiDep) {
if (NewHeight <= getHeight()) if (NewHeight <= getHeight(IgnoreAntiDep))
return; return;
setHeightDirty(); setHeightDirty();
Height = NewHeight; Height = NewHeight;
@ -204,7 +204,7 @@ void SUnit::setHeightToAtLeast(unsigned NewHeight) {
/// ComputeDepth - Calculate the maximal path from the node to the exit. /// ComputeDepth - Calculate the maximal path from the node to the exit.
/// ///
void SUnit::ComputeDepth() { void SUnit::ComputeDepth(bool IgnoreAntiDep) {
SmallVector<SUnit*, 8> WorkList; SmallVector<SUnit*, 8> WorkList;
WorkList.push_back(this); WorkList.push_back(this);
do { do {
@ -214,6 +214,7 @@ void SUnit::ComputeDepth() {
unsigned MaxPredDepth = 0; unsigned MaxPredDepth = 0;
for (SUnit::const_pred_iterator I = Cur->Preds.begin(), for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
E = Cur->Preds.end(); I != E; ++I) { E = Cur->Preds.end(); I != E; ++I) {
if (IgnoreAntiDep && (I->getKind() == SDep::Anti)) continue;
SUnit *PredSU = I->getSUnit(); SUnit *PredSU = I->getSUnit();
if (PredSU->isDepthCurrent) if (PredSU->isDepthCurrent)
MaxPredDepth = std::max(MaxPredDepth, MaxPredDepth = std::max(MaxPredDepth,
@ -237,7 +238,7 @@ void SUnit::ComputeDepth() {
/// ComputeHeight - Calculate the maximal path from the node to the entry. /// ComputeHeight - Calculate the maximal path from the node to the entry.
/// ///
void SUnit::ComputeHeight() { void SUnit::ComputeHeight(bool IgnoreAntiDep) {
SmallVector<SUnit*, 8> WorkList; SmallVector<SUnit*, 8> WorkList;
WorkList.push_back(this); WorkList.push_back(this);
do { do {
@ -247,6 +248,7 @@ void SUnit::ComputeHeight() {
unsigned MaxSuccHeight = 0; unsigned MaxSuccHeight = 0;
for (SUnit::const_succ_iterator I = Cur->Succs.begin(), for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
E = Cur->Succs.end(); I != E; ++I) { E = Cur->Succs.end(); I != E; ++I) {
if (IgnoreAntiDep && (I->getKind() == SDep::Anti)) continue;
SUnit *SuccSU = I->getSUnit(); SUnit *SuccSU = I->getSUnit();
if (SuccSU->isHeightCurrent) if (SuccSU->isHeightCurrent)
MaxSuccHeight = std::max(MaxSuccHeight, MaxSuccHeight = std::max(MaxSuccHeight,
@ -346,7 +348,7 @@ void ScheduleDAG::VerifySchedule(bool isBottomUp) {
AnyNotSched = true; AnyNotSched = true;
} }
if (SUnits[i].isScheduled && if (SUnits[i].isScheduled &&
(isBottomUp ? SUnits[i].getHeight() : SUnits[i].getHeight()) > (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
unsigned(INT_MAX)) { unsigned(INT_MAX)) {
if (!AnyNotSched) if (!AnyNotSched)
errs() << "*** Scheduling failed! ***\n"; errs() << "*** Scheduling failed! ***\n";