LLVM  14.0.0git
LiveIntervalUnion.cpp
Go to the documentation of this file.
1 //===- LiveIntervalUnion.cpp - Live interval union data structure ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // LiveIntervalUnion represents a coalesced set of live intervals. This may be
10 // used during coalescing to represent a congruence class, or during register
11 // allocation to model liveness of a physical register.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/ADT/STLExtras.h"
21 #include <cassert>
22 #include <cstdlib>
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "regalloc"
27 
28 // Merge a LiveInterval's segments. Guarantee no overlaps.
29 void LiveIntervalUnion::unify(LiveInterval &VirtReg, const LiveRange &Range) {
30  if (Range.empty())
31  return;
32  ++Tag;
33 
34  // Insert each of the virtual register's live segments into the map.
35  LiveRange::const_iterator RegPos = Range.begin();
36  LiveRange::const_iterator RegEnd = Range.end();
37  SegmentIter SegPos = Segments.find(RegPos->start);
38 
39  while (SegPos.valid()) {
40  SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
41  if (++RegPos == RegEnd)
42  return;
43  SegPos.advanceTo(RegPos->start);
44  }
45 
46  // We have reached the end of Segments, so it is no longer necessary to search
47  // for the insertion position.
48  // It is faster to insert the end first.
49  --RegEnd;
50  SegPos.insert(RegEnd->start, RegEnd->end, &VirtReg);
51  for (; RegPos != RegEnd; ++RegPos, ++SegPos)
52  SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
53 }
54 
55 // Remove a live virtual register's segments from this union.
56 void LiveIntervalUnion::extract(LiveInterval &VirtReg, const LiveRange &Range) {
57  if (Range.empty())
58  return;
59  ++Tag;
60 
61  // Remove each of the virtual register's live segments from the map.
62  LiveRange::const_iterator RegPos = Range.begin();
63  LiveRange::const_iterator RegEnd = Range.end();
64  SegmentIter SegPos = Segments.find(RegPos->start);
65 
66  while (true) {
67  assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
68  SegPos.erase();
69  if (!SegPos.valid())
70  return;
71 
72  // Skip all segments that may have been coalesced.
73  RegPos = Range.advanceTo(RegPos, SegPos.start());
74  if (RegPos == RegEnd)
75  return;
76 
77  SegPos.advanceTo(RegPos->start);
78  }
79 }
80 
81 void
83  if (empty()) {
84  OS << " empty\n";
85  return;
86  }
87  for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
88  OS << " [" << SI.start() << ' ' << SI.stop()
89  << "):" << printReg(SI.value()->reg(), TRI);
90  }
91  OS << '\n';
92 }
93 
94 #ifndef NDEBUG
95 // Verify the live intervals in this union and add them to the visited set.
97  for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
98  VisitedVRegs.set(SI.value()->reg());
99 }
100 #endif //!NDEBUG
101 
103  if (empty())
104  return nullptr;
105  for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
106  // return the first valid live interval
107  return SI.value();
108  }
109  return nullptr;
110 }
111 
112 // Scan the vector of interfering virtual registers in this union. Assume it's
113 // quite small.
115  return is_contained(*InterferingVRegs, VirtReg);
116 }
117 
118 // Collect virtual registers in this union that interfere with this
119 // query's live virtual register.
120 //
121 // The query state is one of:
122 //
123 // 1. CheckedFirstInterference == false: Iterators are uninitialized.
124 // 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused.
125 // 3. Iterators left at the last seen intersection.
126 //
128 collectInterferingVRegs(unsigned MaxInterferingRegs) {
129  if (!InterferingVRegs)
130  InterferingVRegs.emplace();
131 
132  // Fast path return if we already have the desired information.
133  if (SeenAllInterferences || InterferingVRegs->size() >= MaxInterferingRegs)
134  return InterferingVRegs->size();
135 
136  // Set up iterators on the first call.
137  if (!CheckedFirstInterference) {
138  CheckedFirstInterference = true;
139 
140  // Quickly skip interference check for empty sets.
141  if (LR->empty() || LiveUnion->empty()) {
142  SeenAllInterferences = true;
143  return 0;
144  }
145 
146  // In most cases, the union will start before LR.
147  LRI = LR->begin();
148  LiveUnionI.setMap(LiveUnion->getMap());
149  LiveUnionI.find(LRI->start);
150  }
151 
152  LiveRange::const_iterator LREnd = LR->end();
153  LiveInterval *RecentReg = nullptr;
154  while (LiveUnionI.valid()) {
155  assert(LRI != LREnd && "Reached end of LR");
156 
157  // Check for overlapping interference.
158  while (LRI->start < LiveUnionI.stop() && LRI->end > LiveUnionI.start()) {
159  // This is an overlap, record the interfering register.
160  LiveInterval *VReg = LiveUnionI.value();
161  if (VReg != RecentReg && !isSeenInterference(VReg)) {
162  RecentReg = VReg;
163  InterferingVRegs->push_back(VReg);
164  if (InterferingVRegs->size() >= MaxInterferingRegs)
165  return InterferingVRegs->size();
166  }
167  // This LiveUnion segment is no longer interesting.
168  if (!(++LiveUnionI).valid()) {
169  SeenAllInterferences = true;
170  return InterferingVRegs->size();
171  }
172  }
173 
174  // The iterators are now not overlapping, LiveUnionI has been advanced
175  // beyond LRI.
176  assert(LRI->end <= LiveUnionI.start() && "Expected non-overlap");
177 
178  // Advance the iterator that ends first.
179  LRI = LR->advanceTo(LRI, LiveUnionI.start());
180  if (LRI == LREnd)
181  break;
182 
183  // Detect overlap, handle above.
184  if (LRI->start < LiveUnionI.stop())
185  continue;
186 
187  // Still not overlapping. Catch up LiveUnionI.
188  LiveUnionI.advanceTo(LRI->start);
189  }
190  SeenAllInterferences = true;
191  return InterferingVRegs->size();
192 }
193 
195  unsigned NSize) {
196  // Reuse existing allocation.
197  if (NSize == Size)
198  return;
199  clear();
200  Size = NSize;
201  LIUs = static_cast<LiveIntervalUnion*>(
202  safe_malloc(sizeof(LiveIntervalUnion)*NSize));
203  for (unsigned i = 0; i != Size; ++i)
204  new(LIUs + i) LiveIntervalUnion(Alloc);
205 }
206 
208  if (!LIUs)
209  return;
210  for (unsigned i = 0; i != Size; ++i)
211  LIUs[i].~LiveIntervalUnion();
212  free(LIUs);
213  Size = 0;
214  LIUs = nullptr;
215 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
i
i
Definition: README.txt:29
llvm::LiveIntervalUnion::getOneVReg
LiveInterval * getOneVReg() const
NDEBUG.
Definition: LiveIntervalUnion.cpp:102
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::LiveRange::empty
bool empty() const
Definition: LiveInterval.h:374
llvm::LiveIntervalUnion::Query::isSeenInterference
bool isSeenInterference(LiveInterval *VirtReg) const
Definition: LiveIntervalUnion.cpp:114
llvm::LiveIntervalUnion::LiveIntervalUnion
LiveIntervalUnion(Allocator &a)
Definition: LiveIntervalUnion.h:65
llvm::LiveIntervalUnion::Array::clear
void clear()
Definition: LiveIntervalUnion.cpp:207
llvm::LiveRange::advanceTo
iterator advanceTo(iterator I, SlotIndex Pos)
advanceTo - Advance the specified iterator to point to the Segment containing the specified position,...
Definition: LiveInterval.h:263
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:233
STLExtras.h
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1567
llvm::LiveIntervalUnion::Allocator
LiveSegments::Allocator Allocator
Definition: LiveIntervalUnion.h:58
SparseBitVector.h
llvm::SparseBitVector
Definition: SparseBitVector.h:255
llvm::LiveRange::begin
iterator begin()
Definition: LiveInterval.h:215
llvm::LiveRange::const_iterator
Segments::const_iterator const_iterator
Definition: LiveInterval.h:213
LiveIntervalUnion.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::LiveRange::size
size_t size() const
Definition: LiveInterval.h:297
llvm::LiveInterval
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:680
llvm::LiveIntervalUnion::print
void print(raw_ostream &OS, const TargetRegisterInfo *TRI) const
Definition: LiveIntervalUnion.cpp:82
llvm::SparseBitVector::set
void set(unsigned Idx)
Definition: SparseBitVector.h:507
llvm::LiveRange
This class represents the liveness of a register, stack slot, etc.
Definition: LiveInterval.h:157
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1616
llvm::LiveIntervalUnion
Union of live intervals that are strong candidates for coalescing into a single register (either phys...
Definition: LiveIntervalUnion.h:42
llvm::LiveIntervalUnion::empty
bool empty() const
Definition: LiveIntervalUnion.h:76
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::IntervalMap::find
const_iterator find(KeyT x) const
find - Return an iterator pointing to the first interval ending at or after x, or end().
Definition: IntervalMap.h:1126
SI
StandardInstrumentations SI(Debug, VerifyEach)
llvm::LiveIntervalUnion::SegmentIter
LiveSegments::iterator SegmentIter
Definition: LiveIntervalUnion.h:52
llvm::LiveIntervalUnion::Array::init
void init(LiveIntervalUnion::Allocator &, unsigned Size)
Definition: LiveIntervalUnion.cpp:194
llvm::LiveIntervalUnion::extract
void extract(LiveInterval &VirtReg, const LiveRange &Range)
Definition: LiveIntervalUnion.cpp:56
llvm::LiveIntervalUnion::Query::collectInterferingVRegs
unsigned collectInterferingVRegs(unsigned MaxInterferingRegs=std::numeric_limits< unsigned >::max())
Definition: LiveIntervalUnion.cpp:128
llvm::LiveIntervalUnion::unify
void unify(LiveInterval &VirtReg, const LiveRange &Range)
Definition: LiveIntervalUnion.cpp:29
LiveInterval.h
llvm::LiveIntervalUnion::verify
void verify(LiveVirtRegBitSet &VisitedVRegs)
Definition: LiveIntervalUnion.cpp:96
llvm::IntervalMap::begin
const_iterator begin() const
Definition: IntervalMap.h:1100
llvm::LiveIntervalUnion::clear
void clear()
Definition: LiveIntervalUnion.h:97
llvm::IntervalMap< SlotIndex, LiveInterval * >::const_iterator
friend class const_iterator
Definition: IntervalMap.h:1096
raw_ostream.h
llvm::printReg
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
Definition: TargetRegisterInfo.cpp:110
llvm::safe_malloc
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition: MemAlloc.h:25
TargetRegisterInfo.h
llvm::LiveRange::end
iterator end()
Definition: LiveInterval.h:216