Skip to content

Commit 106accd

Browse files
committed
Simplify naming and add end block
1 parent bc23182 commit 106accd

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

core/engine/src/optimizer/control_flow_graph/basic_block.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ bitflags! {
1616
/// TODO: doc
1717
#[derive(Default, Clone)]
1818
pub struct BasicBlock {
19-
pub(crate) predecessors: Vec<BasicBlockKey>,
19+
pub(crate) previous: Vec<BasicBlockKey>,
2020
pub(crate) instructions: Vec<Instruction>,
2121
pub(crate) terminator: Terminator,
2222
pub(crate) handler: Option<BasicBlockKey>,
@@ -54,23 +54,15 @@ impl BasicBlock {
5454
self.flags.contains(BasicBlockFlags::REACHABLE)
5555
}
5656

57-
pub(crate) fn successors(&self) -> Vec<BasicBlockKey> {
58-
match self.terminator {
59-
Terminator::None => vec![],
60-
Terminator::JumpUnconditional { target, .. } => {
61-
vec![target]
62-
}
63-
Terminator::JumpConditional { no, yes, .. }
64-
| Terminator::TemplateLookup { no, yes, .. } => {
65-
vec![no, yes]
66-
}
67-
Terminator::Return => Vec::new(),
68-
}
57+
pub(crate) fn next(&self) -> Vec<BasicBlockKey> {
58+
let mut result = Vec::new();
59+
self.next_into(&mut result);
60+
result
6961
}
7062

71-
pub(crate) fn next(&self, nexts: &mut Vec<BasicBlockKey>) {
63+
pub(crate) fn next_into(&self, nexts: &mut Vec<BasicBlockKey>) {
7264
match self.terminator {
73-
Terminator::None | Terminator::Return => {}
65+
Terminator::None => {}
7466
Terminator::JumpUnconditional { target, .. } => {
7567
nexts.push(target);
7668
}
@@ -79,6 +71,7 @@ impl BasicBlock {
7971
nexts.push(no);
8072
nexts.push(yes);
8173
}
74+
Terminator::Return { end } => nexts.push(end),
8275
}
8376
}
8477
}

core/engine/src/optimizer/control_flow_graph/mod.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub(crate) enum Terminator {
5454
},
5555

5656
/// TODO: doc
57-
Return,
57+
Return { end: BasicBlockKey },
5858
}
5959

6060
impl Terminator {
@@ -89,6 +89,7 @@ impl Terminator {
8989
/// TODO: doc
9090
pub struct ControlFlowGraph {
9191
basic_block_start: BasicBlockKey,
92+
basic_block_end: BasicBlockKey,
9293
basic_blocks: SlotMap<BasicBlockKey, BasicBlock>,
9394
}
9495

@@ -122,19 +123,26 @@ impl Debug for ControlFlowGraph {
122123
if basic_block.reachable() { "" } else { "not " }
123124
)?;
124125

125-
if !basic_block.predecessors.is_empty() {
126-
write!(f, " -- predecessors ")?;
127-
for predecessor in &basic_block.predecessors {
126+
if key == self.basic_block_start {
127+
write!(f, " -- start")?;
128+
}
129+
if key == self.basic_block_end {
130+
write!(f, " -- end")?;
131+
}
132+
133+
if !basic_block.previous.is_empty() {
134+
write!(f, " -- previous ")?;
135+
for predecessor in &basic_block.previous {
128136
let index = index_from_basic_block(*predecessor);
129137
write!(f, "B{index}, ")?;
130138
}
131139
}
132140

133-
let successors = basic_block.successors();
134-
if !successors.is_empty() {
135-
write!(f, " -- successors ")?;
136-
for successor in &successors {
137-
let index = index_from_basic_block(*successor);
141+
let next = basic_block.next();
142+
if !next.is_empty() {
143+
write!(f, " -- next ")?;
144+
for bb in &next {
145+
let index = index_from_basic_block(*bb);
138146
write!(f, "B{index}, ")?;
139147
}
140148
}
@@ -171,8 +179,9 @@ impl Debug for ControlFlowGraph {
171179
let target = index_from_basic_block(*yes);
172180
write!(f, "TemplateLookup B{target}")?;
173181
}
174-
Terminator::Return => {
175-
write!(f, "Return")?;
182+
Terminator::Return { end } => {
183+
let target = index_from_basic_block(*end);
184+
write!(f, "Return B{target}")?;
176185
}
177186
}
178187
writeln!(f)?;
@@ -271,6 +280,8 @@ impl ControlFlowGraph {
271280
basic_block_keys[index]
272281
};
273282

283+
let basic_block_end = basic_block_keys[leaders.len() - 1];
284+
274285
let mut iter = InstructionIterator::new(bytecode);
275286
for (i, leader) in leaders
276287
.iter()
@@ -296,12 +307,14 @@ impl ControlFlowGraph {
296307
while let Some((_, _, instruction)) = iter.next() {
297308
match instruction {
298309
Instruction::Return => {
299-
terminator = Terminator::Return;
310+
terminator = Terminator::Return {
311+
end: basic_block_end,
312+
};
300313
}
301314
Instruction::Jump { address } | Instruction::Default { address } => {
302315
let target = basic_block_from_bytecode_position(address);
303316

304-
basic_blocks[target].predecessors.push(key);
317+
basic_blocks[target].previous.push(key);
305318

306319
terminator = Terminator::JumpUnconditional {
307320
opcode: instruction.opcode(),
@@ -315,8 +328,8 @@ impl ControlFlowGraph {
315328
let yes = basic_block_from_bytecode_position(address);
316329
let no = basic_block_keys[i + 1];
317330

318-
basic_blocks[yes].predecessors.push(key);
319-
basic_blocks[no].predecessors.push(key);
331+
basic_blocks[yes].previous.push(key);
332+
basic_blocks[no].previous.push(key);
320333

321334
terminator = Terminator::TemplateLookup { no, yes, site };
322335
}
@@ -325,8 +338,8 @@ impl ControlFlowGraph {
325338
let yes = basic_block_from_bytecode_position(address);
326339
let no = basic_block_keys[i + 1];
327340

328-
basic_blocks[yes].predecessors.push(key);
329-
basic_blocks[no].predecessors.push(key);
341+
basic_blocks[yes].previous.push(key);
342+
basic_blocks[no].previous.push(key);
330343

331344
terminator = Terminator::JumpConditional {
332345
opcode: instruction.opcode(),
@@ -351,6 +364,7 @@ impl ControlFlowGraph {
351364

352365
Self {
353366
basic_block_start: basic_block_keys[0],
367+
basic_block_end,
354368
basic_blocks,
355369
}
356370
}
@@ -520,7 +534,7 @@ impl GraphEliminateUnreachableBasicBlocks {
520534
// "reachable basic blocks should not be eliminated"
521535
// );
522536

523-
// basic_block.predecessors.clear();
537+
// basic_block.previous.clear();
524538
// basic_block.terminator = Terminator::None;
525539

526540
// changed |= true;

0 commit comments

Comments
 (0)