blob: 92d248266e0a0a2d88340503870dbefc5f69829b [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
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000025class TargetRegisterInfo;
26
Andrew Trick071d1c02010-11-09 21:04:34 +000027#ifndef NDEBUG
28// forward declaration
29template <unsigned Element> class SparseBitVector;
Andrew Trick18c57a82010-11-30 23:18:47 +000030typedef SparseBitVector<128> LiveVirtRegBitSet;
Andrew Trick071d1c02010-11-09 21:04:34 +000031#endif
32
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000033/// Compare a live virtual register segment to a LiveIntervalUnion segment.
34inline bool
35overlap(const LiveRange &VRSeg,
36 const IntervalMap<SlotIndex, LiveInterval*>::const_iterator &LUSeg) {
37 return VRSeg.start < LUSeg.stop() && LUSeg.start() < VRSeg.end;
38}
39
Andrew Trick14e8d712010-10-22 23:09:15 +000040/// Union of live intervals that are strong candidates for coalescing into a
41/// single register (either physical or virtual depending on the context). We
42/// expect the constituent live intervals to be disjoint, although we may
43/// eventually make exceptions to handle value-based interference.
44class LiveIntervalUnion {
45 // A set of live virtual register segments that supports fast insertion,
Andrew Trick18c57a82010-11-30 23:18:47 +000046 // intersection, and removal.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000047 // Mapping SlotIndex intervals to virtual register numbers.
48 typedef IntervalMap<SlotIndex, LiveInterval*> LiveSegments;
Andrew Trick14e8d712010-10-22 23:09:15 +000049
Andrew Trick14e8d712010-10-22 23:09:15 +000050public:
51 // SegmentIter can advance to the next segment ordered by starting position
52 // which may belong to a different live virtual register. We also must be able
53 // to reach the current segment's containing virtual register.
54 typedef LiveSegments::iterator SegmentIter;
55
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000056 // LiveIntervalUnions share an external allocator.
57 typedef LiveSegments::Allocator Allocator;
58
Andrew Trick14e8d712010-10-22 23:09:15 +000059 class InterferenceResult;
60 class Query;
61
62private:
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000063 const unsigned RepReg; // representative register number
64 LiveSegments Segments; // union of virtual reg segments
Andrew Trick14e8d712010-10-22 23:09:15 +000065
66public:
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000067 LiveIntervalUnion(unsigned r, Allocator &a) : RepReg(r), Segments(a) {}
Andrew Trick14e8d712010-10-22 23:09:15 +000068
Andrew Tricke16eecc2010-10-26 18:34:01 +000069 // Iterate over all segments in the union of live virtual registers ordered
70 // by their starting position.
Andrew Trick18c57a82010-11-30 23:18:47 +000071 SegmentIter begin() { return Segments.begin(); }
72 SegmentIter end() { return Segments.end(); }
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +000073 SegmentIter find(SlotIndex x) { return Segments.find(x); }
Jakob Stoklund Olesen1b19dc12010-12-08 01:06:06 +000074 bool empty() { return Segments.empty(); }
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +000075 SlotIndex startIndex() { return Segments.start(); }
Andrew Trick14e8d712010-10-22 23:09:15 +000076
Andrew Tricke16eecc2010-10-26 18:34:01 +000077 // Add a live virtual register to this union and merge its segments.
Andrew Trick18c57a82010-11-30 23:18:47 +000078 void unify(LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +000079
Andrew Tricke141a492010-11-08 18:02:08 +000080 // Remove a live virtual register's segments from this union.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000081 void extract(LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +000082
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000083 // Print union, using TRI to translate register names
84 void print(raw_ostream &OS, const TargetRegisterInfo *TRI) const;
Andrew Trick18c57a82010-11-30 23:18:47 +000085
Andrew Trick071d1c02010-11-09 21:04:34 +000086#ifndef NDEBUG
87 // Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +000088 void verify(LiveVirtRegBitSet& VisitedVRegs);
Andrew Trick071d1c02010-11-09 21:04:34 +000089#endif
90
Andrew Trick14e8d712010-10-22 23:09:15 +000091 /// Cache a single interference test result in the form of two intersecting
92 /// segments. This allows efficiently iterating over the interferences. The
93 /// iteration logic is handled by LiveIntervalUnion::Query which may
94 /// filter interferences depending on the type of query.
95 class InterferenceResult {
96 friend class Query;
97
Andrew Trick18c57a82010-11-30 23:18:47 +000098 LiveInterval::iterator VirtRegI; // current position in VirtReg
99 SegmentIter LiveUnionI; // current position in LiveUnion
100
Andrew Trick14e8d712010-10-22 23:09:15 +0000101 // Internal ctor.
Andrew Trick18c57a82010-11-30 23:18:47 +0000102 InterferenceResult(LiveInterval::iterator VRegI, SegmentIter UnionI)
103 : VirtRegI(VRegI), LiveUnionI(UnionI) {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000104
105 public:
106 // Public default ctor.
Andrew Trick18c57a82010-11-30 23:18:47 +0000107 InterferenceResult(): VirtRegI(), LiveUnionI() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000108
109 // Note: this interface provides raw access to the iterators because the
110 // result has no way to tell if it's valid to dereference them.
111
Andrew Trick18c57a82010-11-30 23:18:47 +0000112 // Access the VirtReg segment.
113 LiveInterval::iterator virtRegPos() const { return VirtRegI; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000114
Andrew Trick18c57a82010-11-30 23:18:47 +0000115 // Access the LiveUnion segment.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000116 const SegmentIter &liveUnionPos() const { return LiveUnionI; }
Andrew Trick14e8d712010-10-22 23:09:15 +0000117
Andrew Trick18c57a82010-11-30 23:18:47 +0000118 bool operator==(const InterferenceResult &IR) const {
119 return VirtRegI == IR.VirtRegI && LiveUnionI == IR.LiveUnionI;
Andrew Trick14e8d712010-10-22 23:09:15 +0000120 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000121 bool operator!=(const InterferenceResult &IR) const {
122 return !operator==(IR);
Andrew Trick14e8d712010-10-22 23:09:15 +0000123 }
124 };
125
126 /// Query interferences between a single live virtual register and a live
127 /// interval union.
128 class Query {
Andrew Trick18c57a82010-11-30 23:18:47 +0000129 LiveIntervalUnion *LiveUnion;
130 LiveInterval *VirtReg;
131 InterferenceResult FirstInterference;
132 SmallVector<LiveInterval*,4> InterferingVRegs;
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000133 bool CheckedFirstInterference;
Andrew Trick18c57a82010-11-30 23:18:47 +0000134 bool SeenAllInterferences;
135 bool SeenUnspillableVReg;
Andrew Trick14e8d712010-10-22 23:09:15 +0000136
137 public:
Andrew Trick18c57a82010-11-30 23:18:47 +0000138 Query(): LiveUnion(), VirtReg() {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000139
Andrew Trick18c57a82010-11-30 23:18:47 +0000140 Query(LiveInterval *VReg, LiveIntervalUnion *LIU):
Jakob Stoklund Olesena0382c62010-12-09 21:20:44 +0000141 LiveUnion(LIU), VirtReg(VReg), CheckedFirstInterference(false),
142 SeenAllInterferences(false), SeenUnspillableVReg(false)
Andrew Trick18c57a82010-11-30 23:18:47 +0000143 {}
Andrew Tricke141a492010-11-08 18:02:08 +0000144
145 void clear() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000146 LiveUnion = NULL;
147 VirtReg = NULL;
Andrew Trick18c57a82010-11-30 23:18:47 +0000148 InterferingVRegs.clear();
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000149 CheckedFirstInterference = false;
Andrew Trick18c57a82010-11-30 23:18:47 +0000150 SeenAllInterferences = false;
151 SeenUnspillableVReg = false;
Andrew Tricke141a492010-11-08 18:02:08 +0000152 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000153
154 void init(LiveInterval *VReg, LiveIntervalUnion *LIU) {
Jakob Stoklund Olesena0382c62010-12-09 21:20:44 +0000155 assert(VReg && LIU && "Invalid arguments");
Andrew Trickb853e6c2010-12-09 18:15:21 +0000156 if (VirtReg == VReg && LiveUnion == LIU) {
Andrew Tricke141a492010-11-08 18:02:08 +0000157 // Retain cached results, e.g. firstInterference.
158 return;
159 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000160 clear();
161 LiveUnion = LIU;
162 VirtReg = VReg;
Andrew Tricke141a492010-11-08 18:02:08 +0000163 }
164
Andrew Trick18c57a82010-11-30 23:18:47 +0000165 LiveInterval &virtReg() const {
166 assert(VirtReg && "uninitialized");
167 return *VirtReg;
168 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000169
Andrew Trick18c57a82010-11-30 23:18:47 +0000170 bool isInterference(const InterferenceResult &IR) const {
171 if (IR.VirtRegI != VirtReg->end()) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000172 assert(overlap(*IR.VirtRegI, IR.LiveUnionI) &&
Andrew Trick14e8d712010-10-22 23:09:15 +0000173 "invalid segment iterators");
174 return true;
175 }
176 return false;
177 }
178
Andrew Trick18c57a82010-11-30 23:18:47 +0000179 // Does this live virtual register interfere with the union?
Andrew Trick14e8d712010-10-22 23:09:15 +0000180 bool checkInterference() { return isInterference(firstInterference()); }
181
Andrew Tricke141a492010-11-08 18:02:08 +0000182 // Get the first pair of interfering segments, or a noninterfering result.
183 // This initializes the firstInterference_ cache.
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000184 const InterferenceResult &firstInterference();
Andrew Trick14e8d712010-10-22 23:09:15 +0000185
186 // Treat the result as an iterator and advance to the next interfering pair
187 // of segments. Visiting each unique interfering pairs means that the same
Andrew Trick18c57a82010-11-30 23:18:47 +0000188 // VirtReg or LiveUnion segment may be visited multiple times.
189 bool nextInterference(InterferenceResult &IR) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000190
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000191 // Count the virtual registers in this union that interfere with this
192 // query's live virtual register, up to maxInterferingRegs.
Andrew Trick18c57a82010-11-30 23:18:47 +0000193 unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000194
195 // Was this virtual register visited during collectInterferingVRegs?
Andrew Trick18c57a82010-11-30 23:18:47 +0000196 bool isSeenInterference(LiveInterval *VReg) const;
197
198 // Did collectInterferingVRegs collect all interferences?
199 bool seenAllInterferences() const { return SeenAllInterferences; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000200
201 // Did collectInterferingVRegs encounter an unspillable vreg?
Andrew Trick18c57a82010-11-30 23:18:47 +0000202 bool seenUnspillableVReg() const { return SeenUnspillableVReg; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000203
204 // Vector generated by collectInterferingVRegs.
205 const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
Andrew Trick18c57a82010-11-30 23:18:47 +0000206 return InterferingVRegs;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000207 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000208
Andrew Trick14e8d712010-10-22 23:09:15 +0000209 private:
Andrew Trick8a83d542010-11-11 17:46:29 +0000210 Query(const Query&); // DO NOT IMPLEMENT
211 void operator=(const Query&); // DO NOT IMPLEMENT
Andrew Trick18c57a82010-11-30 23:18:47 +0000212
Andrew Trick14e8d712010-10-22 23:09:15 +0000213 // Private interface for queries
Andrew Trick18c57a82010-11-30 23:18:47 +0000214 void findIntersection(InterferenceResult &IR) const;
Andrew Trick14e8d712010-10-22 23:09:15 +0000215 };
216};
217
218} // end namespace llvm
219
220#endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)