blob: 160e2b16e2943a995a05a2f38c2074eb0cba3539 [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//
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 Olesen96eebf02012-06-20 22:52:26 +000010// InterferenceCache remembers per-block interference from LiveIntervalUnions,
11// fixed RegUnit interference, and register masks.
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000012//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_LIB_CODEGEN_INTERFERENCECACHE_H
16#define LLVM_LIB_CODEGEN_INTERFERENCECACHE_H
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000017
Eugene Zelenkof1933322017-09-22 23:46:57 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/CodeGen/LiveInterval.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000020#include "llvm/CodeGen/LiveIntervalUnion.h"
Eugene Zelenkof1933322017-09-22 23:46:57 +000021#include "llvm/CodeGen/SlotIndexes.h"
22#include "llvm/Support/Compiler.h"
23#include <cassert>
24#include <cstddef>
25#include <cstdlib>
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000026
27namespace llvm {
28
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000029class LiveIntervals;
Eugene Zelenkof1933322017-09-22 23:46:57 +000030class MachineFunction;
31class TargetRegisterInfo;
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000032
Benjamin Kramerf4c20252015-07-01 14:47:39 +000033class LLVM_LIBRARY_VISIBILITY InterferenceCache {
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000034 /// BlockInterference - information about the interference in a single basic
35 /// block.
36 struct BlockInterference {
Eugene Zelenkof1933322017-09-22 23:46:57 +000037 unsigned Tag = 0;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000038 SlotIndex First;
39 SlotIndex Last;
Eugene Zelenkof1933322017-09-22 23:46:57 +000040
Eugene Zelenko8e30a1c2017-09-22 23:55:32 +000041 BlockInterference() {}
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000042 };
43
44 /// Entry - A cache entry containing interference information for all aliases
45 /// of PhysReg in all basic blocks.
46 class Entry {
47 /// PhysReg - The register currently represented.
Eugene Zelenkof1933322017-09-22 23:46:57 +000048 unsigned PhysReg = 0;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000049
50 /// Tag - Cache tag is changed when any of the underlying LiveIntervalUnions
51 /// change.
Eugene Zelenkof1933322017-09-22 23:46:57 +000052 unsigned Tag = 0;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000053
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +000054 /// RefCount - The total number of Cursor instances referring to this Entry.
Eugene Zelenkof1933322017-09-22 23:46:57 +000055 unsigned RefCount = 0;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +000056
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +000057 /// MF - The current function.
58 MachineFunction *MF;
59
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000060 /// Indexes - Mapping block numbers to SlotIndex ranges.
Eugene Zelenkof1933322017-09-22 23:46:57 +000061 SlotIndexes *Indexes = nullptr;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000062
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000063 /// LIS - Used for accessing register mask interference maps.
Eugene Zelenkof1933322017-09-22 23:46:57 +000064 LiveIntervals *LIS = nullptr;
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +000065
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000066 /// PrevPos - The previous position the iterators were moved to.
67 SlotIndex PrevPos;
68
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000069 /// RegUnitInfo - Information tracked about each RegUnit in PhysReg.
70 /// When PrevPos is set, the iterators are valid as if advanceTo(PrevPos)
71 /// had just been called.
72 struct RegUnitInfo {
73 /// Iterator pointing into the LiveIntervalUnion containing virtual
74 /// register interference.
75 LiveIntervalUnion::SegmentIter VirtI;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000076
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000077 /// Tag of the LIU last time we looked.
78 unsigned VirtTag;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000079
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000080 /// Fixed interference in RegUnit.
Eugene Zelenkof1933322017-09-22 23:46:57 +000081 LiveRange *Fixed = nullptr;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000082
83 /// Iterator pointing into the fixed RegUnit interference.
84 LiveInterval::iterator FixedI;
85
Eugene Zelenkof1933322017-09-22 23:46:57 +000086 RegUnitInfo(LiveIntervalUnion &LIU) : VirtTag(LIU.getTag()) {
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +000087 VirtI.setMap(LIU.getMap());
88 }
89 };
90
91 /// Info for each RegUnit in PhysReg. It is very rare ofr a PHysReg to have
92 /// more than 4 RegUnits.
93 SmallVector<RegUnitInfo, 4> RegUnits;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000094
95 /// Blocks - Interference for each block in the function.
96 SmallVector<BlockInterference, 8> Blocks;
97
98 /// update - Recompute Blocks[MBBNum]
99 void update(unsigned MBBNum);
100
101 public:
Eugene Zelenkof1933322017-09-22 23:46:57 +0000102 Entry() = default;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000103
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000104 void clear(MachineFunction *mf, SlotIndexes *indexes, LiveIntervals *lis) {
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000105 assert(!hasRefs() && "Cannot clear cache entry with references");
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000106 PhysReg = 0;
Jakob Stoklund Olesen4ad6c162011-04-09 02:59:05 +0000107 MF = mf;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000108 Indexes = indexes;
Jakob Stoklund Olesena16ae592012-02-10 18:58:34 +0000109 LIS = lis;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000110 }
111
112 unsigned getPhysReg() const { return PhysReg; }
113
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000114 void addRef(int Delta) { RefCount += Delta; }
115
116 bool hasRefs() const { return RefCount > 0; }
117
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000118 void revalidate(LiveIntervalUnion *LIUArray, const TargetRegisterInfo *TRI);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000119
120 /// valid - Return true if this is a valid entry for physReg.
121 bool valid(LiveIntervalUnion *LIUArray, const TargetRegisterInfo *TRI);
122
123 /// reset - Initialize entry to represent physReg's aliases.
124 void reset(unsigned physReg,
125 LiveIntervalUnion *LIUArray,
126 const TargetRegisterInfo *TRI,
127 const MachineFunction *MF);
128
129 /// get - Return an up to date BlockInterference.
130 BlockInterference *get(unsigned MBBNum) {
131 if (Blocks[MBBNum].Tag != Tag)
132 update(MBBNum);
133 return &Blocks[MBBNum];
134 }
135 };
136
137 // We don't keep a cache entry for every physical register, that would use too
138 // much memory. Instead, a fixed number of cache entries are used in a round-
139 // robin manner.
140 enum { CacheEntries = 32 };
141
Eugene Zelenkof1933322017-09-22 23:46:57 +0000142 const TargetRegisterInfo *TRI = nullptr;
143 LiveIntervalUnion *LIUArray = nullptr;
144 MachineFunction *MF = nullptr;
145
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000146 // Point to an entry for each physreg. The entry pointed to may not be up to
147 // date, and it may have been reused for a different physreg.
Eugene Zelenkof1933322017-09-22 23:46:57 +0000148 unsigned char* PhysRegEntries = nullptr;
149 size_t PhysRegEntriesCount = 0;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000150
151 // Next round-robin entry to be picked.
Eugene Zelenkof1933322017-09-22 23:46:57 +0000152 unsigned RoundRobin = 0;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000153
154 // The actual cache entries.
155 Entry Entries[CacheEntries];
156
157 // get - Get a valid entry for PhysReg.
158 Entry *get(unsigned PhysReg);
159
160public:
Eugene Zelenkof1933322017-09-22 23:46:57 +0000161 friend class Cursor;
162
163 InterferenceCache() = default;
Puyan Lotfi5eb10042014-02-06 09:23:24 +0000164
165 ~InterferenceCache() {
166 free(PhysRegEntries);
167 }
168
169 void reinitPhysRegEntries();
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000170
171 /// init - Prepare cache for a new function.
Eugene Zelenkof1933322017-09-22 23:46:57 +0000172 void init(MachineFunction *mf, LiveIntervalUnion *liuarray,
173 SlotIndexes *indexes, LiveIntervals *lis,
174 const TargetRegisterInfo *tri);
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000175
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000176 /// getMaxCursors - Return the maximum number of concurrent cursors that can
177 /// be supported.
178 unsigned getMaxCursors() const { return CacheEntries; }
179
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000180 /// Cursor - The primary query interface for the block interference cache.
181 class Cursor {
Eugene Zelenkof1933322017-09-22 23:46:57 +0000182 Entry *CacheEntry = nullptr;
183 const BlockInterference *Current = nullptr;
Benjamin Kramer57a3d082015-03-08 16:07:39 +0000184 static const BlockInterference NoInterference;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000185
186 void setEntry(Entry *E) {
Craig Topperada08572014-04-16 04:21:27 +0000187 Current = nullptr;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000188 // Update reference counts. Nothing happens when RefCount reaches 0, so
189 // we don't have to check for E == CacheEntry etc.
190 if (CacheEntry)
191 CacheEntry->addRef(-1);
192 CacheEntry = E;
193 if (CacheEntry)
194 CacheEntry->addRef(+1);
195 }
196
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000197 public:
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000198 /// Cursor - Create a dangling cursor.
Eugene Zelenkof1933322017-09-22 23:46:57 +0000199 Cursor() = default;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000200
Eugene Zelenkof1933322017-09-22 23:46:57 +0000201 Cursor(const Cursor &O) {
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000202 setEntry(O.CacheEntry);
203 }
204
205 Cursor &operator=(const Cursor &O) {
206 setEntry(O.CacheEntry);
207 return *this;
208 }
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000209
Eugene Zelenkof1933322017-09-22 23:46:57 +0000210 ~Cursor() { setEntry(nullptr); }
211
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000212 /// setPhysReg - Point this cursor to PhysReg's interference.
213 void setPhysReg(InterferenceCache &Cache, unsigned PhysReg) {
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000214 // Release reference before getting a new one. That guarantees we can
215 // actually have CacheEntries live cursors.
Craig Topperada08572014-04-16 04:21:27 +0000216 setEntry(nullptr);
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +0000217 if (PhysReg)
218 setEntry(Cache.get(PhysReg));
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000219 }
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000220
221 /// moveTo - Move cursor to basic block MBBNum.
222 void moveToBlock(unsigned MBBNum) {
Jakob Stoklund Olesencacefc72011-07-23 03:10:17 +0000223 Current = CacheEntry ? CacheEntry->get(MBBNum) : &NoInterference;
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000224 }
225
226 /// hasInterference - Return true if the current block has any interference.
227 bool hasInterference() {
228 return Current->First.isValid();
229 }
230
231 /// first - Return the starting index of the first interfering range in the
232 /// current block.
233 SlotIndex first() {
234 return Current->First;
235 }
236
237 /// last - Return the ending index of the last interfering range in the
238 /// current block.
239 SlotIndex last() {
240 return Current->Last;
241 }
242 };
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000243};
244
Eugene Zelenkof1933322017-09-22 23:46:57 +0000245} // end namespace llvm
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +0000246
Eugene Zelenkof1933322017-09-22 23:46:57 +0000247#endif // LLVM_LIB_CODEGEN_INTERFERENCECACHE_H