blob: 50c6ac62d194156d982e2bc16611280704980db9 [file] [log] [blame]
Eugene Zelenkof1933322017-09-22 23:46:57 +00001//===- InterferenceCache.h - Caching per-block interference ----*- C++ -*--===//
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Olesen91cbcaf2011-04-02 06:03:35 +00006//
7//===----------------------------------------------------------------------===//
8//
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +00009// InterferenceCache remembers per-block interference from LiveIntervalUnions,
10// fixed RegUnit interference, and register masks.
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000011//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_CODEGEN_INTERFERENCECACHE_H
15#define LLVM_LIB_CODEGEN_INTERFERENCECACHE_H
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000016
Eugene Zelenkof1933322017-09-22 23:46:57 +000017#include "llvm/ADT/SmallVector.h"
18#include "llvm/CodeGen/LiveInterval.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000019#include "llvm/CodeGen/LiveIntervalUnion.h"
Eugene Zelenkof1933322017-09-22 23:46:57 +000020#include "llvm/CodeGen/SlotIndexes.h"
21#include "llvm/Support/Compiler.h"
22#include <cassert>
23#include <cstddef>
24#include <cstdlib>
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000025
26namespace llvm {
27
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000028class LiveIntervals;
Eugene Zelenkof1933322017-09-22 23:46:57 +000029class MachineFunction;
30class TargetRegisterInfo;
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000031
Benjamin Kramerf4c20252015-07-01 14:47:39 +000032class LLVM_LIBRARY_VISIBILITY InterferenceCache {
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000033 /// BlockInterference - information about the interference in a single basic
34 /// block.
35 struct BlockInterference {
Eugene Zelenkof1933322017-09-22 23:46:57 +000036 unsigned Tag = 0;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000037 SlotIndex First;
38 SlotIndex Last;
Eugene Zelenkof1933322017-09-22 23:46:57 +000039
Eugene Zelenko8e30a1c2017-09-22 23:55:32 +000040 BlockInterference() {}
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000041 };
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 Zelenkof1933322017-09-22 23:46:57 +000047 unsigned PhysReg = 0;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000048
49 /// Tag - Cache tag is changed when any of the underlying LiveIntervalUnions
50 /// change.
Eugene Zelenkof1933322017-09-22 23:46:57 +000051 unsigned Tag = 0;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000052
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +000053 /// RefCount - The total number of Cursor instances referring to this Entry.
Eugene Zelenkof1933322017-09-22 23:46:57 +000054 unsigned RefCount = 0;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +000055
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +000056 /// MF - The current function.
57 MachineFunction *MF;
58
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000059 /// Indexes - Mapping block numbers to SlotIndex ranges.
Eugene Zelenkof1933322017-09-22 23:46:57 +000060 SlotIndexes *Indexes = nullptr;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000061
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000062 /// LIS - Used for accessing register mask interference maps.
Eugene Zelenkof1933322017-09-22 23:46:57 +000063 LiveIntervals *LIS = nullptr;
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000064
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000065 /// PrevPos - The previous position the iterators were moved to.
66 SlotIndex PrevPos;
67
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000068 /// 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 Olesen91cbcaf2011-04-02 06:03:35 +000075
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000076 /// Tag of the LIU last time we looked.
77 unsigned VirtTag;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000078
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000079 /// Fixed interference in RegUnit.
Eugene Zelenkof1933322017-09-22 23:46:57 +000080 LiveRange *Fixed = nullptr;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000081
82 /// Iterator pointing into the fixed RegUnit interference.
83 LiveInterval::iterator FixedI;
84
Eugene Zelenkof1933322017-09-22 23:46:57 +000085 RegUnitInfo(LiveIntervalUnion &LIU) : VirtTag(LIU.getTag()) {
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000086 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 Olesen91cbcaf2011-04-02 06:03:35 +000093
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 Zelenkof1933322017-09-22 23:46:57 +0000101 Entry() = default;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000102
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000103 void clear(MachineFunction *mf, SlotIndexes *indexes, LiveIntervals *lis) {
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000104 assert(!hasRefs() && "Cannot clear cache entry with references");
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000105 PhysReg = 0;
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000106 MF = mf;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000107 Indexes = indexes;
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000108 LIS = lis;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000109 }
110
111 unsigned getPhysReg() const { return PhysReg; }
112
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000113 void addRef(int Delta) { RefCount += Delta; }
114
115 bool hasRefs() const { return RefCount > 0; }
116
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000117 void revalidate(LiveIntervalUnion *LIUArray, const TargetRegisterInfo *TRI);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000118
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 Zelenkof1933322017-09-22 23:46:57 +0000141 const TargetRegisterInfo *TRI = nullptr;
142 LiveIntervalUnion *LIUArray = nullptr;
143 MachineFunction *MF = nullptr;
144
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000145 // 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 Zelenkof1933322017-09-22 23:46:57 +0000147 unsigned char* PhysRegEntries = nullptr;
148 size_t PhysRegEntriesCount = 0;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000149
150 // Next round-robin entry to be picked.
Eugene Zelenkof1933322017-09-22 23:46:57 +0000151 unsigned RoundRobin = 0;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000152
153 // The actual cache entries.
154 Entry Entries[CacheEntries];
155
156 // get - Get a valid entry for PhysReg.
157 Entry *get(unsigned PhysReg);
158
159public:
Eugene Zelenkof1933322017-09-22 23:46:57 +0000160 friend class Cursor;
161
162 InterferenceCache() = default;
Puyan Lotfi5eb10042014-02-06 09:23:24 +0000163
164 ~InterferenceCache() {
165 free(PhysRegEntries);
166 }
167
168 void reinitPhysRegEntries();
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000169
170 /// init - Prepare cache for a new function.
Eugene Zelenkof1933322017-09-22 23:46:57 +0000171 void init(MachineFunction *mf, LiveIntervalUnion *liuarray,
172 SlotIndexes *indexes, LiveIntervals *lis,
173 const TargetRegisterInfo *tri);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000174
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000175 /// getMaxCursors - Return the maximum number of concurrent cursors that can
176 /// be supported.
177 unsigned getMaxCursors() const { return CacheEntries; }
178
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000179 /// Cursor - The primary query interface for the block interference cache.
180 class Cursor {
Eugene Zelenkof1933322017-09-22 23:46:57 +0000181 Entry *CacheEntry = nullptr;
182 const BlockInterference *Current = nullptr;
Benjamin Kramer57a3d082015-03-08 16:07:39 +0000183 static const BlockInterference NoInterference;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000184
185 void setEntry(Entry *E) {
Craig Topperada08572014-04-16 04:21:27 +0000186 Current = nullptr;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000187 // 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 Olesen91cbcaf2011-04-02 06:03:35 +0000196 public:
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000197 /// Cursor - Create a dangling cursor.
Eugene Zelenkof1933322017-09-22 23:46:57 +0000198 Cursor() = default;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000199
Eugene Zelenkof1933322017-09-22 23:46:57 +0000200 Cursor(const Cursor &O) {
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000201 setEntry(O.CacheEntry);
202 }
203
204 Cursor &operator=(const Cursor &O) {
205 setEntry(O.CacheEntry);
206 return *this;
207 }
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000208
Eugene Zelenkof1933322017-09-22 23:46:57 +0000209 ~Cursor() { setEntry(nullptr); }
210
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000211 /// setPhysReg - Point this cursor to PhysReg's interference.
212 void setPhysReg(InterferenceCache &Cache, unsigned PhysReg) {
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000213 // Release reference before getting a new one. That guarantees we can
214 // actually have CacheEntries live cursors.
Craig Topperada08572014-04-16 04:21:27 +0000215 setEntry(nullptr);
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000216 if (PhysReg)
217 setEntry(Cache.get(PhysReg));
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000218 }
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000219
220 /// moveTo - Move cursor to basic block MBBNum.
221 void moveToBlock(unsigned MBBNum) {
Jakob Stoklund Olesencacefc72011-07-23 03:10:17 +0000222 Current = CacheEntry ? CacheEntry->get(MBBNum) : &NoInterference;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000223 }
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 Olesen91cbcaf2011-04-02 06:03:35 +0000242};
243
Eugene Zelenkof1933322017-09-22 23:46:57 +0000244} // end namespace llvm
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000245
Eugene Zelenkof1933322017-09-22 23:46:57 +0000246#endif // LLVM_LIB_CODEGEN_INTERFERENCECACHE_H