LLVM API Documentation
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 bool isCloned : 1; // True if this node has been cloned. 00254 private: 00255 bool isDepthCurrent : 1; // True if Depth is current. 00256 bool isHeightCurrent : 1; // True if Height is current. 00257 unsigned Depth; // Node depth. 00258 unsigned Height; // Node height. 00259 public: 00260 const TargetRegisterClass *CopyDstRC; // Is a special copy node if not null. 00261 const TargetRegisterClass *CopySrcRC; 00262 00263 /// SUnit - Construct an SUnit for pre-regalloc scheduling to represent 00264 /// an SDNode and any nodes flagged to it. 00265 SUnit(SDNode *node, unsigned nodenum) 00266 : Node(node), Instr(0), OrigNode(0), NodeNum(nodenum), NodeQueueId(0), 00267 Latency(0), NumPreds(0), NumSuccs(0), NumPredsLeft(0), NumSuccsLeft(0), 00268 isTwoAddress(false), isCommutable(false), hasPhysRegDefs(false), 00269 isPending(false), isAvailable(false), isScheduled(false), 00270 isScheduleHigh(false), isCloned(false), 00271 isDepthCurrent(false), isHeightCurrent(false), Depth(0), Height(0), 00272 CopyDstRC(NULL), CopySrcRC(NULL) {} 00273 00274 /// SUnit - Construct an SUnit for post-regalloc scheduling to represent 00275 /// a MachineInstr. 00276 SUnit(MachineInstr *instr, unsigned nodenum) 00277 : Node(0), Instr(instr), OrigNode(0), NodeNum(nodenum), NodeQueueId(0), 00278 Latency(0), NumPreds(0), NumSuccs(0), NumPredsLeft(0), NumSuccsLeft(0), 00279 isTwoAddress(false), isCommutable(false), hasPhysRegDefs(false), 00280 isPending(false), isAvailable(false), isScheduled(false), 00281 isScheduleHigh(false), isCloned(false), 00282 isDepthCurrent(false), isHeightCurrent(false), Depth(0), Height(0), 00283 CopyDstRC(NULL), CopySrcRC(NULL) {} 00284 00285 /// setNode - Assign the representative SDNode for this SUnit. 00286 /// This may be used during pre-regalloc scheduling. 00287 void setNode(SDNode *N) { 00288 assert(!Instr && "Setting SDNode of SUnit with MachineInstr!"); 00289 Node = N; 00290 } 00291 00292 /// getNode - Return the representative SDNode for this SUnit. 00293 /// This may be used during pre-regalloc scheduling. 00294 SDNode *getNode() const { 00295 assert(!Instr && "Reading SDNode of SUnit with MachineInstr!"); 00296 return Node; 00297 } 00298 00299 /// setInstr - Assign the instruction for the SUnit. 00300 /// This may be used during post-regalloc scheduling. 00301 void setInstr(MachineInstr *MI) { 00302 assert(!Node && "Setting MachineInstr of SUnit with SDNode!"); 00303 Instr = MI; 00304 } 00305 00306 /// getInstr - Return the representative MachineInstr for this SUnit. 00307 /// This may be used during post-regalloc scheduling. 00308 MachineInstr *getInstr() const { 00309 assert(!Node && "Reading MachineInstr of SUnit with SDNode!"); 00310 return Instr; 00311 } 00312 00313 /// addPred - This adds the specified edge as a pred of the current node if 00314 /// not already. It also adds the current node as a successor of the 00315 /// specified node. 00316 void addPred(const SDep &D); 00317 00318 /// removePred - This removes the specified edge as a pred of the current 00319 /// node if it exists. It also removes the current node as a successor of 00320 /// the specified node. 00321 void removePred(const SDep &D); 00322 00323 /// getDepth - Return the depth of this node, which is the length of the 00324 /// maximum path up to any node with has no predecessors. 00325 unsigned getDepth() const { 00326 if (!isDepthCurrent) const_cast<SUnit *>(this)->ComputeDepth(); 00327 return Depth; 00328 } 00329 00330 /// getHeight - Return the height of this node, which is the length of the 00331 /// maximum path down to any node with has no successors. 00332 unsigned getHeight() const { 00333 if (!isHeightCurrent) const_cast<SUnit *>(this)->ComputeHeight(); 00334 return Height; 00335 } 00336 00337 /// setDepthToAtLeast - If NewDepth is greater than this node's depth 00338 /// value, set it to be the new depth value. This also recursively 00339 /// marks successor nodes dirty. 00340 void setDepthToAtLeast(unsigned NewDepth); 00341 00342 /// setDepthToAtLeast - If NewDepth is greater than this node's depth 00343 /// value, set it to be the new height value. This also recursively 00344 /// marks predecessor nodes dirty. 00345 void setHeightToAtLeast(unsigned NewHeight); 00346 00347 /// setDepthDirty - Set a flag in this node to indicate that its 00348 /// stored Depth value will require recomputation the next time 00349 /// getDepth() is called. 00350 void setDepthDirty(); 00351 00352 /// setHeightDirty - Set a flag in this node to indicate that its 00353 /// stored Height value will require recomputation the next time 00354 /// getHeight() is called. 00355 void setHeightDirty(); 00356 00357 /// isPred - Test if node N is a predecessor of this node. 00358 bool isPred(SUnit *N) { 00359 for (unsigned i = 0, e = (unsigned)Preds.size(); i != e; ++i) 00360 if (Preds[i].getSUnit() == N) 00361 return true; 00362 return false; 00363 } 00364 00365 /// isSucc - Test if node N is a successor of this node. 00366 bool isSucc(SUnit *N) { 00367 for (unsigned i = 0, e = (unsigned)Succs.size(); i != e; ++i) 00368 if (Succs[i].getSUnit() == N) 00369 return true; 00370 return false; 00371 } 00372 00373 void dump(const ScheduleDAG *G) const; 00374 void dumpAll(const ScheduleDAG *G) const; 00375 void print(raw_ostream &O, const ScheduleDAG *G) const; 00376 00377 private: 00378 void ComputeDepth(); 00379 void ComputeHeight(); 00380 }; 00381 00382 //===--------------------------------------------------------------------===// 00383 /// SchedulingPriorityQueue - This interface is used to plug different 00384 /// priorities computation algorithms into the list scheduler. It implements 00385 /// the interface of a standard priority queue, where nodes are inserted in 00386 /// arbitrary order and returned in priority order. The computation of the 00387 /// priority and the representation of the queue are totally up to the 00388 /// implementation to decide. 00389 /// 00390 class SchedulingPriorityQueue { 00391 public: 00392 virtual ~SchedulingPriorityQueue() {} 00393 00394 virtual void initNodes(std::vector<SUnit> &SUnits) = 0; 00395 virtual void addNode(const SUnit *SU) = 0; 00396 virtual void updateNode(const SUnit *SU) = 0; 00397 virtual void releaseState() = 0; 00398 00399 virtual unsigned size() const = 0; 00400 virtual bool empty() const = 0; 00401 virtual void push(SUnit *U) = 0; 00402 00403 virtual void push_all(const std::vector<SUnit *> &Nodes) = 0; 00404 virtual SUnit *pop() = 0; 00405 00406 virtual void remove(SUnit *SU) = 0; 00407 00408 /// ScheduledNode - As each node is scheduled, this method is invoked. This 00409 /// allows the priority function to adjust the priority of related 00410 /// unscheduled nodes, for example. 00411 /// 00412 virtual void ScheduledNode(SUnit *) {} 00413 00414 virtual void UnscheduledNode(SUnit *) {} 00415 }; 00416 00417 class ScheduleDAG { 00418 public: 00419 SelectionDAG *DAG; // DAG of the current basic block 00420 MachineBasicBlock *BB; // Current basic block 00421 MachineBasicBlock::iterator Begin; // The beginning of the range to be scheduled. 00422 MachineBasicBlock::iterator End; // The end of the range to be scheduled. 00423 const TargetMachine &TM; // Target processor 00424 const TargetInstrInfo *TII; // Target instruction information 00425 const TargetRegisterInfo *TRI; // Target processor register info 00426 TargetLowering *TLI; // Target lowering info 00427 MachineFunction &MF; // Machine function 00428 MachineRegisterInfo &MRI; // Virtual/real register map 00429 MachineConstantPool *ConstPool; // Target constant pool 00430 std::vector<SUnit*> Sequence; // The schedule. Null SUnit*'s 00431 // represent noop instructions. 00432 std::vector<SUnit> SUnits; // The scheduling units. 00433 00434 explicit ScheduleDAG(MachineFunction &mf); 00435 00436 virtual ~ScheduleDAG(); 00437 00438 /// viewGraph - Pop up a GraphViz/gv window with the ScheduleDAG rendered 00439 /// using 'dot'. 00440 /// 00441 void viewGraph(); 00442 00443 /// Run - perform scheduling. 00444 /// 00445 void Run(SelectionDAG *DAG, MachineBasicBlock *MBB, 00446 MachineBasicBlock::iterator Begin, 00447 MachineBasicBlock::iterator End); 00448 00449 /// BuildSchedGraph - Build SUnits and set up their Preds and Succs 00450 /// to form the scheduling dependency graph. 00451 /// 00452 virtual void BuildSchedGraph() = 0; 00453 00454 /// ComputeLatency - Compute node latency. 00455 /// 00456 virtual void ComputeLatency(SUnit *SU) = 0; 00457 00458 protected: 00459 /// EmitNoop - Emit a noop instruction. 00460 /// 00461 void EmitNoop(); 00462 00463 public: 00464 virtual MachineBasicBlock *EmitSchedule() = 0; 00465 00466 void dumpSchedule() const; 00467 00468 /// Schedule - Order nodes according to selected style, filling 00469 /// in the Sequence member. 00470 /// 00471 virtual void Schedule() = 0; 00472 00473 virtual void dumpNode(const SUnit *SU) const = 0; 00474 00475 /// getGraphNodeLabel - Return a label for an SUnit node in a visualization 00476 /// of the ScheduleDAG. 00477 virtual std::string getGraphNodeLabel(const SUnit *SU) const = 0; 00478 00479 /// addCustomGraphFeatures - Add custom features for a visualization of 00480 /// the ScheduleDAG. 00481 virtual void addCustomGraphFeatures(GraphWriter<ScheduleDAG*> &) const {} 00482 00483 #ifndef NDEBUG 00484 /// VerifySchedule - Verify that all SUnits were scheduled and that 00485 /// their state is consistent. 00486 void VerifySchedule(bool isBottomUp); 00487 #endif 00488 00489 protected: 00490 void AddMemOperand(MachineInstr *MI, const MachineMemOperand &MO); 00491 00492 void EmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap); 00493 00494 /// ForceUnitLatencies - Return true if all scheduling edges should be given a 00495 /// latency value of one. The default is to return false; schedulers may 00496 /// override this as needed. 00497 virtual bool ForceUnitLatencies() const { return false; } 00498 00499 private: 00500 /// EmitLiveInCopy - Emit a copy for a live in physical register. If the 00501 /// physical register has only a single copy use, then coalesced the copy 00502 /// if possible. 00503 void EmitLiveInCopy(MachineBasicBlock *MBB, 00504 MachineBasicBlock::iterator &InsertPos, 00505 unsigned VirtReg, unsigned PhysReg, 00506 const TargetRegisterClass *RC, 00507 DenseMap<MachineInstr*, unsigned> &CopyRegMap); 00508 00509 /// EmitLiveInCopies - If this is the first basic block in the function, 00510 /// and if it has live ins that need to be copied into vregs, emit the 00511 /// copies into the top of the block. 00512 void EmitLiveInCopies(MachineBasicBlock *MBB); 00513 }; 00514 00515 class SUnitIterator : public forward_iterator<SUnit, ptrdiff_t> { 00516 SUnit *Node; 00517 unsigned Operand; 00518 00519 SUnitIterator(SUnit *N, unsigned Op) : Node(N), Operand(Op) {} 00520 public: 00521 bool operator==(const SUnitIterator& x) const { 00522 return Operand == x.Operand; 00523 } 00524 bool operator!=(const SUnitIterator& x) const { return !operator==(x); } 00525 00526 const SUnitIterator &operator=(const SUnitIterator &I) { 00527 assert(I.Node == Node && "Cannot assign iterators to two different nodes!"); 00528 Operand = I.Operand; 00529 return *this; 00530 } 00531 00532 pointer operator*() const { 00533 return Node->Preds[Operand].getSUnit(); 00534 } 00535 pointer operator->() const { return operator*(); } 00536 00537 SUnitIterator& operator++() { // Preincrement 00538 ++Operand; 00539 return *this; 00540 } 00541 SUnitIterator operator++(int) { // Postincrement 00542 SUnitIterator tmp = *this; ++*this; return tmp; 00543 } 00544 00545 static SUnitIterator begin(SUnit *N) { return SUnitIterator(N, 0); } 00546 static SUnitIterator end (SUnit *N) { 00547 return SUnitIterator(N, (unsigned)N->Preds.size()); 00548 } 00549 00550 unsigned getOperand() const { return Operand; } 00551 const SUnit *getNode() const { return Node; } 00552 /// isCtrlDep - Test if this is not an SDep::Data dependence. 00553 bool isCtrlDep() const { 00554 return getSDep().isCtrl(); 00555 } 00556 bool isArtificialDep() const { 00557 return getSDep().isArtificial(); 00558 } 00559 const SDep &getSDep() const { 00560 return Node->Preds[Operand]; 00561 } 00562 }; 00563 00564 template <> struct GraphTraits<SUnit*> { 00565 typedef SUnit NodeType; 00566 typedef SUnitIterator ChildIteratorType; 00567 static inline NodeType *getEntryNode(SUnit *N) { return N; } 00568 static inline ChildIteratorType child_begin(NodeType *N) { 00569 return SUnitIterator::begin(N); 00570 } 00571 static inline ChildIteratorType child_end(NodeType *N) { 00572 return SUnitIterator::end(N); 00573 } 00574 }; 00575 00576 template <> struct GraphTraits<ScheduleDAG*> : public GraphTraits<SUnit*> { 00577 typedef std::vector<SUnit>::iterator nodes_iterator; 00578 static nodes_iterator nodes_begin(ScheduleDAG *G) { 00579 return G->SUnits.begin(); 00580 } 00581 static nodes_iterator nodes_end(ScheduleDAG *G) { 00582 return G->SUnits.end(); 00583 } 00584 }; 00585 00586 /// ScheduleDAGTopologicalSort is a class that computes a topological 00587 /// ordering for SUnits and provides methods for dynamically updating 00588 /// the ordering as new edges are added. 00589 /// 00590 /// This allows a very fast implementation of IsReachable, for example. 00591 /// 00592 class ScheduleDAGTopologicalSort { 00593 /// SUnits - A reference to the ScheduleDAG's SUnits. 00594 std::vector<SUnit> &SUnits; 00595 00596 /// Index2Node - Maps topological index to the node number. 00597 std::vector<int> Index2Node; 00598 /// Node2Index - Maps the node number to its topological index. 00599 std::vector<int> Node2Index; 00600 /// Visited - a set of nodes visited during a DFS traversal. 00601 BitVector Visited; 00602 00603 /// DFS - make a DFS traversal and mark all nodes affected by the 00604 /// edge insertion. These nodes will later get new topological indexes 00605 /// by means of the Shift method. 00606 void DFS(const SUnit *SU, int UpperBound, bool& HasLoop); 00607 00608 /// Shift - reassign topological indexes for the nodes in the DAG 00609 /// to preserve the topological ordering. 00610 void Shift(BitVector& Visited, int LowerBound, int UpperBound); 00611 00612 /// Allocate - assign the topological index to the node n. 00613 void Allocate(int n, int index); 00614 00615 public: 00616 explicit ScheduleDAGTopologicalSort(std::vector<SUnit> &SUnits); 00617 00618 /// InitDAGTopologicalSorting - create the initial topological 00619 /// ordering from the DAG to be scheduled. 00620 void InitDAGTopologicalSorting(); 00621 00622 /// IsReachable - Checks if SU is reachable from TargetSU. 00623 bool IsReachable(const SUnit *SU, const SUnit *TargetSU); 00624 00625 /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU 00626 /// will create a cycle. 00627 bool WillCreateCycle(SUnit *SU, SUnit *TargetSU); 00628 00629 /// AddPred - Updates the topological ordering to accomodate an edge 00630 /// to be added from SUnit X to SUnit Y. 00631 void AddPred(SUnit *Y, SUnit *X); 00632 00633 /// RemovePred - Updates the topological ordering to accomodate an 00634 /// an edge to be removed from the specified node N from the predecessors 00635 /// of the current node M. 00636 void RemovePred(SUnit *M, SUnit *N); 00637 00638 typedef std::vector<int>::iterator iterator; 00639 typedef std::vector<int>::const_iterator const_iterator; 00640 iterator begin() { return Index2Node.begin(); } 00641 const_iterator begin() const { return Index2Node.begin(); } 00642 iterator end() { return Index2Node.end(); } 00643 const_iterator end() const { return Index2Node.end(); } 00644 00645 typedef std::vector<int>::reverse_iterator reverse_iterator; 00646 typedef std::vector<int>::const_reverse_iterator const_reverse_iterator; 00647 reverse_iterator rbegin() { return Index2Node.rbegin(); } 00648 const_reverse_iterator rbegin() const { return Index2Node.rbegin(); } 00649 reverse_iterator rend() { return Index2Node.rend(); } 00650 const_reverse_iterator rend() const { return Index2Node.rend(); } 00651 }; 00652 } 00653 00654 #endif