Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LiveRange and LiveInterval classes. Given some |
| 11 | // numbering of each the machine instructions an interval [i, j) is said to be a |
| 12 | // live interval for register v if there is no instruction with number j' > j |
| 13 | // such that v is live at j' abd there is no instruction with number i' < i such |
| 14 | // that v is live at i'. In this implementation intervals can have holes, |
| 15 | // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each |
| 16 | // individual range is represented as an instance of LiveRange, and the whole |
| 17 | // interval is represented as an instance of LiveInterval. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #ifndef LLVM_CODEGEN_LIVEINTERVAL_H |
| 22 | #define LLVM_CODEGEN_LIVEINTERVAL_H |
| 23 | |
| 24 | #include <iosfwd> |
| 25 | #include <vector> |
| 26 | #include <cassert> |
| 27 | |
| 28 | namespace llvm { |
| 29 | /// LiveRange structure - This represents a simple register range in the |
| 30 | /// program, with an inclusive start point and an exclusive end point. |
| 31 | /// These ranges are rendered as [start,end). |
| 32 | struct LiveRange { |
| 33 | unsigned start; // Start point of the interval (inclusive) |
| 34 | unsigned end; // End point of the interval (exclusive) |
| 35 | LiveRange(unsigned S, unsigned E) : start(S), end(E) { |
| 36 | assert(S < E && "Cannot create empty or backwards range"); |
| 37 | } |
| 38 | |
Chris Lattner | e6ad392 | 2004-07-23 18:39:12 +0000 | [diff] [blame] | 39 | /// contains - Return true if the index is covered by this range. |
| 40 | /// |
| 41 | bool contains(unsigned I) const { |
| 42 | return start <= I && I < end; |
| 43 | } |
| 44 | |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 45 | bool operator<(const LiveRange &LR) const { |
| 46 | return start < LR.start || (start == LR.start && end < LR.end); |
| 47 | } |
| 48 | bool operator==(const LiveRange &LR) const { |
| 49 | return start == LR.start && end == LR.end; |
| 50 | } |
Chris Lattner | e6ad392 | 2004-07-23 18:39:12 +0000 | [diff] [blame] | 51 | private: |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 52 | LiveRange(); // DO NOT IMPLEMENT |
| 53 | }; |
| 54 | std::ostream& operator<<(std::ostream& os, const LiveRange &LR); |
| 55 | |
Chris Lattner | ebd7e6c | 2004-07-23 18:13:24 +0000 | [diff] [blame] | 56 | inline bool operator<(unsigned V, const LiveRange &LR) { |
| 57 | return V < LR.start; |
| 58 | } |
| 59 | |
| 60 | |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 61 | /// LiveInterval - This class represents some number of live ranges for a |
| 62 | /// register or value. This class also contains a bit of register allocator |
| 63 | /// state. |
| 64 | struct LiveInterval { |
| 65 | typedef std::vector<LiveRange> Ranges; |
| 66 | unsigned reg; // the register of this interval |
| 67 | float weight; // weight of this interval |
| 68 | Ranges ranges; // the ranges in which this register is live |
| 69 | bool isDefinedOnce; // True if this interval contains one value. |
| 70 | |
| 71 | LiveInterval(unsigned Reg, float Weight) |
| 72 | : reg(Reg), weight(Weight), isDefinedOnce(false) { |
| 73 | } |
| 74 | |
| 75 | bool containsOneValue() const { return isDefinedOnce; } |
| 76 | |
| 77 | bool empty() const { return ranges.empty(); } |
| 78 | |
| 79 | /// start - Return the lowest numbered slot covered by interval. |
| 80 | unsigned start() const { |
| 81 | assert(!empty() && "empty interval for register"); |
| 82 | return ranges.front().start; |
| 83 | } |
| 84 | |
| 85 | /// end - return the maximum point of the interval of the whole, |
| 86 | /// exclusive. |
| 87 | unsigned end() const { |
| 88 | assert(!empty() && "empty interval for register"); |
| 89 | return ranges.back().end; |
| 90 | } |
| 91 | |
| 92 | bool expiredAt(unsigned index) const { |
| 93 | return end() <= (index + 1); |
| 94 | } |
| 95 | |
| 96 | bool liveAt(unsigned index) const; |
| 97 | |
| 98 | bool overlaps(const LiveInterval& other) const; |
| 99 | |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame^] | 100 | /// addRange - Add the specified LiveRange to this interval, merging |
| 101 | /// intervals as appropriate. This returns an iterator to the inserted live |
| 102 | /// range (which may have grown since it was inserted. |
| 103 | void addRange(LiveRange LR) { |
| 104 | addRangeFrom(LR, ranges.begin()); |
| 105 | } |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 106 | |
| 107 | void join(const LiveInterval& other); |
| 108 | |
| 109 | bool operator<(const LiveInterval& other) const { |
| 110 | return start() < other.start(); |
| 111 | } |
| 112 | |
| 113 | bool operator==(const LiveInterval& other) const { |
| 114 | return reg == other.reg; |
| 115 | } |
| 116 | |
| 117 | private: |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame^] | 118 | Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From); |
| 119 | void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd); |
| 120 | Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | std::ostream& operator<<(std::ostream& os, const LiveInterval& li); |
| 124 | } |
| 125 | |
| 126 | #endif |