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