LLVM API Documentation

Interval.h
Go to the documentation of this file.
00001 //===- llvm/Analysis/Interval.h - Interval Class Declaration ----*- 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 contains the declaration of the Interval class, which
00011 // represents a set of CFG nodes and is a portion of an interval partition.
00012 //
00013 // Intervals have some interesting and useful properties, including the
00014 // following:
00015 //    1. The header node of an interval dominates all of the elements of the
00016 //       interval
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_ANALYSIS_INTERVAL_H
00021 #define LLVM_ANALYSIS_INTERVAL_H
00022 
00023 #include "llvm/ADT/GraphTraits.h"
00024 #include <vector>
00025 
00026 namespace llvm {
00027 
00028 class BasicBlock;
00029 class raw_ostream;
00030 
00031 //===----------------------------------------------------------------------===//
00032 //
00033 /// Interval Class - An Interval is a set of nodes defined such that every node
00034 /// in the interval has all of its predecessors in the interval (except for the
00035 /// header)
00036 ///
00037 class Interval {
00038   /// HeaderNode - The header BasicBlock, which dominates all BasicBlocks in this
00039   /// interval.  Also, any loops in this interval must go through the HeaderNode.
00040   ///
00041   BasicBlock *HeaderNode;
00042 public:
00043   typedef std::vector<BasicBlock*>::iterator succ_iterator;
00044   typedef std::vector<BasicBlock*>::iterator pred_iterator;
00045   typedef std::vector<BasicBlock*>::iterator node_iterator;
00046 
00047   inline Interval(BasicBlock *Header) : HeaderNode(Header) {
00048     Nodes.push_back(Header);
00049   }
00050 
00051   inline BasicBlock *getHeaderNode() const { return HeaderNode; }
00052 
00053   /// Nodes - The basic blocks in this interval.
00054   ///
00055   std::vector<BasicBlock*> Nodes;
00056 
00057   /// Successors - List of BasicBlocks that are reachable directly from nodes in
00058   /// this interval, but are not in the interval themselves.
00059   /// These nodes necessarily must be header nodes for other intervals.
00060   ///
00061   std::vector<BasicBlock*> Successors;
00062 
00063   /// Predecessors - List of BasicBlocks that have this Interval's header block
00064   /// as one of their successors.
00065   ///
00066   std::vector<BasicBlock*> Predecessors;
00067 
00068   /// contains - Find out if a basic block is in this interval
00069   inline bool contains(BasicBlock *BB) const {
00070     for (unsigned i = 0; i < Nodes.size(); ++i)
00071       if (Nodes[i] == BB) return true;
00072     return false;
00073     // I don't want the dependency on <algorithm>
00074     //return find(Nodes.begin(), Nodes.end(), BB) != Nodes.end();
00075   }
00076 
00077   /// isSuccessor - find out if a basic block is a successor of this Interval
00078   inline bool isSuccessor(BasicBlock *BB) const {
00079     for (unsigned i = 0; i < Successors.size(); ++i)
00080       if (Successors[i] == BB) return true;
00081     return false;
00082     // I don't want the dependency on <algorithm>
00083     //return find(Successors.begin(), Successors.end(), BB) != Successors.end();
00084   }
00085 
00086   /// Equality operator.  It is only valid to compare two intervals from the
00087   /// same partition, because of this, all we have to check is the header node
00088   /// for equality.
00089   ///
00090   inline bool operator==(const Interval &I) const {
00091     return HeaderNode == I.HeaderNode;
00092   }
00093 
00094   /// isLoop - Find out if there is a back edge in this interval...
00095   bool isLoop() const;
00096 
00097   /// print - Show contents in human readable format...
00098   void print(raw_ostream &O) const;
00099 };
00100 
00101 /// succ_begin/succ_end - define methods so that Intervals may be used
00102 /// just like BasicBlocks can with the succ_* functions, and *::succ_iterator.
00103 ///
00104 inline Interval::succ_iterator succ_begin(Interval *I) {
00105   return I->Successors.begin();
00106 }
00107 inline Interval::succ_iterator succ_end(Interval *I)   {
00108   return I->Successors.end();
00109 }
00110 
00111 /// pred_begin/pred_end - define methods so that Intervals may be used
00112 /// just like BasicBlocks can with the pred_* functions, and *::pred_iterator.
00113 ///
00114 inline Interval::pred_iterator pred_begin(Interval *I) {
00115   return I->Predecessors.begin();
00116 }
00117 inline Interval::pred_iterator pred_end(Interval *I)   {
00118   return I->Predecessors.end();
00119 }
00120 
00121 template <> struct GraphTraits<Interval*> {
00122   typedef Interval NodeType;
00123   typedef Interval::succ_iterator ChildIteratorType;
00124 
00125   static NodeType *getEntryNode(Interval *I) { return I; }
00126 
00127   /// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
00128   static inline ChildIteratorType child_begin(NodeType *N) {
00129     return succ_begin(N);
00130   }
00131   static inline ChildIteratorType child_end(NodeType *N) {
00132     return succ_end(N);
00133   }
00134 };
00135 
00136 template <> struct GraphTraits<Inverse<Interval*> > {
00137   typedef Interval NodeType;
00138   typedef Interval::pred_iterator ChildIteratorType;
00139   static NodeType *getEntryNode(Inverse<Interval *> G) { return G.Graph; }
00140   static inline ChildIteratorType child_begin(NodeType *N) {
00141     return pred_begin(N);
00142   }
00143   static inline ChildIteratorType child_end(NodeType *N) {
00144     return pred_end(N);
00145   }
00146 };
00147 
00148 } // End llvm namespace
00149 
00150 #endif