Skip to content

Commit a5c105b

Browse files
committed
Rename: ControlFlowGraph -> IRModule, BasicBlockGraph -> ControlFlowGraph
1 parent 8f93a8f commit a5c105b

File tree

9 files changed

+160
-160
lines changed

9 files changed

+160
-160
lines changed

src/frontend/dead_code.rs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use petgraph::graph::NodeIndex;
1010
use petgraph::visit::EdgeRef;
1111

1212
use crate::ir::BasicBlock;
13-
use crate::ir::BasicBlockGraph;
13+
use crate::ir::ControlFlowGraph;
1414
use crate::ir::FunctionMap;
1515
use crate::ir::get_statement_operands_mut;
1616
use crate::ir::get_statement_value_operands;
@@ -25,22 +25,22 @@ pub(crate) fn remove_dead_code(
2525
entry_block: NodeIndex,
2626
program_result: &mut ValueId,
2727
values: &mut ValueStorage,
28-
graph: &mut BasicBlockGraph,
28+
cfg: &mut ControlFlowGraph,
2929
definitions: &mut HashMap<ValueId, StatementLocation>,
3030
functions: &mut FunctionMap)
3131
{
32-
let mut value_usage = compute_value_usage(*program_result, values, graph, definitions);
33-
remove_unused_definitions(&mut value_usage, values, graph, definitions);
34-
remove_empty_blocks(graph);
35-
merge_consecutive_basic_blocks(entry_block, values, graph, &mut value_usage, definitions);
36-
remove_unused_definitions(&mut value_usage, values, graph, definitions);
37-
remove_unused_values(definitions, values, functions, graph, program_result);
32+
let mut value_usage = compute_value_usage(*program_result, values, cfg, definitions);
33+
remove_unused_definitions(&mut value_usage, values, cfg, definitions);
34+
remove_empty_blocks(cfg);
35+
merge_consecutive_basic_blocks(entry_block, values, cfg, &mut value_usage, definitions);
36+
remove_unused_definitions(&mut value_usage, values, cfg, definitions);
37+
remove_unused_values(definitions, values, functions, cfg, program_result);
3838
}
3939

