Skip to content

Commit 2c84259

Browse files
authored
Refactor matches that used to consume BranchInfo (#5734)
Explicitly borrow the instruction data, and use a mutable borrow to avoid rematch.
1 parent fdd4a77 commit 2c84259

File tree

8 files changed

+24
-50
lines changed

8 files changed

+24
-50
lines changed

cranelift/codegen/src/dominator_tree.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl DominatorTree {
351351
/// post-order except for the insertion of the new block header at the split point.
352352
fn push_successors(&mut self, func: &Function, block: Block) {
353353
if let Some(inst) = func.layout.last_inst(block) {
354-
match func.dfg.insts[inst] {
354+
match &func.dfg.insts[inst] {
355355
ir::InstructionData::Jump {
356356
destination: succ, ..
357357
} => self.push_if_unseen(succ.block(&func.dfg.value_lists)),
@@ -367,10 +367,10 @@ impl DominatorTree {
367367
destination: dest,
368368
..
369369
} => {
370-
for succ in func.jump_tables[jt].iter() {
370+
for succ in func.jump_tables[*jt].iter() {
371371
self.push_if_unseen(*succ);
372372
}
373-
self.push_if_unseen(dest);
373+
self.push_if_unseen(*dest);
374374
}
375375
_ => {}
376376
}

cranelift/codegen/src/flowgraph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl ControlFlowGraph {
117117

118118
fn compute_block(&mut self, func: &Function, block: Block) {
119119
if let Some(inst) = func.layout.last_inst(block) {
120-
match func.dfg.insts[inst] {
120+
match &func.dfg.insts[inst] {
121121
ir::InstructionData::Jump {
122122
destination: dest, ..
123123
} => {
@@ -135,9 +135,9 @@ impl ControlFlowGraph {
135135
destination: dest,
136136
..
137137
} => {
138-
self.add_edge(block, inst, dest);
138+
self.add_edge(block, inst, *dest);
139139

140-
for dest in func.jump_tables[jt].iter() {
140+
for dest in func.jump_tables[*jt].iter() {
141141
self.add_edge(block, inst, *dest);
142142
}
143143
}

cranelift/codegen/src/inst_predicates.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub(crate) fn visit_block_succs<F: FnMut(Inst, Block, bool)>(
175175
mut visit: F,
176176
) {
177177
if let Some(inst) = f.layout.last_inst(block) {
178-
match f.dfg.insts[inst] {
178+
match &f.dfg.insts[inst] {
179179
ir::InstructionData::Jump {
180180
destination: dest, ..
181181
} => {
@@ -197,9 +197,9 @@ pub(crate) fn visit_block_succs<F: FnMut(Inst, Block, bool)>(
197197
} => {
198198
// The default block is reached via a direct conditional branch,
199199
// so it is not part of the table.
200-
visit(inst, dest, false);
200+
visit(inst, *dest, false);
201201

202-
for &dest in f.jump_tables[table].as_slice() {
202+
for &dest in f.jump_tables[*table].as_slice() {
203203
visit(inst, dest, true);
204204
}
205205
}

cranelift/codegen/src/ir/function.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,12 @@ impl FunctionStencil {
277277
/// Rewrite the branch destination to `new_dest` if the destination matches `old_dest`.
278278
/// Does nothing if called with a non-jump or non-branch instruction.
279279
pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {
280-
match self.dfg.insts[inst] {
280+
match &mut self.dfg.insts[inst] {
281281
InstructionData::Jump {
282282
destination: dest, ..
283283
} => {
284284
if dest.block(&self.dfg.value_lists) == old_dest {
285-
for block in self.dfg.insts[inst].branch_destination_mut() {
286-
block.set_block(new_dest, &mut self.dfg.value_lists)
287-
}
285+
dest.set_block(new_dest, &mut self.dfg.value_lists)
288286
}
289287
}
290288

@@ -293,27 +291,11 @@ impl FunctionStencil {
293291
..
294292
} => {
295293
if block_then.block(&self.dfg.value_lists) == old_dest {
296-
if let InstructionData::Brif {
297-
blocks: [block_then, _],
298-
..
299-
} = &mut self.dfg.insts[inst]
300-
{
301-
block_then.set_block(new_dest, &mut self.dfg.value_lists);
302-
} else {
303-
unreachable!();
304-
}
294+
block_then.set_block(new_dest, &mut self.dfg.value_lists);
305295
}
306296

307297
if block_else.block(&self.dfg.value_lists) == old_dest {
308-
if let InstructionData::Brif {
309-
blocks: [_, block_else],
310-
..
311-
} = &mut self.dfg.insts[inst]
312-
{
313-
block_else.set_block(new_dest, &mut self.dfg.value_lists);
314-
} else {
315-
unreachable!();
316-
}
298+
block_else.set_block(new_dest, &mut self.dfg.value_lists);
317299
}
318300
}
319301

@@ -322,22 +304,14 @@ impl FunctionStencil {
322304
destination: default_dest,
323305
..
324306
} => {
325-
self.jump_tables[table].iter_mut().for_each(|entry| {
307+
self.jump_tables[*table].iter_mut().for_each(|entry| {
326308
if *entry == old_dest {
327309
*entry = new_dest;
328310
}
329311
});
330312

331-
if default_dest == old_dest {
332-
match &mut self.dfg.insts[inst] {
333-
InstructionData::BranchTable { destination, .. } => {
334-
*destination = new_dest;
335-
}
336-
_ => panic!(
337-
"Unexpected instruction {} having default destination",
338-
self.dfg.display_inst(inst)
339-
),
340-
}
313+
if *default_dest == old_dest {
314+
*default_dest = new_dest;
341315
}
342316
}
343317

cranelift/codegen/src/machinst/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
943943
let (inst, succ) = self.vcode.block_order().succ_indices(block)[succ_idx];
944944

945945
// Get branch args and convert to Regs.
946-
let branch_args = match self.f.dfg.insts[inst] {
946+
let branch_args = match &self.f.dfg.insts[inst] {
947947
InstructionData::Jump {
948948
destination: block, ..
949949
} => block.args_slice(&self.f.dfg.value_lists),

cranelift/codegen/src/verifier/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ impl<'a> Verifier<'a> {
12931293
inst: Inst,
12941294
errors: &mut VerifierErrors,
12951295
) -> VerifierStepResult<()> {
1296-
match self.func.dfg.insts[inst] {
1296+
match &self.func.dfg.insts[inst] {
12971297
ir::InstructionData::Jump {
12981298
destination: block, ..
12991299
} => {
@@ -1333,7 +1333,7 @@ impl<'a> Verifier<'a> {
13331333
destination: block,
13341334
..
13351335
} => {
1336-
let arg_count = self.func.dfg.num_block_params(block);
1336+
let arg_count = self.func.dfg.num_block_params(*block);
13371337
if arg_count != 0 {
13381338
return errors.nonfatal((
13391339
inst,
@@ -1344,7 +1344,7 @@ impl<'a> Verifier<'a> {
13441344
),
13451345
));
13461346
}
1347-
for block in self.func.jump_tables[table].iter() {
1347+
for block in self.func.jump_tables[*table].iter() {
13481348
let arg_count = self.func.dfg.num_block_params(*block);
13491349
if arg_count != 0 {
13501350
return errors.nonfatal((

cranelift/frontend/src/frontend.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'short, 'long> InstBuilderBase<'short> for FuncInstBuilder<'short, 'long> {
106106
self.builder.func.set_srcloc(inst, self.builder.srcloc);
107107
}
108108

109-
match self.builder.func.dfg.insts[inst] {
109+
match &self.builder.func.dfg.insts[inst] {
110110
ir::InstructionData::Jump {
111111
destination: dest, ..
112112
} => {
@@ -140,7 +140,7 @@ impl<'short, 'long> InstBuilderBase<'short> for FuncInstBuilder<'short, 'long> {
140140
.builder
141141
.func
142142
.jump_tables
143-
.get(table)
143+
.get(*table)
144144
.expect("you are referencing an undeclared jump table")
145145
.iter()
146146
.filter(|&dest_block| unique.insert(*dest_block))
@@ -152,7 +152,7 @@ impl<'short, 'long> InstBuilderBase<'short> for FuncInstBuilder<'short, 'long> {
152152
.ssa
153153
.declare_block_predecessor(*dest_block, inst);
154154
}
155-
self.builder.declare_successor(destination, inst);
155+
self.builder.declare_successor(*destination, inst);
156156
}
157157

158158
_ => {}

cranelift/frontend/src/ssa.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ impl SSABuilder {
577577
dest_block: Block,
578578
val: Value,
579579
) -> Option<(Block, Inst)> {
580-
match func.dfg.insts[branch] {
580+
match &func.dfg.insts[branch] {
581581
// For a single destination appending a jump argument to the instruction
582582
// is sufficient.
583583
InstructionData::Jump { .. } => {

0 commit comments

Comments
 (0)