@@ -10,7 +10,7 @@ use petgraph::graph::NodeIndex;
10
10
use petgraph:: visit:: EdgeRef ;
11
11
12
12
use crate :: ir:: BasicBlock ;
13
- use crate :: ir:: BasicBlockGraph ;
13
+ use crate :: ir:: ControlFlowGraph ;
14
14
use crate :: ir:: FunctionMap ;
15
15
use crate :: ir:: get_statement_operands_mut;
16
16
use crate :: ir:: get_statement_value_operands;
@@ -25,22 +25,22 @@ pub(crate) fn remove_dead_code(
25
25
entry_block : NodeIndex ,
26
26
program_result : & mut ValueId ,
27
27
values : & mut ValueStorage ,
28
- graph : & mut BasicBlockGraph ,
28
+ cfg : & mut ControlFlowGraph ,
29
29
definitions : & mut HashMap < ValueId , StatementLocation > ,
30
30
functions : & mut FunctionMap )
31
31
{
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) ;
38
38
}
39
39
40
40
fn compute_value_usage (
41
41
program_result : ValueId ,
42
42
values : & mut ValueStorage ,
43
- graph : & mut BasicBlockGraph ,
43
+ cfg : & mut ControlFlowGraph ,
44
44
definitions : & mut HashMap < ValueId , StatementLocation > )
45
45
-> HashMap < ValueId , usize >
46
46
{
@@ -49,9 +49,9 @@ fn compute_value_usage(
49
49
. keys ( )
50
50
. cloned ( )
51
51
. zip ( repeat ( 0 ) ) ) ;
52
- let used_values = graph
52
+ let used_values = cfg
53
53
. node_indices ( )
54
- . flat_map ( |node| graph [ node] . iter ( ) )
54
+ . flat_map ( |node| cfg [ node] . iter ( ) )
55
55
. flat_map ( |statement| get_statement_value_operands ( & values, statement) )
56
56
. chain ( once ( program_result) ) ;
57
57
for value_id in used_values {
@@ -63,7 +63,7 @@ fn compute_value_usage(
63
63
fn remove_unused_definitions (
64
64
value_usage : & mut HashMap < ValueId , usize > ,
65
65
values : & mut ValueStorage ,
66
- graph : & mut BasicBlockGraph ,
66
+ cfg : & mut ControlFlowGraph ,
67
67
definitions : & mut HashMap < ValueId , StatementLocation > )
68
68
{
69
69
let unused_values = value_usage
@@ -74,7 +74,7 @@ fn remove_unused_definitions(
74
74
} )
75
75
. collect_vec ( ) ;
76
76
for value_id in unused_values {
77
- unuse ( value_id, graph , values, value_usage, definitions) ;
77
+ unuse ( value_id, cfg , values, value_usage, definitions) ;
78
78
}
79
79
80
80
let ( unused_values, dead_code) : ( Vec < _ > , Vec < _ > ) = value_usage
@@ -88,12 +88,12 @@ fn remove_unused_definitions(
88
88
definitions. remove ( & value_id) ;
89
89
value_usage. remove ( & value_id) ;
90
90
}
91
- remove_statements ( dead_code, graph ) ;
91
+ remove_statements ( dead_code, cfg ) ;
92
92
}
93
93
94
94
fn unuse (
95
95
value_id : ValueId ,
96
- graph : & BasicBlockGraph ,
96
+ cfg : & ControlFlowGraph ,
97
97
values : & ValueStorage ,
98
98
value_usage : & mut HashMap < ValueId , usize > ,
99
99
definitions : & mut HashMap < ValueId , StatementLocation > )
@@ -105,38 +105,38 @@ fn unuse(
105
105
106
106
if * usage_count == 0 {
107
107
let location = & definitions[ & value_id] ;
108
- let statement = & graph [ location. block ] [ location. index ] ;
108
+ let statement = & cfg [ location. block ] [ location. index ] ;
109
109
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) ;
111
111
}
112
112
}
113
113
}
114
114
115
- fn remove_empty_blocks ( graph : & mut BasicBlockGraph ) {
116
- let blocks = graph
115
+ fn remove_empty_blocks ( cfg : & mut ControlFlowGraph ) {
116
+ let blocks = cfg
117
117
. node_indices ( )
118
118
. collect_vec ( ) ;
119
119
for block in blocks {
120
- let is_empty = graph [ block]
120
+ let is_empty = cfg [ block]
121
121
. iter ( )
122
122
. find ( |statement| !matches ! ( statement, Statement :: Comment ( _) ) )
123
123
. is_none ( ) ;
124
124
if is_empty {
125
- let sources = graph
125
+ let sources = cfg
126
126
. edges_directed ( block, Direction :: Incoming )
127
127
. map ( |edge| edge. source ( ) )
128
128
. collect_vec ( ) ;
129
- let targets = graph
129
+ let targets = cfg
130
130
. edges_directed ( block, Direction :: Outgoing )
131
131
. map ( |edge| edge. target ( ) )
132
132
. collect_vec ( ) ;
133
133
if sources. len ( ) == 1 && targets. len ( ) == 1 {
134
134
let source = sources[ 0 ] ;
135
135
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, ( ) ) ;
138
138
}
139
- graph . remove_node ( block) ;
139
+ cfg . remove_node ( block) ;
140
140
}
141
141
}
142
142
}
@@ -145,27 +145,27 @@ fn remove_empty_blocks(graph: &mut BasicBlockGraph) {
145
145
fn merge_consecutive_basic_blocks (
146
146
block : NodeIndex ,
147
147
values : & ValueStorage ,
148
- graph : & mut BasicBlockGraph ,
148
+ cfg : & mut ControlFlowGraph ,
149
149
value_usage : & mut HashMap < ValueId , usize > ,
150
150
definitions : & mut HashMap < ValueId , StatementLocation > )
151
151
{
152
- let successors = graph
152
+ let successors = cfg
153
153
. edges_directed ( block, Direction :: Outgoing )
154
154
. map ( |edge| edge. target ( ) )
155
155
. collect_vec ( ) ;
156
156
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) ;
158
158
}
159
159
if successors. len ( ) == 1 {
160
160
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 ( ) ;
165
165
}
166
166
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] ;
169
169
for statement in successor_basic_block. into_iter ( ) {
170
170
if let Statement :: Definition ( value_id) = statement {
171
171
let definition = definitions. get_mut ( & value_id) . unwrap ( ) ;
@@ -174,14 +174,14 @@ fn merge_consecutive_basic_blocks(
174
174
}
175
175
}
176
176
177
- let targets = graph
177
+ let targets = cfg
178
178
. edges_directed ( successor, Direction :: Outgoing )
179
179
. map ( |edge| edge. target ( ) )
180
180
. collect_vec ( ) ;
181
181
for target in targets {
182
- graph . add_edge ( block, target, ( ) ) ;
182
+ cfg . add_edge ( block, target, ( ) ) ;
183
183
}
184
- graph . remove_node ( successor) ;
184
+ cfg . remove_node ( successor) ;
185
185
}
186
186
}
187
187
}
@@ -190,7 +190,7 @@ fn remove_unused_values(
190
190
definitions : & mut HashMap < ValueId , StatementLocation > ,
191
191
values : & mut ValueStorage ,
192
192
functions : & mut FunctionMap ,
193
- graph : & mut BasicBlockGraph ,
193
+ cfg : & mut ControlFlowGraph ,
194
194
program_result : & mut ValueId )
195
195
{
196
196
for ( _, value_id) in values. iter ( ) {
@@ -212,9 +212,9 @@ fn remove_unused_values(
212
212
* operand = remap[ operand] ;
213
213
}
214
214
}
215
- let blocks = graph . node_indices ( ) . collect_vec ( ) ;
215
+ let blocks = cfg . node_indices ( ) . collect_vec ( ) ;
216
216
for block in blocks {
217
- let basic_block = & mut graph [ block] ;
217
+ let basic_block = & mut cfg [ block] ;
218
218
for statement in basic_block. iter_mut ( ) {
219
219
for operand in get_statement_operands_mut ( statement) {
220
220
* operand = remap[ operand] ;
@@ -225,8 +225,8 @@ fn remove_unused_values(
225
225
* program_result = remap[ program_result] ;
226
226
}
227
227
228
- fn remove_statements ( locations : Vec < StatementLocation > , graph : & mut BasicBlockGraph ) {
228
+ fn remove_statements ( locations : Vec < StatementLocation > , cfg : & mut ControlFlowGraph ) {
229
229
for location in locations {
230
- graph [ location. block ] . remove ( location. index ) ;
230
+ cfg [ location. block ] . remove ( location. index ) ;
231
231
}
232
232
}
0 commit comments