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