2008-11-20 07:18:57 +08: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"
|
2010-03-15 03:56:39 +08:00
|
|
|
#include "SDNodeDbgValue.h"
|
2009-02-07 01:22:58 +08:00
|
|
|
#include "ScheduleDAGSDNodes.h"
|
2009-10-10 09:32:21 +08:00
|
|
|
#include "InstrEmitter.h"
|
2008-11-20 07:18:57 +08:00
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2010-05-21 07:26:43 +08:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2008-11-20 07:18:57 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2009-08-14 00:05:04 +08:00
|
|
|
#include "llvm/Target/TargetSubtarget.h"
|
2010-01-22 11:36:51 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2010-03-25 09:38:16 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2010-01-22 11:36:51 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-11-20 07:18:57 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-01-22 11:36:51 +08:00
|
|
|
STATISTIC(LoadsClustered, "Number of loads clustered together");
|
|
|
|
|
2009-01-16 03:20:50 +08:00
|
|
|
ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
|
|
|
|
: ScheduleDAG(mf) {
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
2009-02-11 12:27:20 +08:00
|
|
|
/// Run - perform scheduling.
|
|
|
|
///
|
|
|
|
void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb,
|
|
|
|
MachineBasicBlock::iterator insertPos) {
|
|
|
|
DAG = dag;
|
|
|
|
ScheduleDAG::Run(bb, insertPos);
|
|
|
|
}
|
|
|
|
|
2010-05-21 07:26:43 +08:00
|
|
|
/// NewSUnit - Creates a new SUnit and return a ptr to it.
|
|
|
|
///
|
|
|
|
SUnit *ScheduleDAGSDNodes::NewSUnit(SDNode *N) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
const SUnit *Addr = 0;
|
|
|
|
if (!SUnits.empty())
|
|
|
|
Addr = &SUnits[0];
|
|
|
|
#endif
|
|
|
|
SUnits.push_back(SUnit(N, (unsigned)SUnits.size()));
|
|
|
|
assert((Addr == 0 || Addr == &SUnits[0]) &&
|
|
|
|
"SUnits std::vector reallocated on the fly!");
|
|
|
|
SUnits.back().OrigNode = &SUnits.back();
|
|
|
|
SUnit *SU = &SUnits.back();
|
|
|
|
const TargetLowering &TLI = DAG->getTargetLoweringInfo();
|
2010-05-29 07:26:21 +08:00
|
|
|
if (N->isMachineOpcode() &&
|
|
|
|
N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF)
|
|
|
|
SU->SchedulingPref = Sched::None;
|
|
|
|
else
|
|
|
|
SU->SchedulingPref = TLI.getSchedulingPreference(N);
|
2010-05-21 07:26:43 +08:00
|
|
|
return SU;
|
|
|
|
}
|
|
|
|
|
2008-11-20 07:18:57 +08: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-24 00:10:52 +08:00
|
|
|
SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
|
2010-05-21 07:26:43 +08:00
|
|
|
SU->SchedulingPref = Old->SchedulingPref;
|
2009-01-17 04:57:18 +08:00
|
|
|
Old->isCloned = true;
|
2008-11-20 07:18:57 +08: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 11:19:55 +08:00
|
|
|
/// register and the cost of copying the register.
|
2008-11-20 07:18:57 +08:00
|
|
|
static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
|
|
|
|
const TargetRegisterInfo *TRI,
|
|
|
|
const TargetInstrInfo *TII,
|
2009-01-12 11:19:55 +08:00
|
|
|
unsigned &PhysReg, int &Cost) {
|
2008-11-20 07:18:57 +08: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 11:19:55 +08:00
|
|
|
II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
|
2008-11-20 07:18:57 +08:00
|
|
|
PhysReg = Reg;
|
2009-01-12 11:19:55 +08:00
|
|
|
const TargetRegisterClass *RC =
|
2010-06-29 22:02:34 +08:00
|
|
|
TRI->getMinimalPhysRegClass(Reg, Def->getValueType(ResNo));
|
2009-01-12 11:19:55 +08:00
|
|
|
Cost = RC->getCopyCost();
|
|
|
|
}
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-22 11:36:51 +08:00
|
|
|
static void AddFlags(SDNode *N, SDValue Flag, bool AddFlag,
|
|
|
|
SelectionDAG *DAG) {
|
|
|
|
SmallVector<EVT, 4> VTs;
|
2010-06-25 06:00:37 +08:00
|
|
|
SDNode *FlagDestNode = Flag.getNode();
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2010-06-25 06:00:37 +08:00
|
|
|
// Don't add a flag from a node to itself.
|
|
|
|
if (FlagDestNode == N) return;
|
|
|
|
|
|
|
|
// Don't add a flag to something which already has a flag.
|
|
|
|
if (N->getValueType(N->getNumValues() - 1) == MVT::Flag) return;
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
|
|
|
|
VTs.push_back(N->getValueType(I));
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2010-01-22 11:36:51 +08:00
|
|
|
if (AddFlag)
|
|
|
|
VTs.push_back(MVT::Flag);
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2010-01-22 11:36:51 +08:00
|
|
|
SmallVector<SDValue, 4> Ops;
|
2010-06-25 06:00:37 +08:00
|
|
|
for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
|
|
|
|
Ops.push_back(N->getOperand(I));
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2010-06-25 06:00:37 +08:00
|
|
|
if (FlagDestNode)
|
2010-01-22 11:36:51 +08:00
|
|
|
Ops.push_back(Flag);
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2010-01-22 11:36:51 +08:00
|
|
|
SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size());
|
2010-06-24 02:16:24 +08:00
|
|
|
MachineSDNode::mmo_iterator Begin = 0, End = 0;
|
|
|
|
MachineSDNode *MN = dyn_cast<MachineSDNode>(N);
|
|
|
|
|
|
|
|
// Store memory references.
|
|
|
|
if (MN) {
|
|
|
|
Begin = MN->memoperands_begin();
|
|
|
|
End = MN->memoperands_end();
|
|
|
|
}
|
|
|
|
|
2010-01-22 11:36:51 +08:00
|
|
|
DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size());
|
2010-06-24 02:16:24 +08:00
|
|
|
|
|
|
|
// Reset the memory references
|
|
|
|
if (MN)
|
|
|
|
MN->setMemRefs(Begin, End);
|
2010-01-22 11:36:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ClusterNeighboringLoads - Force nearby loads together by "flagging" them.
|
|
|
|
/// This function finds loads of the same base and different offsets. If the
|
|
|
|
/// offsets are not far apart (target specific), it add MVT::Flag inputs and
|
|
|
|
/// outputs to ensure they are scheduled together and in order. This
|
|
|
|
/// optimization may benefit some targets by improving cache locality.
|
2010-06-10 10:09:31 +08:00
|
|
|
void ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) {
|
|
|
|
SDNode *Chain = 0;
|
|
|
|
unsigned NumOps = Node->getNumOperands();
|
|
|
|
if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
|
|
|
|
Chain = Node->getOperand(NumOps-1).getNode();
|
|
|
|
if (!Chain)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Look for other loads of the same chain. Find loads that are loading from
|
|
|
|
// the same base pointer and different offsets.
|
2010-01-22 11:36:51 +08:00
|
|
|
SmallPtrSet<SDNode*, 16> Visited;
|
|
|
|
SmallVector<int64_t, 4> Offsets;
|
|
|
|
DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode.
|
2010-06-10 10:09:31 +08:00
|
|
|
bool Cluster = false;
|
|
|
|
SDNode *Base = Node;
|
|
|
|
for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
SDNode *User = *I;
|
|
|
|
if (User == Node || !Visited.insert(User))
|
2010-01-22 11:36:51 +08:00
|
|
|
continue;
|
2010-06-10 10:09:31 +08:00
|
|
|
int64_t Offset1, Offset2;
|
|
|
|
if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
|
|
|
|
Offset1 == Offset2)
|
|
|
|
// FIXME: Should be ok if they addresses are identical. But earlier
|
|
|
|
// optimizations really should have eliminated one of the loads.
|
2010-01-22 11:36:51 +08:00
|
|
|
continue;
|
2010-06-10 10:09:31 +08:00
|
|
|
if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
|
|
|
|
Offsets.push_back(Offset1);
|
|
|
|
O2SMap.insert(std::make_pair(Offset2, User));
|
|
|
|
Offsets.push_back(Offset2);
|
2010-06-25 22:48:39 +08:00
|
|
|
if (Offset2 < Offset1)
|
2010-06-10 10:09:31 +08:00
|
|
|
Base = User;
|
|
|
|
Cluster = true;
|
|
|
|
}
|
2010-01-22 11:36:51 +08:00
|
|
|
|
2010-06-10 10:09:31 +08:00
|
|
|
if (!Cluster)
|
|
|
|
return;
|
2010-01-22 11:36:51 +08:00
|
|
|
|
2010-06-10 10:09:31 +08:00
|
|
|
// Sort them in increasing order.
|
|
|
|
std::sort(Offsets.begin(), Offsets.end());
|
2010-01-22 11:36:51 +08:00
|
|
|
|
2010-06-10 10:09:31 +08:00
|
|
|
// Check if the loads are close enough.
|
|
|
|
SmallVector<SDNode*, 4> Loads;
|
|
|
|
unsigned NumLoads = 0;
|
|
|
|
int64_t BaseOff = Offsets[0];
|
|
|
|
SDNode *BaseLoad = O2SMap[BaseOff];
|
|
|
|
Loads.push_back(BaseLoad);
|
|
|
|
for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
|
|
|
|
int64_t Offset = Offsets[i];
|
|
|
|
SDNode *Load = O2SMap[Offset];
|
|
|
|
if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads))
|
|
|
|
break; // Stop right here. Ignore loads that are further away.
|
|
|
|
Loads.push_back(Load);
|
|
|
|
++NumLoads;
|
|
|
|
}
|
2010-01-22 11:36:51 +08:00
|
|
|
|
2010-06-10 10:09:31 +08:00
|
|
|
if (NumLoads == 0)
|
|
|
|
return;
|
2010-01-22 11:36:51 +08:00
|
|
|
|
2010-06-10 10:09:31 +08:00
|
|
|
// Cluster loads by adding MVT::Flag outputs and inputs. This also
|
|
|
|
// ensure they are scheduled in order of increasing addresses.
|
|
|
|
SDNode *Lead = Loads[0];
|
2010-06-25 06:00:37 +08:00
|
|
|
AddFlags(Lead, SDValue(0, 0), true, DAG);
|
|
|
|
|
|
|
|
SDValue InFlag = SDValue(Lead, Lead->getNumValues() - 1);
|
|
|
|
for (unsigned I = 1, E = Loads.size(); I != E; ++I) {
|
|
|
|
bool OutFlag = I < E - 1;
|
|
|
|
SDNode *Load = Loads[I];
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2010-06-10 10:09:31 +08:00
|
|
|
AddFlags(Load, InFlag, OutFlag, DAG);
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2010-06-10 10:09:31 +08:00
|
|
|
if (OutFlag)
|
2010-06-25 06:00:37 +08:00
|
|
|
InFlag = SDValue(Load, Load->getNumValues() - 1);
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2010-06-10 10:09:31 +08:00
|
|
|
++LoadsClustered;
|
|
|
|
}
|
|
|
|
}
|
2010-01-22 11:36:51 +08:00
|
|
|
|
2010-06-10 10:09:31 +08:00
|
|
|
/// ClusterNodes - Cluster certain nodes which should be scheduled together.
|
|
|
|
///
|
|
|
|
void ScheduleDAGSDNodes::ClusterNodes() {
|
|
|
|
for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
|
|
|
|
E = DAG->allnodes_end(); NI != E; ++NI) {
|
|
|
|
SDNode *Node = &*NI;
|
|
|
|
if (!Node || !Node->isMachineOpcode())
|
2010-01-22 11:36:51 +08:00
|
|
|
continue;
|
|
|
|
|
2010-06-10 10:09:31 +08:00
|
|
|
unsigned Opc = Node->getMachineOpcode();
|
|
|
|
const TargetInstrDesc &TID = TII->get(Opc);
|
|
|
|
if (TID.mayLoad())
|
|
|
|
// Cluster loads from "near" addresses into combined SUnits.
|
|
|
|
ClusterNeighboringLoads(Node);
|
2010-01-22 11:36:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-20 07:18:57 +08: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-24 01:24:50 +08:00
|
|
|
unsigned NumNodes = 0;
|
2008-11-20 07:18:57 +08:00
|
|
|
for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
|
2008-12-24 01:24:50 +08:00
|
|
|
E = DAG->allnodes_end(); NI != E; ++NI) {
|
2008-11-20 07:18:57 +08:00
|
|
|
NI->setNodeId(-1);
|
2008-12-24 01:24:50 +08:00
|
|
|
++NumNodes;
|
|
|
|
}
|
2008-11-20 07:18:57 +08:00
|
|
|
|
2008-12-24 01:24:50 +08: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);
|
|
|
|
|
2010-02-24 14:11:37 +08:00
|
|
|
// Add all nodes in depth first order.
|
|
|
|
SmallVector<SDNode*, 64> Worklist;
|
|
|
|
SmallPtrSet<SDNode*, 64> Visited;
|
|
|
|
Worklist.push_back(DAG->getRoot().getNode());
|
|
|
|
Visited.insert(DAG->getRoot().getNode());
|
|
|
|
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
SDNode *NI = Worklist.pop_back_val();
|
|
|
|
|
|
|
|
// Add all operands to the worklist unless they've already been added.
|
|
|
|
for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i)
|
|
|
|
if (Visited.insert(NI->getOperand(i).getNode()))
|
|
|
|
Worklist.push_back(NI->getOperand(i).getNode());
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
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-21 04:42:23 +08:00
|
|
|
// are required to be the last operand and result of a node.
|
2008-11-20 07:18:57 +08:00
|
|
|
|
|
|
|
// Scan up to find flagged preds.
|
|
|
|
SDNode *N = NI;
|
2009-03-21 04:42:23 +08:00
|
|
|
while (N->getNumOperands() &&
|
2009-08-12 04:47:22 +08:00
|
|
|
N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
|
2009-03-21 04:42:23 +08:00
|
|
|
N = N->getOperand(N->getNumOperands()-1).getNode();
|
|
|
|
assert(N->getNodeId() == -1 && "Node already inserted!");
|
|
|
|
N->setNodeId(NodeSUnit->NodeNum);
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan down to find any flagged succs.
|
|
|
|
N = NI;
|
2009-08-12 04:47:22 +08:00
|
|
|
while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
|
2008-11-20 07:18:57 +08: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 09:44:51 +08:00
|
|
|
// Assign the Latency field of NodeSUnit using target-provided information.
|
2010-05-20 06:42:23 +08:00
|
|
|
ComputeLatency(NodeSUnit);
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
2008-12-24 02:36:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleDAGSDNodes::AddSchedEdges() {
|
2009-08-14 00:05:04 +08:00
|
|
|
const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
|
|
|
|
|
2009-08-20 00:08:58 +08:00
|
|
|
// Check to see if the scheduler cares about latencies.
|
|
|
|
bool UnitLatencies = ForceUnitLatencies();
|
|
|
|
|
2008-11-20 07:18:57 +08: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-24 00:10:52 +08:00
|
|
|
TII->get(N->getMachineOpcode()).getImplicitDefs()) {
|
|
|
|
SU->hasPhysRegClobbers = true;
|
2009-10-10 09:32:21 +08:00
|
|
|
unsigned NumUsed = InstrEmitter::CountResults(N);
|
2009-03-24 01:39:36 +08: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-24 00:10:52 +08:00
|
|
|
SU->hasPhysRegDefs = true;
|
|
|
|
}
|
2008-11-20 07:18:57 +08: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 06:56:29 +08:00
|
|
|
EVT OpVT = N->getOperand(i).getValueType();
|
2009-08-12 04:47:22 +08:00
|
|
|
assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
|
|
|
|
bool isChain = OpVT == MVT::Other;
|
2008-11-20 07:18:57 +08:00
|
|
|
|
|
|
|
unsigned PhysReg = 0;
|
2009-01-12 11:19:55 +08:00
|
|
|
int Cost = 1;
|
2008-11-20 07:18:57 +08:00
|
|
|
// Determine if this is a physical register dependency.
|
2009-01-12 11:19:55 +08:00
|
|
|
CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
|
2008-12-10 06:54:47 +08:00
|
|
|
assert((PhysReg == 0 || !isChain) &&
|
|
|
|
"Chain dependence via physreg data?");
|
2009-01-12 11:19:55 +08: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-14 00:05:04 +08:00
|
|
|
|
2010-05-29 07:26:21 +08:00
|
|
|
// If this is a ctrl dep, latency is 1.
|
|
|
|
unsigned OpLatency = isChain ? 1 : OpSU->Latency;
|
|
|
|
const SDep &dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data,
|
|
|
|
OpLatency, PhysReg);
|
2009-08-20 00:08:58 +08:00
|
|
|
if (!isChain && !UnitLatencies) {
|
2010-05-20 14:13:19 +08:00
|
|
|
ComputeOperandLatency(OpN, N, i, const_cast<SDep &>(dep));
|
2010-04-18 01:42:52 +08:00
|
|
|
ST.adjustSchedDependency(OpSU, SU, const_cast<SDep &>(dep));
|
2009-08-20 00:08:58 +08:00
|
|
|
}
|
2009-08-14 00:05:04 +08:00
|
|
|
|
|
|
|
SU->addPred(dep);
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-24 02:36:58 +08: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 07:33:48 +08:00
|
|
|
void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
|
2010-06-10 10:09:31 +08:00
|
|
|
// Cluster certain nodes which should be scheduled together.
|
|
|
|
ClusterNodes();
|
2008-12-24 02:36:58 +08:00
|
|
|
// Populate the SUnits array.
|
|
|
|
BuildSchedUnits();
|
|
|
|
// Compute all the scheduling dependencies between nodes.
|
|
|
|
AddSchedEdges();
|
|
|
|
}
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) {
|
2010-05-20 06:42:23 +08:00
|
|
|
// Check to see if the scheduler cares about latencies.
|
|
|
|
if (ForceUnitLatencies()) {
|
|
|
|
SU->Latency = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
|
2010-05-20 14:13:19 +08:00
|
|
|
if (InstrItins.isEmpty()) {
|
|
|
|
SU->Latency = 1;
|
|
|
|
return;
|
|
|
|
}
|
2008-11-20 07:18:57 +08:00
|
|
|
|
|
|
|
// 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 08:12:10 +08:00
|
|
|
for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
|
2008-11-20 07:18:57 +08:00
|
|
|
if (N->isMachineOpcode()) {
|
2009-08-20 00:08:58 +08:00
|
|
|
SU->Latency += InstrItins.
|
|
|
|
getStageLatency(TII->get(N->getMachineOpcode()).getSchedClass());
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-20 14:13:19 +08:00
|
|
|
void ScheduleDAGSDNodes::ComputeOperandLatency(SDNode *Def, SDNode *Use,
|
|
|
|
unsigned OpIdx, SDep& dep) const{
|
|
|
|
// Check to see if the scheduler cares about latencies.
|
|
|
|
if (ForceUnitLatencies())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
|
|
|
|
if (InstrItins.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (dep.getKind() != SDep::Data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned DefIdx = Use->getOperand(OpIdx).getResNo();
|
2010-05-29 07:26:21 +08:00
|
|
|
if (Def->isMachineOpcode()) {
|
2010-05-20 14:13:19 +08:00
|
|
|
const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
|
|
|
|
if (DefIdx >= II.getNumDefs())
|
|
|
|
return;
|
|
|
|
int DefCycle = InstrItins.getOperandCycle(II.getSchedClass(), DefIdx);
|
|
|
|
if (DefCycle < 0)
|
|
|
|
return;
|
2010-05-29 07:26:21 +08:00
|
|
|
int UseCycle = 1;
|
|
|
|
if (Use->isMachineOpcode()) {
|
|
|
|
const unsigned UseClass = TII->get(Use->getMachineOpcode()).getSchedClass();
|
|
|
|
UseCycle = InstrItins.getOperandCycle(UseClass, OpIdx);
|
|
|
|
}
|
2010-05-20 14:13:19 +08:00
|
|
|
if (UseCycle >= 0) {
|
|
|
|
int Latency = DefCycle - UseCycle + 1;
|
|
|
|
if (Latency >= 0)
|
|
|
|
dep.setLatency(Latency);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
|
2009-01-12 11:19:55 +08:00
|
|
|
if (!SU->getNode()) {
|
2010-01-05 09:25:11 +08:00
|
|
|
dbgs() << "PHYS REG COPY\n";
|
2009-01-12 11:19:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SU->getNode()->dump(DAG);
|
2010-01-05 09:25:11 +08:00
|
|
|
dbgs() << "\n";
|
2008-11-20 07:18:57 +08:00
|
|
|
SmallVector<SDNode *, 4> FlaggedNodes;
|
|
|
|
for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
|
|
|
|
FlaggedNodes.push_back(N);
|
|
|
|
while (!FlaggedNodes.empty()) {
|
2010-01-05 09:25:11 +08:00
|
|
|
dbgs() << " ";
|
2008-11-20 07:18:57 +08:00
|
|
|
FlaggedNodes.back()->dump(DAG);
|
2010-01-05 09:25:11 +08:00
|
|
|
dbgs() << "\n";
|
2008-11-20 07:18:57 +08:00
|
|
|
FlaggedNodes.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2009-10-10 09:32:21 +08:00
|
|
|
|
2010-03-25 09:38:16 +08:00
|
|
|
namespace {
|
|
|
|
struct OrderSorter {
|
|
|
|
bool operator()(const std::pair<unsigned, MachineInstr*> &A,
|
|
|
|
const std::pair<unsigned, MachineInstr*> &B) {
|
|
|
|
return A.first < B.first;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessSourceNode - Process nodes with source order numbers. These are added
|
2010-07-01 05:27:56 +08:00
|
|
|
// to a vector which EmitSchedule uses to determine how to insert dbg_value
|
2010-03-25 09:38:16 +08:00
|
|
|
// instructions in the right order.
|
|
|
|
static void ProcessSourceNode(SDNode *N, SelectionDAG *DAG,
|
|
|
|
InstrEmitter &Emitter,
|
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap,
|
|
|
|
SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
|
|
|
|
SmallSet<unsigned, 8> &Seen) {
|
|
|
|
unsigned Order = DAG->GetOrdering(N);
|
|
|
|
if (!Order || !Seen.insert(Order))
|
|
|
|
return;
|
|
|
|
|
|
|
|
MachineBasicBlock *BB = Emitter.getBlock();
|
2010-07-10 17:00:22 +08:00
|
|
|
if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI()) {
|
2010-03-25 09:38:16 +08:00
|
|
|
// Did not insert any instruction.
|
|
|
|
Orders.push_back(std::make_pair(Order, (MachineInstr*)0));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-10 17:00:22 +08:00
|
|
|
Orders.push_back(std::make_pair(Order, prior(Emitter.getInsertPos())));
|
2010-03-25 09:38:16 +08:00
|
|
|
if (!N->getHasDebugValue())
|
|
|
|
return;
|
|
|
|
// Opportunistically insert immediate dbg_value uses, i.e. those with source
|
|
|
|
// order number right after the N.
|
|
|
|
MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
|
|
|
|
SmallVector<SDDbgValue*,2> &DVs = DAG->GetDbgValues(N);
|
|
|
|
for (unsigned i = 0, e = DVs.size(); i != e; ++i) {
|
|
|
|
if (DVs[i]->isInvalidated())
|
|
|
|
continue;
|
|
|
|
unsigned DVOrder = DVs[i]->getOrder();
|
|
|
|
if (DVOrder == ++Order) {
|
2010-05-01 03:35:33 +08:00
|
|
|
MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap);
|
2010-04-26 15:38:55 +08:00
|
|
|
if (DbgMI) {
|
|
|
|
Orders.push_back(std::make_pair(DVOrder, DbgMI));
|
|
|
|
BB->insert(InsertPos, DbgMI);
|
|
|
|
}
|
2010-03-25 09:38:16 +08:00
|
|
|
DVs[i]->setIsInvalidated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-10 09:32:21 +08:00
|
|
|
/// EmitSchedule - Emit the machine code in scheduled order.
|
2010-05-01 08:01:06 +08:00
|
|
|
MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
|
2009-10-10 09:32:21 +08:00
|
|
|
InstrEmitter Emitter(BB, InsertPos);
|
|
|
|
DenseMap<SDValue, unsigned> VRBaseMap;
|
|
|
|
DenseMap<SUnit*, unsigned> CopyVRBaseMap;
|
2010-03-25 09:38:16 +08:00
|
|
|
SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders;
|
|
|
|
SmallSet<unsigned, 8> Seen;
|
|
|
|
bool HasDbg = DAG->hasDebugValues();
|
2010-03-11 06:13:47 +08:00
|
|
|
|
2010-04-27 04:06:49 +08:00
|
|
|
// If this is the first BB, emit byval parameter dbg_value's.
|
|
|
|
if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) {
|
|
|
|
SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin();
|
|
|
|
SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd();
|
|
|
|
for (; PDI != PDE; ++PDI) {
|
2010-05-01 03:35:33 +08:00
|
|
|
MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap);
|
2010-04-27 04:06:49 +08:00
|
|
|
if (DbgMI)
|
2010-07-10 17:00:22 +08:00
|
|
|
BB->insert(InsertPos, DbgMI);
|
2010-04-27 04:06:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-10 09:32:21 +08:00
|
|
|
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()) {
|
2010-03-25 09:38:16 +08:00
|
|
|
SDNode *N = FlaggedNodes.back();
|
2009-10-10 09:32:21 +08:00
|
|
|
Emitter.EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,
|
2010-05-01 08:01:06 +08:00
|
|
|
VRBaseMap);
|
2010-04-27 04:06:49 +08:00
|
|
|
// Remember the source order of the inserted instruction.
|
2010-03-25 09:38:16 +08:00
|
|
|
if (HasDbg)
|
2010-05-01 03:35:33 +08:00
|
|
|
ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen);
|
2009-10-10 09:32:21 +08:00
|
|
|
FlaggedNodes.pop_back();
|
|
|
|
}
|
|
|
|
Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned,
|
2010-05-01 08:01:06 +08:00
|
|
|
VRBaseMap);
|
2010-04-27 04:06:49 +08:00
|
|
|
// Remember the source order of the inserted instruction.
|
2010-03-25 09:38:16 +08:00
|
|
|
if (HasDbg)
|
2010-05-01 03:35:33 +08:00
|
|
|
ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders,
|
2010-03-25 09:38:16 +08:00
|
|
|
Seen);
|
|
|
|
}
|
|
|
|
|
2010-04-27 04:06:49 +08:00
|
|
|
// Insert all the dbg_values which have not already been inserted in source
|
2010-03-25 09:38:16 +08:00
|
|
|
// order sequence.
|
|
|
|
if (HasDbg) {
|
2010-07-10 17:00:22 +08:00
|
|
|
MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI();
|
2010-03-25 09:38:16 +08:00
|
|
|
|
|
|
|
// Sort the source order instructions and use the order to insert debug
|
|
|
|
// values.
|
|
|
|
std::sort(Orders.begin(), Orders.end(), OrderSorter());
|
|
|
|
|
|
|
|
SDDbgInfo::DbgIterator DI = DAG->DbgBegin();
|
|
|
|
SDDbgInfo::DbgIterator DE = DAG->DbgEnd();
|
|
|
|
// Now emit the rest according to source order.
|
|
|
|
unsigned LastOrder = 0;
|
|
|
|
for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) {
|
|
|
|
unsigned Order = Orders[i].first;
|
|
|
|
MachineInstr *MI = Orders[i].second;
|
|
|
|
// Insert all SDDbgValue's whose order(s) are before "Order".
|
|
|
|
if (!MI)
|
|
|
|
continue;
|
2010-03-25 15:16:57 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
unsigned LastDIOrder = 0;
|
|
|
|
#endif
|
2010-03-25 09:38:16 +08:00
|
|
|
for (; DI != DE &&
|
|
|
|
(*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) {
|
2010-03-25 15:16:57 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert((*DI)->getOrder() >= LastDIOrder &&
|
|
|
|
"SDDbgValue nodes must be in source order!");
|
|
|
|
LastDIOrder = (*DI)->getOrder();
|
|
|
|
#endif
|
2010-03-25 09:38:16 +08:00
|
|
|
if ((*DI)->isInvalidated())
|
|
|
|
continue;
|
2010-05-01 03:35:33 +08:00
|
|
|
MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap);
|
2010-04-26 15:38:55 +08:00
|
|
|
if (DbgMI) {
|
|
|
|
if (!LastOrder)
|
|
|
|
// Insert to start of the BB (after PHIs).
|
|
|
|
BB->insert(BBBegin, DbgMI);
|
|
|
|
else {
|
2010-07-11 06:42:31 +08:00
|
|
|
// Insert at the instruction, which may be in a different
|
|
|
|
// block, if the block was split by a custom inserter.
|
2010-04-26 15:38:55 +08:00
|
|
|
MachineBasicBlock::iterator Pos = MI;
|
2010-07-11 06:42:31 +08:00
|
|
|
MI->getParent()->insert(llvm::next(Pos), DbgMI);
|
2010-04-26 15:38:55 +08:00
|
|
|
}
|
2010-03-25 09:38:16 +08:00
|
|
|
}
|
2010-03-11 06:13:47 +08:00
|
|
|
}
|
2010-03-25 09:38:16 +08:00
|
|
|
LastOrder = Order;
|
|
|
|
}
|
|
|
|
// Add trailing DbgValue's before the terminator. FIXME: May want to add
|
|
|
|
// some of them before one or more conditional branches?
|
|
|
|
while (DI != DE) {
|
|
|
|
MachineBasicBlock *InsertBB = Emitter.getBlock();
|
|
|
|
MachineBasicBlock::iterator Pos= Emitter.getBlock()->getFirstTerminator();
|
|
|
|
if (!(*DI)->isInvalidated()) {
|
2010-05-01 03:35:33 +08:00
|
|
|
MachineInstr *DbgMI= Emitter.EmitDbgValue(*DI, VRBaseMap);
|
2010-04-26 15:38:55 +08:00
|
|
|
if (DbgMI)
|
|
|
|
InsertBB->insert(Pos, DbgMI);
|
2010-03-25 09:38:16 +08:00
|
|
|
}
|
|
|
|
++DI;
|
|
|
|
}
|
2009-10-10 09:32:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BB = Emitter.getBlock();
|
|
|
|
InsertPos = Emitter.getInsertPos();
|
|
|
|
return BB;
|
|
|
|
}
|