LiveIntervalUnion.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #ifndef LLVM_CODEGEN_LIVEINTERVALUNION_H
  17. #define LLVM_CODEGEN_LIVEINTERVALUNION_H
  18. #include "llvm/ADT/IntervalMap.h"
  19. #include "llvm/CodeGen/LiveInterval.h"
  20. namespace llvm {
  21. class TargetRegisterInfo;
  22. #ifndef NDEBUG
  23. // forward declaration
  24. template <unsigned Element> class SparseBitVector;
  25. typedef SparseBitVector<128> LiveVirtRegBitSet;
  26. #endif
  27. /// Compare a live virtual register segment to a LiveIntervalUnion segment.
  28. inline bool
  29. overlap(const LiveInterval::Segment &VRSeg,
  30. const IntervalMap<SlotIndex, LiveInterval*>::const_iterator &LUSeg) {
  31. return VRSeg.start < LUSeg.stop() && LUSeg.start() < VRSeg.end;
  32. }
  33. /// Union of live intervals that are strong candidates for coalescing into a
  34. /// single register (either physical or virtual depending on the context). We
  35. /// expect the constituent live intervals to be disjoint, although we may
  36. /// eventually make exceptions to handle value-based interference.
  37. class LiveIntervalUnion {
  38. // A set of live virtual register segments that supports fast insertion,
  39. // intersection, and removal.
  40. // Mapping SlotIndex intervals to virtual register numbers.
  41. typedef IntervalMap<SlotIndex, LiveInterval*> LiveSegments;
  42. public:
  43. // SegmentIter can advance to the next segment ordered by starting position
  44. // which may belong to a different live virtual register. We also must be able
  45. // to reach the current segment's containing virtual register.
  46. typedef LiveSegments::iterator SegmentIter;
  47. // LiveIntervalUnions share an external allocator.
  48. typedef LiveSegments::Allocator Allocator;
  49. class Query;
  50. private:
  51. unsigned Tag; // unique tag for current contents.
  52. LiveSegments Segments; // union of virtual reg segments
  53. public:
  54. explicit LiveIntervalUnion(Allocator &a) : Tag(0), Segments(a) {}
  55. // Iterate over all segments in the union of live virtual registers ordered
  56. // by their starting position.
  57. SegmentIter begin() { return Segments.begin(); }
  58. SegmentIter end() { return Segments.end(); }
  59. SegmentIter find(SlotIndex x) { return Segments.find(x); }
  60. bool empty() const { return Segments.empty(); }
  61. SlotIndex startIndex() const { return Segments.start(); }
  62. // Provide public access to the underlying map to allow overlap iteration.
  63. typedef LiveSegments Map;
  64. const Map &getMap() { return Segments; }
  65. /// getTag - Return an opaque tag representing the current state of the union.
  66. unsigned getTag() const { return Tag; }
  67. /// changedSince - Return true if the union change since getTag returned tag.
  68. bool changedSince(unsigned tag) const { return tag != Tag; }
  69. // Add a live virtual register to this union and merge its segments.
  70. void unify(LiveInterval &VirtReg, const LiveRange &Range);
  71. void unify(LiveInterval &VirtReg) {
  72. unify(VirtReg, VirtReg);
  73. }
  74. // Remove a live virtual register's segments from this union.
  75. void extract(LiveInterval &VirtReg, const LiveRange &Range);
  76. void extract(LiveInterval &VirtReg) {
  77. extract(VirtReg, VirtReg);
  78. }
  79. // Remove all inserted virtual registers.
  80. void clear() { Segments.clear(); ++Tag; }
  81. // Print union, using TRI to translate register names
  82. void print(raw_ostream &OS, const TargetRegisterInfo *TRI) const;
  83. #ifndef NDEBUG
  84. // Verify the live intervals in this union and add them to the visited set.
  85. void verify(LiveVirtRegBitSet& VisitedVRegs);
  86. #endif
  87. /// Query interferences between a single live virtual register and a live
  88. /// interval union.
  89. class Query {
  90. LiveIntervalUnion *LiveUnion;
  91. LiveInterval *VirtReg;
  92. LiveInterval::iterator VirtRegI; // current position in VirtReg
  93. SegmentIter LiveUnionI; // current position in LiveUnion
  94. SmallVector<LiveInterval*,4> InterferingVRegs;
  95. bool CheckedFirstInterference;
  96. bool SeenAllInterferences;
  97. bool SeenUnspillableVReg;
  98. unsigned Tag, UserTag;
  99. public:
  100. Query(): LiveUnion(), VirtReg(), Tag(0), UserTag(0) {}
  101. Query(LiveInterval *VReg, LiveIntervalUnion *LIU):
  102. LiveUnion(LIU), VirtReg(VReg), CheckedFirstInterference(false),
  103. SeenAllInterferences(false), SeenUnspillableVReg(false)
  104. {}
  105. void clear() {
  106. LiveUnion = nullptr;
  107. VirtReg = nullptr;
  108. InterferingVRegs.clear();
  109. CheckedFirstInterference = false;
  110. SeenAllInterferences = false;
  111. SeenUnspillableVReg = false;
  112. Tag = 0;
  113. UserTag = 0;
  114. }
  115. void init(unsigned UTag, LiveInterval *VReg, LiveIntervalUnion *LIU) {
  116. assert(VReg && LIU && "Invalid arguments");
  117. if (UserTag == UTag && VirtReg == VReg &&
  118. LiveUnion == LIU && !LIU->changedSince(Tag)) {
  119. // Retain cached results, e.g. firstInterference.
  120. return;
  121. }
  122. clear();
  123. LiveUnion = LIU;
  124. VirtReg = VReg;
  125. Tag = LIU->getTag();
  126. UserTag = UTag;
  127. }
  128. LiveInterval &virtReg() const {
  129. assert(VirtReg && "uninitialized");
  130. return *VirtReg;
  131. }
  132. // Does this live virtual register interfere with the union?
  133. bool checkInterference() { return collectInterferingVRegs(1); }
  134. // Count the virtual registers in this union that interfere with this
  135. // query's live virtual register, up to maxInterferingRegs.
  136. unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX);
  137. // Was this virtual register visited during collectInterferingVRegs?
  138. bool isSeenInterference(LiveInterval *VReg) const;
  139. // Did collectInterferingVRegs collect all interferences?
  140. bool seenAllInterferences() const { return SeenAllInterferences; }
  141. // Did collectInterferingVRegs encounter an unspillable vreg?
  142. bool seenUnspillableVReg() const { return SeenUnspillableVReg; }
  143. // Vector generated by collectInterferingVRegs.
  144. const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
  145. return InterferingVRegs;
  146. }
  147. private:
  148. Query(const Query&) = delete;
  149. void operator=(const Query&) = delete;
  150. };
  151. // Array of LiveIntervalUnions.
  152. class Array {
  153. unsigned Size;
  154. LiveIntervalUnion *LIUs;
  155. public:
  156. Array() : Size(0), LIUs(nullptr) {}
  157. ~Array() { clear(); }
  158. // Initialize the array to have Size entries.
  159. // Reuse an existing allocation if the size matches.
  160. void init(LiveIntervalUnion::Allocator&, unsigned Size);
  161. unsigned size() const { return Size; }
  162. void clear();
  163. LiveIntervalUnion& operator[](unsigned idx) {
  164. assert(idx < Size && "idx out of bounds");
  165. return LIUs[idx];
  166. }
  167. const LiveIntervalUnion& operator[](unsigned Idx) const {
  168. assert(Idx < Size && "Idx out of bounds");
  169. return LIUs[Idx];
  170. }
  171. };
  172. };
  173. } // end namespace llvm
  174. #endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION_H)