Skip to content

Commit 644113b

Browse files
authored
Fix several bugs (#129)
* Fix `PReg::class` * Fix `Display` impl for fixed-non-allocatable `Operand`s These were previous displayed as a Use with a very large vreg number. * Extend `PRegSet` to 256 bits * Replace a stray `log::trace!` with `trace!`
1 parent a938faf commit 644113b

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

src/ion/liveranges.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ impl<'a, F: Function> Env<'a, F> {
577577
if late_def_fixed.contains(&preg)
578578
|| self.func.inst_clobbers(inst).contains(preg)
579579
{
580-
log::trace!(
580+
trace!(
581581
concat!(
582582
"-> operand {:?} is fixed to preg {:?}, ",
583583
"is downward live, and there is also a ",

src/lib.rs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl PReg {
125125
/// The register class.
126126
#[inline(always)]
127127
pub const fn class(self) -> RegClass {
128-
match self.bits & (0b11 << Self::MAX_BITS) {
128+
match (self.bits >> Self::MAX_BITS) & 0b11 {
129129
0 => RegClass::Int,
130130
1 => RegClass::Float,
131131
2 => RegClass::Vector,
@@ -188,49 +188,54 @@ impl core::fmt::Display for PReg {
188188
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
189189
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
190190
pub struct PRegSet {
191-
bits: u128,
191+
bits: [u128; 2],
192192
}
193193

194194
impl PRegSet {
195195
/// Create an empty set.
196196
pub const fn empty() -> Self {
197-
Self { bits: 0 }
197+
Self { bits: [0; 2] }
198198
}
199199

200200
/// Returns whether the given register is part of the set.
201201
pub fn contains(&self, reg: PReg) -> bool {
202-
let bit = reg.index();
203-
debug_assert!(bit < 128);
204-
self.bits & 1u128 << bit != 0
202+
debug_assert!(reg.index() < 256);
203+
let bit = reg.index() & 127;
204+
let index = reg.index() >> 7;
205+
self.bits[index] & (1u128 << bit) != 0
205206
}
206207

207208
/// Add a physical register (PReg) to the set, returning the new value.
208209
pub const fn with(self, reg: PReg) -> Self {
209-
let bit = reg.index();
210-
debug_assert!(bit < 128);
211-
Self {
212-
bits: self.bits | (1u128 << bit),
213-
}
210+
debug_assert!(reg.index() < 256);
211+
let bit = reg.index() & 127;
212+
let index = reg.index() >> 7;
213+
let mut out = self;
214+
out.bits[index] |= 1u128 << bit;
215+
out
214216
}
215217

216218
/// Add a physical register (PReg) to the set.
217219
pub fn add(&mut self, reg: PReg) {
218-
let bit = reg.index();
219-
debug_assert!(bit < 128);
220-
self.bits |= 1u128 << bit;
220+
debug_assert!(reg.index() < 256);
221+
let bit = reg.index() & 127;
222+
let index = reg.index() >> 7;
223+
self.bits[index] |= 1u128 << bit;
221224
}
222225

223226
/// Remove a physical register (PReg) from the set.
224227
pub fn remove(&mut self, reg: PReg) {
225-
let bit = reg.index();
226-
debug_assert!(bit < 128);
227-
self.bits &= !(1u128 << bit);
228+
debug_assert!(reg.index() < 256);
229+
let bit = reg.index() & 127;
230+
let index = reg.index() >> 7;
231+
self.bits[index] &= !(1u128 << bit);
228232
}
229233

230234
/// Add all of the registers in one set to this one, mutating in
231235
/// place.
232236
pub fn union_from(&mut self, other: PRegSet) {
233-
self.bits |= other.bits;
237+
self.bits[0] |= other.bits[0];
238+
self.bits[1] |= other.bits[1];
234239
}
235240
}
236241

@@ -243,18 +248,22 @@ impl IntoIterator for PRegSet {
243248
}
244249

245250
pub struct PRegSetIter {
246-
bits: u128,
251+
bits: [u128; 2],
247252
}
248253

249254
impl Iterator for PRegSetIter {
250255
type Item = PReg;
251256
fn next(&mut self) -> Option<PReg> {
252-
if self.bits == 0 {
253-
None
254-
} else {
255-
let index = self.bits.trailing_zeros();
256-
self.bits &= !(1u128 << index);
257+
if self.bits[0] != 0 {
258+
let index = self.bits[0].trailing_zeros();
259+
self.bits[0] &= !(1u128 << index);
257260
Some(PReg::from_index(index as usize))
261+
} else if self.bits[1] != 0 {
262+
let index = self.bits[1].trailing_zeros();
263+
self.bits[1] &= !(1u128 << index);
264+
Some(PReg::from_index(index as usize + 128))
265+
} else {
266+
None
258267
}
259268
}
260269
}
@@ -822,6 +831,9 @@ impl core::fmt::Debug for Operand {
822831

823832
impl core::fmt::Display for Operand {
824833
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
834+
if let Some(preg) = self.as_fixed_nonallocatable() {
835+
return write!(f, "Fixed: {preg}");
836+
}
825837
match (self.kind(), self.pos()) {
826838
(OperandKind::Def, OperandPos::Late) | (OperandKind::Use, OperandPos::Early) => {
827839
write!(f, "{:?}", self.kind())?;

0 commit comments

Comments
 (0)