regalloc2/fastalloc/
iter.rs

1use crate::{Operand, OperandConstraint, OperandKind, OperandPos};
2
3pub struct Operands<'a>(pub &'a [Operand]);
4
5impl<'a> Operands<'a> {
6    pub fn new(operands: &'a [Operand]) -> Self {
7        Self(operands)
8    }
9
10    pub fn matches<F: Fn(Operand) -> bool + 'a>(
11        &self,
12        predicate: F,
13    ) -> impl Iterator<Item = (usize, Operand)> + 'a {
14        self.0
15            .iter()
16            .cloned()
17            .enumerate()
18            .filter(move |(_, op)| predicate(*op))
19    }
20
21    pub fn def_ops(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
22        self.matches(|op| op.kind() == OperandKind::Def)
23    }
24
25    pub fn use_ops(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
26        self.matches(|op| op.kind() == OperandKind::Use)
27    }
28
29    pub fn reuse(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
30        self.matches(|op| matches!(op.constraint(), OperandConstraint::Reuse(_)))
31    }
32
33    pub fn fixed(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
34        self.matches(|op| matches!(op.constraint(), OperandConstraint::FixedReg(_)))
35    }
36
37    pub fn any_reg(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
38        self.matches(|op| matches!(op.constraint(), OperandConstraint::Reg))
39    }
40
41    pub fn late(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
42        self.matches(|op| op.pos() == OperandPos::Late)
43    }
44
45    pub fn early(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
46        self.matches(|op| op.pos() == OperandPos::Early)
47    }
48}
49
50impl<'a> core::ops::Index<usize> for Operands<'a> {
51    type Output = Operand;
52
53    fn index(&self, index: usize) -> &Self::Output {
54        &self.0[index]
55    }
56}