LLVM 22.0.0git
RDFRegisters.h
Go to the documentation of this file.
1//===- RDFRegisters.h -------------------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CODEGEN_RDFREGISTERS_H
10#define LLVM_CODEGEN_RDFREGISTERS_H
11
12#include "llvm/ADT/BitVector.h"
13#include "llvm/ADT/STLExtras.h"
16#include "llvm/MC/LaneBitmask.h"
17#include "llvm/MC/MCRegister.h"
18#include <cassert>
19#include <cstdint>
20#include <map>
21#include <set>
22#include <vector>
23
24namespace llvm {
25
26class MachineFunction;
27class raw_ostream;
28
29namespace rdf {
30struct RegisterAggr;
31
33
34template <typename T>
35bool disjoint(const std::set<T> &A, const std::set<T> &B) {
36 auto ItA = A.begin(), EndA = A.end();
37 auto ItB = B.begin(), EndB = B.end();
38 while (ItA != EndA && ItB != EndB) {
39 if (*ItA < *ItB)
40 ++ItA;
41 else if (*ItB < *ItA)
42 ++ItB;
43 else
44 return false;
45 }
46 return true;
47}
48
49// Template class for a map translating uint32_t into arbitrary types.
50// The map will act like an indexed set: upon insertion of a new object,
51// it will automatically assign a new index to it. Index of 0 is treated
52// as invalid and is never allocated.
53template <typename T, unsigned N = 32> struct IndexedSet {
54 IndexedSet() { Map.reserve(N); }
55
56 T get(uint32_t Idx) const {
57 // Index Idx corresponds to Map[Idx-1].
58 assert(Idx != 0 && !Map.empty() && Idx - 1 < Map.size());
59 return Map[Idx - 1];
60 }
61
63 // Linear search.
64 auto F = llvm::find(Map, Val);
65 if (F != Map.end())
66 return F - Map.begin() + 1;
67 Map.push_back(Val);
68 return Map.size(); // Return actual_index + 1.
69 }
70
71 uint32_t find(T Val) const {
72 auto F = llvm::find(Map, Val);
73 assert(F != Map.end());
74 return F - Map.begin() + 1;
75 }
76
77 uint32_t size() const { return Map.size(); }
78
79 using const_iterator = typename std::vector<T>::const_iterator;
80
81 const_iterator begin() const { return Map.begin(); }
82 const_iterator end() const { return Map.end(); }
83
84private:
85 std::vector<T> Map;
86};
87
90 LaneBitmask Mask = LaneBitmask::getNone(); // Only for registers.
91
92 constexpr RegisterRef() = default;
93 constexpr explicit RegisterRef(RegisterId R,
95 : Reg(R), Mask(isRegId(R) && R != 0 ? M : LaneBitmask::getNone()) {}
96
97 // Classify null register as a "register".
98 constexpr bool isReg() const { return Reg == 0 || isRegId(Reg); }
99 constexpr bool isUnit() const { return isUnitId(Reg); }
100 constexpr bool isMask() const { return isMaskId(Reg); }
101
102 constexpr unsigned idx() const { return toIdx(Reg); }
103
104 constexpr operator bool() const {
105 return !isReg() || (Reg != 0 && Mask.any());
106 }
107
108 size_t hash() const {
109 return std::hash<RegisterId>{}(Reg) ^
110 std::hash<LaneBitmask::Type>{}(Mask.getAsInteger());
111 }
112
113 static constexpr bool isRegId(unsigned Id) {
115 }
116 static constexpr bool isUnitId(unsigned Id) {
118 }
119 static constexpr bool isMaskId(unsigned Id) { return Register(Id).isStack(); }
120
121 static constexpr RegisterId toUnitId(unsigned Idx) {
123 }
124
125 static constexpr unsigned toIdx(RegisterId Id) {
126 // Not using virtReg2Index or stackSlot2Index, because they are
127 // not constexpr.
128 if (isUnitId(Id))
129 return Id & ~Register::VirtualRegFlag;
130 // RegId and MaskId are unchanged.
131 return Id;
132 }
133
134 bool operator<(RegisterRef) const = delete;
135 bool operator==(RegisterRef) const = delete;
136 bool operator!=(RegisterRef) const = delete;
137};
138
141 const MachineFunction &mf);
142
144 return Register::index2StackSlot(RegMasks.find(RM));
145 }
146
148 return RegMasks.get(Register(R).stackSlotIndex());
149 }
150
151 bool alias(RegisterRef RA, RegisterRef RB) const;
152
153 // Returns the set of aliased physical registers.
154 std::set<RegisterId> getAliasSet(RegisterId Reg) const;
155
157 return RegisterRef(UnitInfos[U].Reg, UnitInfos[U].Mask);
158 }
159
160 const BitVector &getMaskUnits(RegisterId MaskId) const {
161 return MaskInfos[Register(MaskId).stackSlotIndex()].Units;
162 }
163
164 std::set<RegisterId> getUnits(RegisterRef RR) const;
165
167 return AliasInfos[U].Regs;
168 }
169
170 RegisterRef mapTo(RegisterRef RR, unsigned R) const;
171 const TargetRegisterInfo &getTRI() const { return TRI; }
172
173 bool equal_to(RegisterRef A, RegisterRef B) const;
174 bool less(RegisterRef A, RegisterRef B) const;
175
176 void print(raw_ostream &OS, RegisterRef A) const;
177 void print(raw_ostream &OS, const RegisterAggr &A) const;
178
179private:
180 struct RegInfo {
181 const TargetRegisterClass *RegClass = nullptr;
182 };
183 struct UnitInfo {
184 RegisterId Reg = 0;
185 LaneBitmask Mask;
186 };
187 struct MaskInfo {
188 BitVector Units;
189 };
190 struct AliasInfo {
191 BitVector Regs;
192 };
193
194 const TargetRegisterInfo &TRI;
195 IndexedSet<const uint32_t *> RegMasks;
196 std::vector<RegInfo> RegInfos;
197 std::vector<UnitInfo> UnitInfos;
198 std::vector<MaskInfo> MaskInfos;
199 std::vector<AliasInfo> AliasInfos;
200};
201
204 : Units(pri.getTRI().getNumRegUnits()), PRI(pri) {}
205 RegisterAggr(const RegisterAggr &RG) = default;
206
207 unsigned size() const { return Units.count(); }
208 bool empty() const { return Units.none(); }
209 bool hasAliasOf(RegisterRef RR) const;
210 bool hasCoverOf(RegisterRef RR) const;
211
212 const PhysicalRegisterInfo &getPRI() const { return PRI; }
213
214 bool operator==(const RegisterAggr &A) const {
215 return DenseMapInfo<BitVector>::isEqual(Units, A.Units);
216 }
217
219 const PhysicalRegisterInfo &PRI) {
220 return RegisterAggr(PRI).insert(RA).hasCoverOf(RB);
221 }
222
224 RegisterAggr &insert(const RegisterAggr &RG);
228 RegisterAggr &clear(const RegisterAggr &RG);
229
232 RegisterRef makeRegRef() const;
233
234 size_t hash() const { return DenseMapInfo<BitVector>::getHashValue(Units); }
235
237 using MapType = std::map<RegisterId, LaneBitmask>;
238
239 private:
240 MapType Masks;
241 MapType::iterator Pos;
242 unsigned Index;
243 const RegisterAggr *Owner;
244
245 public:
246 ref_iterator(const RegisterAggr &RG, bool End);
247
249 return RegisterRef(Pos->first, Pos->second);
250 }
251
253 ++Pos;
254 ++Index;
255 return *this;
256 }
257
258 bool operator==(const ref_iterator &I) const {
259 assert(Owner == I.Owner);
260 (void)Owner;
261 return Index == I.Index;
262 }
263
264 bool operator!=(const ref_iterator &I) const { return !(*this == I); }
265 };
266
267 ref_iterator ref_begin() const { return ref_iterator(*this, false); }
268 ref_iterator ref_end() const { return ref_iterator(*this, true); }
269
271 unit_iterator unit_begin() const { return Units.set_bits_begin(); }
272 unit_iterator unit_end() const { return Units.set_bits_end(); }
273
275 return make_range(ref_begin(), ref_end());
276 }
278 return make_range(unit_begin(), unit_end());
279 }
280
281private:
282 BitVector Units;
283 const PhysicalRegisterInfo &PRI;
284};
285
286// This is really a std::map, except that it provides a non-trivial
287// default constructor to the element accessed via [].
288template <typename KeyType> struct RegisterAggrMap {
289 RegisterAggrMap(const PhysicalRegisterInfo &pri) : Empty(pri) {}
290
291 RegisterAggr &operator[](KeyType Key) {
292 return Map.emplace(Key, Empty).first->second;
293 }
294
295 auto begin() { return Map.begin(); }
296 auto end() { return Map.end(); }
297 auto begin() const { return Map.begin(); }
298 auto end() const { return Map.end(); }
299 auto find(const KeyType &Key) const { return Map.find(Key); }
300
301private:
302 RegisterAggr Empty;
303 std::map<KeyType, RegisterAggr> Map;
304
305public:
306 using key_type = typename decltype(Map)::key_type;
307 using mapped_type = typename decltype(Map)::mapped_type;
308 using value_type = typename decltype(Map)::value_type;
309};
310
312
313// Print the lane mask in a short form (or not at all if all bits are set).
317};
319
320} // end namespace rdf
321} // end namespace llvm
322
323namespace std {
324
325template <> struct hash<llvm::rdf::RegisterRef> {
327 return A.hash();
328 }
329};
330
331template <> struct hash<llvm::rdf::RegisterAggr> {
332 size_t operator()(const llvm::rdf::RegisterAggr &A) const { //
333 return A.hash();
334 }
335};
336
337template <> struct equal_to<llvm::rdf::RegisterRef> {
338 constexpr equal_to(const llvm::rdf::PhysicalRegisterInfo &pri) : PRI(&pri) {}
339
341 return PRI->equal_to(A, B);
342 }
343
344private:
345 // Make it a pointer just in case. See comment in `less` below.
347};
348
349template <> struct equal_to<llvm::rdf::RegisterAggr> {
351 const llvm::rdf::RegisterAggr &B) const {
352 return A == B;
353 }
354};
355
356template <> struct less<llvm::rdf::RegisterRef> {
357 constexpr less(const llvm::rdf::PhysicalRegisterInfo &pri) : PRI(&pri) {}
358
360 return PRI->less(A, B);
361 }
362
363private:
364 // Make it a pointer because apparently some versions of MSVC use std::swap
365 // on the std::less specialization.
367};
368
369} // namespace std
370
371namespace llvm::rdf {
372using RegisterSet = std::set<RegisterRef, std::less<RegisterRef>>;
373} // namespace llvm::rdf
374
375#endif // LLVM_CODEGEN_RDFREGISTERS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
bool End
Definition: ELF_riscv.cpp:480
A common definition of LaneBitmask for use in TableGen and CodeGen.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Register Reg
#define P(N)
SI optimize exec mask operations pre RA
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
size_type count() const
count - Returns the number of bits which are set.
Definition: BitVector.h:162
const_set_bits_iterator set_bits_end() const
Definition: BitVector.h:137
const_set_bits_iterator_impl< BitVector > const_set_bits_iterator
Definition: BitVector.h:131
bool none() const
none - Returns true if none of the bits are set.
Definition: BitVector.h:188
const_set_bits_iterator set_bits_begin() const
Definition: BitVector.h:134
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isStack() const
Return true if this is a stack slot.
Definition: Register.h:43
int stackSlotIndex() const
Compute the frame index from a register value representing a stack slot.
Definition: Register.h:88
static Register index2StackSlot(int FI)
Convert a non-negative frame index to a stack slot register value.
Definition: Register.h:48
static constexpr unsigned VirtualRegFlag
Definition: Register.h:40
static constexpr bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:61
static constexpr bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:55
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
std::set< RegisterRef > RegisterSet
Definition: RDFGraph.h:450
raw_ostream & operator<<(raw_ostream &OS, const Print< RegisterRef > &P)
Definition: RDFGraph.cpp:44
uint32_t RegisterId
Definition: RDFRegisters.h:32
bool disjoint(const std::set< T > &A, const std::set< T > &B)
Definition: RDFRegisters.h:35
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1770
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
#define N
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:54
static constexpr LaneBitmask getAll()
Definition: LaneBitmask.h:82
constexpr bool any() const
Definition: LaneBitmask.h:53
static constexpr LaneBitmask getNone()
Definition: LaneBitmask.h:81
constexpr Type getAsInteger() const
Definition: LaneBitmask.h:74
const_iterator end() const
Definition: RDFRegisters.h:82
typename std::vector< T >::const_iterator const_iterator
Definition: RDFRegisters.h:79
T get(uint32_t Idx) const
Definition: RDFRegisters.h:56
uint32_t size() const
Definition: RDFRegisters.h:77
uint32_t insert(T Val)
Definition: RDFRegisters.h:62
const_iterator begin() const
Definition: RDFRegisters.h:81
uint32_t find(T Val) const
Definition: RDFRegisters.h:71
const BitVector & getMaskUnits(RegisterId MaskId) const
Definition: RDFRegisters.h:160
RegisterId getRegMaskId(const uint32_t *RM) const
Definition: RDFRegisters.h:143
void print(raw_ostream &OS, RegisterRef A) const
const TargetRegisterInfo & getTRI() const
Definition: RDFRegisters.h:171
const uint32_t * getRegMaskBits(RegisterId R) const
Definition: RDFRegisters.h:147
std::set< RegisterId > getAliasSet(RegisterId Reg) const
const BitVector & getUnitAliases(uint32_t U) const
Definition: RDFRegisters.h:166
bool equal_to(RegisterRef A, RegisterRef B) const
bool alias(RegisterRef RA, RegisterRef RB) const
RegisterRef mapTo(RegisterRef RR, unsigned R) const
bool less(RegisterRef A, RegisterRef B) const
std::set< RegisterId > getUnits(RegisterRef RR) const
RegisterRef getRefForUnit(uint32_t U) const
Definition: RDFRegisters.h:156
PrintLaneMaskShort(LaneBitmask M)
Definition: RDFRegisters.h:315
RegisterAggrMap(const PhysicalRegisterInfo &pri)
Definition: RDFRegisters.h:289
auto find(const KeyType &Key) const
Definition: RDFRegisters.h:299
typename decltype(Map)::mapped_type mapped_type
Definition: RDFRegisters.h:307
typename decltype(Map)::key_type key_type
Definition: RDFRegisters.h:306
RegisterAggr & operator[](KeyType Key)
Definition: RDFRegisters.h:291
typename decltype(Map)::value_type value_type
Definition: RDFRegisters.h:308
std::map< RegisterId, LaneBitmask > MapType
Definition: RDFRegisters.h:237
bool operator!=(const ref_iterator &I) const
Definition: RDFRegisters.h:264
bool operator==(const ref_iterator &I) const
Definition: RDFRegisters.h:258
iterator_range< ref_iterator > refs() const
Definition: RDFRegisters.h:274
RegisterAggr & insert(RegisterRef RR)
iterator_range< unit_iterator > units() const
Definition: RDFRegisters.h:277
RegisterAggr(const PhysicalRegisterInfo &pri)
Definition: RDFRegisters.h:203
unsigned size() const
Definition: RDFRegisters.h:207
const PhysicalRegisterInfo & getPRI() const
Definition: RDFRegisters.h:212
RegisterRef clearIn(RegisterRef RR) const
unit_iterator unit_end() const
Definition: RDFRegisters.h:272
RegisterAggr(const RegisterAggr &RG)=default
RegisterAggr & clear(RegisterRef RR)
ref_iterator ref_begin() const
Definition: RDFRegisters.h:267
RegisterRef makeRegRef() const
RegisterAggr & intersect(RegisterRef RR)
bool hasAliasOf(RegisterRef RR) const
RegisterRef intersectWith(RegisterRef RR) const
bool operator==(const RegisterAggr &A) const
Definition: RDFRegisters.h:214
bool hasCoverOf(RegisterRef RR) const
unit_iterator unit_begin() const
Definition: RDFRegisters.h:271
static bool isCoverOf(RegisterRef RA, RegisterRef RB, const PhysicalRegisterInfo &PRI)
Definition: RDFRegisters.h:218
ref_iterator ref_end() const
Definition: RDFRegisters.h:268
typename BitVector::const_set_bits_iterator unit_iterator
Definition: RDFRegisters.h:270
constexpr unsigned idx() const
Definition: RDFRegisters.h:102
constexpr RegisterRef(RegisterId R, LaneBitmask M=LaneBitmask::getAll())
Definition: RDFRegisters.h:93
bool operator!=(RegisterRef) const =delete
static constexpr RegisterId toUnitId(unsigned Idx)
Definition: RDFRegisters.h:121
constexpr RegisterRef()=default
constexpr bool isReg() const
Definition: RDFRegisters.h:98
constexpr bool isMask() const
Definition: RDFRegisters.h:100
static constexpr bool isMaskId(unsigned Id)
Definition: RDFRegisters.h:119
static constexpr bool isRegId(unsigned Id)
Definition: RDFRegisters.h:113
static constexpr bool isUnitId(unsigned Id)
Definition: RDFRegisters.h:116
constexpr bool isUnit() const
Definition: RDFRegisters.h:99
static constexpr unsigned toIdx(RegisterId Id)
Definition: RDFRegisters.h:125
size_t hash() const
Definition: RDFRegisters.h:108
bool operator<(RegisterRef) const =delete
bool operator==(RegisterRef) const =delete
bool operator()(const llvm::rdf::RegisterAggr &A, const llvm::rdf::RegisterAggr &B) const
Definition: RDFRegisters.h:350
bool operator()(llvm::rdf::RegisterRef A, llvm::rdf::RegisterRef B) const
Definition: RDFRegisters.h:340
constexpr equal_to(const llvm::rdf::PhysicalRegisterInfo &pri)
Definition: RDFRegisters.h:338
size_t operator()(const llvm::rdf::RegisterAggr &A) const
Definition: RDFRegisters.h:332
size_t operator()(llvm::rdf::RegisterRef A) const
Definition: RDFRegisters.h:326
bool operator()(llvm::rdf::RegisterRef A, llvm::rdf::RegisterRef B) const
Definition: RDFRegisters.h:359
constexpr less(const llvm::rdf::PhysicalRegisterInfo &pri)
Definition: RDFRegisters.h:357