LLVM API Documentation

ScheduleDAG.h

Go to the documentation of this file.
00001 //===------- llvm/CodeGen/ScheduleDAG.h - Common Base Class------*- C++ -*-===//
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 file implements the ScheduleDAG class, which is used as the common
00011 // base class for instruction schedulers.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CODEGEN_SCHEDULEDAG_H
00016 #define LLVM_CODEGEN_SCHEDULEDAG_H
00017 
00018 #include "llvm/CodeGen/MachineBasicBlock.h"
00019 #include "llvm/ADT/DenseMap.h"
00020 #include "llvm/ADT/BitVector.h"
00021 #include "llvm/ADT/GraphTraits.h"
00022 #include "llvm/ADT/SmallVector.h"
00023 #include "llvm/ADT/PointerIntPair.h"
00024 
00025 namespace llvm {
00026   struct SUnit;
00027   class MachineConstantPool;
00028   class MachineFunction;
00029   class MachineModuleInfo;
00030   class MachineRegisterInfo;
00031   class MachineInstr;
00032   class TargetRegisterInfo;
00033   class ScheduleDAG;
00034   class SelectionDAG;
00035   class SDNode;
00036   class TargetInstrInfo;
00037   class TargetInstrDesc;
00038   class TargetLowering;
00039   class TargetMachine;
00040   class TargetRegisterClass;
00041   template<class Graph> class GraphWriter;
00042 
00043   /// SDep - Scheduling dependency. This represents one direction of an
00044   /// edge in the scheduling DAG.
00045   class SDep {
00046   public:
00047     /// Kind - These are the different kinds of scheduling dependencies.
00048     enum Kind {
00049       Data,        ///< Regular data dependence (aka true-dependence).
00050       Anti,        ///< A register anti-dependedence (aka WAR).
00051       Output,      ///< A register output-dependence (aka WAW).
00052       Order        ///< Any other ordering dependency.
00053     };
00054 
00055   private:
00056     /// Dep - A pointer to the depending/depended-on SUnit, and an enum
00057     /// indicating the kind of the dependency.
00058     PointerIntPair<SUnit *, 2, Kind> Dep;
00059 
00060     /// Contents - A union discriminated by the dependence kind.
00061     union {
00062       /// Reg - For Data, Anti, and Output dependencies, the associated
00063       /// register. For Data dependencies that don't currently have a register
00064       /// assigned, this is set to zero.
00065       unsigned Reg;
00066 
00067       /// Order - Additional information about Order dependencies.
00068       struct {
00069         /// isNormalMemory - True if both sides of the dependence
00070         /// access memory in non-volatile and fully modeled ways.
00071         bool isNormalMemory : 1;
00072 
00073         /// isMustAlias - True if both sides of the dependence are known to
00074         /// access the same memory.
00075         bool isMustAlias : 1;
00076 
00077         /// isArtificial - True if this is an artificial dependency, meaning
00078         /// it is not necessary for program correctness, and may be safely
00079         /// deleted if necessary.
00080         bool isArtificial : 1;
00081       } Order;
00082     } Contents;
00083 
00084     /// Latency - The time associated with this edge. Often this is just
00085     /// the value of the Latency field of the predecessor, however advanced
00086     /// models may provide additional information about specific edges.
00087     unsigned Latency;
00088 
00089   public:
00090     /// SDep - Construct a null SDep. This is only for use by container
00091     /// classes which require default constructors. SUnits may not
00092     /// have null SDep edges.
00093     SDep() : Dep(0, Data) {}
00094 
00095     /// SDep - Construct an SDep with the specified values.
00096     SDep(SUnit *S, Kind kind, unsigned latency = 1, unsigned Reg = 0,
00097          bool isNormalMemory = false, bool isMustAlias = false,
00098          bool isArtificial = false)
00099       : Dep(S, kind), Contents(), Latency(latency) {
00100       switch (kind) {
00101       case Anti:
00102       case Output:
00103         assert(Reg != 0 &&
00104                "SDep::Anti and SDep::Output must use a non-zero Reg!");
00105         // fall through
00106       case Data:
00107         assert(!isMustAlias && "isMustAlias only applies with SDep::Order!");
00108         assert(!isArtificial && "isArtificial only applies with SDep::Order!");
00109         Contents.Reg = Reg;
00110         break;
00111       case Order:
00112         assert(Reg == 0 && "Reg given for non-register dependence!");
00113         Contents.Order.isNormalMemory = isNormalMemory;
00114         Contents.Order.isMustAlias = isMustAlias;
00115         Contents.Order.isArtificial = isArtificial;
00116         break;
00117       }
00118     }
00119 
00120     bool operator==(const SDep &Other) const {
00121       if (Dep != Other.Dep || Latency != Other.Latency) return false;
00122       switch (Dep.getInt()) {
00123       case Data:
00124       case Anti:
00125       case Output:
00126         return Contents.Reg == Other.Contents.Reg;
00127       case Order:
00128         return Contents.Order.isNormalMemory ==
00129                  Other.Contents.Order.isNormalMemory &&
00130                Contents.Order.isMustAlias == Other.Contents.Order.isMustAlias &&
00131                Contents.Order.isArtificial == Other.Contents.Order.isArtificial;
00132       }
00133       assert(0 && "Invalid dependency kind!");
00134       return false;
00135     }
00136 
00137     bool operator!=(const SDep &Other) const {
00138       return !operator==(Other);
00139     }
00140 
00141     /// getLatency - Return the latency value for this edge, which roughly
00142     /// means the minimum number of cycles that must elapse between the
00143     /// predecessor and the successor, given that they have this edge
00144     /// between them.
00145     unsigned getLatency() const {
00146       return Latency;
00147     }
00148 
00149     //// getSUnit - Return the SUnit to which this edge points.
00150     SUnit *getSUnit() const {
00151       return Dep.getPointer();
00152     }
00153 
00154     //// setSUnit - Assign the SUnit to which this edge points.
00155     void setSUnit(SUnit *SU) {
00156       Dep.setPointer(SU);
00157     }
00158 
00159     /// getKind - Return an enum value representing the kind of the dependence.
00160     Kind getKind() const {
00161       return Dep.getInt();
00162     }
00163 
00164     /// isCtrl - Shorthand for getKind() != SDep::Data.
00165     bool isCtrl() const {
00166       return getKind() != Data;
00167     }
00168 
00169     /// isNormalMemory - Test if this is an Order dependence between two
00170     /// memory accesses where both sides of the dependence access memory
00171     /// in non-volatile and fully modeled ways.
00172     bool isNormalMemory() const {
00173       return getKind() == Order && Contents.Order.isNormalMemory;
00174     }
00175 
00176     /// isMustAlias - Test if this is an Order dependence that is marked
00177     /// as "must alias", meaning that the SUnits at either end of the edge
00178     /// have a memory dependence on a known memory location.
00179     bool isMustAlias() const {
00180       return getKind() == Order && Contents.Order.isMustAlias;
00181     }
00182 
00183     /// isArtificial - Test if this is an Order dependence that is marked
00184     /// as "artificial", meaning it isn't necessary for correctness.
00185     bool isArtificial() const {
00186       return getKind() == Order && Contents.Order.isArtificial;
00187     }
00188 
00189     /// isAssignedRegDep - Test if this is a Data dependence that is
00190     /// associated with a register.
00191     bool isAssignedRegDep() const {
00192       return getKind() == Data && Contents.Reg != 0;
00193     }
00194 
00195     /// getReg - Return the register associated with this edge. This is
00196     /// only valid on Data, Anti, and Output edges. On Data edges, this
00197     /// value may be zero, meaning there is no associated register.
00198     unsigned getReg() const {
00199       assert((getKind() == Data || getKind() == Anti || getKind() == Output) &&
00200              "getReg called on non-register dependence edge!");
00201       return Contents.Reg;
00202     }
00203 
00204     /// setReg - Assign the associated register for this edge. This is
00205     /// only valid on Data, Anti, and Output edges. On Anti and Output
00206     /// edges, this value must not be zero. On Data edges, the value may
00207     /// be zero, which would mean that no specific register is associated
00208     /// with this edge.
00209     void setReg(unsigned Reg) {
00210       assert((getKind() == Data || getKind() == Anti || getKind() == Output) &&
00211              "setReg called on non-register dependence edge!");
00212       assert((getKind() != Anti || Reg != 0) &&
00213              "SDep::Anti edge cannot use the zero register!");
00214       assert((getKind() != Output || Reg != 0) &&
00215              "SDep::Output edge cannot use the zero register!");
00216       Contents.Reg = Reg;
00217     }
00218   };
00219 
00220   /// SUnit - Scheduling unit. This is a node in the scheduling DAG.
00221   struct SUnit {
00222   private:
00223     SDNode *Node;                       // Representative node.
00224     MachineInstr *Instr;                // Alternatively, a MachineInstr.
00225   public:
00226     SUnit *OrigNode;                    // If not this, the node from which
00227                                         // this node was cloned.
00228     
00229     // Preds/Succs - The SUnits before/after us in the graph.  The boolean value
00230     // is true if the edge is a token chain edge, false if it is a value edge. 
00231     SmallVector<SDep, 4> Preds;  // All sunit predecessors.
00232     SmallVector<SDep, 4> Succs;  // All sunit successors.
00233 
00234     typedef SmallVector<SDep, 4>::iterator pred_iterator;
00235     typedef SmallVector<SDep, 4>::iterator succ_iterator;
00236     typedef SmallVector<SDep, 4>::const_iterator const_pred_iterator;
00237     typedef SmallVector<SDep, 4>::const_iterator const_succ_iterator;
00238     
00239     unsigned NodeNum;                   // Entry # of node in the node vector.
00240     unsigned NodeQueueId;               // Queue id of node.
00241     unsigned short Latency;             // Node latency.
00242     short NumPreds;                     // # of SDep::Data preds.
00243     short NumSuccs;                     // # of SDep::Data sucss.
00244     short NumPredsLeft;                 // # of preds not scheduled.
00245     short NumSuccsLeft;                 // # of succs not scheduled.
00246     bool isTwoAddress     : 1;          // Is a two-address instruction.
00247     bool isCommutable     : 1;          // Is a commutable instruction.
00248     bool hasPhysRegDefs   : 1;          // Has physreg defs that are being used.
00249     bool isPending        : 1;          // True once pending.
00250     bool isAvailable      : 1;          // True once available.
00251     bool isScheduled      : 1;          // True once scheduled.
00252     bool isScheduleHigh   : 1;          // True if preferable to schedule high.
00253   private:
00254     bool isDepthCurrent   : 1;          // True if Depth is current.
00255     bool isHeightCurrent  : 1;          // True if Height is current.
00256     unsigned Depth;                     // Node depth.
00257     unsigned Height;                    // Node height.
00258   public:
00259     const TargetRegisterClass *CopyDstRC; // Is a special copy node if not null.
00260     const TargetRegisterClass *CopySrcRC;
00261     
00262     /// SUnit - Construct an SUnit for pre-regalloc scheduling to represent
00263     /// an SDNode and any nodes flagged to it.
00264     SUnit(SDNode *node, unsigned nodenum)
00265       : Node(node), Instr(0), OrigNode(0), NodeNum(nodenum), NodeQueueId(0),
00266         Latency(0), NumPreds(0), NumSuccs(0), NumPredsLeft(0), NumSuccsLeft(0),
00267         isTwoAddress(false), isCommutable(false), hasPhysRegDefs(false),
00268         isPending(false), isAvailable(false), isScheduled(false),
00269         isScheduleHigh(false), isDepthCurrent(false), isHeightCurrent(false),
00270         Depth(0), Height(0),
00271         CopyDstRC(NULL), CopySrcRC(NULL) {}
00272 
00273     /// SUnit - Construct an SUnit for post-regalloc scheduling to represent
00274     /// a MachineInstr.
00275     SUnit(MachineInstr *instr, unsigned nodenum)
00276       : Node(0), Instr(instr), OrigNode(0), NodeNum(nodenum), NodeQueueId(0),
00277         Latency(0), NumPreds(0), NumSuccs(0), NumPredsLeft(0), NumSuccsLeft(0),
00278         isTwoAddress(false), isCommutable(false), hasPhysRegDefs(false),
00279         isPending(false), isAvailable(false), isScheduled(false),
00280         isScheduleHigh(false), isDepthCurrent(false), isHeightCurrent(false),
00281         Depth(0), Height(0),
00282         CopyDstRC(NULL), CopySrcRC(NULL) {}
00283 
00284     /// setNode - Assign the representative SDNode for this SUnit.
00285     /// This may be used during pre-regalloc scheduling.
00286     void setNode(SDNode *N) {
00287       assert(!Instr && "Setting SDNode of SUnit with MachineInstr!");
00288       Node = N;
00289     }
00290 
00291     /// getNode - Return the representative SDNode for this SUnit.
00292     /// This may be used during pre-regalloc scheduling.
00293     SDNode *getNode() const {
00294       assert(!Instr && "Reading SDNode of SUnit with MachineInstr!");
00295       return Node;
00296     }
00297 
00298     /// setInstr - Assign the instruction for the SUnit.
00299     /// This may be used during post-regalloc scheduling.
00300     void setInstr(MachineInstr *MI) {
00301       assert(!Node && "Setting MachineInstr of SUnit with SDNode!");
00302       Instr = MI;
00303     }
00304 
00305     /// getInstr - Return the representative MachineInstr for this SUnit.
00306     /// This may be used during post-regalloc scheduling.
00307     MachineInstr *getInstr() const {
00308       assert(!Node && "Reading MachineInstr of SUnit with SDNode!");
00309       return Instr;
00310     }
00311 
00312     /// addPred - This adds the specified edge as a pred of the current node if
00313     /// not already.  It also adds the current node as a successor of the
00314     /// specified node.
00315     void addPred(const SDep &D);
00316 
00317     /// removePred - This removes the specified edge as a pred of the current
00318     /// node if it exists.  It also removes the current node as a successor of
00319     /// the specified node.
00320     void removePred(const SDep &D);
00321 
00322     /// getDepth - Return the depth of this node, which is the length of the
00323     /// maximum path up to any node with has no predecessors.
00324     unsigned getDepth() const {
00325       if (!isDepthCurrent) const_cast<SUnit *>(this)->ComputeDepth();
00326       return Depth;
00327     }
00328 
00329     /// getHeight - Return the height of this node, which is the length of the
00330     /// maximum path down to any node with has no successors.
00331     unsigned getHeight() const {
00332       if (!isHeightCurrent) const_cast<SUnit *>(this)->ComputeHeight();
00333       return Height;
00334     }
00335 
00336     /// setDepthToAtLeast - If NewDepth is greater than this node's depth
00337     /// value, set it to be the new depth value. This also recursively
00338     /// marks successor nodes dirty.
00339     void setDepthToAtLeast(unsigned NewDepth);
00340 
00341     /// setDepthToAtLeast - If NewDepth is greater than this node's depth
00342     /// value, set it to be the new height value. This also recursively
00343     /// marks predecessor nodes dirty.
00344     void setHeightToAtLeast(unsigned NewHeight);
00345 
00346     /// setDepthDirty - Set a flag in this node to indicate that its
00347     /// stored Depth value will require recomputation the next time
00348     /// getDepth() is called.
00349     void setDepthDirty();
00350 
00351     /// setHeightDirty - Set a flag in this node to indicate that its
00352     /// stored Height value will require recomputation the next time
00353     /// getHeight() is called.
00354     void setHeightDirty();
00355 
00356     /// isPred - Test if node N is a predecessor of this node.
00357     bool isPred(SUnit *N) {
00358       for (unsigned i = 0, e = (unsigned)Preds.size(); i != e; ++i)
00359         if (Preds[i].getSUnit() == N)
00360           return true;
00361       return false;
00362     }
00363     
00364     /// isSucc - Test if node N is a successor of this node.
00365     bool isSucc(SUnit *N) {
00366       for (unsigned i = 0, e = (unsigned)Succs.size(); i != e; ++i)
00367         if (Succs[i].getSUnit() == N)
00368           return true;
00369       return false;
00370     }
00371     
00372     void dump(const ScheduleDAG *G) const;
00373     void dumpAll(const ScheduleDAG *G) const;
00374     void print(raw_ostream &O, const ScheduleDAG *G) const;
00375 
00376   private:
00377     void ComputeDepth();
00378     void ComputeHeight();
00379   };
00380 
00381   //===--------------------------------------------------------------------===//
00382   /// SchedulingPriorityQueue - This interface is used to plug different
00383   /// priorities computation algorithms into the list scheduler. It implements
00384   /// the interface of a standard priority queue, where nodes are inserted in 
00385   /// arbitrary order and returned in priority order.  The computation of the
00386   /// priority and the representation of the queue are totally up to the
00387   /// implementation to decide.
00388   /// 
00389   class SchedulingPriorityQueue {
00390   public:
00391     virtual ~SchedulingPriorityQueue() {}
00392   
00393     virtual void initNodes(std::vector<SUnit> &SUnits) = 0;
00394     virtual void addNode(const SUnit *SU) = 0;
00395     virtual void updateNode(const SUnit *SU) = 0;
00396     virtual void releaseState() = 0;
00397 
00398     virtual unsigned size() const = 0;
00399     virtual bool empty() const = 0;
00400     virtual void push(SUnit *U) = 0;
00401   
00402     virtual void push_all(const std::vector<SUnit *> &Nodes) = 0;
00403     virtual SUnit *pop() = 0;
00404 
00405     virtual void remove(SUnit *SU) = 0;
00406 
00407     /// ScheduledNode - As each node is scheduled, this method is invoked.  This
00408     /// allows the priority function to adjust the priority of related
00409     /// unscheduled nodes, for example.
00410     ///
00411     virtual void ScheduledNode(SUnit *) {}
00412 
00413     virtual void UnscheduledNode(SUnit *) {}
00414   };
00415 
00416   class ScheduleDAG {
00417   public:
00418     SelectionDAG *DAG;                    // DAG of the current basic block
00419     MachineBasicBlock *BB;                // Current basic block
00420     const TargetMachine &TM;              // Target processor
00421     const TargetInstrInfo *TII;           // Target instruction information
00422     const TargetRegisterInfo *TRI;        // Target processor register info
00423     TargetLowering *TLI;                  // Target lowering info
00424     MachineFunction *MF;                  // Machine function
00425     MachineRegisterInfo &MRI;             // Virtual/real register map
00426     MachineConstantPool *ConstPool;       // Target constant pool
00427     std::vector<SUnit*> Sequence;         // The schedule. Null SUnit*'s
00428                                           // represent noop instructions.
00429     std::vector<SUnit> SUnits;            // The scheduling units.
00430 
00431     ScheduleDAG(SelectionDAG *dag, MachineBasicBlock *bb,
00432                 const TargetMachine &tm);
00433 
00434     virtual ~ScheduleDAG();
00435 
00436     /// viewGraph - Pop up a GraphViz/gv window with the ScheduleDAG rendered
00437     /// using 'dot'.
00438     ///
00439     void viewGraph();
00440   
00441     /// Run - perform scheduling.
00442     ///
00443     void Run();
00444 
00445     /// BuildSchedGraph - Build SUnits and set up their Preds and Succs
00446     /// to form the scheduling dependency graph.
00447     ///
00448     virtual void BuildSchedGraph() = 0;
00449 
00450     /// ComputeLatency - Compute node latency.
00451     ///
00452     virtual void ComputeLatency(SUnit *SU) = 0;
00453 
00454   protected:
00455     /// EmitNoop - Emit a noop instruction.
00456     ///
00457     void EmitNoop();
00458 
00459   public:
00460     virtual MachineBasicBlock *EmitSchedule() = 0;
00461 
00462     void dumpSchedule() const;
00463 
00464     /// Schedule - Order nodes according to selected style, filling
00465     /// in the Sequence member.
00466     ///
00467     virtual void Schedule() = 0;
00468 
00469     virtual void dumpNode(const SUnit *SU) const = 0;
00470 
00471     /// getGraphNodeLabel - Return a label for an SUnit node in a visualization
00472     /// of the ScheduleDAG.
00473     virtual std::string getGraphNodeLabel(const SUnit *SU) const = 0;
00474 
00475     /// addCustomGraphFeatures - Add custom features for a visualization of
00476     /// the ScheduleDAG.
00477     virtual void addCustomGraphFeatures(GraphWriter<ScheduleDAG*> &) const {}
00478 
00479 #ifndef NDEBUG
00480     /// VerifySchedule - Verify that all SUnits were scheduled and that
00481     /// their state is consistent.
00482     void VerifySchedule(bool isBottomUp);
00483 #endif
00484 
00485   protected:
00486     void AddMemOperand(MachineInstr *MI, const MachineMemOperand &MO);
00487 
00488     void EmitCrossRCCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap);
00489 
00490     /// ForceUnitLatencies - Return true if all scheduling edges should be given a
00491     /// latency value of one.  The default is to return false; schedulers may
00492     /// override this as needed.
00493     virtual bool ForceUnitLatencies() const { return false; }
00494 
00495   private:
00496     /// EmitLiveInCopy - Emit a copy for a live in physical register. If the
00497     /// physical register has only a single copy use, then coalesced the copy
00498     /// if possible.
00499     void EmitLiveInCopy(MachineBasicBlock *MBB,
00500                         MachineBasicBlock::iterator &InsertPos,
00501                         unsigned VirtReg, unsigned PhysReg,
00502                         const TargetRegisterClass *RC,
00503                         DenseMap<MachineInstr*, unsigned> &CopyRegMap);
00504 
00505     /// EmitLiveInCopies - If this is the first basic block in the function,
00506     /// and if it has live ins that need to be copied into vregs, emit the
00507     /// copies into the top of the block.
00508     void EmitLiveInCopies(MachineBasicBlock *MBB);
00509   };
00510 
00511   class SUnitIterator : public forward_iterator<SUnit, ptrdiff_t> {
00512     SUnit *Node;
00513     unsigned Operand;
00514 
00515     SUnitIterator(SUnit *N, unsigned Op) : Node(N), Operand(Op) {}
00516   public:
00517     bool operator==(const SUnitIterator& x) const {
00518       return Operand == x.Operand;
00519     }
00520     bool operator!=(const SUnitIterator& x) const { return !operator==(x); }
00521 
00522     const SUnitIterator &operator=(const SUnitIterator &I) {
00523       assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
00524       Operand = I.Operand;
00525       return *this;
00526     }
00527 
00528     pointer operator*() const {
00529       return Node->Preds[Operand].getSUnit();
00530     }
00531     pointer operator->() const { return operator*(); }
00532 
00533     SUnitIterator& operator++() {                // Preincrement
00534       ++Operand;
00535       return *this;
00536     }
00537     SUnitIterator operator++(int) { // Postincrement
00538       SUnitIterator tmp = *this; ++*this; return tmp;
00539     }
00540 
00541     static SUnitIterator begin(SUnit *N) { return SUnitIterator(N, 0); }
00542     static SUnitIterator end  (SUnit *N) {
00543       return SUnitIterator(N, (unsigned)N->Preds.size());
00544     }
00545 
00546     unsigned getOperand() const { return Operand; }
00547     const SUnit *getNode() const { return Node; }
00548     /// isCtrlDep - Test if this is not an SDep::Data dependence.
00549     bool isCtrlDep() const {
00550       return getSDep().isCtrl();
00551     }
00552     bool isArtificialDep() const {
00553       return getSDep().isArtificial();
00554     }
00555     const SDep &getSDep() const {
00556       return Node->Preds[Operand];
00557     }
00558   };
00559 
00560   template <> struct GraphTraits<SUnit*> {
00561     typedef SUnit NodeType;
00562     typedef SUnitIterator ChildIteratorType;
00563     static inline NodeType *getEntryNode(SUnit *N) { return N; }
00564     static inline ChildIteratorType child_begin(NodeType *N) {
00565       return SUnitIterator::begin(N);
00566     }
00567     static inline ChildIteratorType child_end(NodeType *N) {
00568       return SUnitIterator::end(N);
00569     }
00570   };
00571 
00572   template <> struct GraphTraits<ScheduleDAG*> : public GraphTraits<SUnit*> {
00573     typedef std::vector<SUnit>::iterator nodes_iterator;
00574     static nodes_iterator nodes_begin(ScheduleDAG *G) {
00575       return G->SUnits.begin();
00576     }
00577     static nodes_iterator nodes_end(ScheduleDAG *G) {
00578       return G->SUnits.end();
00579     }
00580   };
00581 
00582   /// ScheduleDAGTopologicalSort is a class that computes a topological
00583   /// ordering for SUnits and provides methods for dynamically updating
00584   /// the ordering as new edges are added.
00585   ///
00586   /// This allows a very fast implementation of IsReachable, for example.
00587   ///
00588   class ScheduleDAGTopologicalSort {
00589     /// SUnits - A reference to the ScheduleDAG's SUnits.
00590     std::vector<SUnit> &SUnits;
00591 
00592     /// Index2Node - Maps topological index to the node number.
00593     std::vector<int> Index2Node;
00594     /// Node2Index - Maps the node number to its topological index.
00595     std::vector<int> Node2Index;
00596     /// Visited - a set of nodes visited during a DFS traversal.
00597     BitVector Visited;
00598 
00599     /// DFS - make a DFS traversal and mark all nodes affected by the 
00600     /// edge insertion. These nodes will later get new topological indexes
00601     /// by means of the Shift method.
00602     void DFS(const SUnit *SU, int UpperBound, bool& HasLoop);
00603 
00604     /// Shift - reassign topological indexes for the nodes in the DAG
00605     /// to preserve the topological ordering.
00606     void Shift(BitVector& Visited, int LowerBound, int UpperBound);
00607 
00608     /// Allocate - assign the topological index to the node n.
00609     void Allocate(int n, int index);
00610 
00611   public:
00612     explicit ScheduleDAGTopologicalSort(std::vector<SUnit> &SUnits);
00613 
00614     /// InitDAGTopologicalSorting - create the initial topological 
00615     /// ordering from the DAG to be scheduled.
00616     void InitDAGTopologicalSorting();
00617 
00618     /// IsReachable - Checks if SU is reachable from TargetSU.
00619     bool IsReachable(const SUnit *SU, const SUnit *TargetSU);
00620 
00621     /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU
00622     /// will create a cycle.
00623     bool WillCreateCycle(SUnit *SU, SUnit *TargetSU);
00624 
00625     /// AddPred - Updates the topological ordering to accomodate an edge
00626     /// to be added from SUnit X to SUnit Y.
00627     void AddPred(SUnit *Y, SUnit *X);
00628 
00629     /// RemovePred - Updates the topological ordering to accomodate an
00630     /// an edge to be removed from the specified node N from the predecessors
00631     /// of the current node M.
00632     void RemovePred(SUnit *M, SUnit *N);
00633 
00634     typedef std::vector<int>::iterator iterator;
00635     typedef std::vector<int>::const_iterator const_iterator;
00636     iterator begin() { return Index2Node.begin(); }
00637     const_iterator begin() const { return Index2Node.begin(); }
00638     iterator end() { return Index2Node.end(); }
00639     const_iterator end() const { return Index2Node.end(); }
00640 
00641     typedef std::vector<int>::reverse_iterator reverse_iterator;
00642     typedef std::vector<int>::const_reverse_iterator const_reverse_iterator;
00643     reverse_iterator rbegin() { return Index2Node.rbegin(); }
00644     const_reverse_iterator rbegin() const { return Index2Node.rbegin(); }
00645     reverse_iterator rend() { return Index2Node.rend(); }
00646     const_reverse_iterator rend() const { return Index2Node.rend(); }
00647   };
00648 }
00649 
00650 #endif



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