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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-02-07 01:22:58 +08:00
|
|
|
#include "ScheduleDAGSDNodes.h"
|
2009-10-10 09:32:21 +08:00
|
|
|
#include "InstrEmitter.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "SDNodeDbgValue.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"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/SelectionDAG.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/TargetLowering.h"
|
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCInstrItineraries.h"
|
2011-03-05 16:00:22 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2008-11-20 07:18:57 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "pre-RA-sched"
|
|
|
|
|
2010-01-22 11:36:51 +08:00
|
|
|
STATISTIC(LoadsClustered, "Number of loads clustered together");
|
|
|
|
|
2014-10-08 01:38:33 +08:00
|
|
|
// This allows the latency-based scheduler to notice high latency instructions
|
2014-10-08 01:36:50 +08:00
|
|
|
// without a target itinerary. The choice of number here has more to do with
|
2014-10-08 01:38:33 +08:00
|
|
|
// balancing scheduler heuristics than with the actual machine latency.
|
2011-03-05 16:00:22 +08:00
|
|
|
static cl::opt<int> HighLatencyCycles(
|
|
|
|
"sched-high-latency-cycles", cl::Hidden, cl::init(10),
|
|
|
|
cl::desc("Roughly estimate the number of cycles that 'long latency'"
|
|
|
|
"instructions take for targets with no itinerary"));
|
|
|
|
|
2009-01-16 03:20:50 +08:00
|
|
|
ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
|
2014-08-05 05:25:23 +08:00
|
|
|
: ScheduleDAG(mf), BB(nullptr), DAG(nullptr),
|
2014-08-05 10:39:49 +08:00
|
|
|
InstrItins(mf.getSubtarget().getInstrItineraryData()) {}
|
2008-11-20 07:18:57 +08:00
|
|
|
|
2009-02-11 12:27:20 +08:00
|
|
|
/// Run - perform scheduling.
|
|
|
|
///
|
2012-03-07 13:21:52 +08:00
|
|
|
void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb) {
|
|
|
|
BB = bb;
|
2009-02-11 12:27:20 +08:00
|
|
|
DAG = dag;
|
2012-03-07 13:21:52 +08:00
|
|
|
|
|
|
|
// Clear the scheduler's SUnit DAG.
|
|
|
|
ScheduleDAG::clearDAG();
|
|
|
|
Sequence.clear();
|
|
|
|
|
|
|
|
// Invoke the target's selection of scheduler.
|
|
|
|
Schedule();
|
2009-02-11 12:27:20 +08:00
|
|
|
}
|
|
|
|
|
2010-05-21 07:26:43 +08:00
|
|
|
/// NewSUnit - Creates a new SUnit and return a ptr to it.
|
|
|
|
///
|
2012-03-08 07:00:49 +08:00
|
|
|
SUnit *ScheduleDAGSDNodes::newSUnit(SDNode *N) {
|
2010-05-21 07:26:43 +08:00
|
|
|
#ifndef NDEBUG
|
2014-04-14 08:51:57 +08:00
|
|
|
const SUnit *Addr = nullptr;
|
2010-05-21 07:26:43 +08:00
|
|
|
if (!SUnits.empty())
|
|
|
|
Addr = &SUnits[0];
|
|
|
|
#endif
|
2015-05-30 03:43:39 +08:00
|
|
|
SUnits.emplace_back(N, (unsigned)SUnits.size());
|
2014-04-14 08:51:57 +08:00
|
|
|
assert((Addr == nullptr || Addr == &SUnits[0]) &&
|
2010-05-21 07:26:43 +08:00
|
|
|
"SUnits std::vector reallocated on the fly!");
|
|
|
|
SUnits.back().OrigNode = &SUnits.back();
|
|
|
|
SUnit *SU = &SUnits.back();
|
|
|
|
const TargetLowering &TLI = DAG->getTargetLoweringInfo();
|
2010-08-10 10:39:45 +08:00
|
|
|
if (!N ||
|
|
|
|
(N->isMachineOpcode() &&
|
|
|
|
N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF))
|
2010-05-29 07:26:21 +08:00
|
|
|
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) {
|
2012-03-08 07:00:49 +08:00
|
|
|
SUnit *SU = newSUnit(Old->getNode());
|
2008-11-20 07:18:57 +08:00
|
|
|
SU->OrigNode = Old->OrigNode;
|
|
|
|
SU->Latency = Old->Latency;
|
2011-04-08 03:54:57 +08:00
|
|
|
SU->isVRegCycle = Old->isVRegCycle;
|
2010-11-03 08:45:17 +08:00
|
|
|
SU->isCall = Old->isCall;
|
2011-04-27 05:31:35 +08:00
|
|
|
SU->isCallOp = Old->isCallOp;
|
2008-11-20 07:18:57 +08:00
|
|
|
SU->isTwoAddress = Old->isTwoAddress;
|
|
|
|
SU->isCommutable = Old->isCommutable;
|
|
|
|
SU->hasPhysRegDefs = Old->hasPhysRegDefs;
|
2009-03-24 00:10:52 +08:00
|
|
|
SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
|
2011-04-14 13:15:06 +08:00
|
|
|
SU->isScheduleHigh = Old->isScheduleHigh;
|
|
|
|
SU->isScheduleLow = Old->isScheduleLow;
|
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,
|
2011-02-04 07:00:17 +08:00
|
|
|
const TargetRegisterInfo *TRI,
|
2008-11-20 07:18:57 +08:00
|
|
|
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();
|
2014-10-24 06:31:48 +08:00
|
|
|
if (Def->getOpcode() == ISD::CopyFromReg &&
|
|
|
|
cast<RegisterSDNode>(Def->getOperand(1))->getReg() == Reg) {
|
|
|
|
PhysReg = Reg;
|
|
|
|
} else if (Def->isMachineOpcode()) {
|
2011-06-29 03:10:37 +08:00
|
|
|
const MCInstrDesc &II = TII->get(Def->getMachineOpcode());
|
2008-11-20 07:18:57 +08:00
|
|
|
if (ResNo >= II.getNumDefs() &&
|
2014-10-24 06:31:48 +08:00
|
|
|
II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg)
|
2008-11-20 07:18:57 +08:00
|
|
|
PhysReg = Reg;
|
2014-10-24 06:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PhysReg != 0) {
|
|
|
|
const TargetRegisterClass *RC =
|
2014-11-17 05:17:18 +08:00
|
|
|
TRI->getMinimalPhysRegClass(Reg, Def->getSimpleValueType(ResNo));
|
2014-10-24 06:31:48 +08:00
|
|
|
Cost = RC->getCopyCost();
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-28 09:03:23 +08:00
|
|
|
// Helper for AddGlue to clone node operands.
|
2015-02-17 23:29:18 +08:00
|
|
|
static void CloneNodeWithValues(SDNode *N, SelectionDAG *DAG, ArrayRef<EVT> VTs,
|
2012-04-28 09:03:23 +08:00
|
|
|
SDValue ExtraOper = SDValue()) {
|
2015-02-17 23:29:18 +08:00
|
|
|
SmallVector<SDValue, 8> Ops(N->op_begin(), N->op_end());
|
2012-04-28 09:03:23 +08:00
|
|
|
if (ExtraOper.getNode())
|
|
|
|
Ops.push_back(ExtraOper);
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2014-04-16 14:10:51 +08:00
|
|
|
SDVTList VTList = DAG->getVTList(VTs);
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineSDNode::mmo_iterator Begin = nullptr, End = nullptr;
|
2010-06-24 02:16:24 +08:00
|
|
|
MachineSDNode *MN = dyn_cast<MachineSDNode>(N);
|
|
|
|
|
|
|
|
// Store memory references.
|
|
|
|
if (MN) {
|
|
|
|
Begin = MN->memoperands_begin();
|
|
|
|
End = MN->memoperands_end();
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:21:16 +08:00
|
|
|
DAG->MorphNodeTo(N, N->getOpcode(), VTList, Ops);
|
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
|
|
|
}
|
|
|
|
|
2012-04-28 09:03:23 +08:00
|
|
|
static bool AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) {
|
|
|
|
SDNode *GlueDestNode = Glue.getNode();
|
|
|
|
|
|
|
|
// Don't add glue from a node to itself.
|
|
|
|
if (GlueDestNode == N) return false;
|
|
|
|
|
|
|
|
// Don't add a glue operand to something that already uses glue.
|
|
|
|
if (GlueDestNode &&
|
|
|
|
N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Don't add glue to something that already has a glue value.
|
|
|
|
if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return false;
|
|
|
|
|
2015-02-17 23:29:18 +08:00
|
|
|
SmallVector<EVT, 4> VTs(N->value_begin(), N->value_end());
|
2012-04-28 09:03:23 +08:00
|
|
|
if (AddGlue)
|
|
|
|
VTs.push_back(MVT::Glue);
|
|
|
|
|
|
|
|
CloneNodeWithValues(N, DAG, VTs, Glue);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup after unsuccessful AddGlue. Use the standard method of morphing the
|
|
|
|
// node even though simply shrinking the value list is sufficient.
|
|
|
|
static void RemoveUnusedGlue(SDNode *N, SelectionDAG *DAG) {
|
|
|
|
assert((N->getValueType(N->getNumValues() - 1) == MVT::Glue &&
|
|
|
|
!N->hasAnyUseOfValue(N->getNumValues() - 1)) &&
|
|
|
|
"expected an unused glue value");
|
|
|
|
|
2015-02-17 23:29:18 +08:00
|
|
|
CloneNodeWithValues(N, DAG,
|
|
|
|
makeArrayRef(N->value_begin(), N->getNumValues() - 1));
|
2012-04-28 09:03:23 +08:00
|
|
|
}
|
|
|
|
|
2010-12-24 01:24:32 +08:00
|
|
|
/// ClusterNeighboringLoads - Force nearby loads together by "gluing" them.
|
2010-01-22 11:36:51 +08:00
|
|
|
/// This function finds loads of the same base and different offsets. If the
|
2010-12-21 10:38:05 +08:00
|
|
|
/// offsets are not far apart (target specific), it add MVT::Glue inputs and
|
2010-01-22 11:36:51 +08:00
|
|
|
/// 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) {
|
2014-04-14 08:51:57 +08:00
|
|
|
SDNode *Chain = nullptr;
|
2010-06-10 10:09:31 +08:00
|
|
|
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;
|
2014-04-08 05:29:22 +08:00
|
|
|
// This algorithm requires a reasonably low use count before finding a match
|
|
|
|
// to avoid uselessly blowing up compile time in large blocks.
|
|
|
|
unsigned UseCount = 0;
|
2010-06-10 10:09:31 +08:00
|
|
|
for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
|
2014-04-08 05:29:22 +08:00
|
|
|
I != E && UseCount < 100; ++I, ++UseCount) {
|
2010-06-10 10:09:31 +08:00
|
|
|
SDNode *User = *I;
|
2014-11-19 15:49:26 +08:00
|
|
|
if (User == Node || !Visited.insert(User).second)
|
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;
|
2014-04-08 05:29:22 +08:00
|
|
|
// Reset UseCount to allow more matches.
|
|
|
|
UseCount = 0;
|
2010-06-10 10:09:31 +08:00
|
|
|
}
|
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.
|
2018-04-07 02:08:42 +08:00
|
|
|
llvm::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-12-21 10:38:05 +08:00
|
|
|
// Cluster loads by adding MVT::Glue outputs and inputs. This also
|
2010-06-10 10:09:31 +08:00
|
|
|
// ensure they are scheduled in order of increasing addresses.
|
|
|
|
SDNode *Lead = Loads[0];
|
2014-04-14 08:51:57 +08:00
|
|
|
SDValue InGlue = SDValue(nullptr, 0);
|
2012-04-28 09:03:23 +08:00
|
|
|
if (AddGlue(Lead, InGlue, true, DAG))
|
|
|
|
InGlue = SDValue(Lead, Lead->getNumValues() - 1);
|
2010-06-25 06:00:37 +08:00
|
|
|
for (unsigned I = 1, E = Loads.size(); I != E; ++I) {
|
2010-12-24 01:24:32 +08:00
|
|
|
bool OutGlue = I < E - 1;
|
2010-06-25 06:00:37 +08:00
|
|
|
SDNode *Load = Loads[I];
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2012-04-28 09:03:23 +08:00
|
|
|
// If AddGlue fails, we could leave an unsused glue value. This should not
|
|
|
|
// cause any
|
|
|
|
if (AddGlue(Load, InGlue, OutGlue, DAG)) {
|
|
|
|
if (OutGlue)
|
|
|
|
InGlue = SDValue(Load, Load->getNumValues() - 1);
|
2010-06-24 02:16:24 +08:00
|
|
|
|
2012-04-28 09:03:23 +08:00
|
|
|
++LoadsClustered;
|
|
|
|
}
|
|
|
|
else if (!OutGlue && InGlue.getNode())
|
|
|
|
RemoveUnusedGlue(InGlue.getNode(), DAG);
|
2010-06-10 10:09:31 +08:00
|
|
|
}
|
|
|
|
}
|
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() {
|
2015-07-15 06:10:54 +08:00
|
|
|
for (SDNode &NI : DAG->allnodes()) {
|
|
|
|
SDNode *Node = &NI;
|
2010-06-10 10:09:31 +08:00
|
|
|
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();
|
2011-06-29 03:10:37 +08:00
|
|
|
const MCInstrDesc &MCID = TII->get(Opc);
|
|
|
|
if (MCID.mayLoad())
|
2010-06-10 10:09:31 +08:00
|
|
|
// 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;
|
2015-07-15 06:10:54 +08:00
|
|
|
for (SDNode &NI : DAG->allnodes()) {
|
|
|
|
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);
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2010-02-24 14:11:37 +08:00
|
|
|
// Add all nodes in depth first order.
|
|
|
|
SmallVector<SDNode*, 64> Worklist;
|
2016-01-30 09:24:31 +08:00
|
|
|
SmallPtrSet<SDNode*, 32> Visited;
|
2010-02-24 14:11:37 +08:00
|
|
|
Worklist.push_back(DAG->getRoot().getNode());
|
|
|
|
Visited.insert(DAG->getRoot().getNode());
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2011-04-27 05:31:35 +08:00
|
|
|
SmallVector<SUnit*, 8> CallSUnits;
|
2010-02-24 14:11:37 +08:00
|
|
|
while (!Worklist.empty()) {
|
|
|
|
SDNode *NI = Worklist.pop_back_val();
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2010-02-24 14:11:37 +08:00
|
|
|
// Add all operands to the worklist unless they've already been added.
|
2015-06-27 03:18:49 +08:00
|
|
|
for (const SDValue &Op : NI->op_values())
|
|
|
|
if (Visited.insert(Op.getNode()).second)
|
|
|
|
Worklist.push_back(Op.getNode());
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
|
|
|
|
continue;
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
// If this node has already been processed, stop now.
|
|
|
|
if (NI->getNodeId() != -1) continue;
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2012-03-08 07:00:49 +08:00
|
|
|
SUnit *NodeSUnit = newSUnit(NI);
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2010-12-24 01:24:32 +08:00
|
|
|
// See if anything is glued to this node, if so, add them to glued
|
|
|
|
// nodes. Nodes can have at most one glue input and one glue output. Glue
|
|
|
|
// is required to be the last operand and result of a node.
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2010-12-24 01:24:32 +08:00
|
|
|
// Scan up to find glued preds.
|
2008-11-20 07:18:57 +08:00
|
|
|
SDNode *N = NI;
|
2009-03-21 04:42:23 +08:00
|
|
|
while (N->getNumOperands() &&
|
2010-12-21 10:38:05 +08:00
|
|
|
N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
|
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);
|
2010-11-03 08:45:17 +08:00
|
|
|
if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
|
|
|
|
NodeSUnit->isCall = true;
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2010-12-24 01:24:32 +08:00
|
|
|
// Scan down to find any glued succs.
|
2008-11-20 07:18:57 +08:00
|
|
|
N = NI;
|
2010-12-21 10:38:05 +08:00
|
|
|
while (N->getValueType(N->getNumValues()-1) == MVT::Glue) {
|
2010-12-24 01:24:32 +08:00
|
|
|
SDValue GlueVal(N, N->getNumValues()-1);
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2010-12-24 01:24:32 +08:00
|
|
|
// There are either zero or one users of the Glue result.
|
|
|
|
bool HasGlueUse = false;
|
2011-02-04 07:00:17 +08:00
|
|
|
for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
|
2008-11-20 07:18:57 +08:00
|
|
|
UI != E; ++UI)
|
2010-12-24 01:24:32 +08:00
|
|
|
if (GlueVal.isOperandOf(*UI)) {
|
|
|
|
HasGlueUse = true;
|
2008-11-20 07:18:57 +08:00
|
|
|
assert(N->getNodeId() == -1 && "Node already inserted!");
|
|
|
|
N->setNodeId(NodeSUnit->NodeNum);
|
|
|
|
N = *UI;
|
2010-11-03 08:45:17 +08:00
|
|
|
if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
|
|
|
|
NodeSUnit->isCall = true;
|
2008-11-20 07:18:57 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-12-24 01:24:32 +08:00
|
|
|
if (!HasGlueUse) break;
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2011-04-27 05:31:35 +08:00
|
|
|
if (NodeSUnit->isCall)
|
|
|
|
CallSUnits.push_back(NodeSUnit);
|
|
|
|
|
2011-04-14 13:15:06 +08:00
|
|
|
// Schedule zero-latency TokenFactor below any nodes that may increase the
|
|
|
|
// schedule height. Otherwise, ancestors of the TokenFactor may appear to
|
|
|
|
// have false stalls.
|
|
|
|
if (NI->getOpcode() == ISD::TokenFactor)
|
|
|
|
NodeSUnit->isScheduleLow = true;
|
|
|
|
|
2010-12-24 01:24:32 +08:00
|
|
|
// If there are glue operands involved, N is now the bottom-most node
|
|
|
|
// of the sequence of nodes that are glued together.
|
2008-11-20 07:18:57 +08:00
|
|
|
// Update the SUnit.
|
|
|
|
NodeSUnit->setNode(N);
|
|
|
|
assert(N->getNodeId() == -1 && "Node already inserted!");
|
|
|
|
N->setNodeId(NodeSUnit->NodeNum);
|
|
|
|
|
2011-02-04 11:18:17 +08:00
|
|
|
// Compute NumRegDefsLeft. This must be done before AddSchedEdges.
|
|
|
|
InitNumRegDefsLeft(NodeSUnit);
|
|
|
|
|
2008-11-21 09:44:51 +08:00
|
|
|
// Assign the Latency field of NodeSUnit using target-provided information.
|
2012-03-08 07:00:49 +08:00
|
|
|
computeLatency(NodeSUnit);
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
2011-04-27 05:31:35 +08:00
|
|
|
|
|
|
|
// Find all call operands.
|
|
|
|
while (!CallSUnits.empty()) {
|
|
|
|
SUnit *SU = CallSUnits.pop_back_val();
|
|
|
|
for (const SDNode *SUNode = SU->getNode(); SUNode;
|
|
|
|
SUNode = SUNode->getGluedNode()) {
|
|
|
|
if (SUNode->getOpcode() != ISD::CopyToReg)
|
|
|
|
continue;
|
|
|
|
SDNode *SrcN = SUNode->getOperand(2).getNode();
|
|
|
|
if (isPassiveNode(SrcN)) continue; // Not scheduled.
|
|
|
|
SUnit *SrcSU = &SUnits[SrcN->getNodeId()];
|
|
|
|
SrcSU->isCallOp = true;
|
|
|
|
}
|
|
|
|
}
|
2008-12-24 02:36:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleDAGSDNodes::AddSchedEdges() {
|
2014-10-09 14:28:06 +08:00
|
|
|
const TargetSubtargetInfo &ST = MF.getSubtarget();
|
2009-08-14 00:05:04 +08:00
|
|
|
|
2009-08-20 00:08:58 +08:00
|
|
|
// Check to see if the scheduler cares about latencies.
|
2012-03-08 07:00:49 +08:00
|
|
|
bool UnitLatencies = forceUnitLatencies();
|
2009-08-20 00:08:58 +08:00
|
|
|
|
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();
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
if (MainNode->isMachineOpcode()) {
|
|
|
|
unsigned Opc = MainNode->getMachineOpcode();
|
2011-06-29 03:10:37 +08:00
|
|
|
const MCInstrDesc &MCID = TII->get(Opc);
|
|
|
|
for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
|
|
|
|
if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
|
2008-11-20 07:18:57 +08:00
|
|
|
SU->isTwoAddress = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-06-29 03:10:37 +08:00
|
|
|
if (MCID.isCommutable())
|
2008-11-20 07:18:57 +08:00
|
|
|
SU->isCommutable = true;
|
|
|
|
}
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
// Find all predecessors and successors of the group.
|
2010-12-24 01:24:32 +08:00
|
|
|
for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
|
2008-11-20 07:18:57 +08:00
|
|
|
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;
|
|
|
|
}
|
2011-02-04 07:00:17 +08:00
|
|
|
|
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();
|
2010-12-24 01:24:32 +08:00
|
|
|
assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!");
|
2009-08-12 04:47:22 +08:00
|
|
|
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.
|
2011-06-16 01:16:12 +08:00
|
|
|
if (Cost >= 0 && !StressSched)
|
2009-01-12 11:19:55 +08:00
|
|
|
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.
|
2011-04-13 04:14:07 +08:00
|
|
|
unsigned OpLatency = isChain ? 1 : OpSU->Latency;
|
2011-04-13 08:38:32 +08:00
|
|
|
// Special-case TokenFactor chains as zero-latency.
|
|
|
|
if(isChain && OpN->getOpcode() == ISD::TokenFactor)
|
|
|
|
OpLatency = 0;
|
|
|
|
|
2012-11-06 11:13:46 +08:00
|
|
|
SDep Dep = isChain ? SDep(OpSU, SDep::Barrier)
|
|
|
|
: SDep(OpSU, SDep::Data, PhysReg);
|
|
|
|
Dep.setLatency(OpLatency);
|
2009-08-20 00:08:58 +08:00
|
|
|
if (!isChain && !UnitLatencies) {
|
2012-11-06 11:13:46 +08:00
|
|
|
computeOperandLatency(OpN, N, i, Dep);
|
|
|
|
ST.adjustSchedDependency(OpSU, SU, Dep);
|
2009-08-20 00:08:58 +08:00
|
|
|
}
|
2009-08-14 00:05:04 +08:00
|
|
|
|
2012-11-06 11:13:46 +08:00
|
|
|
if (!SU->addPred(Dep) && !Dep.isCtrl() && OpSU->NumRegDefsLeft > 1) {
|
2011-02-04 11:18:17 +08:00
|
|
|
// Multiple register uses are combined in the same SUnit. For example,
|
|
|
|
// we could have a set of glued nodes with all their defs consumed by
|
|
|
|
// another set of glued nodes. Register pressure tracking sees this as
|
|
|
|
// a single use, so to keep pressure balanced we reduce the defs.
|
2011-03-10 03:12:43 +08:00
|
|
|
//
|
|
|
|
// We can't tell (without more book-keeping) if this results from
|
|
|
|
// glued nodes or duplicate operands. As long as we don't reduce
|
|
|
|
// NumRegDefsLeft to zero, we handle the common cases well.
|
2011-02-04 11:18:17 +08:00
|
|
|
--OpSU->NumRegDefsLeft;
|
|
|
|
}
|
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
|
2010-12-24 01:24:32 +08:00
|
|
|
/// glued 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();
|
|
|
|
}
|
|
|
|
|
2011-02-04 11:18:17 +08:00
|
|
|
// Initialize NumNodeDefs for the current Node's opcode.
|
|
|
|
void ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() {
|
2011-03-09 03:35:47 +08:00
|
|
|
// Check for phys reg copy.
|
|
|
|
if (!Node)
|
|
|
|
return;
|
|
|
|
|
2011-02-04 11:18:17 +08:00
|
|
|
if (!Node->isMachineOpcode()) {
|
|
|
|
if (Node->getOpcode() == ISD::CopyFromReg)
|
|
|
|
NodeNumDefs = 1;
|
|
|
|
else
|
|
|
|
NodeNumDefs = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned POpc = Node->getMachineOpcode();
|
|
|
|
if (POpc == TargetOpcode::IMPLICIT_DEF) {
|
|
|
|
// No register need be allocated for this.
|
|
|
|
NodeNumDefs = 0;
|
|
|
|
return;
|
|
|
|
}
|
2015-01-14 09:07:03 +08:00
|
|
|
if (POpc == TargetOpcode::PATCHPOINT &&
|
|
|
|
Node->getValueType(0) == MVT::Other) {
|
|
|
|
// PATCHPOINT is defined to have one result, but it might really have none
|
|
|
|
// if we're not using CallingConv::AnyReg. Don't mistake the chain for a
|
|
|
|
// real definition.
|
|
|
|
NodeNumDefs = 0;
|
|
|
|
return;
|
|
|
|
}
|
2011-02-04 11:18:17 +08:00
|
|
|
unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs();
|
|
|
|
// Some instructions define regs that are not represented in the selection DAG
|
|
|
|
// (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues.
|
|
|
|
NodeNumDefs = std::min(Node->getNumValues(), NRegDefs);
|
|
|
|
DefIdx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a RegDefIter for this SUnit and find the first valid value.
|
|
|
|
ScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU,
|
|
|
|
const ScheduleDAGSDNodes *SD)
|
|
|
|
: SchedDAG(SD), Node(SU->getNode()), DefIdx(0), NodeNumDefs(0) {
|
|
|
|
InitNodeNumDefs();
|
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to the next valid value defined by the SUnit.
|
|
|
|
void ScheduleDAGSDNodes::RegDefIter::Advance() {
|
|
|
|
for (;Node;) { // Visit all glued nodes.
|
|
|
|
for (;DefIdx < NodeNumDefs; ++DefIdx) {
|
|
|
|
if (!Node->hasAnyUseOfValue(DefIdx))
|
|
|
|
continue;
|
2012-12-14 02:45:35 +08:00
|
|
|
ValueType = Node->getSimpleValueType(DefIdx);
|
2011-02-04 11:18:17 +08:00
|
|
|
++DefIdx;
|
|
|
|
return; // Found a normal regdef.
|
|
|
|
}
|
|
|
|
Node = Node->getGluedNode();
|
2014-04-14 08:51:57 +08:00
|
|
|
if (!Node) {
|
2011-02-04 11:18:17 +08:00
|
|
|
return; // No values left to visit.
|
|
|
|
}
|
|
|
|
InitNodeNumDefs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) {
|
|
|
|
assert(SU->NumRegDefsLeft == 0 && "expect a new node");
|
|
|
|
for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) {
|
|
|
|
assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected");
|
|
|
|
++SU->NumRegDefsLeft;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-08 07:00:49 +08:00
|
|
|
void ScheduleDAGSDNodes::computeLatency(SUnit *SU) {
|
2011-04-13 08:38:32 +08:00
|
|
|
SDNode *N = SU->getNode();
|
|
|
|
|
|
|
|
// TokenFactor operands are considered zero latency, and some schedulers
|
|
|
|
// (e.g. Top-Down list) may rely on the fact that operand latency is nonzero
|
|
|
|
// whenever node latency is nonzero.
|
|
|
|
if (N && N->getOpcode() == ISD::TokenFactor) {
|
|
|
|
SU->Latency = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-20 06:42:23 +08:00
|
|
|
// Check to see if the scheduler cares about latencies.
|
2012-03-08 07:00:49 +08:00
|
|
|
if (forceUnitLatencies()) {
|
2010-05-20 06:42:23 +08:00
|
|
|
SU->Latency = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-10 09:29:16 +08:00
|
|
|
if (!InstrItins || InstrItins->isEmpty()) {
|
2011-03-05 17:18:16 +08:00
|
|
|
if (N && N->isMachineOpcode() &&
|
|
|
|
TII->isHighLatencyDef(N->getMachineOpcode()))
|
2011-03-05 16:00:22 +08:00
|
|
|
SU->Latency = HighLatencyCycles;
|
|
|
|
else
|
|
|
|
SU->Latency = 1;
|
2010-05-20 14:13:19 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-02-04 07:00:17 +08:00
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
// Compute the latency for the node. We use the sum of the latencies for
|
2010-12-24 01:24:32 +08:00
|
|
|
// all nodes glued together into this SUnit.
|
2008-11-20 07:18:57 +08:00
|
|
|
SU->Latency = 0;
|
2010-12-24 01:24:32 +08:00
|
|
|
for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
|
2010-11-03 08:45:17 +08:00
|
|
|
if (N->isMachineOpcode())
|
|
|
|
SU->Latency += TII->getInstrLatency(InstrItins, N);
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
2012-03-08 07:00:49 +08:00
|
|
|
void ScheduleDAGSDNodes::computeOperandLatency(SDNode *Def, SDNode *Use,
|
2010-05-20 14:13:19 +08:00
|
|
|
unsigned OpIdx, SDep& dep) const{
|
|
|
|
// Check to see if the scheduler cares about latencies.
|
2012-03-08 07:00:49 +08:00
|
|
|
if (forceUnitLatencies())
|
2010-05-20 14:13:19 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (dep.getKind() != SDep::Data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned DefIdx = Use->getOperand(OpIdx).getResNo();
|
2010-10-28 14:47:08 +08:00
|
|
|
if (Use->isMachineOpcode())
|
|
|
|
// Adjust the use operand index by num of defs.
|
|
|
|
OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs();
|
2010-10-06 14:27:31 +08:00
|
|
|
int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx);
|
Avoiding overly aggressive latency scheduling. If the two nodes share an
operand and one of them has a single use that is a live out copy, favor the
one that is live out. Otherwise it will be difficult to eliminate the copy
if the instruction is a loop induction variable update. e.g.
BB:
sub r1, r3, #1
str r0, [r2, r3]
mov r3, r1
cmp
bne BB
=>
BB:
str r0, [r2, r3]
sub r3, r3, #1
cmp
bne BB
This fixed the recent 256.bzip2 regression.
llvm-svn: 117675
2010-10-30 02:09:28 +08:00
|
|
|
if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg &&
|
|
|
|
!BB->succ_empty()) {
|
|
|
|
unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
// This copy is a liveout value. It is likely coalesced, so reduce the
|
|
|
|
// latency so not to penalize the def.
|
|
|
|
// FIXME: need target specific adjustment here?
|
|
|
|
Latency = (Latency > 1) ? Latency - 1 : 1;
|
|
|
|
}
|
2010-09-30 06:42:35 +08:00
|
|
|
if (Latency >= 0)
|
|
|
|
dep.setLatency(Latency);
|
2010-05-20 14:13:19 +08:00
|
|
|
}
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
|
2017-01-28 10:02:38 +08:00
|
|
|
// Cannot completely remove virtual function even in release mode.
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
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";
|
2010-12-24 01:24:32 +08:00
|
|
|
SmallVector<SDNode *, 4> GluedNodes;
|
|
|
|
for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
|
|
|
|
GluedNodes.push_back(N);
|
|
|
|
while (!GluedNodes.empty()) {
|
2010-01-05 09:25:11 +08:00
|
|
|
dbgs() << " ";
|
2010-12-24 01:24:32 +08:00
|
|
|
GluedNodes.back()->dump(DAG);
|
2010-01-05 09:25:11 +08:00
|
|
|
dbgs() << "\n";
|
2010-12-24 01:24:32 +08:00
|
|
|
GluedNodes.pop_back();
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
2012-09-07 03:06:06 +08:00
|
|
|
#endif
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
2009-10-10 09:32:21 +08:00
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2012-03-07 13:21:40 +08:00
|
|
|
void ScheduleDAGSDNodes::dumpSchedule() const {
|
|
|
|
for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
|
|
|
|
if (SUnit *SU = Sequence[i])
|
|
|
|
SU->dump(this);
|
|
|
|
else
|
|
|
|
dbgs() << "**** NOOP ****\n";
|
|
|
|
}
|
|
|
|
}
|
2012-09-07 03:06:06 +08:00
|
|
|
#endif
|
2012-03-07 13:21:40 +08:00
|
|
|
|
2012-03-07 13:21:36 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
/// VerifyScheduledSequence - Verify that all SUnits were scheduled and that
|
|
|
|
/// their state is consistent with the nodes listed in Sequence.
|
|
|
|
///
|
|
|
|
void ScheduleDAGSDNodes::VerifyScheduledSequence(bool isBottomUp) {
|
|
|
|
unsigned ScheduledNodes = ScheduleDAG::VerifyScheduledDAG(isBottomUp);
|
|
|
|
unsigned Noops = 0;
|
|
|
|
for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
|
|
|
|
if (!Sequence[i])
|
|
|
|
++Noops;
|
|
|
|
assert(Sequence.size() - Noops == ScheduledNodes &&
|
|
|
|
"The number of nodes scheduled doesn't match the expected number!");
|
|
|
|
}
|
|
|
|
#endif // NDEBUG
|
|
|
|
|
2011-04-15 13:18:47 +08:00
|
|
|
/// ProcessSDDbgValues - Process SDDbgValues associated with this node.
|
2013-07-14 12:42:23 +08:00
|
|
|
static void
|
|
|
|
ProcessSDDbgValues(SDNode *N, SelectionDAG *DAG, InstrEmitter &Emitter,
|
|
|
|
SmallVectorImpl<std::pair<unsigned, MachineInstr*> > &Orders,
|
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap, unsigned Order) {
|
2011-01-27 02:20:04 +08:00
|
|
|
if (!N->getHasDebugValue())
|
|
|
|
return;
|
|
|
|
|
[DAG] Don't increase SDNodeOrder for dbg.value/declare.
Summary:
The SDNodeOrder is saved in the IROrder field in the SDNode, and this
field may affects scheduling. Thus, letting dbg.value/declare increase
the order numbers may in turn affect scheduling.
Because of this change we also need to update the code deciding when
dbg values should be output, in ScheduleDAGSDNodes.cpp/ProcessSDDbgValues.
Dbg values now have the same order as the SDNode they are connected to,
not the following orders.
Test cases provided by Florian Hahn.
Reviewers: bogner, aprantl, sunfish, atrick
Reviewed By: atrick
Subscribers: fhahn, probinson, andreadb, llvm-commits, MatzeB
Differential Revision: https://reviews.llvm.org/D25318
llvm-svn: 292485
2017-01-19 21:55:55 +08:00
|
|
|
// Opportunistically insert immediate dbg_value uses, i.e. those with the same
|
|
|
|
// source order number as N.
|
2011-01-27 02:20:04 +08:00
|
|
|
MachineBasicBlock *BB = Emitter.getBlock();
|
|
|
|
MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
|
2017-10-25 03:32:59 +08:00
|
|
|
for (auto DV : DAG->GetDbgValues(N)) {
|
|
|
|
if (DV->isInvalidated())
|
2011-01-27 02:20:04 +08:00
|
|
|
continue;
|
2017-10-25 03:32:59 +08:00
|
|
|
unsigned DVOrder = DV->getOrder();
|
[DAG] Don't increase SDNodeOrder for dbg.value/declare.
Summary:
The SDNodeOrder is saved in the IROrder field in the SDNode, and this
field may affects scheduling. Thus, letting dbg.value/declare increase
the order numbers may in turn affect scheduling.
Because of this change we also need to update the code deciding when
dbg values should be output, in ScheduleDAGSDNodes.cpp/ProcessSDDbgValues.
Dbg values now have the same order as the SDNode they are connected to,
not the following orders.
Test cases provided by Florian Hahn.
Reviewers: bogner, aprantl, sunfish, atrick
Reviewed By: atrick
Subscribers: fhahn, probinson, andreadb, llvm-commits, MatzeB
Differential Revision: https://reviews.llvm.org/D25318
llvm-svn: 292485
2017-01-19 21:55:55 +08:00
|
|
|
if (!Order || DVOrder == Order) {
|
2017-10-25 03:32:59 +08:00
|
|
|
MachineInstr *DbgMI = Emitter.EmitDbgValue(DV, VRBaseMap);
|
2011-01-27 02:20:04 +08:00
|
|
|
if (DbgMI) {
|
2017-09-26 00:14:39 +08:00
|
|
|
Orders.push_back({DVOrder, DbgMI});
|
2011-01-27 02:20:04 +08:00
|
|
|
BB->insert(InsertPos, DbgMI);
|
|
|
|
}
|
2017-10-25 03:32:59 +08:00
|
|
|
DV->setIsInvalidated();
|
2011-01-27 02:20:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:38:16 +08:00
|
|
|
// 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.
|
2013-07-14 12:42:23 +08:00
|
|
|
static void
|
|
|
|
ProcessSourceNode(SDNode *N, SelectionDAG *DAG, InstrEmitter &Emitter,
|
|
|
|
DenseMap<SDValue, unsigned> &VRBaseMap,
|
|
|
|
SmallVectorImpl<std::pair<unsigned, MachineInstr*> > &Orders,
|
|
|
|
SmallSet<unsigned, 8> &Seen) {
|
2013-05-25 11:08:10 +08:00
|
|
|
unsigned Order = N->getIROrder();
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!Order || !Seen.insert(Order).second) {
|
2011-01-27 08:13:27 +08:00
|
|
|
// Process any valid SDDbgValues even if node does not have any order
|
|
|
|
// assigned.
|
|
|
|
ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0);
|
2010-03-25 09:38:16 +08:00
|
|
|
return;
|
2011-01-27 08:13:27 +08:00
|
|
|
}
|
2010-03-25 09:38:16 +08:00
|
|
|
|
|
|
|
MachineBasicBlock *BB = Emitter.getBlock();
|
2017-09-26 00:14:39 +08:00
|
|
|
auto IP = Emitter.getInsertPos();
|
|
|
|
if (IP == BB->begin() || BB->back().isPHI() ||
|
2013-10-18 22:20:11 +08:00
|
|
|
// Fast-isel may have inserted some instructions, in which case the
|
|
|
|
// BB->back().isPHI() test will not fire when we want it to.
|
2017-09-26 00:14:39 +08:00
|
|
|
std::prev(IP)->isPHI()) {
|
2010-03-25 09:38:16 +08:00
|
|
|
// Did not insert any instruction.
|
2017-09-26 00:14:39 +08:00
|
|
|
Orders.push_back({Order, (MachineInstr *)nullptr});
|
2010-03-25 09:38:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-26 00:14:39 +08:00
|
|
|
Orders.push_back({Order, &*std::prev(IP)});
|
2011-01-27 02:20:04 +08:00
|
|
|
ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order);
|
2010-03-25 09:38:16 +08:00
|
|
|
}
|
|
|
|
|
2012-03-07 13:21:44 +08:00
|
|
|
void ScheduleDAGSDNodes::
|
|
|
|
EmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap,
|
|
|
|
MachineBasicBlock::iterator InsertPos) {
|
|
|
|
for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->isCtrl()) continue; // ignore chain preds
|
|
|
|
if (I->getSUnit()->CopyDstRC) {
|
|
|
|
// Copy to physical register.
|
|
|
|
DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->getSUnit());
|
|
|
|
assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
|
|
|
|
// Find the destination physical register.
|
|
|
|
unsigned Reg = 0;
|
|
|
|
for (SUnit::const_succ_iterator II = SU->Succs.begin(),
|
|
|
|
EE = SU->Succs.end(); II != EE; ++II) {
|
|
|
|
if (II->isCtrl()) continue; // ignore chain preds
|
|
|
|
if (II->getReg()) {
|
|
|
|
Reg = II->getReg();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), Reg)
|
|
|
|
.addReg(VRI->second);
|
|
|
|
} else {
|
|
|
|
// Copy from physical register.
|
|
|
|
assert(I->getReg() && "Unknown physical register!");
|
|
|
|
unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
|
|
|
|
bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase)).second;
|
|
|
|
(void)isNew; // Silence compiler warning.
|
|
|
|
assert(isNew && "Node emitted out of order - early");
|
|
|
|
BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), VRBase)
|
|
|
|
.addReg(I->getReg());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-03-25 09:38:16 +08:00
|
|
|
|
2012-03-07 13:21:44 +08:00
|
|
|
/// EmitSchedule - Emit the machine code in scheduled order. Return the new
|
|
|
|
/// InsertPos and MachineBasicBlock that contains this insertion
|
|
|
|
/// point. ScheduleDAGSDNodes holds a BB pointer for convenience, but this does
|
|
|
|
/// not necessarily refer to returned BB. The emitter may split blocks.
|
2012-03-07 13:21:52 +08:00
|
|
|
MachineBasicBlock *ScheduleDAGSDNodes::
|
|
|
|
EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
|
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.
|
2012-03-07 13:21:44 +08:00
|
|
|
TII->insertNoop(*Emitter.getBlock(), InsertPos);
|
2009-10-10 09:32:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For pre-regalloc scheduling, create instructions corresponding to the
|
2010-12-24 01:24:32 +08:00
|
|
|
// SDNode and any glued SDNodes and append them to the block.
|
2009-10-10 09:32:21 +08:00
|
|
|
if (!SU->getNode()) {
|
|
|
|
// Emit a copy.
|
2012-03-07 13:21:44 +08:00
|
|
|
EmitPhysRegCopy(SU, CopyVRBaseMap, InsertPos);
|
2009-10-10 09:32:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-12-24 01:24:32 +08:00
|
|
|
SmallVector<SDNode *, 4> GluedNodes;
|
2012-10-18 03:39:36 +08:00
|
|
|
for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
|
2010-12-24 01:24:32 +08:00
|
|
|
GluedNodes.push_back(N);
|
|
|
|
while (!GluedNodes.empty()) {
|
|
|
|
SDNode *N = GluedNodes.back();
|
2017-02-20 00:56:18 +08:00
|
|
|
Emitter.EmitNode(N, SU->OrigNode != SU, SU->isCloned, 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);
|
2010-12-24 01:24:32 +08:00
|
|
|
GluedNodes.pop_back();
|
2009-10-10 09:32:21 +08:00
|
|
|
}
|
|
|
|
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
|
2017-09-26 00:14:53 +08:00
|
|
|
// values. Use stable_sort so that DBG_VALUEs are inserted in the same order
|
|
|
|
// regardless of the host's implementation fo std::sort.
|
|
|
|
std::stable_sort(Orders.begin(), Orders.end(), less_first());
|
|
|
|
std::stable_sort(DAG->DbgBegin(), DAG->DbgEnd(),
|
|
|
|
[](const SDDbgValue *LHS, const SDDbgValue *RHS) {
|
|
|
|
return LHS->getOrder() < RHS->getOrder();
|
|
|
|
});
|
2010-03-25 09:38:16 +08:00
|
|
|
|
|
|
|
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;
|
2017-09-26 00:14:53 +08:00
|
|
|
for (; DI != DE; ++DI) {
|
|
|
|
if ((*DI)->getOrder() < LastOrder || (*DI)->getOrder() >= Order)
|
|
|
|
break;
|
2010-03-25 09:38:16 +08:00
|
|
|
if ((*DI)->isInvalidated())
|
|
|
|
continue;
|
2017-09-26 00:14:53 +08:00
|
|
|
|
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;
|
2013-05-26 16:58:50 +08:00
|
|
|
MI->getParent()->insert(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?
|
2012-03-14 15:14:25 +08:00
|
|
|
SmallVector<MachineInstr*, 8> DbgMIs;
|
2017-09-26 00:14:53 +08:00
|
|
|
for (; DI != DE; ++DI) {
|
|
|
|
if ((*DI)->isInvalidated())
|
|
|
|
continue;
|
|
|
|
assert((*DI)->getOrder() >= LastOrder &&
|
|
|
|
"emitting DBG_VALUE out of order");
|
|
|
|
if (MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap))
|
|
|
|
DbgMIs.push_back(DbgMI);
|
2010-03-25 09:38:16 +08:00
|
|
|
}
|
2012-03-14 15:14:25 +08:00
|
|
|
|
|
|
|
MachineBasicBlock *InsertBB = Emitter.getBlock();
|
|
|
|
MachineBasicBlock::iterator Pos = InsertBB->getFirstTerminator();
|
|
|
|
InsertBB->insert(Pos, DbgMIs.begin(), DbgMIs.end());
|
2009-10-10 09:32:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
InsertPos = Emitter.getInsertPos();
|
2012-03-07 13:21:52 +08:00
|
|
|
return Emitter.getBlock();
|
2009-10-10 09:32:21 +08:00
|
|
|
}
|
2012-03-07 08:18:22 +08:00
|
|
|
|
|
|
|
/// Return the basic block label.
|
|
|
|
std::string ScheduleDAGSDNodes::getDAGName() const {
|
|
|
|
return "sunit-dag." + BB->getFullName();
|
|
|
|
}
|