4040
fn compute_value_usage(
4141
program_result: ValueId,
4242
values: &mut ValueStorage,
43-
graph: &mut BasicBlockGraph,
43+
cfg: &mut ControlFlowGraph,
4444
definitions: &mut HashMap<ValueId, StatementLocation>)
4545
-> HashMap<ValueId, usize>
4646
{
@@ -49,9 +49,9 @@ fn compute_value_usage(
4949
.keys()
5050
.cloned()
5151
.zip(repeat(0)));
52-
let used_values = graph
52+
let used_values = cfg
5353
.node_indices()
54-
.flat_map(|node| graph[node].iter())
54+
.flat_map(|node| cfg[node].iter())
5555
.flat_map(|statement| get_statement_value_operands(&values, statement))
5656
.chain(once(program_result));
5757
for value_id in used_values {
@@ -63,7 +63,7 @@ fn compute_value_usage(
6363
fn remove_unused_definitions(
6464
value_usage: &mut HashMap<ValueId, usize>,
6565
values: &mut ValueStorage,
66-
graph: &mut BasicBlockGraph,
66+
cfg: &mut ControlFlowGraph,
6767
definitions: &mut HashMap<ValueId, StatementLocation>)
6868
{
6969
let unused_values = value_usage
@@ -74,7 +74,7 @@ fn remove_unused_definitions(
7474
})
7575
.collect_vec();
7676
for value_id in unused_values {
77-
unuse(value_id, graph, values, value_usage, definitions);
77+
unuse(value_id, cfg, values, value_usage, definitions);
7878
}
7979

8080
let (unused_values, dead_code): (Vec<_>, Vec<_>) = value_usage
@@ -88,12 +88,12 @@ fn remove_unused_definitions(
8888
definitions.remove(&value_id);
8989
value_usage.remove(&value_id);
9090
}
91-
remove_statements(dead_code, graph);
91+
remove_statements(dead_code, cfg);
9292
}
9393

9494
fn unuse(
9595
value_id: ValueId,
96-
graph: &BasicBlockGraph,
96+
cfg: &ControlFlowGraph,
9797
values: &ValueStorage,
9898
value_usage: &mut HashMap<ValueId, usize>,
9999
definitions: &mut HashMap<ValueId, StatementLocation>)
@@ -105,38 +105,38 @@ fn unuse(
105105

106106
if *usage_count == 0 {
107107
let location = &definitions[&value_id];
108-
let statement = &graph[location.block][location.index];
108+
let statement = &cfg[location.block][location.index];
109109
for operand in get_statement_value_operands(&values, statement) {
110-
unuse(operand, graph, values, value_usage, definitions);
110+
unuse(operand, cfg, values, value_usage, definitions);
111111
}
112112
}
113113
}
114114

115-
fn remove_empty_blocks(graph: &mut BasicBlockGraph) {
116-
let blocks = graph
115+
fn remove_empty_blocks(cfg: &mut ControlFlowGraph) {
116+
let blocks = cfg
117117
.node_indices()
118118
.collect_vec();
119119
for block in blocks {
120-
let is_empty = graph[block]
120+
let is_empty = cfg[block]
121121
.iter()
122122
.find(|statement| !matches!(statement, Statement::Comment(_)))
123123
.is_none();
124124
if is_empty {
125-
let sources = graph
125+
let sources = cfg
126126
.edges_directed(block, Direction::Incoming)
127127
.map(|edge| edge.source())
128128
.collect_vec();
129-
let targets = graph
129+
let targets = cfg
130130
.edges_directed(block, Direction::Outgoing)
131131
.map(|edge| edge.target())
132132
.collect_vec();
133133
if sources.len() == 1 && targets.len() == 1 {
134134
let source = sources[0];
135135
let target = targets[0];
136-
if graph.find_edge(source, target).is_none() {
137-
graph.add_edge(source, target, ());
136+
if cfg.find_edge(source, target).is_none() {
137+
cfg.add_edge(source, target, ());
138138
}
139-
graph.remove_node(block);
139+
cfg.remove_node(block);
140140
}
141141
}
142142
}
@@ -145,27 +145,27 @@ fn remove_empty_blocks(graph: &mut BasicBlockGraph) {
145145
fn merge_consecutive_basic_blocks(
146146
block: NodeIndex,
147147
values: &ValueStorage,
148-
graph: &mut BasicBlockGraph,
148+
cfg: &mut ControlFlowGraph,
149149
value_usage: &mut HashMap<ValueId, usize>,
150150
definitions: &mut HashMap<ValueId, StatementLocation>)
151151
{
152-
let successors = graph
152+
let successors = cfg
153153
.edges_directed(block, Direction::Outgoing)
154154
.map(|edge| edge.target())
155155
.collect_vec();
156156
for successor in &successors {
157-
merge_consecutive_basic_blocks(*successor, values, graph, value_usage, definitions);
157+
merge_consecutive_basic_blocks(*successor, values, cfg, value_usage, definitions);
158158
}
159159
if successors.len() == 1 {
160160
let successor = successors[0];
161-
if graph.edges_directed(successor, Direction::Incoming).count() == 1 {
162-
if let Some(Statement::CondJump(condition, _, _)) = graph[block].find_last() {
163-
unuse(*condition, graph, values, value_usage, definitions);
164-
graph[block].pop();
161+
if cfg.edges_directed(successor, Direction::Incoming).count() == 1 {
162+
if let Some(Statement::CondJump(condition, _, _)) = cfg[block].find_last() {
163+
unuse(*condition, cfg, values, value_usage, definitions);
164+
cfg[block].pop();
165165
}
166166

167-
let successor_basic_block = replace(&mut graph[successor], BasicBlock::new());
168-
let basic_block = &mut graph[block];
167+
let successor_basic_block = replace(&mut cfg[successor], BasicBlock::new());
168+
let basic_block = &mut cfg[block];
169169
for statement in successor_basic_block.into_iter() {
170170
if let Statement::Definition(value_id) = statement {
171171
let definition = definitions.get_mut(&value_id).unwrap();
@@ -174,14 +174,14 @@ fn merge_consecutive_basic_blocks(
174174
}
175175
}
176176

177-
let targets = graph
177+
let targets = cfg
178178
.edges_directed(successor, Direction::Outgoing)
179179
.map(|edge| edge.target())
180180
.collect_vec();
181181
for target in targets {
182-
graph.add_edge(block, target, ());
182+
cfg.add_edge(block, target, ());
183183
}
184-
graph.remove_node(successor);
184+
cfg.remove_node(successor);
185185
}
186186
}
187187
}
@@ -190,7 +190,7 @@ fn remove_unused_values(
190190
definitions: &mut HashMap<ValueId, StatementLocation>,
191191
values: &mut ValueStorage,
192192
functions: &mut FunctionMap,
193-
graph: &mut BasicBlockGraph,
193+
cfg: &mut ControlFlowGraph,
194194
program_result: &mut ValueId)
195195
{
196196
for (_, value_id) in values.iter() {
@@ -212,9 +212,9 @@ fn remove_unused_values(
212212
*operand = remap[operand];
213213
}
214214
}
215-
let blocks = graph.node_indices().collect_vec();
215+
let blocks = cfg.node_indices().collect_vec();
216216
for block in blocks {
217-
let basic_block = &mut graph[block];
217+
let basic_block = &mut cfg[block];
218218
for statement in basic_block.iter_mut() {
219219
for operand in get_statement_operands_mut(statement) {
220220
*operand = remap[operand];
@@ -225,8 +225,8 @@ fn remove_unused_values(
225225
*program_result = remap[program_result];
226226
}
227227

228-
fn remove_statements(locations: Vec<StatementLocation>, graph: &mut BasicBlockGraph) {
228+
fn remove_statements(locations: Vec<StatementLocation>, cfg: &mut ControlFlowGraph) {
229229
for location in locations {
230-
graph[location.block].remove(location.index);
230+
cfg[location.block].remove(location.index);
231231
}
232232
}

src/frontend/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ use petgraph::visit::EdgeRef;
2222

2323
use crate::frontend::dead_code::remove_dead_code;
2424
use crate::ir::BasicBlock;
25-
use crate::ir::BasicBlockGraph;
2625
use crate::ir::ControlFlowGraph;
2726
use crate::ir::FnId;
2827
use crate::ir::FunctionMap;
2928
use crate::ir::get_value_operands;
3029
use crate::ir::IdentGenerator;
30+
use crate::ir::IRModule;
3131
use crate::ir::normalize;
3232
use crate::ir::Phi;
3333
use crate::ir::replace_value_id;
@@ -51,7 +51,7 @@ pub(crate) struct FrontEnd {
5151
include_comments: bool,
5252
enable_cfp: bool,
5353
enable_dce: bool,
54-
graph: BasicBlockGraph,
54+
cfg: ControlFlowGraph,
5555
function_idgen: IdentGenerator<FnId>,
5656
variables: HashMap<BindingRef, HashMap<NodeIndex, ValueId>>,
5757
variables_read: HashSet<BindingRef>,
@@ -75,7 +75,7 @@ impl FrontEnd {
7575
include_comments: false,
7676
enable_cfp: false,
7777
enable_dce: false,
78-
graph: BasicBlockGraph::new(),
78+
cfg: ControlFlowGraph::new(),
7979
function_idgen: IdentGenerator::new(),
8080
variables: HashMap::new(),
8181
variables_read: HashSet::new(),
@@ -115,7 +115,7 @@ impl FrontEnd {
115115
self
116116
}
117117

118-
pub(crate) fn build(mut self, code: &ExprRef) -> ControlFlowGraph {
118+
pub(crate) fn build(mut self, code: &ExprRef) -> IRModule {
119119
let (exit_block, mut program_result) = self.process_expr(code, self.entry_block);
120120
self.push_return(exit_block, program_result);
121121
self.assert_no_undefined_variables();
@@ -131,12 +131,12 @@ impl FrontEnd {
131131

132132
if self.enable_dce {
133133
remove_dead_code(self.entry_block, &mut program_result, &mut self.values,
134-
&mut self.graph, &mut self.definitions, &mut self.functions);
134+
&mut self.cfg, &mut self.definitions, &mut self.functions);
135135
}
136136

137-
ControlFlowGraph {
137+
IRModule {
138138
name: self.name,
139-
graph: self.graph,
139+
cfg: self.cfg,
140140
entry_block: self.entry_block,
141141
exit_block,
142142
definitions: self.definitions,
@@ -217,19 +217,19 @@ impl FrontEnd {
217217
let then_branch_block = self.new_block();
218218
let else_branch_block = self.new_block();
219219

220-
self.graph[block].push(Statement::CondJump(condition, then_branch_block, else_branch_block));
220+
self.cfg[block].push(Statement::CondJump(condition, then_branch_block, else_branch_block));
221221
self.record_phi_and_undefined_usages(condition);
222222

223-
self.graph.add_edge(block, then_branch_block, ());
224-
self.graph.add_edge(block, else_branch_block, ());
223+
self.cfg.add_edge(block, then_branch_block, ());
224+
self.cfg.add_edge(block, else_branch_block, ());
225225
self.seal_block(then_branch_block);
226226
self.seal_block(else_branch_block);
227227
let (then_branch_exit_block, then_branch) = self.process_expr(then_branch, then_branch_block);
228228
let (else_branch_exit_block, else_branch) = self.process_expr(else_branch, else_branch_block);
229229

230230
let exit = self.new_block();
231-
self.graph.add_edge(then_branch_exit_block, exit, ());
232-
self.graph.add_edge(else_branch_exit_block, exit, ());
231+
self.cfg.add_edge(then_branch_exit_block, exit, ());
232+
self.cfg.add_edge(else_branch_exit_block, exit, ());
233233
self.seal_block(exit);
234234
let result_phi = self.define_phi(exit, &[then_branch, else_branch]);
235235
(exit, self.try_remove_trivial_phi(result_phi))
@@ -340,7 +340,7 @@ impl FrontEnd {
340340
_ => (),
341341
}
342342

343-
let mut predecessors = self.graph
343+
let mut predecessors = self.cfg
344344
.edges_directed(block, Direction::Incoming)
345345
.map(|edge| edge.source());
346346
let first_predecessor = predecessors.next();
@@ -384,7 +384,7 @@ impl FrontEnd {
384384
}
385385

386386
fn add_phi_operands(&mut self, variable: &BindingRef, phi: ValueId) -> ValueId {
387-
let predecessors = self.graph
387+
let predecessors = self.cfg
388388
.edges_directed(self.definitions[&phi].block, Direction::Incoming)
389389
.map(|edge| edge.source())
390390
.collect_vec();
@@ -458,7 +458,7 @@ impl FrontEnd {
458458
if self.enable_cfp {
459459
fold_constants(block, &self.definitions, &mut self.values, &self.functions, value_id);
460460
}
461-
let basic_block = &mut self.graph[block];
461+
let basic_block = &mut self.cfg[block];
462462
let statement_index = basic_block.push(Statement::Definition(value_id));
463463
let location = StatementLocation::new(block, statement_index);
464464
self.definitions.insert(value_id, location);
@@ -477,7 +477,7 @@ impl FrontEnd {
477477
}
478478

479479
fn push_return(&mut self, block: NodeIndex, value_id: ValueId) {
480-
self.graph[block].push(Statement::Return(value_id));
480+
self.cfg[block].push(Statement::Return(value_id));
481481
}
482482

483483
fn record_phi_and_undefined_usages(&mut self, value_id: ValueId) {
@@ -518,12 +518,12 @@ impl FrontEnd {
518518

519519
fn comment(&mut self, block: NodeIndex, comment: &str) {
520520
if self.include_comments {
521-
self.graph[block].push(Statement::Comment(comment.to_owned()));
521+
self.cfg[block].push(Statement::Comment(comment.to_owned()));
522522
}
523523
}
524524

525525
fn new_block(&mut self) -> NodeIndex {
526-
self.graph.add_node(BasicBlock::new())
526+
self.cfg.add_node(BasicBlock::new())
527527
}
528528

529529
fn reset_markers(&mut self) {

0 commit comments

Comments
 (0)