Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 1 | //===-- InterferenceCache.h - Caching per-block interference ---*- C++ -*--===// |
| 2 | // |
| 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 | // |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 10 | // InterferenceCache remembers per-block interference from LiveIntervalUnions, |
| 11 | // fixed RegUnit interference, and register masks. |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CODEGEN_INTERFERENCECACHE |
| 16 | #define LLVM_CODEGEN_INTERFERENCECACHE |
| 17 | |
Jakob Stoklund Olesen | 1ead68d | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/LiveIntervalUnion.h" |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 19 | |
| 20 | namespace llvm { |
| 21 | |
Jakob Stoklund Olesen | 6ef7da0 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 22 | class LiveIntervals; |
| 23 | |
Benjamin Kramer | 55c06ae | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 24 | class InterferenceCache { |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 25 | const TargetRegisterInfo *TRI; |
| 26 | LiveIntervalUnion *LIUArray; |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 27 | MachineFunction *MF; |
| 28 | |
| 29 | /// BlockInterference - information about the interference in a single basic |
| 30 | /// block. |
| 31 | struct BlockInterference { |
| 32 | BlockInterference() : Tag(0) {} |
| 33 | unsigned Tag; |
| 34 | SlotIndex First; |
| 35 | SlotIndex Last; |
| 36 | }; |
| 37 | |
| 38 | /// Entry - A cache entry containing interference information for all aliases |
| 39 | /// of PhysReg in all basic blocks. |
| 40 | class Entry { |
| 41 | /// PhysReg - The register currently represented. |
| 42 | unsigned PhysReg; |
| 43 | |
| 44 | /// Tag - Cache tag is changed when any of the underlying LiveIntervalUnions |
| 45 | /// change. |
| 46 | unsigned Tag; |
| 47 | |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 48 | /// RefCount - The total number of Cursor instances referring to this Entry. |
| 49 | unsigned RefCount; |
| 50 | |
Jakob Stoklund Olesen | 9d29cba | 2011-04-09 02:59:05 +0000 | [diff] [blame] | 51 | /// MF - The current function. |
| 52 | MachineFunction *MF; |
| 53 | |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 54 | /// Indexes - Mapping block numbers to SlotIndex ranges. |
| 55 | SlotIndexes *Indexes; |
| 56 | |
Jakob Stoklund Olesen | 6ef7da0 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 57 | /// LIS - Used for accessing register mask interference maps. |
| 58 | LiveIntervals *LIS; |
| 59 | |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 60 | /// PrevPos - The previous position the iterators were moved to. |
| 61 | SlotIndex PrevPos; |
| 62 | |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 63 | /// RegUnitInfo - Information tracked about each RegUnit in PhysReg. |
| 64 | /// When PrevPos is set, the iterators are valid as if advanceTo(PrevPos) |
| 65 | /// had just been called. |
| 66 | struct RegUnitInfo { |
| 67 | /// Iterator pointing into the LiveIntervalUnion containing virtual |
| 68 | /// register interference. |
| 69 | LiveIntervalUnion::SegmentIter VirtI; |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 70 | |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 71 | /// Tag of the LIU last time we looked. |
| 72 | unsigned VirtTag; |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 73 | |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 74 | /// Fixed interference in RegUnit. |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 75 | LiveRange *Fixed; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 76 | |
| 77 | /// Iterator pointing into the fixed RegUnit interference. |
| 78 | LiveInterval::iterator FixedI; |
| 79 | |
| 80 | RegUnitInfo(LiveIntervalUnion &LIU) : VirtTag(LIU.getTag()), Fixed(0) { |
| 81 | VirtI.setMap(LIU.getMap()); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | /// Info for each RegUnit in PhysReg. It is very rare ofr a PHysReg to have |
| 86 | /// more than 4 RegUnits. |
| 87 | SmallVector<RegUnitInfo, 4> RegUnits; |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 88 | |
| 89 | /// Blocks - Interference for each block in the function. |
| 90 | SmallVector<BlockInterference, 8> Blocks; |
| 91 | |
| 92 | /// update - Recompute Blocks[MBBNum] |
| 93 | void update(unsigned MBBNum); |
| 94 | |
| 95 | public: |
Jakob Stoklund Olesen | 6ef7da0 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 96 | Entry() : PhysReg(0), Tag(0), RefCount(0), Indexes(0), LIS(0) {} |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 97 | |
Jakob Stoklund Olesen | 6ef7da0 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 98 | void clear(MachineFunction *mf, SlotIndexes *indexes, LiveIntervals *lis) { |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 99 | assert(!hasRefs() && "Cannot clear cache entry with references"); |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 100 | PhysReg = 0; |
Jakob Stoklund Olesen | 9d29cba | 2011-04-09 02:59:05 +0000 | [diff] [blame] | 101 | MF = mf; |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 102 | Indexes = indexes; |
Jakob Stoklund Olesen | 6ef7da0 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 103 | LIS = lis; |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | unsigned getPhysReg() const { return PhysReg; } |
| 107 | |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 108 | void addRef(int Delta) { RefCount += Delta; } |
| 109 | |
| 110 | bool hasRefs() const { return RefCount > 0; } |
| 111 | |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 112 | void revalidate(LiveIntervalUnion *LIUArray, const TargetRegisterInfo *TRI); |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 113 | |
| 114 | /// valid - Return true if this is a valid entry for physReg. |
| 115 | bool valid(LiveIntervalUnion *LIUArray, const TargetRegisterInfo *TRI); |
| 116 | |
| 117 | /// reset - Initialize entry to represent physReg's aliases. |
| 118 | void reset(unsigned physReg, |
| 119 | LiveIntervalUnion *LIUArray, |
| 120 | const TargetRegisterInfo *TRI, |
| 121 | const MachineFunction *MF); |
| 122 | |
| 123 | /// get - Return an up to date BlockInterference. |
| 124 | BlockInterference *get(unsigned MBBNum) { |
| 125 | if (Blocks[MBBNum].Tag != Tag) |
| 126 | update(MBBNum); |
| 127 | return &Blocks[MBBNum]; |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | // We don't keep a cache entry for every physical register, that would use too |
| 132 | // much memory. Instead, a fixed number of cache entries are used in a round- |
| 133 | // robin manner. |
| 134 | enum { CacheEntries = 32 }; |
| 135 | |
| 136 | // Point to an entry for each physreg. The entry pointed to may not be up to |
| 137 | // date, and it may have been reused for a different physreg. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 138 | unsigned char* PhysRegEntries; |
| 139 | size_t PhysRegEntriesCount; |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 140 | |
| 141 | // Next round-robin entry to be picked. |
| 142 | unsigned RoundRobin; |
| 143 | |
| 144 | // The actual cache entries. |
| 145 | Entry Entries[CacheEntries]; |
| 146 | |
| 147 | // get - Get a valid entry for PhysReg. |
| 148 | Entry *get(unsigned PhysReg); |
| 149 | |
| 150 | public: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 151 | InterferenceCache() : TRI(0), LIUArray(0), MF(0), PhysRegEntries(NULL), |
| 152 | PhysRegEntriesCount(0), RoundRobin(0) {} |
| 153 | |
| 154 | ~InterferenceCache() { |
| 155 | free(PhysRegEntries); |
| 156 | } |
| 157 | |
| 158 | void reinitPhysRegEntries(); |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 159 | |
| 160 | /// init - Prepare cache for a new function. |
Jakob Stoklund Olesen | 6ef7da0 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 161 | void init(MachineFunction*, LiveIntervalUnion*, SlotIndexes*, LiveIntervals*, |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 162 | const TargetRegisterInfo *); |
| 163 | |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 164 | /// getMaxCursors - Return the maximum number of concurrent cursors that can |
| 165 | /// be supported. |
| 166 | unsigned getMaxCursors() const { return CacheEntries; } |
| 167 | |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 168 | /// Cursor - The primary query interface for the block interference cache. |
| 169 | class Cursor { |
| 170 | Entry *CacheEntry; |
| 171 | BlockInterference *Current; |
Jakob Stoklund Olesen | c7931fd | 2011-07-23 03:10:17 +0000 | [diff] [blame] | 172 | static BlockInterference NoInterference; |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 173 | |
| 174 | void setEntry(Entry *E) { |
| 175 | Current = 0; |
| 176 | // Update reference counts. Nothing happens when RefCount reaches 0, so |
| 177 | // we don't have to check for E == CacheEntry etc. |
| 178 | if (CacheEntry) |
| 179 | CacheEntry->addRef(-1); |
| 180 | CacheEntry = E; |
| 181 | if (CacheEntry) |
| 182 | CacheEntry->addRef(+1); |
| 183 | } |
| 184 | |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 185 | public: |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 186 | /// Cursor - Create a dangling cursor. |
| 187 | Cursor() : CacheEntry(0), Current(0) {} |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 188 | ~Cursor() { setEntry(0); } |
| 189 | |
| 190 | Cursor(const Cursor &O) : CacheEntry(0), Current(0) { |
| 191 | setEntry(O.CacheEntry); |
| 192 | } |
| 193 | |
| 194 | Cursor &operator=(const Cursor &O) { |
| 195 | setEntry(O.CacheEntry); |
| 196 | return *this; |
| 197 | } |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 198 | |
| 199 | /// setPhysReg - Point this cursor to PhysReg's interference. |
| 200 | void setPhysReg(InterferenceCache &Cache, unsigned PhysReg) { |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 201 | // Release reference before getting a new one. That guarantees we can |
| 202 | // actually have CacheEntries live cursors. |
| 203 | setEntry(0); |
| 204 | if (PhysReg) |
| 205 | setEntry(Cache.get(PhysReg)); |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 206 | } |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 207 | |
| 208 | /// moveTo - Move cursor to basic block MBBNum. |
| 209 | void moveToBlock(unsigned MBBNum) { |
Jakob Stoklund Olesen | c7931fd | 2011-07-23 03:10:17 +0000 | [diff] [blame] | 210 | Current = CacheEntry ? CacheEntry->get(MBBNum) : &NoInterference; |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /// hasInterference - Return true if the current block has any interference. |
| 214 | bool hasInterference() { |
| 215 | return Current->First.isValid(); |
| 216 | } |
| 217 | |
| 218 | /// first - Return the starting index of the first interfering range in the |
| 219 | /// current block. |
| 220 | SlotIndex first() { |
| 221 | return Current->First; |
| 222 | } |
| 223 | |
| 224 | /// last - Return the ending index of the last interfering range in the |
| 225 | /// current block. |
| 226 | SlotIndex last() { |
| 227 | return Current->Last; |
| 228 | } |
| 229 | }; |
| 230 | |
| 231 | friend class Cursor; |
| 232 | }; |
| 233 | |
| 234 | } // namespace llvm |
| 235 | |
| 236 | #endif |