Skip to content

Commit

Permalink
shorten names of types
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertObkircher committed Jan 18, 2025
1 parent 2cdf1d1 commit 01f2d3e
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 192 deletions.
8 changes: 3 additions & 5 deletions src/sea_of_nodes/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl Node {
pub fn err(self, sea: &Nodes) -> Option<String> {
if let Some(memop) = self.to_mem_name(sea) {
let ptr = self.inputs(sea)[2]?.ty(sea)?;
if ptr == sea.types.ty_bot || matches!(*ptr, Type::Pointer(MemPtr { nil: true, .. })) {
if ptr == sea.types.bot || matches!(*ptr, Type::Pointer(MemPtr { nil: true, .. })) {
return Some(format!("Might be null accessing '{memop}'"));
}
}
Expand Down Expand Up @@ -360,8 +360,7 @@ impl Node {
impl Phi {
fn single_unique_input(self, sea: &mut Nodes) -> Option<Node> {
let region = self.inputs(sea)[0].unwrap();
if region.is_loop(sea) && region.inputs(sea)[1].unwrap().ty(sea) == Some(sea.types.ty_xctrl)
{
if region.is_loop(sea) && region.inputs(sea)[1].unwrap().ty(sea) == Some(sea.types.xctrl) {
return None; // Dead entry loops just ignore and let the loop collapse
}

Expand All @@ -370,8 +369,7 @@ impl Phi {
// If the region's control input is live, add this as a dependency
// to the control because we can be peeped should it become dead.
let region_in_i = region.inputs(sea)[i].unwrap().add_dep(*self, sea);
if region_in_i.ty(sea) != Some(sea.types.ty_xctrl) && self.inputs(sea)[i] != Some(*self)
{
if region_in_i.ty(sea) != Some(sea.types.xctrl) && self.inputs(sea)[i] != Some(*self) {
if live.is_none() || live == self.inputs(sea)[i] {
live = self.inputs(sea)[i];
} else {
Expand Down
31 changes: 15 additions & 16 deletions src/sea_of_nodes/nodes/idealize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ impl Add {

// Add of 0. We do not check for (0+x) because this will already
// canonicalize to (x+0)
if t2 == sea.types.ty_int_zero {
if t2 == sea.types.int_zero {
return Some(lhs);
}

// Add of same to a multiply by 2
if lhs == rhs {
let two = Constant::new(sea.types.ty_int_two, sea).peephole(sea);
let two = Constant::new(sea.types.int_two, sea).peephole(sea);
return Some(*Mul::new(lhs, two, sea));
}

Expand Down Expand Up @@ -195,9 +195,9 @@ impl Bool {
let op = sea[self];
if self.inputs(sea)[1]? == self.inputs(sea)[2]? {
let value = if op.compute(3, 3) {
sea.types.ty_int_one
sea.types.int_one
} else {
sea.types.ty_int_zero
sea.types.int_zero
};
return Some(*Constant::new(value, sea));
}
Expand All @@ -217,8 +217,8 @@ impl Bool {
}
}
// Equals X==0 becomes a !X
if rhs.ty(sea) == Some(sea.types.ty_int_zero)
|| rhs.ty(sea) == Some(sea.types.ty_pointer_null)
if rhs.ty(sea) == Some(sea.types.int_zero)
|| rhs.ty(sea) == Some(sea.types.pointer_null)
{
return Some(*Not::new(lhs, sea));
}
Expand Down Expand Up @@ -338,7 +338,7 @@ impl Stop {
let mut result = None;
let mut i = 0;
while i < self.inputs(sea).len() {
if self.inputs(sea)[i].unwrap().ty(sea) == Some(sea.types.ty_xctrl) {
if self.inputs(sea)[i].unwrap().ty(sea) == Some(sea.types.xctrl) {
self.del_def(i, sea);
result = Some(*self);
} else {
Expand All @@ -351,7 +351,7 @@ impl Stop {

impl Return {
fn idealize_return(self, sea: &mut Nodes) -> Option<Node> {
self.inputs(sea)[0].filter(|ctrl| ctrl.ty(sea) == Some(sea.types.ty_xctrl))
self.inputs(sea)[0].filter(|ctrl| ctrl.ty(sea) == Some(sea.types.xctrl))
}
}

Expand All @@ -360,7 +360,7 @@ impl CProj {
let index = sea[self].index;
if let Some(iff) = self.inputs(sea)[0]?.to_if(sea) {
if let Some(Type::Tuple { types: ts }) = iff.ty(sea).as_deref() {
if ts[1 - index] == sea.types.ty_xctrl {
if ts[1 - index] == sea.types.xctrl {
return iff.inputs(sea)[0]; // We become our input control
}
}
Expand Down Expand Up @@ -476,9 +476,9 @@ impl If {
if d.pred(sea).unwrap().add_dep(*self, sea) == pred {
if let Op::CProj(p) = &sea[prior] {
let value = if p.index == 0 {
sea.types.ty_int_one
sea.types.int_one
} else {
sea.types.ty_int_zero
sea.types.int_zero
};
let new_constant = Constant::new(value, sea).peephole(sea);
self.set_def(1, Some(new_constant), sea);
Expand Down Expand Up @@ -521,8 +521,7 @@ impl Load {
// else ptr.x = e1; : e1;
// val = ptr.x; ptr.x = val;
if let Some(mem) = mem.to_phi(sea) {
if mem.inputs(sea)[0]?.ty(sea) == Some(sea.types.ty_ctrl) && mem.inputs(sea).len() == 3
{
if mem.inputs(sea)[0]?.ty(sea) == Some(sea.types.ctrl) && mem.inputs(sea).len() == 3 {
// Profit on RHS/Loop backedge
if self.profit(mem, 2, sea) ||
// Else must not be a loop to count profit on LHS.
Expand Down Expand Up @@ -643,7 +642,7 @@ impl Div {
impl Node {
fn find_dead_input(self, sea: &Nodes) -> Option<usize> {
(1..self.inputs(sea).len())
.find(|&i| self.inputs(sea)[i].unwrap().ty(sea).unwrap() == sea.types.ty_xctrl)
.find(|&i| self.inputs(sea)[i].unwrap().ty(sea).unwrap() == sea.types.xctrl)
}
}

Expand All @@ -662,12 +661,12 @@ impl<'t> Nodes<'t> {
}

if matches!(self[lo], Op::Phi(_))
&& self.ty[self.inputs[lo][0].unwrap()] == Some(self.types.ty_xctrl)
&& self.ty[self.inputs[lo][0].unwrap()] == Some(self.types.xctrl)
{
return false;
}
if matches!(self[hi], Op::Phi(_))
&& self.ty[self.inputs[hi][0].unwrap()] == Some(self.types.ty_xctrl)
&& self.ty[self.inputs[hi][0].unwrap()] == Some(self.types.xctrl)
{
return false;
}
Expand Down
85 changes: 41 additions & 44 deletions src/sea_of_nodes/nodes/peephole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'t> Node {

// Replace constant computations from non-constants with a constant node
if !self.is_constant(sea) && !self.is_xctrl(sea) && ty.is_high_or_constant() {
return if ty == sea.types.ty_xctrl {
return if ty == sea.types.xctrl {
*XCtrl::new(sea)
} else {
*Constant::new(ty, sea)
Expand Down Expand Up @@ -112,22 +112,22 @@ impl<'t> Node {
let types = sea.types;
match &sea[self] {
Op::Constant(ty) => *ty,
Op::XCtrl => types.ty_xctrl,
Op::XCtrl => types.xctrl,
Op::Return => {
let ctrl = self.inputs(sea)[0]
.and_then(|n| n.ty(sea))
.unwrap_or(types.ty_bot);
.unwrap_or(types.bot);
let expr = self.inputs(sea)[1]
.and_then(|n| n.ty(sea))
.unwrap_or(types.ty_bot);
.unwrap_or(types.bot);
types.get_tuple_from_array([ctrl, expr])
}
Op::Start(s) => s.args,
Op::Add => self.compute_binary_int(i64::wrapping_add, sea),
Op::Sub => {
// Sub of same is 0
if self.inputs(sea)[1] == self.inputs(sea)[2] {
sea.types.ty_int_zero
sea.types.int_zero
} else {
self.compute_binary_int(i64::wrapping_sub, sea)
}
Expand All @@ -138,82 +138,82 @@ impl<'t> Node {
}
Op::Minus => {
let Some(input) = self.inputs(sea)[1].and_then(|n| n.ty(sea)) else {
return types.ty_bot;
return types.bot;
};
match &*input {
Type::Int(Int::Constant(v)) => types.get_int(v.wrapping_neg()),
_ => types.meet(types.ty_top, input),
_ => types.meet(types.top, input),
}
}
Op::Scope(_) => types.ty_bot,
Op::Scope(_) => types.bot,
Op::Bool(op) => self.compute_binary_int(|x, y| op.compute(x, y) as i64, sea),
Op::Not => {
let t0 = self.inputs(sea)[1].unwrap().ty(sea).unwrap();
match &*t0 {
Type::Int(i) => match i {
Int::Constant(0) => types.ty_int_one,
Int::Constant(_) => types.ty_int_zero,
Int::Constant(0) => types.int_one,
Int::Constant(_) => types.int_zero,
_ => t0,
},
Type::Pointer(p) => {
// top->top, bot->bot, null->1, *void->0, not-null ptr->0, ptr/nil->bot
// If input in null then true
// If input is not null ptr then false
if t0 == types.ty_pointer_top {
types.ty_int_top
} else if t0 == types.ty_pointer_null {
types.ty_int_one
if t0 == types.pointer_top {
types.int_top
} else if t0 == types.pointer_null {
types.int_one
} else if !p.nil {
types.ty_int_zero
types.int_zero
} else {
types.ty_int_bot
types.int_bot
}
}
_ => {
// Only doing NOT on ints and ptrs
assert!(t0 == types.ty_top || t0 == types.ty_bot);
if t0 == types.ty_top {
assert!(t0 == types.top || t0 == types.bot);
if t0 == types.top {
t0
} else {
types.ty_bot
types.bot
}
}
}
}
Op::Proj(n) | Op::CProj(n) => {
let Some(input) = self.inputs(sea)[0].and_then(|n| n.ty(sea)) else {
return types.ty_bot;
return types.bot;
};
match &*input {
Type::Tuple { types } => types[n.index],
_ => unreachable!("proj node ctrl must always be tuple, if present"),
}
}
Op::If(IfOp::Never) => types.ty_int_bot,
Op::If(IfOp::Never) => types.int_bot,
Op::If(IfOp::Cond) => {
let s = self.to_if(sea).unwrap();

// If the If node is not reachable then neither is any following Proj
let ctrl_ty = self.inputs(sea)[0].unwrap().ty(sea);
if ctrl_ty != Some(types.ty_ctrl) && ctrl_ty != Some(types.ty_bot) {
return types.ty_if_neither;
if ctrl_ty != Some(types.ctrl) && ctrl_ty != Some(types.bot) {
return types.if_neither;
}

let t = s.pred(sea).unwrap().ty(sea).unwrap();

// High types mean NEITHER side is reachable.
// Wait until the type falls to decide which way to go.
if t == types.ty_top || t == types.ty_int_top {
return types.ty_if_neither;
if t == types.top || t == types.int_top {
return types.if_neither;
}

// If constant is 0 then false branch is reachable
// Else true branch is reachable
if let Type::Int(Int::Constant(c)) = &*t {
return if *c == 0 {
types.ty_if_false
types.if_false
} else {
types.ty_if_true
types.if_true
};
}

Expand All @@ -228,9 +228,9 @@ impl<'t> Node {
return if let Op::Proj(proj) = &sea[prior] {
// Repeated test, dominated on one side. Test result is the same.
if proj.index == 0 {
types.ty_if_true
types.if_true
} else {
types.ty_if_false
types.if_false
}
} else {
// Repeated test not dominated on one side
Expand All @@ -243,13 +243,13 @@ impl<'t> Node {
dom = d.idom(sea);
}

types.ty_if_both
types.if_both
}
Op::Phi(_) => {
let region = self.inputs(sea)[0].unwrap();
if !sea.instanceof_region(Some(region)) {
if region.ty(sea) == Some(types.ty_xctrl) {
types.ty_top
if region.ty(sea) == Some(types.xctrl) {
types.top
} else {
self.ty(sea).unwrap()
}
Expand All @@ -266,7 +266,7 @@ impl<'t> Node {
// If the region's control input is live, add this as a dependency
// to the control because we can be peeped should it become dead.
if region.inputs(sea)[i].unwrap().add_dep(self, sea).ty(sea)
!= Some(types.ty_xctrl)
!= Some(types.xctrl)
{
t = types.meet(t, self.inputs(sea)[i].unwrap().ty(sea).unwrap())
}
Expand All @@ -276,24 +276,21 @@ impl<'t> Node {
}
Op::Region { .. } => {
if Nodes::in_progress(&sea.ops, &sea.inputs, self) {
types.ty_ctrl
types.ctrl
} else {
self.inputs(sea)
.iter()
.skip(1)
.fold(types.ty_xctrl, |t, n| {
types.meet(t, n.unwrap().ty(sea).unwrap())
})
self.inputs(sea).iter().skip(1).fold(types.xctrl, |t, n| {
types.meet(t, n.unwrap().ty(sea).unwrap())
})
}
}
Op::Loop => {
if Nodes::in_progress(&sea.ops, &sea.inputs, self) {
types.ty_ctrl
types.ctrl
} else {
self.inputs(sea)[1].unwrap().ty(sea).unwrap()
}
}
Op::Stop => types.ty_bot,
Op::Stop => types.bot,
Op::Cast(t) => types.join(self.inputs(sea)[1].unwrap().ty(sea).unwrap(), *t),
Op::Load(l) => l.declared_type,
Op::Store(s) => types.get_mem(s.alias),
Expand All @@ -304,10 +301,10 @@ impl<'t> Node {
fn compute_binary_int<F: FnOnce(i64, i64) -> i64>(self, op: F, sea: &Nodes<'t>) -> Ty<'t> {
let types = sea.types;
let Some(first) = self.inputs(sea)[1].and_then(|n| n.ty(sea)) else {
return types.ty_bot;
return types.bot;
};
let Some(second) = self.inputs(sea)[2].and_then(|n| n.ty(sea)) else {
return types.ty_bot;
return types.bot;
};

match [&*first, &*second] {
Expand Down
6 changes: 3 additions & 3 deletions src/sea_of_nodes/nodes/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl<'t> Scope {
invert: bool,
sea: &mut Nodes<'t>,
) -> Option<Node> {
if ctrl.ty(sea)? == sea.types.ty_xctrl {
if ctrl.ty(sea)? == sea.types.xctrl {
return None;
}
// Invert the If conditional
Expand All @@ -263,11 +263,11 @@ impl<'t> Scope {
// being tested. No representation for a generic not-null int, so no upcast.
return None;
};
if sea.types.isa(tmp, sea.types.ty_pointer_void) {
if sea.types.isa(tmp, sea.types.pointer_void) {
return None; // Already not-null, no reason to upcast
}
// Upcast the ptr to not-null ptr, and replace in scope
let c = Cast::new(sea.types.ty_pointer_void, ctrl, pred, sea).peephole(sea);
let c = Cast::new(sea.types.pointer_void, ctrl, pred, sea).peephole(sea);
let t = c.compute(sea);
c.set_type(t, sea);
self.replace(pred, Some(c), sea);
Expand Down
Loading

0 comments on commit 01f2d3e

Please sign in to comment.