2008-09-19 00:26:26 +08:00
|
|
|
//===----- ScheduleDAGFast.cpp - Fast poor list scheduler -----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements a fast scheduler.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "InstrEmitter.h"
|
|
|
|
#include "ScheduleDAGSDNodes.h"
|
2018-03-03 06:59:51 +08:00
|
|
|
#include "SDNodeDbgValue.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/CodeGen/SchedulerRegistry.h"
|
2009-01-16 03:20:50 +08:00
|
|
|
#include "llvm/CodeGen/SelectionDAGISel.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/InlineAsm.h"
|
2008-09-19 00:26:26 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 21:10:19 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-23 14:35:02 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-09-19 00:26:26 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "pre-RA-sched"
|
|
|
|
|
2008-09-19 00:26:26 +08:00
|
|
|
STATISTIC(NumUnfolds, "Number of nodes unfolded");
|
|
|
|
STATISTIC(NumDups, "Number of duplicated nodes");
|
2009-01-12 11:19:55 +08:00
|
|
|
STATISTIC(NumPRCopies, "Number of physical copies");
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
static RegisterScheduler
|
2008-10-15 04:25:08 +08:00
|
|
|
fastDAGScheduler("fast", "Fast suboptimal list scheduling",
|
2008-09-19 00:26:26 +08:00
|
|
|
createFastDAGScheduler);
|
2012-10-18 03:39:36 +08:00
|
|
|
static RegisterScheduler
|
|
|
|
linearizeDAGScheduler("linearize", "Linearize DAG, no scheduling",
|
|
|
|
createDAGLinearizer);
|
|
|
|
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// FastPriorityQueue - A degenerate priority queue that considers
|
|
|
|
/// all nodes to have the same priority.
|
|
|
|
///
|
2009-10-25 14:33:48 +08:00
|
|
|
struct FastPriorityQueue {
|
2008-09-24 02:50:48 +08:00
|
|
|
SmallVector<SUnit *, 16> Queue;
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
bool empty() const { return Queue.empty(); }
|
2012-03-07 13:21:32 +08:00
|
|
|
|
2008-09-19 00:26:26 +08:00
|
|
|
void push(SUnit *U) {
|
|
|
|
Queue.push_back(U);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUnit *pop() {
|
2014-04-14 08:51:57 +08:00
|
|
|
if (empty()) return nullptr;
|
2008-09-19 00:26:26 +08:00
|
|
|
SUnit *V = Queue.back();
|
|
|
|
Queue.pop_back();
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// ScheduleDAGFast - The actual "fast" list scheduler implementation.
|
|
|
|
///
|
2009-10-25 14:33:48 +08:00
|
|
|
class ScheduleDAGFast : public ScheduleDAGSDNodes {
|
2008-09-19 00:26:26 +08:00
|
|
|
private:
|
|
|
|
/// AvailableQueue - The priority queue to use for the available SUnits.
|
|
|
|
FastPriorityQueue AvailableQueue;
|
|
|
|
|
2008-09-24 02:50:48 +08:00
|
|
|
/// LiveRegDefs - A set of physical registers and their definition
|
2008-09-19 00:26:26 +08:00
|
|
|
/// that are "live". These nodes must be scheduled before any other nodes that
|
|
|
|
/// modifies the registers can be scheduled.
|
2008-09-24 02:50:48 +08:00
|
|
|
unsigned NumLiveRegs;
|
2008-09-19 00:26:26 +08:00
|
|
|
std::vector<SUnit*> LiveRegDefs;
|
|
|
|
std::vector<unsigned> LiveRegCycles;
|
|
|
|
|
|
|
|
public:
|
2009-01-16 03:20:50 +08:00
|
|
|
ScheduleDAGFast(MachineFunction &mf)
|
|
|
|
: ScheduleDAGSDNodes(mf) {}
|
2008-09-19 00:26:26 +08:00
|
|
|
|
2014-03-08 14:31:39 +08:00
|
|
|
void Schedule() override;
|
2008-09-19 00:26:26 +08:00
|
|
|
|
2008-12-10 06:54:47 +08:00
|
|
|
/// AddPred - adds a predecessor edge to SUnit SU.
|
2008-09-19 00:26:26 +08:00
|
|
|
/// This returns true if this is a new predecessor.
|
2008-12-16 09:00:55 +08:00
|
|
|
void AddPred(SUnit *SU, const SDep &D) {
|
|
|
|
SU->addPred(D);
|
2008-12-10 06:54:47 +08:00
|
|
|
}
|
2008-09-19 00:26:26 +08:00
|
|
|
|
2008-12-10 06:54:47 +08:00
|
|
|
/// RemovePred - removes a predecessor edge from SUnit SU.
|
|
|
|
/// This returns true if an edge was removed.
|
2008-12-16 09:00:55 +08:00
|
|
|
void RemovePred(SUnit *SU, const SDep &D) {
|
|
|
|
SU->removePred(D);
|
2008-12-10 06:54:47 +08:00
|
|
|
}
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
private:
|
2008-12-10 06:54:47 +08:00
|
|
|
void ReleasePred(SUnit *SU, SDep *PredEdge);
|
2009-02-11 07:27:53 +08:00
|
|
|
void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
|
2008-09-19 00:26:26 +08:00
|
|
|
void ScheduleNodeBottomUp(SUnit*, unsigned);
|
|
|
|
SUnit *CopyAndMoveSuccessors(SUnit*);
|
2009-01-12 11:19:55 +08:00
|
|
|
void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
|
|
|
|
const TargetRegisterClass*,
|
|
|
|
const TargetRegisterClass*,
|
2013-07-14 12:42:23 +08:00
|
|
|
SmallVectorImpl<SUnit*>&);
|
|
|
|
bool DelayForLiveRegsBottomUp(SUnit*, SmallVectorImpl<unsigned>&);
|
2008-09-19 00:26:26 +08:00
|
|
|
void ListScheduleBottomUp();
|
2008-12-16 11:25:46 +08:00
|
|
|
|
2012-03-08 07:00:49 +08:00
|
|
|
/// forceUnitLatencies - The fast scheduler doesn't care about real latencies.
|
2014-03-08 14:31:39 +08:00
|
|
|
bool forceUnitLatencies() const override { return true; }
|
2008-09-19 00:26:26 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
|
/// Schedule - Schedule the DAG using list scheduling.
|
|
|
|
void ScheduleDAGFast::Schedule() {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "********** List Scheduling **********\n");
|
2008-09-19 00:26:26 +08:00
|
|
|
|
2008-09-24 02:50:48 +08:00
|
|
|
NumLiveRegs = 0;
|
2014-04-14 08:51:57 +08:00
|
|
|
LiveRegDefs.resize(TRI->getNumRegs(), nullptr);
|
2008-09-19 00:26:26 +08:00
|
|
|
LiveRegCycles.resize(TRI->getNumRegs(), 0);
|
|
|
|
|
2008-12-24 02:36:58 +08:00
|
|
|
// Build the scheduling graph.
|
2014-04-14 08:51:57 +08:00
|
|
|
BuildSchedGraph(nullptr);
|
2008-09-19 00:26:26 +08:00
|
|
|
|
2018-09-19 08:23:35 +08:00
|
|
|
LLVM_DEBUG(dump());
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
// Execute the actual scheduling loop.
|
|
|
|
ListScheduleBottomUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Bottom-Up Scheduling
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
|
|
|
|
/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
|
2008-12-10 06:54:47 +08:00
|
|
|
void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) {
|
|
|
|
SUnit *PredSU = PredEdge->getSUnit();
|
2009-10-01 04:15:38 +08:00
|
|
|
|
2008-09-19 00:26:26 +08:00
|
|
|
#ifndef NDEBUG
|
2009-10-01 04:15:38 +08:00
|
|
|
if (PredSU->NumSuccsLeft == 0) {
|
2010-01-05 09:25:09 +08:00
|
|
|
dbgs() << "*** Scheduling failed! ***\n";
|
2018-09-19 08:23:35 +08:00
|
|
|
dumpNode(*PredSU);
|
2010-01-05 09:25:09 +08:00
|
|
|
dbgs() << " has been released too many times!\n";
|
2014-04-14 08:51:57 +08:00
|
|
|
llvm_unreachable(nullptr);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
#endif
|
2009-10-01 04:15:38 +08:00
|
|
|
--PredSU->NumSuccsLeft;
|
|
|
|
|
2009-02-11 07:27:53 +08:00
|
|
|
// If all the node's successors are scheduled, this node is ready
|
|
|
|
// to be scheduled. Ignore the special EntrySU node.
|
|
|
|
if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
|
2008-09-19 00:26:26 +08:00
|
|
|
PredSU->isAvailable = true;
|
|
|
|
AvailableQueue.push(PredSU);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-11 07:27:53 +08:00
|
|
|
void ScheduleDAGFast::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
|
2008-09-19 00:26:26 +08:00
|
|
|
// Bottom up: release predecessors
|
2017-05-04 21:35:17 +08:00
|
|
|
for (SDep &Pred : SU->Preds) {
|
|
|
|
ReleasePred(SU, &Pred);
|
|
|
|
if (Pred.isAssignedRegDep()) {
|
2008-09-19 00:26:26 +08:00
|
|
|
// This is a physical register dependency and it's impossible or
|
2012-03-07 13:21:32 +08:00
|
|
|
// expensive to copy the register. Make sure nothing that can
|
2008-09-19 00:26:26 +08:00
|
|
|
// clobber the register is scheduled between the predecessor and
|
|
|
|
// this node.
|
2017-05-04 21:35:17 +08:00
|
|
|
if (!LiveRegDefs[Pred.getReg()]) {
|
2008-09-24 02:50:48 +08:00
|
|
|
++NumLiveRegs;
|
2017-05-04 21:35:17 +08:00
|
|
|
LiveRegDefs[Pred.getReg()] = Pred.getSUnit();
|
|
|
|
LiveRegCycles[Pred.getReg()] = CurCycle;
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-11 07:27:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
|
|
|
|
/// count of its predecessors. If a predecessor pending count is zero, add it to
|
|
|
|
/// the Available queue.
|
|
|
|
void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
|
2018-09-19 08:23:35 +08:00
|
|
|
LLVM_DEBUG(dumpNode(*SU));
|
2009-02-11 07:27:53 +08:00
|
|
|
|
|
|
|
assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
|
|
|
|
SU->setHeightToAtLeast(CurCycle);
|
|
|
|
Sequence.push_back(SU);
|
|
|
|
|
|
|
|
ReleasePredecessors(SU, CurCycle);
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
// Release all the implicit physical register defs that are live.
|
2017-05-04 21:35:17 +08:00
|
|
|
for (SDep &Succ : SU->Succs) {
|
|
|
|
if (Succ.isAssignedRegDep()) {
|
|
|
|
if (LiveRegCycles[Succ.getReg()] == Succ.getSUnit()->getHeight()) {
|
2008-09-24 02:50:48 +08:00
|
|
|
assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
|
2017-05-04 21:35:17 +08:00
|
|
|
assert(LiveRegDefs[Succ.getReg()] == SU &&
|
2008-09-19 00:26:26 +08:00
|
|
|
"Physical register dependency violated?");
|
2008-09-24 02:50:48 +08:00
|
|
|
--NumLiveRegs;
|
2017-05-04 21:35:17 +08:00
|
|
|
LiveRegDefs[Succ.getReg()] = nullptr;
|
|
|
|
LiveRegCycles[Succ.getReg()] = 0;
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SU->isScheduled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
|
|
|
|
/// successors to the newly created node.
|
|
|
|
SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
|
2010-12-24 01:24:32 +08:00
|
|
|
if (SU->getNode()->getGluedNode())
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2008-09-19 00:26:26 +08:00
|
|
|
|
2008-11-14 05:36:12 +08:00
|
|
|
SDNode *N = SU->getNode();
|
2008-09-19 00:26:26 +08:00
|
|
|
if (!N)
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
SUnit *NewSU;
|
|
|
|
bool TryUnfold = false;
|
|
|
|
for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
|
2014-11-17 05:17:18 +08:00
|
|
|
MVT VT = N->getSimpleValueType(i);
|
2010-12-21 10:38:05 +08:00
|
|
|
if (VT == MVT::Glue)
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2009-08-12 04:47:22 +08:00
|
|
|
else if (VT == MVT::Other)
|
2008-09-19 00:26:26 +08:00
|
|
|
TryUnfold = true;
|
|
|
|
}
|
2015-06-27 03:18:49 +08:00
|
|
|
for (const SDValue &Op : N->op_values()) {
|
2014-11-17 05:17:18 +08:00
|
|
|
MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
|
2010-12-21 10:38:05 +08:00
|
|
|
if (VT == MVT::Glue)
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (TryUnfold) {
|
|
|
|
SmallVector<SDNode*, 2> NewNodes;
|
2008-11-14 05:21:28 +08:00
|
|
|
if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2008-09-19 00:26:26 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n");
|
2008-09-19 00:26:26 +08:00
|
|
|
assert(NewNodes.size() == 2 && "Expected a load folding node!");
|
|
|
|
|
|
|
|
N = NewNodes[1];
|
|
|
|
SDNode *LoadNode = NewNodes[0];
|
|
|
|
unsigned NumVals = N->getNumValues();
|
2008-11-14 05:36:12 +08:00
|
|
|
unsigned OldNumVals = SU->getNode()->getNumValues();
|
2008-09-19 00:26:26 +08:00
|
|
|
for (unsigned i = 0; i != NumVals; ++i)
|
2008-11-14 05:36:12 +08:00
|
|
|
DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
|
|
|
|
DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
|
2008-11-14 05:21:28 +08:00
|
|
|
SDValue(LoadNode, 1));
|
2008-09-19 00:26:26 +08:00
|
|
|
|
2012-03-08 07:00:49 +08:00
|
|
|
SUnit *NewSU = newSUnit(N);
|
2008-09-19 00:26:26 +08:00
|
|
|
assert(N->getNodeId() == -1 && "Node already inserted!");
|
|
|
|
N->setNodeId(NewSU->NodeNum);
|
2012-03-07 13:21:32 +08:00
|
|
|
|
2011-06-29 03:10:37 +08:00
|
|
|
const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
|
|
|
|
for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
|
|
|
|
if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
|
2008-09-19 00:26:26 +08:00
|
|
|
NewSU->isTwoAddress = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-06-29 03:10:37 +08:00
|
|
|
if (MCID.isCommutable())
|
2008-09-19 00:26:26 +08:00
|
|
|
NewSU->isCommutable = true;
|
|
|
|
|
|
|
|
// LoadNode may already exist. This can happen when there is another
|
|
|
|
// load from the same location and producing the same type of value
|
|
|
|
// but it has different alignment or volatileness.
|
|
|
|
bool isNewLoad = true;
|
|
|
|
SUnit *LoadSU;
|
|
|
|
if (LoadNode->getNodeId() != -1) {
|
|
|
|
LoadSU = &SUnits[LoadNode->getNodeId()];
|
|
|
|
isNewLoad = false;
|
|
|
|
} else {
|
2012-03-08 07:00:49 +08:00
|
|
|
LoadSU = newSUnit(LoadNode);
|
2008-09-19 00:26:26 +08:00
|
|
|
LoadNode->setNodeId(LoadSU->NodeNum);
|
|
|
|
}
|
|
|
|
|
2008-12-10 06:54:47 +08:00
|
|
|
SDep ChainPred;
|
2008-09-19 00:26:26 +08:00
|
|
|
SmallVector<SDep, 4> ChainSuccs;
|
|
|
|
SmallVector<SDep, 4> LoadPreds;
|
|
|
|
SmallVector<SDep, 4> NodePreds;
|
|
|
|
SmallVector<SDep, 4> NodeSuccs;
|
2017-05-04 21:35:17 +08:00
|
|
|
for (SDep &Pred : SU->Preds) {
|
|
|
|
if (Pred.isCtrl())
|
|
|
|
ChainPred = Pred;
|
|
|
|
else if (Pred.getSUnit()->getNode() &&
|
|
|
|
Pred.getSUnit()->getNode()->isOperandOf(LoadNode))
|
|
|
|
LoadPreds.push_back(Pred);
|
2008-09-19 00:26:26 +08:00
|
|
|
else
|
2017-05-04 21:35:17 +08:00
|
|
|
NodePreds.push_back(Pred);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
2017-05-04 21:35:17 +08:00
|
|
|
for (SDep &Succ : SU->Succs) {
|
|
|
|
if (Succ.isCtrl())
|
|
|
|
ChainSuccs.push_back(Succ);
|
2008-09-19 00:26:26 +08:00
|
|
|
else
|
2017-05-04 21:35:17 +08:00
|
|
|
NodeSuccs.push_back(Succ);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2008-12-10 06:54:47 +08:00
|
|
|
if (ChainPred.getSUnit()) {
|
|
|
|
RemovePred(SU, ChainPred);
|
2008-09-19 00:26:26 +08:00
|
|
|
if (isNewLoad)
|
2008-12-10 06:54:47 +08:00
|
|
|
AddPred(LoadSU, ChainPred);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
|
2008-12-10 06:54:47 +08:00
|
|
|
const SDep &Pred = LoadPreds[i];
|
|
|
|
RemovePred(SU, Pred);
|
2008-09-19 00:26:26 +08:00
|
|
|
if (isNewLoad) {
|
2008-12-10 06:54:47 +08:00
|
|
|
AddPred(LoadSU, Pred);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
|
2008-12-10 06:54:47 +08:00
|
|
|
const SDep &Pred = NodePreds[i];
|
|
|
|
RemovePred(SU, Pred);
|
|
|
|
AddPred(NewSU, Pred);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
|
2008-12-10 06:54:47 +08:00
|
|
|
SDep D = NodeSuccs[i];
|
|
|
|
SUnit *SuccDep = D.getSUnit();
|
|
|
|
D.setSUnit(SU);
|
|
|
|
RemovePred(SuccDep, D);
|
|
|
|
D.setSUnit(NewSU);
|
|
|
|
AddPred(SuccDep, D);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
|
2008-12-10 06:54:47 +08:00
|
|
|
SDep D = ChainSuccs[i];
|
|
|
|
SUnit *SuccDep = D.getSUnit();
|
|
|
|
D.setSUnit(SU);
|
|
|
|
RemovePred(SuccDep, D);
|
2008-09-19 00:26:26 +08:00
|
|
|
if (isNewLoad) {
|
2008-12-10 06:54:47 +08:00
|
|
|
D.setSUnit(LoadSU);
|
|
|
|
AddPred(SuccDep, D);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
2012-03-07 13:21:32 +08:00
|
|
|
}
|
2008-09-19 00:26:26 +08:00
|
|
|
if (isNewLoad) {
|
2012-11-06 11:13:46 +08:00
|
|
|
SDep D(LoadSU, SDep::Barrier);
|
|
|
|
D.setLatency(LoadSU->Latency);
|
|
|
|
AddPred(NewSU, D);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
++NumUnfolds;
|
|
|
|
|
|
|
|
if (NewSU->NumSuccsLeft == 0) {
|
|
|
|
NewSU->isAvailable = true;
|
|
|
|
return NewSU;
|
|
|
|
}
|
|
|
|
SU = NewSU;
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n");
|
2008-11-20 07:39:02 +08:00
|
|
|
NewSU = Clone(SU);
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
// New SUnit has the exact same predecessors.
|
2017-05-04 21:35:17 +08:00
|
|
|
for (SDep &Pred : SU->Preds)
|
|
|
|
if (!Pred.isArtificial())
|
|
|
|
AddPred(NewSU, Pred);
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
// Only copy scheduled successors. Cut them from old node's successor
|
|
|
|
// list and move them over.
|
2008-12-10 06:54:47 +08:00
|
|
|
SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
|
2017-05-04 21:35:17 +08:00
|
|
|
for (SDep &Succ : SU->Succs) {
|
|
|
|
if (Succ.isArtificial())
|
2008-09-19 00:26:26 +08:00
|
|
|
continue;
|
2017-05-04 21:35:17 +08:00
|
|
|
SUnit *SuccSU = Succ.getSUnit();
|
2008-12-10 06:54:47 +08:00
|
|
|
if (SuccSU->isScheduled) {
|
2017-05-04 21:35:17 +08:00
|
|
|
SDep D = Succ;
|
2008-12-10 06:54:47 +08:00
|
|
|
D.setSUnit(NewSU);
|
|
|
|
AddPred(SuccSU, D);
|
|
|
|
D.setSUnit(SU);
|
|
|
|
DelDeps.push_back(std::make_pair(SuccSU, D));
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
2009-01-12 11:19:55 +08:00
|
|
|
for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
|
2008-12-10 06:54:47 +08:00
|
|
|
RemovePred(DelDeps[i].first, DelDeps[i].second);
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
++NumDups;
|
|
|
|
return NewSU;
|
|
|
|
}
|
|
|
|
|
2009-01-12 11:19:55 +08:00
|
|
|
/// InsertCopiesAndMoveSuccs - Insert register copies and move all
|
|
|
|
/// scheduled successors of the given SUnit to the last copy.
|
|
|
|
void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
|
2008-09-19 00:26:26 +08:00
|
|
|
const TargetRegisterClass *DestRC,
|
|
|
|
const TargetRegisterClass *SrcRC,
|
2013-07-14 12:42:23 +08:00
|
|
|
SmallVectorImpl<SUnit*> &Copies) {
|
2014-04-14 08:51:57 +08:00
|
|
|
SUnit *CopyFromSU = newSUnit(static_cast<SDNode *>(nullptr));
|
2008-09-19 00:26:26 +08:00
|
|
|
CopyFromSU->CopySrcRC = SrcRC;
|
|
|
|
CopyFromSU->CopyDstRC = DestRC;
|
|
|
|
|
2014-04-14 08:51:57 +08:00
|
|
|
SUnit *CopyToSU = newSUnit(static_cast<SDNode *>(nullptr));
|
2008-09-19 00:26:26 +08:00
|
|
|
CopyToSU->CopySrcRC = DestRC;
|
|
|
|
CopyToSU->CopyDstRC = SrcRC;
|
|
|
|
|
|
|
|
// Only copy scheduled successors. Cut them from old node's successor
|
|
|
|
// list and move them over.
|
2008-12-10 06:54:47 +08:00
|
|
|
SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
|
2017-05-04 21:35:17 +08:00
|
|
|
for (SDep &Succ : SU->Succs) {
|
|
|
|
if (Succ.isArtificial())
|
2008-09-19 00:26:26 +08:00
|
|
|
continue;
|
2017-05-04 21:35:17 +08:00
|
|
|
SUnit *SuccSU = Succ.getSUnit();
|
2008-12-10 06:54:47 +08:00
|
|
|
if (SuccSU->isScheduled) {
|
2017-05-04 21:35:17 +08:00
|
|
|
SDep D = Succ;
|
2008-12-10 06:54:47 +08:00
|
|
|
D.setSUnit(CopyToSU);
|
|
|
|
AddPred(SuccSU, D);
|
2017-05-04 21:35:17 +08:00
|
|
|
DelDeps.push_back(std::make_pair(SuccSU, Succ));
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
|
2008-12-10 06:54:47 +08:00
|
|
|
RemovePred(DelDeps[i].first, DelDeps[i].second);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
2012-11-06 11:13:46 +08:00
|
|
|
SDep FromDep(SU, SDep::Data, Reg);
|
|
|
|
FromDep.setLatency(SU->Latency);
|
|
|
|
AddPred(CopyFromSU, FromDep);
|
|
|
|
SDep ToDep(CopyFromSU, SDep::Data, 0);
|
|
|
|
ToDep.setLatency(CopyFromSU->Latency);
|
|
|
|
AddPred(CopyToSU, ToDep);
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
Copies.push_back(CopyFromSU);
|
|
|
|
Copies.push_back(CopyToSU);
|
|
|
|
|
2009-01-12 11:19:55 +08:00
|
|
|
++NumPRCopies;
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getPhysicalRegisterVT - Returns the ValueType of the physical register
|
|
|
|
/// definition of the specified node.
|
|
|
|
/// FIXME: Move to SelectionDAG?
|
2014-11-17 05:17:18 +08:00
|
|
|
static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
|
2008-09-19 00:26:26 +08:00
|
|
|
const TargetInstrInfo *TII) {
|
2014-10-24 06:31:48 +08:00
|
|
|
unsigned NumRes;
|
|
|
|
if (N->getOpcode() == ISD::CopyFromReg) {
|
|
|
|
// CopyFromReg has: "chain, Val, glue" so operand 1 gives the type.
|
|
|
|
NumRes = 1;
|
|
|
|
} else {
|
|
|
|
const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
|
|
|
|
assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!");
|
|
|
|
NumRes = MCID.getNumDefs();
|
2015-12-05 15:13:35 +08:00
|
|
|
for (const MCPhysReg *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) {
|
2014-10-24 06:31:48 +08:00
|
|
|
if (Reg == *ImpDef)
|
|
|
|
break;
|
|
|
|
++NumRes;
|
|
|
|
}
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
2014-11-17 05:17:18 +08:00
|
|
|
return N->getSimpleValueType(NumRes);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2010-08-18 06:17:24 +08:00
|
|
|
/// CheckForLiveRegDef - Return true and update live register vector if the
|
|
|
|
/// specified register def of the specified SUnit clobbers any "live" registers.
|
|
|
|
static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
|
|
|
|
std::vector<SUnit*> &LiveRegDefs,
|
|
|
|
SmallSet<unsigned, 4> &RegAdded,
|
2013-07-14 12:42:23 +08:00
|
|
|
SmallVectorImpl<unsigned> &LRegs,
|
2010-08-18 06:17:24 +08:00
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
bool Added = false;
|
2012-06-02 06:38:17 +08:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
|
|
|
|
if (LiveRegDefs[*AI] && LiveRegDefs[*AI] != SU) {
|
2014-11-19 15:49:26 +08:00
|
|
|
if (RegAdded.insert(*AI).second) {
|
2012-06-02 06:38:17 +08:00
|
|
|
LRegs.push_back(*AI);
|
2010-08-18 06:17:24 +08:00
|
|
|
Added = true;
|
|
|
|
}
|
|
|
|
}
|
2012-06-02 06:38:17 +08:00
|
|
|
}
|
2010-08-18 06:17:24 +08:00
|
|
|
return Added;
|
|
|
|
}
|
|
|
|
|
2008-09-19 00:26:26 +08:00
|
|
|
/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
|
|
|
|
/// scheduling of the given node to satisfy live physical register dependencies.
|
|
|
|
/// If the specific node is the last one that's available to schedule, do
|
|
|
|
/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
|
|
|
|
bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
|
2013-07-14 12:42:23 +08:00
|
|
|
SmallVectorImpl<unsigned> &LRegs){
|
2008-09-24 02:50:48 +08:00
|
|
|
if (NumLiveRegs == 0)
|
2008-09-19 00:26:26 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallSet<unsigned, 4> RegAdded;
|
|
|
|
// If this node would clobber any "live" register, then it's not ready.
|
2017-05-04 21:35:17 +08:00
|
|
|
for (SDep &Pred : SU->Preds) {
|
|
|
|
if (Pred.isAssignedRegDep()) {
|
|
|
|
CheckForLiveRegDef(Pred.getSUnit(), Pred.getReg(), LiveRegDefs,
|
2010-08-18 06:17:24 +08:00
|
|
|
RegAdded, LRegs, TRI);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-24 01:24:32 +08:00
|
|
|
for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
|
2010-08-18 06:17:24 +08:00
|
|
|
if (Node->getOpcode() == ISD::INLINEASM) {
|
|
|
|
// Inline asm can clobber physical defs.
|
|
|
|
unsigned NumOps = Node->getNumOperands();
|
2010-12-21 10:38:05 +08:00
|
|
|
if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
|
2010-12-24 01:24:32 +08:00
|
|
|
--NumOps; // Ignore the glue operand.
|
2010-08-18 06:17:24 +08:00
|
|
|
|
|
|
|
for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
|
|
|
|
unsigned Flags =
|
|
|
|
cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
|
|
|
|
unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
|
|
|
|
|
|
|
|
++i; // Skip the ID value.
|
|
|
|
if (InlineAsm::isRegDefKind(Flags) ||
|
2011-06-27 12:08:33 +08:00
|
|
|
InlineAsm::isRegDefEarlyClobberKind(Flags) ||
|
|
|
|
InlineAsm::isClobberKind(Flags)) {
|
2010-08-18 06:17:24 +08:00
|
|
|
// Check for def of register or earlyclobber register.
|
|
|
|
for (; NumVals; --NumVals, ++i) {
|
|
|
|
unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg))
|
|
|
|
CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
i += NumVals;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-14 07:24:17 +08:00
|
|
|
if (!Node->isMachineOpcode())
|
2008-09-19 00:26:26 +08:00
|
|
|
continue;
|
2011-06-29 03:10:37 +08:00
|
|
|
const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode());
|
|
|
|
if (!MCID.ImplicitDefs)
|
2008-09-19 00:26:26 +08:00
|
|
|
continue;
|
2015-12-05 15:13:35 +08:00
|
|
|
for (const MCPhysReg *Reg = MCID.getImplicitDefs(); *Reg; ++Reg) {
|
2010-08-18 06:17:24 +08:00
|
|
|
CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return !LRegs.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
|
|
|
|
/// schedulers.
|
|
|
|
void ScheduleDAGFast::ListScheduleBottomUp() {
|
|
|
|
unsigned CurCycle = 0;
|
2009-02-11 07:27:53 +08:00
|
|
|
|
|
|
|
// Release any predecessors of the special Exit node.
|
|
|
|
ReleasePredecessors(&ExitSU, CurCycle);
|
|
|
|
|
2008-09-19 00:26:26 +08:00
|
|
|
// Add root to Available queue.
|
|
|
|
if (!SUnits.empty()) {
|
2008-11-14 05:21:28 +08:00
|
|
|
SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
|
2008-09-19 00:26:26 +08:00
|
|
|
assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
|
|
|
|
RootSU->isAvailable = true;
|
|
|
|
AvailableQueue.push(RootSU);
|
|
|
|
}
|
|
|
|
|
|
|
|
// While Available queue is not empty, grab the node with the highest
|
|
|
|
// priority. If it is not ready put it back. Schedule the node.
|
|
|
|
SmallVector<SUnit*, 4> NotReady;
|
|
|
|
DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
|
|
|
|
Sequence.reserve(SUnits.size());
|
|
|
|
while (!AvailableQueue.empty()) {
|
|
|
|
bool Delayed = false;
|
|
|
|
LRegsMap.clear();
|
|
|
|
SUnit *CurSU = AvailableQueue.pop();
|
|
|
|
while (CurSU) {
|
2008-11-18 03:52:36 +08:00
|
|
|
SmallVector<unsigned, 4> LRegs;
|
|
|
|
if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
|
|
|
|
break;
|
|
|
|
Delayed = true;
|
|
|
|
LRegsMap.insert(std::make_pair(CurSU, LRegs));
|
2008-09-19 00:26:26 +08:00
|
|
|
|
|
|
|
CurSU->isPending = true; // This SU is not in AvailableQueue right now.
|
|
|
|
NotReady.push_back(CurSU);
|
|
|
|
CurSU = AvailableQueue.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// All candidates are delayed due to live physical reg dependencies.
|
|
|
|
// Try code duplication or inserting cross class copies
|
|
|
|
// to resolve it.
|
|
|
|
if (Delayed && !CurSU) {
|
|
|
|
if (!CurSU) {
|
|
|
|
// Try duplicating the nodes that produces these
|
|
|
|
// "expensive to copy" values to break the dependency. In case even
|
|
|
|
// that doesn't work, insert cross class copies.
|
|
|
|
SUnit *TrySU = NotReady[0];
|
2013-07-14 12:42:23 +08:00
|
|
|
SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU];
|
2008-09-19 00:26:26 +08:00
|
|
|
assert(LRegs.size() == 1 && "Can't handle this yet!");
|
|
|
|
unsigned Reg = LRegs[0];
|
|
|
|
SUnit *LRDef = LiveRegDefs[Reg];
|
2014-11-17 05:17:18 +08:00
|
|
|
MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
|
2009-01-12 11:19:55 +08:00
|
|
|
const TargetRegisterClass *RC =
|
2010-06-29 22:02:34 +08:00
|
|
|
TRI->getMinimalPhysRegClass(Reg, VT);
|
2009-01-12 11:19:55 +08:00
|
|
|
const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
|
|
|
|
|
2011-03-10 08:16:32 +08:00
|
|
|
// If cross copy register class is the same as RC, then it must be
|
|
|
|
// possible copy the value directly. Do not try duplicate the def.
|
|
|
|
// If cross copy register class is not the same as RC, then it's
|
|
|
|
// possible to copy the value but it require cross register class copies
|
|
|
|
// and it is expensive.
|
|
|
|
// If cross copy register class is null, then it's not possible to copy
|
|
|
|
// the value at all.
|
2014-04-14 08:51:57 +08:00
|
|
|
SUnit *NewDef = nullptr;
|
2011-03-10 08:16:32 +08:00
|
|
|
if (DestRC != RC) {
|
2009-01-12 11:19:55 +08:00
|
|
|
NewDef = CopyAndMoveSuccessors(LRDef);
|
2011-03-10 08:16:32 +08:00
|
|
|
if (!DestRC && !NewDef)
|
|
|
|
report_fatal_error("Can't handle live physical "
|
|
|
|
"register dependency!");
|
|
|
|
}
|
2008-09-19 00:26:26 +08:00
|
|
|
if (!NewDef) {
|
2009-01-12 11:19:55 +08:00
|
|
|
// Issue copies, these can be expensive cross register class copies.
|
2008-09-19 00:26:26 +08:00
|
|
|
SmallVector<SUnit*, 2> Copies;
|
2009-01-12 11:19:55 +08:00
|
|
|
InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Adding an edge from SU # " << TrySU->NodeNum
|
|
|
|
<< " to SU #" << Copies.front()->NodeNum << "\n");
|
2012-11-06 11:13:46 +08:00
|
|
|
AddPred(TrySU, SDep(Copies.front(), SDep::Artificial));
|
2008-09-19 00:26:26 +08:00
|
|
|
NewDef = Copies.back();
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Adding an edge from SU # " << NewDef->NodeNum
|
|
|
|
<< " to SU #" << TrySU->NodeNum << "\n");
|
2008-09-19 00:26:26 +08:00
|
|
|
LiveRegDefs[Reg] = NewDef;
|
2012-11-06 11:13:46 +08:00
|
|
|
AddPred(NewDef, SDep(TrySU, SDep::Artificial));
|
2008-09-19 00:26:26 +08:00
|
|
|
TrySU->isAvailable = false;
|
|
|
|
CurSU = NewDef;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CurSU) {
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("Unable to resolve live physical register dependencies!");
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the nodes that aren't ready back onto the available list.
|
|
|
|
for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
|
|
|
|
NotReady[i]->isPending = false;
|
|
|
|
// May no longer be available due to backtracking.
|
|
|
|
if (NotReady[i]->isAvailable)
|
|
|
|
AvailableQueue.push(NotReady[i]);
|
|
|
|
}
|
|
|
|
NotReady.clear();
|
|
|
|
|
2008-11-21 08:10:42 +08:00
|
|
|
if (CurSU)
|
2008-09-19 00:26:26 +08:00
|
|
|
ScheduleNodeBottomUp(CurSU, CurCycle);
|
|
|
|
++CurCycle;
|
|
|
|
}
|
|
|
|
|
2009-09-29 00:09:41 +08:00
|
|
|
// Reverse the order since it is bottom up.
|
2008-09-19 00:26:26 +08:00
|
|
|
std::reverse(Sequence.begin(), Sequence.end());
|
2009-09-29 00:09:41 +08:00
|
|
|
|
2008-09-19 00:26:26 +08:00
|
|
|
#ifndef NDEBUG
|
2012-03-07 13:21:36 +08:00
|
|
|
VerifyScheduledSequence(/*isBottomUp=*/true);
|
2008-09-19 00:26:26 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-10-18 03:39:36 +08:00
|
|
|
|
2012-10-20 20:53:26 +08:00
|
|
|
namespace {
|
2012-10-18 03:39:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ScheduleDAGLinearize - No scheduling scheduler, it simply linearize the
|
|
|
|
// DAG in topological order.
|
|
|
|
// IMPORTANT: this may not work for targets with phyreg dependency.
|
|
|
|
//
|
|
|
|
class ScheduleDAGLinearize : public ScheduleDAGSDNodes {
|
|
|
|
public:
|
|
|
|
ScheduleDAGLinearize(MachineFunction &mf) : ScheduleDAGSDNodes(mf) {}
|
|
|
|
|
2014-03-08 14:31:39 +08:00
|
|
|
void Schedule() override;
|
2012-10-18 03:39:36 +08:00
|
|
|
|
2014-03-08 14:31:39 +08:00
|
|
|
MachineBasicBlock *
|
|
|
|
EmitSchedule(MachineBasicBlock::iterator &InsertPos) override;
|
2012-10-18 03:39:36 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<SDNode*> Sequence;
|
|
|
|
DenseMap<SDNode*, SDNode*> GluedMap; // Cache glue to its user
|
|
|
|
|
|
|
|
void ScheduleNode(SDNode *N);
|
|
|
|
};
|
2012-10-20 20:53:26 +08:00
|
|
|
} // end anonymous namespace
|
2012-10-18 03:39:36 +08:00
|
|
|
|
|
|
|
void ScheduleDAGLinearize::ScheduleNode(SDNode *N) {
|
|
|
|
if (N->getNodeId() != 0)
|
2014-04-14 08:51:57 +08:00
|
|
|
llvm_unreachable(nullptr);
|
2012-10-18 03:39:36 +08:00
|
|
|
|
|
|
|
if (!N->isMachineOpcode() &&
|
|
|
|
(N->getOpcode() == ISD::EntryToken || isPassiveNode(N)))
|
|
|
|
// These nodes do not need to be translated into MIs.
|
|
|
|
return;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n*** Scheduling: ");
|
|
|
|
LLVM_DEBUG(N->dump(DAG));
|
2012-10-18 03:39:36 +08:00
|
|
|
Sequence.push_back(N);
|
|
|
|
|
|
|
|
unsigned NumOps = N->getNumOperands();
|
|
|
|
if (unsigned NumLeft = NumOps) {
|
2014-04-14 08:51:57 +08:00
|
|
|
SDNode *GluedOpN = nullptr;
|
2012-10-18 03:39:36 +08:00
|
|
|
do {
|
|
|
|
const SDValue &Op = N->getOperand(NumLeft-1);
|
|
|
|
SDNode *OpN = Op.getNode();
|
|
|
|
|
|
|
|
if (NumLeft == NumOps && Op.getValueType() == MVT::Glue) {
|
|
|
|
// Schedule glue operand right above N.
|
|
|
|
GluedOpN = OpN;
|
|
|
|
assert(OpN->getNodeId() != 0 && "Glue operand not ready?");
|
|
|
|
OpN->setNodeId(0);
|
|
|
|
ScheduleNode(OpN);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OpN == GluedOpN)
|
|
|
|
// Glue operand is already scheduled.
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DenseMap<SDNode*, SDNode*>::iterator DI = GluedMap.find(OpN);
|
|
|
|
if (DI != GluedMap.end() && DI->second != N)
|
|
|
|
// Users of glues are counted against the glued users.
|
|
|
|
OpN = DI->second;
|
|
|
|
|
|
|
|
unsigned Degree = OpN->getNodeId();
|
|
|
|
assert(Degree > 0 && "Predecessor over-released!");
|
|
|
|
OpN->setNodeId(--Degree);
|
|
|
|
if (Degree == 0)
|
|
|
|
ScheduleNode(OpN);
|
|
|
|
} while (--NumLeft);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// findGluedUser - Find the representative use of a glue value by walking
|
|
|
|
/// the use chain.
|
|
|
|
static SDNode *findGluedUser(SDNode *N) {
|
|
|
|
while (SDNode *Glued = N->getGluedUser())
|
|
|
|
N = Glued;
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleDAGLinearize::Schedule() {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "********** DAG Linearization **********\n");
|
2012-10-18 03:39:36 +08:00
|
|
|
|
|
|
|
SmallVector<SDNode*, 8> Glues;
|
|
|
|
unsigned DAGSize = 0;
|
2015-07-15 06:10:54 +08:00
|
|
|
for (SDNode &Node : DAG->allnodes()) {
|
|
|
|
SDNode *N = &Node;
|
2012-10-18 03:39:36 +08:00
|
|
|
|
|
|
|
// Use node id to record degree.
|
|
|
|
unsigned Degree = N->use_size();
|
|
|
|
N->setNodeId(Degree);
|
|
|
|
unsigned NumVals = N->getNumValues();
|
|
|
|
if (NumVals && N->getValueType(NumVals-1) == MVT::Glue &&
|
|
|
|
N->hasAnyUseOfValue(NumVals-1)) {
|
|
|
|
SDNode *User = findGluedUser(N);
|
|
|
|
if (User) {
|
|
|
|
Glues.push_back(N);
|
|
|
|
GluedMap.insert(std::make_pair(N, User));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (N->isMachineOpcode() ||
|
|
|
|
(N->getOpcode() != ISD::EntryToken && !isPassiveNode(N)))
|
|
|
|
++DAGSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Glues.size(); i != e; ++i) {
|
|
|
|
SDNode *Glue = Glues[i];
|
|
|
|
SDNode *GUser = GluedMap[Glue];
|
|
|
|
unsigned Degree = Glue->getNodeId();
|
|
|
|
unsigned UDegree = GUser->getNodeId();
|
|
|
|
|
|
|
|
// Glue user must be scheduled together with the glue operand. So other
|
|
|
|
// users of the glue operand must be treated as its users.
|
|
|
|
SDNode *ImmGUser = Glue->getGluedUser();
|
2017-05-04 21:35:17 +08:00
|
|
|
for (const SDNode *U : Glue->uses())
|
|
|
|
if (U == ImmGUser)
|
2012-10-18 03:39:36 +08:00
|
|
|
--Degree;
|
|
|
|
GUser->setNodeId(UDegree + Degree);
|
|
|
|
Glue->setNodeId(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sequence.reserve(DAGSize);
|
|
|
|
ScheduleNode(DAG->getRoot().getNode());
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock*
|
|
|
|
ScheduleDAGLinearize::EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
|
|
|
|
InstrEmitter Emitter(BB, InsertPos);
|
|
|
|
DenseMap<SDValue, unsigned> VRBaseMap;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({ dbgs() << "\n*** Final schedule ***\n"; });
|
2012-10-18 03:39:36 +08:00
|
|
|
|
|
|
|
unsigned NumNodes = Sequence.size();
|
2018-03-03 06:59:51 +08:00
|
|
|
MachineBasicBlock *BB = Emitter.getBlock();
|
2012-10-18 03:39:36 +08:00
|
|
|
for (unsigned i = 0; i != NumNodes; ++i) {
|
|
|
|
SDNode *N = Sequence[NumNodes-i-1];
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(N->dump(DAG));
|
2012-10-18 03:39:36 +08:00
|
|
|
Emitter.EmitNode(N, false, false, VRBaseMap);
|
2018-03-03 06:59:51 +08:00
|
|
|
|
|
|
|
// Emit any debug values associated with the node.
|
|
|
|
if (N->getHasDebugValue()) {
|
|
|
|
MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
|
|
|
|
for (auto DV : DAG->GetDbgValues(N)) {
|
|
|
|
if (DV->isInvalidated())
|
|
|
|
continue;
|
|
|
|
if (auto *DbgMI = Emitter.EmitDbgValue(DV, VRBaseMap))
|
|
|
|
BB->insert(InsertPos, DbgMI);
|
|
|
|
DV->setIsInvalidated();
|
|
|
|
}
|
|
|
|
}
|
2012-10-18 03:39:36 +08:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << '\n');
|
2012-10-18 03:39:36 +08:00
|
|
|
|
|
|
|
InsertPos = Emitter.getInsertPos();
|
|
|
|
return Emitter.getBlock();
|
|
|
|
}
|
|
|
|
|
2008-09-19 00:26:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Public Constructor Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-02-11 12:27:20 +08:00
|
|
|
llvm::ScheduleDAGSDNodes *
|
2009-04-30 07:29:43 +08:00
|
|
|
llvm::createFastDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
|
2009-01-16 03:20:50 +08:00
|
|
|
return new ScheduleDAGFast(*IS->MF);
|
2008-09-19 00:26:26 +08:00
|
|
|
}
|
2012-10-18 03:39:36 +08:00
|
|
|
|
|
|
|
llvm::ScheduleDAGSDNodes *
|
|
|
|
llvm::createDAGLinearizer(SelectionDAGISel *IS, CodeGenOpt::Level) {
|
|
|
|
return new ScheduleDAGLinearize(*IS->MF);
|
|
|
|
}
|