1 //===-- InterferenceCache.cpp - Caching per-block interference ---------*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // InterferenceCache remembers per-block interference in LiveIntervalUnions.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "regalloc"
15 #include "InterferenceCache.h"
16 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Target/TargetRegisterInfo.h"
22 // Static member used for null interference cursors.
23 InterferenceCache::BlockInterference InterferenceCache::Cursor::NoInterference;
25 void InterferenceCache::init(MachineFunction *mf,
26 LiveIntervalUnion *liuarray,
29 const TargetRegisterInfo *tri) {
33 PhysRegEntries.assign(TRI->getNumRegs(), 0);
34 for (unsigned i = 0; i != CacheEntries; ++i)
35 Entries[i].clear(mf, indexes, lis);
38 InterferenceCache::Entry *InterferenceCache::get(unsigned PhysReg) {
39 unsigned E = PhysRegEntries[PhysReg];
40 if (E < CacheEntries && Entries[E].getPhysReg() == PhysReg) {
41 if (!Entries[E].valid(LIUArray, TRI))
42 Entries[E].revalidate(LIUArray, TRI);
45 // No valid entry exists, pick the next round-robin entry.
47 if (++RoundRobin == CacheEntries)
49 for (unsigned i = 0; i != CacheEntries; ++i) {
50 // Skip entries that are in use.
51 if (Entries[E].hasRefs()) {
52 if (++E == CacheEntries)
56 Entries[E].reset(PhysReg, LIUArray, TRI, MF);
57 PhysRegEntries[PhysReg] = E;
60 llvm_unreachable("Ran out of interference cache entries.");
63 /// revalidate - LIU contents have changed, update tags.
64 void InterferenceCache::Entry::revalidate(LiveIntervalUnion *LIUArray,
65 const TargetRegisterInfo *TRI) {
66 // Invalidate all block entries.
68 // Invalidate all iterators.
69 PrevPos = SlotIndex();
71 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i)
72 RegUnits[i].VirtTag = LIUArray[*Units].getTag();
75 void InterferenceCache::Entry::reset(unsigned physReg,
76 LiveIntervalUnion *LIUArray,
77 const TargetRegisterInfo *TRI,
78 const MachineFunction *MF) {
79 assert(!hasRefs() && "Cannot reset cache entry with references");
80 // LIU's changed, invalidate cache.
83 Blocks.resize(MF->getNumBlockIDs());
86 PrevPos = SlotIndex();
88 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
89 RegUnits.push_back(LIUArray[*Units]);
90 RegUnits.back().Fixed = &LIS->getRegUnit(*Units);
94 bool InterferenceCache::Entry::valid(LiveIntervalUnion *LIUArray,
95 const TargetRegisterInfo *TRI) {
96 unsigned i = 0, e = RegUnits.size();
97 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i) {
100 if (LIUArray[*Units].changedSince(RegUnits[i].VirtTag))
106 void InterferenceCache::Entry::update(unsigned MBBNum) {
107 SlotIndex Start, Stop;
108 tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
110 // Use advanceTo only when possible.
111 if (PrevPos != Start) {
112 if (!PrevPos.isValid() || Start < PrevPos) {
113 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
114 RegUnitInfo &RUI = RegUnits[i];
115 RUI.VirtI.find(Start);
116 RUI.FixedI = RUI.Fixed->find(Start);
119 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
120 RegUnitInfo &RUI = RegUnits[i];
121 RUI.VirtI.advanceTo(Start);
122 if (RUI.FixedI != RUI.Fixed->end())
123 RUI.FixedI = RUI.Fixed->advanceTo(RUI.FixedI, Start);
129 MachineFunction::const_iterator MFI = MF->getBlockNumbered(MBBNum);
130 BlockInterference *BI = &Blocks[MBBNum];
131 ArrayRef<SlotIndex> RegMaskSlots;
132 ArrayRef<const uint32_t*> RegMaskBits;
135 BI->First = BI->Last = SlotIndex();
137 // Check for first interference from virtregs.
138 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
139 LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
142 SlotIndex StartI = I.start();
145 if (!BI->First.isValid() || StartI < BI->First)
149 // Same thing for fixed interference.
150 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
151 LiveInterval::const_iterator I = RegUnits[i].FixedI;
152 LiveInterval::const_iterator E = RegUnits[i].Fixed->end();
155 SlotIndex StartI = I->start;
158 if (!BI->First.isValid() || StartI < BI->First)
162 // Also check for register mask interference.
163 RegMaskSlots = LIS->getRegMaskSlotsInBlock(MBBNum);
164 RegMaskBits = LIS->getRegMaskBitsInBlock(MBBNum);
165 SlotIndex Limit = BI->First.isValid() ? BI->First : Stop;
166 for (unsigned i = 0, e = RegMaskSlots.size();
167 i != e && RegMaskSlots[i] < Limit; ++i)
168 if (MachineOperand::clobbersPhysReg(RegMaskBits[i], PhysReg)) {
169 // Register mask i clobbers PhysReg before the LIU interference.
170 BI->First = RegMaskSlots[i];
175 if (BI->First.isValid())
178 // No interference in this block? Go ahead and precompute the next block.
179 if (++MFI == MF->end())
181 MBBNum = MFI->getNumber();
182 BI = &Blocks[MBBNum];
185 tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
188 // Check for last interference in block.
189 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
190 LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
191 if (!I.valid() || I.start() >= Stop)
194 bool Backup = !I.valid() || I.start() >= Stop;
197 SlotIndex StopI = I.stop();
198 if (!BI->Last.isValid() || StopI > BI->Last)
204 // Fixed interference.
205 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
206 LiveInterval::iterator &I = RegUnits[i].FixedI;
207 LiveRange *LR = RegUnits[i].Fixed;
208 if (I == LR->end() || I->start >= Stop)
210 I = LR->advanceTo(I, Stop);
211 bool Backup = I == LR->end() || I->start >= Stop;
214 SlotIndex StopI = I->end;
215 if (!BI->Last.isValid() || StopI > BI->Last)
221 // Also check for register mask interference.
222 SlotIndex Limit = BI->Last.isValid() ? BI->Last : Start;
223 for (unsigned i = RegMaskSlots.size();
224 i && RegMaskSlots[i-1].getDeadSlot() > Limit; --i)
225 if (MachineOperand::clobbersPhysReg(RegMaskBits[i-1], PhysReg)) {
226 // Register mask i-1 clobbers PhysReg after the LIU interference.
227 // Model the regmask clobber as a dead def.
228 BI->Last = RegMaskSlots[i-1].getDeadSlot();