ScheduleDAGSDNodes.cpp revision 29d8f0cae425f1bba583565227eaebf58f26ce73
1//===--- ScheduleDAGSDNodes.cpp - Implement the ScheduleDAGSDNodes class --===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This implements the ScheduleDAG class, which is a base class used by 11// scheduling implementation classes. 12// 13//===----------------------------------------------------------------------===// 14 15#define DEBUG_TYPE "pre-RA-sched" 16#include "SDNodeDbgValue.h" 17#include "ScheduleDAGSDNodes.h" 18#include "InstrEmitter.h" 19#include "llvm/CodeGen/SelectionDAG.h" 20#include "llvm/Target/TargetMachine.h" 21#include "llvm/Target/TargetInstrInfo.h" 22#include "llvm/Target/TargetLowering.h" 23#include "llvm/Target/TargetRegisterInfo.h" 24#include "llvm/Target/TargetSubtarget.h" 25#include "llvm/ADT/DenseMap.h" 26#include "llvm/ADT/SmallPtrSet.h" 27#include "llvm/ADT/SmallSet.h" 28#include "llvm/ADT/SmallVector.h" 29#include "llvm/ADT/Statistic.h" 30#include "llvm/Support/Debug.h" 31#include "llvm/Support/raw_ostream.h" 32using namespace llvm; 33 34STATISTIC(LoadsClustered, "Number of loads clustered together"); 35 36ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf) 37 : ScheduleDAG(mf), 38 InstrItins(mf.getTarget().getInstrItineraryData()) {} 39 40/// Run - perform scheduling. 41/// 42void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb, 43 MachineBasicBlock::iterator insertPos) { 44 DAG = dag; 45 ScheduleDAG::Run(bb, insertPos); 46} 47 48/// NewSUnit - Creates a new SUnit and return a ptr to it. 49/// 50SUnit *ScheduleDAGSDNodes::NewSUnit(SDNode *N) { 51#ifndef NDEBUG 52 const SUnit *Addr = 0; 53 if (!SUnits.empty()) 54 Addr = &SUnits[0]; 55#endif 56 SUnits.push_back(SUnit(N, (unsigned)SUnits.size())); 57 assert((Addr == 0 || Addr == &SUnits[0]) && 58 "SUnits std::vector reallocated on the fly!"); 59 SUnits.back().OrigNode = &SUnits.back(); 60 SUnit *SU = &SUnits.back(); 61 const TargetLowering &TLI = DAG->getTargetLoweringInfo(); 62 if (!N || 63 (N->isMachineOpcode() && 64 N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF)) 65 SU->SchedulingPref = Sched::None; 66 else 67 SU->SchedulingPref = TLI.getSchedulingPreference(N); 68 return SU; 69} 70 71SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) { 72 SUnit *SU = NewSUnit(Old->getNode()); 73 SU->OrigNode = Old->OrigNode; 74 SU->Latency = Old->Latency; 75 SU->isCall = Old->isCall; 76 SU->isTwoAddress = Old->isTwoAddress; 77 SU->isCommutable = Old->isCommutable; 78 SU->hasPhysRegDefs = Old->hasPhysRegDefs; 79 SU->hasPhysRegClobbers = Old->hasPhysRegClobbers; 80 SU->SchedulingPref = Old->SchedulingPref; 81 Old->isCloned = true; 82 return SU; 83} 84 85/// CheckForPhysRegDependency - Check if the dependency between def and use of 86/// a specified operand is a physical register dependency. If so, returns the 87/// register and the cost of copying the register. 88static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op, 89 const TargetRegisterInfo *TRI, 90 const TargetInstrInfo *TII, 91 unsigned &PhysReg, int &Cost) { 92 if (Op != 2 || User->getOpcode() != ISD::CopyToReg) 93 return; 94 95 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); 96 if (TargetRegisterInfo::isVirtualRegister(Reg)) 97 return; 98 99 unsigned ResNo = User->getOperand(2).getResNo(); 100 if (Def->isMachineOpcode()) { 101 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode()); 102 if (ResNo >= II.getNumDefs() && 103 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) { 104 PhysReg = Reg; 105 const TargetRegisterClass *RC = 106 TRI->getMinimalPhysRegClass(Reg, Def->getValueType(ResNo)); 107 Cost = RC->getCopyCost(); 108 } 109 } 110} 111 112static void AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) { 113 SmallVector<EVT, 4> VTs; 114 SDNode *GlueDestNode = Glue.getNode(); 115 116 // Don't add glue from a node to itself. 117 if (GlueDestNode == N) return; 118 119 // Don't add glue to something which already has glue. 120 if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return; 121 122 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I) 123 VTs.push_back(N->getValueType(I)); 124 125 if (AddGlue) 126 VTs.push_back(MVT::Glue); 127 128 SmallVector<SDValue, 4> Ops; 129 for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I) 130 Ops.push_back(N->getOperand(I)); 131 132 if (GlueDestNode) 133 Ops.push_back(Glue); 134 135 SDVTList VTList = DAG->getVTList(&VTs[0], VTs.size()); 136 MachineSDNode::mmo_iterator Begin = 0, End = 0; 137 MachineSDNode *MN = dyn_cast<MachineSDNode>(N); 138 139 // Store memory references. 140 if (MN) { 141 Begin = MN->memoperands_begin(); 142 End = MN->memoperands_end(); 143 } 144 145 DAG->MorphNodeTo(N, N->getOpcode(), VTList, &Ops[0], Ops.size()); 146 147 // Reset the memory references 148 if (MN) 149 MN->setMemRefs(Begin, End); 150} 151 152/// ClusterNeighboringLoads - Force nearby loads together by "gluing" them. 153/// This function finds loads of the same base and different offsets. If the 154/// offsets are not far apart (target specific), it add MVT::Glue inputs and 155/// outputs to ensure they are scheduled together and in order. This 156/// optimization may benefit some targets by improving cache locality. 157void ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) { 158 SDNode *Chain = 0; 159 unsigned NumOps = Node->getNumOperands(); 160 if (Node->getOperand(NumOps-1).getValueType() == MVT::Other) 161 Chain = Node->getOperand(NumOps-1).getNode(); 162 if (!Chain) 163 return; 164 165 // Look for other loads of the same chain. Find loads that are loading from 166 // the same base pointer and different offsets. 167 SmallPtrSet<SDNode*, 16> Visited; 168 SmallVector<int64_t, 4> Offsets; 169 DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode. 170 bool Cluster = false; 171 SDNode *Base = Node; 172 for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end(); 173 I != E; ++I) { 174 SDNode *User = *I; 175 if (User == Node || !Visited.insert(User)) 176 continue; 177 int64_t Offset1, Offset2; 178 if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) || 179 Offset1 == Offset2) 180 // FIXME: Should be ok if they addresses are identical. But earlier 181 // optimizations really should have eliminated one of the loads. 182 continue; 183 if (O2SMap.insert(std::make_pair(Offset1, Base)).second) 184 Offsets.push_back(Offset1); 185 O2SMap.insert(std::make_pair(Offset2, User)); 186 Offsets.push_back(Offset2); 187 if (Offset2 < Offset1) 188 Base = User; 189 Cluster = true; 190 } 191 192 if (!Cluster) 193 return; 194 195 // Sort them in increasing order. 196 std::sort(Offsets.begin(), Offsets.end()); 197 198 // Check if the loads are close enough. 199 SmallVector<SDNode*, 4> Loads; 200 unsigned NumLoads = 0; 201 int64_t BaseOff = Offsets[0]; 202 SDNode *BaseLoad = O2SMap[BaseOff]; 203 Loads.push_back(BaseLoad); 204 for (unsigned i = 1, e = Offsets.size(); i != e; ++i) { 205 int64_t Offset = Offsets[i]; 206 SDNode *Load = O2SMap[Offset]; 207 if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads)) 208 break; // Stop right here. Ignore loads that are further away. 209 Loads.push_back(Load); 210 ++NumLoads; 211 } 212 213 if (NumLoads == 0) 214 return; 215 216 // Cluster loads by adding MVT::Glue outputs and inputs. This also 217 // ensure they are scheduled in order of increasing addresses. 218 SDNode *Lead = Loads[0]; 219 AddGlue(Lead, SDValue(0, 0), true, DAG); 220 221 SDValue InGlue = SDValue(Lead, Lead->getNumValues() - 1); 222 for (unsigned I = 1, E = Loads.size(); I != E; ++I) { 223 bool OutGlue = I < E - 1; 224 SDNode *Load = Loads[I]; 225 226 AddGlue(Load, InGlue, OutGlue, DAG); 227 228 if (OutGlue) 229 InGlue = SDValue(Load, Load->getNumValues() - 1); 230 231 ++LoadsClustered; 232 } 233} 234 235/// ClusterNodes - Cluster certain nodes which should be scheduled together. 236/// 237void ScheduleDAGSDNodes::ClusterNodes() { 238 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(), 239 E = DAG->allnodes_end(); NI != E; ++NI) { 240 SDNode *Node = &*NI; 241 if (!Node || !Node->isMachineOpcode()) 242 continue; 243 244 unsigned Opc = Node->getMachineOpcode(); 245 const TargetInstrDesc &TID = TII->get(Opc); 246 if (TID.mayLoad()) 247 // Cluster loads from "near" addresses into combined SUnits. 248 ClusterNeighboringLoads(Node); 249 } 250} 251 252void ScheduleDAGSDNodes::BuildSchedUnits() { 253 // During scheduling, the NodeId field of SDNode is used to map SDNodes 254 // to their associated SUnits by holding SUnits table indices. A value 255 // of -1 means the SDNode does not yet have an associated SUnit. 256 unsigned NumNodes = 0; 257 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(), 258 E = DAG->allnodes_end(); NI != E; ++NI) { 259 NI->setNodeId(-1); 260 ++NumNodes; 261 } 262 263 // Reserve entries in the vector for each of the SUnits we are creating. This 264 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get 265 // invalidated. 266 // FIXME: Multiply by 2 because we may clone nodes during scheduling. 267 // This is a temporary workaround. 268 SUnits.reserve(NumNodes * 2); 269 270 // Add all nodes in depth first order. 271 SmallVector<SDNode*, 64> Worklist; 272 SmallPtrSet<SDNode*, 64> Visited; 273 Worklist.push_back(DAG->getRoot().getNode()); 274 Visited.insert(DAG->getRoot().getNode()); 275 276 while (!Worklist.empty()) { 277 SDNode *NI = Worklist.pop_back_val(); 278 279 // Add all operands to the worklist unless they've already been added. 280 for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i) 281 if (Visited.insert(NI->getOperand(i).getNode())) 282 Worklist.push_back(NI->getOperand(i).getNode()); 283 284 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate. 285 continue; 286 287 // If this node has already been processed, stop now. 288 if (NI->getNodeId() != -1) continue; 289 290 SUnit *NodeSUnit = NewSUnit(NI); 291 292 // See if anything is glued to this node, if so, add them to glued 293 // nodes. Nodes can have at most one glue input and one glue output. Glue 294 // is required to be the last operand and result of a node. 295 296 // Scan up to find glued preds. 297 SDNode *N = NI; 298 while (N->getNumOperands() && 299 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) { 300 N = N->getOperand(N->getNumOperands()-1).getNode(); 301 assert(N->getNodeId() == -1 && "Node already inserted!"); 302 N->setNodeId(NodeSUnit->NodeNum); 303 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall()) 304 NodeSUnit->isCall = true; 305 } 306 307 // Scan down to find any glued succs. 308 N = NI; 309 while (N->getValueType(N->getNumValues()-1) == MVT::Glue) { 310 SDValue GlueVal(N, N->getNumValues()-1); 311 312 // There are either zero or one users of the Glue result. 313 bool HasGlueUse = false; 314 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); 315 UI != E; ++UI) 316 if (GlueVal.isOperandOf(*UI)) { 317 HasGlueUse = true; 318 assert(N->getNodeId() == -1 && "Node already inserted!"); 319 N->setNodeId(NodeSUnit->NodeNum); 320 N = *UI; 321 if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall()) 322 NodeSUnit->isCall = true; 323 break; 324 } 325 if (!HasGlueUse) break; 326 } 327 328 // If there are glue operands involved, N is now the bottom-most node 329 // of the sequence of nodes that are glued together. 330 // Update the SUnit. 331 NodeSUnit->setNode(N); 332 assert(N->getNodeId() == -1 && "Node already inserted!"); 333 N->setNodeId(NodeSUnit->NodeNum); 334 335 // Assign the Latency field of NodeSUnit using target-provided information. 336 ComputeLatency(NodeSUnit); 337 } 338} 339 340void ScheduleDAGSDNodes::AddSchedEdges() { 341 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>(); 342 343 // Check to see if the scheduler cares about latencies. 344 bool UnitLatencies = ForceUnitLatencies(); 345 346 // Pass 2: add the preds, succs, etc. 347 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) { 348 SUnit *SU = &SUnits[su]; 349 SDNode *MainNode = SU->getNode(); 350 351 if (MainNode->isMachineOpcode()) { 352 unsigned Opc = MainNode->getMachineOpcode(); 353 const TargetInstrDesc &TID = TII->get(Opc); 354 for (unsigned i = 0; i != TID.getNumOperands(); ++i) { 355 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) { 356 SU->isTwoAddress = true; 357 break; 358 } 359 } 360 if (TID.isCommutable()) 361 SU->isCommutable = true; 362 } 363 364 // Find all predecessors and successors of the group. 365 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) { 366 if (N->isMachineOpcode() && 367 TII->get(N->getMachineOpcode()).getImplicitDefs()) { 368 SU->hasPhysRegClobbers = true; 369 unsigned NumUsed = InstrEmitter::CountResults(N); 370 while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1)) 371 --NumUsed; // Skip over unused values at the end. 372 if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs()) 373 SU->hasPhysRegDefs = true; 374 } 375 376 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 377 SDNode *OpN = N->getOperand(i).getNode(); 378 if (isPassiveNode(OpN)) continue; // Not scheduled. 379 SUnit *OpSU = &SUnits[OpN->getNodeId()]; 380 assert(OpSU && "Node has no SUnit!"); 381 if (OpSU == SU) continue; // In the same group. 382 383 EVT OpVT = N->getOperand(i).getValueType(); 384 assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!"); 385 bool isChain = OpVT == MVT::Other; 386 387 unsigned PhysReg = 0; 388 int Cost = 1; 389 // Determine if this is a physical register dependency. 390 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost); 391 assert((PhysReg == 0 || !isChain) && 392 "Chain dependence via physreg data?"); 393 // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler 394 // emits a copy from the physical register to a virtual register unless 395 // it requires a cross class copy (cost < 0). That means we are only 396 // treating "expensive to copy" register dependency as physical register 397 // dependency. This may change in the future though. 398 if (Cost >= 0) 399 PhysReg = 0; 400 401 // If this is a ctrl dep, latency is 1. 402 unsigned OpLatency = isChain ? 1 : OpSU->Latency; 403 const SDep &dep = SDep(OpSU, isChain ? SDep::Order : SDep::Data, 404 OpLatency, PhysReg); 405 if (!isChain && !UnitLatencies) { 406 ComputeOperandLatency(OpN, N, i, const_cast<SDep &>(dep)); 407 ST.adjustSchedDependency(OpSU, SU, const_cast<SDep &>(dep)); 408 } 409 410 SU->addPred(dep); 411 } 412 } 413 } 414} 415 416/// BuildSchedGraph - Build the SUnit graph from the selection dag that we 417/// are input. This SUnit graph is similar to the SelectionDAG, but 418/// excludes nodes that aren't interesting to scheduling, and represents 419/// glued together nodes with a single SUnit. 420void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) { 421 // Cluster certain nodes which should be scheduled together. 422 ClusterNodes(); 423 // Populate the SUnits array. 424 BuildSchedUnits(); 425 // Compute all the scheduling dependencies between nodes. 426 AddSchedEdges(); 427} 428 429void ScheduleDAGSDNodes::ComputeLatency(SUnit *SU) { 430 // Check to see if the scheduler cares about latencies. 431 if (ForceUnitLatencies()) { 432 SU->Latency = 1; 433 return; 434 } 435 436 if (!InstrItins || InstrItins->isEmpty()) { 437 SU->Latency = 1; 438 return; 439 } 440 441 // Compute the latency for the node. We use the sum of the latencies for 442 // all nodes glued together into this SUnit. 443 SU->Latency = 0; 444 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) 445 if (N->isMachineOpcode()) 446 SU->Latency += TII->getInstrLatency(InstrItins, N); 447} 448 449void ScheduleDAGSDNodes::ComputeOperandLatency(SDNode *Def, SDNode *Use, 450 unsigned OpIdx, SDep& dep) const{ 451 // Check to see if the scheduler cares about latencies. 452 if (ForceUnitLatencies()) 453 return; 454 455 if (dep.getKind() != SDep::Data) 456 return; 457 458 unsigned DefIdx = Use->getOperand(OpIdx).getResNo(); 459 if (Use->isMachineOpcode()) 460 // Adjust the use operand index by num of defs. 461 OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs(); 462 int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx); 463 if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg && 464 !BB->succ_empty()) { 465 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg(); 466 if (TargetRegisterInfo::isVirtualRegister(Reg)) 467 // This copy is a liveout value. It is likely coalesced, so reduce the 468 // latency so not to penalize the def. 469 // FIXME: need target specific adjustment here? 470 Latency = (Latency > 1) ? Latency - 1 : 1; 471 } 472 if (Latency >= 0) 473 dep.setLatency(Latency); 474} 475 476void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const { 477 if (!SU->getNode()) { 478 dbgs() << "PHYS REG COPY\n"; 479 return; 480 } 481 482 SU->getNode()->dump(DAG); 483 dbgs() << "\n"; 484 SmallVector<SDNode *, 4> GluedNodes; 485 for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode()) 486 GluedNodes.push_back(N); 487 while (!GluedNodes.empty()) { 488 dbgs() << " "; 489 GluedNodes.back()->dump(DAG); 490 dbgs() << "\n"; 491 GluedNodes.pop_back(); 492 } 493} 494 495namespace { 496 struct OrderSorter { 497 bool operator()(const std::pair<unsigned, MachineInstr*> &A, 498 const std::pair<unsigned, MachineInstr*> &B) { 499 return A.first < B.first; 500 } 501 }; 502} 503 504// ProcessSourceNode - Process nodes with source order numbers. These are added 505// to a vector which EmitSchedule uses to determine how to insert dbg_value 506// instructions in the right order. 507static void ProcessSourceNode(SDNode *N, SelectionDAG *DAG, 508 InstrEmitter &Emitter, 509 DenseMap<SDValue, unsigned> &VRBaseMap, 510 SmallVector<std::pair<unsigned, MachineInstr*>, 32> &Orders, 511 SmallSet<unsigned, 8> &Seen) { 512 unsigned Order = DAG->GetOrdering(N); 513 if (!Order || !Seen.insert(Order)) 514 return; 515 516 MachineBasicBlock *BB = Emitter.getBlock(); 517 if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI()) { 518 // Did not insert any instruction. 519 Orders.push_back(std::make_pair(Order, (MachineInstr*)0)); 520 return; 521 } 522 523 Orders.push_back(std::make_pair(Order, prior(Emitter.getInsertPos()))); 524 if (!N->getHasDebugValue()) 525 return; 526 // Opportunistically insert immediate dbg_value uses, i.e. those with source 527 // order number right after the N. 528 MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos(); 529 SmallVector<SDDbgValue*,2> &DVs = DAG->GetDbgValues(N); 530 for (unsigned i = 0, e = DVs.size(); i != e; ++i) { 531 if (DVs[i]->isInvalidated()) 532 continue; 533 unsigned DVOrder = DVs[i]->getOrder(); 534 if (DVOrder == ++Order) { 535 MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap); 536 if (DbgMI) { 537 Orders.push_back(std::make_pair(DVOrder, DbgMI)); 538 BB->insert(InsertPos, DbgMI); 539 } 540 DVs[i]->setIsInvalidated(); 541 } 542 } 543} 544 545 546/// EmitSchedule - Emit the machine code in scheduled order. 547MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() { 548 InstrEmitter Emitter(BB, InsertPos); 549 DenseMap<SDValue, unsigned> VRBaseMap; 550 DenseMap<SUnit*, unsigned> CopyVRBaseMap; 551 SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders; 552 SmallSet<unsigned, 8> Seen; 553 bool HasDbg = DAG->hasDebugValues(); 554 555 // If this is the first BB, emit byval parameter dbg_value's. 556 if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) { 557 SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin(); 558 SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd(); 559 for (; PDI != PDE; ++PDI) { 560 MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap); 561 if (DbgMI) 562 BB->insert(InsertPos, DbgMI); 563 } 564 } 565 566 for (unsigned i = 0, e = Sequence.size(); i != e; i++) { 567 SUnit *SU = Sequence[i]; 568 if (!SU) { 569 // Null SUnit* is a noop. 570 EmitNoop(); 571 continue; 572 } 573 574 // For pre-regalloc scheduling, create instructions corresponding to the 575 // SDNode and any glued SDNodes and append them to the block. 576 if (!SU->getNode()) { 577 // Emit a copy. 578 EmitPhysRegCopy(SU, CopyVRBaseMap); 579 continue; 580 } 581 582 SmallVector<SDNode *, 4> GluedNodes; 583 for (SDNode *N = SU->getNode()->getGluedNode(); N; 584 N = N->getGluedNode()) 585 GluedNodes.push_back(N); 586 while (!GluedNodes.empty()) { 587 SDNode *N = GluedNodes.back(); 588 Emitter.EmitNode(GluedNodes.back(), SU->OrigNode != SU, SU->isCloned, 589 VRBaseMap); 590 // Remember the source order of the inserted instruction. 591 if (HasDbg) 592 ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen); 593 GluedNodes.pop_back(); 594 } 595 Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, 596 VRBaseMap); 597 // Remember the source order of the inserted instruction. 598 if (HasDbg) 599 ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders, 600 Seen); 601 } 602 603 // Insert all the dbg_values which have not already been inserted in source 604 // order sequence. 605 if (HasDbg) { 606 MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI(); 607 608 // Sort the source order instructions and use the order to insert debug 609 // values. 610 std::sort(Orders.begin(), Orders.end(), OrderSorter()); 611 612 SDDbgInfo::DbgIterator DI = DAG->DbgBegin(); 613 SDDbgInfo::DbgIterator DE = DAG->DbgEnd(); 614 // Now emit the rest according to source order. 615 unsigned LastOrder = 0; 616 for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) { 617 unsigned Order = Orders[i].first; 618 MachineInstr *MI = Orders[i].second; 619 // Insert all SDDbgValue's whose order(s) are before "Order". 620 if (!MI) 621 continue; 622#ifndef NDEBUG 623 unsigned LastDIOrder = 0; 624#endif 625 for (; DI != DE && 626 (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) { 627#ifndef NDEBUG 628 assert((*DI)->getOrder() >= LastDIOrder && 629 "SDDbgValue nodes must be in source order!"); 630 LastDIOrder = (*DI)->getOrder(); 631#endif 632 if ((*DI)->isInvalidated()) 633 continue; 634 MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap); 635 if (DbgMI) { 636 if (!LastOrder) 637 // Insert to start of the BB (after PHIs). 638 BB->insert(BBBegin, DbgMI); 639 else { 640 // Insert at the instruction, which may be in a different 641 // block, if the block was split by a custom inserter. 642 MachineBasicBlock::iterator Pos = MI; 643 MI->getParent()->insert(llvm::next(Pos), DbgMI); 644 } 645 } 646 } 647 LastOrder = Order; 648 } 649 // Add trailing DbgValue's before the terminator. FIXME: May want to add 650 // some of them before one or more conditional branches? 651 while (DI != DE) { 652 MachineBasicBlock *InsertBB = Emitter.getBlock(); 653 MachineBasicBlock::iterator Pos= Emitter.getBlock()->getFirstTerminator(); 654 if (!(*DI)->isInvalidated()) { 655 MachineInstr *DbgMI= Emitter.EmitDbgValue(*DI, VRBaseMap); 656 if (DbgMI) 657 InsertBB->insert(Pos, DbgMI); 658 } 659 ++DI; 660 } 661 } 662 663 BB = Emitter.getBlock(); 664 InsertPos = Emitter.getInsertPos(); 665 return BB; 666} 667