blob: ff23cf61a3338c16a5d47d5acf54bf1ea569f5d2 [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
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000023#include <algorithm>
24
Andrew Trick14e8d712010-10-22 23:09:15 +000025namespace llvm {
26
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +000027class MachineLoopRange;
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000028class TargetRegisterInfo;
29
Andrew Trick071d1c02010-11-09 21:04:34 +000030#ifndef NDEBUG
31// forward declaration
32template <unsigned Element> class SparseBitVector;
Andrew Trick18c57a82010-11-30 23:18:47 +000033typedef SparseBitVector<128> LiveVirtRegBitSet;
Andrew Trick071d1c02010-11-09 21:04:34 +000034#endif
35
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000036/// Compare a live virtual register segment to a LiveIntervalUnion segment.
37inline bool
38overlap(const LiveRange &VRSeg,
39 const IntervalMap<SlotIndex, LiveInterval*>::const_iterator &LUSeg) {
40 return VRSeg.start < LUSeg.stop() && LUSeg.start() < VRSeg.end;
41}
42
Andrew Trick14e8d712010-10-22 23:09:15 +000043/// Union of live intervals that are strong candidates for coalescing into a
44/// single register (either physical or virtual depending on the context). We
45/// expect the constituent live intervals to be disjoint, although we may
46/// eventually make exceptions to handle value-based interference.
47class LiveIntervalUnion {
48 // A set of live virtual register segments that supports fast insertion,
Andrew Trick18c57a82010-11-30 23:18:47 +000049 // intersection, and removal.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000050 // Mapping SlotIndex intervals to virtual register numbers.
51 typedef IntervalMap<SlotIndex, LiveInterval*> LiveSegments;
Andrew Trick14e8d712010-10-22 23:09:15 +000052
Andrew Trick14e8d712010-10-22 23:09:15 +000053public:
54 // SegmentIter can advance to the next segment ordered by starting position
55 // which may belong to a different live virtual register. We also must be able
56 // to reach the current segment's containing virtual register.
57 typedef LiveSegments::iterator SegmentIter;
58
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000059 // LiveIntervalUnions share an external allocator.
60 typedef LiveSegments::Allocator Allocator;
61
Andrew Trick14e8d712010-10-22 23:09:15 +000062 class InterferenceResult;
63 class Query;
64
65private:
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000066 const unsigned RepReg; // representative register number
67 LiveSegments Segments; // union of virtual reg segments
Andrew Trick14e8d712010-10-22 23:09:15 +000068
69public:
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000070 LiveIntervalUnion(unsigned r, Allocator &a) : RepReg(r), Segments(a) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000071
Andrew Tricke16eecc2010-10-26 18:34:01 +000072 // Iterate over all segments in the union of live virtual registers ordered
73 // by their starting position.
Andrew Trick18c57a82010-11-30 23:18:47 +000074 SegmentIter begin() { return Segments.begin(); }
75 SegmentIter end() { return Segments.end(); }
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +000076 SegmentIter find(SlotIndex x) { return Segments.find(x); }
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000077 bool empty() const { return Segments.empty(); }
78 SlotIndex startIndex() const { return Segments.start(); }
Andrew Trick14e8d712010-10-22 23:09:15 +000079
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +000080 // Provide public access to the underlying map to allow overlap iteration.
81 typedef LiveSegments Map;
82 const Map &getMap() { return Segments; }
83
Andrew Tricke16eecc2010-10-26 18:34:01 +000084 // Add a live virtual register to this union and merge its segments.
Andrew Trick18c57a82010-11-30 23:18:47 +000085 void unify(LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +000086
Andrew Tricke141a492010-11-08 18:02:08 +000087 // Remove a live virtual register's segments from this union.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000088 void extract(LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +000089
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000090 // Print union, using TRI to translate register names
91 void print(raw_ostream &OS, const TargetRegisterInfo *TRI) const;
Andrew Trick18c57a82010-11-30 23:18:47 +000092
Andrew Trick071d1c02010-11-09 21:04:34 +000093#ifndef NDEBUG
94 // Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +000095 void verify(LiveVirtRegBitSet& VisitedVRegs);
Andrew Trick071d1c02010-11-09 21:04:34 +000096#endif
97
Andrew Trick14e8d712010-10-22 23:09:15 +000098 /// Cache a single interference test result in the form of two intersecting
99 /// segments. This allows efficiently iterating over the interferences. The
100 /// iteration logic is handled by LiveIntervalUnion::Query which may
101 /// filter interferences depending on the type of query.
102 class InterferenceResult {
103 friend class Query;
104
Andrew Trick18c57a82010-11-30 23:18:47 +0000105 LiveInterval::iterator VirtRegI; // current position in VirtReg
106 SegmentIter LiveUnionI; // current position in LiveUnion
107
Andrew Trick14e8d712010-10-22 23:09:15 +0000108 // Internal ctor.
Andrew Trick18c57a82010-11-30 23:18:47 +0000109 InterferenceResult(LiveInterval::iterator VRegI, SegmentIter UnionI)
110 : VirtRegI(VRegI), LiveUnionI(UnionI) {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000111
112 public:
113 // Public default ctor.
Andrew Trick18c57a82010-11-30 23:18:47 +0000114 InterferenceResult(): VirtRegI(), LiveUnionI() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000115
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +0000116 /// start - Return the start of the current overlap.
117 SlotIndex start() const {
118 return std::max(VirtRegI->start, LiveUnionI.start());
119 }
120
121 /// stop - Return the end of the current overlap.
122 SlotIndex stop() const {
123 return std::min(VirtRegI->end, LiveUnionI.stop());
124 }
125
126 /// interference - Return the register that is interfering here.
127 LiveInterval *interference() const { return LiveUnionI.value(); }
128
Andrew Trick14e8d712010-10-22 23:09:15 +0000129 // Note: this interface provides raw access to the iterators because the
130 // result has no way to tell if it's valid to dereference them.
131
Andrew Trick18c57a82010-11-30 23:18:47 +0000132 // Access the VirtReg segment.
133 LiveInterval::iterator virtRegPos() const { return VirtRegI; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000134
Andrew Trick18c57a82010-11-30 23:18:47 +0000135 // Access the LiveUnion segment.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000136 const SegmentIter &liveUnionPos() const { return LiveUnionI; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000137
Andrew Trick18c57a82010-11-30 23:18:47 +0000138 bool operator==(const InterferenceResult &IR) const {
139 return VirtRegI == IR.VirtRegI && LiveUnionI == IR.LiveUnionI;
Andrew Trick14e8d712010-10-22 23:09:15 +0000140 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000141 bool operator!=(const InterferenceResult &IR) const {
142 return !operator==(IR);
Andrew Trick14e8d712010-10-22 23:09:15 +0000143 }
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +0000144
145 void print(raw_ostream &OS, const TargetRegisterInfo *TRI) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000146 };
147
148 /// Query interferences between a single live virtual register and a live
149 /// interval union.
150 class Query {
Andrew Trick18c57a82010-11-30 23:18:47 +0000151 LiveIntervalUnion *LiveUnion;
152 LiveInterval *VirtReg;
153 InterferenceResult FirstInterference;
154 SmallVector<LiveInterval*,4> InterferingVRegs;
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000155 bool CheckedFirstInterference;
Andrew Trick18c57a82010-11-30 23:18:47 +0000156 bool SeenAllInterferences;
157 bool SeenUnspillableVReg;
Andrew Trick14e8d712010-10-22 23:09:15 +0000158
159 public:
Andrew Trick18c57a82010-11-30 23:18:47 +0000160 Query(): LiveUnion(), VirtReg() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000161
Andrew Trick18c57a82010-11-30 23:18:47 +0000162 Query(LiveInterval *VReg, LiveIntervalUnion *LIU):
Jakob Stoklund Olesena0382c62010-12-09 21:20:44 +0000163 LiveUnion(LIU), VirtReg(VReg), CheckedFirstInterference(false),
164 SeenAllInterferences(false), SeenUnspillableVReg(false)
Andrew Trick18c57a82010-11-30 23:18:47 +0000165 {}
Andrew Tricke141a492010-11-08 18:02:08 +0000166
167 void clear() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000168 LiveUnion = NULL;
169 VirtReg = NULL;
Andrew Trick18c57a82010-11-30 23:18:47 +0000170 InterferingVRegs.clear();
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000171 CheckedFirstInterference = false;
Andrew Trick18c57a82010-11-30 23:18:47 +0000172 SeenAllInterferences = false;
173 SeenUnspillableVReg = false;
Andrew Tricke141a492010-11-08 18:02:08 +0000174 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000175
176 void init(LiveInterval *VReg, LiveIntervalUnion *LIU) {
Jakob Stoklund Olesena0382c62010-12-09 21:20:44 +0000177 assert(VReg && LIU && "Invalid arguments");
Andrew Trickb853e6c2010-12-09 18:15:21 +0000178 if (VirtReg == VReg && LiveUnion == LIU) {
Andrew Tricke141a492010-11-08 18:02:08 +0000179 // Retain cached results, e.g. firstInterference.
180 return;
181 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000182 clear();
183 LiveUnion = LIU;
184 VirtReg = VReg;
Andrew Tricke141a492010-11-08 18:02:08 +0000185 }
186
Andrew Trick18c57a82010-11-30 23:18:47 +0000187 LiveInterval &virtReg() const {
188 assert(VirtReg && "uninitialized");
189 return *VirtReg;
190 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000191
Andrew Trick18c57a82010-11-30 23:18:47 +0000192 bool isInterference(const InterferenceResult &IR) const {
193 if (IR.VirtRegI != VirtReg->end()) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000194 assert(overlap(*IR.VirtRegI, IR.LiveUnionI) &&
Andrew Trick14e8d712010-10-22 23:09:15 +0000195 "invalid segment iterators");
196 return true;
197 }
198 return false;
199 }
200
Andrew Trick18c57a82010-11-30 23:18:47 +0000201 // Does this live virtual register interfere with the union?
Andrew Trick14e8d712010-10-22 23:09:15 +0000202 bool checkInterference() { return isInterference(firstInterference()); }
203
Andrew Tricke141a492010-11-08 18:02:08 +0000204 // Get the first pair of interfering segments, or a noninterfering result.
205 // This initializes the firstInterference_ cache.
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000206 const InterferenceResult &firstInterference();
Andrew Trick14e8d712010-10-22 23:09:15 +0000207
208 // Treat the result as an iterator and advance to the next interfering pair
209 // of segments. Visiting each unique interfering pairs means that the same
Andrew Trick18c57a82010-11-30 23:18:47 +0000210 // VirtReg or LiveUnion segment may be visited multiple times.
211 bool nextInterference(InterferenceResult &IR) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000212
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000213 // Count the virtual registers in this union that interfere with this
214 // query's live virtual register, up to maxInterferingRegs.
Andrew Trick18c57a82010-11-30 23:18:47 +0000215 unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000216
217 // Was this virtual register visited during collectInterferingVRegs?
Andrew Trick18c57a82010-11-30 23:18:47 +0000218 bool isSeenInterference(LiveInterval *VReg) const;
219
220 // Did collectInterferingVRegs collect all interferences?
221 bool seenAllInterferences() const { return SeenAllInterferences; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000222
223 // Did collectInterferingVRegs encounter an unspillable vreg?
Andrew Trick18c57a82010-11-30 23:18:47 +0000224 bool seenUnspillableVReg() const { return SeenUnspillableVReg; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000225
226 // Vector generated by collectInterferingVRegs.
227 const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
Andrew Trick18c57a82010-11-30 23:18:47 +0000228 return InterferingVRegs;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000229 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000230
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +0000231 /// checkLoopInterference - Return true if there is interference overlapping
232 /// Loop.
233 bool checkLoopInterference(MachineLoopRange*);
234
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +0000235 void print(raw_ostream &OS, const TargetRegisterInfo *TRI);
Andrew Trick14e8d712010-10-22 23:09:15 +0000236 private:
Andrew Trick8a83d542010-11-11 17:46:29 +0000237 Query(const Query&); // DO NOT IMPLEMENT
238 void operator=(const Query&); // DO NOT IMPLEMENT
Andrew Trick18c57a82010-11-30 23:18:47 +0000239
Andrew Trick14e8d712010-10-22 23:09:15 +0000240 // Private interface for queries
Andrew Trick18c57a82010-11-30 23:18:47 +0000241 void findIntersection(InterferenceResult &IR) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000242 };
243};
244
245} // end namespace llvm
246
247#endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)