2008-11-20 00:18:57 +01:00
|
|
|
//===--- ScheduleDAGSDNodes.cpp - Implement the ScheduleDAGSDNodes class --===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements the ScheduleDAG class, which is a base class used by
|
|
|
|
// scheduling implementation classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "pre-RA-sched"
|
2009-02-06 18:22:58 +01:00
|
|
|
#include "ScheduleDAGSDNodes.h"
|
2009-10-10 03:32:21 +02:00
|
|
|
#include "InstrEmitter.h"
|
2008-11-20 00:18:57 +01:00
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2009-08-13 18:05:04 +02:00
|
|
|
#include "llvm/Target/TargetSubtarget.h"
|
2008-11-20 00:18:57 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-01-15 20:20:50 +01:00
|
|
|
ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
|
|
|
|
: ScheduleDAG(mf) {
|
2008-11-20 00:18:57 +01:00
|
|
|
}
|
|
|
|
|
2009-02-11 05:27:20 +01:00
|
|
|
/// Run - perform scheduling.
|
|
|
|
///
|
|
|
|
void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb,
|
|
|
|
MachineBasicBlock::iterator insertPos) {
|
|
|
|
DAG = dag;
|
|
|
|
ScheduleDAG::Run(bb, insertPos);
|
|
|
|
}
|
|
|
|
|
2008-11-20 00:18:57 +01:00
|
|
|
SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
|
|
|
|
SUnit *SU = NewSUnit(Old->getNode());
|
|
|
|
SU->OrigNode = Old->OrigNode;
|
|
|
|
SU->Latency = Old->Latency;
|
|
|
|
SU->isTwoAddress = Old->isTwoAddress;
|
|
|
|
SU->isCommutable = Old->isCommutable;
|
|
|
|
SU->hasPhysRegDefs = Old->hasPhysRegDefs;
|
2009-03-23 17:10:52 +01:00
|
|
|
SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
|
2009-01-16 21:57:18 +01:00
|
|
|
Old->isCloned = true;
|
2008-11-20 00:18:57 +01:00
|
|
|
return SU;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckForPhysRegDependency - Check if the dependency between def and use of
|
|
|
|
/// a specified operand is a physical register dependency. If so, returns the
|
2009-01-12 04:19:55 +01:00
|
|
|
/// register and the cost of copying the register.
|
2008-11-20 00:18:57 +01:00
|
|
|
static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
|
|
|
|
const TargetRegisterInfo *TRI,
|
|
|
|
const TargetInstrInfo *TII,
|
2009-01-12 04:19:55 +01:00
|
|
|
unsigned &PhysReg, int &Cost) {
|
2008-11-20 00:18:57 +01:00
|
|
|
if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned ResNo = User->getOperand(2).getResNo();
|
|
|
|
if (Def->isMachineOpcode()) {
|
|
|
|
const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
|
|
|
|
if (ResNo >= II.getNumDefs() &&
|
2009-01-12 04:19:55 +01:00
|
|
|
II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
|
2008-11-20 00:18:57 +01:00
|
|
|
PhysReg = Reg;
|
2009-01-12 04:19:55 +01:00
|
|
|
const TargetRegisterClass *RC =
|
|
|
|
TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
|
|
|
|
Cost = RC->getCopyCost();
|
|
|
|
}
|
2008-11-20 00:18:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleDAGSDNodes::BuildSchedUnits() {
|
|
|
|
// During scheduling, the NodeId field of SDNode is used to map SDNodes
|
|
|
|
// to their associated SUnits by holding SUnits table indices. A value
|
|
|
|
// of -1 means the SDNode does not yet have an associated SUnit.
|
2008-12-23 18:24:50 +01:00
|
|
|
unsigned NumNodes = 0;
|
2008-11-20 00:18:57 +01:00
|
|
|
for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
|
2008-12-23 18:24:50 +01:00
|
|
|
E = DAG->allnodes_end(); NI != E; ++NI) {
|
2008-11-20 00:18:57 +01:00
|
|
|
NI->setNodeId(-1);
|
2008-12-23 18:24:50 +01:00
|
|
|
++NumNodes;
|
|
|
|
}
|
2008-11-20 00:18:57 +01:00
|
|
|
|
2008-12-23 18:24:50 +01:00
|
|
|
// Reserve entries in the vector for each of the SUnits we are creating. This
|
|
|
|
// ensure that reallocation of the vector won't happen, so SUnit*'s won't get
|
|
|
|
// invalidated.
|
|
|
|
// FIXME: Multiply by 2 because we may clone nodes during scheduling.
|
|
|
|
// This is a temporary workaround.
|
|
|
|
SUnits.reserve(NumNodes * 2);
|
|
|
|
|
2008-12-16 04:25:46 +01:00
|
|
|
// Check to see if the scheduler cares about latencies.
|
|
|
|
bool UnitLatencies = ForceUnitLatencies();
|
|
|
|
|
2008-11-20 00:18:57 +01:00
|
|
|
for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
|
|
|
|
E = DAG->allnodes_end(); NI != E; ++NI) {
|
|
|
|
if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this node has already been processed, stop now.
|
|
|
|
if (NI->getNodeId() != -1) continue;
|
|
|
|
|
|
|
|
SUnit *NodeSUnit = NewSUnit(NI);
|
|
|
|
|
|
|
|
// See if anything is flagged to this node, if so, add them to flagged
|
|
|
|
// nodes. Nodes can have at most one flag input and one flag output. Flags
|
2009-03-20 21:42:23 +01:00
|
|
|
// are required to be the last operand and result of a node.
|
2008-11-20 00:18:57 +01:00
|
|
|
|
|
|
|
// Scan up to find flagged preds.
|
|
|
|
SDNode *N = NI;
|
2009-03-20 21:42:23 +01:00
|
|
|
while (N->getNumOperands() &&
|
2009-08-11 22:47:22 +02:00
|
|
|
N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
|
2009-03-20 21:42:23 +01:00
|
|
|
N = N->getOperand(N->getNumOperands()-1).getNode();
|
|
|
|
assert(N->getNodeId() == -1 && "Node already inserted!");
|
|
|
|
N->setNodeId(NodeSUnit->NodeNum);
|
2008-11-20 00:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan down to find any flagged succs.
|
|
|
|
N = NI;
|
2009-08-11 22:47:22 +02:00
|
|
|
while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
|
2008-11-20 00:18:57 +01:00
|
|
|
SDValue FlagVal(N, N->getNumValues()-1);
|
|
|
|
|
|
|
|
// There are either zero or one users of the Flag result.
|
|
|
|
bool HasFlagUse = false;
|
|
|
|
for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
|
|
|
|
UI != E; ++UI)
|
|
|
|
if (FlagVal.isOperandOf(*UI)) {
|
|
|
|
HasFlagUse = true;
|
|
|
|
assert(N->getNodeId() == -1 && "Node already inserted!");
|
|
|
|
N->setNodeId(NodeSUnit->NodeNum);
|
|
|
|
N = *UI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!HasFlagUse) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are flag operands involved, N is now the bottom-most node
|
|
|
|
// of the sequence of nodes that are flagged together.
|
|
|
|
// Update the SUnit.
|
|
|
|
NodeSUnit->setNode(N);
|
|
|
|
assert(N->getNodeId() == -1 && "Node already inserted!");
|
|
|
|
N->setNodeId(NodeSUnit->NodeNum);
|
|
|
|
|
2008-11-21 02:44:51 +01:00
|
|
|
// Assign the Latency field of NodeSUnit using target-provided information.
|
2008-12-16 04:25:46 +01:00
|
|
|
if (UnitLatencies)
|
|
|
|
NodeSUnit->Latency = 1;
|
|
|
|
else
|
|
|
|
ComputeLatency(NodeSUnit);
|
2008-11-20 00:18:57 +01:00
|
|
|
}
|
2008-12-23 19:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleDAGSDNodes::AddSchedEdges() {
|
2009-08-13 18:05:04 +02:00
|
|
|
const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
|
|
|
|
|
2009-08-19 18:08:58 +02:00
|
|
|
// Check to see if the scheduler cares about latencies.
|
|
|
|
bool UnitLatencies = ForceUnitLatencies();
|
|
|
|
|
2008-11-20 00:18:57 +01:00
|
|
|
// Pass 2: add the preds, succs, etc.
|
|
|
|
for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
|
|
|
|
SUnit *SU = &SUnits[su];
|
|
|
|
SDNode *MainNode = SU->getNode();
|
|
|
|
|
|
|
|
if (MainNode->isMachineOpcode()) {
|
|
|
|
unsigned Opc = MainNode->getMachineOpcode();
|
|
|
|
const TargetInstrDesc &TID = TII->get(Opc);
|
|
|
|
for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
|
|
|
|
if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
|
|
|
|
SU->isTwoAddress = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (TID.isCommutable())
|
|
|
|
SU->isCommutable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all predecessors and successors of the group.
|
|
|
|
for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
|
|
|
|
if (N->isMachineOpcode() &&
|
2009-03-23 17:10:52 +01:00
|
|
|
TII->get(N->getMachineOpcode()).getImplicitDefs()) {
|
|
|
|
SU->hasPhysRegClobbers = true;
|
2009-10-10 03:32:21 +02:00
|
|
|
unsigned NumUsed = InstrEmitter::CountResults(N);
|
2009-03-23 18:39:36 +01:00
|
|
|
while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
|
|
|
|
--NumUsed; // Skip over unused values at the end.
|
|
|
|
if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
|
2009-03-23 17:10:52 +01:00
|
|
|
SU->hasPhysRegDefs = true;
|
|
|
|
}
|
2008-11-20 00:18:57 +01:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
|
|
|
|
SDNode *OpN = N->getOperand(i).getNode();
|
|
|
|
if (isPassiveNode(OpN)) continue; // Not scheduled.
|
|
|
|
SUnit *OpSU = &SUnits[OpN->getNodeId()];
|
|
|
|
assert(OpSU && "Node has no SUnit!");
|
|
|
|
if (OpSU == SU) continue; // In the same group.
|
|
|
|
|
2009-08-11 00:56:29 +02:00
|
|
|
EVT OpVT = N->getOperand(i).getValueType();
|
2009-08-11 22:47:22 +02:00
|
|
|
assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
|
|
|
|
bool isChain = OpVT == MVT::Other;
|
2008-11-20 00:18:57 +01:00
|
|
|
|
|
|
|
unsigned PhysReg = 0;
|
2009-01-12 04:19:55 +01:00
|
|
|
int Cost = 1;
|
2008-11-20 00:18:57 +01:00
|
|
|
// Determine if this is a physical register dependency.
|
2009-01-12 04:19:55 +01:00
|
|
|
CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
|
2008-12-09 23:54:47 +01:00
|
|
|
assert((PhysReg == 0 || !isChain) &&
|
|
|
|
"Chain dependence via physreg data?");
|
2009-01-12 04:19:55 +01:00
|
|
|
// FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
|
|
|
|
// emits a copy from the physical register to a virtual register unless
|
|
|
|
// it requires a cross class copy (cost < 0). That means we are only
|
|
|
|
// treating "expensive to copy" register dependency as physical register
|
|
|
|
// dependency. This may change in the future though.
|
|
|
|
if (Cost >= 0)
|
|
|
|
PhysReg = 0;
|
2009-08-13 18:05:04 +02:00
|
|
|
|
|
|
|
const SDep& dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
|
|
|
|
OpSU->Latency, PhysReg);
|
2009-08-19 18:08:58 +02:00
|
|
|
if (!isChain && !UnitLatencies) {
|
|
|
|
ComputeOperandLatency(OpSU, SU, (SDep &)dep);
|
|
|
|
ST.adjustSchedDependency(OpSU, SU, (SDep &)dep);
|
|
|
|
}
|
2009-08-13 18:05:04 +02:00
|
|
|
|
|
|
|
SU->addPred(dep);
|
2008-11-20 00:18:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-23 19:36:58 +01:00
|
|
|
/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
|
|
|
|
/// are input. This SUnit graph is similar to the SelectionDAG, but
|
|
|
|
/// excludes nodes that aren't interesting to scheduling, and represents
|
|
|
|
/// flagged together nodes with a single SUnit.
|
2009-10-10 01:33:48 +02:00
|
|
|
void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
|
2008-12-23 19:36:58 +01:00
|
|
|
// Populate the SUnits array.
|
|
|
|
BuildSchedUnits();
|
|
|
|
// Compute all the scheduling dependencies between nodes.
|
|
|
|
AddSchedEdges();
|
|
|
|
}
|
|
|
|
|
2008-11-20 00:18:57 +01:00
|
|
|
void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
|
|
|
|
const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
|
|
|
|
|
|
|
|
// Compute the latency for the node. We use the sum of the latencies for
|
|
|
|
// all nodes flagged together into this SUnit.
|
|
|
|
SU->Latency = 0;
|
2008-11-21 01:12:10 +01:00
|
|
|
for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
|
2008-11-20 00:18:57 +01:00
|
|
|
if (N->isMachineOpcode()) {
|
2009-08-19 18:08:58 +02:00
|
|
|
SU->Latency += InstrItins.
|
|
|
|
getStageLatency(TII->get(N->getMachineOpcode()).getSchedClass());
|
2008-11-20 00:18:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
|
2009-01-12 04:19:55 +01:00
|
|
|
if (!SU->getNode()) {
|
2009-07-24 12:36:58 +02:00
|
|
|
errs() << "PHYS REG COPY\n";
|
2009-01-12 04:19:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SU->getNode()->dump(DAG);
|
2009-07-24 12:36:58 +02:00
|
|
|
errs() << "\n";
|
2008-11-20 00:18:57 +01:00
|
|
|
SmallVector<SDNode *, 4> FlaggedNodes;
|
|
|
|
for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
|
|
|
|
FlaggedNodes.push_back(N);
|
|
|
|
while (!FlaggedNodes.empty()) {
|
2009-07-24 12:36:58 +02:00
|
|
|
errs() << " ";
|
2008-11-20 00:18:57 +01:00
|
|
|
FlaggedNodes.back()->dump(DAG);
|
2009-07-24 12:36:58 +02:00
|
|
|
errs() << "\n";
|
2008-11-20 00:18:57 +01:00
|
|
|
FlaggedNodes.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2009-10-10 03:32:21 +02:00
|
|
|
|
|
|
|
/// EmitSchedule - Emit the machine code in scheduled order.
|
|
|
|
MachineBasicBlock *ScheduleDAGSDNodes::
|
|
|
|
EmitSchedule(DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) {
|
|
|
|
InstrEmitter Emitter(BB, InsertPos);
|
|
|
|
DenseMap<SDValue, unsigned> VRBaseMap;
|
|
|
|
DenseMap<SUnit*, unsigned> CopyVRBaseMap;
|
|
|
|
for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
|
|
|
|
SUnit *SU = Sequence[i];
|
|
|
|
if (!SU) {
|
|
|
|
// Null SUnit* is a noop.
|
|
|
|
EmitNoop();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For pre-regalloc scheduling, create instructions corresponding to the
|
|
|
|
// SDNode and any flagged SDNodes and append them to the block.
|
|
|
|
if (!SU->getNode()) {
|
|
|
|
// Emit a copy.
|
|
|
|
EmitPhysRegCopy(SU, CopyVRBaseMap);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<SDNode *, 4> FlaggedNodes;
|
|
|
|
for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
|
|
|
|
N = N->getFlaggedNode())
|
|
|
|
FlaggedNodes.push_back(N);
|
|
|
|
while (!FlaggedNodes.empty()) {
|
|
|
|
Emitter.EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,
|
|
|
|
VRBaseMap, EM);
|
|
|
|
FlaggedNodes.pop_back();
|
|
|
|
}
|
|
|
|
Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned,
|
|
|
|
VRBaseMap, EM);
|
|
|
|
}
|
|
|
|
|
|
|
|
BB = Emitter.getBlock();
|
|
|
|
InsertPos = Emitter.getInsertPos();
|
|
|
|
return BB;
|
|
|
|
}
|