Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 | |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/IntervalMap.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveInterval.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 25 | #ifndef NDEBUG |
| 26 | // forward declaration |
| 27 | template <unsigned Element> class SparseBitVector; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 28 | typedef SparseBitVector<128> LiveVirtRegBitSet; |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
Matt Beaumont-Gay | e33daaa | 2010-11-09 19:56:25 +0000 | [diff] [blame] | 31 | /// Abstraction to provide info for the representative register. |
| 32 | class AbstractRegisterDescription { |
| 33 | public: |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 34 | virtual const char *getName(unsigned Reg) const = 0; |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 35 | virtual ~AbstractRegisterDescription() {} |
Matt Beaumont-Gay | e33daaa | 2010-11-09 19:56:25 +0000 | [diff] [blame] | 36 | }; |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 37 | |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 38 | /// Compare a live virtual register segment to a LiveIntervalUnion segment. |
| 39 | inline bool |
| 40 | overlap(const LiveRange &VRSeg, |
| 41 | const IntervalMap<SlotIndex, LiveInterval*>::const_iterator &LUSeg) { |
| 42 | return VRSeg.start < LUSeg.stop() && LUSeg.start() < VRSeg.end; |
| 43 | } |
| 44 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 45 | /// 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. |
| 49 | class LiveIntervalUnion { |
| 50 | // A set of live virtual register segments that supports fast insertion, |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 51 | // intersection, and removal. |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 52 | // Mapping SlotIndex intervals to virtual register numbers. |
| 53 | typedef IntervalMap<SlotIndex, LiveInterval*> LiveSegments; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 54 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 55 | public: |
| 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 Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 61 | // LiveIntervalUnions share an external allocator. |
| 62 | typedef LiveSegments::Allocator Allocator; |
| 63 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 64 | class InterferenceResult; |
| 65 | class Query; |
| 66 | |
| 67 | private: |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 68 | const unsigned RepReg; // representative register number |
| 69 | LiveSegments Segments; // union of virtual reg segments |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 70 | |
| 71 | public: |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 72 | LiveIntervalUnion(unsigned r, Allocator &a) : RepReg(r), Segments(a) {} |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 73 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 74 | // Iterate over all segments in the union of live virtual registers ordered |
| 75 | // by their starting position. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 76 | SegmentIter begin() { return Segments.begin(); } |
| 77 | SegmentIter end() { return Segments.end(); } |
Jakob Stoklund Olesen | a35cce1 | 2010-12-09 01:06:52 +0000 | [diff] [blame^] | 78 | SegmentIter find(SlotIndex x) { return Segments.find(x); } |
Jakob Stoklund Olesen | 1b19dc1 | 2010-12-08 01:06:06 +0000 | [diff] [blame] | 79 | bool empty() { return Segments.empty(); } |
Jakob Stoklund Olesen | a35cce1 | 2010-12-09 01:06:52 +0000 | [diff] [blame^] | 80 | SlotIndex startIndex() { return Segments.start(); } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 81 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 82 | // Add a live virtual register to this union and merge its segments. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 83 | void unify(LiveInterval &VirtReg); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 84 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 85 | // Remove a live virtual register's segments from this union. |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 86 | void extract(LiveInterval &VirtReg); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 87 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 88 | void dump(const AbstractRegisterDescription *RegDesc) const; |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 89 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 90 | // If tri != NULL, use it to decode RepReg |
| 91 | void print(raw_ostream &OS, const AbstractRegisterDescription *RegDesc) const; |
| 92 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 93 | #ifndef NDEBUG |
| 94 | // Verify the live intervals in this union and add them to the visited set. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 95 | void verify(LiveVirtRegBitSet& VisitedVRegs); |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 98 | /// 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 Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 105 | LiveInterval::iterator VirtRegI; // current position in VirtReg |
| 106 | SegmentIter LiveUnionI; // current position in LiveUnion |
| 107 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 108 | // Internal ctor. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 109 | InterferenceResult(LiveInterval::iterator VRegI, SegmentIter UnionI) |
| 110 | : VirtRegI(VRegI), LiveUnionI(UnionI) {} |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 111 | |
| 112 | public: |
| 113 | // Public default ctor. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 114 | InterferenceResult(): VirtRegI(), LiveUnionI() {} |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 115 | |
| 116 | // Note: this interface provides raw access to the iterators because the |
| 117 | // result has no way to tell if it's valid to dereference them. |
| 118 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 119 | // Access the VirtReg segment. |
| 120 | LiveInterval::iterator virtRegPos() const { return VirtRegI; } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 121 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 122 | // Access the LiveUnion segment. |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 123 | const SegmentIter &liveUnionPos() const { return LiveUnionI; } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 124 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 125 | bool operator==(const InterferenceResult &IR) const { |
| 126 | return VirtRegI == IR.VirtRegI && LiveUnionI == IR.LiveUnionI; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 127 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 128 | bool operator!=(const InterferenceResult &IR) const { |
| 129 | return !operator==(IR); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 130 | } |
| 131 | }; |
| 132 | |
| 133 | /// Query interferences between a single live virtual register and a live |
| 134 | /// interval union. |
| 135 | class Query { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 136 | LiveIntervalUnion *LiveUnion; |
| 137 | LiveInterval *VirtReg; |
| 138 | InterferenceResult FirstInterference; |
| 139 | SmallVector<LiveInterval*,4> InterferingVRegs; |
Jakob Stoklund Olesen | a35cce1 | 2010-12-09 01:06:52 +0000 | [diff] [blame^] | 140 | bool CheckedFirstInterference; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 141 | bool SeenAllInterferences; |
| 142 | bool SeenUnspillableVReg; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 143 | |
| 144 | public: |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 145 | Query(): LiveUnion(), VirtReg() {} |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 146 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 147 | Query(LiveInterval *VReg, LiveIntervalUnion *LIU): |
| 148 | LiveUnion(LIU), VirtReg(VReg), SeenAllInterferences(false), |
| 149 | SeenUnspillableVReg(false) |
| 150 | {} |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 151 | |
| 152 | void clear() { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 153 | LiveUnion = NULL; |
| 154 | VirtReg = NULL; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 155 | InterferingVRegs.clear(); |
Jakob Stoklund Olesen | a35cce1 | 2010-12-09 01:06:52 +0000 | [diff] [blame^] | 156 | CheckedFirstInterference = false; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 157 | SeenAllInterferences = false; |
| 158 | SeenUnspillableVReg = false; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 159 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 160 | |
| 161 | void init(LiveInterval *VReg, LiveIntervalUnion *LIU) { |
| 162 | if (VirtReg == VReg) { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 163 | // We currently allow query objects to be reused acrossed live virtual |
| 164 | // registers, but always for the same live interval union. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 165 | assert(LiveUnion == LIU && "inconsistent initialization"); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 166 | // Retain cached results, e.g. firstInterference. |
| 167 | return; |
| 168 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 169 | clear(); |
| 170 | LiveUnion = LIU; |
| 171 | VirtReg = VReg; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 174 | LiveInterval &virtReg() const { |
| 175 | assert(VirtReg && "uninitialized"); |
| 176 | return *VirtReg; |
| 177 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 178 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 179 | bool isInterference(const InterferenceResult &IR) const { |
| 180 | if (IR.VirtRegI != VirtReg->end()) { |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 181 | assert(overlap(*IR.VirtRegI, IR.LiveUnionI) && |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 182 | "invalid segment iterators"); |
| 183 | return true; |
| 184 | } |
| 185 | return false; |
| 186 | } |
| 187 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 188 | // Does this live virtual register interfere with the union? |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 189 | bool checkInterference() { return isInterference(firstInterference()); } |
| 190 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 191 | // Get the first pair of interfering segments, or a noninterfering result. |
| 192 | // This initializes the firstInterference_ cache. |
Jakob Stoklund Olesen | a35cce1 | 2010-12-09 01:06:52 +0000 | [diff] [blame^] | 193 | const InterferenceResult &firstInterference(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 194 | |
| 195 | // Treat the result as an iterator and advance to the next interfering pair |
| 196 | // of segments. Visiting each unique interfering pairs means that the same |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 197 | // VirtReg or LiveUnion segment may be visited multiple times. |
| 198 | bool nextInterference(InterferenceResult &IR) const; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 199 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 200 | // Count the virtual registers in this union that interfere with this |
| 201 | // query's live virtual register, up to maxInterferingRegs. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 202 | unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 203 | |
| 204 | // Was this virtual register visited during collectInterferingVRegs? |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 205 | bool isSeenInterference(LiveInterval *VReg) const; |
| 206 | |
| 207 | // Did collectInterferingVRegs collect all interferences? |
| 208 | bool seenAllInterferences() const { return SeenAllInterferences; } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 209 | |
| 210 | // Did collectInterferingVRegs encounter an unspillable vreg? |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 211 | bool seenUnspillableVReg() const { return SeenUnspillableVReg; } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 212 | |
| 213 | // Vector generated by collectInterferingVRegs. |
| 214 | const SmallVectorImpl<LiveInterval*> &interferingVRegs() const { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 215 | return InterferingVRegs; |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 216 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 217 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 218 | private: |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 219 | Query(const Query&); // DO NOT IMPLEMENT |
| 220 | void operator=(const Query&); // DO NOT IMPLEMENT |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 221 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 222 | // Private interface for queries |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 223 | void findIntersection(InterferenceResult &IR) const; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 224 | }; |
| 225 | }; |
| 226 | |
| 227 | } // end namespace llvm |
| 228 | |
| 229 | #endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION) |