Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
6.8 KiB

  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 LiveRange &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);
  71. // Remove a live virtual register's segments from this union.
  72. void extract(LiveInterval &VirtReg);
  73. // Remove all inserted virtual registers.
  74. void clear() { Segments.clear(); ++Tag; }
  75. // Print union, using TRI to translate register names
  76. void print(raw_ostream &OS, const TargetRegisterInfo *TRI) const;
  77. #ifndef NDEBUG
  78. // Verify the live intervals in this union and add them to the visited set.
  79. void verify(LiveVirtRegBitSet& VisitedVRegs);
  80. #endif
  81. /// Query interferences between a single live virtual register and a live
  82. /// interval union.
  83. class Query {
  84. LiveIntervalUnion *LiveUnion;
  85. LiveInterval *VirtReg;
  86. LiveInterval::iterator VirtRegI; // current position in VirtReg
  87. SegmentIter LiveUnionI; // current position in LiveUnion
  88. SmallVector<LiveInterval*,4> InterferingVRegs;
  89. bool CheckedFirstInterference;
  90. bool SeenAllInterferences;
  91. bool SeenUnspillableVReg;
  92. unsigned Tag, UserTag;
  93. public:
  94. Query(): LiveUnion(), VirtReg(), Tag(0), UserTag(0) {}
  95. Query(LiveInterval *VReg, LiveIntervalUnion *LIU):
  96. LiveUnion(LIU), VirtReg(VReg), CheckedFirstInterference(false),
  97. SeenAllInterferences(false), SeenUnspillableVReg(false)
  98. {}
  99. void clear() {
  100. LiveUnion = NULL;
  101. VirtReg = NULL;
  102. InterferingVRegs.clear();
  103. CheckedFirstInterference = false;
  104. SeenAllInterferences = false;
  105. SeenUnspillableVReg = false;
  106. Tag = 0;
  107. UserTag = 0;
  108. }
  109. void init(unsigned UTag, LiveInterval *VReg, LiveIntervalUnion *LIU) {
  110. assert(VReg && LIU && "Invalid arguments");
  111. if (UserTag == UTag && VirtReg == VReg &&
  112. LiveUnion == LIU && !LIU->changedSince(Tag)) {
  113. // Retain cached results, e.g. firstInterference.
  114. return;
  115. }
  116. clear();
  117. LiveUnion = LIU;
  118. VirtReg = VReg;
  119. Tag = LIU->getTag();
  120. UserTag = UTag;
  121. }
  122. LiveInterval &virtReg() const {
  123. assert(VirtReg && "uninitialized");
  124. return *VirtReg;
  125. }
  126. // Does this live virtual register interfere with the union?
  127. bool checkInterference() { return collectInterferingVRegs(1); }
  128. // Count the virtual registers in this union that interfere with this
  129. // query's live virtual register, up to maxInterferingRegs.
  130. unsigned collectInterferingVRegs(unsigned MaxInterferingRegs = UINT_MAX);
  131. // Was this virtual register visited during collectInterferingVRegs?
  132. bool isSeenInterference(LiveInterval *VReg) const;
  133. // Did collectInterferingVRegs collect all interferences?
  134. bool seenAllInterferences() const { return SeenAllInterferences; }
  135. // Did collectInterferingVRegs encounter an unspillable vreg?
  136. bool seenUnspillableVReg() const { return SeenUnspillableVReg; }
  137. // Vector generated by collectInterferingVRegs.
  138. const SmallVectorImpl<LiveInterval*> &interferingVRegs() const {
  139. return InterferingVRegs;
  140. }
  141. private:
  142. Query(const Query&) LLVM_DELETED_FUNCTION;
  143. void operator=(const Query&) LLVM_DELETED_FUNCTION;
  144. };
  145. // Array of LiveIntervalUnions.
  146. class Array {
  147. unsigned Size;
  148. LiveIntervalUnion *LIUs;
  149. public:
  150. Array() : Size(0), LIUs(0) {}
  151. ~Array() { clear(); }
  152. // Initialize the array to have Size entries.
  153. // Reuse an existing allocation if the size matches.
  154. void init(LiveIntervalUnion::Allocator&, unsigned Size);
  155. unsigned size() const { return Size; }
  156. void clear();
  157. LiveIntervalUnion& operator[](unsigned idx) {
  158. assert(idx < Size && "idx out of bounds");
  159. return LIUs[idx];
  160. }
  161. };
  162. };
  163. } // end namespace llvm
  164. #endif // !defined(LLVM_CODEGEN_LIVEINTERVALUNION_H)