Skip to content

Commit ceb2c92

Browse files
committed
TEMP: More SCCP
1 parent 324539c commit ceb2c92

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

crates/opt/src/sccp.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ir::{
77
builder::{Builder, BuilderExt},
88
node::NodeKind,
99
valgraph::{DepValue, Node},
10-
valwalk::{UnpinnedDataflowPreds, cfg_outputs, get_attached_phis},
10+
valwalk::{UnpinnedDataflowPreds, cfg_outputs, dataflow_outputs, get_attached_phis},
1111
};
1212
use log::trace;
1313
use smallvec::SmallVec;
@@ -146,6 +146,7 @@ struct SolverState {
146146
dataflow_worklist: Worklist<Node>,
147147
live_cfg_edges: DenseEntitySet<DepValue>,
148148
value_constant_states: FxHashMap<DepValue, ConstantState>,
149+
scratch_postorder: PostOrderContext<Node>,
149150
cfg_discovered_nodes: DenseEntitySet<Node>,
150151
visited_nodes: DenseEntitySet<Node>,
151152
}
@@ -156,6 +157,7 @@ fn solve_sccp(ctx: &EditContext) -> Solution {
156157
dataflow_worklist: Worklist::new(),
157158
live_cfg_edges: DenseEntitySet::new(),
158159
value_constant_states: FxHashMap::default(),
160+
scratch_postorder: PostOrderContext::new(),
159161
cfg_discovered_nodes: DenseEntitySet::new(),
160162
visited_nodes: DenseEntitySet::new(),
161163
};
@@ -165,8 +167,6 @@ fn solve_sccp(ctx: &EditContext) -> Solution {
165167

166168
state.cfg_queue.push_back(body.entry);
167169

168-
let mut scratch_postorder = PostOrderContext::new();
169-
170170
trace!("solving SCCP");
171171

172172
while !state.cfg_queue.is_empty() || !state.dataflow_worklist.is_empty() {
@@ -177,16 +177,7 @@ fn solve_sccp(ctx: &EditContext) -> Solution {
177177
while let Some(node) = state.cfg_queue.pop_front() {
178178
trace!("ctrl: {node} ({})", ctx.display_node(node));
179179

180-
// Visit any dataflow inputs to this node that we haven't seen yet, as well as the node
181-
// itself. This will only walk the nodes the first time we encounter them.
182-
scratch_postorder.reset([node]);
183-
let pred_graph = UnpinnedDataflowPreds::new(graph, ctx.live_nodes());
184-
while let Some(pred) =
185-
scratch_postorder.next(&pred_graph, &mut state.cfg_discovered_nodes)
186-
{
187-
visit_node(ctx, &mut state, pred);
188-
state.visited_nodes.insert(pred);
189-
}
180+
discover_node_and_dataflow_preds(ctx, &mut state, node);
190181

191182
let node_inputs = graph.node_inputs(node);
192183

@@ -200,12 +191,10 @@ fn solve_sccp(ctx: &EditContext) -> Solution {
200191

201192
// Discover phi inputs from newly-discovered control inputs.
202193
let phi_pred = graph.value_def(phi_input).0;
203-
if !state.visited_nodes.contains(phi_pred) {
204-
visit_node(ctx, &mut state, phi_pred);
205-
state.visited_nodes.insert(phi_pred);
206-
}
194+
discover_node_and_dataflow_preds(ctx, &mut state, phi_pred);
207195
}
208196

197+
// Visit the phi itself now that its inputs are up-to-date.
209198
visit_node(ctx, &mut state, phi);
210199
state.visited_nodes.insert(phi);
211200
}
@@ -221,6 +210,20 @@ fn solve_sccp(ctx: &EditContext) -> Solution {
221210
}
222211
}
223212

213+
fn discover_node_and_dataflow_preds(ctx: &EditContext, state: &mut SolverState, node: Node) {
214+
// Visit any dataflow inputs to this node that we haven't seen yet, as well as the node
215+
// itself. This will only walk the nodes the first time we encounter them.
216+
state.scratch_postorder.reset([node]);
217+
let pred_graph = UnpinnedDataflowPreds::new(ctx.graph(), ctx.live_nodes());
218+
while let Some(pred) = state
219+
.scratch_postorder
220+
.next(&pred_graph, &mut state.cfg_discovered_nodes)
221+
{
222+
visit_node(ctx, state, pred);
223+
state.visited_nodes.insert(pred);
224+
}
225+
}
226+
224227
macro_rules! prop_exttrunc {
225228
($ctx:expr, $state:expr, $node:expr, $func:ident) => {{
226229
let graph = $ctx.graph();
@@ -365,7 +368,12 @@ fn visit_node(ctx: &EditContext, state: &mut SolverState, node: Node) {
365368
);
366369
}
367370
}
368-
_ => {}
371+
_ => {
372+
// We don't know anything about this node's outputs...
373+
for out in dataflow_outputs(graph, node) {
374+
update_value_const_state(ctx, state, out, ConstantState::NotConstant);
375+
}
376+
}
369377
}
370378
}
371379

0 commit comments

Comments
 (0)