1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//===- RDFRegisters.h -------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CODEGEN_RDFREGISTERS_H
#define LLVM_CODEGEN_RDFREGISTERS_H
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/MC/LaneBitmask.h"
#include "llvm/MC/MCRegister.h"
#include <cassert>
#include <cstdint>
#include <map>
#include <set>
#include <vector>
namespace llvm {
class MachineFunction;
class raw_ostream;
namespace rdf {
struct RegisterAggr;
using RegisterId = uint32_t;
template <typename T>
bool disjoint(const std::set<T> &A, const std::set<T> &B) {
auto ItA = A.begin(), EndA = A.end();
auto ItB = B.begin(), EndB = B.end();
while (ItA != EndA && ItB != EndB) {
if (*ItA < *ItB)
++ItA;
else if (*ItB < *ItA)
++ItB;
else
return false;
}
return true;
}
// Template class for a map translating uint32_t into arbitrary types.
// The map will act like an indexed set: upon insertion of a new object,
// it will automatically assign a new index to it. Index of 0 is treated
// as invalid and is never allocated.
template <typename T, unsigned N = 32> struct IndexedSet {
IndexedSet() { Map.reserve(N); }
T get(uint32_t Idx) const {
// Index Idx corresponds to Map[Idx-1].
assert(Idx != 0 && !Map.empty() && Idx - 1 < Map.size());
return Map[Idx - 1];
}
uint32_t insert(T Val) {
// Linear search.
auto F = llvm::find(Map, Val);
if (F != Map.end())
return F - Map.begin() + 1;
Map.push_back(Val);
return Map.size(); // Return actual_index + 1.
}
uint32_t find(T Val) const {
auto F = llvm::find(Map, Val);
assert(F != Map.end());
return F - Map.begin() + 1;
}
uint32_t size() const { return Map.size(); }
using const_iterator = typename std::vector<T>::const_iterator;
const_iterator begin() const { return Map.begin(); }
const_iterator end() const { return Map.end(); }
private:
std::vector<T> Map;
};
struct RegisterRef {
RegisterId Reg = 0;
LaneBitmask Mask = LaneBitmask::getNone(); // Only for registers.
constexpr RegisterRef() = default;
constexpr explicit RegisterRef(RegisterId R,
LaneBitmask M = LaneBitmask::getAll())
: Reg(R), Mask(isRegId(R) && R != 0 ? M : LaneBitmask::getNone()) {}
// Classify null register as a "register".
constexpr bool isReg() const { return Reg == 0 || isRegId(Reg); }
constexpr bool isUnit() const { return isUnitId(Reg); }
constexpr bool isMask() const { return isMaskId(Reg); }
constexpr unsigned idx() const { return toIdx(Reg); }
constexpr operator bool() const {
return !isReg() || (Reg != 0 && Mask.any());
}
size_t hash() const {
return std::hash<RegisterId>{}(Reg) ^
std::hash<LaneBitmask::Type>{}(Mask.getAsInteger());
}
static constexpr bool isRegId(unsigned Id) {
return Register::isPhysicalRegister(Id);
}
static constexpr bool isUnitId(unsigned Id) {
return Register::isVirtualRegister(Id);
}
static constexpr bool isMaskId(unsigned Id) {
return Register::isStackSlot(Id);
}
static constexpr RegisterId toUnitId(unsigned Idx) {
return Idx | MCRegister::VirtualRegFlag;
}
static constexpr unsigned toIdx(RegisterId Id) {
// Not using virtReg2Index or stackSlot2Index, because they are
// not constexpr.
if (isUnitId(Id))
return Id & ~MCRegister::VirtualRegFlag;
// RegId and MaskId are unchanged.
return Id;
}
bool operator<(RegisterRef) const = delete;
bool operator==(RegisterRef) const = delete;
bool operator!=(RegisterRef) const = delete;
};
struct PhysicalRegisterInfo {
PhysicalRegisterInfo(const TargetRegisterInfo &tri,
const MachineFunction &mf);
RegisterId getRegMaskId(const uint32_t *RM) const {
return Register::index2StackSlot(RegMasks.find(RM));
}
const uint32_t *getRegMaskBits(RegisterId R) const {
return RegMasks.get(Register::stackSlot2Index(R));
}
bool alias(RegisterRef RA, RegisterRef RB) const;
// Returns the set of aliased physical registers.
std::set<RegisterId> getAliasSet(RegisterId Reg) const;
RegisterRef getRefForUnit(uint32_t U) const {
return RegisterRef(UnitInfos[U].Reg, UnitInfos[U].Mask);
}
const BitVector &getMaskUnits(RegisterId MaskId) const {
return MaskInfos[Register::stackSlot2Index(MaskId)].Units;
}
std::set<RegisterId> getUnits(RegisterRef RR) const;
const BitVector &getUnitAliases(uint32_t U) const {
return AliasInfos[U].Regs;
}
RegisterRef mapTo(RegisterRef RR, unsigned R) const;
const TargetRegisterInfo &getTRI() const { return TRI; }
bool equal_to(RegisterRef A, RegisterRef B) const;
bool less(RegisterRef A, RegisterRef B) const;
void print(raw_ostream &OS, RegisterRef A) const;
void print(raw_ostream &OS, const RegisterAggr &A) const;
private:
struct RegInfo {
const TargetRegisterClass *RegClass = nullptr;
};
struct UnitInfo {
RegisterId Reg = 0;
LaneBitmask Mask;
};
struct MaskInfo {
BitVector Units;
};
struct AliasInfo {
BitVector Regs;
};
const TargetRegisterInfo &TRI;
IndexedSet<const uint32_t *> RegMasks;
std::vector<RegInfo> RegInfos;
std::vector<UnitInfo> UnitInfos;
std::vector<MaskInfo> MaskInfos;
std::vector<AliasInfo> AliasInfos;
};
struct RegisterAggr {
RegisterAggr(const PhysicalRegisterInfo &pri)
: Units(pri.getTRI().getNumRegUnits()), PRI(pri) {}
RegisterAggr(const RegisterAggr &RG) = default;
unsigned size() const { return Units.count(); }
bool empty() const { return Units.none(); }
bool hasAliasOf(RegisterRef RR) const;
bool hasCoverOf(RegisterRef RR) const;
const PhysicalRegisterInfo &getPRI() const { return PRI; }
bool operator==(const RegisterAggr &A) const {
return DenseMapInfo<BitVector>::isEqual(Units, A.Units);
}
static bool isCoverOf(RegisterRef RA, RegisterRef RB,
const PhysicalRegisterInfo &PRI) {
return RegisterAggr(PRI).insert(RA).hasCoverOf(RB);
}
RegisterAggr &insert(RegisterRef RR);
RegisterAggr &insert(const RegisterAggr &RG);
RegisterAggr &intersect(RegisterRef RR);
RegisterAggr &intersect(const RegisterAggr &RG);
RegisterAggr &clear(RegisterRef RR);
RegisterAggr &clear(const RegisterAggr &RG);
RegisterRef intersectWith(RegisterRef RR) const;
RegisterRef clearIn(RegisterRef RR) const;
RegisterRef makeRegRef() const;
size_t hash() const { return DenseMapInfo<BitVector>::getHashValue(Units); }
struct ref_iterator {
using MapType = std::map<RegisterId, LaneBitmask>;
private:
MapType Masks;
MapType::iterator Pos;
unsigned Index;
const RegisterAggr *Owner;
public:
ref_iterator(const RegisterAggr &RG, bool End);
RegisterRef operator*() const {
return RegisterRef(Pos->first, Pos->second);
}
ref_iterator &operator++() {
++Pos;
++Index;
return *this;
}
bool operator==(const ref_iterator &I) const {
assert(Owner == I.Owner);
(void)Owner;
return Index == I.Index;
}
bool operator!=(const ref_iterator &I) const { return !(*this == I); }
};
ref_iterator ref_begin() const { return ref_iterator(*this, false); }
ref_iterator ref_end() const { return ref_iterator(*this, true); }
using unit_iterator = typename BitVector::const_set_bits_iterator;
unit_iterator unit_begin() const { return Units.set_bits_begin(); }
unit_iterator unit_end() const { return Units.set_bits_end(); }
iterator_range<ref_iterator> refs() const {
return make_range(ref_begin(), ref_end());
}
iterator_range<unit_iterator> units() const {
return make_range(unit_begin(), unit_end());
}
private:
BitVector Units;
const PhysicalRegisterInfo &PRI;
};
// This is really a std::map, except that it provides a non-trivial
// default constructor to the element accessed via [].
template <typename KeyType> struct RegisterAggrMap {
RegisterAggrMap(const PhysicalRegisterInfo &pri) : Empty(pri) {}
RegisterAggr &operator[](KeyType Key) {
return Map.emplace(Key, Empty).first->second;
}
auto begin() { return Map.begin(); }
auto end() { return Map.end(); }
auto begin() const { return Map.begin(); }
auto end() const { return Map.end(); }
auto find(const KeyType &Key) const { return Map.find(Key); }
private:
RegisterAggr Empty;
std::map<KeyType, RegisterAggr> Map;
public:
using key_type = typename decltype(Map)::key_type;
using mapped_type = typename decltype(Map)::mapped_type;
using value_type = typename decltype(Map)::value_type;
};
raw_ostream &operator<<(raw_ostream &OS, const RegisterAggr &A);
// Print the lane mask in a short form (or not at all if all bits are set).
struct PrintLaneMaskShort {
PrintLaneMaskShort(LaneBitmask M) : Mask(M) {}
LaneBitmask Mask;
};
raw_ostream &operator<<(raw_ostream &OS, const PrintLaneMaskShort &P);
} // end namespace rdf
} // end namespace llvm
namespace std {
template <> struct hash<llvm::rdf::RegisterRef> {
size_t operator()(llvm::rdf::RegisterRef A) const { //
return A.hash();
}
};
template <> struct hash<llvm::rdf::RegisterAggr> {
size_t operator()(const llvm::rdf::RegisterAggr &A) const { //
return A.hash();
}
};
template <> struct equal_to<llvm::rdf::RegisterRef> {
constexpr equal_to(const llvm::rdf::PhysicalRegisterInfo &pri) : PRI(&pri) {}
bool operator()(llvm::rdf::RegisterRef A, llvm::rdf::RegisterRef B) const {
return PRI->equal_to(A, B);
}
private:
// Make it a pointer just in case. See comment in `less` below.
const llvm::rdf::PhysicalRegisterInfo *PRI;
};
template <> struct equal_to<llvm::rdf::RegisterAggr> {
bool operator()(const llvm::rdf::RegisterAggr &A,
const llvm::rdf::RegisterAggr &B) const {
return A == B;
}
};
template <> struct less<llvm::rdf::RegisterRef> {
constexpr less(const llvm::rdf::PhysicalRegisterInfo &pri) : PRI(&pri) {}
bool operator()(llvm::rdf::RegisterRef A, llvm::rdf::RegisterRef B) const {
return PRI->less(A, B);
}
private:
// Make it a pointer because apparently some versions of MSVC use std::swap
// on the std::less specialization.
const llvm::rdf::PhysicalRegisterInfo *PRI;
};
} // namespace std
namespace llvm::rdf {
using RegisterSet = std::set<RegisterRef, std::less<RegisterRef>>;
} // namespace llvm::rdf
#endif // LLVM_CODEGEN_RDFREGISTERS_H
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|