Skip to content

Commit

Permalink
Remove support for backwards InstRange (#169)
Browse files Browse the repository at this point in the history
These are not needed except for one place in live range calculation
where we can use a reversed iterator instead.
  • Loading branch information
Amanieu committed Dec 5, 2023
1 parent 8eae901 commit 98fbea5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/fuzzing/func.rs
Expand Up @@ -172,7 +172,7 @@ impl FuncBuilder {
let b = Block::new(self.f.blocks.len());
self.f
.blocks
.push(InstRange::forward(Inst::new(0), Inst::new(0)));
.push(InstRange::new(Inst::new(0), Inst::new(0)));
self.f.block_preds.push(vec![]);
self.f.block_succs.push(vec![]);
self.f.block_params_in.push(vec![]);
Expand Down Expand Up @@ -217,7 +217,7 @@ impl FuncBuilder {
self.f.insts.push(inst.clone());
}
let end_inst = self.f.insts.len();
*blockrange = InstRange::forward(Inst::new(begin_inst), Inst::new(end_inst));
*blockrange = InstRange::new(Inst::new(begin_inst), Inst::new(end_inst));
}

self.f
Expand Down
79 changes: 10 additions & 69 deletions src/index.rs
Expand Up @@ -141,49 +141,31 @@ define_index!(Block);
feature = "enable-serde",
derive(::serde::Serialize, ::serde::Deserialize)
)]
pub struct InstRange(Inst, Inst, bool);
pub struct InstRange(Inst, Inst);

impl InstRange {
#[inline(always)]
pub fn forward(from: Inst, to: Inst) -> Self {
pub fn new(from: Inst, to: Inst) -> Self {
debug_assert!(from.index() <= to.index());
InstRange(from, to, true)
}

#[inline(always)]
pub fn backward(from: Inst, to: Inst) -> Self {
debug_assert!(from.index() >= to.index());
InstRange(to, from, false)
InstRange(from, to)
}

#[inline(always)]
pub fn first(self) -> Inst {
debug_assert!(self.len() > 0);
if self.is_forward() {
self.0
} else {
self.1.prev()
}
self.0
}

#[inline(always)]
pub fn last(self) -> Inst {
debug_assert!(self.len() > 0);
if self.is_forward() {
self.1.prev()
} else {
self.0
}
self.1.prev()
}

#[inline(always)]
pub fn rest(self) -> InstRange {
debug_assert!(self.len() > 0);
if self.is_forward() {
InstRange::forward(self.0.next(), self.1)
} else {
InstRange::backward(self.1.prev(), self.0)
}
InstRange::new(self.0.next(), self.1)
}

#[inline(always)]
Expand All @@ -192,35 +174,8 @@ impl InstRange {
}

#[inline(always)]
pub fn is_forward(self) -> bool {
self.2
}

#[inline(always)]
pub fn rev(self) -> Self {
Self(self.0, self.1, !self.2)
}

#[inline(always)]
pub fn iter(self) -> InstRangeIter {
InstRangeIter(self)
}
}

#[derive(Clone, Copy, Debug)]
pub struct InstRangeIter(InstRange);

impl Iterator for InstRangeIter {
type Item = Inst;
#[inline(always)]
fn next(&mut self) -> Option<Inst> {
if self.0.len() == 0 {
None
} else {
let ret = self.0.first();
self.0 = self.0.rest();
Some(ret)
}
pub fn iter(self) -> impl DoubleEndedIterator<Item = Inst> {
(self.0.index()..self.1.index()).map(|i| Inst::new(i))
}
}

Expand All @@ -233,10 +188,10 @@ mod test {

#[test]
fn test_inst_range() {
let range = InstRange::forward(Inst::new(0), Inst::new(0));
let range = InstRange::new(Inst::new(0), Inst::new(0));
debug_assert_eq!(range.len(), 0);

let range = InstRange::forward(Inst::new(0), Inst::new(5));
let range = InstRange::new(Inst::new(0), Inst::new(5));
debug_assert_eq!(range.first().index(), 0);
debug_assert_eq!(range.last().index(), 4);
debug_assert_eq!(range.len(), 5);
Expand All @@ -250,19 +205,5 @@ mod test {
Inst::new(4)
]
);
let range = range.rev();
debug_assert_eq!(range.first().index(), 4);
debug_assert_eq!(range.last().index(), 0);
debug_assert_eq!(range.len(), 5);
debug_assert_eq!(
range.iter().collect::<Vec<_>>(),
vec![
Inst::new(4),
Inst::new(3),
Inst::new(2),
Inst::new(1),
Inst::new(0)
]
);
}
}
4 changes: 2 additions & 2 deletions src/ion/liveranges.rs
Expand Up @@ -315,7 +315,7 @@ impl<'a, F: Function> Env<'a, F> {
}
}

for inst in insns.rev().iter() {
for inst in insns.iter().rev() {
for pos in &[OperandPos::Late, OperandPos::Early] {
for op in self.func.inst_operands(inst) {
if op.as_fixed_nonallocatable().is_some() {
Expand Down Expand Up @@ -442,7 +442,7 @@ impl<'a, F: Function> Env<'a, F> {

// For each instruction, in reverse order, process
// operands and clobbers.
for inst in insns.rev().iter() {
for inst in insns.iter().rev() {
// Mark clobbers with CodeRanges on PRegs.
for clobber in self.func.inst_clobbers(inst) {
// Clobber range is at After point only: an
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -51,7 +51,7 @@ pub mod ssa;
mod index;

use alloc::vec::Vec;
pub use index::{Block, Inst, InstRange, InstRangeIter};
pub use index::{Block, Inst, InstRange};

pub mod checker;

Expand Down

0 comments on commit 98fbea5

Please sign in to comment.