Skip to content

Commit ddc747a

Browse files
committed
Use template functions
1 parent 0d57351 commit ddc747a

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

llvm/lib/CodeGen/TargetRegisterInfo.cpp

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -201,79 +201,83 @@ TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
201201
return nullptr;
202202
}
203203

204-
/// getMinimalPhysRegClass - Returns the Register Class of a physical
205-
/// register of the given type, picking the most sub register class of
206-
/// the right type that contains this physreg.
207-
const TargetRegisterClass *
208-
TargetRegisterInfo::getMinimalPhysRegClass(MCRegister reg, MVT VT) const {
209-
assert(Register::isPhysicalRegister(reg) &&
204+
template <typename TypeT>
205+
static const TargetRegisterClass *
206+
getMinimalPhysRegClass(const TargetRegisterInfo *TRI, MCRegister Reg,
207+
TypeT Ty) {
208+
static_assert(std::is_same_v<TypeT, MVT> || std::is_same_v<TypeT, LLT>);
209+
assert(Register::isPhysicalRegister(Reg) &&
210210
"reg must be a physical register");
211211

212+
bool IsDefault = [&]() {
213+
if constexpr (std::is_same_v<TypeT, MVT>)
214+
return Ty == MVT::Other;
215+
else
216+
return !Ty.isValid();
217+
}();
218+
212219
// Pick the most sub register class of the right type that contains
213220
// this physreg.
214-
const TargetRegisterClass* BestRC = nullptr;
215-
for (const TargetRegisterClass* RC : regclasses()) {
216-
if ((VT == MVT::Other || isTypeLegalForClass(*RC, VT)) &&
217-
RC->contains(reg) && (!BestRC || BestRC->hasSubClass(RC)))
221+
const TargetRegisterClass *BestRC = nullptr;
222+
for (const TargetRegisterClass *RC : TRI->regclasses()) {
223+
if ((IsDefault || TRI->isTypeLegalForClass(*RC, Ty)) && RC->contains(Reg) &&
224+
(!BestRC || BestRC->hasSubClass(RC)))
218225
BestRC = RC;
219226
}
220227

221-
assert(BestRC && "Couldn't find the register class");
228+
if constexpr (std::is_same_v<TypeT, MVT>)
229+
assert(BestRC && "Couldn't find the register class");
222230
return BestRC;
223231
}
224232

225-
const TargetRegisterClass *TargetRegisterInfo::getCommonMinimalPhysRegClass(
226-
MCRegister Reg1, MCRegister Reg2, MVT VT) const {
233+
template <typename TypeT>
234+
static const TargetRegisterClass *
235+
getCommonMinimalPhysRegClass(const TargetRegisterInfo *TRI, MCRegister Reg1,
236+
MCRegister Reg2, TypeT Ty) {
237+
static_assert(std::is_same_v<TypeT, MVT> || std::is_same_v<TypeT, LLT>);
227238
assert(Register::isPhysicalRegister(Reg1) &&
228239
Register::isPhysicalRegister(Reg2) &&
229240
"Reg1/Reg2 must be a physical register");
230241

242+
bool IsDefault = [&]() {
243+
if constexpr (std::is_same_v<TypeT, MVT>)
244+
return Ty == MVT::Other;
245+
else
246+
return !Ty.isValid();
247+
}();
248+
231249
// Pick the most sub register class of the right type that contains
232250
// this physreg.
233251
const TargetRegisterClass *BestRC = nullptr;
234-
for (const TargetRegisterClass *RC : regclasses()) {
235-
if ((VT == MVT::Other || isTypeLegalForClass(*RC, VT)) &&
252+
for (const TargetRegisterClass *RC : TRI->regclasses()) {
253+
if ((IsDefault || TRI->isTypeLegalForClass(*RC, Ty)) &&
236254
RC->contains(Reg1, Reg2) && (!BestRC || BestRC->hasSubClass(RC)))
237255
BestRC = RC;
238256
}
239257

240-
assert(BestRC && "Couldn't find the register class");
258+
if constexpr (std::is_same_v<TypeT, MVT>)
259+
assert(BestRC && "Couldn't find the register class");
241260
return BestRC;
242261
}
243262

244263
const TargetRegisterClass *
245-
TargetRegisterInfo::getMinimalPhysRegClassLLT(MCRegister reg, LLT Ty) const {
246-
assert(Register::isPhysicalRegister(reg) &&
247-
"reg must be a physical register");
264+
TargetRegisterInfo::getMinimalPhysRegClass(MCRegister Reg, MVT VT) const {
265+
return ::getMinimalPhysRegClass(this, Reg, VT);
266+
}
248267

249-
// Pick the most sub register class of the right type that contains
250-
// this physreg.
251-
const TargetRegisterClass *BestRC = nullptr;
252-
for (const TargetRegisterClass *RC : regclasses()) {
253-
if ((!Ty.isValid() || isTypeLegalForClass(*RC, Ty)) && RC->contains(reg) &&
254-
(!BestRC || BestRC->hasSubClass(RC)))
255-
BestRC = RC;
256-
}
268+
const TargetRegisterClass *TargetRegisterInfo::getCommonMinimalPhysRegClass(
269+
MCRegister Reg1, MCRegister Reg2, MVT VT) const {
270+
return ::getCommonMinimalPhysRegClass(this, Reg1, Reg2, VT);
271+
}
257272

258-
return BestRC;
273+
const TargetRegisterClass *
274+
TargetRegisterInfo::getMinimalPhysRegClassLLT(MCRegister Reg, LLT Ty) const {
275+
return ::getMinimalPhysRegClass(this, Reg, Ty);
259276
}
260277

261278
const TargetRegisterClass *TargetRegisterInfo::getCommonMinimalPhysRegClassLLT(
262279
MCRegister Reg1, MCRegister Reg2, LLT Ty) const {
263-
assert(Register::isPhysicalRegister(Reg1) &&
264-
Register::isPhysicalRegister(Reg2) &&
265-
"Reg1/Reg2 must be a physical register");
266-
267-
// Pick the most sub register class of the right type that contains
268-
// this physreg.
269-
const TargetRegisterClass *BestRC = nullptr;
270-
for (const TargetRegisterClass *RC : regclasses()) {
271-
if ((!Ty.isValid() || isTypeLegalForClass(*RC, Ty)) &&
272-
RC->contains(Reg1, Reg2) && (!BestRC || BestRC->hasSubClass(RC)))
273-
BestRC = RC;
274-
}
275-
276-
return BestRC;
280+
return ::getCommonMinimalPhysRegClass(this, Reg1, Reg2, Ty);
277281
}
278282

279283
/// getAllocatableSetForRC - Toggle the bits that represent allocatable

0 commit comments

Comments
 (0)