forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfg.rs
More file actions
354 lines (294 loc) · 9.92 KB
/
Copy pathcfg.rs
File metadata and controls
354 lines (294 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::convert::Infallible;
use cranelift_entity::{EntityRef, SecondaryMap};
use crate::{JoinSemiLattice, queue::WorkQueue};
pub trait ForwardCfgAnalysis {
type Block: EntityRef;
type State: Clone + JoinSemiLattice;
type Error;
fn block_count(&self) -> usize;
fn seed_blocks(&self) -> Vec<Self::Block>;
fn bottom(&self) -> Self::State;
fn initialize(
&mut self,
_entry_states: &mut SecondaryMap<Self::Block, Self::State>,
) -> Result<(), Self::Error> {
Ok(())
}
fn transfer(
&mut self,
block: Self::Block,
in_state: &Self::State,
) -> Result<Self::State, Self::Error>;
fn successors(&self, block: Self::Block) -> &[Self::Block];
}
pub trait BackwardCfgAnalysis {
type Block: EntityRef;
type State: Clone + JoinSemiLattice;
fn block_count(&self) -> usize;
fn seed_blocks(&self) -> Vec<Self::Block>;
fn bottom(&self) -> Self::State;
fn initialize(&mut self, exit_states: &mut SecondaryMap<Self::Block, Self::State>);
fn transfer(&mut self, block: Self::Block, out_state: &Self::State) -> Self::State;
fn predecessors(&self, block: Self::Block) -> &[Self::Block];
}
pub fn solve_forward_cfg<A: ForwardCfgAnalysis<Error = Infallible>>(
analysis: &mut A,
) -> SecondaryMap<A::Block, A::State> {
match try_solve_forward_cfg(analysis) {
Ok(states) => states,
Err(err) => match err {},
}
}
pub fn solve_backward_cfg<A: BackwardCfgAnalysis>(
analysis: &mut A,
) -> SecondaryMap<A::Block, A::State> {
let mut exit_states = SecondaryMap::with_default(analysis.bottom());
exit_states.resize(analysis.block_count());
analysis.initialize(&mut exit_states);
let seed_blocks = analysis.seed_blocks();
let mut reached = SecondaryMap::with_default(false);
reached.resize(analysis.block_count());
for block in seed_blocks.iter().copied() {
reached[block] = true;
}
let mut queue = WorkQueue::with_seed(analysis.block_count(), seed_blocks);
while let Some(block) = queue.pop() {
let in_state = analysis.transfer(block, &exit_states[block]);
for pred in analysis.predecessors(block).iter().copied() {
let changed = exit_states[pred].join_into(&in_state);
let newly_reached = !reached[pred];
reached[pred] = true;
if newly_reached || changed {
queue.push(pred);
}
}
}
exit_states
}
pub fn try_solve_forward_cfg<A: ForwardCfgAnalysis>(
analysis: &mut A,
) -> Result<SecondaryMap<A::Block, A::State>, A::Error> {
let mut entry_states = SecondaryMap::with_default(analysis.bottom());
entry_states.resize(analysis.block_count());
analysis.initialize(&mut entry_states)?;
let seed_blocks = analysis.seed_blocks();
let mut reached = SecondaryMap::with_default(false);
reached.resize(analysis.block_count());
for block in seed_blocks.iter().copied() {
reached[block] = true;
}
let mut queue = WorkQueue::with_seed(analysis.block_count(), seed_blocks);
while let Some(block) = queue.pop() {
let out_state = analysis.transfer(block, &entry_states[block])?;
for succ in analysis.successors(block).iter().copied() {
let changed = entry_states[succ].join_into(&out_state);
let newly_reached = !reached[succ];
reached[succ] = true;
if newly_reached || changed {
queue.push(succ);
}
}
}
Ok(entry_states)
}
#[cfg(test)]
mod tests {
use std::convert::Infallible;
use cranelift_entity::{EntityRef, entity_impl};
use super::{BackwardCfgAnalysis, ForwardCfgAnalysis, solve_backward_cfg, solve_forward_cfg};
use crate::JoinSemiLattice;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Block(u32);
entity_impl!(Block);
const SUCCESSORS: [&[Block]; 4] = [&[Block(1), Block(2)], &[Block(3)], &[Block(3)], &[]];
const PREDECESSORS: [&[Block]; 4] = [&[], &[Block(0)], &[Block(0)], &[Block(1), Block(2)]];
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct Bits(u8);
impl JoinSemiLattice for Bits {
fn join_into(&mut self, other: &Self) -> bool {
let joined = self.0 | other.0;
let changed = joined != self.0;
self.0 = joined;
changed
}
}
struct ForwardBitsAnalysis;
impl ForwardCfgAnalysis for ForwardBitsAnalysis {
type Block = Block;
type State = Bits;
type Error = Infallible;
fn block_count(&self) -> usize {
SUCCESSORS.len()
}
fn seed_blocks(&self) -> Vec<Self::Block> {
vec![Block::new(0)]
}
fn bottom(&self) -> Self::State {
Bits(0)
}
fn initialize(
&mut self,
entry_states: &mut cranelift_entity::SecondaryMap<Self::Block, Self::State>,
) -> Result<(), Self::Error> {
entry_states[Block::new(0)] = Bits(0b0001);
Ok(())
}
fn transfer(
&mut self,
block: Self::Block,
in_state: &Self::State,
) -> Result<Self::State, Self::Error> {
Ok(Bits(in_state.0 | (1 << block.index())))
}
fn successors(&self, block: Self::Block) -> &[Self::Block] {
SUCCESSORS[block.index()]
}
}
struct BackwardBitsAnalysis;
impl BackwardCfgAnalysis for BackwardBitsAnalysis {
type Block = Block;
type State = Bits;
fn block_count(&self) -> usize {
PREDECESSORS.len()
}
fn seed_blocks(&self) -> Vec<Self::Block> {
vec![Block::new(3)]
}
fn bottom(&self) -> Self::State {
Bits(0)
}
fn initialize(
&mut self,
exit_states: &mut cranelift_entity::SecondaryMap<Self::Block, Self::State>,
) {
exit_states[Block::new(3)] = Bits(0b1000);
}
fn transfer(&mut self, block: Self::Block, out_state: &Self::State) -> Self::State {
Bits(out_state.0 | (1 << block.index()))
}
fn predecessors(&self, block: Self::Block) -> &[Self::Block] {
PREDECESSORS[block.index()]
}
}
#[test]
fn forward_cfg_solver_propagates_through_diamond() {
let states = solve_forward_cfg(&mut ForwardBitsAnalysis);
assert_eq!(
[
states[Block::new(0)],
states[Block::new(1)],
states[Block::new(2)],
states[Block::new(3)]
],
[Bits(0b0001), Bits(0b0001), Bits(0b0001), Bits(0b0111)]
);
}
#[test]
fn backward_cfg_solver_propagates_through_diamond() {
let states = solve_backward_cfg(&mut BackwardBitsAnalysis);
assert_eq!(
[
states[Block::new(0)],
states[Block::new(1)],
states[Block::new(2)],
states[Block::new(3)]
],
[Bits(0b1110), Bits(0b1000), Bits(0b1000), Bits(0b1000)]
);
}
struct ReachabilityAnalysis;
impl ForwardCfgAnalysis for ReachabilityAnalysis {
type Block = Block;
type State = Bits;
type Error = Infallible;
fn block_count(&self) -> usize {
3
}
fn seed_blocks(&self) -> Vec<Self::Block> {
vec![Block::new(0)]
}
fn bottom(&self) -> Self::State {
Bits(0)
}
fn initialize(
&mut self,
entry_states: &mut cranelift_entity::SecondaryMap<Self::Block, Self::State>,
) -> Result<(), Self::Error> {
entry_states[Block::new(0)] = Bits(1);
Ok(())
}
fn transfer(
&mut self,
block: Self::Block,
in_state: &Self::State,
) -> Result<Self::State, Self::Error> {
Ok(Bits(in_state.0 | (1 << block.index())))
}
fn successors(&self, block: Self::Block) -> &[Self::Block] {
match block.index() {
0 => &[Block(1)],
1 | 2 => &[],
_ => unreachable!(),
}
}
}
#[test]
fn forward_cfg_solver_does_not_process_unreachable_blocks() {
let states = solve_forward_cfg(&mut ReachabilityAnalysis);
assert_eq!(
[
states[Block::new(0)],
states[Block::new(1)],
states[Block::new(2)]
],
[Bits(1), Bits(1), Bits(0)]
);
}
struct ReachBottomThenGenerateAnalysis;
impl ForwardCfgAnalysis for ReachBottomThenGenerateAnalysis {
type Block = Block;
type State = Bits;
type Error = Infallible;
fn block_count(&self) -> usize {
3
}
fn seed_blocks(&self) -> Vec<Self::Block> {
vec![Block::new(0)]
}
fn bottom(&self) -> Self::State {
Bits(0)
}
fn transfer(
&mut self,
block: Self::Block,
in_state: &Self::State,
) -> Result<Self::State, Self::Error> {
Ok(match block.index() {
0 => *in_state,
1 => Bits(0b10),
2 => Bits(in_state.0 | 0b100),
_ => unreachable!(),
})
}
fn successors(&self, block: Self::Block) -> &[Self::Block] {
match block.index() {
0 => &[Block(1)],
1 => &[Block(2)],
2 => &[],
_ => unreachable!(),
}
}
}
#[test]
fn forward_cfg_solver_processes_newly_reached_bottom_state_blocks() {
let states = solve_forward_cfg(&mut ReachBottomThenGenerateAnalysis);
assert_eq!(
[
states[Block::new(0)],
states[Block::new(1)],
states[Block::new(2)]
],
[Bits(0), Bits(0), Bits(0b10)]
);
}
}