ScheduleDAG.h revision 50bee42b54cd9aec5f49566307df2b0cf23afcf6
1//===------- llvm/CodeGen/ScheduleDAG.h - Common Base Class------*- C++ -*-===//
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 file implements the ScheduleDAG class, which is used as the common
11// base class for instruction schedulers.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_SCHEDULEDAG_H
16#define LLVM_CODEGEN_SCHEDULEDAG_H
17
18#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/Target/TargetLowering.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/BitVector.h"
22#include "llvm/ADT/GraphTraits.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/PointerIntPair.h"
25
26namespace llvm {
27  class AliasAnalysis;
28  class SUnit;
29  class MachineConstantPool;
30  class MachineFunction;
31  class MachineRegisterInfo;
32  class MachineInstr;
33  class TargetRegisterInfo;
34  class ScheduleDAG;
35  class SDNode;
36  class TargetInstrInfo;
37  class MCInstrDesc;
38  class TargetMachine;
39  class TargetRegisterClass;
40  template<class Graph> class GraphWriter;
41
42  /// SDep - Scheduling dependency. This represents one direction of an
43  /// edge in the scheduling DAG.
44  class SDep {
45  public:
46    /// Kind - These are the different kinds of scheduling dependencies.
47    enum Kind {
48      Data,        ///< Regular data dependence (aka true-dependence).
49      Anti,        ///< A register anti-dependedence (aka WAR).
50      Output,      ///< A register output-dependence (aka WAW).
51      Order        ///< Any other ordering dependency.
52    };
53
54  private:
55    /// Dep - A pointer to the depending/depended-on SUnit, and an enum
56    /// indicating the kind of the dependency.
57    PointerIntPair<SUnit *, 2, Kind> Dep;
58
59    /// Contents - A union discriminated by the dependence kind.
60    union {
61      /// Reg - For Data, Anti, and Output dependencies, the associated
62      /// register. For Data dependencies that don't currently have a register
63      /// assigned, this is set to zero.
64      unsigned Reg;
65
66      /// Order - Additional information about Order dependencies.
67      struct {
68        /// isNormalMemory - True if both sides of the dependence
69        /// access memory in non-volatile and fully modeled ways.
70        bool isNormalMemory : 1;
71
72        /// isMustAlias - True if both sides of the dependence are known to
73        /// access the same memory.
74        bool isMustAlias : 1;
75
76        /// isArtificial - True if this is an artificial dependency, meaning
77        /// it is not necessary for program correctness, and may be safely
78        /// deleted if necessary.
79        bool isArtificial : 1;
80      } Order;
81    } Contents;
82
83    /// Latency - The time associated with this edge. Often this is just
84    /// the value of the Latency field of the predecessor, however advanced
85    /// models may provide additional information about specific edges.
86    unsigned Latency;
87
88  public:
89    /// SDep - Construct a null SDep. This is only for use by container
90    /// classes which require default constructors. SUnits may not
91    /// have null SDep edges.
92    SDep() : Dep(0, Data) {}
93
94    /// SDep - Construct an SDep with the specified values.
95    SDep(SUnit *S, Kind kind, unsigned latency = 1, unsigned Reg = 0,
96         bool isNormalMemory = false, bool isMustAlias = false,
97         bool isArtificial = false)
98      : Dep(S, kind), Contents(), Latency(latency) {
99      switch (kind) {
100      case Anti:
101      case Output:
102        assert(Reg != 0 &&
103               "SDep::Anti and SDep::Output must use a non-zero Reg!");
104        // fall through
105      case Data:
106        assert(!isMustAlias && "isMustAlias only applies with SDep::Order!");
107        assert(!isArtificial && "isArtificial only applies with SDep::Order!");
108        Contents.Reg = Reg;
109        break;
110      case Order:
111        assert(Reg == 0 && "Reg given for non-register dependence!");
112        Contents.Order.isNormalMemory = isNormalMemory;
113        Contents.Order.isMustAlias = isMustAlias;
114        Contents.Order.isArtificial = isArtificial;
115        break;
116      }
117    }
118
119    bool operator==(const SDep &Other) const {
120      if (Dep != Other.Dep || Latency != Other.Latency) return false;
121      switch (Dep.getInt()) {
122      case Data:
123      case Anti:
124      case Output:
125        return Contents.Reg == Other.Contents.Reg;
126      case Order:
127        return Contents.Order.isNormalMemory ==
128                 Other.Contents.Order.isNormalMemory &&
129               Contents.Order.isMustAlias == Other.Contents.Order.isMustAlias &&
130               Contents.Order.isArtificial == Other.Contents.Order.isArtificial;
131      default: llvm_unreachable("Invalid dependency kind!");
132      }
133    }
134
135    bool operator!=(const SDep &Other) const {
136      return !operator==(Other);
137    }
138
139    /// getLatency - Return the latency value for this edge, which roughly
140    /// means the minimum number of cycles that must elapse between the
141    /// predecessor and the successor, given that they have this edge
142    /// between them.
143    unsigned getLatency() const {
144      return Latency;
145    }
146
147    /// setLatency - Set the latency for this edge.
148    void setLatency(unsigned Lat) {
149      Latency = Lat;
150    }
151
152    //// getSUnit - Return the SUnit to which this edge points.
153    SUnit *getSUnit() const {
154      return Dep.getPointer();
155    }
156
157    //// setSUnit - Assign the SUnit to which this edge points.
158    void setSUnit(SUnit *SU) {
159      Dep.setPointer(SU);
160    }
161
162    /// getKind - Return an enum value representing the kind of the dependence.
163    Kind getKind() const {
164      return Dep.getInt();
165    }
166
167    /// isCtrl - Shorthand for getKind() != SDep::Data.
168    bool isCtrl() const {
169      return getKind() != Data;
170    }
171
172    /// isNormalMemory - Test if this is an Order dependence between two
173    /// memory accesses where both sides of the dependence access memory
174    /// in non-volatile and fully modeled ways.
175    bool isNormalMemory() const {
176      return getKind() == Order && Contents.Order.isNormalMemory;
177    }
178
179    /// isMustAlias - Test if this is an Order dependence that is marked
180    /// as "must alias", meaning that the SUnits at either end of the edge
181    /// have a memory dependence on a known memory location.
182    bool isMustAlias() const {
183      return getKind() == Order && Contents.Order.isMustAlias;
184    }
185
186    /// isArtificial - Test if this is an Order dependence that is marked
187    /// as "artificial", meaning it isn't necessary for correctness.
188    bool isArtificial() const {
189      return getKind() == Order && Contents.Order.isArtificial;
190    }
191
192    /// isAssignedRegDep - Test if this is a Data dependence that is
193    /// associated with a register.
194    bool isAssignedRegDep() const {
195      return getKind() == Data && Contents.Reg != 0;
196    }
197
198    /// getReg - Return the register associated with this edge. This is
199    /// only valid on Data, Anti, and Output edges. On Data edges, this
200    /// value may be zero, meaning there is no associated register.
201    unsigned getReg() const {
202      assert((getKind() == Data || getKind() == Anti || getKind() == Output) &&
203             "getReg called on non-register dependence edge!");
204      return Contents.Reg;
205    }
206
207    /// setReg - Assign the associated register for this edge. This is
208    /// only valid on Data, Anti, and Output edges. On Anti and Output
209    /// edges, this value must not be zero. On Data edges, the value may
210    /// be zero, which would mean that no specific register is associated
211    /// with this edge.
212    void setReg(unsigned Reg) {
213      assert((getKind() == Data || getKind() == Anti || getKind() == Output) &&
214             "setReg called on non-register dependence edge!");
215      assert((getKind() != Anti || Reg != 0) &&
216             "SDep::Anti edge cannot use the zero register!");
217      assert((getKind() != Output || Reg != 0) &&
218             "SDep::Output edge cannot use the zero register!");
219      Contents.Reg = Reg;
220    }
221  };
222
223  template <>
224  struct isPodLike<SDep> { static const bool value = true; };
225
226  /// SUnit - Scheduling unit. This is a node in the scheduling DAG.
227  class SUnit {
228  private:
229    SDNode *Node;                       // Representative node.
230    MachineInstr *Instr;                // Alternatively, a MachineInstr.
231  public:
232    SUnit *OrigNode;                    // If not this, the node from which
233                                        // this node was cloned.
234
235    // Preds/Succs - The SUnits before/after us in the graph.
236    SmallVector<SDep, 4> Preds;  // All sunit predecessors.
237    SmallVector<SDep, 4> Succs;  // All sunit successors.
238
239    typedef SmallVector<SDep, 4>::iterator pred_iterator;
240    typedef SmallVector<SDep, 4>::iterator succ_iterator;
241    typedef SmallVector<SDep, 4>::const_iterator const_pred_iterator;
242    typedef SmallVector<SDep, 4>::const_iterator const_succ_iterator;
243
244    unsigned NodeNum;                   // Entry # of node in the node vector.
245    unsigned NodeQueueId;               // Queue id of node.
246    unsigned NumPreds;                  // # of SDep::Data preds.
247    unsigned NumSuccs;                  // # of SDep::Data sucss.
248    unsigned NumPredsLeft;              // # of preds not scheduled.
249    unsigned NumSuccsLeft;              // # of succs not scheduled.
250    unsigned short NumRegDefsLeft;      // # of reg defs with no scheduled use.
251    unsigned short Latency;             // Node latency.
252    bool isVRegCycle      : 1;          // May use and def the same vreg.
253    bool isCall           : 1;          // Is a function call.
254    bool isCallOp         : 1;          // Is a function call operand.
255    bool isTwoAddress     : 1;          // Is a two-address instruction.
256    bool isCommutable     : 1;          // Is a commutable instruction.
257    bool hasPhysRegDefs   : 1;          // Has physreg defs that are being used.
258    bool hasPhysRegClobbers : 1;        // Has any physreg defs, used or not.
259    bool isPending        : 1;          // True once pending.
260    bool isAvailable      : 1;          // True once available.
261    bool isScheduled      : 1;          // True once scheduled.
262    bool isScheduleHigh   : 1;          // True if preferable to schedule high.
263    bool isScheduleLow    : 1;          // True if preferable to schedule low.
264    bool isCloned         : 1;          // True if this node has been cloned.
265    Sched::Preference SchedulingPref;   // Scheduling preference.
266
267  private:
268    bool isDepthCurrent   : 1;          // True if Depth is current.
269    bool isHeightCurrent  : 1;          // True if Height is current.
270    unsigned Depth;                     // Node depth.
271    unsigned Height;                    // Node height.
272  public:
273    const TargetRegisterClass *CopyDstRC; // Is a special copy node if not null.
274    const TargetRegisterClass *CopySrcRC;
275
276    /// SUnit - Construct an SUnit for pre-regalloc scheduling to represent
277    /// an SDNode and any nodes flagged to it.
278    SUnit(SDNode *node, unsigned nodenum)
279      : Node(node), Instr(0), OrigNode(0), NodeNum(nodenum),
280        NodeQueueId(0), NumPreds(0), NumSuccs(0), NumPredsLeft(0),
281        NumSuccsLeft(0), NumRegDefsLeft(0), Latency(0),
282        isVRegCycle(false), isCall(false), isCallOp(false), isTwoAddress(false),
283        isCommutable(false), hasPhysRegDefs(false), hasPhysRegClobbers(false),
284        isPending(false), isAvailable(false), isScheduled(false),
285        isScheduleHigh(false), isScheduleLow(false), isCloned(false),
286        SchedulingPref(Sched::None),
287        isDepthCurrent(false), isHeightCurrent(false), Depth(0), Height(0),
288        CopyDstRC(NULL), CopySrcRC(NULL) {}
289
290    /// SUnit - Construct an SUnit for post-regalloc scheduling to represent
291    /// a MachineInstr.
292    SUnit(MachineInstr *instr, unsigned nodenum)
293      : Node(0), Instr(instr), OrigNode(0), NodeNum(nodenum),
294        NodeQueueId(0), NumPreds(0), NumSuccs(0), NumPredsLeft(0),
295        NumSuccsLeft(0), NumRegDefsLeft(0), Latency(0),
296        isVRegCycle(false), isCall(false), isCallOp(false), isTwoAddress(false),
297        isCommutable(false), hasPhysRegDefs(false), hasPhysRegClobbers(false),
298        isPending(false), isAvailable(false), isScheduled(false),
299        isScheduleHigh(false), isScheduleLow(false), isCloned(false),
300        SchedulingPref(Sched::None),
301        isDepthCurrent(false), isHeightCurrent(false), Depth(0), Height(0),
302        CopyDstRC(NULL), CopySrcRC(NULL) {}
303
304    /// SUnit - Construct a placeholder SUnit.
305    SUnit()
306      : Node(0), Instr(0), OrigNode(0), NodeNum(~0u),
307        NodeQueueId(0), NumPreds(0), NumSuccs(0), NumPredsLeft(0),
308        NumSuccsLeft(0), NumRegDefsLeft(0), Latency(0),
309        isVRegCycle(false), isCall(false), isCallOp(false), isTwoAddress(false),
310        isCommutable(false), hasPhysRegDefs(false), hasPhysRegClobbers(false),
311        isPending(false), isAvailable(false), isScheduled(false),
312        isScheduleHigh(false), isScheduleLow(false), isCloned(false),
313        SchedulingPref(Sched::None),
314        isDepthCurrent(false), isHeightCurrent(false), Depth(0), Height(0),
315        CopyDstRC(NULL), CopySrcRC(NULL) {}
316
317    /// setNode - Assign the representative SDNode for this SUnit.
318    /// This may be used during pre-regalloc scheduling.
319    void setNode(SDNode *N) {
320      assert(!Instr && "Setting SDNode of SUnit with MachineInstr!");
321      Node = N;
322    }
323
324    /// getNode - Return the representative SDNode for this SUnit.
325    /// This may be used during pre-regalloc scheduling.
326    SDNode *getNode() const {
327      assert(!Instr && "Reading SDNode of SUnit with MachineInstr!");
328      return Node;
329    }
330
331    /// isInstr - Return true if this SUnit refers to a machine instruction as
332    /// opposed to an SDNode.
333    bool isInstr() const { return Instr; }
334
335    /// setInstr - Assign the instruction for the SUnit.
336    /// This may be used during post-regalloc scheduling.
337    void setInstr(MachineInstr *MI) {
338      assert(!Node && "Setting MachineInstr of SUnit with SDNode!");
339      Instr = MI;
340    }
341
342    /// getInstr - Return the representative MachineInstr for this SUnit.
343    /// This may be used during post-regalloc scheduling.
344    MachineInstr *getInstr() const {
345      assert(!Node && "Reading MachineInstr of SUnit with SDNode!");
346      return Instr;
347    }
348
349    /// addPred - This adds the specified edge as a pred of the current node if
350    /// not already.  It also adds the current node as a successor of the
351    /// specified node.
352    bool addPred(const SDep &D);
353
354    /// removePred - This removes the specified edge as a pred of the current
355    /// node if it exists.  It also removes the current node as a successor of
356    /// the specified node.
357    void removePred(const SDep &D);
358
359    /// getDepth - Return the depth of this node, which is the length of the
360    /// maximum path up to any node which has no predecessors.
361    unsigned getDepth() const {
362      if (!isDepthCurrent)
363        const_cast<SUnit *>(this)->ComputeDepth();
364      return Depth;
365    }
366
367    /// getHeight - Return the height of this node, which is the length of the
368    /// maximum path down to any node which has no successors.
369    unsigned getHeight() const {
370      if (!isHeightCurrent)
371        const_cast<SUnit *>(this)->ComputeHeight();
372      return Height;
373    }
374
375    /// setDepthToAtLeast - If NewDepth is greater than this node's
376    /// depth value, set it to be the new depth value. This also
377    /// recursively marks successor nodes dirty.
378    void setDepthToAtLeast(unsigned NewDepth);
379
380    /// setDepthToAtLeast - If NewDepth is greater than this node's
381    /// depth value, set it to be the new height value. This also
382    /// recursively marks predecessor nodes dirty.
383    void setHeightToAtLeast(unsigned NewHeight);
384
385    /// setDepthDirty - Set a flag in this node to indicate that its
386    /// stored Depth value will require recomputation the next time
387    /// getDepth() is called.
388    void setDepthDirty();
389
390    /// setHeightDirty - Set a flag in this node to indicate that its
391    /// stored Height value will require recomputation the next time
392    /// getHeight() is called.
393    void setHeightDirty();
394
395    /// isPred - Test if node N is a predecessor of this node.
396    bool isPred(SUnit *N) {
397      for (unsigned i = 0, e = (unsigned)Preds.size(); i != e; ++i)
398        if (Preds[i].getSUnit() == N)
399          return true;
400      return false;
401    }
402
403    /// isSucc - Test if node N is a successor of this node.
404    bool isSucc(SUnit *N) {
405      for (unsigned i = 0, e = (unsigned)Succs.size(); i != e; ++i)
406        if (Succs[i].getSUnit() == N)
407          return true;
408      return false;
409    }
410
411    void dump(const ScheduleDAG *G) const;
412    void dumpAll(const ScheduleDAG *G) const;
413    void print(raw_ostream &O, const ScheduleDAG *G) const;
414
415  private:
416    void ComputeDepth();
417    void ComputeHeight();
418  };
419
420  //===--------------------------------------------------------------------===//
421  /// SchedulingPriorityQueue - This interface is used to plug different
422  /// priorities computation algorithms into the list scheduler. It implements
423  /// the interface of a standard priority queue, where nodes are inserted in
424  /// arbitrary order and returned in priority order.  The computation of the
425  /// priority and the representation of the queue are totally up to the
426  /// implementation to decide.
427  ///
428  class SchedulingPriorityQueue {
429    virtual void anchor();
430    unsigned CurCycle;
431    bool HasReadyFilter;
432  public:
433    SchedulingPriorityQueue(bool rf = false):
434      CurCycle(0), HasReadyFilter(rf) {}
435    virtual ~SchedulingPriorityQueue() {}
436
437    virtual bool isBottomUp() const = 0;
438
439    virtual void initNodes(std::vector<SUnit> &SUnits) = 0;
440    virtual void addNode(const SUnit *SU) = 0;
441    virtual void updateNode(const SUnit *SU) = 0;
442    virtual void releaseState() = 0;
443
444    virtual bool empty() const = 0;
445
446    bool hasReadyFilter() const { return HasReadyFilter; }
447
448    virtual bool tracksRegPressure() const { return false; }
449
450    virtual bool isReady(SUnit *) const {
451      assert(!HasReadyFilter && "The ready filter must override isReady()");
452      return true;
453    }
454    virtual void push(SUnit *U) = 0;
455
456    void push_all(const std::vector<SUnit *> &Nodes) {
457      for (std::vector<SUnit *>::const_iterator I = Nodes.begin(),
458           E = Nodes.end(); I != E; ++I)
459        push(*I);
460    }
461
462    virtual SUnit *pop() = 0;
463
464    virtual void remove(SUnit *SU) = 0;
465
466    virtual void dump(ScheduleDAG *) const {}
467
468    /// ScheduledNode - As each node is scheduled, this method is invoked.  This
469    /// allows the priority function to adjust the priority of related
470    /// unscheduled nodes, for example.
471    ///
472    virtual void ScheduledNode(SUnit *) {}
473
474    virtual void UnscheduledNode(SUnit *) {}
475
476    void setCurCycle(unsigned Cycle) {
477      CurCycle = Cycle;
478    }
479
480    unsigned getCurCycle() const {
481      return CurCycle;
482    }
483  };
484
485  class ScheduleDAG {
486  public:
487    MachineBasicBlock *BB;          // The block in which to insert instructions
488    MachineBasicBlock::iterator InsertPos;// The position to insert instructions
489    const TargetMachine &TM;              // Target processor
490    const TargetInstrInfo *TII;           // Target instruction information
491    const TargetRegisterInfo *TRI;        // Target processor register info
492    MachineFunction &MF;                  // Machine function
493    MachineRegisterInfo &MRI;             // Virtual/real register map
494    std::vector<SUnit*> Sequence;         // The schedule. Null SUnit*'s
495                                          // represent noop instructions.
496    std::vector<SUnit> SUnits;            // The scheduling units.
497    SUnit EntrySU;                        // Special node for the region entry.
498    SUnit ExitSU;                         // Special node for the region exit.
499
500#ifdef NDEBUG
501    static const bool StressSched = false;
502#else
503    bool StressSched;
504#endif
505
506    explicit ScheduleDAG(MachineFunction &mf);
507
508    virtual ~ScheduleDAG();
509
510    /// getInstrDesc - Return the MCInstrDesc of this SUnit.
511    /// Return NULL for SDNodes without a machine opcode.
512    const MCInstrDesc *getInstrDesc(const SUnit *SU) const {
513      if (SU->isInstr()) return &SU->getInstr()->getDesc();
514      return getNodeDesc(SU->getNode());
515    }
516
517    /// viewGraph - Pop up a GraphViz/gv window with the ScheduleDAG rendered
518    /// using 'dot'.
519    ///
520    void viewGraph();
521
522    /// EmitSchedule - Insert MachineInstrs into the MachineBasicBlock
523    /// according to the order specified in Sequence.
524    ///
525    virtual MachineBasicBlock *EmitSchedule() = 0;
526
527    void dumpSchedule() const;
528
529    virtual void dumpNode(const SUnit *SU) const = 0;
530
531    /// getGraphNodeLabel - Return a label for an SUnit node in a visualization
532    /// of the ScheduleDAG.
533    virtual std::string getGraphNodeLabel(const SUnit *SU) const = 0;
534
535    /// addCustomGraphFeatures - Add custom features for a visualization of
536    /// the ScheduleDAG.
537    virtual void addCustomGraphFeatures(GraphWriter<ScheduleDAG*> &) const {}
538
539#ifndef NDEBUG
540    /// VerifySchedule - Verify that all SUnits were scheduled and that
541    /// their state is consistent.
542    void VerifySchedule(bool isBottomUp);
543#endif
544
545  protected:
546    /// Run - perform scheduling.
547    ///
548    void Run(MachineBasicBlock *bb, MachineBasicBlock::iterator insertPos);
549
550    /// BuildSchedGraph - Build SUnits and set up their Preds and Succs
551    /// to form the scheduling dependency graph.
552    ///
553    virtual void BuildSchedGraph(AliasAnalysis *AA) = 0;
554
555    /// ComputeLatency - Compute node latency.
556    ///
557    virtual void ComputeLatency(SUnit *SU) = 0;
558
559    /// ComputeOperandLatency - Override dependence edge latency using
560    /// operand use/def information
561    ///
562    virtual void ComputeOperandLatency(SUnit *, SUnit *,
563                                       SDep&) const { }
564
565    /// Schedule - Order nodes according to selected style, filling
566    /// in the Sequence member.
567    ///
568    virtual void Schedule() = 0;
569
570    /// ForceUnitLatencies - Return true if all scheduling edges should be given
571    /// a latency value of one.  The default is to return false; schedulers may
572    /// override this as needed.
573    virtual bool ForceUnitLatencies() const { return false; }
574
575    /// EmitNoop - Emit a noop instruction.
576    ///
577    void EmitNoop();
578
579    void EmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap);
580
581  private:
582    // Return the MCInstrDesc of this SDNode or NULL.
583    const MCInstrDesc *getNodeDesc(const SDNode *Node) const;
584  };
585
586  class SUnitIterator : public std::iterator<std::forward_iterator_tag,
587                                             SUnit, ptrdiff_t> {
588    SUnit *Node;
589    unsigned Operand;
590
591    SUnitIterator(SUnit *N, unsigned Op) : Node(N), Operand(Op) {}
592  public:
593    bool operator==(const SUnitIterator& x) const {
594      return Operand == x.Operand;
595    }
596    bool operator!=(const SUnitIterator& x) const { return !operator==(x); }
597
598    const SUnitIterator &operator=(const SUnitIterator &I) {
599      assert(I.Node==Node && "Cannot assign iterators to two different nodes!");
600      Operand = I.Operand;
601      return *this;
602    }
603
604    pointer operator*() const {
605      return Node->Preds[Operand].getSUnit();
606    }
607    pointer operator->() const { return operator*(); }
608
609    SUnitIterator& operator++() {                // Preincrement
610      ++Operand;
611      return *this;
612    }
613    SUnitIterator operator++(int) { // Postincrement
614      SUnitIterator tmp = *this; ++*this; return tmp;
615    }
616
617    static SUnitIterator begin(SUnit *N) { return SUnitIterator(N, 0); }
618    static SUnitIterator end  (SUnit *N) {
619      return SUnitIterator(N, (unsigned)N->Preds.size());
620    }
621
622    unsigned getOperand() const { return Operand; }
623    const SUnit *getNode() const { return Node; }
624    /// isCtrlDep - Test if this is not an SDep::Data dependence.
625    bool isCtrlDep() const {
626      return getSDep().isCtrl();
627    }
628    bool isArtificialDep() const {
629      return getSDep().isArtificial();
630    }
631    const SDep &getSDep() const {
632      return Node->Preds[Operand];
633    }
634  };
635
636  template <> struct GraphTraits<SUnit*> {
637    typedef SUnit NodeType;
638    typedef SUnitIterator ChildIteratorType;
639    static inline NodeType *getEntryNode(SUnit *N) { return N; }
640    static inline ChildIteratorType child_begin(NodeType *N) {
641      return SUnitIterator::begin(N);
642    }
643    static inline ChildIteratorType child_end(NodeType *N) {
644      return SUnitIterator::end(N);
645    }
646  };
647
648  template <> struct GraphTraits<ScheduleDAG*> : public GraphTraits<SUnit*> {
649    typedef std::vector<SUnit>::iterator nodes_iterator;
650    static nodes_iterator nodes_begin(ScheduleDAG *G) {
651      return G->SUnits.begin();
652    }
653    static nodes_iterator nodes_end(ScheduleDAG *G) {
654      return G->SUnits.end();
655    }
656  };
657
658  /// ScheduleDAGTopologicalSort is a class that computes a topological
659  /// ordering for SUnits and provides methods for dynamically updating
660  /// the ordering as new edges are added.
661  ///
662  /// This allows a very fast implementation of IsReachable, for example.
663  ///
664  class ScheduleDAGTopologicalSort {
665    /// SUnits - A reference to the ScheduleDAG's SUnits.
666    std::vector<SUnit> &SUnits;
667
668    /// Index2Node - Maps topological index to the node number.
669    std::vector<int> Index2Node;
670    /// Node2Index - Maps the node number to its topological index.
671    std::vector<int> Node2Index;
672    /// Visited - a set of nodes visited during a DFS traversal.
673    BitVector Visited;
674
675    /// DFS - make a DFS traversal and mark all nodes affected by the
676    /// edge insertion. These nodes will later get new topological indexes
677    /// by means of the Shift method.
678    void DFS(const SUnit *SU, int UpperBound, bool& HasLoop);
679
680    /// Shift - reassign topological indexes for the nodes in the DAG
681    /// to preserve the topological ordering.
682    void Shift(BitVector& Visited, int LowerBound, int UpperBound);
683
684    /// Allocate - assign the topological index to the node n.
685    void Allocate(int n, int index);
686
687  public:
688    explicit ScheduleDAGTopologicalSort(std::vector<SUnit> &SUnits);
689
690    /// InitDAGTopologicalSorting - create the initial topological
691    /// ordering from the DAG to be scheduled.
692    void InitDAGTopologicalSorting();
693
694    /// IsReachable - Checks if SU is reachable from TargetSU.
695    bool IsReachable(const SUnit *SU, const SUnit *TargetSU);
696
697    /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU
698    /// will create a cycle.
699    bool WillCreateCycle(SUnit *SU, SUnit *TargetSU);
700
701    /// AddPred - Updates the topological ordering to accommodate an edge
702    /// to be added from SUnit X to SUnit Y.
703    void AddPred(SUnit *Y, SUnit *X);
704
705    /// RemovePred - Updates the topological ordering to accommodate an
706    /// an edge to be removed from the specified node N from the predecessors
707    /// of the current node M.
708    void RemovePred(SUnit *M, SUnit *N);
709
710    typedef std::vector<int>::iterator iterator;
711    typedef std::vector<int>::const_iterator const_iterator;
712    iterator begin() { return Index2Node.begin(); }
713    const_iterator begin() const { return Index2Node.begin(); }
714    iterator end() { return Index2Node.end(); }
715    const_iterator end() const { return Index2Node.end(); }
716
717    typedef std::vector<int>::reverse_iterator reverse_iterator;
718    typedef std::vector<int>::const_reverse_iterator const_reverse_iterator;
719    reverse_iterator rbegin() { return Index2Node.rbegin(); }
720    const_reverse_iterator rbegin() const { return Index2Node.rbegin(); }
721    reverse_iterator rend() { return Index2Node.rend(); }
722    const_reverse_iterator rend() const { return Index2Node.rend(); }
723  };
724}
725
726#endif
727