Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 1 | //===- InterferenceCache.cpp - Caching per-block interference -------------===// |
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 | // |
| 9 | // InterferenceCache remembers per-block interference in LiveIntervalUnions. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 13 | #include "InterferenceCache.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/ArrayRef.h" |
| 15 | #include "llvm/CodeGen/LiveInterval.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/LiveIntervalUnion.h" |
Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/LiveIntervals.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/MachineOperand.h" |
| 21 | #include "llvm/CodeGen/SlotIndexes.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCRegisterInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 25 | #include <cassert> |
| 26 | #include <cstdint> |
| 27 | #include <cstdlib> |
| 28 | #include <tuple> |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "regalloc" |
| 33 | |
Jakob Stoklund Olesen | cacefc7 | 2011-07-23 03:10:17 +0000 | [diff] [blame] | 34 | // Static member used for null interference cursors. |
Benjamin Kramer | 57a3d08 | 2015-03-08 16:07:39 +0000 | [diff] [blame] | 35 | const InterferenceCache::BlockInterference |
| 36 | InterferenceCache::Cursor::NoInterference; |
Jakob Stoklund Olesen | cacefc7 | 2011-07-23 03:10:17 +0000 | [diff] [blame] | 37 | |
Puyan Lotfi | 5eb1004 | 2014-02-06 09:23:24 +0000 | [diff] [blame] | 38 | // Initializes PhysRegEntries (instead of a SmallVector, PhysRegEntries is a |
| 39 | // buffer of size NumPhysRegs to speed up alloc/clear for targets with large |
| 40 | // reg files). Calloced memory is used for good form, and quites tools like |
| 41 | // Valgrind too, but zero initialized memory is not required by the algorithm: |
| 42 | // this is because PhysRegEntries works like a SparseSet and its entries are |
| 43 | // only valid when there is a corresponding CacheEntries assignment. There is |
| 44 | // also support for when pass managers are reused for targets with different |
| 45 | // numbers of PhysRegs: in this case PhysRegEntries is freed and reinitialized. |
| 46 | void InterferenceCache::reinitPhysRegEntries() { |
| 47 | if (PhysRegEntriesCount == TRI->getNumRegs()) return; |
| 48 | free(PhysRegEntries); |
| 49 | PhysRegEntriesCount = TRI->getNumRegs(); |
Serge Pavlov | 76d8cce | 2018-02-20 05:41:26 +0000 | [diff] [blame] | 50 | PhysRegEntries = static_cast<unsigned char*>( |
| 51 | safe_calloc(PhysRegEntriesCount, sizeof(unsigned char))); |
Puyan Lotfi | 5eb1004 | 2014-02-06 09:23:24 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 54 | void InterferenceCache::init(MachineFunction *mf, |
| 55 | LiveIntervalUnion *liuarray, |
| 56 | SlotIndexes *indexes, |
Jakob Stoklund Olesen | a16ae59 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 57 | LiveIntervals *lis, |
Jakob Stoklund Olesen | cacefc7 | 2011-07-23 03:10:17 +0000 | [diff] [blame] | 58 | const TargetRegisterInfo *tri) { |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 59 | MF = mf; |
| 60 | LIUArray = liuarray; |
| 61 | TRI = tri; |
Puyan Lotfi | 5eb1004 | 2014-02-06 09:23:24 +0000 | [diff] [blame] | 62 | reinitPhysRegEntries(); |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 63 | for (unsigned i = 0; i != CacheEntries; ++i) |
Jakob Stoklund Olesen | a16ae59 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 64 | Entries[i].clear(mf, indexes, lis); |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | InterferenceCache::Entry *InterferenceCache::get(unsigned PhysReg) { |
| 68 | unsigned E = PhysRegEntries[PhysReg]; |
| 69 | if (E < CacheEntries && Entries[E].getPhysReg() == PhysReg) { |
| 70 | if (!Entries[E].valid(LIUArray, TRI)) |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 71 | Entries[E].revalidate(LIUArray, TRI); |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 72 | return &Entries[E]; |
| 73 | } |
| 74 | // No valid entry exists, pick the next round-robin entry. |
| 75 | E = RoundRobin; |
| 76 | if (++RoundRobin == CacheEntries) |
| 77 | RoundRobin = 0; |
Jakob Stoklund Olesen | a153ca5 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 78 | for (unsigned i = 0; i != CacheEntries; ++i) { |
| 79 | // Skip entries that are in use. |
| 80 | if (Entries[E].hasRefs()) { |
| 81 | if (++E == CacheEntries) |
| 82 | E = 0; |
| 83 | continue; |
| 84 | } |
| 85 | Entries[E].reset(PhysReg, LIUArray, TRI, MF); |
| 86 | PhysRegEntries[PhysReg] = E; |
| 87 | return &Entries[E]; |
| 88 | } |
| 89 | llvm_unreachable("Ran out of interference cache entries."); |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /// revalidate - LIU contents have changed, update tags. |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 93 | void InterferenceCache::Entry::revalidate(LiveIntervalUnion *LIUArray, |
| 94 | const TargetRegisterInfo *TRI) { |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 95 | // Invalidate all block entries. |
| 96 | ++Tag; |
| 97 | // Invalidate all iterators. |
| 98 | PrevPos = SlotIndex(); |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 99 | unsigned i = 0; |
| 100 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i) |
| 101 | RegUnits[i].VirtTag = LIUArray[*Units].getTag(); |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void InterferenceCache::Entry::reset(unsigned physReg, |
| 105 | LiveIntervalUnion *LIUArray, |
| 106 | const TargetRegisterInfo *TRI, |
| 107 | const MachineFunction *MF) { |
Jakob Stoklund Olesen | a153ca5 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 108 | assert(!hasRefs() && "Cannot reset cache entry with references"); |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 109 | // LIU's changed, invalidate cache. |
| 110 | ++Tag; |
| 111 | PhysReg = physReg; |
| 112 | Blocks.resize(MF->getNumBlockIDs()); |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 113 | |
| 114 | // Reset iterators. |
| 115 | PrevPos = SlotIndex(); |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 116 | RegUnits.clear(); |
| 117 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 118 | RegUnits.push_back(LIUArray[*Units]); |
| 119 | RegUnits.back().Fixed = &LIS->getRegUnit(*Units); |
| 120 | } |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool InterferenceCache::Entry::valid(LiveIntervalUnion *LIUArray, |
| 124 | const TargetRegisterInfo *TRI) { |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 125 | unsigned i = 0, e = RegUnits.size(); |
| 126 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i) { |
| 127 | if (i == e) |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 128 | return false; |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 129 | if (LIUArray[*Units].changedSince(RegUnits[i].VirtTag)) |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | return i == e; |
| 133 | } |
| 134 | |
| 135 | void InterferenceCache::Entry::update(unsigned MBBNum) { |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 136 | SlotIndex Start, Stop; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 137 | std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum); |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 138 | |
| 139 | // Use advanceTo only when possible. |
Jakob Stoklund Olesen | 994c168 | 2011-04-07 17:27:50 +0000 | [diff] [blame] | 140 | if (PrevPos != Start) { |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 141 | if (!PrevPos.isValid() || Start < PrevPos) { |
| 142 | for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) { |
| 143 | RegUnitInfo &RUI = RegUnits[i]; |
| 144 | RUI.VirtI.find(Start); |
| 145 | RUI.FixedI = RUI.Fixed->find(Start); |
| 146 | } |
| 147 | } else { |
| 148 | for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) { |
| 149 | RegUnitInfo &RUI = RegUnits[i]; |
| 150 | RUI.VirtI.advanceTo(Start); |
| 151 | if (RUI.FixedI != RUI.Fixed->end()) |
| 152 | RUI.FixedI = RUI.Fixed->advanceTo(RUI.FixedI, Start); |
| 153 | } |
| 154 | } |
Jakob Stoklund Olesen | 994c168 | 2011-04-07 17:27:50 +0000 | [diff] [blame] | 155 | PrevPos = Start; |
| 156 | } |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 157 | |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 158 | MachineFunction::const_iterator MFI = |
| 159 | MF->getBlockNumbered(MBBNum)->getIterator(); |
Jakob Stoklund Olesen | 4ad6c16 | 2011-04-09 02:59:05 +0000 | [diff] [blame] | 160 | BlockInterference *BI = &Blocks[MBBNum]; |
Jakob Stoklund Olesen | a16ae59 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 161 | ArrayRef<SlotIndex> RegMaskSlots; |
| 162 | ArrayRef<const uint32_t*> RegMaskBits; |
Eugene Zelenko | f193332 | 2017-09-22 23:46:57 +0000 | [diff] [blame] | 163 | while (true) { |
Jakob Stoklund Olesen | 4ad6c16 | 2011-04-09 02:59:05 +0000 | [diff] [blame] | 164 | BI->Tag = Tag; |
| 165 | BI->First = BI->Last = SlotIndex(); |
| 166 | |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 167 | // Check for first interference from virtregs. |
| 168 | for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) { |
| 169 | LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI; |
Jakob Stoklund Olesen | 4ad6c16 | 2011-04-09 02:59:05 +0000 | [diff] [blame] | 170 | if (!I.valid()) |
| 171 | continue; |
| 172 | SlotIndex StartI = I.start(); |
| 173 | if (StartI >= Stop) |
| 174 | continue; |
| 175 | if (!BI->First.isValid() || StartI < BI->First) |
| 176 | BI->First = StartI; |
| 177 | } |
| 178 | |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 179 | // Same thing for fixed interference. |
| 180 | for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) { |
| 181 | LiveInterval::const_iterator I = RegUnits[i].FixedI; |
| 182 | LiveInterval::const_iterator E = RegUnits[i].Fixed->end(); |
| 183 | if (I == E) |
| 184 | continue; |
| 185 | SlotIndex StartI = I->start; |
| 186 | if (StartI >= Stop) |
| 187 | continue; |
| 188 | if (!BI->First.isValid() || StartI < BI->First) |
| 189 | BI->First = StartI; |
| 190 | } |
| 191 | |
Jakob Stoklund Olesen | a16ae59 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 192 | // Also check for register mask interference. |
| 193 | RegMaskSlots = LIS->getRegMaskSlotsInBlock(MBBNum); |
| 194 | RegMaskBits = LIS->getRegMaskBitsInBlock(MBBNum); |
| 195 | SlotIndex Limit = BI->First.isValid() ? BI->First : Stop; |
| 196 | for (unsigned i = 0, e = RegMaskSlots.size(); |
| 197 | i != e && RegMaskSlots[i] < Limit; ++i) |
Jakob Stoklund Olesen | 024d7ae | 2012-02-10 19:23:53 +0000 | [diff] [blame] | 198 | if (MachineOperand::clobbersPhysReg(RegMaskBits[i], PhysReg)) { |
Jakob Stoklund Olesen | a16ae59 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 199 | // Register mask i clobbers PhysReg before the LIU interference. |
| 200 | BI->First = RegMaskSlots[i]; |
| 201 | break; |
| 202 | } |
| 203 | |
Jakob Stoklund Olesen | 4ad6c16 | 2011-04-09 02:59:05 +0000 | [diff] [blame] | 204 | PrevPos = Stop; |
| 205 | if (BI->First.isValid()) |
| 206 | break; |
| 207 | |
| 208 | // No interference in this block? Go ahead and precompute the next block. |
| 209 | if (++MFI == MF->end()) |
| 210 | return; |
| 211 | MBBNum = MFI->getNumber(); |
| 212 | BI = &Blocks[MBBNum]; |
| 213 | if (BI->Tag == Tag) |
| 214 | return; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 215 | std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum); |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Jakob Stoklund Olesen | 4ad6c16 | 2011-04-09 02:59:05 +0000 | [diff] [blame] | 218 | // Check for last interference in block. |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 219 | for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) { |
| 220 | LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI; |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 221 | if (!I.valid() || I.start() >= Stop) |
| 222 | continue; |
| 223 | I.advanceTo(Stop); |
Jakob Stoklund Olesen | 994c168 | 2011-04-07 17:27:50 +0000 | [diff] [blame] | 224 | bool Backup = !I.valid() || I.start() >= Stop; |
| 225 | if (Backup) |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 226 | --I; |
| 227 | SlotIndex StopI = I.stop(); |
| 228 | if (!BI->Last.isValid() || StopI > BI->Last) |
| 229 | BI->Last = StopI; |
Jakob Stoklund Olesen | 994c168 | 2011-04-07 17:27:50 +0000 | [diff] [blame] | 230 | if (Backup) |
| 231 | ++I; |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 232 | } |
Jakob Stoklund Olesen | a16ae59 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 233 | |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 234 | // Fixed interference. |
| 235 | for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) { |
| 236 | LiveInterval::iterator &I = RegUnits[i].FixedI; |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 237 | LiveRange *LR = RegUnits[i].Fixed; |
| 238 | if (I == LR->end() || I->start >= Stop) |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 239 | continue; |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 240 | I = LR->advanceTo(I, Stop); |
| 241 | bool Backup = I == LR->end() || I->start >= Stop; |
Jakob Stoklund Olesen | 96eebf0 | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 242 | if (Backup) |
| 243 | --I; |
| 244 | SlotIndex StopI = I->end; |
| 245 | if (!BI->Last.isValid() || StopI > BI->Last) |
| 246 | BI->Last = StopI; |
| 247 | if (Backup) |
| 248 | ++I; |
| 249 | } |
| 250 | |
Jakob Stoklund Olesen | a16ae59 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 251 | // Also check for register mask interference. |
| 252 | SlotIndex Limit = BI->Last.isValid() ? BI->Last : Start; |
Jakob Stoklund Olesen | c4cf13f | 2012-02-14 23:53:23 +0000 | [diff] [blame] | 253 | for (unsigned i = RegMaskSlots.size(); |
| 254 | i && RegMaskSlots[i-1].getDeadSlot() > Limit; --i) |
Jakob Stoklund Olesen | 024d7ae | 2012-02-10 19:23:53 +0000 | [diff] [blame] | 255 | if (MachineOperand::clobbersPhysReg(RegMaskBits[i-1], PhysReg)) { |
Jakob Stoklund Olesen | a16ae59 | 2012-02-10 18:58:34 +0000 | [diff] [blame] | 256 | // Register mask i-1 clobbers PhysReg after the LIU interference. |
| 257 | // Model the regmask clobber as a dead def. |
| 258 | BI->Last = RegMaskSlots[i-1].getDeadSlot(); |
| 259 | break; |
| 260 | } |
Jakob Stoklund Olesen | 91cbcaf | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 261 | } |