Skip to content

Commit

Permalink
chapter17: idealize region
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertObkircher committed Feb 7, 2025
1 parent 887b308 commit 866a922
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/sea_of_nodes/nodes/idealize.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::datastructures::id::Id;
use crate::sea_of_nodes::nodes::node::IfOp;
use crate::sea_of_nodes::nodes::node::{
Add, Bool, CProj, Cast, Constant, Div, If, Load, Minus, Mul, Not, Phi, Return, Stop, Store,
Sub, TypedNode,
};
use crate::sea_of_nodes::nodes::node::{IfOp, Region};
use crate::sea_of_nodes::nodes::{BoolOp, Node, Nodes, Op};
use crate::sea_of_nodes::types::Ty;

Expand All @@ -20,7 +20,8 @@ impl Node {
TypedNode::Return(n) => n.idealize_return(sea),
TypedNode::Proj(_) => None,
TypedNode::CProj(n) => n.idealize_cproj(sea),
TypedNode::Region(_) | TypedNode::Loop(_) => self.idealize_region(sea),
TypedNode::Region(n) => n.idealize_region(sea),
TypedNode::Loop(n) => n.idealize_region(sea), // super
TypedNode::If(n) => n.idealize_if(sea),
TypedNode::Cast(n) => n.idealize_cast(sea),
TypedNode::Load(n) => n.idealize_load(sea),
Expand Down Expand Up @@ -376,9 +377,9 @@ impl CProj {
}
}

impl Node {
impl Region {
fn idealize_region(self, sea: &mut Nodes) -> Option<Node> {
if Nodes::in_progress(&sea.ops, &sea.inputs, self) {
if Nodes::in_progress(&sea.ops, &sea.inputs, **self) {
return None;
}

Expand Down Expand Up @@ -416,7 +417,7 @@ impl Node {
Some(**sea.xctrl)
} else {
self.del_def(path, sea);
Some(self)
Some(**self)
};
}
}
Expand All @@ -429,10 +430,12 @@ impl Node {
// If a CFG diamond with no merging, delete: "if( pred ) {} else {};"
if !self.has_phi(sea) {
// No Phi users, just a control user
if let Some(p1) = self.inputs(sea)[1].and_then(|n| n.to_proj(sea)) {
if let Some(p2) = self.inputs(sea)[2].and_then(|n| n.to_proj(sea)) {
if p1.inputs(sea)[0] == p2.inputs(sea)[0] {
if let Some(iff) = p1.inputs(sea)[0].and_then(|n| n.to_if(sea)) {
if let Some(p1) = self.inputs(sea)[1].and_then(|n| n.to_cproj(sea)) {
if let Some(p2) = self.inputs(sea)[2].and_then(|n| n.to_cproj(sea)) {
if p1.inputs(sea)[0].unwrap().add_dep(self, sea)
== p2.inputs(sea)[0].unwrap().add_dep(self, sea)
{
if let Some(iff) = p1.inputs(sea)[0].unwrap().to_if(sea) {
return iff.inputs(sea)[0];
}
}
Expand Down

0 comments on commit 866a922

Please sign in to comment.