LLVM API Documentation

ScheduleDAGInstrs.cpp

Go to the documentation of this file.
00001 //===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This implements the ScheduleDAGInstrs class, which implements re-scheduling
00011 // of MachineInstrs.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "sched-instrs"
00016 #include "llvm/Analysis/AliasAnalysis.h"
00017 #include "llvm/CodeGen/MachineDominators.h"
00018 #include "llvm/CodeGen/MachineFunctionPass.h"
00019 #include "llvm/CodeGen/MachineLoopInfo.h"
00020 #include "llvm/CodeGen/MachineRegisterInfo.h"
00021 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
00022 #include "llvm/CodeGen/PseudoSourceValue.h"
00023 #include "llvm/Target/TargetMachine.h"
00024 #include "llvm/Target/TargetInstrInfo.h"
00025 #include "llvm/Target/TargetRegisterInfo.h"
00026 #include "llvm/Target/TargetSubtarget.h"
00027 #include "llvm/Support/Compiler.h"
00028 #include "llvm/Support/Debug.h"
00029 #include "llvm/Support/raw_ostream.h"
00030 #include "llvm/ADT/SmallSet.h"
00031 #include <map>
00032 using namespace llvm;
00033 
00034 namespace {
00035   class VISIBILITY_HIDDEN LoopDependencies {
00036     const MachineLoopInfo &MLI;
00037     const MachineDominatorTree &MDT;
00038 
00039   public:
00040     typedef std::map<unsigned, std::pair<const MachineOperand *, unsigned> >
00041       LoopDeps;
00042     LoopDeps Deps;
00043 
00044     LoopDependencies(const MachineLoopInfo &mli,
00045                      const MachineDominatorTree &mdt) :
00046       MLI(mli), MDT(mdt) {}
00047 
00048     void VisitLoop(const MachineLoop *Loop) {
00049       Deps.clear();
00050       MachineBasicBlock *Header = Loop->getHeader();
00051       SmallSet<unsigned, 8> LoopLiveIns;
00052       for (MachineBasicBlock::livein_iterator LI = Header->livein_begin(),
00053            LE = Header->livein_end(); LI != LE; ++LI)
00054         LoopLiveIns.insert(*LI);
00055 
00056       const MachineDomTreeNode *Node = MDT.getNode(Header);
00057       const MachineBasicBlock *MBB = Node->getBlock();
00058       assert(Loop->contains(MBB) &&
00059              "Loop does not contain header!");
00060       VisitRegion(Node, MBB, Loop, LoopLiveIns);
00061     }
00062 
00063   private:
00064     void VisitRegion(const MachineDomTreeNode *Node,
00065                      const MachineBasicBlock *MBB,
00066                      const MachineLoop *Loop,
00067                      const SmallSet<unsigned, 8> &LoopLiveIns) {
00068       unsigned Count = 0;
00069       for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
00070            I != E; ++I, ++Count) {
00071         const MachineInstr *MI = I;
00072         for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00073           const MachineOperand &MO = MI->getOperand(i);
00074           if (!MO.isReg() || !MO.isUse())
00075             continue;
00076           unsigned MOReg = MO.getReg();
00077           if (LoopLiveIns.count(MOReg))
00078             Deps.insert(std::make_pair(MOReg, std::make_pair(&MO, Count)));
00079         }
00080       }
00081 
00082       const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
00083       for (std::vector<MachineDomTreeNode*>::const_iterator I =
00084            Children.begin(), E = Children.end(); I != E; ++I) {
00085         const MachineDomTreeNode *ChildNode = *I;
00086         MachineBasicBlock *ChildBlock = ChildNode->getBlock();
00087         if (Loop->contains(ChildBlock))
00088           VisitRegion(ChildNode, ChildBlock, Loop, LoopLiveIns);
00089       }
00090     }
00091   };
00092 }
00093 
00094 ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
00095                                      const MachineLoopInfo &mli,
00096                                      const MachineDominatorTree &mdt)
00097   : ScheduleDAG(mf), MLI(mli), MDT(mdt) {}
00098 
00099 /// getOpcode - If this is an Instruction or a ConstantExpr, return the
00100 /// opcode value. Otherwise return UserOp1.
00101 static unsigned getOpcode(const Value *V) {
00102   if (const Instruction *I = dyn_cast<Instruction>(V))
00103     return I->getOpcode();
00104   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00105     return CE->getOpcode();
00106   // Use UserOp1 to mean there's no opcode.
00107   return Instruction::UserOp1;
00108 }
00109 
00110 /// getUnderlyingObjectFromInt - This is the function that does the work of
00111 /// looking through basic ptrtoint+arithmetic+inttoptr sequences.
00112 static const Value *getUnderlyingObjectFromInt(const Value *V) {
00113   do {
00114     if (const User *U = dyn_cast<User>(V)) {
00115       // If we find a ptrtoint, we can transfer control back to the
00116       // regular getUnderlyingObjectFromInt.
00117       if (getOpcode(U) == Instruction::PtrToInt)
00118         return U->getOperand(0);
00119       // If we find an add of a constant or a multiplied value, it's
00120       // likely that the other operand will lead us to the base
00121       // object. We don't have to worry about the case where the
00122       // object address is somehow being computed bt the multiply,
00123       // because our callers only care when the result is an
00124       // identifibale object.
00125       if (getOpcode(U) != Instruction::Add ||
00126           (!isa<ConstantInt>(U->getOperand(1)) &&
00127            getOpcode(U->getOperand(1)) != Instruction::Mul))
00128         return V;
00129       V = U->getOperand(0);
00130     } else {
00131       return V;
00132     }
00133     assert(isa<IntegerType>(V->getType()) && "Unexpected operand type!");
00134   } while (1);
00135 }
00136 
00137 /// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject
00138 /// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
00139 static const Value *getUnderlyingObject(const Value *V) {
00140   // First just call Value::getUnderlyingObject to let it do what it does.
00141   do {
00142     V = V->getUnderlyingObject();
00143     // If it found an inttoptr, use special code to continue climing.
00144     if (getOpcode(V) != Instruction::IntToPtr)
00145       break;
00146     const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
00147     // If that succeeded in finding a pointer, continue the search.
00148     if (!isa<PointerType>(O->getType()))
00149       break;
00150     V = O;
00151   } while (1);
00152   return V;
00153 }
00154 
00155 /// getUnderlyingObjectForInstr - If this machine instr has memory reference
00156 /// information and it can be tracked to a normal reference to a known
00157 /// object, return the Value for that object. Otherwise return null.
00158 static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI) {
00159   if (!MI->hasOneMemOperand() ||
00160       !MI->memoperands_begin()->getValue() ||
00161       MI->memoperands_begin()->isVolatile())
00162     return 0;
00163 
00164   const Value *V = MI->memoperands_begin()->getValue();
00165   if (!V)
00166     return 0;
00167 
00168   V = getUnderlyingObject(V);
00169   if (!isa<PseudoSourceValue>(V) && !isIdentifiedObject(V))
00170     return 0;
00171 
00172   return V;
00173 }
00174 
00175 void ScheduleDAGInstrs::BuildSchedGraph() {
00176   SUnits.reserve(BB->size());
00177 
00178   // We build scheduling units by walking a block's instruction list from bottom
00179   // to top.
00180 
00181   // Remember where a generic side-effecting instruction is as we procede. If
00182   // ChainMMO is null, this is assumed to have arbitrary side-effects. If
00183   // ChainMMO is non-null, then Chain makes only a single memory reference.
00184   SUnit *Chain = 0;
00185   MachineMemOperand *ChainMMO = 0;
00186 
00187   // Memory references to specific known memory locations are tracked so that
00188   // they can be given more precise dependencies.
00189   std::map<const Value *, SUnit *> MemDefs;
00190   std::map<const Value *, std::vector<SUnit *> > MemUses;
00191 
00192   // If we have an SUnit which is representing a terminator instruction, we
00193   // can use it as a place-holder successor for inter-block dependencies.
00194   SUnit *Terminator = 0;
00195 
00196   // Terminators can perform control transfers, we we need to make sure that
00197   // all the work of the block is done before the terminator. Labels can
00198   // mark points of interest for various types of meta-data (eg. EH data),
00199   // and we need to make sure nothing is scheduled around them.
00200   SUnit *SchedulingBarrier = 0;
00201 
00202   LoopDependencies LoopRegs(MLI, MDT);
00203 
00204   // Track which regs are live into a loop, to help guide back-edge-aware
00205   // scheduling.
00206   SmallSet<unsigned, 8> LoopLiveInRegs;
00207   if (MachineLoop *ML = MLI.getLoopFor(BB))
00208     if (BB == ML->getLoopLatch()) {
00209       MachineBasicBlock *Header = ML->getHeader();
00210       for (MachineBasicBlock::livein_iterator I = Header->livein_begin(),
00211            E = Header->livein_end(); I != E; ++I)
00212         LoopLiveInRegs.insert(*I);
00213       LoopRegs.VisitLoop(ML);
00214     }
00215 
00216   // Check to see if the scheduler cares about latencies.
00217   bool UnitLatencies = ForceUnitLatencies();
00218 
00219   // Ask the target if address-backscheduling is desirable, and if so how much.
00220   unsigned SpecialAddressLatency =
00221     TM.getSubtarget<TargetSubtarget>().getSpecialAddressLatency();
00222 
00223   for (MachineBasicBlock::iterator MII = End, MIE = Begin;
00224        MII != MIE; --MII) {
00225     MachineInstr *MI = prior(MII);
00226     const TargetInstrDesc &TID = MI->getDesc();
00227     SUnit *SU = NewSUnit(MI);
00228 
00229     // Assign the Latency field of SU using target-provided information.
00230     if (UnitLatencies)
00231       SU->Latency = 1;
00232     else
00233       ComputeLatency(SU);
00234 
00235     // Add register-based dependencies (data, anti, and output).
00236     for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
00237       const MachineOperand &MO = MI->getOperand(j);
00238       if (!MO.isReg()) continue;
00239       unsigned Reg = MO.getReg();
00240       if (Reg == 0) continue;
00241 
00242       assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
00243       std::vector<SUnit *> &UseList = Uses[Reg];
00244       std::vector<SUnit *> &DefList = Defs[Reg];
00245       // Optionally add output and anti dependencies.
00246       // TODO: Using a latency of 1 here assumes there's no cost for
00247       //       reusing registers.
00248       SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
00249       for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
00250         SUnit *DefSU = DefList[i];
00251         if (DefSU != SU &&
00252             (Kind != SDep::Output || !MO.isDead() ||
00253              !DefSU->getInstr()->registerDefIsDead(Reg)))
00254           DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/Reg));
00255       }
00256       for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
00257         std::vector<SUnit *> &DefList = Defs[*Alias];
00258         for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
00259           SUnit *DefSU = DefList[i];
00260           if (DefSU != SU &&
00261               (Kind != SDep::Output || !MO.isDead() ||
00262                !DefSU->getInstr()->registerDefIsDead(Reg)))
00263             DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/ *Alias));
00264         }
00265       }
00266 
00267       if (MO.isDef()) {
00268         // Add any data dependencies.
00269         unsigned DataLatency = SU->Latency;
00270         for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
00271           SUnit *UseSU = UseList[i];
00272           if (UseSU != SU) {
00273             unsigned LDataLatency = DataLatency;
00274             // Optionally add in a special extra latency for nodes that
00275             // feed addresses.
00276             // TODO: Do this for register aliases too.
00277             if (SpecialAddressLatency != 0 && !UnitLatencies) {
00278               MachineInstr *UseMI = UseSU->getInstr();
00279               const TargetInstrDesc &UseTID = UseMI->getDesc();
00280               int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
00281               assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
00282               if ((UseTID.mayLoad() || UseTID.mayStore()) &&
00283                   (unsigned)RegUseIndex < UseTID.getNumOperands() &&
00284                   UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass())
00285                 LDataLatency += SpecialAddressLatency;
00286             }
00287             UseSU->addPred(SDep(SU, SDep::Data, LDataLatency, Reg));
00288           }
00289         }
00290         for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
00291           std::vector<SUnit *> &UseList = Uses[*Alias];
00292           for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
00293             SUnit *UseSU = UseList[i];
00294             if (UseSU != SU)
00295               UseSU->addPred(SDep(SU, SDep::Data, DataLatency, *Alias));
00296           }
00297         }
00298 
00299         // If a def is going to wrap back around to the top of the loop,
00300         // backschedule it.
00301         // TODO: Blocks in loops without terminators can benefit too.
00302         if (!UnitLatencies && Terminator && DefList.empty()) {
00303           LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
00304           if (I != LoopRegs.Deps.end()) {
00305             const MachineOperand *UseMO = I->second.first;
00306             unsigned Count = I->second.second;
00307             const MachineInstr *UseMI = UseMO->getParent();
00308             unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
00309             const TargetInstrDesc &UseTID = UseMI->getDesc();
00310             // TODO: If we knew the total depth of the region here, we could
00311             // handle the case where the whole loop is inside the region but
00312             // is large enough that the isScheduleHigh trick isn't needed.
00313             if (UseMOIdx < UseTID.getNumOperands()) {
00314               // Currently, we only support scheduling regions consisting of
00315               // single basic blocks. Check to see if the instruction is in
00316               // the same region by checking to see if it has the same parent.
00317               if (UseMI->getParent() != MI->getParent()) {
00318                 unsigned Latency = SU->Latency;
00319                 if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass())
00320                   Latency += SpecialAddressLatency;
00321                 // This is a wild guess as to the portion of the latency which
00322                 // will be overlapped by work done outside the current
00323                 // scheduling region.
00324                 Latency -= std::min(Latency, Count);
00325                 // Add the artifical edge.
00326                 Terminator->addPred(SDep(SU, SDep::Order, Latency,
00327                                          /*Reg=*/0, /*isNormalMemory=*/false,
00328                                          /*isMustAlias=*/false,
00329                                          /*isArtificial=*/true));
00330               } else if (SpecialAddressLatency > 0 &&
00331                          UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
00332                 // The entire loop body is within the current scheduling region
00333                 // and the latency of this operation is assumed to be greater
00334                 // than the latency of the loop.
00335                 // TODO: Recursively mark data-edge predecessors as
00336                 //       isScheduleHigh too.
00337                 SU->isScheduleHigh = true;
00338               }
00339             }
00340             LoopRegs.Deps.erase(I);
00341           }
00342         }
00343 
00344         UseList.clear();
00345         if (!MO.isDead())
00346           DefList.clear();
00347         DefList.push_back(SU);
00348       } else {
00349         UseList.push_back(SU);
00350       }
00351     }
00352 
00353     // Add chain dependencies.
00354     // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
00355     // after stack slots are lowered to actual addresses.
00356     // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
00357     // produce more precise dependence information.
00358     if (TID.isCall() || TID.isTerminator() || TID.hasUnmodeledSideEffects()) {
00359     new_chain:
00360       // This is the conservative case. Add dependencies on all memory
00361       // references.
00362       if (Chain)
00363         Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
00364       Chain = SU;
00365       for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
00366         PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
00367       PendingLoads.clear();
00368       for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
00369            E = MemDefs.end(); I != E; ++I) {
00370         I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
00371         I->second = SU;
00372       }
00373       for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
00374            MemUses.begin(), E = MemUses.end(); I != E; ++I) {
00375         for (unsigned i = 0, e = I->second.size(); i != e; ++i)
00376           I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency));
00377         I->second.clear();
00378       }
00379       // See if it is known to just have a single memory reference.
00380       MachineInstr *ChainMI = Chain->getInstr();
00381       const TargetInstrDesc &ChainTID = ChainMI->getDesc();
00382       if (!ChainTID.isCall() && !ChainTID.isTerminator() &&
00383           !ChainTID.hasUnmodeledSideEffects() &&
00384           ChainMI->hasOneMemOperand() &&
00385           !ChainMI->memoperands_begin()->isVolatile() &&
00386           ChainMI->memoperands_begin()->getValue())
00387         // We know that the Chain accesses one specific memory location.
00388         ChainMMO = &*ChainMI->memoperands_begin();
00389       else
00390         // Unknown memory accesses. Assume the worst.
00391         ChainMMO = 0;
00392     } else if (TID.mayStore()) {
00393       if (const Value *V = getUnderlyingObjectForInstr(MI)) {
00394         // A store to a specific PseudoSourceValue. Add precise dependencies.
00395         // Handle the def in MemDefs, if there is one.
00396         std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
00397         if (I != MemDefs.end()) {
00398           I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
00399                                   /*isNormalMemory=*/true));
00400           I->second = SU;
00401         } else {
00402           MemDefs[V] = SU;
00403         }
00404         // Handle the uses in MemUses, if there are any.
00405         std::map<const Value *, std::vector<SUnit *> >::iterator J =
00406           MemUses.find(V);
00407         if (J != MemUses.end()) {
00408           for (unsigned i = 0, e = J->second.size(); i != e; ++i)
00409             J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
00410                                        /*isNormalMemory=*/true));
00411           J->second.clear();
00412         }
00413         // Add dependencies from all the PendingLoads, since without
00414         // memoperands we must assume they alias anything.
00415         for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
00416           PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
00417         // Add a general dependence too, if needed.
00418         if (Chain)
00419           Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
00420       } else
00421         // Treat all other stores conservatively.
00422         goto new_chain;
00423     } else if (TID.mayLoad()) {
00424       if (TII->isInvariantLoad(MI)) {
00425         // Invariant load, no chain dependencies needed!
00426       } else if (const Value *V = getUnderlyingObjectForInstr(MI)) {
00427         // A load from a specific PseudoSourceValue. Add precise dependencies.
00428         std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
00429         if (I != MemDefs.end())
00430           I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
00431                                   /*isNormalMemory=*/true));
00432         MemUses[V].push_back(SU);
00433 
00434         // Add a general dependence too, if needed.
00435         if (Chain && (!ChainMMO ||
00436                       (ChainMMO->isStore() || ChainMMO->isVolatile())))
00437           Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
00438       } else if (MI->hasVolatileMemoryRef()) {
00439         // Treat volatile loads conservatively. Note that this includes
00440         // cases where memoperand information is unavailable.
00441         goto new_chain;
00442       } else {
00443         // A normal load. Depend on the general chain, as well as on
00444         // all stores. In the absense of MachineMemOperand information,
00445         // we can't even assume that the load doesn't alias well-behaved
00446         // memory locations.
00447         if (Chain)
00448           Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
00449         for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
00450              E = MemDefs.end(); I != E; ++I)
00451           I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
00452         PendingLoads.push_back(SU);
00453       }
00454     }
00455 
00456     // Add chain edges from terminators and labels to ensure that no
00457     // instructions are scheduled past them.
00458     if (SchedulingBarrier && SU->Succs.empty())
00459       SchedulingBarrier->addPred(SDep(SU, SDep::Order, SU->Latency));
00460     // If we encounter a mid-block label, we need to go back and add
00461     // dependencies on SUnits we've already processed to prevent the
00462     // label from moving downward.
00463     if (MI->isLabel())
00464       for (SUnit *I = SU; I != &SUnits[0]; --I) {
00465         SUnit *SuccSU = SU-1;
00466         SuccSU->addPred(SDep(SU, SDep::Order, SU->Latency));
00467         MachineInstr *SuccMI = SuccSU->getInstr();
00468         if (SuccMI->getDesc().isTerminator() || SuccMI->isLabel())
00469           break;
00470       }
00471     // If this instruction obstructs all scheduling, remember it.
00472     if (TID.isTerminator() || MI->isLabel())
00473       SchedulingBarrier = SU;
00474     // If this instruction is a terminator, remember it.
00475     if (TID.isTerminator())
00476       Terminator = SU;
00477   }
00478 
00479   for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
00480     Defs[i].clear();
00481     Uses[i].clear();
00482   }
00483   PendingLoads.clear();
00484 }
00485 
00486 void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
00487   const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
00488 
00489   // Compute the latency for the node.  We use the sum of the latencies for
00490   // all nodes flagged together into this SUnit.
00491   SU->Latency =
00492     InstrItins.getLatency(SU->getInstr()->getDesc().getSchedClass());
00493 
00494   // Simplistic target-independent heuristic: assume that loads take
00495   // extra time.
00496   if (InstrItins.isEmpty())
00497     if (SU->getInstr()->getDesc().mayLoad())
00498       SU->Latency += 2;
00499 }
00500 
00501 void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
00502   SU->getInstr()->dump();
00503 }
00504 
00505 std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
00506   std::string s;
00507   raw_string_ostream oss(s);
00508   SU->getInstr()->print(oss);
00509   return oss.str();
00510 }
00511 
00512 // EmitSchedule - Emit the machine code in scheduled order.
00513 MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
00514   // For MachineInstr-based scheduling, we're rescheduling the instructions in
00515   // the block, so start by removing them from the block.
00516   while (Begin != End) {
00517     MachineBasicBlock::iterator I = Begin;
00518     ++Begin;
00519     BB->remove(I);
00520   }
00521 
00522   // Then re-insert them according to the given schedule.
00523   for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
00524     SUnit *SU = Sequence[i];
00525     if (!SU) {
00526       // Null SUnit* is a noop.
00527       EmitNoop();
00528       continue;
00529     }
00530 
00531     BB->insert(End, SU->getInstr());
00532   }
00533 
00534   return BB;
00535 }