Skip to content

Commit

Permalink
[NFCI] Cleanup range checks in Register/MCRegister
Browse files Browse the repository at this point in the history
Summary:
by removing casts from unsigned to int that which may be implementation
defined according to C++14 (and thus trip up the XL compiler on AIX) by
just using unsigned comparisons/masks and refactor out the range
constants to cleanup things a bit while we are at it.

Reviewers: hubert.reinterpretcast, arsenm

Reviewed By: hubert.reinterpretcast

Subscribers: wdng, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D82197
  • Loading branch information
daltenty committed Jun 26, 2020
1 parent b537c81 commit 16dae81
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
17 changes: 9 additions & 8 deletions llvm/include/llvm/CodeGen/Register.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Register {
//
// Further sentinels can be allocated from the small negative integers.
// DenseMapInfo<unsigned> uses -1u and -2u.
static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
"Reg isn't large enough to hold full range.");

/// isStackSlot - Sometimes it is useful the be able to store a non-negative
/// frame index in a variable that normally holds a register. isStackSlot()
Expand All @@ -49,13 +51,13 @@ class Register {
/// Compute the frame index from a register value representing a stack slot.
static int stackSlot2Index(unsigned Reg) {
assert(isStackSlot(Reg) && "Not a stack slot");
return int(Reg - (1u << 30));
return int(Reg - MCRegister::FirstStackSlot);
}

/// Convert a non-negative frame index to a stack slot register value.
static unsigned index2StackSlot(int FI) {
assert(FI >= 0 && "Cannot hold a negative frame index.");
return FI + (1u << 30);
return FI + MCRegister::FirstStackSlot;
}

/// Return true if the specified register number is in
Expand All @@ -68,20 +70,21 @@ class Register {
/// the virtual register namespace.
static bool isVirtualRegister(unsigned Reg) {
assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
return int(Reg) < 0;
return Reg & MCRegister::VirtualRegFlag;
}

/// Convert a virtual register number to a 0-based index.
/// The first virtual register in a function will get the index 0.
static unsigned virtReg2Index(unsigned Reg) {
assert(isVirtualRegister(Reg) && "Not a virtual register");
return Reg & ~(1u << 31);
return Reg & ~MCRegister::VirtualRegFlag;
}

/// Convert a 0-based index to a virtual register number.
/// This is the inverse operation of VirtReg2IndexFunctor below.
static unsigned index2VirtReg(unsigned Index) {
return Index | (1u << 31);
assert(Index < (1u << 31) && "Index too large for virtual register range.");
return Index | MCRegister::VirtualRegFlag;
}

/// Return true if the specified register number is in the virtual register
Expand Down Expand Up @@ -112,9 +115,7 @@ class Register {
return MCRegister(Reg);
}

bool isValid() const {
return Reg != 0;
}
bool isValid() const { return Reg != MCRegister::NoRegister; }

/// Comparisons between register objects
bool operator==(const Register &Other) const { return Reg == Other.Reg; }
Expand Down
15 changes: 10 additions & 5 deletions llvm/include/llvm/MC/MCRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class MCRegister {
//
// Further sentinels can be allocated from the small negative integers.
// DenseMapInfo<unsigned> uses -1u and -2u.
static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
"Reg isn't large enough to hold full range.");
static constexpr unsigned NoRegister = 0u;
static constexpr unsigned FirstPhysicalReg = 1u;
static constexpr unsigned FirstStackSlot = 1u << 30;
static constexpr unsigned VirtualRegFlag = 1u << 31;

/// This is the portion of the positive number space that is not a physical
/// register. StackSlot values do not exist in the MC layer, see
Expand All @@ -44,14 +50,15 @@ class MCRegister {
/// slots, so if a variable may contains a stack slot, always check
/// isStackSlot() first.
static bool isStackSlot(unsigned Reg) {
return int(Reg) >= (1 << 30);
return !(Reg & VirtualRegFlag) &&
uint32_t(Reg & ~VirtualRegFlag) >= FirstStackSlot;
}

/// Return true if the specified register number is in
/// the physical register namespace.
static bool isPhysicalRegister(unsigned Reg) {
assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
return int(Reg) > 0;
return Reg >= FirstPhysicalReg && !(Reg & VirtualRegFlag);
}

/// Return true if the specified register number is in the physical register
Expand All @@ -68,9 +75,7 @@ class MCRegister {
return Reg;
}

bool isValid() const {
return Reg != 0;
}
bool isValid() const { return Reg != NoRegister; }

/// Comparisons between register objects
bool operator==(const MCRegister &Other) const { return Reg == Other.Reg; }
Expand Down

0 comments on commit 16dae81

Please sign in to comment.