Skip to content

Conversation

@topperc
Copy link
Collaborator

@topperc topperc commented Nov 14, 2025

RegisterId can represent a physical register, a MCRegUnit, or
an index into a side structure that stores register masks. These 3
types were encoded by using the physical reg, stack slot, and
virtual register encoding partitions from the Register class.
This encoding scheme alias wasn't well contained so
Register::index2StackSlot and Register::stackSlotIndex appeared
in multiple places.

This patch gives RegisterRef its own encoding defines and separates
it from Register.

I've removed the generic idx() method in favor of getAsMCReg(),
getAsMCRegUnit(), and getMaskIdx() for some degree of type safety.

Some places used the RegisterId field of RegisterRef directly as a
register. Those have been updated to use getAsMCReg.

Some special cases for RegisterId 0 have been removed as it can
be treated like a MCRegister by existing code.

I think I want to rename the Reg field of RegisterRef to Id, but
I'll do that in another patch.

Additionally, callers of the RegisterRef constructor need to be
audited for implicit conversions from Register/MCRegister
to unsigned.

RegisterId can represent a physical register, a MCRegUnit, or
an index into a side structure that stores register masks. These 3
types were encoded by using the physical reg, stack slot, and
virtual register encoding partitions from the Register class.
This encoding scheme alias wasn't well contained so
Register::index2StackSlot and Register::stackSlotIndex appeared
in multiple places.

This patch gives RegisterRef its own encoding defines and separates
it from Register.

I've removed the generic idx() method in favor of getAsMCReg(),
getAsMCRegUnit(), and getMaskIdx() for some degree of type safety.

Some places used the RegisterId field of RegisterRef directly as a
register. Those have been updated to use getAsMCReg.

Some special cases for RegisterId 0 have been removed as it can
be treated like a MCRegister by existing code.

I think I want to rename the Reg field of RegisterRef to Id, but
I'll do that in another patch.

Additionally, callers of the RegisterRef constructor need to be
audited for implicit conversions from Register/MCRegister
to unsigned.
@topperc topperc changed the title Pr/mcregister/registerref [RDF] RegisterRef/RegisterId improvements. NFC Nov 14, 2025
@github-actions
Copy link

github-actions bot commented Nov 14, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

std::set<RegisterId> PhysicalRegisterInfo::getUnits(RegisterRef RR) const {
std::set<RegisterId> Units;

if (RR.Reg == 0)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RegisterId 0 is considered a register and the mask is forced to none, so we can let the RR.isReg(), RR.Mask.none() handle this.

if (A.isReg()) {
MCRegister Reg = A.asMCReg();
if (Reg && Reg.id() < TRI.getNumRegs())
OS << TRI.getName(Reg);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably use printReg for all cases, but it would slightly change the formatting so I didn't include it here.

static constexpr RegisterId toUnitId(unsigned Idx) {
return Idx | Register::VirtualRegFlag;
}
static constexpr RegisterId toUnitId(unsigned Idx) { return Idx | UnitFlag; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is unused. I wonder how UnitFlag gets set; if it is never set,asMCRegUnit() should be unreachable.
regunit support was introduced back in 2023 2af7036, but never used since? @kparzysz

Copy link
Contributor

@s-barannikov s-barannikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

return RegisterRef::toMaskId(RegMasks.find(RM));
}

const uint32_t *getRegMaskBits(RegisterId R) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about passing RegisterRef to this, getMaskUnits and getAliasSet to avoid reconstructing RegisterRef
But LGTM as is

@topperc topperc merged commit 7108b12 into llvm:main Nov 14, 2025
10 checks passed
@topperc topperc deleted the pr/mcregister/registerref branch November 14, 2025 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants