blob: 0c9a13a606c7f1c7d7304ee47f5a6c9a87369bce [file] [log] [blame]
Andrew Trick14e8d712010-10-22 23:09:15 +00001//===-- 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
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000020#include "llvm/ADT/IntervalMap.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000021#include "llvm/CodeGen/LiveInterval.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000022
23namespace llvm {
24
Andrew Trick071d1c02010-11-09 21:04:34 +000025#ifndef NDEBUG
26// forward declaration
27template <unsigned Element> class SparseBitVector;
Andrew Trick18c57a82010-11-30 23:18:47 +000028typedef SparseBitVector<128> LiveVirtRegBitSet;
Andrew Trick071d1c02010-11-09 21:04:34 +000029#endif
30
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000031/// Abstraction to provide info for the representative register.
32class AbstractRegisterDescription {
33public:
Andrew Trick18c57a82010-11-30 23:18:47 +000034 virtual const char *getName(unsigned Reg) const = 0;
Andrew Trick071d1c02010-11-09 21:04:34 +000035 virtual ~AbstractRegisterDescription() {}
Matt Beaumont-Gaye33daaa2010-11-09 19:56:25 +000036};
Andrew Trick071d1c02010-11-09 21:04:34 +000037
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000038/// Compare a live virtual register segment to a LiveIntervalUnion segment.
39inline bool
40overlap(const LiveRange &VRSeg,
41 const IntervalMap<SlotIndex, LiveInterval*>::const_iterator &LUSeg) {
42 return VRSeg.start < LUSeg.stop() && LUSeg.start() < VRSeg.end;
43}
44
Andrew Trick14e8d712010-10-22 23:09:15 +000045/// Union of live intervals that are strong candidates for coalescing into a
46/// single register (either physical or virtual depending on the context). We
47/// expect the constituent live intervals to be disjoint, although we may
48/// eventually make exceptions to handle value-based interference.
49class LiveIntervalUnion {
50 // A set of live virtual register segments that supports fast insertion,
Andrew Trick18c57a82010-11-30 23:18:47 +000051 // intersection, and removal.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000052 // Mapping SlotIndex intervals to virtual register numbers.
53 typedef IntervalMap<SlotIndex, LiveInterval*> LiveSegments;
Andrew Trick14e8d712010-10-22 23:09:15 +000054
Andrew Trick14e8d712010-10-22 23:09:15 +000055public:
56 // SegmentIter can advance to the next segment ordered by starting position
57 // which may belong to a different live virtual register. We also must be able
58 // to reach the current segment's containing virtual register.
59 typedef LiveSegments::iterator SegmentIter;
60
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000061 // LiveIntervalUnions share an external allocator.
62 typedef LiveSegments::Allocator Allocator;
63
Andrew Trick14e8d712010-10-22 23:09:15 +000064 class InterferenceResult;
65 class Query;
66
67private:
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000068 const unsigned RepReg; // representative register number
69 LiveSegments Segments; // union of virtual reg segments
Andrew Trick14e8d712010-10-22 23:09:15 +000070
71public:
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000072 LiveIntervalUnion(unsigned r, Allocator &a) : RepReg(r), Segments(a) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000073
Andrew Tricke16eecc2010-10-26 18:34:01 +000074 // Iterate over all segments in the union of live virtual registers ordered
75 // by their starting position.
Andrew Trick18c57a82010-11-30 23:18:47 +000076 SegmentIter begin() { return Segments.begin(); }
77 SegmentIter end() { return Segments.end(); }
Jakob Stoklund Olesen1b19dc12010-12-08 01:06:06 +000078 bool empty() { return Segments.empty(); }
Andrew Trick14e8d712010-10-22 23:09:15 +000079
Andrew Tricke16eecc2010-10-26 18:34:01 +000080 // Add a live virtual register to this union and merge its segments.
Andrew Trick18c57a82010-11-30 23:18:47 +000081 void unify(LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +000082
Andrew Tricke141a492010-11-08 18:02:08 +000083 // Remove a live virtual register's segments from this union.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000084 void extract(LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +000085
Andrew Trick18c57a82010-11-30 23:18:47 +000086 void dump(const AbstractRegisterDescription *RegDesc) const;
Andrew Trick071d1c02010-11-09 21:04:34 +000087
Andrew Trick18c57a82010-11-30 23:18:47 +000088 // If tri != NULL, use it to decode RepReg
89 void print(raw_ostream &OS, const AbstractRegisterDescription *RegDesc) const;
90
Andrew Trick071d1c02010-11-09 21:04:34 +000091#ifndef NDEBUG
92 // Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +000093 void verify(LiveVirtRegBitSet& VisitedVRegs);
Andrew Trick071d1c02010-11-09 21:04:34 +000094#endif
95
Andrew Trick14e8d712010-10-22 23:09:15 +000096 /// Cache a single interference test result in the form of two intersecting
97 /// segments. This allows efficiently iterating over the interferences. The
98 /// iteration logic is handled by LiveIntervalUnion::Query which may
99 /// filter interferences depending on the type of query.
100 class InterferenceResult {
101 friend class Query;
102
Andrew Trick18c57a82010-11-30 23:18:47 +0000103 LiveInterval::iterator VirtRegI; // current position in VirtReg
104 SegmentIter LiveUnionI; // current position in LiveUnion
105
Andrew Trick14e8d712010-10-22 23:09:15 +0000106 // Internal ctor.
Andrew Trick18c57a82010-11-30 23:18:47 +0000107 InterferenceResult(LiveInterval::iterator VRegI, SegmentIter UnionI)
108 : VirtRegI(VRegI), LiveUnionI(UnionI) {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000109
110 public:
111 // Public default ctor.
Andrew Trick18c57a82010-11-30 23:18:47 +0000112 InterferenceResult(): VirtRegI(), LiveUnionI() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000113
114 // Note: this interface provides raw access to the iterators because the
115 // result has no way to tell if it's valid to dereference them.
116
Andrew Trick18c57a82010-11-30 23:18:47 +0000117 // Access the VirtReg segment.
118 LiveInterval::iterator virtRegPos() const { return VirtRegI; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000119
Andrew Trick18c57a82010-11-30 23:18:47 +0000120 // Access the LiveUnion segment.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000121 const SegmentIter &liveUnionPos() const { return LiveUnionI; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000122
Andrew Trick18c57a82010-11-30 23:18:47 +0000123 bool operator==(const InterferenceResult &IR) const {
124 return VirtRegI == IR.VirtRegI && LiveUnionI == IR.LiveUnionI;
Andrew Trick14e8d712010-10-22 23:09:15 +0000125 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000126 bool operator!=(const InterferenceResult &IR) const {
127 return !operator==(IR);
Andrew Trick14e8d712010-10-22 23:09:15 +0000128 }
129 };
130
131 /// Query interferences between a single live virtual register and a live
132 /// interval union.
133 class Query {
Andrew Trick18c57a82010-11-30 23:18:47 +0000134 LiveIntervalUnion *LiveUnion;
135 LiveInterval *VirtReg;
136 InterferenceResult FirstInterference;
137 SmallVector<LiveInterval*,4> InterferingVRegs;
138 bool SeenAllInterferences;
139 bool SeenUnspillableVReg;
Andrew Trick14e8d712010-10-22 23:09:15 +0000140
141 public:
Andrew Trick18c57a82010-11-30 23:18:47 +0000142 Query(): LiveUnion(), VirtReg() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000143
Andrew Trick18c57a82010-11-30 23:18:47 +0000144 Query(LiveInterval *VReg, LiveIntervalUnion *LIU):
145 LiveUnion(LIU), VirtReg(VReg), SeenAllInterferences(false),
146 SeenUnspillableVReg(false)
147 {}
Andrew Tricke141a492010-11-08 18:02:08 +0000148
149 void clear() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000150 LiveUnion = NULL;
151 VirtReg = NULL;
152 FirstInterference = InterferenceResult();
153 InterferingVRegs.clear();
154 SeenAllInterferences = false;
155 SeenUnspillableVReg = false;
Andrew Tricke141a492010-11-08 18:02:08 +0000156 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000157
158 void init(LiveInterval *VReg, LiveIntervalUnion *LIU) {
159 if (VirtReg == VReg) {
Andrew Tricke141a492010-11-08 18:02:08 +0000160 // We currently allow query objects to be reused acrossed live virtual
161 // registers, but always for the same live interval union.
Andrew Trick18c57a82010-11-30 23:18:47 +0000162 assert(LiveUnion == LIU && "inconsistent initialization");
Andrew Tricke141a492010-11-08 18:02:08 +0000163 // Retain cached results, e.g. firstInterference.
164 return;
165 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000166 clear();
167 LiveUnion = LIU;
168 VirtReg = VReg;
Andrew Tricke141a492010-11-08 18:02:08 +0000169 }
170
Andrew Trick18c57a82010-11-30 23:18:47 +0000171 LiveInterval &virtReg() const {
172 assert(VirtReg && "uninitialized");
173 return *VirtReg;
174 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000175
Andrew Trick18c57a82010-11-30 23:18:47 +0000176 bool isInterference(const InterferenceResult &IR) const {
177 if (IR.VirtRegI != VirtReg->end()) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000178 assert(overlap(*IR.VirtRegI, IR.LiveUnionI) &&
Andrew Trick14e8d712010-10-22 23:09:15 +0000179 "invalid segment iterators");
180 return true;
181 }
182 return false;
183 }
184
Andrew Trick18c57a82010-11-30 23:18:47 +0000185 // Does this live virtual register interfere with the union?
Andrew Trick14e8d712010-10-22 23:09:15 +0000186 bool checkInterference() { return isInterference(firstInterference()); }
187
Andrew Tricke141a492010-11-08 18:02:08 +0000188 // Get the first pair of interfering segments, or a noninterfering result.
189 // This initializes the firstInterference_ cache.
Andrew Trick14e8d712010-10-22 23:09:15 +0000190 InterferenceResult firstInterference();
191
192 // Treat the result as an iterator and advance to the next interfering pair
193 // of segments. Visiting each unique interfering pairs means that the same
Andrew Trick18c57a82010-11-30 23:18:47 +0000194 // VirtReg or LiveUnion segment may be visited multiple times.
195 bool nextInterference(InterferenceResult &IR) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000196
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000197 // Count the virtual registers in this union that interfere with this
198 // query's live virtual register, up to maxInterferingRegs.
Andrew Trick18c57a82010-11-30 23:18:47 +0000199 unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000200
201 // Was this virtual register visited during collectInterferingVRegs?
Andrew Trick18c57a82010-11-30 23:18:47 +0000202 bool isSeenInterference(LiveInterval *VReg) const;
203
204 // Did collectInterferingVRegs collect all interferences?
205 bool seenAllInterferences() const { return SeenAllInterferences; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000206
207 // Did collectInterferingVRegs encounter an unspillable vreg?
Andrew Trick18c57a82010-11-30 23:18:47 +0000208 bool seenUnspillableVReg() const { return SeenUnspillableVReg; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000209
210 // Vector generated by collectInterferingVRegs.
211 const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
Andrew Trick18c57a82010-11-30 23:18:47 +0000212 return InterferingVRegs;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000213 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000214
Andrew Trick14e8d712010-10-22 23:09:15 +0000215 private:
Andrew Trick8a83d542010-11-11 17:46:29 +0000216 Query(const Query&); // DO NOT IMPLEMENT
217 void operator=(const Query&); // DO NOT IMPLEMENT
Andrew Trick18c57a82010-11-30 23:18:47 +0000218
Andrew Trick14e8d712010-10-22 23:09:15 +0000219 // Private interface for queries
Andrew Trick18c57a82010-11-30 23:18:47 +0000220 void findIntersection(InterferenceResult &IR) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000221 };
222};
223
224} // end namespace llvm
225
226#endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)