1343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===--- ScheduleDAGSDNodes.cpp - Implement the ScheduleDAGSDNodes class --===//
2343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//
3343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//                     The LLVM Compiler Infrastructure
4343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//
5343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman// This file is distributed under the University of Illinois Open Source
6343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman// License. See LICENSE.TXT for details.
7343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//
8343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===----------------------------------------------------------------------===//
9343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//
10343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman// This implements the ScheduleDAG class, which is a base class used by
11343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman// scheduling implementation classes.
12343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//
13343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===----------------------------------------------------------------------===//
14343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
15343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#define DEBUG_TYPE "pre-RA-sched"
1684fbac580941548a6ab1121ed3b0ffdc4e2bc080Dan Gohman#include "ScheduleDAGSDNodes.h"
17bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman#include "InstrEmitter.h"
18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "SDNodeDbgValue.h"
19c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng#include "llvm/ADT/DenseMap.h"
20c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng#include "llvm/ADT/SmallPtrSet.h"
21bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng#include "llvm/ADT/SmallSet.h"
22c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng#include "llvm/ADT/SmallVector.h"
23c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng#include "llvm/ADT/Statistic.h"
24d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/MachineInstrBuilder.h"
25d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/MachineRegisterInfo.h"
26d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/SelectionDAG.h"
27d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCInstrItineraries.h"
28e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick#include "llvm/Support/CommandLine.h"
29343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/Support/Debug.h"
30343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/Support/raw_ostream.h"
31d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetInstrInfo.h"
32d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetLowering.h"
33d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetMachine.h"
34d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetRegisterInfo.h"
35d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetSubtargetInfo.h"
36343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanusing namespace llvm;
37343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
38c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan ChengSTATISTIC(LoadsClustered, "Number of loads clustered together");
39c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng
40e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick// This allows latency based scheduler to notice high latency instructions
41e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick// without a target itinerary. The choise if number here has more to do with
42e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick// balancing scheduler heursitics than with the actual machine latency.
43e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trickstatic cl::opt<int> HighLatencyCycles(
44e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick  "sched-high-latency-cycles", cl::Hidden, cl::init(10),
45e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick  cl::desc("Roughly estimate the number of cycles that 'long latency'"
46e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick           "instructions take for targets with no itinerary"));
47e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick
4879ce276083ced01256a0eb7d80731e4948ca6e87Dan GohmanScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf)
4947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  : ScheduleDAG(mf), BB(0), DAG(0),
503ef1c8759a20167457eb7fd82ebcaffe7ccaa1d1Evan Cheng    InstrItins(mf.getTarget().getInstrItineraryData()) {}
51343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
5247ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman/// Run - perform scheduling.
5347ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman///
5447c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trickvoid ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb) {
5547c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  BB = bb;
5647ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman  DAG = dag;
5747c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick
5847c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  // Clear the scheduler's SUnit DAG.
5947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  ScheduleDAG::clearDAG();
6047c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  Sequence.clear();
6147c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick
6247c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  // Invoke the target's selection of scheduler.
6347c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  Schedule();
6447ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman}
6547ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman
661cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng/// NewSUnit - Creates a new SUnit and return a ptr to it.
671cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng///
68953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew TrickSUnit *ScheduleDAGSDNodes::newSUnit(SDNode *N) {
691cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng#ifndef NDEBUG
701cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng  const SUnit *Addr = 0;
711cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng  if (!SUnits.empty())
721cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng    Addr = &SUnits[0];
731cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng#endif
741cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng  SUnits.push_back(SUnit(N, (unsigned)SUnits.size()));
751cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng  assert((Addr == 0 || Addr == &SUnits[0]) &&
761cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng         "SUnits std::vector reallocated on the fly!");
771cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng  SUnits.back().OrigNode = &SUnits.back();
781cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng  SUnit *SU = &SUnits.back();
791cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng  const TargetLowering &TLI = DAG->getTargetLoweringInfo();
80c120af45671c75fd1297ac6300c03a6a9e1264daEvan Cheng  if (!N ||
81c120af45671c75fd1297ac6300c03a6a9e1264daEvan Cheng      (N->isMachineOpcode() &&
82c120af45671c75fd1297ac6300c03a6a9e1264daEvan Cheng       N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF))
83046fa3f90a31ebfa10df89ae348f478d492709a9Evan Cheng    SU->SchedulingPref = Sched::None;
84046fa3f90a31ebfa10df89ae348f478d492709a9Evan Cheng  else
85046fa3f90a31ebfa10df89ae348f478d492709a9Evan Cheng    SU->SchedulingPref = TLI.getSchedulingPreference(N);
861cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng  return SU;
871cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng}
881cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng
89343f0c046702831a4a6aec951b6a297a23241a55Dan GohmanSUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) {
90953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick  SUnit *SU = newSUnit(Old->getNode());
91343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  SU->OrigNode = Old->OrigNode;
92343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  SU->Latency = Old->Latency;
9354699765064842fd08d1466adc93453660bc2a85Andrew Trick  SU->isVRegCycle = Old->isVRegCycle;
948239daf7c83a65a189c352cce3191cdc3bbfe151Evan Cheng  SU->isCall = Old->isCall;
95554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng  SU->isCallOp = Old->isCallOp;
96343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  SU->isTwoAddress = Old->isTwoAddress;
97343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  SU->isCommutable = Old->isCommutable;
98343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  SU->hasPhysRegDefs = Old->hasPhysRegDefs;
993974667c1a6d48686e92f85bc4463bb239af7442Dan Gohman  SU->hasPhysRegClobbers = Old->hasPhysRegClobbers;
10012f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick  SU->isScheduleHigh = Old->isScheduleHigh;
10112f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick  SU->isScheduleLow = Old->isScheduleLow;
1021cc3984148be113c6e5e470f23c9ddbd37679c5fEvan Cheng  SU->SchedulingPref = Old->SchedulingPref;
103e57187cbe321a286f6a7f409a7badd1ae4e4642cEvan Cheng  Old->isCloned = true;
104343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  return SU;
105343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
106343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
107343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// CheckForPhysRegDependency - Check if the dependency between def and use of
108343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// a specified operand is a physical register dependency. If so, returns the
109c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng/// register and the cost of copying the register.
110343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanstatic void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
111cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick                                      const TargetRegisterInfo *TRI,
112343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman                                      const TargetInstrInfo *TII,
113c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng                                      unsigned &PhysReg, int &Cost) {
114343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
115343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    return;
116343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
117343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
118343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  if (TargetRegisterInfo::isVirtualRegister(Reg))
119343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    return;
120343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
121343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  unsigned ResNo = User->getOperand(2).getResNo();
122343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  if (Def->isMachineOpcode()) {
123e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng    const MCInstrDesc &II = TII->get(Def->getMachineOpcode());
124343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    if (ResNo >= II.getNumDefs() &&
125c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng        II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
126343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      PhysReg = Reg;
127c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng      const TargetRegisterClass *RC =
128d31f972bd33de85071c716f69bf5c6d735f730f2Rafael Espindola        TRI->getMinimalPhysRegClass(Reg, Def->getValueType(ResNo));
129c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng      Cost = RC->getCopyCost();
130c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng    }
131343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  }
132343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
133343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
1342674a4acdb51ce610d060b76608f631765a6e508Andrew Trick// Helper for AddGlue to clone node operands.
1352674a4acdb51ce610d060b76608f631765a6e508Andrew Trickstatic void CloneNodeWithValues(SDNode *N, SelectionDAG *DAG,
1362674a4acdb51ce610d060b76608f631765a6e508Andrew Trick                                SmallVectorImpl<EVT> &VTs,
1372674a4acdb51ce610d060b76608f631765a6e508Andrew Trick                                SDValue ExtraOper = SDValue()) {
138c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng  SmallVector<SDValue, 4> Ops;
13910707f3b442aa5a6cc55b899d630871f06b8ebbcBill Wendling  for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
14010707f3b442aa5a6cc55b899d630871f06b8ebbcBill Wendling    Ops.push_back(N->getOperand(I));
141151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling
1422674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  if (ExtraOper.getNode())
1432674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    Ops.push_back(ExtraOper);
144151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling
145c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng  SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size());
146151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling  MachineSDNode::mmo_iterator Begin = 0, End = 0;
147151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling  MachineSDNode *MN = dyn_cast<MachineSDNode>(N);
148151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling
149151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling  // Store memory references.
150151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling  if (MN) {
151151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling    Begin = MN->memoperands_begin();
152151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling    End = MN->memoperands_end();
153151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling  }
154151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling
155c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng  DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size());
156151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling
157151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling  // Reset the memory references
158151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling  if (MN)
159151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling    MN->setMemRefs(Begin, End);
160c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng}
161c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng
1622674a4acdb51ce610d060b76608f631765a6e508Andrew Trickstatic bool AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) {
1632674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  SmallVector<EVT, 4> VTs;
1642674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  SDNode *GlueDestNode = Glue.getNode();
1652674a4acdb51ce610d060b76608f631765a6e508Andrew Trick
1662674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  // Don't add glue from a node to itself.
1672674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  if (GlueDestNode == N) return false;
1682674a4acdb51ce610d060b76608f631765a6e508Andrew Trick
1692674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  // Don't add a glue operand to something that already uses glue.
1702674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  if (GlueDestNode &&
1712674a4acdb51ce610d060b76608f631765a6e508Andrew Trick      N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
1722674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    return false;
1732674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  }
1742674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  // Don't add glue to something that already has a glue value.
1752674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return false;
1762674a4acdb51ce610d060b76608f631765a6e508Andrew Trick
1772674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
1782674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    VTs.push_back(N->getValueType(I));
1792674a4acdb51ce610d060b76608f631765a6e508Andrew Trick
1802674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  if (AddGlue)
1812674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    VTs.push_back(MVT::Glue);
1822674a4acdb51ce610d060b76608f631765a6e508Andrew Trick
1832674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  CloneNodeWithValues(N, DAG, VTs, Glue);
1842674a4acdb51ce610d060b76608f631765a6e508Andrew Trick
1852674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  return true;
1862674a4acdb51ce610d060b76608f631765a6e508Andrew Trick}
1872674a4acdb51ce610d060b76608f631765a6e508Andrew Trick
1882674a4acdb51ce610d060b76608f631765a6e508Andrew Trick// Cleanup after unsuccessful AddGlue. Use the standard method of morphing the
1892674a4acdb51ce610d060b76608f631765a6e508Andrew Trick// node even though simply shrinking the value list is sufficient.
1902674a4acdb51ce610d060b76608f631765a6e508Andrew Trickstatic void RemoveUnusedGlue(SDNode *N, SelectionDAG *DAG) {
1912674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  assert((N->getValueType(N->getNumValues() - 1) == MVT::Glue &&
1922674a4acdb51ce610d060b76608f631765a6e508Andrew Trick          !N->hasAnyUseOfValue(N->getNumValues() - 1)) &&
1932674a4acdb51ce610d060b76608f631765a6e508Andrew Trick         "expected an unused glue value");
1942674a4acdb51ce610d060b76608f631765a6e508Andrew Trick
1952674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  SmallVector<EVT, 4> VTs;
1962674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  for (unsigned I = 0, E = N->getNumValues()-1; I != E; ++I)
1972674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    VTs.push_back(N->getValueType(I));
1982674a4acdb51ce610d060b76608f631765a6e508Andrew Trick
1992674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  CloneNodeWithValues(N, DAG, VTs);
2002674a4acdb51ce610d060b76608f631765a6e508Andrew Trick}
2012674a4acdb51ce610d060b76608f631765a6e508Andrew Trick
20229d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner/// ClusterNeighboringLoads - Force nearby loads together by "gluing" them.
203c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng/// This function finds loads of the same base and different offsets. If the
204f1b4eafbfec976f939ec0ea3e8acf91cef5363e3Chris Lattner/// offsets are not far apart (target specific), it add MVT::Glue inputs and
205c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng/// outputs to ensure they are scheduled together and in order. This
206c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng/// optimization may benefit some targets by improving cache locality.
207302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Chengvoid ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) {
208302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  SDNode *Chain = 0;
209302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  unsigned NumOps = Node->getNumOperands();
210302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  if (Node->getOperand(NumOps-1).getValueType() == MVT::Other)
211302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    Chain = Node->getOperand(NumOps-1).getNode();
212302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  if (!Chain)
213302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    return;
214302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng
215302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  // Look for other loads of the same chain. Find loads that are loading from
216302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  // the same base pointer and different offsets.
217c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng  SmallPtrSet<SDNode*, 16> Visited;
218c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng  SmallVector<int64_t, 4> Offsets;
219c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng  DenseMap<long long, SDNode*> O2SMap;  // Map from offset to SDNode.
220302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  bool Cluster = false;
221302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  SDNode *Base = Node;
222302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end();
223302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng       I != E; ++I) {
224302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    SDNode *User = *I;
225302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    if (User == Node || !Visited.insert(User))
226c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng      continue;
227302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    int64_t Offset1, Offset2;
228302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) ||
229302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng        Offset1 == Offset2)
230302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng      // FIXME: Should be ok if they addresses are identical. But earlier
231302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng      // optimizations really should have eliminated one of the loads.
232c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng      continue;
233302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    if (O2SMap.insert(std::make_pair(Offset1, Base)).second)
234302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng      Offsets.push_back(Offset1);
235302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    O2SMap.insert(std::make_pair(Offset2, User));
236302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    Offsets.push_back(Offset2);
237b447c4e65b5f6d39db16cb8fc338133965291972Duncan Sands    if (Offset2 < Offset1)
238302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng      Base = User;
239302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    Cluster = true;
240302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  }
241c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng
242302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  if (!Cluster)
243302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    return;
244c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng
245302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  // Sort them in increasing order.
246302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  std::sort(Offsets.begin(), Offsets.end());
247c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng
248302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  // Check if the loads are close enough.
249302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  SmallVector<SDNode*, 4> Loads;
250302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  unsigned NumLoads = 0;
251302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  int64_t BaseOff = Offsets[0];
252302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  SDNode *BaseLoad = O2SMap[BaseOff];
253302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  Loads.push_back(BaseLoad);
254302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  for (unsigned i = 1, e = Offsets.size(); i != e; ++i) {
255302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    int64_t Offset = Offsets[i];
256302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    SDNode *Load = O2SMap[Offset];
257302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads))
258302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng      break; // Stop right here. Ignore loads that are further away.
259302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    Loads.push_back(Load);
260302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    ++NumLoads;
261302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  }
262c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng
263302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  if (NumLoads == 0)
264302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    return;
265c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng
266f1b4eafbfec976f939ec0ea3e8acf91cef5363e3Chris Lattner  // Cluster loads by adding MVT::Glue outputs and inputs. This also
267302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  // ensure they are scheduled in order of increasing addresses.
268302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  SDNode *Lead = Loads[0];
2692674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  SDValue InGlue = SDValue(0, 0);
2702674a4acdb51ce610d060b76608f631765a6e508Andrew Trick  if (AddGlue(Lead, InGlue, true, DAG))
2712674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    InGlue = SDValue(Lead, Lead->getNumValues() - 1);
27210707f3b442aa5a6cc55b899d630871f06b8ebbcBill Wendling  for (unsigned I = 1, E = Loads.size(); I != E; ++I) {
27329d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    bool OutGlue = I < E - 1;
27410707f3b442aa5a6cc55b899d630871f06b8ebbcBill Wendling    SDNode *Load = Loads[I];
275151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling
2762674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    // If AddGlue fails, we could leave an unsused glue value. This should not
2772674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    // cause any
2782674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    if (AddGlue(Load, InGlue, OutGlue, DAG)) {
2792674a4acdb51ce610d060b76608f631765a6e508Andrew Trick      if (OutGlue)
2802674a4acdb51ce610d060b76608f631765a6e508Andrew Trick        InGlue = SDValue(Load, Load->getNumValues() - 1);
281151d26d15dc6fe89329d7cccb0638c324c58f485Bill Wendling
2822674a4acdb51ce610d060b76608f631765a6e508Andrew Trick      ++LoadsClustered;
2832674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    }
2842674a4acdb51ce610d060b76608f631765a6e508Andrew Trick    else if (!OutGlue && InGlue.getNode())
2852674a4acdb51ce610d060b76608f631765a6e508Andrew Trick      RemoveUnusedGlue(InGlue.getNode(), DAG);
286302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  }
287302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng}
288c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng
289302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng/// ClusterNodes - Cluster certain nodes which should be scheduled together.
290302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng///
291302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Chengvoid ScheduleDAGSDNodes::ClusterNodes() {
292302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
293302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng       E = DAG->allnodes_end(); NI != E; ++NI) {
294302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    SDNode *Node = &*NI;
295302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    if (!Node || !Node->isMachineOpcode())
296c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng      continue;
297c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng
298302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng    unsigned Opc = Node->getMachineOpcode();
299e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng    const MCInstrDesc &MCID = TII->get(Opc);
300e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng    if (MCID.mayLoad())
301302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng      // Cluster loads from "near" addresses into combined SUnits.
302302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng      ClusterNeighboringLoads(Node);
303c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng  }
304c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng}
305c589e03865bb31da70e0037d5c32fdaaa5f79f24Evan Cheng
306343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanvoid ScheduleDAGSDNodes::BuildSchedUnits() {
307343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  // During scheduling, the NodeId field of SDNode is used to map SDNodes
308343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  // to their associated SUnits by holding SUnits table indices. A value
309343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  // of -1 means the SDNode does not yet have an associated SUnit.
310e1dfc7da8991270db5094aa736fde273bfab6061Dan Gohman  unsigned NumNodes = 0;
311343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
312e1dfc7da8991270db5094aa736fde273bfab6061Dan Gohman       E = DAG->allnodes_end(); NI != E; ++NI) {
313343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    NI->setNodeId(-1);
314e1dfc7da8991270db5094aa736fde273bfab6061Dan Gohman    ++NumNodes;
315e1dfc7da8991270db5094aa736fde273bfab6061Dan Gohman  }
316343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
317e1dfc7da8991270db5094aa736fde273bfab6061Dan Gohman  // Reserve entries in the vector for each of the SUnits we are creating.  This
318e1dfc7da8991270db5094aa736fde273bfab6061Dan Gohman  // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
319e1dfc7da8991270db5094aa736fde273bfab6061Dan Gohman  // invalidated.
320e1dfc7da8991270db5094aa736fde273bfab6061Dan Gohman  // FIXME: Multiply by 2 because we may clone nodes during scheduling.
321e1dfc7da8991270db5094aa736fde273bfab6061Dan Gohman  // This is a temporary workaround.
322e1dfc7da8991270db5094aa736fde273bfab6061Dan Gohman  SUnits.reserve(NumNodes * 2);
323cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
324736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner  // Add all nodes in depth first order.
325736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner  SmallVector<SDNode*, 64> Worklist;
326736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner  SmallPtrSet<SDNode*, 64> Visited;
327736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner  Worklist.push_back(DAG->getRoot().getNode());
328736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner  Visited.insert(DAG->getRoot().getNode());
329cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
330554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng  SmallVector<SUnit*, 8> CallSUnits;
331736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner  while (!Worklist.empty()) {
332736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner    SDNode *NI = Worklist.pop_back_val();
333cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
334736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner    // Add all operands to the worklist unless they've already been added.
335736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner    for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i)
336736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner      if (Visited.insert(NI->getOperand(i).getNode()))
337736a6ea3a2a5322db0e09d97651a1acc07502e41Chris Lattner        Worklist.push_back(NI->getOperand(i).getNode());
338cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
339343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    if (isPassiveNode(NI))  // Leaf node, e.g. a TargetImmediate.
340343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      continue;
341cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
342343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    // If this node has already been processed, stop now.
343343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    if (NI->getNodeId() != -1) continue;
344cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
345953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick    SUnit *NodeSUnit = newSUnit(NI);
346cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
34729d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    // See if anything is glued to this node, if so, add them to glued
34829d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    // nodes.  Nodes can have at most one glue input and one glue output.  Glue
34929d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    // is required to be the last operand and result of a node.
350cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
35129d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    // Scan up to find glued preds.
352343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    SDNode *N = NI;
353db95fa131a229652f925794ca7a5b84e9490050bDan Gohman    while (N->getNumOperands() &&
354f1b4eafbfec976f939ec0ea3e8acf91cef5363e3Chris Lattner           N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) {
355db95fa131a229652f925794ca7a5b84e9490050bDan Gohman      N = N->getOperand(N->getNumOperands()-1).getNode();
356db95fa131a229652f925794ca7a5b84e9490050bDan Gohman      assert(N->getNodeId() == -1 && "Node already inserted!");
357db95fa131a229652f925794ca7a5b84e9490050bDan Gohman      N->setNodeId(NodeSUnit->NodeNum);
3588239daf7c83a65a189c352cce3191cdc3bbfe151Evan Cheng      if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
3598239daf7c83a65a189c352cce3191cdc3bbfe151Evan Cheng        NodeSUnit->isCall = true;
360343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
361cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
36229d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    // Scan down to find any glued succs.
363343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    N = NI;
364f1b4eafbfec976f939ec0ea3e8acf91cef5363e3Chris Lattner    while (N->getValueType(N->getNumValues()-1) == MVT::Glue) {
36529d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner      SDValue GlueVal(N, N->getNumValues()-1);
366cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
36729d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner      // There are either zero or one users of the Glue result.
36829d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner      bool HasGlueUse = false;
369cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick      for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
370343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman           UI != E; ++UI)
37129d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner        if (GlueVal.isOperandOf(*UI)) {
37229d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner          HasGlueUse = true;
373343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman          assert(N->getNodeId() == -1 && "Node already inserted!");
374343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman          N->setNodeId(NodeSUnit->NodeNum);
375343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman          N = *UI;
3768239daf7c83a65a189c352cce3191cdc3bbfe151Evan Cheng          if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall())
3778239daf7c83a65a189c352cce3191cdc3bbfe151Evan Cheng            NodeSUnit->isCall = true;
378343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman          break;
379343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        }
38029d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner      if (!HasGlueUse) break;
381343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
382cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
383554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng    if (NodeSUnit->isCall)
384554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng      CallSUnits.push_back(NodeSUnit);
385554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng
38612f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick    // Schedule zero-latency TokenFactor below any nodes that may increase the
38712f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick    // schedule height. Otherwise, ancestors of the TokenFactor may appear to
38812f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick    // have false stalls.
38912f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick    if (NI->getOpcode() == ISD::TokenFactor)
39012f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick      NodeSUnit->isScheduleLow = true;
39112f0dc6bb556976f22d89ebcf42bce273c9e7d38Andrew Trick
39229d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    // If there are glue operands involved, N is now the bottom-most node
39329d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    // of the sequence of nodes that are glued together.
394343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    // Update the SUnit.
395343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    NodeSUnit->setNode(N);
396343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    assert(N->getNodeId() == -1 && "Node already inserted!");
397343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    N->setNodeId(NodeSUnit->NodeNum);
398343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
39992e946630d5f9bb092853b93501387dd216899b9Andrew Trick    // Compute NumRegDefsLeft. This must be done before AddSchedEdges.
40092e946630d5f9bb092853b93501387dd216899b9Andrew Trick    InitNumRegDefsLeft(NodeSUnit);
40192e946630d5f9bb092853b93501387dd216899b9Andrew Trick
402787782f4ca0cca2523825131c24a6f78535a3eb8Dan Gohman    // Assign the Latency field of NodeSUnit using target-provided information.
403953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick    computeLatency(NodeSUnit);
404343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  }
405554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng
406554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng  // Find all call operands.
407554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng  while (!CallSUnits.empty()) {
408554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng    SUnit *SU = CallSUnits.pop_back_val();
409554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng    for (const SDNode *SUNode = SU->getNode(); SUNode;
410554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng         SUNode = SUNode->getGluedNode()) {
411554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng      if (SUNode->getOpcode() != ISD::CopyToReg)
412554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng        continue;
413554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng      SDNode *SrcN = SUNode->getOperand(2).getNode();
414554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng      if (isPassiveNode(SrcN)) continue;   // Not scheduled.
415554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng      SUnit *SrcSU = &SUnits[SrcN->getNodeId()];
416554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng      SrcSU->isCallOp = true;
417554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng    }
418554daa67bd1c4f01fb7a00f2f4255a52b81e9fa3Evan Cheng  }
419c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman}
420c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman
421c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohmanvoid ScheduleDAGSDNodes::AddSchedEdges() {
4225b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng  const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>();
423710461688bba935f0ad5c75da7fec2ad0f225c00David Goodwin
424dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin  // Check to see if the scheduler cares about latencies.
425953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick  bool UnitLatencies = forceUnitLatencies();
426dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin
427343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  // Pass 2: add the preds, succs, etc.
428343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
429343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    SUnit *SU = &SUnits[su];
430343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    SDNode *MainNode = SU->getNode();
431cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
432343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    if (MainNode->isMachineOpcode()) {
433343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      unsigned Opc = MainNode->getMachineOpcode();
434e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng      const MCInstrDesc &MCID = TII->get(Opc);
435e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng      for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
436e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng        if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
437343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman          SU->isTwoAddress = true;
438343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman          break;
439343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        }
440343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      }
441e837dead3c8dc3445ef6a0e2322179c57e264a13Evan Cheng      if (MCID.isCommutable())
442343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        SU->isCommutable = true;
443343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
444cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
445343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    // Find all predecessors and successors of the group.
44629d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) {
447343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      if (N->isMachineOpcode() &&
4483974667c1a6d48686e92f85bc4463bb239af7442Dan Gohman          TII->get(N->getMachineOpcode()).getImplicitDefs()) {
4493974667c1a6d48686e92f85bc4463bb239af7442Dan Gohman        SU->hasPhysRegClobbers = true;
450bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman        unsigned NumUsed = InstrEmitter::CountResults(N);
4518cccf0ef0ced7f4d75ca574b596036a9b6cd4315Dan Gohman        while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1))
4528cccf0ef0ced7f4d75ca574b596036a9b6cd4315Dan Gohman          --NumUsed;    // Skip over unused values at the end.
4538cccf0ef0ced7f4d75ca574b596036a9b6cd4315Dan Gohman        if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs())
4543974667c1a6d48686e92f85bc4463bb239af7442Dan Gohman          SU->hasPhysRegDefs = true;
4553974667c1a6d48686e92f85bc4463bb239af7442Dan Gohman      }
456cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
457343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
458343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        SDNode *OpN = N->getOperand(i).getNode();
459343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        if (isPassiveNode(OpN)) continue;   // Not scheduled.
460343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        SUnit *OpSU = &SUnits[OpN->getNodeId()];
461343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        assert(OpSU && "Node has no SUnit!");
462343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        if (OpSU == SU) continue;           // In the same group.
463343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
464e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson        EVT OpVT = N->getOperand(i).getValueType();
46529d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner        assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!");
466825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson        bool isChain = OpVT == MVT::Other;
467343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
468343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        unsigned PhysReg = 0;
469c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng        int Cost = 1;
470343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        // Determine if this is a physical register dependency.
471c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng        CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
47254e4c36a7349e94a84773afb56eccd4ca65b49e9Dan Gohman        assert((PhysReg == 0 || !isChain) &&
47354e4c36a7349e94a84773afb56eccd4ca65b49e9Dan Gohman               "Chain dependence via physreg data?");
474c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng        // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler
475c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng        // emits a copy from the physical register to a virtual register unless
476c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng        // it requires a cross class copy (cost < 0). That means we are only
477c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng        // treating "expensive to copy" register dependency as physical register
478c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng        // dependency. This may change in the future though.
4794cb971ce1c8b254f29365c988b55f6dcfe86d21eAndrew Trick        if (Cost >= 0 && !StressSched)
480c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng          PhysReg = 0;
481710461688bba935f0ad5c75da7fec2ad0f225c00David Goodwin
482046fa3f90a31ebfa10df89ae348f478d492709a9Evan Cheng        // If this is a ctrl dep, latency is 1.
483c558bf397257f5ef902bdb45a28e622ee2b5b4f2Andrew Trick        unsigned OpLatency = isChain ? 1 : OpSU->Latency;
48487896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick        // Special-case TokenFactor chains as zero-latency.
48587896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick        if(isChain && OpN->getOpcode() == ISD::TokenFactor)
48687896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick          OpLatency = 0;
48787896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick
488a78d3228e8b2a14915ea9908dbaaf2c934803e11Andrew Trick        SDep Dep = isChain ? SDep(OpSU, SDep::Barrier)
489a78d3228e8b2a14915ea9908dbaaf2c934803e11Andrew Trick          : SDep(OpSU, SDep::Data, PhysReg);
490a78d3228e8b2a14915ea9908dbaaf2c934803e11Andrew Trick        Dep.setLatency(OpLatency);
491dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin        if (!isChain && !UnitLatencies) {
492a78d3228e8b2a14915ea9908dbaaf2c934803e11Andrew Trick          computeOperandLatency(OpN, N, i, Dep);
493a78d3228e8b2a14915ea9908dbaaf2c934803e11Andrew Trick          ST.adjustSchedDependency(OpSU, SU, Dep);
494dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin        }
495710461688bba935f0ad5c75da7fec2ad0f225c00David Goodwin
496a78d3228e8b2a14915ea9908dbaaf2c934803e11Andrew Trick        if (!SU->addPred(Dep) && !Dep.isCtrl() && OpSU->NumRegDefsLeft > 1) {
49792e946630d5f9bb092853b93501387dd216899b9Andrew Trick          // Multiple register uses are combined in the same SUnit. For example,
49892e946630d5f9bb092853b93501387dd216899b9Andrew Trick          // we could have a set of glued nodes with all their defs consumed by
49992e946630d5f9bb092853b93501387dd216899b9Andrew Trick          // another set of glued nodes. Register pressure tracking sees this as
50092e946630d5f9bb092853b93501387dd216899b9Andrew Trick          // a single use, so to keep pressure balanced we reduce the defs.
5014bbf4678e341e9bf899c0faa3e3bcfe134db81ebAndrew Trick          //
5024bbf4678e341e9bf899c0faa3e3bcfe134db81ebAndrew Trick          // We can't tell (without more book-keeping) if this results from
5034bbf4678e341e9bf899c0faa3e3bcfe134db81ebAndrew Trick          // glued nodes or duplicate operands. As long as we don't reduce
5044bbf4678e341e9bf899c0faa3e3bcfe134db81ebAndrew Trick          // NumRegDefsLeft to zero, we handle the common cases well.
50592e946630d5f9bb092853b93501387dd216899b9Andrew Trick          --OpSU->NumRegDefsLeft;
50692e946630d5f9bb092853b93501387dd216899b9Andrew Trick        }
507343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      }
508343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
509343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  }
510343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
511343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
512c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman/// BuildSchedGraph - Build the SUnit graph from the selection dag that we
513c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman/// are input.  This SUnit graph is similar to the SelectionDAG, but
514c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman/// excludes nodes that aren't interesting to scheduling, and represents
51529d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner/// glued together nodes with a single SUnit.
51698976e4dcd18adbbe676048c0069e67346eb4adeDan Gohmanvoid ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) {
517302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  // Cluster certain nodes which should be scheduled together.
518302ef834e0a2fd03e4b435079a9fa6c1e1cdc23bEvan Cheng  ClusterNodes();
519c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman  // Populate the SUnits array.
520c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman  BuildSchedUnits();
521c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman  // Compute all the scheduling dependencies between nodes.
522c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman  AddSchedEdges();
523c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman}
524c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman
52592e946630d5f9bb092853b93501387dd216899b9Andrew Trick// Initialize NumNodeDefs for the current Node's opcode.
52692e946630d5f9bb092853b93501387dd216899b9Andrew Trickvoid ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() {
52729449448b0f0420dfcf52e278fc01adbf1690d70Eric Christopher  // Check for phys reg copy.
52829449448b0f0420dfcf52e278fc01adbf1690d70Eric Christopher  if (!Node)
52929449448b0f0420dfcf52e278fc01adbf1690d70Eric Christopher    return;
53029449448b0f0420dfcf52e278fc01adbf1690d70Eric Christopher
53192e946630d5f9bb092853b93501387dd216899b9Andrew Trick  if (!Node->isMachineOpcode()) {
53292e946630d5f9bb092853b93501387dd216899b9Andrew Trick    if (Node->getOpcode() == ISD::CopyFromReg)
53392e946630d5f9bb092853b93501387dd216899b9Andrew Trick      NodeNumDefs = 1;
53492e946630d5f9bb092853b93501387dd216899b9Andrew Trick    else
53592e946630d5f9bb092853b93501387dd216899b9Andrew Trick      NodeNumDefs = 0;
53692e946630d5f9bb092853b93501387dd216899b9Andrew Trick    return;
53792e946630d5f9bb092853b93501387dd216899b9Andrew Trick  }
53892e946630d5f9bb092853b93501387dd216899b9Andrew Trick  unsigned POpc = Node->getMachineOpcode();
53992e946630d5f9bb092853b93501387dd216899b9Andrew Trick  if (POpc == TargetOpcode::IMPLICIT_DEF) {
54092e946630d5f9bb092853b93501387dd216899b9Andrew Trick    // No register need be allocated for this.
54192e946630d5f9bb092853b93501387dd216899b9Andrew Trick    NodeNumDefs = 0;
54292e946630d5f9bb092853b93501387dd216899b9Andrew Trick    return;
54392e946630d5f9bb092853b93501387dd216899b9Andrew Trick  }
54492e946630d5f9bb092853b93501387dd216899b9Andrew Trick  unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs();
54592e946630d5f9bb092853b93501387dd216899b9Andrew Trick  // Some instructions define regs that are not represented in the selection DAG
54692e946630d5f9bb092853b93501387dd216899b9Andrew Trick  // (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues.
54792e946630d5f9bb092853b93501387dd216899b9Andrew Trick  NodeNumDefs = std::min(Node->getNumValues(), NRegDefs);
54892e946630d5f9bb092853b93501387dd216899b9Andrew Trick  DefIdx = 0;
54992e946630d5f9bb092853b93501387dd216899b9Andrew Trick}
55092e946630d5f9bb092853b93501387dd216899b9Andrew Trick
55192e946630d5f9bb092853b93501387dd216899b9Andrew Trick// Construct a RegDefIter for this SUnit and find the first valid value.
55292e946630d5f9bb092853b93501387dd216899b9Andrew TrickScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU,
55392e946630d5f9bb092853b93501387dd216899b9Andrew Trick                                           const ScheduleDAGSDNodes *SD)
55492e946630d5f9bb092853b93501387dd216899b9Andrew Trick  : SchedDAG(SD), Node(SU->getNode()), DefIdx(0), NodeNumDefs(0) {
55592e946630d5f9bb092853b93501387dd216899b9Andrew Trick  InitNodeNumDefs();
55692e946630d5f9bb092853b93501387dd216899b9Andrew Trick  Advance();
55792e946630d5f9bb092853b93501387dd216899b9Andrew Trick}
55892e946630d5f9bb092853b93501387dd216899b9Andrew Trick
55992e946630d5f9bb092853b93501387dd216899b9Andrew Trick// Advance to the next valid value defined by the SUnit.
56092e946630d5f9bb092853b93501387dd216899b9Andrew Trickvoid ScheduleDAGSDNodes::RegDefIter::Advance() {
56192e946630d5f9bb092853b93501387dd216899b9Andrew Trick  for (;Node;) { // Visit all glued nodes.
56292e946630d5f9bb092853b93501387dd216899b9Andrew Trick    for (;DefIdx < NodeNumDefs; ++DefIdx) {
56392e946630d5f9bb092853b93501387dd216899b9Andrew Trick      if (!Node->hasAnyUseOfValue(DefIdx))
56492e946630d5f9bb092853b93501387dd216899b9Andrew Trick        continue;
565860e7cdab9d5eceda5ac52ae0ddfb4bdab0067f2Patrik Hagglund      ValueType = Node->getSimpleValueType(DefIdx);
56692e946630d5f9bb092853b93501387dd216899b9Andrew Trick      ++DefIdx;
56792e946630d5f9bb092853b93501387dd216899b9Andrew Trick      return; // Found a normal regdef.
56892e946630d5f9bb092853b93501387dd216899b9Andrew Trick    }
56992e946630d5f9bb092853b93501387dd216899b9Andrew Trick    Node = Node->getGluedNode();
57092e946630d5f9bb092853b93501387dd216899b9Andrew Trick    if (Node == NULL) {
57192e946630d5f9bb092853b93501387dd216899b9Andrew Trick      return; // No values left to visit.
57292e946630d5f9bb092853b93501387dd216899b9Andrew Trick    }
57392e946630d5f9bb092853b93501387dd216899b9Andrew Trick    InitNodeNumDefs();
57492e946630d5f9bb092853b93501387dd216899b9Andrew Trick  }
57592e946630d5f9bb092853b93501387dd216899b9Andrew Trick}
57692e946630d5f9bb092853b93501387dd216899b9Andrew Trick
57792e946630d5f9bb092853b93501387dd216899b9Andrew Trickvoid ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) {
57892e946630d5f9bb092853b93501387dd216899b9Andrew Trick  assert(SU->NumRegDefsLeft == 0 && "expect a new node");
57992e946630d5f9bb092853b93501387dd216899b9Andrew Trick  for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) {
58092e946630d5f9bb092853b93501387dd216899b9Andrew Trick    assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected");
58192e946630d5f9bb092853b93501387dd216899b9Andrew Trick    ++SU->NumRegDefsLeft;
58292e946630d5f9bb092853b93501387dd216899b9Andrew Trick  }
58392e946630d5f9bb092853b93501387dd216899b9Andrew Trick}
58492e946630d5f9bb092853b93501387dd216899b9Andrew Trick
585953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trickvoid ScheduleDAGSDNodes::computeLatency(SUnit *SU) {
58687896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick  SDNode *N = SU->getNode();
58787896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick
58887896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick  // TokenFactor operands are considered zero latency, and some schedulers
58987896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick  // (e.g. Top-Down list) may rely on the fact that operand latency is nonzero
59087896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick  // whenever node latency is nonzero.
59187896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick  if (N && N->getOpcode() == ISD::TokenFactor) {
59287896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick    SU->Latency = 0;
59387896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick    return;
59487896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick  }
59587896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick
596e163168aab987dc3df0845b9e92310f764d8b158Evan Cheng  // Check to see if the scheduler cares about latencies.
597953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick  if (forceUnitLatencies()) {
598e163168aab987dc3df0845b9e92310f764d8b158Evan Cheng    SU->Latency = 1;
599e163168aab987dc3df0845b9e92310f764d8b158Evan Cheng    return;
600e163168aab987dc3df0845b9e92310f764d8b158Evan Cheng  }
601e163168aab987dc3df0845b9e92310f764d8b158Evan Cheng
6023ef1c8759a20167457eb7fd82ebcaffe7ccaa1d1Evan Cheng  if (!InstrItins || InstrItins->isEmpty()) {
6035e84e3ccaa555bd48ecca384e93e55abd76fb40aAndrew Trick    if (N && N->isMachineOpcode() &&
6045e84e3ccaa555bd48ecca384e93e55abd76fb40aAndrew Trick        TII->isHighLatencyDef(N->getMachineOpcode()))
605e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick      SU->Latency = HighLatencyCycles;
606e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick    else
607e0ef509aeb47b396cf1bdc170ca4f468f799719fAndrew Trick      SU->Latency = 1;
60815a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng    return;
60915a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng  }
610cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trick
611343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  // Compute the latency for the node.  We use the sum of the latencies for
61229d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner  // all nodes glued together into this SUnit.
613343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  SU->Latency = 0;
61429d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner  for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
6158239daf7c83a65a189c352cce3191cdc3bbfe151Evan Cheng    if (N->isMachineOpcode())
6168239daf7c83a65a189c352cce3191cdc3bbfe151Evan Cheng      SU->Latency += TII->getInstrLatency(InstrItins, N);
617343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
618343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
619953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trickvoid ScheduleDAGSDNodes::computeOperandLatency(SDNode *Def, SDNode *Use,
62015a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng                                               unsigned OpIdx, SDep& dep) const{
62115a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng  // Check to see if the scheduler cares about latencies.
622953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick  if (forceUnitLatencies())
62315a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng    return;
62415a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng
62515a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng  if (dep.getKind() != SDep::Data)
62615a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng    return;
62715a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng
62815a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng  unsigned DefIdx = Use->getOperand(OpIdx).getResNo();
6297e2fe9150f905167f6685c9730911c2abc08293cEvan Cheng  if (Use->isMachineOpcode())
6307e2fe9150f905167f6685c9730911c2abc08293cEvan Cheng    // Adjust the use operand index by num of defs.
6317e2fe9150f905167f6685c9730911c2abc08293cEvan Cheng    OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs();
632a0792de66c8364d47b0a688c7f408efb7b10f31bEvan Cheng  int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx);
633089751535d6e9adf65842e2ca5867bf9a70e1e95Evan Cheng  if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg &&
634089751535d6e9adf65842e2ca5867bf9a70e1e95Evan Cheng      !BB->succ_empty()) {
635089751535d6e9adf65842e2ca5867bf9a70e1e95Evan Cheng    unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
636089751535d6e9adf65842e2ca5867bf9a70e1e95Evan Cheng    if (TargetRegisterInfo::isVirtualRegister(Reg))
637089751535d6e9adf65842e2ca5867bf9a70e1e95Evan Cheng      // This copy is a liveout value. It is likely coalesced, so reduce the
638089751535d6e9adf65842e2ca5867bf9a70e1e95Evan Cheng      // latency so not to penalize the def.
639089751535d6e9adf65842e2ca5867bf9a70e1e95Evan Cheng      // FIXME: need target specific adjustment here?
640089751535d6e9adf65842e2ca5867bf9a70e1e95Evan Cheng      Latency = (Latency > 1) ? Latency - 1 : 1;
641089751535d6e9adf65842e2ca5867bf9a70e1e95Evan Cheng  }
6423881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  if (Latency >= 0)
6433881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    dep.setLatency(Latency);
64415a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng}
64515a16def6e70c8f7df1023da80ceb89887203b40Evan Cheng
646343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanvoid ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const {
647b720be6a50f4e1b3280d2b029ee38dda14577525Manman Ren#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
648c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng  if (!SU->getNode()) {
64984fa8229bbd3813505b7e8d6555fb2e522104e30David Greene    dbgs() << "PHYS REG COPY\n";
650c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng    return;
651c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng  }
652c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng
653c29a56dedbe4297dad94b9bf2e19035c5903fd1fEvan Cheng  SU->getNode()->dump(DAG);
65484fa8229bbd3813505b7e8d6555fb2e522104e30David Greene  dbgs() << "\n";
65529d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner  SmallVector<SDNode *, 4> GluedNodes;
65629d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner  for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
65729d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    GluedNodes.push_back(N);
65829d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner  while (!GluedNodes.empty()) {
65984fa8229bbd3813505b7e8d6555fb2e522104e30David Greene    dbgs() << "    ";
66029d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    GluedNodes.back()->dump(DAG);
66184fa8229bbd3813505b7e8d6555fb2e522104e30David Greene    dbgs() << "\n";
66229d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    GluedNodes.pop_back();
663343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  }
66477e300e8f0b8db8eec448cae9c87d7c5bfad9757Manman Ren#endif
665343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
666bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman
667b720be6a50f4e1b3280d2b029ee38dda14577525Manman Ren#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
66873ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trickvoid ScheduleDAGSDNodes::dumpSchedule() const {
66973ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick  for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
67073ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick    if (SUnit *SU = Sequence[i])
67173ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick      SU->dump(this);
67273ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick    else
67373ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick      dbgs() << "**** NOOP ****\n";
67473ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick  }
67573ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick}
67677e300e8f0b8db8eec448cae9c87d7c5bfad9757Manman Ren#endif
67773ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick
6784c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick#ifndef NDEBUG
6794c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick/// VerifyScheduledSequence - Verify that all SUnits were scheduled and that
6804c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick/// their state is consistent with the nodes listed in Sequence.
6814c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick///
6824c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trickvoid ScheduleDAGSDNodes::VerifyScheduledSequence(bool isBottomUp) {
6834c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick  unsigned ScheduledNodes = ScheduleDAG::VerifyScheduledDAG(isBottomUp);
6844c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick  unsigned Noops = 0;
6854c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick  for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
6864c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick    if (!Sequence[i])
6874c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick      ++Noops;
6884c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick  assert(Sequence.size() - Noops == ScheduledNodes &&
6894c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick         "The number of nodes scheduled doesn't match the expected number!");
6904c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick}
6914c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick#endif // NDEBUG
6924c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick
693bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Chengnamespace {
694bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  struct OrderSorter {
695bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    bool operator()(const std::pair<unsigned, MachineInstr*> &A,
696bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng                    const std::pair<unsigned, MachineInstr*> &B) {
697bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng      return A.first < B.first;
698bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    }
699bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  };
700bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng}
701bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng
7027a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner/// ProcessSDDbgValues - Process SDDbgValues associated with this node.
703cd5af07c4573c6b1270d6737e76ef3219091a733Andrew Trickstatic void ProcessSDDbgValues(SDNode *N, SelectionDAG *DAG,
70455d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel                               InstrEmitter &Emitter,
70555d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel                    SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
70655d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel                            DenseMap<SDValue, unsigned> &VRBaseMap,
70755d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel                            unsigned Order) {
70855d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel  if (!N->getHasDebugValue())
70955d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel    return;
71055d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel
71155d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel  // Opportunistically insert immediate dbg_value uses, i.e. those with source
71255d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel  // order number right after the N.
71355d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel  MachineBasicBlock *BB = Emitter.getBlock();
71455d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel  MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
71522a54c1cd711afccd4558374918d12a939e1cca5Benjamin Kramer  ArrayRef<SDDbgValue*> DVs = DAG->GetDbgValues(N);
71655d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel  for (unsigned i = 0, e = DVs.size(); i != e; ++i) {
71755d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel    if (DVs[i]->isInvalidated())
71855d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel      continue;
71955d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel    unsigned DVOrder = DVs[i]->getOrder();
72055d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel    if (!Order || DVOrder == ++Order) {
72155d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel      MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap);
72255d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel      if (DbgMI) {
72355d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel        Orders.push_back(std::make_pair(DVOrder, DbgMI));
72455d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel        BB->insert(InsertPos, DbgMI);
72555d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel      }
72655d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel      DVs[i]->setIsInvalidated();
72755d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel    }
72855d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel  }
72955d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel}
73055d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel
731bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng// ProcessSourceNode - Process nodes with source order numbers. These are added
732d27946d1d4272d7e2bbee00fac020dc8147dfd25Jim Grosbach// to a vector which EmitSchedule uses to determine how to insert dbg_value
733bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng// instructions in the right order.
734bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Chengstatic void ProcessSourceNode(SDNode *N, SelectionDAG *DAG,
735bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng                           InstrEmitter &Emitter,
736bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng                           DenseMap<SDValue, unsigned> &VRBaseMap,
737bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng                    SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders,
738bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng                           SmallSet<unsigned, 8> &Seen) {
739bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  unsigned Order = DAG->GetOrdering(N);
74039078a8bde256ee22e981713a4d2ff8235dc7706Devang Patel  if (!Order || !Seen.insert(Order)) {
74139078a8bde256ee22e981713a4d2ff8235dc7706Devang Patel    // Process any valid SDDbgValues even if node does not have any order
74239078a8bde256ee22e981713a4d2ff8235dc7706Devang Patel    // assigned.
74339078a8bde256ee22e981713a4d2ff8235dc7706Devang Patel    ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0);
744bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    return;
74539078a8bde256ee22e981713a4d2ff8235dc7706Devang Patel  }
746bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng
747bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  MachineBasicBlock *BB = Emitter.getBlock();
74884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI()) {
749bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    // Did not insert any instruction.
750bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    Orders.push_back(std::make_pair(Order, (MachineInstr*)0));
751bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    return;
752bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  }
753bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng
75484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  Orders.push_back(std::make_pair(Order, prior(Emitter.getInsertPos())));
75555d20e8ff1e458f177302386d14f1a4dbdd86028Devang Patel  ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order);
756bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng}
757bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng
75884b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trickvoid ScheduleDAGSDNodes::
75984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew TrickEmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap,
76084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick                MachineBasicBlock::iterator InsertPos) {
76184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
76284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick       I != E; ++I) {
76384b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    if (I->isCtrl()) continue;  // ignore chain preds
76484b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    if (I->getSUnit()->CopyDstRC) {
76584b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      // Copy to physical register.
76684b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->getSUnit());
76784b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
76884b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      // Find the destination physical register.
76984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      unsigned Reg = 0;
77084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      for (SUnit::const_succ_iterator II = SU->Succs.begin(),
77184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick             EE = SU->Succs.end(); II != EE; ++II) {
77284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick        if (II->isCtrl()) continue;  // ignore chain preds
77384b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick        if (II->getReg()) {
77484b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick          Reg = II->getReg();
77584b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick          break;
77684b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick        }
77784b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      }
77884b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), Reg)
77984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick        .addReg(VRI->second);
78084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    } else {
78184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      // Copy from physical register.
78284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      assert(I->getReg() && "Unknown physical register!");
78384b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
78484b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase)).second;
78584b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      (void)isNew; // Silence compiler warning.
78684b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      assert(isNew && "Node emitted out of order - early");
78784b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), VRBase)
78884b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick        .addReg(I->getReg());
78984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    }
79084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    break;
79184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  }
79284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick}
793bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng
79484b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick/// EmitSchedule - Emit the machine code in scheduled order. Return the new
79584b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick/// InsertPos and MachineBasicBlock that contains this insertion
79684b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick/// point. ScheduleDAGSDNodes holds a BB pointer for convenience, but this does
79784b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick/// not necessarily refer to returned BB. The emitter may split blocks.
79847c144505b9be28ed22c626b3a407c11dba2fec5Andrew TrickMachineBasicBlock *ScheduleDAGSDNodes::
79947c144505b9be28ed22c626b3a407c11dba2fec5Andrew TrickEmitSchedule(MachineBasicBlock::iterator &InsertPos) {
800bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman  InstrEmitter Emitter(BB, InsertPos);
801bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman  DenseMap<SDValue, unsigned> VRBaseMap;
802bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman  DenseMap<SUnit*, unsigned> CopyVRBaseMap;
803bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders;
804bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  SmallSet<unsigned, 8> Seen;
805bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  bool HasDbg = DAG->hasDebugValues();
806bfdf7f38523bd38ae0538861a2bfd8bdc46e5c33Dale Johannesen
807fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen  // If this is the first BB, emit byval parameter dbg_value's.
808fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen  if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) {
809fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen    SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin();
810fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen    SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd();
811fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen    for (; PDI != PDE; ++PDI) {
812891ff8fbd61a06ef8ea57461fa377ebbb663ed09Dan Gohman      MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap);
813fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen      if (DbgMI)
81484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman        BB->insert(InsertPos, DbgMI);
815fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen    }
816fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen  }
817fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen
818bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman  for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
819bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman    SUnit *SU = Sequence[i];
820bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman    if (!SU) {
821bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman      // Null SUnit* is a noop.
82284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      TII->insertNoop(*Emitter.getBlock(), InsertPos);
823bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman      continue;
824bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman    }
825bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman
826bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman    // For pre-regalloc scheduling, create instructions corresponding to the
82729d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    // SDNode and any glued SDNodes and append them to the block.
828bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman    if (!SU->getNode()) {
829bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman      // Emit a copy.
83084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      EmitPhysRegCopy(SU, CopyVRBaseMap, InsertPos);
831bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman      continue;
832bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman    }
833bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman
83429d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    SmallVector<SDNode *, 4> GluedNodes;
835d4f759696d1bd0ba7c0e6eefd7ed8b556840419aEvan Cheng    for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode())
83629d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner      GluedNodes.push_back(N);
83729d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner    while (!GluedNodes.empty()) {
83829d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner      SDNode *N = GluedNodes.back();
83929d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner      Emitter.EmitNode(GluedNodes.back(), SU->OrigNode != SU, SU->isCloned,
840af1d8ca44a18f304f207e209b3bdb94b590f86ffDan Gohman                       VRBaseMap);
841fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen      // Remember the source order of the inserted instruction.
842bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng      if (HasDbg)
843891ff8fbd61a06ef8ea57461fa377ebbb663ed09Dan Gohman        ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen);
84429d8f0cae425f1bba583565227eaebf58f26ce73Chris Lattner      GluedNodes.pop_back();
845bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman    }
846bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman    Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned,
847af1d8ca44a18f304f207e209b3bdb94b590f86ffDan Gohman                     VRBaseMap);
848fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen    // Remember the source order of the inserted instruction.
849bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    if (HasDbg)
850891ff8fbd61a06ef8ea57461fa377ebbb663ed09Dan Gohman      ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders,
851bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng                        Seen);
852bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  }
853bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng
854fdb42fa5fe794cc2c89e2ed7f57a89ed24d9952aDale Johannesen  // Insert all the dbg_values which have not already been inserted in source
855bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  // order sequence.
856bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng  if (HasDbg) {
85784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman    MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI();
858bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng
859bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    // Sort the source order instructions and use the order to insert debug
860bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    // values.
861bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    std::sort(Orders.begin(), Orders.end(), OrderSorter());
862bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng
863bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    SDDbgInfo::DbgIterator DI = DAG->DbgBegin();
864bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    SDDbgInfo::DbgIterator DE = DAG->DbgEnd();
865bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    // Now emit the rest according to source order.
866bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    unsigned LastOrder = 0;
867bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) {
868bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng      unsigned Order = Orders[i].first;
869bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng      MachineInstr *MI = Orders[i].second;
870bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng      // Insert all SDDbgValue's whose order(s) are before "Order".
871bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng      if (!MI)
872bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng        continue;
873bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng      for (; DI != DE &&
874bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng             (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) {
875bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng        if ((*DI)->isInvalidated())
876bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng          continue;
877891ff8fbd61a06ef8ea57461fa377ebbb663ed09Dan Gohman        MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap);
878962021bc7f6721c20c7dfe8ca809e2d98b1c554aEvan Cheng        if (DbgMI) {
879962021bc7f6721c20c7dfe8ca809e2d98b1c554aEvan Cheng          if (!LastOrder)
880962021bc7f6721c20c7dfe8ca809e2d98b1c554aEvan Cheng            // Insert to start of the BB (after PHIs).
881962021bc7f6721c20c7dfe8ca809e2d98b1c554aEvan Cheng            BB->insert(BBBegin, DbgMI);
882962021bc7f6721c20c7dfe8ca809e2d98b1c554aEvan Cheng          else {
883a8dab36f3dfdfcd3f74224afa4ffb32776674c93Dan Gohman            // Insert at the instruction, which may be in a different
884a8dab36f3dfdfcd3f74224afa4ffb32776674c93Dan Gohman            // block, if the block was split by a custom inserter.
885962021bc7f6721c20c7dfe8ca809e2d98b1c554aEvan Cheng            MachineBasicBlock::iterator Pos = MI;
886a8dab36f3dfdfcd3f74224afa4ffb32776674c93Dan Gohman            MI->getParent()->insert(llvm::next(Pos), DbgMI);
887962021bc7f6721c20c7dfe8ca809e2d98b1c554aEvan Cheng          }
888bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng        }
889bfdf7f38523bd38ae0538861a2bfd8bdc46e5c33Dale Johannesen      }
890bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng      LastOrder = Order;
891bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    }
892bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    // Add trailing DbgValue's before the terminator. FIXME: May want to add
893bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    // some of them before one or more conditional branches?
8947bf116acd417e50f6fac677b9cb9204ee7f35c00Bill Wendling    SmallVector<MachineInstr*, 8> DbgMIs;
895bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    while (DI != DE) {
8967bf116acd417e50f6fac677b9cb9204ee7f35c00Bill Wendling      if (!(*DI)->isInvalidated())
8977bf116acd417e50f6fac677b9cb9204ee7f35c00Bill Wendling        if (MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap))
8987bf116acd417e50f6fac677b9cb9204ee7f35c00Bill Wendling          DbgMIs.push_back(DbgMI);
899bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng      ++DI;
900bfcb3051899b7141a946d769fcf6e8a8453bc530Evan Cheng    }
9017bf116acd417e50f6fac677b9cb9204ee7f35c00Bill Wendling
9027bf116acd417e50f6fac677b9cb9204ee7f35c00Bill Wendling    MachineBasicBlock *InsertBB = Emitter.getBlock();
9037bf116acd417e50f6fac677b9cb9204ee7f35c00Bill Wendling    MachineBasicBlock::iterator Pos = InsertBB->getFirstTerminator();
9047bf116acd417e50f6fac677b9cb9204ee7f35c00Bill Wendling    InsertBB->insert(Pos, DbgMIs.begin(), DbgMIs.end());
905bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman  }
906bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman
907bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman  InsertPos = Emitter.getInsertPos();
90847c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  return Emitter.getBlock();
909bcea859fc1dd1af9ac66ec93ea04ce9a19c8451cDan Gohman}
91056b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trick
91156b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trick/// Return the basic block label.
91256b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trickstd::string ScheduleDAGSDNodes::getDAGName() const {
91356b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trick  return "sunit-dag." + BB->getFullName();
91456b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trick}
915