cranelift_codegen/machinst/
valueregs.rs

1//! Data structure for tracking the (possibly multiple) registers that hold one
2//! SSA `Value`.
3
4use regalloc2::{PReg, VReg};
5
6use super::{RealReg, Reg, VirtualReg, Writable};
7use std::fmt::Debug;
8
9const VALUE_REGS_PARTS: usize = 2;
10
11/// Location at which a `Value` is stored in register(s): the value is located
12/// in one or more registers, depending on its width. A value may be stored in
13/// more than one register if the machine has no registers wide enough
14/// otherwise: for example, on a 32-bit architecture, we may store `I64` values
15/// in two registers, and `I128` values in four.
16///
17/// By convention, the register parts are kept in machine-endian order here.
18///
19/// N.B.: we cap the capacity of this at four (when any 32-bit target is
20/// enabled) or two (otherwise), and we use special in-band sentinal `Reg`
21/// values (`Reg::invalid()`) to avoid the need to carry a separate length. This
22/// allows the struct to be `Copy` (no heap or drop overhead) and be only 16 or
23/// 8 bytes, which is important for compiler performance.
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25pub struct ValueRegs<R: Clone + Copy + Debug + PartialEq + Eq + InvalidSentinel> {
26    parts: [R; VALUE_REGS_PARTS],
27}
28
29/// A type with an "invalid" sentinel value.
30pub trait InvalidSentinel: Copy + Eq {
31    /// The invalid sentinel value.
32    fn invalid_sentinel() -> Self;
33    /// Is this the invalid sentinel?
34    fn is_invalid_sentinel(self) -> bool {
35        self == Self::invalid_sentinel()
36    }
37}
38impl InvalidSentinel for Reg {
39    fn invalid_sentinel() -> Self {
40        Reg::from(VReg::invalid())
41    }
42}
43impl InvalidSentinel for VirtualReg {
44    fn invalid_sentinel() -> Self {
45        VirtualReg::from(VReg::invalid())
46    }
47}
48impl InvalidSentinel for RealReg {
49    fn invalid_sentinel() -> Self {
50        RealReg::from(PReg::invalid())
51    }
52}
53impl InvalidSentinel for Writable<Reg> {
54    fn invalid_sentinel() -> Self {
55        Writable::from_reg(Reg::invalid_sentinel())
56    }
57}
58
59impl<R: Clone + Copy + Debug + PartialEq + Eq + InvalidSentinel> ValueRegs<R> {
60    /// Create an invalid Value-in-Reg.
61    pub fn invalid() -> Self {
62        ValueRegs {
63            parts: [R::invalid_sentinel(); VALUE_REGS_PARTS],
64        }
65    }
66
67    /// Is this Value-to-Reg mapping valid?
68    pub fn is_valid(self) -> bool {
69        !self.parts[0].is_invalid_sentinel()
70    }
71    /// Is this Value-to-Reg mapping invalid?
72    pub fn is_invalid(self) -> bool {
73        self.parts[0].is_invalid_sentinel()
74    }
75
76    /// Return the single register used for this value, if any.
77    pub fn only_reg(self) -> Option<R> {
78        if self.len() == 1 {
79            Some(self.parts[0])
80        } else {
81            None
82        }
83    }
84
85    /// Return an iterator over the registers storing this value.
86    pub fn regs(&self) -> &[R] {
87        &self.parts[0..self.len()]
88    }
89}
90
91impl<R: Clone + Copy + Debug + PartialEq + Eq + InvalidSentinel> ValueRegs<R> {
92    /// Create a Value-in-R location for a value stored in one register.
93    pub fn one(reg: R) -> Self {
94        ValueRegs {
95            parts: [reg, R::invalid_sentinel()],
96        }
97    }
98    /// Create a Value-in-R location for a value stored in two registers.
99    pub fn two(r1: R, r2: R) -> Self {
100        ValueRegs { parts: [r1, r2] }
101    }
102
103    /// Return the number of registers used.
104    pub fn len(self) -> usize {
105        // If rustc/LLVM is smart enough, this might even be vectorized...
106        (self.parts[0] != R::invalid_sentinel()) as usize
107            + (self.parts[1] != R::invalid_sentinel()) as usize
108    }
109
110    /// Map individual registers via a map function.
111    pub fn map<NewR, F>(self, f: F) -> ValueRegs<NewR>
112    where
113        NewR: Clone + Copy + Debug + PartialEq + Eq + InvalidSentinel,
114        F: Fn(R) -> NewR,
115    {
116        ValueRegs {
117            parts: [f(self.parts[0]), f(self.parts[1])],
118        }
119    }
120}
121
122/// Create a writable ValueRegs.
123#[allow(dead_code)]
124pub(crate) fn writable_value_regs(regs: ValueRegs<Reg>) -> ValueRegs<Writable<Reg>> {
125    regs.map(|r| Writable::from_reg(r))
126}
127
128/// Strip a writable ValueRegs down to a readonly ValueRegs.
129#[allow(dead_code)]
130pub(crate) fn non_writable_value_regs(regs: ValueRegs<Writable<Reg>>) -> ValueRegs<Reg> {
131    regs.map(|r| r.to_reg())
132}