LLVM 24.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
24using namespace llvm;
25
26#define DEBUG_TYPE "regalloc"
27
28// Merge a LiveInterval's segments. Guarantee no overlaps.
30 const LiveRange &Range) {
31 if (Range.empty())
32 return;
33 ++Tag;
34
35 // Insert each of the virtual register's live segments into the map.
36 LiveRange::const_iterator RegPos = Range.begin();
37 LiveRange::const_iterator RegEnd = Range.end();
38 SegmentIter SegPos = Segments.find(RegPos->start);
39
40 while (SegPos.valid()) {
41 SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
42 if (++RegPos == RegEnd)
43 return;
44 SegPos.advanceTo(RegPos->start);
45 }
46
47 // We have reached the end of Segments, so it is no longer necessary to search
48 // for the insertion position.
49 // It is faster to insert the end first.
50 --RegEnd;
51 SegPos.insert(RegEnd->start, RegEnd->end, &VirtReg);
52 for (; RegPos != RegEnd; ++RegPos, ++SegPos)
53 SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
54}
55
56// Remove a live virtual register's segments from this union.
58 const LiveRange &Range) {
59 if (Range.empty())
60 return;
61 ++Tag;
62
63 // Remove each of the virtual register's live segments from the map.
64 LiveRange::const_iterator RegPos = Range.begin();
65 LiveRange::const_iterator RegEnd = Range.end();
66 SegmentIter SegPos = Segments.find(RegPos->start);
67
68 while (true) {
69 assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
70 SegPos.erase();
71 if (!SegPos.valid())
72 return;
73
74 // Skip all segments that may have been coalesced.
75 RegPos = Range.advanceTo(RegPos, SegPos.start());
76 if (RegPos == RegEnd)
77 return;
78
79 SegPos.advanceTo(RegPos->start);
80 }
81}
82
84 const LiveInterval &VirtRegLI) {
85 ++Tag;
86
87 // Remove all segments referencing VirtReg.
88 for (SegmentIter SegPos = Segments.begin(); SegPos.valid();) {
89 if (SegPos.value()->reg() == VirtRegLI.reg())
90 SegPos.erase();
91 else
92 ++SegPos;
93 }
94}
95
96void
98 if (empty()) {
99 OS << " empty\n";
100 return;
101 }
102 for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
103 OS << " [" << SI.start() << ' ' << SI.stop()
104 << "):" << printReg(SI.value()->reg(), TRI);
105 }
106 OS << '\n';
107}
108
109#ifndef NDEBUG
110// Verify the live intervals in this union and add them to the visited set.
112 for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
113 VisitedVRegs.set(SI.value()->reg().id());
114}
115#endif //!NDEBUG
116
118 if (empty())
119 return nullptr;
120 for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
121 // return the first valid live interval
122 return SI.value();
123 }
124 return nullptr;
125}
126
127// Scan the vector of interfering virtual registers in this union. Assume it's
128// quite small.
129bool LiveIntervalUnion::Query::isSeenInterference(
130 const LiveInterval *VirtReg) const {
131 return is_contained(InterferingVRegs, VirtReg);
132}
133
134// Collect virtual registers in this union that interfere with this
135// query's live virtual register.
136//
137// The query state is one of:
138//
139// 1. CheckedFirstInterference == false: Iterators are uninitialized.
140// 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused.
141// 3. Iterators left at the last seen intersection.
142//
143unsigned
144LiveIntervalUnion::Query::collectInterferingVRegs(unsigned MaxInterferingRegs) {
145 // Fast path return if we already have the desired information.
146 if (SeenAllInterferences || InterferingVRegs.size() >= MaxInterferingRegs)
147 return InterferingVRegs.size();
148
149 // Set up iterators on the first call.
150 if (!CheckedFirstInterference) {
151 CheckedFirstInterference = true;
152
153 // Quickly skip interference check for empty sets.
154 if (LR->empty() || LiveUnion->empty()) {
155 SeenAllInterferences = true;
156 return 0;
157 }
158
159 // In most cases, the union will start before LR.
160 LRI = LR->begin();
161 LiveUnionI.setMap(LiveUnion->getMap());
162 LiveUnionI.find(LRI->start);
163 }
164
165 LiveRange::const_iterator LREnd = LR->end();
166 const LiveInterval *RecentReg = nullptr;
167 while (LiveUnionI.valid()) {
168 assert(LRI != LREnd && "Reached end of LR");
169
170 // Check for overlapping interference.
171 while (LRI->start < LiveUnionI.stop() && LRI->end > LiveUnionI.start()) {
172 // This is an overlap, record the interfering register.
173 const LiveInterval *VReg = LiveUnionI.value();
174 if (VReg != RecentReg && !isSeenInterference(VReg)) {
175 RecentReg = VReg;
176 InterferingVRegs.push_back(VReg);
177 if (InterferingVRegs.size() >= MaxInterferingRegs)
178 return InterferingVRegs.size();
179 }
180 // This LiveUnion segment is no longer interesting.
181 if (!(++LiveUnionI).valid()) {
182 SeenAllInterferences = true;
183 return InterferingVRegs.size();
184 }
185 }
186
187 // The iterators are now not overlapping, LiveUnionI has been advanced
188 // beyond LRI.
189 assert(LRI->end <= LiveUnionI.start() && "Expected non-overlap");
190
191 // Advance the iterator that ends first.
192 LRI = LR->advanceTo(LRI, LiveUnionI.start());
193 if (LRI == LREnd)
194 break;
195
196 // Detect overlap, handle above.
197 if (LRI->start < LiveUnionI.stop())
198 continue;
199
200 // Still not overlapping. Catch up LiveUnionI.
201 LiveUnionI.advanceTo(LRI->start);
202 }
203 SeenAllInterferences = true;
204 return InterferingVRegs.size();
205}
206
208 unsigned NSize) {
209 // Reuse existing allocation.
210 if (NSize == Size)
211 return;
212 clear();
213 Size = NSize;
214 LIUs = static_cast<LiveIntervalUnion*>(
215 safe_malloc(sizeof(LiveIntervalUnion)*NSize));
216 for (unsigned i = 0; i != Size; ++i)
217 new(LIUs + i) LiveIntervalUnion(Alloc);
218}
219
221 if (!LIUs)
222 return;
223 for (unsigned i = 0; i != Size; ++i)
224 LIUs[i].~LiveIntervalUnion();
225 free(LIUs);
226 Size = 0;
227 LIUs = nullptr;
228}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Register const TargetRegisterInfo * TRI
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SparseBitVector class.
LLVM_ABI void init(LiveIntervalUnion::Allocator &, unsigned Size)
LLVM_ABI void unify(const LiveInterval &VirtReg, const LiveRange &Range)
LLVM_ABI const LiveInterval * getOneVReg() const
NDEBUG.
LLVM_ABI void extract(const LiveInterval &VirtReg, const LiveRange &Range)
LiveSegments::iterator SegmentIter
void verify(LiveVirtRegBitSet &VisitedVRegs)
LLVM_ABI void print(raw_ostream &OS, const TargetRegisterInfo *TRI) const
LLVM_ABI void clearAllSegmentsReferencing(const LiveInterval &VirtRegLI)
LiveSegments::Allocator Allocator
LiveInterval - This class represents the liveness of a register, or stack slot.
Register reg() const
This class represents the liveness of a register, stack slot, etc.
Segments::const_iterator const_iterator
size_t size() const
void set(unsigned Idx)
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
SparseBitVector< 128 > LiveVirtRegBitSet
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition MemAlloc.h:25
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
LLVM_ABI 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.