blob: 36428e0335f945c38c52502368b02f78a4ed1029 [file] [log] [blame]
Eugene Zelenkoad3a5402017-02-17 21:43:25 +00001//===- LiveIntervalUnion.cpp - Live interval union data structure ---------===//
Andrew Trick14e8d712010-10-22 23:09:15 +00002//
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 represents a coalesced set of live intervals. This may be
11// used during coalescing to represent a congruence class, or during register
12// allocation to model liveness of a physical register.
13//
14//===----------------------------------------------------------------------===//
15
Eugene Zelenkoad3a5402017-02-17 21:43:25 +000016#include "llvm/CodeGen/LiveIntervalUnion.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SparseBitVector.h"
19#include "llvm/CodeGen/LiveInterval.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000020#include "llvm/CodeGen/TargetRegisterInfo.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000021#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoad3a5402017-02-17 21:43:25 +000022#include <cassert>
23#include <cstdlib>
Lang Hamesb638c782011-12-21 20:16:11 +000024
Andrew Trick14e8d712010-10-22 23:09:15 +000025using namespace llvm;
26
Chandler Carruth8677f2f2014-04-22 02:02:50 +000027#define DEBUG_TYPE "regalloc"
28
Andrew Trick14e8d712010-10-22 23:09:15 +000029// Merge a LiveInterval's segments. Guarantee no overlaps.
Matthias Braun7b54b4d2014-12-10 01:12:59 +000030void LiveIntervalUnion::unify(LiveInterval &VirtReg, const LiveRange &Range) {
31 if (Range.empty())
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000032 return;
Jakob Stoklund Olesen4f6364f2011-02-09 21:52:03 +000033 ++Tag;
Andrew Trick18c57a82010-11-30 23:18:47 +000034
35 // Insert each of the virtual register's live segments into the map.
Matthias Braun7b54b4d2014-12-10 01:12:59 +000036 LiveRange::const_iterator RegPos = Range.begin();
37 LiveRange::const_iterator RegEnd = Range.end();
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000038 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000039
Jakob Stoklund Olesen11983cd2011-04-11 15:00:44 +000040 while (SegPos.valid()) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000041 SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
42 if (++RegPos == RegEnd)
43 return;
44 SegPos.advanceTo(RegPos->start);
Andrew Trick14e8d712010-10-22 23:09:15 +000045 }
Jakob Stoklund Olesen11983cd2011-04-11 15:00:44 +000046
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);
Andrew Trick14e8d712010-10-22 23:09:15 +000054}
55
Andrew Tricke141a492010-11-08 18:02:08 +000056// Remove a live virtual register's segments from this union.
Matthias Braun7b54b4d2014-12-10 01:12:59 +000057void LiveIntervalUnion::extract(LiveInterval &VirtReg, const LiveRange &Range) {
58 if (Range.empty())
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000059 return;
Jakob Stoklund Olesen4f6364f2011-02-09 21:52:03 +000060 ++Tag;
Andrew Trick18c57a82010-11-30 23:18:47 +000061
Andrew Tricke141a492010-11-08 18:02:08 +000062 // Remove each of the virtual register's live segments from the map.
Matthias Braun7b54b4d2014-12-10 01:12:59 +000063 LiveRange::const_iterator RegPos = Range.begin();
64 LiveRange::const_iterator RegEnd = Range.end();
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000065 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000066
Eugene Zelenkoad3a5402017-02-17 21:43:25 +000067 while (true) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000068 assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
69 SegPos.erase();
70 if (!SegPos.valid())
71 return;
Andrew Trick18c57a82010-11-30 23:18:47 +000072
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000073 // Skip all segments that may have been coalesced.
Matthias Braun7b54b4d2014-12-10 01:12:59 +000074 RegPos = Range.advanceTo(RegPos, SegPos.start());
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000075 if (RegPos == RegEnd)
76 return;
77
78 SegPos.advanceTo(RegPos->start);
Andrew Trick14e8d712010-10-22 23:09:15 +000079 }
Andrew Trick14e8d712010-10-22 23:09:15 +000080}
Andrew Trick14e8d712010-10-22 23:09:15 +000081
Andrew Trick071d1c02010-11-09 21:04:34 +000082void
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000083LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000084 if (empty()) {
85 OS << " empty\n";
86 return;
87 }
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000088 for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +000089 OS << " [" << SI.start() << ' ' << SI.stop() << "):"
Francis Visoiu Mistrihaccb3372017-11-28 12:42:37 +000090 << printReg(SI.value()->reg, TRI);
Andrew Trick071d1c02010-11-09 21:04:34 +000091 }
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000092 OS << '\n';
93}
94
Andrew Trick071d1c02010-11-09 21:04:34 +000095#ifndef NDEBUG
96// Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +000097void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000098 for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
99 VisitedVRegs.set(SI.value()->reg);
Andrew Trick071d1c02010-11-09 21:04:34 +0000100}
101#endif //!NDEBUG
102
Andrew Trick18c57a82010-11-30 23:18:47 +0000103// Scan the vector of interfering virtual registers in this union. Assume it's
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000104// quite small.
Andrew Trick18c57a82010-11-30 23:18:47 +0000105bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
David Majnemer975248e2016-08-11 22:21:41 +0000106 return is_contained(InterferingVRegs, VirtReg);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000107}
108
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000109// Collect virtual registers in this union that interfere with this
Andrew Trick18c57a82010-11-30 23:18:47 +0000110// query's live virtual register.
111//
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000112// The query state is one of:
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000113//
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000114// 1. CheckedFirstInterference == false: Iterators are uninitialized.
115// 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused.
116// 3. Iterators left at the last seen intersection.
117//
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000118unsigned LiveIntervalUnion::Query::
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000119collectInterferingVRegs(unsigned MaxInterferingRegs) {
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000120 // Fast path return if we already have the desired information.
121 if (SeenAllInterferences || InterferingVRegs.size() >= MaxInterferingRegs)
Jakob Stoklund Olesen9942ba92011-08-11 21:18:34 +0000122 return InterferingVRegs.size();
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000123
124 // Set up iterators on the first call.
125 if (!CheckedFirstInterference) {
126 CheckedFirstInterference = true;
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000127
128 // Quickly skip interference check for empty sets.
Matthias Brauna9e2ca02017-03-01 21:48:12 +0000129 if (LR->empty() || LiveUnion->empty()) {
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000130 SeenAllInterferences = true;
131 return 0;
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000132 }
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000133
Matthias Brauna9e2ca02017-03-01 21:48:12 +0000134 // In most cases, the union will start before LR.
135 LRI = LR->begin();
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000136 LiveUnionI.setMap(LiveUnion->getMap());
Matthias Brauna9e2ca02017-03-01 21:48:12 +0000137 LiveUnionI.find(LRI->start);
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000138 }
139
Matthias Brauna9e2ca02017-03-01 21:48:12 +0000140 LiveRange::const_iterator LREnd = LR->end();
Craig Topper4ba84432014-04-14 00:51:57 +0000141 LiveInterval *RecentReg = nullptr;
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000142 while (LiveUnionI.valid()) {
Matthias Brauna9e2ca02017-03-01 21:48:12 +0000143 assert(LRI != LREnd && "Reached end of LR");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000144
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000145 // Check for overlapping interference.
Matthias Brauna9e2ca02017-03-01 21:48:12 +0000146 while (LRI->start < LiveUnionI.stop() && LRI->end > LiveUnionI.start()) {
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000147 // This is an overlap, record the interfering register.
148 LiveInterval *VReg = LiveUnionI.value();
149 if (VReg != RecentReg && !isSeenInterference(VReg)) {
150 RecentReg = VReg;
151 InterferingVRegs.push_back(VReg);
152 if (InterferingVRegs.size() >= MaxInterferingRegs)
153 return InterferingVRegs.size();
154 }
155 // This LiveUnion segment is no longer interesting.
156 if (!(++LiveUnionI).valid()) {
157 SeenAllInterferences = true;
158 return InterferingVRegs.size();
159 }
160 }
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000161
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000162 // The iterators are now not overlapping, LiveUnionI has been advanced
Matthias Brauna9e2ca02017-03-01 21:48:12 +0000163 // beyond LRI.
164 assert(LRI->end <= LiveUnionI.start() && "Expected non-overlap");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000165
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000166 // Advance the iterator that ends first.
Matthias Brauna9e2ca02017-03-01 21:48:12 +0000167 LRI = LR->advanceTo(LRI, LiveUnionI.start());
168 if (LRI == LREnd)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000169 break;
170
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000171 // Detect overlap, handle above.
Matthias Brauna9e2ca02017-03-01 21:48:12 +0000172 if (LRI->start < LiveUnionI.stop())
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000173 continue;
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000174
175 // Still not overlapping. Catch up LiveUnionI.
Matthias Brauna9e2ca02017-03-01 21:48:12 +0000176 LiveUnionI.advanceTo(LRI->start);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000177 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000178 SeenAllInterferences = true;
179 return InterferingVRegs.size();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000180}
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +0000181
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000182void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator &Alloc,
183 unsigned NSize) {
184 // Reuse existing allocation.
185 if (NSize == Size)
186 return;
187 clear();
188 Size = NSize;
189 LIUs = static_cast<LiveIntervalUnion*>(
Serge Pavlov06c71d82018-02-20 05:41:26 +0000190 safe_malloc(sizeof(LiveIntervalUnion)*NSize));
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000191 for (unsigned i = 0; i != Size; ++i)
192 new(LIUs + i) LiveIntervalUnion(Alloc);
193}
194
195void LiveIntervalUnion::Array::clear() {
196 if (!LIUs)
197 return;
198 for (unsigned i = 0; i != Size; ++i)
199 LIUs[i].~LiveIntervalUnion();
200 free(LIUs);
201 Size = 0;
Craig Topper4ba84432014-04-14 00:51:57 +0000202 LIUs = nullptr;
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000203}