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 #ifndef LLVM_CODEGEN_INTERFERENCECACHE
15 #define LLVM_CODEGEN_INTERFERENCECACHE
17 #include "LiveIntervalUnion.h"
21 class InterferenceCache {
22 const TargetRegisterInfo *TRI;
23 LiveIntervalUnion *LIUArray;
26 /// BlockInterference - information about the interference in a single basic
28 struct BlockInterference {
29 BlockInterference() : Tag(0) {}
35 /// Entry - A cache entry containing interference information for all aliases
36 /// of PhysReg in all basic blocks.
38 /// PhysReg - The register currently represented.
41 /// Tag - Cache tag is changed when any of the underlying LiveIntervalUnions
45 /// RefCount - The total number of Cursor instances referring to this Entry.
48 /// MF - The current function.
51 /// Indexes - Mapping block numbers to SlotIndex ranges.
54 /// PrevPos - The previous position the iterators were moved to.
57 /// AliasTags - A LiveIntervalUnion pointer and tag for each alias of
59 SmallVector<std::pair<LiveIntervalUnion*, unsigned>, 8> Aliases;
61 typedef LiveIntervalUnion::SegmentIter Iter;
63 /// Iters - an iterator for each alias
64 SmallVector<Iter, 8> Iters;
66 /// Blocks - Interference for each block in the function.
67 SmallVector<BlockInterference, 8> Blocks;
69 /// update - Recompute Blocks[MBBNum]
70 void update(unsigned MBBNum);
73 Entry() : PhysReg(0), Tag(0), RefCount(0), Indexes(0) {}
75 void clear(MachineFunction *mf, SlotIndexes *indexes) {
76 assert(!hasRefs() && "Cannot clear cache entry with references");
82 unsigned getPhysReg() const { return PhysReg; }
84 void addRef(int Delta) { RefCount += Delta; }
86 bool hasRefs() const { return RefCount > 0; }
90 /// valid - Return true if this is a valid entry for physReg.
91 bool valid(LiveIntervalUnion *LIUArray, const TargetRegisterInfo *TRI);
93 /// reset - Initialize entry to represent physReg's aliases.
94 void reset(unsigned physReg,
95 LiveIntervalUnion *LIUArray,
96 const TargetRegisterInfo *TRI,
97 const MachineFunction *MF);
99 /// get - Return an up to date BlockInterference.
100 BlockInterference *get(unsigned MBBNum) {
101 if (Blocks[MBBNum].Tag != Tag)
103 return &Blocks[MBBNum];
107 // We don't keep a cache entry for every physical register, that would use too
108 // much memory. Instead, a fixed number of cache entries are used in a round-
110 enum { CacheEntries = 32 };
112 // Point to an entry for each physreg. The entry pointed to may not be up to
113 // date, and it may have been reused for a different physreg.
114 SmallVector<unsigned char, 2> PhysRegEntries;
116 // Next round-robin entry to be picked.
119 // The actual cache entries.
120 Entry Entries[CacheEntries];
122 // get - Get a valid entry for PhysReg.
123 Entry *get(unsigned PhysReg);
126 InterferenceCache() : TRI(0), LIUArray(0), MF(0), RoundRobin(0) {}
128 /// init - Prepare cache for a new function.
129 void init(MachineFunction*, LiveIntervalUnion*, SlotIndexes*,
130 const TargetRegisterInfo *);
132 /// getMaxCursors - Return the maximum number of concurrent cursors that can
134 unsigned getMaxCursors() const { return CacheEntries; }
136 /// Cursor - The primary query interface for the block interference cache.
139 BlockInterference *Current;
140 static BlockInterference NoInterference;
142 void setEntry(Entry *E) {
144 // Update reference counts. Nothing happens when RefCount reaches 0, so
145 // we don't have to check for E == CacheEntry etc.
147 CacheEntry->addRef(-1);
150 CacheEntry->addRef(+1);
154 /// Cursor - Create a dangling cursor.
155 Cursor() : CacheEntry(0), Current(0) {}
156 ~Cursor() { setEntry(0); }
158 Cursor(const Cursor &O) : CacheEntry(0), Current(0) {
159 setEntry(O.CacheEntry);
162 Cursor &operator=(const Cursor &O) {
163 setEntry(O.CacheEntry);
167 /// setPhysReg - Point this cursor to PhysReg's interference.
168 void setPhysReg(InterferenceCache &Cache, unsigned PhysReg) {
169 // Release reference before getting a new one. That guarantees we can
170 // actually have CacheEntries live cursors.
173 setEntry(Cache.get(PhysReg));
176 /// moveTo - Move cursor to basic block MBBNum.
177 void moveToBlock(unsigned MBBNum) {
178 Current = CacheEntry ? CacheEntry->get(MBBNum) : &NoInterference;
181 /// hasInterference - Return true if the current block has any interference.
182 bool hasInterference() {
183 return Current->First.isValid();
186 /// first - Return the starting index of the first interfering range in the
189 return Current->First;
192 /// last - Return the ending index of the last interfering range in the
195 return Current->Last;