Reverting r118604. Windows build broke.
[oota-llvm.git] / lib / CodeGen / LiveIntervalUnion.h
1 //===-- LiveIntervalUnion.h - Live interval union data struct --*- 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 // LiveIntervalUnion is a union of live segments across multiple live virtual
11 // registers. This may be used during coalescing to represent a congruence
12 // class, or during register allocation to model liveness of a physical
13 // register.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_LIVEINTERVALUNION
18 #define LLVM_CODEGEN_LIVEINTERVALUNION
19
20 #include "llvm/CodeGen/LiveInterval.h"
21 #include <vector>
22 #include <set>
23
24 namespace llvm {
25
26 /// A LiveSegment is a copy of a LiveRange object used within
27 /// LiveIntervalUnion. LiveSegment additionally contains a pointer to its
28 /// original live virtual register (LiveInterval). This allows quick lookup of
29 /// the live virtual register as we iterate over live segments in a union. Note
30 /// that LiveRange is misnamed and actually represents only a single contiguous
31 /// interval within a virtual register's liveness. To limit confusion, in this
32 /// file we refer it as a live segment.
33 ///
34 /// Note: This currently represents a half-open interval [start,end).
35 /// If LiveRange is modified to represent a closed interval, so should this.
36 struct LiveSegment {
37   SlotIndex start;
38   SlotIndex end;
39   LiveInterval *liveVirtReg;
40
41   LiveSegment(SlotIndex s, SlotIndex e, LiveInterval *lvr)
42     : start(s), end(e), liveVirtReg(lvr) {}
43
44   bool operator==(const LiveSegment &ls) const {
45     return start == ls.start && end == ls.end && liveVirtReg == ls.liveVirtReg;
46   }
47
48   bool operator!=(const LiveSegment &ls) const {
49     return !operator==(ls);
50   }
51
52   // Order segments by starting point only--we expect them to be disjoint.
53   bool operator<(const LiveSegment &ls) const { return start < ls.start; }
54 };
55
56 inline bool operator<(SlotIndex V, const LiveSegment &ls) {
57   return V < ls.start;
58 }
59
60 inline bool operator<(const LiveSegment &ls, SlotIndex V) {
61   return ls.start < V;
62 }
63
64 /// Compare a live virtual register segment to a LiveIntervalUnion segment.
65 inline bool overlap(const LiveRange &lvrSeg, const LiveSegment &liuSeg) {
66   return lvrSeg.start < liuSeg.end && liuSeg.start < lvrSeg.end;
67 }
68
69 /// Union of live intervals that are strong candidates for coalescing into a
70 /// single register (either physical or virtual depending on the context).  We
71 /// expect the constituent live intervals to be disjoint, although we may
72 /// eventually make exceptions to handle value-based interference.
73 class LiveIntervalUnion {
74   // A set of live virtual register segments that supports fast insertion,
75   // intersection, and removal. 
76   //
77   // FIXME: std::set is a placeholder until we decide how to
78   // efficiently represent it. Probably need to roll our own B-tree.
79   typedef std::set<LiveSegment> LiveSegments;
80
81   // A set of live virtual registers. Elements have type LiveInterval, where
82   // each element represents the liveness of a single live virtual register.
83   // This is traditionally known as a live range, but we refer is as a live
84   // virtual register to avoid confusing it with the misnamed LiveRange
85   // class.
86   typedef std::vector<LiveInterval*> LiveVirtRegs;
87
88 public:
89   // SegmentIter can advance to the next segment ordered by starting position
90   // which may belong to a different live virtual register. We also must be able
91   // to reach the current segment's containing virtual register.
92   typedef LiveSegments::iterator SegmentIter;
93
94   class InterferenceResult;
95   class Query;
96
97 private:
98   unsigned repReg_;        // representative register number
99   LiveSegments segments_;  // union of virtual reg segements
100
101 public:
102   // default ctor avoids placement new
103   LiveIntervalUnion() : repReg_(0) {}
104
105   // Initialize the union by associating it with a representative register
106   // number.
107   void init(unsigned repReg) { repReg_ = repReg; }
108
109   // Iterate over all segments in the union of live virtual registers ordered
110   // by their starting position.
111   SegmentIter begin() { return segments_.begin(); }
112   SegmentIter end() { return segments_.end(); }
113
114   // Return an iterator to the first segment after or including begin that
115   // intersects with lvrSeg.
116   SegmentIter upperBound(SegmentIter begin, const LiveSegment &seg);
117
118   // Add a live virtual register to this union and merge its segments.
119   // Holds a nonconst reference to the LVR for later maniplution.
120   void unify(LiveInterval &lvr);
121
122   // Remove a live virtual register's segments from this union.
123   void extract(const LiveInterval &lvr);
124
125   /// Cache a single interference test result in the form of two intersecting
126   /// segments. This allows efficiently iterating over the interferences. The
127   /// iteration logic is handled by LiveIntervalUnion::Query which may
128   /// filter interferences depending on the type of query.
129   class InterferenceResult {
130     friend class Query;
131
132     LiveInterval::iterator lvrSegI_; // current position in _lvr
133     SegmentIter liuSegI_;            // current position in _liu
134     
135     // Internal ctor.
136     InterferenceResult(LiveInterval::iterator lvrSegI, SegmentIter liuSegI)
137       : lvrSegI_(lvrSegI), liuSegI_(liuSegI) {}
138
139   public:
140     // Public default ctor.
141     InterferenceResult(): lvrSegI_(), liuSegI_() {}
142
143     // Note: this interface provides raw access to the iterators because the
144     // result has no way to tell if it's valid to dereference them.
145
146     // Access the lvr segment. 
147     const LiveInterval::iterator &lvrSegPos() const { return lvrSegI_; }
148
149     // Access the liu segment.
150     const SegmentIter &liuSegPos() const { return liuSegI_; }
151
152     bool operator==(const InterferenceResult &ir) const {
153       return lvrSegI_ == ir.lvrSegI_ && liuSegI_ == ir.liuSegI_;
154     }
155     bool operator!=(const InterferenceResult &ir) const {
156       return !operator==(ir);
157     }
158   };
159
160   /// Query interferences between a single live virtual register and a live
161   /// interval union.
162   class Query {
163     LiveIntervalUnion *liu_;
164     LiveInterval *lvr_;
165     InterferenceResult firstInterference_;
166     // TBD: interfering vregs
167
168   public:
169     Query(): liu_(), lvr_() {}
170
171     Query(LiveInterval *lvr, LiveIntervalUnion *liu): liu_(liu), lvr_(lvr) {}
172
173     void clear() {
174       liu_ = NULL;
175       lvr_ = NULL;
176       firstInterference_ = InterferenceResult();
177     }
178     
179     void init(LiveInterval *lvr, LiveIntervalUnion *liu) {
180       if (lvr_ == lvr) {
181         // We currently allow query objects to be reused acrossed live virtual
182         // registers, but always for the same live interval union.
183         assert(liu_ == liu && "inconsistent initialization");
184         // Retain cached results, e.g. firstInterference.
185         return;
186       }
187       liu_ = liu;
188       lvr_ = lvr;
189       // Clear cached results.
190       firstInterference_ = InterferenceResult();
191     }
192
193     LiveInterval &lvr() const { assert(lvr_ && "uninitialized"); return *lvr_; }
194
195     bool isInterference(const InterferenceResult &ir) const {
196       if (ir.lvrSegI_ != lvr_->end()) {
197         assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) &&
198                "invalid segment iterators");
199         return true;
200       }
201       return false;
202     }
203
204     // Does this live virtual register interfere with the union.
205     bool checkInterference() { return isInterference(firstInterference()); }
206
207     // Get the first pair of interfering segments, or a noninterfering result.
208     // This initializes the firstInterference_ cache.
209     InterferenceResult firstInterference();
210
211     // Treat the result as an iterator and advance to the next interfering pair
212     // of segments. Visiting each unique interfering pairs means that the same
213     // lvr or liu segment may be visited multiple times.
214     bool nextInterference(InterferenceResult &ir) const;
215         
216     // TBD: bool collectInterferingVirtRegs(unsigned maxInterference)
217
218   private:
219     // Private interface for queries
220     void findIntersection(InterferenceResult &ir) const;
221   };
222 };
223
224 } // end namespace llvm
225
226 #endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)