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