LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

LiveInterval.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the LiveRange and LiveInterval classes.  Given some
00011 // numbering of each the machine instructions an interval [i, j) is said to be a
00012 // live interval for register v if there is no instruction with number j' > j
00013 // such that v is live at j' and there is no instruction with number i' < i such
00014 // that v is live at i'. In this implementation intervals can have holes,
00015 // i.e. an interval might look like [1,20), [50,65), [1000,1001).  Each
00016 // individual range is represented as an instance of LiveRange, and the whole
00017 // interval is represented as an instance of LiveInterval.
00018 //
00019 //===----------------------------------------------------------------------===//
00020 
00021 #ifndef LLVM_CODEGEN_LIVEINTERVAL_H
00022 #define LLVM_CODEGEN_LIVEINTERVAL_H
00023 
00024 #include <iosfwd>
00025 #include <vector>
00026 #include <cassert>
00027 
00028 namespace llvm {
00029   /// LiveRange structure - This represents a simple register range in the
00030   /// program, with an inclusive start point and an exclusive end point.
00031   /// These ranges are rendered as [start,end).
00032   struct LiveRange {
00033     unsigned start;  // Start point of the interval (inclusive)
00034     unsigned end;    // End point of the interval (exclusive)
00035     unsigned ValId;  // identifier for the value contained in this interval.
00036 
00037     LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) {
00038       assert(S < E && "Cannot create empty or backwards range");
00039     }
00040 
00041     /// contains - Return true if the index is covered by this range.
00042     ///
00043     bool contains(unsigned I) const {
00044       return start <= I && I < end;
00045     }
00046 
00047     bool operator<(const LiveRange &LR) const {
00048       return start < LR.start || (start == LR.start && end < LR.end);
00049     }
00050     bool operator==(const LiveRange &LR) const {
00051       return start == LR.start && end == LR.end;
00052     }
00053 
00054     void dump() const;
00055 
00056   private:
00057     LiveRange(); // DO NOT IMPLEMENT
00058   };
00059   std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
00060 
00061   inline bool operator<(unsigned V, const LiveRange &LR) {
00062     return V < LR.start;
00063   }
00064 
00065 
00066   /// LiveInterval - This class represents some number of live ranges for a
00067   /// register or value.  This class also contains a bit of register allocator
00068   /// state.
00069   struct LiveInterval {
00070     typedef std::vector<LiveRange> Ranges;
00071     unsigned reg;        // the register of this interval
00072     float weight;        // weight of this interval
00073     Ranges ranges;       // the ranges in which this register is live
00074 
00075     LiveInterval(unsigned Reg, float Weight)
00076       : reg(Reg), weight(Weight), NumValues(0) {
00077     }
00078 
00079     typedef Ranges::iterator iterator;
00080     iterator begin() { return ranges.begin(); }
00081     iterator end()   { return ranges.end(); }
00082 
00083     typedef Ranges::const_iterator const_iterator;
00084     const_iterator begin() const { return ranges.begin(); }
00085     const_iterator end() const  { return ranges.end(); }
00086 
00087 
00088     /// advanceTo - Advance the specified iterator to point to the LiveRange
00089     /// containing the specified position, or end() if the position is past the
00090     /// end of the interval.  If no LiveRange contains this position, but the
00091     /// position is in a hole, this method returns an iterator pointing the the
00092     /// LiveRange immediately after the hole.
00093     iterator advanceTo(iterator I, unsigned Pos) {
00094       if (Pos >= endNumber())
00095         return end();
00096       while (I->end <= Pos) ++I;
00097       return I;
00098     }
00099 
00100     void swap(LiveInterval& other) {
00101       std::swap(reg, other.reg);
00102       std::swap(weight, other.weight);
00103       ranges.swap(other.ranges);
00104       std::swap(NumValues, other.NumValues);
00105     }
00106 
00107     bool containsOneValue() const { return NumValues == 1; }
00108 
00109     unsigned getNextValue() {
00110       return NumValues++;
00111     }
00112 
00113     bool empty() const { return ranges.empty(); }
00114 
00115     /// beginNumber - Return the lowest numbered slot covered by interval.
00116     unsigned beginNumber() const {
00117       assert(!empty() && "empty interval for register");
00118       return ranges.front().start;
00119     }
00120 
00121     /// endNumber - return the maximum point of the interval of the whole,
00122     /// exclusive.
00123     unsigned endNumber() const {
00124       assert(!empty() && "empty interval for register");
00125       return ranges.back().end;
00126     }
00127 
00128     bool expiredAt(unsigned index) const {
00129       return index >= endNumber();
00130     }
00131 
00132     bool liveAt(unsigned index) const;
00133 
00134     /// getLiveRangeContaining - Return the live range that contains the
00135     /// specified index, or null if there is none.
00136     const LiveRange *getLiveRangeContaining(unsigned Idx) const;
00137 
00138 
00139     /// joinable - Two intervals are joinable if the either don't overlap at all
00140     /// or if the destination of the copy is a single assignment value, and it
00141     /// only overlaps with one value in the source interval.
00142     bool joinable(const LiveInterval& other, unsigned CopyIdx) const;
00143 
00144 
00145     /// overlaps - Return true if the intersection of the two live intervals is
00146     /// not empty.
00147     bool overlaps(const LiveInterval& other) const {
00148       return overlapsFrom(other, other.begin());
00149     }
00150 
00151     /// overlapsFrom - Return true if the intersection of the two live intervals
00152     /// is not empty.  The specified iterator is a hint that we can begin
00153     /// scanning the Other interval starting at I.
00154     bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
00155 
00156     /// addRange - Add the specified LiveRange to this interval, merging
00157     /// intervals as appropriate.  This returns an iterator to the inserted live
00158     /// range (which may have grown since it was inserted.
00159     void addRange(LiveRange LR) {
00160       addRangeFrom(LR, ranges.begin());
00161     }
00162 
00163     /// join - Join two live intervals (this, and other) together.  This
00164     /// operation is the result of a copy instruction in the source program,
00165     /// that occurs at index 'CopyIdx' that copies from 'other' to 'this'.  This
00166     /// destroys 'other'.
00167     void join(LiveInterval& other, unsigned CopyIdx);
00168 
00169 
00170     /// removeRange - Remove the specified range from this interval.  Note that
00171     /// the range must already be in this interval in its entirety.
00172     void removeRange(unsigned Start, unsigned End);
00173 
00174     bool operator<(const LiveInterval& other) const {
00175       return beginNumber() < other.beginNumber();
00176     }
00177 
00178     void dump() const;
00179 
00180   private:
00181     unsigned NumValues;  // the number of distinct values in this interval.
00182     Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
00183     void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
00184     Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
00185     LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
00186   };
00187 
00188   std::ostream& operator<<(std::ostream& os, const LiveInterval& li);
00189 }
00190 
00191 #endif