blob: f8cc24724580a1492ee06ef79ae0394670c79ccf [file] [log] [blame]
Andrew Trickf35c8402012-01-13 22:04:16 +00001//===-- InterferenceCache.cpp - Caching per-block interference ---------*--===//
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +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// InterferenceCache remembers per-block interference in LiveIntervalUnions.
11//
12//===----------------------------------------------------------------------===//
13
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000014#include "InterferenceCache.h"
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000015#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Support/ErrorHandling.h"
17#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000018
19using namespace llvm;
20
Chandler Carruth1b9dde02014-04-22 02:02:50 +000021#define DEBUG_TYPE "regalloc"
22
Jakob Stoklund Olesencacefc72011-07-23 03:10:17 +000023// Static member used for null interference cursors.
Benjamin Kramer57a3d082015-03-08 16:07:39 +000024const InterferenceCache::BlockInterference
25 InterferenceCache::Cursor::NoInterference;
Jakob Stoklund Olesencacefc72011-07-23 03:10:17 +000026
Puyan Lotfi5eb10042014-02-06 09:23:24 +000027// Initializes PhysRegEntries (instead of a SmallVector, PhysRegEntries is a
28// buffer of size NumPhysRegs to speed up alloc/clear for targets with large
29// reg files). Calloced memory is used for good form, and quites tools like
30// Valgrind too, but zero initialized memory is not required by the algorithm:
31// this is because PhysRegEntries works like a SparseSet and its entries are
32// only valid when there is a corresponding CacheEntries assignment. There is
33// also support for when pass managers are reused for targets with different
34// numbers of PhysRegs: in this case PhysRegEntries is freed and reinitialized.
35void InterferenceCache::reinitPhysRegEntries() {
36 if (PhysRegEntriesCount == TRI->getNumRegs()) return;
37 free(PhysRegEntries);
38 PhysRegEntriesCount = TRI->getNumRegs();
39 PhysRegEntries = (unsigned char*)
40 calloc(PhysRegEntriesCount, sizeof(unsigned char));
41}
42
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000043void InterferenceCache::init(MachineFunction *mf,
44 LiveIntervalUnion *liuarray,
45 SlotIndexes *indexes,
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000046 LiveIntervals *lis,
Jakob Stoklund Olesencacefc72011-07-23 03:10:17 +000047 const TargetRegisterInfo *tri) {
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000048 MF = mf;
49 LIUArray = liuarray;
50 TRI = tri;
Puyan Lotfi5eb10042014-02-06 09:23:24 +000051 reinitPhysRegEntries();
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000052 for (unsigned i = 0; i != CacheEntries; ++i)
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000053 Entries[i].clear(mf, indexes, lis);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000054}
55
56InterferenceCache::Entry *InterferenceCache::get(unsigned PhysReg) {
57 unsigned E = PhysRegEntries[PhysReg];
58 if (E < CacheEntries && Entries[E].getPhysReg() == PhysReg) {
59 if (!Entries[E].valid(LIUArray, TRI))
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000060 Entries[E].revalidate(LIUArray, TRI);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000061 return &Entries[E];
62 }
63 // No valid entry exists, pick the next round-robin entry.
64 E = RoundRobin;
65 if (++RoundRobin == CacheEntries)
66 RoundRobin = 0;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +000067 for (unsigned i = 0; i != CacheEntries; ++i) {
68 // Skip entries that are in use.
69 if (Entries[E].hasRefs()) {
70 if (++E == CacheEntries)
71 E = 0;
72 continue;
73 }
74 Entries[E].reset(PhysReg, LIUArray, TRI, MF);
75 PhysRegEntries[PhysReg] = E;
76 return &Entries[E];
77 }
78 llvm_unreachable("Ran out of interference cache entries.");
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000079}
80
81/// revalidate - LIU contents have changed, update tags.
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000082void InterferenceCache::Entry::revalidate(LiveIntervalUnion *LIUArray,
83 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000084 // Invalidate all block entries.
85 ++Tag;
86 // Invalidate all iterators.
87 PrevPos = SlotIndex();
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000088 unsigned i = 0;
89 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i)
90 RegUnits[i].VirtTag = LIUArray[*Units].getTag();
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000091}
92
93void InterferenceCache::Entry::reset(unsigned physReg,
94 LiveIntervalUnion *LIUArray,
95 const TargetRegisterInfo *TRI,
96 const MachineFunction *MF) {
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +000097 assert(!hasRefs() && "Cannot reset cache entry with references");
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000098 // LIU's changed, invalidate cache.
99 ++Tag;
100 PhysReg = physReg;
101 Blocks.resize(MF->getNumBlockIDs());
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000102
103 // Reset iterators.
104 PrevPos = SlotIndex();
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000105 RegUnits.clear();
106 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
107 RegUnits.push_back(LIUArray[*Units]);
108 RegUnits.back().Fixed = &LIS->getRegUnit(*Units);
109 }
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000110}
111
112bool InterferenceCache::Entry::valid(LiveIntervalUnion *LIUArray,
113 const TargetRegisterInfo *TRI) {
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000114 unsigned i = 0, e = RegUnits.size();
115 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i) {
116 if (i == e)
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000117 return false;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000118 if (LIUArray[*Units].changedSince(RegUnits[i].VirtTag))
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000119 return false;
120 }
121 return i == e;
122}
123
124void InterferenceCache::Entry::update(unsigned MBBNum) {
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000125 SlotIndex Start, Stop;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000126 std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000127
128 // Use advanceTo only when possible.
Jakob Stoklund Olesen994c1682011-04-07 17:27:50 +0000129 if (PrevPos != Start) {
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000130 if (!PrevPos.isValid() || Start < PrevPos) {
131 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
132 RegUnitInfo &RUI = RegUnits[i];
133 RUI.VirtI.find(Start);
134 RUI.FixedI = RUI.Fixed->find(Start);
135 }
136 } else {
137 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
138 RegUnitInfo &RUI = RegUnits[i];
139 RUI.VirtI.advanceTo(Start);
140 if (RUI.FixedI != RUI.Fixed->end())
141 RUI.FixedI = RUI.Fixed->advanceTo(RUI.FixedI, Start);
142 }
143 }
Jakob Stoklund Olesen994c1682011-04-07 17:27:50 +0000144 PrevPos = Start;
145 }
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000146
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000147 MachineFunction::const_iterator MFI =
148 MF->getBlockNumbered(MBBNum)->getIterator();
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000149 BlockInterference *BI = &Blocks[MBBNum];
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000150 ArrayRef<SlotIndex> RegMaskSlots;
151 ArrayRef<const uint32_t*> RegMaskBits;
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000152 for (;;) {
153 BI->Tag = Tag;
154 BI->First = BI->Last = SlotIndex();
155
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000156 // Check for first interference from virtregs.
157 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
158 LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000159 if (!I.valid())
160 continue;
161 SlotIndex StartI = I.start();
162 if (StartI >= Stop)
163 continue;
164 if (!BI->First.isValid() || StartI < BI->First)
165 BI->First = StartI;
166 }
167
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000168 // Same thing for fixed interference.
169 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
170 LiveInterval::const_iterator I = RegUnits[i].FixedI;
171 LiveInterval::const_iterator E = RegUnits[i].Fixed->end();
172 if (I == E)
173 continue;
174 SlotIndex StartI = I->start;
175 if (StartI >= Stop)
176 continue;
177 if (!BI->First.isValid() || StartI < BI->First)
178 BI->First = StartI;
179 }
180
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000181 // Also check for register mask interference.
182 RegMaskSlots = LIS->getRegMaskSlotsInBlock(MBBNum);
183 RegMaskBits = LIS->getRegMaskBitsInBlock(MBBNum);
184 SlotIndex Limit = BI->First.isValid() ? BI->First : Stop;
185 for (unsigned i = 0, e = RegMaskSlots.size();
186 i != e && RegMaskSlots[i] < Limit; ++i)
Jakob Stoklund Olesen024d7ae2012-02-10 19:23:53 +0000187 if (MachineOperand::clobbersPhysReg(RegMaskBits[i], PhysReg)) {
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000188 // Register mask i clobbers PhysReg before the LIU interference.
189 BI->First = RegMaskSlots[i];
190 break;
191 }
192
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000193 PrevPos = Stop;
194 if (BI->First.isValid())
195 break;
196
197 // No interference in this block? Go ahead and precompute the next block.
198 if (++MFI == MF->end())
199 return;
200 MBBNum = MFI->getNumber();
201 BI = &Blocks[MBBNum];
202 if (BI->Tag == Tag)
203 return;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000204 std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000205 }
206
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000207 // Check for last interference in block.
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000208 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
209 LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000210 if (!I.valid() || I.start() >= Stop)
211 continue;
212 I.advanceTo(Stop);
Jakob Stoklund Olesen994c1682011-04-07 17:27:50 +0000213 bool Backup = !I.valid() || I.start() >= Stop;
214 if (Backup)
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000215 --I;
216 SlotIndex StopI = I.stop();
217 if (!BI->Last.isValid() || StopI > BI->Last)
218 BI->Last = StopI;
Jakob Stoklund Olesen994c1682011-04-07 17:27:50 +0000219 if (Backup)
220 ++I;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000221 }
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000222
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000223 // Fixed interference.
224 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
225 LiveInterval::iterator &I = RegUnits[i].FixedI;
Matthias Braun34e1be92013-10-10 21:29:02 +0000226 LiveRange *LR = RegUnits[i].Fixed;
227 if (I == LR->end() || I->start >= Stop)
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000228 continue;
Matthias Braun34e1be92013-10-10 21:29:02 +0000229 I = LR->advanceTo(I, Stop);
230 bool Backup = I == LR->end() || I->start >= Stop;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000231 if (Backup)
232 --I;
233 SlotIndex StopI = I->end;
234 if (!BI->Last.isValid() || StopI > BI->Last)
235 BI->Last = StopI;
236 if (Backup)
237 ++I;
238 }
239
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000240 // Also check for register mask interference.
241 SlotIndex Limit = BI->Last.isValid() ? BI->Last : Start;
Jakob Stoklund Olesenc4cf13f2012-02-14 23:53:23 +0000242 for (unsigned i = RegMaskSlots.size();
243 i && RegMaskSlots[i-1].getDeadSlot() > Limit; --i)
Jakob Stoklund Olesen024d7ae2012-02-10 19:23:53 +0000244 if (MachineOperand::clobbersPhysReg(RegMaskBits[i-1], PhysReg)) {
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000245 // Register mask i-1 clobbers PhysReg after the LIU interference.
246 // Model the regmask clobber as a dead def.
247 BI->Last = RegMaskSlots[i-1].getDeadSlot();
248 break;
249 }
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000250}