blob: 7b50dac4cd1a78bd33899774b223bb3be24c63a7 [file] [log] [blame]
Eugene Zelenkof1933322017-09-22 23:46:57 +00001//===- InterferenceCache.cpp - Caching per-block interference -------------===//
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// InterferenceCache remembers per-block interference in LiveIntervalUnions.
10//
11//===----------------------------------------------------------------------===//
12
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000013#include "InterferenceCache.h"
Eugene Zelenkof1933322017-09-22 23:46:57 +000014#include "llvm/ADT/ArrayRef.h"
15#include "llvm/CodeGen/LiveInterval.h"
Eugene Zelenkof1933322017-09-22 23:46:57 +000016#include "llvm/CodeGen/LiveIntervalUnion.h"
Matthias Braunf8422972017-12-13 02:51:04 +000017#include "llvm/CodeGen/LiveIntervals.h"
Eugene Zelenkof1933322017-09-22 23:46:57 +000018#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineOperand.h"
21#include "llvm/CodeGen/SlotIndexes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000022#include "llvm/CodeGen/TargetRegisterInfo.h"
Eugene Zelenkof1933322017-09-22 23:46:57 +000023#include "llvm/MC/MCRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/ErrorHandling.h"
Eugene Zelenkof1933322017-09-22 23:46:57 +000025#include <cassert>
26#include <cstdint>
27#include <cstdlib>
28#include <tuple>
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000029
30using namespace llvm;
31
Chandler Carruth1b9dde02014-04-22 02:02:50 +000032#define DEBUG_TYPE "regalloc"
33
Jakob Stoklund Olesencacefc72011-07-23 03:10:17 +000034// Static member used for null interference cursors.
Benjamin Kramer57a3d082015-03-08 16:07:39 +000035const InterferenceCache::BlockInterference
36 InterferenceCache::Cursor::NoInterference;
Jakob Stoklund Olesencacefc72011-07-23 03:10:17 +000037
Puyan Lotfi5eb10042014-02-06 09:23:24 +000038// Initializes PhysRegEntries (instead of a SmallVector, PhysRegEntries is a
39// buffer of size NumPhysRegs to speed up alloc/clear for targets with large
40// reg files). Calloced memory is used for good form, and quites tools like
41// Valgrind too, but zero initialized memory is not required by the algorithm:
42// this is because PhysRegEntries works like a SparseSet and its entries are
43// only valid when there is a corresponding CacheEntries assignment. There is
44// also support for when pass managers are reused for targets with different
45// numbers of PhysRegs: in this case PhysRegEntries is freed and reinitialized.
46void InterferenceCache::reinitPhysRegEntries() {
47 if (PhysRegEntriesCount == TRI->getNumRegs()) return;
48 free(PhysRegEntries);
49 PhysRegEntriesCount = TRI->getNumRegs();
Serge Pavlov76d8cce2018-02-20 05:41:26 +000050 PhysRegEntries = static_cast<unsigned char*>(
51 safe_calloc(PhysRegEntriesCount, sizeof(unsigned char)));
Puyan Lotfi5eb10042014-02-06 09:23:24 +000052}
53
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000054void InterferenceCache::init(MachineFunction *mf,
55 LiveIntervalUnion *liuarray,
56 SlotIndexes *indexes,
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000057 LiveIntervals *lis,
Jakob Stoklund Olesencacefc72011-07-23 03:10:17 +000058 const TargetRegisterInfo *tri) {
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000059 MF = mf;
60 LIUArray = liuarray;
61 TRI = tri;
Puyan Lotfi5eb10042014-02-06 09:23:24 +000062 reinitPhysRegEntries();
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000063 for (unsigned i = 0; i != CacheEntries; ++i)
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000064 Entries[i].clear(mf, indexes, lis);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000065}
66
67InterferenceCache::Entry *InterferenceCache::get(unsigned PhysReg) {
68 unsigned E = PhysRegEntries[PhysReg];
69 if (E < CacheEntries && Entries[E].getPhysReg() == PhysReg) {
70 if (!Entries[E].valid(LIUArray, TRI))
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000071 Entries[E].revalidate(LIUArray, TRI);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000072 return &Entries[E];
73 }
74 // No valid entry exists, pick the next round-robin entry.
75 E = RoundRobin;
76 if (++RoundRobin == CacheEntries)
77 RoundRobin = 0;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +000078 for (unsigned i = 0; i != CacheEntries; ++i) {
79 // Skip entries that are in use.
80 if (Entries[E].hasRefs()) {
81 if (++E == CacheEntries)
82 E = 0;
83 continue;
84 }
85 Entries[E].reset(PhysReg, LIUArray, TRI, MF);
86 PhysRegEntries[PhysReg] = E;
87 return &Entries[E];
88 }
89 llvm_unreachable("Ran out of interference cache entries.");
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000090}
91
92/// revalidate - LIU contents have changed, update tags.
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000093void InterferenceCache::Entry::revalidate(LiveIntervalUnion *LIUArray,
94 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000095 // Invalidate all block entries.
96 ++Tag;
97 // Invalidate all iterators.
98 PrevPos = SlotIndex();
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000099 unsigned i = 0;
100 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i)
101 RegUnits[i].VirtTag = LIUArray[*Units].getTag();
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000102}
103
104void InterferenceCache::Entry::reset(unsigned physReg,
105 LiveIntervalUnion *LIUArray,
106 const TargetRegisterInfo *TRI,
107 const MachineFunction *MF) {
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000108 assert(!hasRefs() && "Cannot reset cache entry with references");
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000109 // LIU's changed, invalidate cache.
110 ++Tag;
111 PhysReg = physReg;
112 Blocks.resize(MF->getNumBlockIDs());
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000113
114 // Reset iterators.
115 PrevPos = SlotIndex();
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000116 RegUnits.clear();
117 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
118 RegUnits.push_back(LIUArray[*Units]);
119 RegUnits.back().Fixed = &LIS->getRegUnit(*Units);
120 }
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000121}
122
123bool InterferenceCache::Entry::valid(LiveIntervalUnion *LIUArray,
124 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000125 unsigned i = 0, e = RegUnits.size();
126 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i) {
127 if (i == e)
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000128 return false;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000129 if (LIUArray[*Units].changedSince(RegUnits[i].VirtTag))
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000130 return false;
131 }
132 return i == e;
133}
134
135void InterferenceCache::Entry::update(unsigned MBBNum) {
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000136 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000137 std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000138
139 // Use advanceTo only when possible.
Jakob Stoklund Olesen994c1682011-04-07 17:27:50 +0000140 if (PrevPos != Start) {
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000141 if (!PrevPos.isValid() || Start < PrevPos) {
142 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
143 RegUnitInfo &RUI = RegUnits[i];
144 RUI.VirtI.find(Start);
145 RUI.FixedI = RUI.Fixed->find(Start);
146 }
147 } else {
148 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
149 RegUnitInfo &RUI = RegUnits[i];
150 RUI.VirtI.advanceTo(Start);
151 if (RUI.FixedI != RUI.Fixed->end())
152 RUI.FixedI = RUI.Fixed->advanceTo(RUI.FixedI, Start);
153 }
154 }
Jakob Stoklund Olesen994c1682011-04-07 17:27:50 +0000155 PrevPos = Start;
156 }
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000157
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000158 MachineFunction::const_iterator MFI =
159 MF->getBlockNumbered(MBBNum)->getIterator();
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000160 BlockInterference *BI = &Blocks[MBBNum];
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000161 ArrayRef<SlotIndex> RegMaskSlots;
162 ArrayRef<const uint32_t*> RegMaskBits;
Eugene Zelenkof1933322017-09-22 23:46:57 +0000163 while (true) {
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000164 BI->Tag = Tag;
165 BI->First = BI->Last = SlotIndex();
166
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000167 // Check for first interference from virtregs.
168 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
169 LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000170 if (!I.valid())
171 continue;
172 SlotIndex StartI = I.start();
173 if (StartI >= Stop)
174 continue;
175 if (!BI->First.isValid() || StartI < BI->First)
176 BI->First = StartI;
177 }
178
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000179 // Same thing for fixed interference.
180 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
181 LiveInterval::const_iterator I = RegUnits[i].FixedI;
182 LiveInterval::const_iterator E = RegUnits[i].Fixed->end();
183 if (I == E)
184 continue;
185 SlotIndex StartI = I->start;
186 if (StartI >= Stop)
187 continue;
188 if (!BI->First.isValid() || StartI < BI->First)
189 BI->First = StartI;
190 }
191
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000192 // Also check for register mask interference.
193 RegMaskSlots = LIS->getRegMaskSlotsInBlock(MBBNum);
194 RegMaskBits = LIS->getRegMaskBitsInBlock(MBBNum);
195 SlotIndex Limit = BI->First.isValid() ? BI->First : Stop;
196 for (unsigned i = 0, e = RegMaskSlots.size();
197 i != e && RegMaskSlots[i] < Limit; ++i)
Jakob Stoklund Olesen024d7ae2012-02-10 19:23:53 +0000198 if (MachineOperand::clobbersPhysReg(RegMaskBits[i], PhysReg)) {
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000199 // Register mask i clobbers PhysReg before the LIU interference.
200 BI->First = RegMaskSlots[i];
201 break;
202 }
203
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000204 PrevPos = Stop;
205 if (BI->First.isValid())
206 break;
207
208 // No interference in this block? Go ahead and precompute the next block.
209 if (++MFI == MF->end())
210 return;
211 MBBNum = MFI->getNumber();
212 BI = &Blocks[MBBNum];
213 if (BI->Tag == Tag)
214 return;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000215 std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000216 }
217
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000218 // Check for last interference in block.
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000219 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
220 LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000221 if (!I.valid() || I.start() >= Stop)
222 continue;
223 I.advanceTo(Stop);
Jakob Stoklund Olesen994c1682011-04-07 17:27:50 +0000224 bool Backup = !I.valid() || I.start() >= Stop;
225 if (Backup)
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000226 --I;
227 SlotIndex StopI = I.stop();
228 if (!BI->Last.isValid() || StopI > BI->Last)
229 BI->Last = StopI;
Jakob Stoklund Olesen994c1682011-04-07 17:27:50 +0000230 if (Backup)
231 ++I;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000232 }
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000233
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000234 // Fixed interference.
235 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
236 LiveInterval::iterator &I = RegUnits[i].FixedI;
Matthias Braun34e1be92013-10-10 21:29:02 +0000237 LiveRange *LR = RegUnits[i].Fixed;
238 if (I == LR->end() || I->start >= Stop)
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000239 continue;
Matthias Braun34e1be92013-10-10 21:29:02 +0000240 I = LR->advanceTo(I, Stop);
241 bool Backup = I == LR->end() || I->start >= Stop;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000242 if (Backup)
243 --I;
244 SlotIndex StopI = I->end;
245 if (!BI->Last.isValid() || StopI > BI->Last)
246 BI->Last = StopI;
247 if (Backup)
248 ++I;
249 }
250
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000251 // Also check for register mask interference.
252 SlotIndex Limit = BI->Last.isValid() ? BI->Last : Start;
Jakob Stoklund Olesenc4cf13f2012-02-14 23:53:23 +0000253 for (unsigned i = RegMaskSlots.size();
254 i && RegMaskSlots[i-1].getDeadSlot() > Limit; --i)
Jakob Stoklund Olesen024d7ae2012-02-10 19:23:53 +0000255 if (MachineOperand::clobbersPhysReg(RegMaskBits[i-1], PhysReg)) {
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000256 // Register mask i-1 clobbers PhysReg after the LIU interference.
257 // Model the regmask clobber as a dead def.
258 BI->Last = RegMaskSlots[i-1].getDeadSlot();
259 break;
260 }
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000261}