blob: cd4e690c3740a197ef33f8cf44c5e80b5c4defce [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 Olesenff2e9b42010-12-17 04:09:47 +000025class MachineLoopRange;
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000026class TargetRegisterInfo;
27
Andrew Trick071d1c02010-11-09 21:04:34 +000028#ifndef NDEBUG
29// forward declaration
30template <unsigned Element> class SparseBitVector;
Andrew Trick18c57a82010-11-30 23:18:47 +000031typedef SparseBitVector<128> LiveVirtRegBitSet;
Andrew Trick071d1c02010-11-09 21:04:34 +000032#endif
33
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000034/// Compare a live virtual register segment to a LiveIntervalUnion segment.
35inline bool
36overlap(const LiveRange &VRSeg,
37 const IntervalMap<SlotIndex, LiveInterval*>::const_iterator &LUSeg) {
38 return VRSeg.start < LUSeg.stop() && LUSeg.start() < VRSeg.end;
39}
40
Andrew Trick14e8d712010-10-22 23:09:15 +000041/// Union of live intervals that are strong candidates for coalescing into a
42/// single register (either physical or virtual depending on the context). We
43/// expect the constituent live intervals to be disjoint, although we may
44/// eventually make exceptions to handle value-based interference.
45class LiveIntervalUnion {
46 // A set of live virtual register segments that supports fast insertion,
Andrew Trick18c57a82010-11-30 23:18:47 +000047 // intersection, and removal.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000048 // Mapping SlotIndex intervals to virtual register numbers.
49 typedef IntervalMap<SlotIndex, LiveInterval*> LiveSegments;
Andrew Trick14e8d712010-10-22 23:09:15 +000050
Andrew Trick14e8d712010-10-22 23:09:15 +000051public:
52 // SegmentIter can advance to the next segment ordered by starting position
53 // which may belong to a different live virtual register. We also must be able
54 // to reach the current segment's containing virtual register.
55 typedef LiveSegments::iterator SegmentIter;
56
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000057 // LiveIntervalUnions share an external allocator.
58 typedef LiveSegments::Allocator Allocator;
59
Andrew Trick14e8d712010-10-22 23:09:15 +000060 class Query;
61
62private:
Jakob Stoklund Olesen4f6364f2011-02-09 21:52:03 +000063 unsigned Tag; // unique tag for current contents.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000064 LiveSegments Segments; // union of virtual reg segments
Andrew Trick14e8d712010-10-22 23:09:15 +000065
66public:
Jakob Stoklund Olesen2fd09232012-06-05 23:07:19 +000067 explicit LiveIntervalUnion(Allocator &a) : Tag(0), 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 Olesenbfce6782010-12-14 19:38:49 +000074 bool empty() const { return Segments.empty(); }
75 SlotIndex startIndex() const { return Segments.start(); }
Andrew Trick14e8d712010-10-22 23:09:15 +000076
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +000077 // Provide public access to the underlying map to allow overlap iteration.
78 typedef LiveSegments Map;
79 const Map &getMap() { return Segments; }
80
Jakob Stoklund Olesen4f6364f2011-02-09 21:52:03 +000081 /// getTag - Return an opaque tag representing the current state of the union.
82 unsigned getTag() const { return Tag; }
83
84 /// changedSince - Return true if the union change since getTag returned tag.
85 bool changedSince(unsigned tag) const { return tag != Tag; }
86
Andrew Tricke16eecc2010-10-26 18:34:01 +000087 // Add a live virtual register to this union and merge its segments.
Andrew Trick18c57a82010-11-30 23:18:47 +000088 void unify(LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +000089
Andrew Tricke141a492010-11-08 18:02:08 +000090 // Remove a live virtual register's segments from this union.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000091 void extract(LiveInterval &VirtReg);
Andrew Trick14e8d712010-10-22 23:09:15 +000092
Jakob Stoklund Olesen560ab9e2011-04-11 23:57:14 +000093 // Remove all inserted virtual registers.
94 void clear() { Segments.clear(); ++Tag; }
95
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000096 // Print union, using TRI to translate register names
97 void print(raw_ostream &OS, const TargetRegisterInfo *TRI) const;
Andrew Trick18c57a82010-11-30 23:18:47 +000098
Andrew Trick071d1c02010-11-09 21:04:34 +000099#ifndef NDEBUG
100 // Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +0000101 void verify(LiveVirtRegBitSet& VisitedVRegs);
Andrew Trick071d1c02010-11-09 21:04:34 +0000102#endif
103
Andrew Trick14e8d712010-10-22 23:09:15 +0000104 /// Query interferences between a single live virtual register and a live
105 /// interval union.
106 class Query {
Andrew Trick18c57a82010-11-30 23:18:47 +0000107 LiveIntervalUnion *LiveUnion;
108 LiveInterval *VirtReg;
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000109 LiveInterval::iterator VirtRegI; // current position in VirtReg
110 SegmentIter LiveUnionI; // current position in LiveUnion
Andrew Trick18c57a82010-11-30 23:18:47 +0000111 SmallVector<LiveInterval*,4> InterferingVRegs;
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000112 bool CheckedFirstInterference;
Andrew Trick18c57a82010-11-30 23:18:47 +0000113 bool SeenAllInterferences;
114 bool SeenUnspillableVReg;
Jakob Stoklund Olesen29267332011-03-16 22:56:11 +0000115 unsigned Tag, UserTag;
Andrew Trick14e8d712010-10-22 23:09:15 +0000116
117 public:
Jakob Stoklund Olesen314a3ef2011-03-31 15:14:11 +0000118 Query(): LiveUnion(), VirtReg(), Tag(0), UserTag(0) {}
Andrew Trick14e8d712010-10-22 23:09:15 +0000119
Andrew Trick18c57a82010-11-30 23:18:47 +0000120 Query(LiveInterval *VReg, LiveIntervalUnion *LIU):
Jakob Stoklund Olesena0382c62010-12-09 21:20:44 +0000121 LiveUnion(LIU), VirtReg(VReg), CheckedFirstInterference(false),
122 SeenAllInterferences(false), SeenUnspillableVReg(false)
Andrew Trick18c57a82010-11-30 23:18:47 +0000123 {}
Andrew Tricke141a492010-11-08 18:02:08 +0000124
125 void clear() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000126 LiveUnion = NULL;
127 VirtReg = NULL;
Andrew Trick18c57a82010-11-30 23:18:47 +0000128 InterferingVRegs.clear();
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000129 CheckedFirstInterference = false;
Andrew Trick18c57a82010-11-30 23:18:47 +0000130 SeenAllInterferences = false;
131 SeenUnspillableVReg = false;
Jakob Stoklund Olesen4f6364f2011-02-09 21:52:03 +0000132 Tag = 0;
Jakob Stoklund Olesen29267332011-03-16 22:56:11 +0000133 UserTag = 0;
Andrew Tricke141a492010-11-08 18:02:08 +0000134 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000135
Jakob Stoklund Olesen29267332011-03-16 22:56:11 +0000136 void init(unsigned UTag, LiveInterval *VReg, LiveIntervalUnion *LIU) {
Jakob Stoklund Olesena0382c62010-12-09 21:20:44 +0000137 assert(VReg && LIU && "Invalid arguments");
Jakob Stoklund Olesen29267332011-03-16 22:56:11 +0000138 if (UserTag == UTag && VirtReg == VReg &&
139 LiveUnion == LIU && !LIU->changedSince(Tag)) {
Andrew Tricke141a492010-11-08 18:02:08 +0000140 // Retain cached results, e.g. firstInterference.
141 return;
142 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000143 clear();
144 LiveUnion = LIU;
145 VirtReg = VReg;
Jakob Stoklund Olesen4f6364f2011-02-09 21:52:03 +0000146 Tag = LIU->getTag();
Jakob Stoklund Olesen29267332011-03-16 22:56:11 +0000147 UserTag = UTag;
Andrew Tricke141a492010-11-08 18:02:08 +0000148 }
149
Andrew Trick18c57a82010-11-30 23:18:47 +0000150 LiveInterval &virtReg() const {
151 assert(VirtReg && "uninitialized");
152 return *VirtReg;
153 }
Andrew Trick14e8d712010-10-22 23:09:15 +0000154
Andrew Trick18c57a82010-11-30 23:18:47 +0000155 // Does this live virtual register interfere with the union?
Jakob Stoklund Olesen9942ba92011-08-11 21:18:34 +0000156 bool checkInterference() { return collectInterferingVRegs(1); }
Andrew Trick14e8d712010-10-22 23:09:15 +0000157
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000158 // Count the virtual registers in this union that interfere with this
159 // query's live virtual register, up to maxInterferingRegs.
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000160 unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000161
162 // Was this virtual register visited during collectInterferingVRegs?
Andrew Trick18c57a82010-11-30 23:18:47 +0000163 bool isSeenInterference(LiveInterval *VReg) const;
164
165 // Did collectInterferingVRegs collect all interferences?
166 bool seenAllInterferences() const { return SeenAllInterferences; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000167
168 // Did collectInterferingVRegs encounter an unspillable vreg?
Andrew Trick18c57a82010-11-30 23:18:47 +0000169 bool seenUnspillableVReg() const { return SeenUnspillableVReg; }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000170
171 // Vector generated by collectInterferingVRegs.
172 const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
Andrew Trick18c57a82010-11-30 23:18:47 +0000173 return InterferingVRegs;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000174 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000175
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +0000176 /// checkLoopInterference - Return true if there is interference overlapping
177 /// Loop.
178 bool checkLoopInterference(MachineLoopRange*);
179
Andrew Trick14e8d712010-10-22 23:09:15 +0000180 private:
Andrew Trick8a83d542010-11-11 17:46:29 +0000181 Query(const Query&); // DO NOT IMPLEMENT
182 void operator=(const Query&); // DO NOT IMPLEMENT
Andrew Trick14e8d712010-10-22 23:09:15 +0000183 };
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000184
185 // Array of LiveIntervalUnions.
186 class Array {
187 unsigned Size;
188 LiveIntervalUnion *LIUs;
189 public:
190 Array() : Size(0), LIUs(0) {}
191 ~Array() { clear(); }
192
193 // Initialize the array to have Size entries.
194 // Reuse an existing allocation if the size matches.
195 void init(LiveIntervalUnion::Allocator&, unsigned Size);
196
197 unsigned size() const { return Size; }
198
199 void clear();
200
201 LiveIntervalUnion& operator[](unsigned idx) {
202 assert(idx < Size && "idx out of bounds");
203 return LIUs[idx];
204 }
205 };
Andrew Trick14e8d712010-10-22 23:09:15 +0000206};
207
208} // end namespace llvm
209
210#endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION)