1 //===-- InterferenceCache.h - Caching per-block interference ---*- C++ -*--===//
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/Target/TargetRegisterInfo.h"
17 #include "llvm/Support/ErrorHandling.h"
21 // Static member used for null interference cursors.
22 InterferenceCache::BlockInterference InterferenceCache::Cursor::NoInterference;
24 void InterferenceCache::init(MachineFunction *mf,
25 LiveIntervalUnion *liuarray,
27 const TargetRegisterInfo *tri) {
31 PhysRegEntries.assign(TRI->getNumRegs(), 0);
32 for (unsigned i = 0; i != CacheEntries; ++i)
33 Entries[i].clear(mf, indexes);
36 InterferenceCache::Entry *InterferenceCache::get(unsigned PhysReg) {
37 unsigned E = PhysRegEntries[PhysReg];
38 if (E < CacheEntries && Entries[E].getPhysReg() == PhysReg) {
39 if (!Entries[E].valid(LIUArray, TRI))
40 Entries[E].revalidate();
43 // No valid entry exists, pick the next round-robin entry.
45 if (++RoundRobin == CacheEntries)
47 for (unsigned i = 0; i != CacheEntries; ++i) {
48 // Skip entries that are in use.
49 if (Entries[E].hasRefs()) {
50 if (++E == CacheEntries)
54 Entries[E].reset(PhysReg, LIUArray, TRI, MF);
55 PhysRegEntries[PhysReg] = E;
58 llvm_unreachable("Ran out of interference cache entries.");
61 /// revalidate - LIU contents have changed, update tags.
62 void InterferenceCache::Entry::revalidate() {
63 // Invalidate all block entries.
65 // Invalidate all iterators.
66 PrevPos = SlotIndex();
67 for (unsigned i = 0, e = Aliases.size(); i != e; ++i)
68 Aliases[i].second = Aliases[i].first->getTag();
71 void InterferenceCache::Entry::reset(unsigned physReg,
72 LiveIntervalUnion *LIUArray,
73 const TargetRegisterInfo *TRI,
74 const MachineFunction *MF) {
75 assert(!hasRefs() && "Cannot reset cache entry with references");
76 // LIU's changed, invalidate cache.
79 Blocks.resize(MF->getNumBlockIDs());
81 for (const unsigned *AS = TRI->getOverlaps(PhysReg); *AS; ++AS) {
82 LiveIntervalUnion *LIU = LIUArray + *AS;
83 Aliases.push_back(std::make_pair(LIU, LIU->getTag()));
87 PrevPos = SlotIndex();
88 unsigned e = Aliases.size();
90 for (unsigned i = 0; i != e; ++i)
91 Iters[i].setMap(Aliases[i].first->getMap());
94 bool InterferenceCache::Entry::valid(LiveIntervalUnion *LIUArray,
95 const TargetRegisterInfo *TRI) {
96 unsigned i = 0, e = Aliases.size();
97 for (const unsigned *AS = TRI->getOverlaps(PhysReg); *AS; ++AS, ++i) {
98 LiveIntervalUnion *LIU = LIUArray + *AS;
99 if (i == e || Aliases[i].first != LIU)
101 if (LIU->changedSince(Aliases[i].second))
107 void InterferenceCache::Entry::update(unsigned MBBNum) {
108 SlotIndex Start, Stop;
109 tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
111 // Use advanceTo only when possible.
112 if (PrevPos != Start) {
113 if (!PrevPos.isValid() || Start < PrevPos)
114 for (unsigned i = 0, e = Iters.size(); i != e; ++i)
115 Iters[i].find(Start);
117 for (unsigned i = 0, e = Iters.size(); i != e; ++i)
118 Iters[i].advanceTo(Start);
122 MachineFunction::const_iterator MFI = MF->getBlockNumbered(MBBNum);
123 BlockInterference *BI = &Blocks[MBBNum];
126 BI->First = BI->Last = SlotIndex();
128 // Check for first interference.
129 for (unsigned i = 0, e = Iters.size(); i != e; ++i) {
133 SlotIndex StartI = I.start();
136 if (!BI->First.isValid() || StartI < BI->First)
141 if (BI->First.isValid())
144 // No interference in this block? Go ahead and precompute the next block.
145 if (++MFI == MF->end())
147 MBBNum = MFI->getNumber();
148 BI = &Blocks[MBBNum];
151 tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
154 // Check for last interference in block.
155 for (unsigned i = 0, e = Iters.size(); i != e; ++i) {
157 if (!I.valid() || I.start() >= Stop)
160 bool Backup = !I.valid() || I.start() >= Stop;
163 SlotIndex StopI = I.stop();
164 if (!BI->Last.isValid() || StopI > BI->Last)