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
use crate::{
block::Block, context::Context, error::IrError, function::Function, instruction::Instruction,
value::ValueDatum,
};
use std::collections::HashMap;
pub fn simplify_cfg(context: &mut Context, function: &Function) -> Result<bool, IrError> {
let mut modified = false;
modified |= remove_dead_blocks(context, function)?;
let pred_counts = function.count_predecessors(context);
loop {
if merge_blocks(context, &pred_counts, function)? {
modified = true;
continue;
}
break;
}
Ok(modified)
}
fn remove_dead_blocks(context: &mut Context, function: &Function) -> Result<bool, IrError> {
let mut worklist = Vec::<Block>::new();
let mut reachable = std::collections::HashSet::<Block>::new();
let entry_block = function.get_entry_block(context);
reachable.insert(entry_block);
worklist.push(entry_block);
while !worklist.is_empty() {
let block = worklist.pop().unwrap();
let succs = block.successors(context);
for succ in succs {
if !reachable.contains(&succ) {
reachable.insert(succ);
worklist.push(succ);
}
}
}
let mut modified = false;
for block in function.block_iter(context) {
if !reachable.contains(&block) {
modified = true;
for succ in block.successors(context) {
succ.remove_phi_val_coming_from(context, &block);
}
function.remove_block(context, &block)?;
}
}
Ok(modified)
}
fn merge_blocks(
context: &mut Context,
pred_counts: &HashMap<Block, usize>,
function: &Function,
) -> Result<bool, IrError> {
let check_candidate = |from_block: Block| -> Option<(Block, Block)> {
from_block
.get_terminator(context)
.and_then(|term| match term {
Instruction::Branch(to_block) if pred_counts[to_block] == 1 => {
Some((from_block, *to_block))
}
_ => None,
})
};
let twin_blocks = function
.block_iter(context)
.find_map(check_candidate);
let mut block_chain = match twin_blocks {
Some((from_block, to_block)) => vec![from_block, to_block],
None => return Ok(false),
};
loop {
match check_candidate(block_chain.last().copied().unwrap()) {
None => {
break;
}
Some(next_pair) => {
block_chain.push(next_pair.1);
}
}
}
let final_to_block = block_chain.last().copied().unwrap();
let final_to_block_succs = final_to_block.successors(context);
let mut block_chain = block_chain.into_iter();
let from_block = block_chain.next().unwrap();
for to_block in block_chain {
let phi_val = to_block.get_phi(context);
match &context.values[phi_val.0].value {
ValueDatum::Instruction(Instruction::Phi(els)) if els.len() <= 1 => {
if let Some((_, ref_val)) = els.get(0) {
function.replace_value(context, phi_val, *ref_val, None);
to_block.remove_instruction(context, phi_val);
}
}
_otherwise => return Err(IrError::InvalidPhi),
};
let (from_contents, to_contents) = context.blocks.get2_mut(from_block.0, to_block.0);
let from_contents = from_contents.unwrap();
let to_contents = to_contents.unwrap();
from_contents.instructions.pop();
from_contents
.instructions
.append(&mut to_contents.instructions);
function.remove_block(context, &to_block)?;
}
for succ in final_to_block_succs {
succ.update_phi_source_block(context, final_to_block, from_block)
}
Ok(true)
}