1193323Sed//===--- ScheduleDAGSDNodes.cpp - Implement the ScheduleDAGSDNodes class --===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This implements the ScheduleDAG class, which is a base class used by 11193323Sed// scheduling implementation classes. 12193323Sed// 13193323Sed//===----------------------------------------------------------------------===// 14193323Sed 15193323Sed#define DEBUG_TYPE "pre-RA-sched" 16193323Sed#include "ScheduleDAGSDNodes.h" 17198090Srdivacky#include "InstrEmitter.h" 18252723Sdim#include "SDNodeDbgValue.h" 19202878Srdivacky#include "llvm/ADT/DenseMap.h" 20202878Srdivacky#include "llvm/ADT/SmallPtrSet.h" 21206083Srdivacky#include "llvm/ADT/SmallSet.h" 22202878Srdivacky#include "llvm/ADT/SmallVector.h" 23202878Srdivacky#include "llvm/ADT/Statistic.h" 24252723Sdim#include "llvm/CodeGen/MachineInstrBuilder.h" 25252723Sdim#include "llvm/CodeGen/MachineRegisterInfo.h" 26252723Sdim#include "llvm/CodeGen/SelectionDAG.h" 27252723Sdim#include "llvm/MC/MCInstrItineraries.h" 28221345Sdim#include "llvm/Support/CommandLine.h" 29193323Sed#include "llvm/Support/Debug.h" 30193323Sed#include "llvm/Support/raw_ostream.h" 31252723Sdim#include "llvm/Target/TargetInstrInfo.h" 32252723Sdim#include "llvm/Target/TargetLowering.h" 33252723Sdim#include "llvm/Target/TargetMachine.h" 34252723Sdim#include "llvm/Target/TargetRegisterInfo.h" 35252723Sdim#include "llvm/Target/TargetSubtargetInfo.h" 36193323Sedusing namespace llvm; 37193323Sed 38202878SrdivackySTATISTIC(LoadsClustered, "Number of loads clustered together"); 39202878Srdivacky 40221345Sdim// This allows latency based scheduler to notice high latency instructions 41221345Sdim// without a target itinerary. The choise if number here has more to do with 42221345Sdim// balancing scheduler heursitics than with the actual machine latency. 43221345Sdimstatic cl::opt<int> HighLatencyCycles( 44221345Sdim "sched-high-latency-cycles", cl::Hidden, cl::init(10), 45221345Sdim cl::desc("Roughly estimate the number of cycles that 'long latency'" 46221345Sdim "instructions take for targets with no itinerary")); 47221345Sdim 48193323SedScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf) 49235633Sdim : ScheduleDAG(mf), BB(0), DAG(0), 50218893Sdim InstrItins(mf.getTarget().getInstrItineraryData()) {} 51193323Sed 52193323Sed/// Run - perform scheduling. 53193323Sed/// 54235633Sdimvoid ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb) { 55235633Sdim BB = bb; 56193323Sed DAG = dag; 57235633Sdim 58235633Sdim // Clear the scheduler's SUnit DAG. 59235633Sdim ScheduleDAG::clearDAG(); 60235633Sdim Sequence.clear(); 61235633Sdim 62235633Sdim // Invoke the target's selection of scheduler. 63235633Sdim Schedule(); 64193323Sed} 65193323Sed 66208599Srdivacky/// NewSUnit - Creates a new SUnit and return a ptr to it. 67208599Srdivacky/// 68235633SdimSUnit *ScheduleDAGSDNodes::newSUnit(SDNode *N) { 69208599Srdivacky#ifndef NDEBUG 70208599Srdivacky const SUnit *Addr = 0; 71208599Srdivacky if (!SUnits.empty()) 72208599Srdivacky Addr = &SUnits[0]; 73208599Srdivacky#endif 74208599Srdivacky SUnits.push_back(SUnit(N, (unsigned)SUnits.size())); 75208599Srdivacky assert((Addr == 0 || Addr == &SUnits[0]) && 76208599Srdivacky "SUnits std::vector reallocated on the fly!"); 77208599Srdivacky SUnits.back().OrigNode = &SUnits.back(); 78208599Srdivacky SUnit *SU = &SUnits.back(); 79208599Srdivacky const TargetLowering &TLI = DAG->getTargetLoweringInfo(); 80212904Sdim if (!N || 81212904Sdim (N->isMachineOpcode() && 82212904Sdim N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF)) 83210299Sed SU->SchedulingPref = Sched::None; 84210299Sed else 85210299Sed SU->SchedulingPref = TLI.getSchedulingPreference(N); 86208599Srdivacky return SU; 87208599Srdivacky} 88208599Srdivacky 89193323SedSUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) { 90235633Sdim SUnit *SU = newSUnit(Old->getNode()); 91193323Sed SU->OrigNode = Old->OrigNode; 92193323Sed SU->Latency = Old->Latency; 93221345Sdim SU->isVRegCycle = Old->isVRegCycle; 94218893Sdim SU->isCall = Old->isCall; 95221345Sdim SU->isCallOp = Old->isCallOp; 96193323Sed SU->isTwoAddress = Old->isTwoAddress; 97193323Sed SU->isCommutable = Old->isCommutable; 98193323Sed SU->hasPhysRegDefs = Old->hasPhysRegDefs; 99193323Sed SU->hasPhysRegClobbers = Old->hasPhysRegClobbers; 100221345Sdim SU->isScheduleHigh = Old->isScheduleHigh; 101221345Sdim SU->isScheduleLow = Old->isScheduleLow; 102208599Srdivacky SU->SchedulingPref = Old->SchedulingPref; 103193323Sed Old->isCloned = true; 104193323Sed return SU; 105193323Sed} 106193323Sed 107193323Sed/// CheckForPhysRegDependency - Check if the dependency between def and use of 108193323Sed/// a specified operand is a physical register dependency. If so, returns the 109193323Sed/// register and the cost of copying the register. 110193323Sedstatic void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op, 111218893Sdim const TargetRegisterInfo *TRI, 112193323Sed const TargetInstrInfo *TII, 113193323Sed unsigned &PhysReg, int &Cost) { 114193323Sed if (Op != 2 || User->getOpcode() != ISD::CopyToReg) 115193323Sed return; 116193323Sed 117193323Sed unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 118193323Sed if (TargetRegisterInfo::isVirtualRegister(Reg)) 119193323Sed return; 120193323Sed 121193323Sed unsigned ResNo = User->getOperand(2).getResNo(); 122193323Sed if (Def->isMachineOpcode()) { 123224145Sdim const MCInstrDesc &II = TII->get(Def->getMachineOpcode()); 124193323Sed if (ResNo >= II.getNumDefs() && 125193323Sed II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) { 126193323Sed PhysReg = Reg; 127193323Sed const TargetRegisterClass *RC = 128210299Sed TRI->getMinimalPhysRegClass(Reg, Def->getValueType(ResNo)); 129193323Sed Cost = RC->getCopyCost(); 130193323Sed } 131193323Sed } 132193323Sed} 133193323Sed 134236144Sdim// Helper for AddGlue to clone node operands. 135236144Sdimstatic void CloneNodeWithValues(SDNode *N, SelectionDAG *DAG, 136236144Sdim SmallVectorImpl<EVT> &VTs, 137236144Sdim SDValue ExtraOper = SDValue()) { 138202878Srdivacky SmallVector<SDValue, 4> Ops; 139210299Sed for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I) 140210299Sed Ops.push_back(N->getOperand(I)); 141210299Sed 142236144Sdim if (ExtraOper.getNode()) 143236144Sdim Ops.push_back(ExtraOper); 144210299Sed 145202878Srdivacky SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size()); 146210299Sed MachineSDNode::mmo_iterator Begin = 0, End = 0; 147210299Sed MachineSDNode *MN = dyn_cast<MachineSDNode>(N); 148210299Sed 149210299Sed // Store memory references. 150210299Sed if (MN) { 151210299Sed Begin = MN->memoperands_begin(); 152210299Sed End = MN->memoperands_end(); 153210299Sed } 154210299Sed 155202878Srdivacky DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size()); 156210299Sed 157210299Sed // Reset the memory references 158210299Sed if (MN) 159210299Sed MN->setMemRefs(Begin, End); 160202878Srdivacky} 161202878Srdivacky 162236144Sdimstatic bool AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) { 163236144Sdim SmallVector<EVT, 4> VTs; 164236144Sdim SDNode *GlueDestNode = Glue.getNode(); 165236144Sdim 166236144Sdim // Don't add glue from a node to itself. 167236144Sdim if (GlueDestNode == N) return false; 168236144Sdim 169236144Sdim // Don't add a glue operand to something that already uses glue. 170236144Sdim if (GlueDestNode && 171236144Sdim N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) { 172236144Sdim return false; 173236144Sdim } 174236144Sdim // Don't add glue to something that already has a glue value. 175236144Sdim if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return false; 176236144Sdim 177236144Sdim for (unsigned I = 0, E = N->getNumValues(); I != E; ++I) 178236144Sdim VTs.push_back(N->getValueType(I)); 179236144Sdim 180236144Sdim if (AddGlue) 181236144Sdim VTs.push_back(MVT::Glue); 182236144Sdim 183236144Sdim CloneNodeWithValues(N, DAG, VTs, Glue); 184236144Sdim 185236144Sdim return true; 186236144Sdim} 187236144Sdim 188236144Sdim// Cleanup after unsuccessful AddGlue. Use the standard method of morphing the 189236144Sdim// node even though simply shrinking the value list is sufficient. 190236144Sdimstatic void RemoveUnusedGlue(SDNode *N, SelectionDAG *DAG) { 191236144Sdim assert((N->getValueType(N->getNumValues() - 1) == MVT::Glue && 192236144Sdim !N->hasAnyUseOfValue(N->getNumValues() - 1)) && 193236144Sdim "expected an unused glue value"); 194236144Sdim 195236144Sdim SmallVector<EVT, 4> VTs; 196236144Sdim for (unsigned I = 0, E = N->getNumValues()-1; I != E; ++I) 197236144Sdim VTs.push_back(N->getValueType(I)); 198236144Sdim 199236144Sdim CloneNodeWithValues(N, DAG, VTs); 200236144Sdim} 201236144Sdim 202218893Sdim/// ClusterNeighboringLoads - Force nearby loads together by "gluing" them. 203202878Srdivacky/// This function finds loads of the same base and different offsets. If the 204218893Sdim/// offsets are not far apart (target specific), it add MVT::Glue inputs and 205202878Srdivacky/// outputs to ensure they are scheduled together and in order. This 206202878Srdivacky/// optimization may benefit some targets by improving cache locality. 207210299Sedvoid ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) { 208210299Sed SDNode *Chain = 0; 209210299Sed unsigned NumOps = Node->getNumOperands(); 210210299Sed if (Node->getOperand(NumOps-1).getValueType() == MVT::Other) 211210299Sed Chain = Node->getOperand(NumOps-1).getNode(); 212210299Sed if (!Chain) 213210299Sed return; 214210299Sed 215210299Sed // Look for other loads of the same chain. Find loads that are loading from 216210299Sed // the same base pointer and different offsets. 217202878Srdivacky SmallPtrSet<SDNode*, 16> Visited; 218202878Srdivacky SmallVector<int64_t, 4> Offsets; 219202878Srdivacky DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode. 220210299Sed bool Cluster = false; 221210299Sed SDNode *Base = Node; 222266759Sdim // This algorithm requires a reasonably low use count before finding a match 223266759Sdim // to avoid uselessly blowing up compile time in large blocks. 224266759Sdim unsigned UseCount = 0; 225210299Sed for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end(); 226266759Sdim I != E && UseCount < 100; ++I, ++UseCount) { 227210299Sed SDNode *User = *I; 228210299Sed if (User == Node || !Visited.insert(User)) 229202878Srdivacky continue; 230210299Sed int64_t Offset1, Offset2; 231210299Sed if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) || 232210299Sed Offset1 == Offset2) 233210299Sed // FIXME: Should be ok if they addresses are identical. But earlier 234210299Sed // optimizations really should have eliminated one of the loads. 235202878Srdivacky continue; 236210299Sed if (O2SMap.insert(std::make_pair(Offset1, Base)).second) 237210299Sed Offsets.push_back(Offset1); 238210299Sed O2SMap.insert(std::make_pair(Offset2, User)); 239210299Sed Offsets.push_back(Offset2); 240210299Sed if (Offset2 < Offset1) 241210299Sed Base = User; 242210299Sed Cluster = true; 243266759Sdim // Reset UseCount to allow more matches. 244266759Sdim UseCount = 0; 245210299Sed } 246202878Srdivacky 247210299Sed if (!Cluster) 248210299Sed return; 249202878Srdivacky 250210299Sed // Sort them in increasing order. 251210299Sed std::sort(Offsets.begin(), Offsets.end()); 252202878Srdivacky 253210299Sed // Check if the loads are close enough. 254210299Sed SmallVector<SDNode*, 4> Loads; 255210299Sed unsigned NumLoads = 0; 256210299Sed int64_t BaseOff = Offsets[0]; 257210299Sed SDNode *BaseLoad = O2SMap[BaseOff]; 258210299Sed Loads.push_back(BaseLoad); 259210299Sed for (unsigned i = 1, e = Offsets.size(); i != e; ++i) { 260210299Sed int64_t Offset = Offsets[i]; 261210299Sed SDNode *Load = O2SMap[Offset]; 262210299Sed if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads)) 263210299Sed break; // Stop right here. Ignore loads that are further away. 264210299Sed Loads.push_back(Load); 265210299Sed ++NumLoads; 266210299Sed } 267202878Srdivacky 268210299Sed if (NumLoads == 0) 269210299Sed return; 270202878Srdivacky 271218893Sdim // Cluster loads by adding MVT::Glue outputs and inputs. This also 272210299Sed // ensure they are scheduled in order of increasing addresses. 273210299Sed SDNode *Lead = Loads[0]; 274236144Sdim SDValue InGlue = SDValue(0, 0); 275236144Sdim if (AddGlue(Lead, InGlue, true, DAG)) 276236144Sdim InGlue = SDValue(Lead, Lead->getNumValues() - 1); 277210299Sed for (unsigned I = 1, E = Loads.size(); I != E; ++I) { 278218893Sdim bool OutGlue = I < E - 1; 279210299Sed SDNode *Load = Loads[I]; 280210299Sed 281236144Sdim // If AddGlue fails, we could leave an unsused glue value. This should not 282236144Sdim // cause any 283236144Sdim if (AddGlue(Load, InGlue, OutGlue, DAG)) { 284236144Sdim if (OutGlue) 285236144Sdim InGlue = SDValue(Load, Load->getNumValues() - 1); 286210299Sed 287236144Sdim ++LoadsClustered; 288236144Sdim } 289236144Sdim else if (!OutGlue && InGlue.getNode()) 290236144Sdim RemoveUnusedGlue(InGlue.getNode(), DAG); 291210299Sed } 292210299Sed} 293210299Sed 294210299Sed/// ClusterNodes - Cluster certain nodes which should be scheduled together. 295210299Sed/// 296210299Sedvoid ScheduleDAGSDNodes::ClusterNodes() { 297210299Sed for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(), 298210299Sed E = DAG->allnodes_end(); NI != E; ++NI) { 299210299Sed SDNode *Node = &*NI; 300210299Sed if (!Node || !Node->isMachineOpcode()) 301202878Srdivacky continue; 302202878Srdivacky 303210299Sed unsigned Opc = Node->getMachineOpcode(); 304224145Sdim const MCInstrDesc &MCID = TII->get(Opc); 305224145Sdim if (MCID.mayLoad()) 306210299Sed // Cluster loads from "near" addresses into combined SUnits. 307210299Sed ClusterNeighboringLoads(Node); 308202878Srdivacky } 309202878Srdivacky} 310202878Srdivacky 311193323Sedvoid ScheduleDAGSDNodes::BuildSchedUnits() { 312193323Sed // During scheduling, the NodeId field of SDNode is used to map SDNodes 313193323Sed // to their associated SUnits by holding SUnits table indices. A value 314193323Sed // of -1 means the SDNode does not yet have an associated SUnit. 315193323Sed unsigned NumNodes = 0; 316193323Sed for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(), 317193323Sed E = DAG->allnodes_end(); NI != E; ++NI) { 318193323Sed NI->setNodeId(-1); 319193323Sed ++NumNodes; 320193323Sed } 321193323Sed 322193323Sed // Reserve entries in the vector for each of the SUnits we are creating. This 323193323Sed // ensure that reallocation of the vector won't happen, so SUnit*'s won't get 324193323Sed // invalidated. 325193323Sed // FIXME: Multiply by 2 because we may clone nodes during scheduling. 326193323Sed // This is a temporary workaround. 327193323Sed SUnits.reserve(NumNodes * 2); 328218893Sdim 329204642Srdivacky // Add all nodes in depth first order. 330204642Srdivacky SmallVector<SDNode*, 64> Worklist; 331204642Srdivacky SmallPtrSet<SDNode*, 64> Visited; 332204642Srdivacky Worklist.push_back(DAG->getRoot().getNode()); 333204642Srdivacky Visited.insert(DAG->getRoot().getNode()); 334218893Sdim 335221345Sdim SmallVector<SUnit*, 8> CallSUnits; 336204642Srdivacky while (!Worklist.empty()) { 337204642Srdivacky SDNode *NI = Worklist.pop_back_val(); 338218893Sdim 339204642Srdivacky // Add all operands to the worklist unless they've already been added. 340204642Srdivacky for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i) 341204642Srdivacky if (Visited.insert(NI->getOperand(i).getNode())) 342204642Srdivacky Worklist.push_back(NI->getOperand(i).getNode()); 343218893Sdim 344193323Sed if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate. 345193323Sed continue; 346218893Sdim 347193323Sed // If this node has already been processed, stop now. 348193323Sed if (NI->getNodeId() != -1) continue; 349218893Sdim 350235633Sdim SUnit *NodeSUnit = newSUnit(NI); 351218893Sdim 352218893Sdim // See if anything is glued to this node, if so, add them to glued 353218893Sdim // nodes. Nodes can have at most one glue input and one glue output. Glue 354218893Sdim // is required to be the last operand and result of a node. 355218893Sdim 356218893Sdim // Scan up to find glued preds. 357193323Sed SDNode *N = NI; 358193323Sed while (N->getNumOperands() && 359218893Sdim N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) { 360193323Sed N = N->getOperand(N->getNumOperands()-1).getNode(); 361193323Sed assert(N->getNodeId() == -1 && "Node already inserted!"); 362193323Sed N->setNodeId(NodeSUnit->NodeNum); 363218893Sdim if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall()) 364218893Sdim NodeSUnit->isCall = true; 365193323Sed } 366218893Sdim 367218893Sdim // Scan down to find any glued succs. 368193323Sed N = NI; 369218893Sdim while (N->getValueType(N->getNumValues()-1) == MVT::Glue) { 370218893Sdim SDValue GlueVal(N, N->getNumValues()-1); 371218893Sdim 372218893Sdim // There are either zero or one users of the Glue result. 373218893Sdim bool HasGlueUse = false; 374218893Sdim for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); 375193323Sed UI != E; ++UI) 376218893Sdim if (GlueVal.isOperandOf(*UI)) { 377218893Sdim HasGlueUse = true; 378193323Sed assert(N->getNodeId() == -1 && "Node already inserted!"); 379193323Sed N->setNodeId(NodeSUnit->NodeNum); 380193323Sed N = *UI; 381218893Sdim if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall()) 382218893Sdim NodeSUnit->isCall = true; 383193323Sed break; 384193323Sed } 385218893Sdim if (!HasGlueUse) break; 386193323Sed } 387218893Sdim 388221345Sdim if (NodeSUnit->isCall) 389221345Sdim CallSUnits.push_back(NodeSUnit); 390221345Sdim 391221345Sdim // Schedule zero-latency TokenFactor below any nodes that may increase the 392221345Sdim // schedule height. Otherwise, ancestors of the TokenFactor may appear to 393221345Sdim // have false stalls. 394221345Sdim if (NI->getOpcode() == ISD::TokenFactor) 395221345Sdim NodeSUnit->isScheduleLow = true; 396221345Sdim 397218893Sdim // If there are glue operands involved, N is now the bottom-most node 398218893Sdim // of the sequence of nodes that are glued together. 399193323Sed // Update the SUnit. 400193323Sed NodeSUnit->setNode(N); 401193323Sed assert(N->getNodeId() == -1 && "Node already inserted!"); 402193323Sed N->setNodeId(NodeSUnit->NodeNum); 403193323Sed 404218893Sdim // Compute NumRegDefsLeft. This must be done before AddSchedEdges. 405218893Sdim InitNumRegDefsLeft(NodeSUnit); 406218893Sdim 407193323Sed // Assign the Latency field of NodeSUnit using target-provided information. 408235633Sdim computeLatency(NodeSUnit); 409193323Sed } 410221345Sdim 411221345Sdim // Find all call operands. 412221345Sdim while (!CallSUnits.empty()) { 413221345Sdim SUnit *SU = CallSUnits.pop_back_val(); 414221345Sdim for (const SDNode *SUNode = SU->getNode(); SUNode; 415221345Sdim SUNode = SUNode->getGluedNode()) { 416221345Sdim if (SUNode->getOpcode() != ISD::CopyToReg) 417221345Sdim continue; 418221345Sdim SDNode *SrcN = SUNode->getOperand(2).getNode(); 419221345Sdim if (isPassiveNode(SrcN)) continue; // Not scheduled. 420221345Sdim SUnit *SrcSU = &SUnits[SrcN->getNodeId()]; 421221345Sdim SrcSU->isCallOp = true; 422221345Sdim } 423221345Sdim } 424193323Sed} 425193323Sed 426193323Sedvoid ScheduleDAGSDNodes::AddSchedEdges() { 427224145Sdim const TargetSubtargetInfo &ST = TM.getSubtarget<TargetSubtargetInfo>(); 428198090Srdivacky 429198090Srdivacky // Check to see if the scheduler cares about latencies. 430235633Sdim bool UnitLatencies = forceUnitLatencies(); 431198090Srdivacky 432193323Sed // Pass 2: add the preds, succs, etc. 433193323Sed for (unsigned su = 0, e = SUnits.size(); su != e; ++su) { 434193323Sed SUnit *SU = &SUnits[su]; 435193323Sed SDNode *MainNode = SU->getNode(); 436218893Sdim 437193323Sed if (MainNode->isMachineOpcode()) { 438193323Sed unsigned Opc = MainNode->getMachineOpcode(); 439224145Sdim const MCInstrDesc &MCID = TII->get(Opc); 440224145Sdim for (unsigned i = 0; i != MCID.getNumOperands(); ++i) { 441224145Sdim if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) { 442193323Sed SU->isTwoAddress = true; 443193323Sed break; 444193323Sed } 445193323Sed } 446224145Sdim if (MCID.isCommutable()) 447193323Sed SU->isCommutable = true; 448193323Sed } 449218893Sdim 450193323Sed // Find all predecessors and successors of the group. 451218893Sdim for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) { 452193323Sed if (N->isMachineOpcode() && 453193323Sed TII->get(N->getMachineOpcode()).getImplicitDefs()) { 454193323Sed SU->hasPhysRegClobbers = true; 455198090Srdivacky unsigned NumUsed = InstrEmitter::CountResults(N); 456193323Sed while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1)) 457193323Sed --NumUsed; // Skip over unused values at the end. 458193323Sed if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs()) 459193323Sed SU->hasPhysRegDefs = true; 460193323Sed } 461218893Sdim 462193323Sed for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 463193323Sed SDNode *OpN = N->getOperand(i).getNode(); 464193323Sed if (isPassiveNode(OpN)) continue; // Not scheduled. 465193323Sed SUnit *OpSU = &SUnits[OpN->getNodeId()]; 466193323Sed assert(OpSU && "Node has no SUnit!"); 467193323Sed if (OpSU == SU) continue; // In the same group. 468193323Sed 469198090Srdivacky EVT OpVT = N->getOperand(i).getValueType(); 470218893Sdim assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!"); 471193323Sed bool isChain = OpVT == MVT::Other; 472193323Sed 473193323Sed unsigned PhysReg = 0; 474193323Sed int Cost = 1; 475193323Sed // Determine if this is a physical register dependency. 476193323Sed CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost); 477193323Sed assert((PhysReg == 0 || !isChain) && 478193323Sed "Chain dependence via physreg data?"); 479193323Sed // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler 480193323Sed // emits a copy from the physical register to a virtual register unless 481193323Sed // it requires a cross class copy (cost < 0). That means we are only 482193323Sed // treating "expensive to copy" register dependency as physical register 483193323Sed // dependency. This may change in the future though. 484224145Sdim if (Cost >= 0 && !StressSched) 485193323Sed PhysReg = 0; 486198090Srdivacky 487210299Sed // If this is a ctrl dep, latency is 1. 488210299Sed unsigned OpLatency = isChain ? 1 : OpSU->Latency; 489221345Sdim // Special-case TokenFactor chains as zero-latency. 490221345Sdim if(isChain && OpN->getOpcode() == ISD::TokenFactor) 491221345Sdim OpLatency = 0; 492221345Sdim 493245431Sdim SDep Dep = isChain ? SDep(OpSU, SDep::Barrier) 494245431Sdim : SDep(OpSU, SDep::Data, PhysReg); 495245431Sdim Dep.setLatency(OpLatency); 496198090Srdivacky if (!isChain && !UnitLatencies) { 497245431Sdim computeOperandLatency(OpN, N, i, Dep); 498245431Sdim ST.adjustSchedDependency(OpSU, SU, Dep); 499198090Srdivacky } 500198090Srdivacky 501245431Sdim if (!SU->addPred(Dep) && !Dep.isCtrl() && OpSU->NumRegDefsLeft > 1) { 502218893Sdim // Multiple register uses are combined in the same SUnit. For example, 503218893Sdim // we could have a set of glued nodes with all their defs consumed by 504218893Sdim // another set of glued nodes. Register pressure tracking sees this as 505218893Sdim // a single use, so to keep pressure balanced we reduce the defs. 506221345Sdim // 507221345Sdim // We can't tell (without more book-keeping) if this results from 508221345Sdim // glued nodes or duplicate operands. As long as we don't reduce 509221345Sdim // NumRegDefsLeft to zero, we handle the common cases well. 510218893Sdim --OpSU->NumRegDefsLeft; 511218893Sdim } 512193323Sed } 513193323Sed } 514193323Sed } 515193323Sed} 516193323Sed 517193323Sed/// BuildSchedGraph - Build the SUnit graph from the selection dag that we 518193323Sed/// are input. This SUnit graph is similar to the SelectionDAG, but 519193323Sed/// excludes nodes that aren't interesting to scheduling, and represents 520218893Sdim/// glued together nodes with a single SUnit. 521198090Srdivackyvoid ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) { 522210299Sed // Cluster certain nodes which should be scheduled together. 523210299Sed ClusterNodes(); 524193323Sed // Populate the SUnits array. 525193323Sed BuildSchedUnits(); 526193323Sed // Compute all the scheduling dependencies between nodes. 527193323Sed AddSchedEdges(); 528193323Sed} 529193323Sed 530218893Sdim// Initialize NumNodeDefs for the current Node's opcode. 531218893Sdimvoid ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() { 532221345Sdim // Check for phys reg copy. 533221345Sdim if (!Node) 534221345Sdim return; 535221345Sdim 536218893Sdim if (!Node->isMachineOpcode()) { 537218893Sdim if (Node->getOpcode() == ISD::CopyFromReg) 538218893Sdim NodeNumDefs = 1; 539218893Sdim else 540218893Sdim NodeNumDefs = 0; 541218893Sdim return; 542218893Sdim } 543218893Sdim unsigned POpc = Node->getMachineOpcode(); 544218893Sdim if (POpc == TargetOpcode::IMPLICIT_DEF) { 545218893Sdim // No register need be allocated for this. 546218893Sdim NodeNumDefs = 0; 547218893Sdim return; 548218893Sdim } 549218893Sdim unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs(); 550218893Sdim // Some instructions define regs that are not represented in the selection DAG 551218893Sdim // (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues. 552218893Sdim NodeNumDefs = std::min(Node->getNumValues(), NRegDefs); 553218893Sdim DefIdx = 0; 554218893Sdim} 555218893Sdim 556218893Sdim// Construct a RegDefIter for this SUnit and find the first valid value. 557218893SdimScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU, 558218893Sdim const ScheduleDAGSDNodes *SD) 559218893Sdim : SchedDAG(SD), Node(SU->getNode()), DefIdx(0), NodeNumDefs(0) { 560218893Sdim InitNodeNumDefs(); 561218893Sdim Advance(); 562218893Sdim} 563218893Sdim 564218893Sdim// Advance to the next valid value defined by the SUnit. 565218893Sdimvoid ScheduleDAGSDNodes::RegDefIter::Advance() { 566218893Sdim for (;Node;) { // Visit all glued nodes. 567218893Sdim for (;DefIdx < NodeNumDefs; ++DefIdx) { 568218893Sdim if (!Node->hasAnyUseOfValue(DefIdx)) 569218893Sdim continue; 570252723Sdim ValueType = Node->getSimpleValueType(DefIdx); 571218893Sdim ++DefIdx; 572218893Sdim return; // Found a normal regdef. 573218893Sdim } 574218893Sdim Node = Node->getGluedNode(); 575218893Sdim if (Node == NULL) { 576218893Sdim return; // No values left to visit. 577218893Sdim } 578218893Sdim InitNodeNumDefs(); 579218893Sdim } 580218893Sdim} 581218893Sdim 582218893Sdimvoid ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) { 583218893Sdim assert(SU->NumRegDefsLeft == 0 && "expect a new node"); 584218893Sdim for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) { 585218893Sdim assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected"); 586218893Sdim ++SU->NumRegDefsLeft; 587218893Sdim } 588218893Sdim} 589218893Sdim 590235633Sdimvoid ScheduleDAGSDNodes::computeLatency(SUnit *SU) { 591221345Sdim SDNode *N = SU->getNode(); 592221345Sdim 593221345Sdim // TokenFactor operands are considered zero latency, and some schedulers 594221345Sdim // (e.g. Top-Down list) may rely on the fact that operand latency is nonzero 595221345Sdim // whenever node latency is nonzero. 596221345Sdim if (N && N->getOpcode() == ISD::TokenFactor) { 597221345Sdim SU->Latency = 0; 598221345Sdim return; 599221345Sdim } 600221345Sdim 601208599Srdivacky // Check to see if the scheduler cares about latencies. 602235633Sdim if (forceUnitLatencies()) { 603208599Srdivacky SU->Latency = 1; 604208599Srdivacky return; 605208599Srdivacky } 606208599Srdivacky 607218893Sdim if (!InstrItins || InstrItins->isEmpty()) { 608221345Sdim if (N && N->isMachineOpcode() && 609221345Sdim TII->isHighLatencyDef(N->getMachineOpcode())) 610221345Sdim SU->Latency = HighLatencyCycles; 611221345Sdim else 612221345Sdim SU->Latency = 1; 613208599Srdivacky return; 614208599Srdivacky } 615218893Sdim 616193323Sed // Compute the latency for the node. We use the sum of the latencies for 617218893Sdim // all nodes glued together into this SUnit. 618193323Sed SU->Latency = 0; 619218893Sdim for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) 620218893Sdim if (N->isMachineOpcode()) 621218893Sdim SU->Latency += TII->getInstrLatency(InstrItins, N); 622193323Sed} 623193323Sed 624235633Sdimvoid ScheduleDAGSDNodes::computeOperandLatency(SDNode *Def, SDNode *Use, 625208599Srdivacky unsigned OpIdx, SDep& dep) const{ 626208599Srdivacky // Check to see if the scheduler cares about latencies. 627235633Sdim if (forceUnitLatencies()) 628208599Srdivacky return; 629208599Srdivacky 630208599Srdivacky if (dep.getKind() != SDep::Data) 631208599Srdivacky return; 632208599Srdivacky 633208599Srdivacky unsigned DefIdx = Use->getOperand(OpIdx).getResNo(); 634218893Sdim if (Use->isMachineOpcode()) 635218893Sdim // Adjust the use operand index by num of defs. 636218893Sdim OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs(); 637218893Sdim int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx); 638218893Sdim if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg && 639218893Sdim !BB->succ_empty()) { 640218893Sdim unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg(); 641218893Sdim if (TargetRegisterInfo::isVirtualRegister(Reg)) 642218893Sdim // This copy is a liveout value. It is likely coalesced, so reduce the 643218893Sdim // latency so not to penalize the def. 644218893Sdim // FIXME: need target specific adjustment here? 645218893Sdim Latency = (Latency > 1) ? Latency - 1 : 1; 646208599Srdivacky } 647218893Sdim if (Latency >= 0) 648218893Sdim dep.setLatency(Latency); 649208599Srdivacky} 650208599Srdivacky 651193323Sedvoid ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const { 652245431Sdim#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 653193323Sed if (!SU->getNode()) { 654202375Srdivacky dbgs() << "PHYS REG COPY\n"; 655193323Sed return; 656193323Sed } 657193323Sed 658193323Sed SU->getNode()->dump(DAG); 659202375Srdivacky dbgs() << "\n"; 660218893Sdim SmallVector<SDNode *, 4> GluedNodes; 661218893Sdim for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode()) 662218893Sdim GluedNodes.push_back(N); 663218893Sdim while (!GluedNodes.empty()) { 664202375Srdivacky dbgs() << " "; 665218893Sdim GluedNodes.back()->dump(DAG); 666202375Srdivacky dbgs() << "\n"; 667218893Sdim GluedNodes.pop_back(); 668193323Sed } 669245431Sdim#endif 670193323Sed} 671198090Srdivacky 672245431Sdim#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 673235633Sdimvoid ScheduleDAGSDNodes::dumpSchedule() const { 674235633Sdim for (unsigned i = 0, e = Sequence.size(); i != e; i++) { 675235633Sdim if (SUnit *SU = Sequence[i]) 676235633Sdim SU->dump(this); 677235633Sdim else 678235633Sdim dbgs() << "**** NOOP ****\n"; 679235633Sdim } 680235633Sdim} 681245431Sdim#endif 682235633Sdim 683235633Sdim#ifndef NDEBUG 684235633Sdim/// VerifyScheduledSequence - Verify that all SUnits were scheduled and that 685235633Sdim/// their state is consistent with the nodes listed in Sequence. 686235633Sdim/// 687235633Sdimvoid ScheduleDAGSDNodes::VerifyScheduledSequence(bool isBottomUp) { 688235633Sdim unsigned ScheduledNodes = ScheduleDAG::VerifyScheduledDAG(isBottomUp); 689235633Sdim unsigned Noops = 0; 690235633Sdim for (unsigned i = 0, e = Sequence.size(); i != e; ++i) 691235633Sdim if (!Sequence[i]) 692235633Sdim ++Noops; 693235633Sdim assert(Sequence.size() - Noops == ScheduledNodes && 694235633Sdim "The number of nodes scheduled doesn't match the expected number!"); 695235633Sdim} 696235633Sdim#endif // NDEBUG 697235633Sdim 698221345Sdim/// ProcessSDDbgValues - Process SDDbgValues associated with this node. 699263509Sdimstatic void 700263509SdimProcessSDDbgValues(SDNode *N, SelectionDAG *DAG, InstrEmitter &Emitter, 701263509Sdim SmallVectorImpl<std::pair<unsigned, MachineInstr*> > &Orders, 702263509Sdim DenseMap<SDValue, unsigned> &VRBaseMap, unsigned Order) { 703218893Sdim if (!N->getHasDebugValue()) 704206083Srdivacky return; 705206083Srdivacky 706206083Srdivacky // Opportunistically insert immediate dbg_value uses, i.e. those with source 707206083Srdivacky // order number right after the N. 708218893Sdim MachineBasicBlock *BB = Emitter.getBlock(); 709206083Srdivacky MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos(); 710224145Sdim ArrayRef<SDDbgValue*> DVs = DAG->GetDbgValues(N); 711206083Srdivacky for (unsigned i = 0, e = DVs.size(); i != e; ++i) { 712206083Srdivacky if (DVs[i]->isInvalidated()) 713206083Srdivacky continue; 714206083Srdivacky unsigned DVOrder = DVs[i]->getOrder(); 715218893Sdim if (!Order || DVOrder == ++Order) { 716207618Srdivacky MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap); 717207618Srdivacky if (DbgMI) { 718207618Srdivacky Orders.push_back(std::make_pair(DVOrder, DbgMI)); 719207618Srdivacky BB->insert(InsertPos, DbgMI); 720207618Srdivacky } 721206083Srdivacky DVs[i]->setIsInvalidated(); 722206083Srdivacky } 723206083Srdivacky } 724206083Srdivacky} 725206083Srdivacky 726218893Sdim// ProcessSourceNode - Process nodes with source order numbers. These are added 727218893Sdim// to a vector which EmitSchedule uses to determine how to insert dbg_value 728218893Sdim// instructions in the right order. 729263509Sdimstatic void 730263509SdimProcessSourceNode(SDNode *N, SelectionDAG *DAG, InstrEmitter &Emitter, 731263509Sdim DenseMap<SDValue, unsigned> &VRBaseMap, 732263509Sdim SmallVectorImpl<std::pair<unsigned, MachineInstr*> > &Orders, 733263509Sdim SmallSet<unsigned, 8> &Seen) { 734263509Sdim unsigned Order = N->getIROrder(); 735218893Sdim if (!Order || !Seen.insert(Order)) { 736218893Sdim // Process any valid SDDbgValues even if node does not have any order 737218893Sdim // assigned. 738218893Sdim ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0); 739218893Sdim return; 740218893Sdim } 741206083Srdivacky 742218893Sdim MachineBasicBlock *BB = Emitter.getBlock(); 743263509Sdim if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI() || 744263509Sdim // Fast-isel may have inserted some instructions, in which case the 745263509Sdim // BB->back().isPHI() test will not fire when we want it to. 746263509Sdim prior(Emitter.getInsertPos())->isPHI()) { 747218893Sdim // Did not insert any instruction. 748218893Sdim Orders.push_back(std::make_pair(Order, (MachineInstr*)0)); 749218893Sdim return; 750218893Sdim } 751218893Sdim 752218893Sdim Orders.push_back(std::make_pair(Order, prior(Emitter.getInsertPos()))); 753218893Sdim ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order); 754218893Sdim} 755218893Sdim 756235633Sdimvoid ScheduleDAGSDNodes:: 757235633SdimEmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap, 758235633Sdim MachineBasicBlock::iterator InsertPos) { 759235633Sdim for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); 760235633Sdim I != E; ++I) { 761235633Sdim if (I->isCtrl()) continue; // ignore chain preds 762235633Sdim if (I->getSUnit()->CopyDstRC) { 763235633Sdim // Copy to physical register. 764235633Sdim DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->getSUnit()); 765235633Sdim assert(VRI != VRBaseMap.end() && "Node emitted out of order - late"); 766235633Sdim // Find the destination physical register. 767235633Sdim unsigned Reg = 0; 768235633Sdim for (SUnit::const_succ_iterator II = SU->Succs.begin(), 769235633Sdim EE = SU->Succs.end(); II != EE; ++II) { 770235633Sdim if (II->isCtrl()) continue; // ignore chain preds 771235633Sdim if (II->getReg()) { 772235633Sdim Reg = II->getReg(); 773235633Sdim break; 774235633Sdim } 775235633Sdim } 776235633Sdim BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), Reg) 777235633Sdim .addReg(VRI->second); 778235633Sdim } else { 779235633Sdim // Copy from physical register. 780235633Sdim assert(I->getReg() && "Unknown physical register!"); 781235633Sdim unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC); 782235633Sdim bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase)).second; 783235633Sdim (void)isNew; // Silence compiler warning. 784235633Sdim assert(isNew && "Node emitted out of order - early"); 785235633Sdim BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), VRBase) 786235633Sdim .addReg(I->getReg()); 787235633Sdim } 788235633Sdim break; 789235633Sdim } 790235633Sdim} 791218893Sdim 792235633Sdim/// EmitSchedule - Emit the machine code in scheduled order. Return the new 793235633Sdim/// InsertPos and MachineBasicBlock that contains this insertion 794235633Sdim/// point. ScheduleDAGSDNodes holds a BB pointer for convenience, but this does 795235633Sdim/// not necessarily refer to returned BB. The emitter may split blocks. 796235633SdimMachineBasicBlock *ScheduleDAGSDNodes:: 797235633SdimEmitSchedule(MachineBasicBlock::iterator &InsertPos) { 798198090Srdivacky InstrEmitter Emitter(BB, InsertPos); 799198090Srdivacky DenseMap<SDValue, unsigned> VRBaseMap; 800198090Srdivacky DenseMap<SUnit*, unsigned> CopyVRBaseMap; 801206083Srdivacky SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders; 802206083Srdivacky SmallSet<unsigned, 8> Seen; 803206083Srdivacky bool HasDbg = DAG->hasDebugValues(); 804205218Srdivacky 805207618Srdivacky // If this is the first BB, emit byval parameter dbg_value's. 806207618Srdivacky if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) { 807207618Srdivacky SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin(); 808207618Srdivacky SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd(); 809207618Srdivacky for (; PDI != PDE; ++PDI) { 810207618Srdivacky MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap); 811207618Srdivacky if (DbgMI) 812210299Sed BB->insert(InsertPos, DbgMI); 813207618Srdivacky } 814207618Srdivacky } 815207618Srdivacky 816198090Srdivacky for (unsigned i = 0, e = Sequence.size(); i != e; i++) { 817198090Srdivacky SUnit *SU = Sequence[i]; 818198090Srdivacky if (!SU) { 819198090Srdivacky // Null SUnit* is a noop. 820235633Sdim TII->insertNoop(*Emitter.getBlock(), InsertPos); 821198090Srdivacky continue; 822198090Srdivacky } 823198090Srdivacky 824198090Srdivacky // For pre-regalloc scheduling, create instructions corresponding to the 825218893Sdim // SDNode and any glued SDNodes and append them to the block. 826198090Srdivacky if (!SU->getNode()) { 827198090Srdivacky // Emit a copy. 828235633Sdim EmitPhysRegCopy(SU, CopyVRBaseMap, InsertPos); 829198090Srdivacky continue; 830198090Srdivacky } 831198090Srdivacky 832218893Sdim SmallVector<SDNode *, 4> GluedNodes; 833245431Sdim for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode()) 834218893Sdim GluedNodes.push_back(N); 835218893Sdim while (!GluedNodes.empty()) { 836218893Sdim SDNode *N = GluedNodes.back(); 837218893Sdim Emitter.EmitNode(GluedNodes.back(), SU->OrigNode != SU, SU->isCloned, 838207618Srdivacky VRBaseMap); 839207618Srdivacky // Remember the source order of the inserted instruction. 840206083Srdivacky if (HasDbg) 841207618Srdivacky ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen); 842218893Sdim GluedNodes.pop_back(); 843198090Srdivacky } 844198090Srdivacky Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, 845207618Srdivacky VRBaseMap); 846207618Srdivacky // Remember the source order of the inserted instruction. 847206083Srdivacky if (HasDbg) 848207618Srdivacky ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders, 849206083Srdivacky Seen); 850206083Srdivacky } 851206083Srdivacky 852207618Srdivacky // Insert all the dbg_values which have not already been inserted in source 853206083Srdivacky // order sequence. 854206083Srdivacky if (HasDbg) { 855210299Sed MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI(); 856206083Srdivacky 857206083Srdivacky // Sort the source order instructions and use the order to insert debug 858206083Srdivacky // values. 859263509Sdim std::sort(Orders.begin(), Orders.end(), less_first()); 860206083Srdivacky 861206083Srdivacky SDDbgInfo::DbgIterator DI = DAG->DbgBegin(); 862206083Srdivacky SDDbgInfo::DbgIterator DE = DAG->DbgEnd(); 863206083Srdivacky // Now emit the rest according to source order. 864206083Srdivacky unsigned LastOrder = 0; 865206083Srdivacky for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) { 866206083Srdivacky unsigned Order = Orders[i].first; 867206083Srdivacky MachineInstr *MI = Orders[i].second; 868206083Srdivacky // Insert all SDDbgValue's whose order(s) are before "Order". 869206083Srdivacky if (!MI) 870206083Srdivacky continue; 871206083Srdivacky for (; DI != DE && 872206083Srdivacky (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) { 873206083Srdivacky if ((*DI)->isInvalidated()) 874206083Srdivacky continue; 875207618Srdivacky MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap); 876207618Srdivacky if (DbgMI) { 877207618Srdivacky if (!LastOrder) 878207618Srdivacky // Insert to start of the BB (after PHIs). 879207618Srdivacky BB->insert(BBBegin, DbgMI); 880207618Srdivacky else { 881210299Sed // Insert at the instruction, which may be in a different 882210299Sed // block, if the block was split by a custom inserter. 883207618Srdivacky MachineBasicBlock::iterator Pos = MI; 884263509Sdim MI->getParent()->insert(Pos, DbgMI); 885207618Srdivacky } 886206083Srdivacky } 887205218Srdivacky } 888206083Srdivacky LastOrder = Order; 889206083Srdivacky } 890206083Srdivacky // Add trailing DbgValue's before the terminator. FIXME: May want to add 891206083Srdivacky // some of them before one or more conditional branches? 892235633Sdim SmallVector<MachineInstr*, 8> DbgMIs; 893206083Srdivacky while (DI != DE) { 894235633Sdim if (!(*DI)->isInvalidated()) 895235633Sdim if (MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap)) 896235633Sdim DbgMIs.push_back(DbgMI); 897206083Srdivacky ++DI; 898206083Srdivacky } 899235633Sdim 900235633Sdim MachineBasicBlock *InsertBB = Emitter.getBlock(); 901235633Sdim MachineBasicBlock::iterator Pos = InsertBB->getFirstTerminator(); 902235633Sdim InsertBB->insert(Pos, DbgMIs.begin(), DbgMIs.end()); 903198090Srdivacky } 904198090Srdivacky 905198090Srdivacky InsertPos = Emitter.getInsertPos(); 906235633Sdim return Emitter.getBlock(); 907198090Srdivacky} 908235633Sdim 909235633Sdim/// Return the basic block label. 910235633Sdimstd::string ScheduleDAGSDNodes::getDAGName() const { 911235633Sdim return "sunit-dag." + BB->getFullName(); 912235633Sdim} 913