LLVM API Documentation

PostRASchedulerList.cpp

Go to the documentation of this file.
00001 //===----- SchedulePostRAList.cpp - list scheduler ------------------------===//
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 a top-down list scheduler, using standard algorithms.
00011 // The basic approach uses a priority queue of available nodes to schedule.
00012 // One at a time, nodes are taken from the priority queue (thus in priority
00013 // order), checked for legality to schedule, and emitted if legal.
00014 //
00015 // Nodes may not be legal to schedule either due to structural hazards (e.g.
00016 // pipeline or resource constraints) or because an input to the instruction has
00017 // not completed execution.
00018 //
00019 //===----------------------------------------------------------------------===//
00020 
00021 #define DEBUG_TYPE "post-RA-sched"
00022 #include "llvm/CodeGen/Passes.h"
00023 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
00024 #include "llvm/CodeGen/LatencyPriorityQueue.h"
00025 #include "llvm/CodeGen/SchedulerRegistry.h"
00026 #include "llvm/CodeGen/MachineDominators.h"
00027 #include "llvm/CodeGen/MachineFunctionPass.h"
00028 #include "llvm/CodeGen/MachineLoopInfo.h"
00029 #include "llvm/CodeGen/MachineRegisterInfo.h"
00030 #include "llvm/Target/TargetInstrInfo.h"
00031 #include "llvm/Target/TargetRegisterInfo.h"
00032 #include "llvm/Support/Compiler.h"
00033 #include "llvm/Support/Debug.h"
00034 #include "llvm/ADT/Statistic.h"
00035 #include <map>
00036 using namespace llvm;
00037 
00038 STATISTIC(NumStalls, "Number of pipeline stalls");
00039 
00040 static cl::opt<bool>
00041 EnableAntiDepBreaking("break-anti-dependencies",
00042                       cl::desc("Break post-RA scheduling anti-dependencies"),
00043                       cl::init(true), cl::Hidden);
00044 
00045 namespace {
00046   class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass {
00047   public:
00048     static char ID;
00049     PostRAScheduler() : MachineFunctionPass(&ID) {}
00050 
00051     void getAnalysisUsage(AnalysisUsage &AU) const {
00052       AU.addRequired<MachineDominatorTree>();
00053       AU.addPreserved<MachineDominatorTree>();
00054       AU.addRequired<MachineLoopInfo>();
00055       AU.addPreserved<MachineLoopInfo>();
00056       MachineFunctionPass::getAnalysisUsage(AU);
00057     }
00058 
00059     const char *getPassName() const {
00060       return "Post RA top-down list latency scheduler";
00061     }
00062 
00063     bool runOnMachineFunction(MachineFunction &Fn);
00064   };
00065   char PostRAScheduler::ID = 0;
00066 
00067   class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs {
00068     /// AvailableQueue - The priority queue to use for the available SUnits.
00069     ///
00070     LatencyPriorityQueue AvailableQueue;
00071   
00072     /// PendingQueue - This contains all of the instructions whose operands have
00073     /// been issued, but their results are not ready yet (due to the latency of
00074     /// the operation).  Once the operands becomes available, the instruction is
00075     /// added to the AvailableQueue.
00076     std::vector<SUnit*> PendingQueue;
00077 
00078     /// Topo - A topological ordering for SUnits.
00079     ScheduleDAGTopologicalSort Topo;
00080 
00081   public:
00082     SchedulePostRATDList(MachineBasicBlock *mbb, const TargetMachine &tm,
00083                          const MachineLoopInfo &MLI,
00084                          const MachineDominatorTree &MDT)
00085       : ScheduleDAGInstrs(mbb, tm, MLI, MDT), Topo(SUnits) {}
00086 
00087     void Schedule();
00088 
00089   private:
00090     void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
00091     void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
00092     void ListScheduleTopDown();
00093     bool BreakAntiDependencies();
00094   };
00095 }
00096 
00097 bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
00098   DOUT << "PostRAScheduler\n";
00099 
00100   const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
00101   const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
00102 
00103   // Loop over all of the basic blocks
00104   for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
00105        MBB != MBBe; ++MBB) {
00106 
00107     SchedulePostRATDList Scheduler(MBB, Fn.getTarget(), MLI, MDT);
00108 
00109     Scheduler.Run();
00110 
00111     Scheduler.EmitSchedule();
00112   }
00113 
00114   return true;
00115 }
00116   
00117 /// Schedule - Schedule the DAG using list scheduling.
00118 void SchedulePostRATDList::Schedule() {
00119   DOUT << "********** List Scheduling **********\n";
00120   
00121   // Build the scheduling graph.
00122   BuildSchedGraph();
00123 
00124   if (EnableAntiDepBreaking) {
00125     if (BreakAntiDependencies()) {
00126       // We made changes. Update the dependency graph.
00127       // Theoretically we could update the graph in place:
00128       // When a live range is changed to use a different register, remove
00129       // the def's anti-dependence *and* output-dependence edges due to
00130       // that register, and add new anti-dependence and output-dependence
00131       // edges based on the next live range of the register.
00132       SUnits.clear();
00133       BuildSchedGraph();
00134     }
00135   }
00136 
00137   AvailableQueue.initNodes(SUnits);
00138 
00139   ListScheduleTopDown();
00140   
00141   AvailableQueue.releaseState();
00142 }
00143 
00144 /// getInstrOperandRegClass - Return register class of the operand of an
00145 /// instruction of the specified TargetInstrDesc.
00146 static const TargetRegisterClass*
00147 getInstrOperandRegClass(const TargetRegisterInfo *TRI,
00148                         const TargetInstrInfo *TII, const TargetInstrDesc &II,
00149                         unsigned Op) {
00150   if (Op >= II.getNumOperands())
00151     return NULL;
00152   if (II.OpInfo[Op].isLookupPtrRegClass())
00153     return TII->getPointerRegClass();
00154   return TRI->getRegClass(II.OpInfo[Op].RegClass);
00155 }
00156 
00157 /// CriticalPathStep - Return the next SUnit after SU on the bottom-up
00158 /// critical path.
00159 static SDep *CriticalPathStep(SUnit *SU) {
00160   SDep *Next = 0;
00161   unsigned NextDepth = 0;
00162   // Find the predecessor edge with the greatest depth.
00163   for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
00164        P != PE; ++P) {
00165     SUnit *PredSU = P->getSUnit();
00166     unsigned PredLatency = P->getLatency();
00167     unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
00168     // In the case of a latency tie, prefer an anti-dependency edge over
00169     // other types of edges.
00170     if (NextDepth < PredTotalLatency ||
00171         (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
00172       NextDepth = PredTotalLatency;
00173       Next = &*P;
00174     }
00175   }
00176   return Next;
00177 }
00178 
00179 /// BreakAntiDependencies - Identifiy anti-dependencies along the critical path
00180 /// of the ScheduleDAG and break them by renaming registers.
00181 ///
00182 bool SchedulePostRATDList::BreakAntiDependencies() {
00183   // The code below assumes that there is at least one instruction,
00184   // so just duck out immediately if the block is empty.
00185   if (BB->empty()) return false;
00186 
00187   // Find the node at the bottom of the critical path.
00188   SUnit *Max = 0;
00189   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
00190     SUnit *SU = &SUnits[i];
00191     if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
00192       Max = SU;
00193   }
00194 
00195   DOUT << "Critical path has total latency "
00196        << (Max ? Max->getDepth() + Max->Latency : 0) << "\n";
00197 
00198   // We'll be ignoring anti-dependencies on non-allocatable registers, because
00199   // they may not be safe to break.
00200   const BitVector AllocatableSet = TRI->getAllocatableSet(*MF);
00201 
00202   // Track progress along the critical path through the SUnit graph as we walk
00203   // the instructions.
00204   SUnit *CriticalPathSU = Max;
00205   MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
00206 
00207   // For live regs that are only used in one register class in a live range,
00208   // the register class. If the register is not live, the corresponding value
00209   // is null. If the register is live but used in multiple register classes,
00210   // the corresponding value is -1 casted to a pointer.
00211   const TargetRegisterClass *
00212     Classes[TargetRegisterInfo::FirstVirtualRegister] = {};
00213 
00214   // Map registers to all their references within a live range.
00215   std::multimap<unsigned, MachineOperand *> RegRefs;
00216 
00217   // The index of the most recent kill (proceding bottom-up), or ~0u if
00218   // the register is not live.
00219   unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister];
00220   std::fill(KillIndices, array_endof(KillIndices), ~0u);
00221   // The index of the most recent complete def (proceding bottom up), or ~0u if
00222   // the register is live.
00223   unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister];
00224   std::fill(DefIndices, array_endof(DefIndices), BB->size());
00225 
00226   // Determine the live-out physregs for this block.
00227   if (BB->back().getDesc().isReturn())
00228     // In a return block, examine the function live-out regs.
00229     for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
00230          E = MRI.liveout_end(); I != E; ++I) {
00231       unsigned Reg = *I;
00232       Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
00233       KillIndices[Reg] = BB->size();
00234       DefIndices[Reg] = ~0u;
00235       // Repeat, for all aliases.
00236       for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
00237         unsigned AliasReg = *Alias;
00238         Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
00239         KillIndices[AliasReg] = BB->size();
00240         DefIndices[AliasReg] = ~0u;
00241       }
00242     }
00243   else
00244     // In a non-return block, examine the live-in regs of all successors.
00245     for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
00246          SE = BB->succ_end(); SI != SE; ++SI) 
00247       for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
00248            E = (*SI)->livein_end(); I != E; ++I) {
00249         unsigned Reg = *I;
00250         Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
00251         KillIndices[Reg] = BB->size();
00252         DefIndices[Reg] = ~0u;
00253         // Repeat, for all aliases.
00254         for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
00255           unsigned AliasReg = *Alias;
00256           Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
00257           KillIndices[AliasReg] = BB->size();
00258           DefIndices[AliasReg] = ~0u;
00259         }
00260       }
00261 
00262   // Consider callee-saved registers as live-out, since we're running after
00263   // prologue/epilogue insertion so there's no way to add additional
00264   // saved registers.
00265   //
00266   // TODO: If the callee saves and restores these, then we can potentially
00267   // use them between the save and the restore. To do that, we could scan
00268   // the exit blocks to see which of these registers are defined.
00269   // Alternatively, callee-saved registers that aren't saved and restored
00270   // could be marked live-in in every block.
00271   for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
00272     unsigned Reg = *I;
00273     Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
00274     KillIndices[Reg] = BB->size();
00275     DefIndices[Reg] = ~0u;
00276     // Repeat, for all aliases.
00277     for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
00278       unsigned AliasReg = *Alias;
00279       Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
00280       KillIndices[AliasReg] = BB->size();
00281       DefIndices[AliasReg] = ~0u;
00282     }
00283   }
00284 
00285   // Consider this pattern:
00286   //   A = ...
00287   //   ... = A
00288   //   A = ...
00289   //   ... = A
00290   //   A = ...
00291   //   ... = A
00292   //   A = ...
00293   //   ... = A
00294   // There are three anti-dependencies here, and without special care,
00295   // we'd break all of them using the same register:
00296   //   A = ...
00297   //   ... = A
00298   //   B = ...
00299   //   ... = B
00300   //   B = ...
00301   //   ... = B
00302   //   B = ...
00303   //   ... = B
00304   // because at each anti-dependence, B is the first register that
00305   // isn't A which is free.  This re-introduces anti-dependencies
00306   // at all but one of the original anti-dependencies that we were
00307   // trying to break.  To avoid this, keep track of the most recent
00308   // register that each register was replaced with, avoid avoid
00309   // using it to repair an anti-dependence on the same register.
00310   // This lets us produce this:
00311   //   A = ...
00312   //   ... = A
00313   //   B = ...
00314   //   ... = B
00315   //   C = ...
00316   //   ... = C
00317   //   B = ...
00318   //   ... = B
00319   // This still has an anti-dependence on B, but at least it isn't on the
00320   // original critical path.
00321   //
00322   // TODO: If we tracked more than one register here, we could potentially
00323   // fix that remaining critical edge too. This is a little more involved,
00324   // because unlike the most recent register, less recent registers should
00325   // still be considered, though only if no other registers are available.
00326   unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {};
00327 
00328   // Attempt to break anti-dependence edges on the critical path. Walk the
00329   // instructions from the bottom up, tracking information about liveness
00330   // as we go to help determine which registers are available.
00331   bool Changed = false;
00332   unsigned Count = BB->size() - 1;
00333   for (MachineBasicBlock::reverse_iterator I = BB->rbegin(), E = BB->rend();
00334        I != E; ++I, --Count) {
00335     MachineInstr *MI = &*I;
00336 
00337     // After regalloc, IMPLICIT_DEF instructions aren't safe to treat as
00338     // dependence-breaking. In the case of an INSERT_SUBREG, the IMPLICIT_DEF
00339     // is left behind appearing to clobber the super-register, while the
00340     // subregister needs to remain live. So we just ignore them.
00341     if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
00342       continue;
00343 
00344     // Check if this instruction has a dependence on the critical path that
00345     // is an anti-dependence that we may be able to break. If it is, set
00346     // AntiDepReg to the non-zero register associated with the anti-dependence.
00347     //
00348     // We limit our attention to the critical path as a heuristic to avoid
00349     // breaking anti-dependence edges that aren't going to significantly
00350     // impact the overall schedule. There are a limited number of registers
00351     // and we want to save them for the important edges.
00352     // 
00353     // TODO: Instructions with multiple defs could have multiple
00354     // anti-dependencies. The current code here only knows how to break one
00355     // edge per instruction. Note that we'd have to be able to break all of
00356     // the anti-dependencies in an instruction in order to be effective.
00357     unsigned AntiDepReg = 0;
00358     if (MI == CriticalPathMI) {
00359       if (SDep *Edge = CriticalPathStep(CriticalPathSU)) {
00360         SUnit *NextSU = Edge->getSUnit();
00361 
00362         // Only consider anti-dependence edges.
00363         if (Edge->getKind() == SDep::Anti) {
00364           AntiDepReg = Edge->getReg();
00365           assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
00366           // Don't break anti-dependencies on non-allocatable registers.
00367           if (AllocatableSet.test(AntiDepReg)) {
00368             // If the SUnit has other dependencies on the SUnit that it
00369             // anti-depends on, don't bother breaking the anti-dependency
00370             // since those edges would prevent such units from being
00371             // scheduled past each other regardless.
00372             //
00373             // Also, if there are dependencies on other SUnits with the
00374             // same register as the anti-dependency, don't attempt to
00375             // break it.
00376             for (SUnit::pred_iterator P = CriticalPathSU->Preds.begin(),
00377                  PE = CriticalPathSU->Preds.end(); P != PE; ++P)
00378               if (P->getSUnit() == NextSU ?
00379                     (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
00380                     (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
00381                 AntiDepReg = 0;
00382                 break;
00383               }
00384           }
00385         }
00386         CriticalPathSU = NextSU;
00387         CriticalPathMI = CriticalPathSU->getInstr();
00388       } else {
00389         // We've reached the end of the critical path.
00390         CriticalPathSU = 0;
00391         CriticalPathMI = 0;
00392       }
00393     }
00394 
00395     // Scan the register operands for this instruction and update
00396     // Classes and RegRefs.
00397     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00398       MachineOperand &MO = MI->getOperand(i);
00399       if (!MO.isReg()) continue;
00400       unsigned Reg = MO.getReg();
00401       if (Reg == 0) continue;
00402       const TargetRegisterClass *NewRC =
00403         getInstrOperandRegClass(TRI, TII, MI->getDesc(), i);
00404 
00405       // If this instruction has a use of AntiDepReg, breaking it
00406       // is invalid.
00407       if (MO.isUse() && AntiDepReg == Reg)
00408         AntiDepReg = 0;
00409 
00410       // For now, only allow the register to be changed if its register
00411       // class is consistent across all uses.
00412       if (!Classes[Reg] && NewRC)
00413         Classes[Reg] = NewRC;
00414       else if (!NewRC || Classes[Reg] != NewRC)
00415         Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
00416 
00417       // Now check for aliases.
00418       for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
00419         // If an alias of the reg is used during the live range, give up.
00420         // Note that this allows us to skip checking if AntiDepReg
00421         // overlaps with any of the aliases, among other things.
00422         unsigned AliasReg = *Alias;
00423         if (Classes[AliasReg]) {
00424           Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
00425           Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
00426         }
00427       }
00428 
00429       // If we're still willing to consider this register, note the reference.
00430       if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
00431         RegRefs.insert(std::make_pair(Reg, &MO));
00432     }
00433 
00434     // Determine AntiDepReg's register class, if it is live and is
00435     // consistently used within a single class.
00436     const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
00437     assert((AntiDepReg == 0 || RC != NULL) &&
00438            "Register should be live if it's causing an anti-dependence!");
00439     if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
00440       AntiDepReg = 0;
00441 
00442     // Look for a suitable register to use to break the anti-depenence.
00443     //
00444     // TODO: Instead of picking the first free register, consider which might
00445     // be the best.
00446     if (AntiDepReg != 0) {
00447       for (TargetRegisterClass::iterator R = RC->allocation_order_begin(*MF),
00448            RE = RC->allocation_order_end(*MF); R != RE; ++R) {
00449         unsigned NewReg = *R;
00450         // Don't replace a register with itself.
00451         if (NewReg == AntiDepReg) continue;
00452         // Don't replace a register with one that was recently used to repair
00453         // an anti-dependence with this AntiDepReg, because that would
00454         // re-introduce that anti-dependence.
00455         if (NewReg == LastNewReg[AntiDepReg]) continue;
00456         // If NewReg is dead and NewReg's most recent def is not before
00457         // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
00458         assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u)) &&
00459                "Kill and Def maps aren't consistent for AntiDepReg!");
00460         assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u)) &&
00461                "Kill and Def maps aren't consistent for NewReg!");
00462         if (KillIndices[NewReg] == ~0u &&
00463             Classes[NewReg] != reinterpret_cast<TargetRegisterClass *>(-1) &&
00464             KillIndices[AntiDepReg] <= DefIndices[NewReg]) {
00465           DOUT << "Breaking anti-dependence edge on "
00466                << TRI->getName(AntiDepReg)
00467                << " with " << RegRefs.count(AntiDepReg) << " references"
00468                << " using " << TRI->getName(NewReg) << "!\n";
00469 
00470           // Update the references to the old register to refer to the new
00471           // register.
00472           std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
00473                     std::multimap<unsigned, MachineOperand *>::iterator>
00474              Range = RegRefs.equal_range(AntiDepReg);
00475           for (std::multimap<unsigned, MachineOperand *>::iterator
00476                Q = Range.first, QE = Range.second; Q != QE; ++Q)
00477             Q->second->setReg(NewReg);
00478 
00479           // We just went back in time and modified history; the
00480           // liveness information for the anti-depenence reg is now
00481           // inconsistent. Set the state as if it were dead.
00482           Classes[NewReg] = Classes[AntiDepReg];
00483           DefIndices[NewReg] = DefIndices[AntiDepReg];
00484           KillIndices[NewReg] = KillIndices[AntiDepReg];
00485 
00486           Classes[AntiDepReg] = 0;
00487           DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
00488           KillIndices[AntiDepReg] = ~0u;
00489 
00490           RegRefs.erase(AntiDepReg);
00491           Changed = true;
00492           LastNewReg[AntiDepReg] = NewReg;
00493           break;
00494         }
00495       }
00496     }
00497 
00498     // Update liveness.
00499     // Proceding upwards, registers that are defed but not used in this
00500     // instruction are now dead.
00501     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00502       MachineOperand &MO = MI->getOperand(i);
00503       if (!MO.isReg()) continue;
00504       unsigned Reg = MO.getReg();
00505       if (Reg == 0) continue;
00506       if (!MO.isDef()) continue;
00507       // Ignore two-addr defs.
00508       if (MI->isRegReDefinedByTwoAddr(i)) continue;
00509 
00510       DefIndices[Reg] = Count;
00511       KillIndices[Reg] = ~0u;
00512       Classes[Reg] = 0;
00513       RegRefs.erase(Reg);
00514       // Repeat, for all subregs.
00515       for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
00516            *Subreg; ++Subreg) {
00517         unsigned SubregReg = *Subreg;
00518         DefIndices[SubregReg] = Count;
00519         KillIndices[SubregReg] = ~0u;
00520         Classes[SubregReg] = 0;
00521         RegRefs.erase(SubregReg);
00522       }
00523       // Conservatively mark super-registers as unusable.
00524       for (const unsigned *Super = TRI->getSuperRegisters(Reg);
00525            *Super; ++Super) {
00526         unsigned SuperReg = *Super;
00527         Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
00528       }
00529     }
00530     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00531       MachineOperand &MO = MI->getOperand(i);
00532       if (!MO.isReg()) continue;
00533       unsigned Reg = MO.getReg();
00534       if (Reg == 0) continue;
00535       if (!MO.isUse()) continue;
00536 
00537       const TargetRegisterClass *NewRC =
00538         getInstrOperandRegClass(TRI, TII, MI->getDesc(), i);
00539 
00540       // For now, only allow the register to be changed if its register
00541       // class is consistent across all uses.
00542       if (!Classes[Reg] && NewRC)
00543         Classes[Reg] = NewRC;
00544       else if (!NewRC || Classes[Reg] != NewRC)
00545         Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
00546 
00547       RegRefs.insert(std::make_pair(Reg, &MO));
00548 
00549       // It wasn't previously live but now it is, this is a kill.
00550       if (KillIndices[Reg] == ~0u) {
00551         KillIndices[Reg] = Count;
00552         DefIndices[Reg] = ~0u;
00553       }
00554       // Repeat, for all aliases.
00555       for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
00556         unsigned AliasReg = *Alias;
00557         if (KillIndices[AliasReg] == ~0u) {
00558           KillIndices[AliasReg] = Count;
00559           DefIndices[AliasReg] = ~0u;
00560         }
00561       }
00562     }
00563   }
00564   assert(Count == ~0u && "Count mismatch!");
00565 
00566   return Changed;
00567 }
00568 
00569 //===----------------------------------------------------------------------===//
00570 //  Top-Down Scheduling
00571 //===----------------------------------------------------------------------===//
00572 
00573 /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
00574 /// the PendingQueue if the count reaches zero. Also update its cycle bound.
00575 void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
00576   SUnit *SuccSU = SuccEdge->getSUnit();
00577   --SuccSU->NumPredsLeft;
00578   
00579 #ifndef NDEBUG
00580   if (SuccSU->NumPredsLeft < 0) {
00581     cerr << "*** Scheduling failed! ***\n";
00582     SuccSU->dump(this);
00583     cerr << " has been released too many times!\n";
00584     assert(0);
00585   }
00586 #endif
00587   
00588   // Compute how many cycles it will be before this actually becomes
00589   // available.  This is the max of the start time of all predecessors plus
00590   // their latencies.
00591   SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
00592   
00593   if (SuccSU->NumPredsLeft == 0) {
00594     PendingQueue.push_back(SuccSU);
00595   }
00596 }
00597 
00598 /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
00599 /// count of its successors. If a successor pending count is zero, add it to
00600 /// the Available queue.
00601 void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
00602   DOUT << "*** Scheduling [" << CurCycle << "]: ";
00603   DEBUG(SU->dump(this));
00604   
00605   Sequence.push_back(SU);
00606   assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!");
00607   SU->setDepthToAtLeast(CurCycle);
00608 
00609   // Top down: release successors.
00610   for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
00611        I != E; ++I)
00612     ReleaseSucc(SU, &*I);
00613 
00614   SU->isScheduled = true;
00615   AvailableQueue.ScheduledNode(SU);
00616 }
00617 
00618 /// ListScheduleTopDown - The main loop of list scheduling for top-down
00619 /// schedulers.
00620 void SchedulePostRATDList::ListScheduleTopDown() {
00621   unsigned CurCycle = 0;
00622 
00623   // All leaves to Available queue.
00624   for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
00625     // It is available if it has no predecessors.
00626     if (SUnits[i].Preds.empty()) {
00627       AvailableQueue.push(&SUnits[i]);
00628       SUnits[i].isAvailable = true;
00629     }
00630   }
00631   
00632   // While Available queue is not empty, grab the node with the highest
00633   // priority. If it is not ready put it back.  Schedule the node.
00634   Sequence.reserve(SUnits.size());
00635   while (!AvailableQueue.empty() || !PendingQueue.empty()) {
00636     // Check to see if any of the pending instructions are ready to issue.  If
00637     // so, add them to the available queue.
00638     unsigned MinDepth = ~0u;
00639     for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
00640       if (PendingQueue[i]->getDepth() <= CurCycle) {
00641         AvailableQueue.push(PendingQueue[i]);
00642         PendingQueue[i]->isAvailable = true;
00643         PendingQueue[i] = PendingQueue.back();
00644         PendingQueue.pop_back();
00645         --i; --e;
00646       } else if (PendingQueue[i]->getDepth() < MinDepth)
00647         MinDepth = PendingQueue[i]->getDepth();
00648     }
00649     
00650     // If there are no instructions available, don't try to issue anything.
00651     if (AvailableQueue.empty()) {
00652       CurCycle = MinDepth != ~0u ? MinDepth : CurCycle + 1;
00653       continue;
00654     }
00655 
00656     SUnit *FoundSUnit = AvailableQueue.pop();
00657     
00658     // If we found a node to schedule, do it now.
00659     if (FoundSUnit) {
00660       ScheduleNodeTopDown(FoundSUnit, CurCycle);
00661 
00662       // If this is a pseudo-op node, we don't want to increment the current
00663       // cycle.
00664       if (FoundSUnit->Latency)  // Don't increment CurCycle for pseudo-ops!
00665         ++CurCycle;        
00666     } else {
00667       // Otherwise, we have a pipeline stall, but no other problem, just advance
00668       // the current cycle and try again.
00669       DOUT << "*** Advancing cycle, no work to do\n";
00670       ++NumStalls;
00671       ++CurCycle;
00672     }
00673   }
00674 
00675 #ifndef NDEBUG
00676   VerifySchedule(/*isBottomUp=*/false);
00677 #endif
00678 }
00679 
00680 //===----------------------------------------------------------------------===//
00681 //                         Public Constructor Functions
00682 //===----------------------------------------------------------------------===//
00683 
00684 FunctionPass *llvm::createPostRAScheduler() {
00685   return new PostRAScheduler();
00686 }



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.