LLVM 24.0.0git
PISARegisterInfo.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This file implements register information for the PISA target.
11///
12//===----------------------------------------------------------------------===//
13
14#include "PISARegisterInfo.h"
15#include "PISA.h"
16#include "PISASubtarget.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/StringRef.h"
21
22#define GET_REGINFO_TARGET_DESC
23#include "PISAGenRegisterInfo.inc"
24
25using namespace llvm;
26
27namespace {
28struct SpecialRegEntry {
30};
31
32#ifdef __GNUC__
33#pragma GCC diagnostic push
34#pragma GCC diagnostic ignored "-Wunused-function"
35#endif
36#define GET_SpecialRegNames_IMPL
37#include "PISAGenSearchableTables.inc"
38#ifdef __GNUC__
39#pragma GCC diagnostic pop
40#endif
41} // anonymous namespace
42
43static_assert(PISA::NUM_TARGET_SUBREGS == 63, "updated needed!");
45PISARegisterInfo::getSwizzleMap() {
46 static const DenseMap<unsigned, SwizzleDesc> SwizzleMap = {
47 {PISA::NoSubRegister, {PISA::Swizzle::NONE, nullptr}},
48 {PISA::sub8_0, {PISA::Swizzle::X, ".x"}},
49 {PISA::sub8_1, {PISA::Swizzle::Y, ".y"}},
50 {PISA::sub8_2, {PISA::Swizzle::Z, ".z"}},
51 {PISA::sub8_3, {PISA::Swizzle::W, ".w"}},
52 {PISA::sub16_0, {PISA::Swizzle::X, ".x"}},
53 {PISA::sub16_1, {PISA::Swizzle::Y, ".y"}},
54 {PISA::sub16_2, {PISA::Swizzle::Z, ".z"}},
55 {PISA::sub16_3, {PISA::Swizzle::W, ".w"}},
56 {PISA::sub32_0, {PISA::Swizzle::X, ".x"}},
57 {PISA::sub32_1, {PISA::Swizzle::Y, ".y"}},
58 {PISA::sub32_2, {PISA::Swizzle::Z, ".z"}},
59 {PISA::sub32_3, {PISA::Swizzle::W, ".w"}},
60 {PISA::sub64_0, {PISA::Swizzle::X, ".x"}},
61 {PISA::sub64_1, {PISA::Swizzle::Y, ".y"}},
62 {PISA::sub64_2, {PISA::Swizzle::Z, ".z"}},
63 {PISA::sub64_3, {PISA::Swizzle::W, ".w"}},
64 {PISA::sub8_xy, {PISA::Swizzle::XY, ".xy"}},
65 {PISA::sub8_zw, {PISA::Swizzle::ZW, ".zw"}},
66 {PISA::sub16_xy, {PISA::Swizzle::XY, ".xy"}},
67 {PISA::sub16_zw, {PISA::Swizzle::ZW, ".zw"}},
68 {PISA::sub32_xy, {PISA::Swizzle::XY, ".xy"}},
69 {PISA::sub32_zw, {PISA::Swizzle::ZW, ".zw"}},
70 {PISA::sub64_xy, {PISA::Swizzle::XY, ".xy"}},
71 {PISA::sub64_zw, {PISA::Swizzle::ZW, ".zw"}},
72 };
73 return SwizzleMap;
74}
75
77 MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg,
78 const TargetRegisterClass *DstRC, unsigned DstSubReg,
79 const TargetRegisterClass *NewRC, LiveIntervals &LIS) const {
80
81 if (!MI->isCopy())
82 return false;
83
84 const DenseMap<unsigned, SwizzleDesc> &SwizzleMap = getSwizzleMap();
85 return SwizzleMap.contains(SubReg) && SwizzleMap.contains(DstSubReg);
86}
87
89 // Tablegen can sometimes synthesize register classes if you don't set
90 // subregs explicitly on some regs. For now at least, we probably want to
91 // be explicit about what regclasses exist. If you added a register class
92 // explicitly, go ahead and update this number. If not, you might want to
93 // figure out what happened.
94 // New reg classes must also be reflected in PISATargetLowering constructor
95 static_assert(std::size(PISAMCRegisterClassStorage.Classes) == 29);
96 unsigned NumRCs = getNumRegClasses();
97 for (unsigned I = 0; I < NumRCs; I++) {
99 std::unique_ptr<RegClassDescription> RCD =
100 std::make_unique<RegClassDescription>();
101 if (RC == &PISA::RegV64_32bRegClass) {
102 // RegV64_32b has no per-element sub-register structure (LLVM's
103 // LaneBitmask cannot represent 64 lanes), so derive the metadata
104 // from the known type rather than from lane masks.
105 RCD->NumElements = 64;
106 RCD->ScalarBitSize = 32;
107 } else {
108 RCD->NumElements = RC->LaneMask.getNumLanes();
109 RCD->ScalarBitSize = getScalarBitSize(RC, RCD->NumElements);
110 }
111 std::pair<unsigned, unsigned> Key =
112 std::make_pair(RCD->NumElements, RCD->ScalarBitSize);
113 if (!VecRegClassMap[Key])
114 VecRegClassMap[std::make_pair(RCD->NumElements, RCD->ScalarBitSize)] = RC;
115 RegClassMap[RC] = std::move(RCD);
116 }
117
118 // Precompute special register BitVector for O(1) lookup.
119 SpecialRegs.resize(getNumRegs());
120 for (unsigned I = 1, E = getNumRegs(); I < E; ++I) {
122 if (lookupSpecialRegByName(getName(Reg)))
123 SpecialRegs.set(I);
124 }
125}
126
128 // Reserve special registers (hardware-defined values like %localid,
129 // %groupid, etc. that appear in machine instructions without explicit
130 // definitions).
131 //
132 // General-purpose physical registers are left unreserved so that
133 // RegisterClassInfo::getNumAllocatableRegs returns non-zero values for
134 // every register class. This is required because the register coalescer
135 // refuses to coalesce into register classes with zero allocatable
136 // registers. PISA works entirely with virtual registers and disables
137 // physical register allocation (createTargetRegisterAllocator returns
138 // nullptr), so unreserved physical registers are never actually allocated.
139 BitVector Reserved(getNumRegs());
140 Reserved |= SpecialRegs;
141 return Reserved;
142}
143
144const MCPhysReg *
146 static const MCPhysReg CalleeSavedReg = {0};
147 return &CalleeSavedReg;
148}
149
150unsigned PISARegisterInfo::getScalarBitSize(const TargetRegisterClass *RC,
151 unsigned NumElts) const {
152 unsigned RegSizeInBits = getRegSizeInBits(*RC);
153 assert((RegSizeInBits % NumElts == 0) && "not divisible?");
154 return RegSizeInBits / NumElts;
155}
156
157unsigned PISARegisterInfo::getSubRegIdx(unsigned Size, unsigned Idx) const {
158 assert(Idx < 4 && "Only 4 sub-registers supported!");
159
160 static unsigned Subregs[4][4] = {
161 {PISA::sub8_0, PISA::sub8_1, PISA::sub8_2, PISA::sub8_3},
162 {PISA::sub16_0, PISA::sub16_1, PISA::sub16_2, PISA::sub16_3},
163 {PISA::sub32_0, PISA::sub32_1, PISA::sub32_2, PISA::sub32_3},
164 {PISA::sub64_0, PISA::sub64_1, PISA::sub64_2, PISA::sub64_3}};
165
166 switch (Size) {
167 case 8:
168 return Subregs[0][Idx];
169 case 16:
170 return Subregs[1][Idx];
171 case 32:
172 return Subregs[2][Idx];
173 case 64:
174 return Subregs[3][Idx];
175 default:
176 llvm_unreachable("unknown type size!");
177 }
178}
179
181 unsigned Count) const {
182 // Only nameable 2-element pairs: .xy (base 0) and .zw (base 2).
183 if (Count != 2 || (Base != 0 && Base != 2))
184 return 0;
185 bool Low = (Base == 0);
186 switch (Size) {
187 case 8:
188 return Low ? PISA::sub8_xy : PISA::sub8_zw;
189 case 16:
190 return Low ? PISA::sub16_xy : PISA::sub16_zw;
191 case 32:
192 return Low ? PISA::sub32_xy : PISA::sub32_zw;
193 case 64:
194 return Low ? PISA::sub64_xy : PISA::sub64_zw;
195 default:
196 return 0;
197 }
198}
199
201 const DenseMap<unsigned, SwizzleDesc> &SwizzleMap = getSwizzleMap();
203 SwizzleMap.find(SubReg);
204 assert((Swizzle != SwizzleMap.end()) && "invalid swizzle!");
205 return Swizzle->second.Swizzle;
206}
207
208const char *PISARegisterInfo::getSwizzleName(unsigned SubReg) const {
209 const DenseMap<unsigned, SwizzleDesc> &SwizzleMap = getSwizzleMap();
211 SwizzleMap.find(SubReg);
212 assert((Swizzle != SwizzleMap.end()) && "invalid swizzle!");
213 return Swizzle->second.SwizzleName;
214}
215
217 switch (Swizzle) {
218 case PISA::Swizzle::X:
219 case PISA::Swizzle::Y:
220 case PISA::Swizzle::Z:
221 case PISA::Swizzle::W:
222 return true;
223 default:
224 return false;
225 }
226}
227
229 if (Ty.isScalar() || Ty.isPointer()) {
230 switch (Ty.getSizeInBits()) {
231 case 1:
232 return &PISA::PredRegClass;
233 case 8:
234 return &PISA::Reg8bRegClass;
235 case 16:
236 return &PISA::Reg16bRegClass;
237 case 32:
238 return &PISA::Reg32bRegClass;
239 case 64:
240 return &PISA::Reg64bRegClass;
241 case 128:
242 return &PISA::Reg128bRegClass;
243 default:
244 break;
245 }
246 } else if (Ty.isVector()) {
247 unsigned NumElts = Ty.getNumElements();
248 unsigned BitSize = Ty.getScalarSizeInBits();
249 return getVectorRegClass(NumElts, BitSize);
250 }
251
252 llvm_unreachable("unhandled LLT!");
253}
254
255unsigned
258 std::unique_ptr<RegClassDescription>>::const_iterator I =
259 RegClassMap.find(RC);
260 assert(I != RegClassMap.end());
261 const std::unique_ptr<RegClassDescription> &RCD = I->second;
262 return RCD->NumElements;
263}
264
265unsigned
268 std::unique_ptr<RegClassDescription>>::const_iterator I =
269 RegClassMap.find(RC);
270 assert(I != RegClassMap.end());
271 const std::unique_ptr<RegClassDescription> &RCD = I->second;
272 return RCD->ScalarBitSize;
273}
274
276PISARegisterInfo::getVectorRegClass(unsigned NumElts, unsigned BitSize) const {
277 std::pair<unsigned, unsigned> P = std::make_pair(NumElts, BitSize);
280 VecRegClassMap.find(P);
281 assert(I != VecRegClassMap.end());
282 return I->second;
283}
284
286 return Reg.isPhysical() && SpecialRegs.test(Reg.asMCReg());
287}
288
291 const TargetRegisterClass *B,
292 unsigned SubIdx) const {
293 switch (SubIdx) {
294 default:
295 return PISAGenRegisterInfo::getMatchingSuperRegClass(A, B, SubIdx);
296 // generic code (above) is unable to find 'hi' subreg
297 // index within 2-element vector ('.zw' within v2b?).
298 case PISA::sub8_zw:
299 case PISA::sub16_zw:
300 case PISA::sub32_zw:
301 case PISA::sub64_zw:
302 return PISAGenRegisterInfo::getSubClassWithSubReg(A, SubIdx);
303 }
304}
static const TargetRegisterClass * getRegClass(const MachineInstr &MI, Register Reg)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
#define P(N)
This file declares register information for the PISA target.
This file declares the PISA subtarget.
This file declares the top-level interfaces for the PISA target.
static StringRef getName(Value *V)
static const MCPhysReg CalleeSavedReg
uint16_t RegSizeInBits(const MCRegisterInfo &MRI, MCRegister RegNo)
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:757
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:679
iterator end()
Definition DenseMap.h:686
const LaneBitmask LaneMask
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
Representation of each machine instruction.
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
unsigned getBitSizeFromRegClass(const TargetRegisterClass *RC) const
const char * getSwizzleName(unsigned SubReg) const
bool shouldCoalesce(MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg, const TargetRegisterClass *DstRC, unsigned DstSubReg, const TargetRegisterClass *NewRC, LiveIntervals &LIS) const override
unsigned getNumEltsFromRegClass(const TargetRegisterClass *RC) const
const TargetRegisterClass * getMatchingSuperRegClass(const TargetRegisterClass *A, const TargetRegisterClass *B, unsigned SubIdx) const override
const TargetRegisterClass * getVectorRegClass(unsigned NumElts, unsigned BitSize) const
bool isSpecialReg(Register Reg) const
static bool isSelectorSwizzle(PISA::Swizzle Swizzle)
PISA::Swizzle getSwizzle(unsigned SubReg) const
const TargetRegisterClass * getRegClassFromLLT(LLT Ty) const
unsigned getSubRegIdx(unsigned Size, unsigned Elt) const
unsigned getCompositeSubRegIdx(unsigned Size, unsigned Base, unsigned Count) const
BitVector getReservedRegs(const MachineFunction &MF) const override
Wrapper class representing virtual and physical registers.
Definition Register.h:20
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
unsigned getNumLanes() const
Definition LaneBitmask.h:76