Skip to content

Commit

Permalink
Make Alloc use only 30 bits.
Browse files Browse the repository at this point in the history
This is useful in the in-progress Cranelift glue in particular, where we
wrap `Operand` and `Allocation` together in one 32-bit union, using the
MSB to disambiguate. In the `Allocation` case, we still want to carry
def/use info so we can tell reads and writes apart for
machine-code-level analyses. So stealing an extra bit from `Allocation`
is necessary; this just reduces our stack-slot limit to 2^28 slots,
which should be no problem.
  • Loading branch information
cfallin committed Apr 15, 2021
1 parent 33ac6cb commit 757042c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub struct Operand {
/// Bit-pack into 31 bits. This allows a `Reg` to encode an
/// `Operand` or an `Allocation` in 32 bits.
///
/// op-or-alloc:1 pos:2 kind:1 policy:2 class:1 preg:5 vreg:20
/// free:1 pos:2 kind:1 policy:2 class:1 preg:5 vreg:20
bits: u32,
}

Expand Down Expand Up @@ -422,9 +422,9 @@ pub enum OperandPos {
/// Operand.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Allocation {
/// Bit-pack in 31 bits:
/// Bit-pack in 30 bits:
///
/// op-or-alloc:1 kind:2 index:29
/// free:2 kind:2 index:28
bits: u32,
}

Expand Down Expand Up @@ -452,8 +452,9 @@ impl std::fmt::Display for Allocation {
impl Allocation {
#[inline(always)]
pub(crate) fn new(kind: AllocationKind, index: usize) -> Self {
assert!(index < (1 << 28));
Self {
bits: ((kind as u8 as u32) << 29) | (index as u32),
bits: ((kind as u8 as u32) << 28) | (index as u32),
}
}

Expand All @@ -474,7 +475,7 @@ impl Allocation {

#[inline(always)]
pub fn kind(self) -> AllocationKind {
match (self.bits >> 29) & 3 {
match (self.bits >> 28) & 3 {
0 => AllocationKind::None,
1 => AllocationKind::Reg,
2 => AllocationKind::Stack,
Expand All @@ -484,7 +485,7 @@ impl Allocation {

#[inline(always)]
pub fn index(self) -> usize {
(self.bits & ((1 << 29) - 1)) as usize
(self.bits & ((1 << 28) - 1)) as usize
}

#[inline(always)]
Expand Down

0 comments on commit 757042c

Please sign in to comment.