Skip to content

Commit 921acf0

Browse files
authored
Merge 474013d into af3db4b
2 parents af3db4b + 474013d commit 921acf0

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ struct PerFunctionContext<'f> {
117117
/// Track a value's last load across all blocks.
118118
/// If a value is not used in anymore loads we can remove the last store to that value.
119119
last_loads: HashMap<ValueId, (InstructionId, BasicBlockId)>,
120+
121+
/// Track whether the last instruction is an inc_rc/dec_rc instruction.
122+
/// If it is we should not remove any known store values.
123+
inside_rc_reload: bool,
120124
}
121125

122126
impl<'f> PerFunctionContext<'f> {
@@ -131,6 +135,7 @@ impl<'f> PerFunctionContext<'f> {
131135
blocks: BTreeMap::new(),
132136
instructions_to_remove: BTreeSet::new(),
133137
last_loads: HashMap::default(),
138+
inside_rc_reload: false,
134139
}
135140
}
136141

@@ -281,6 +286,10 @@ impl<'f> PerFunctionContext<'f> {
281286
} else {
282287
references.mark_value_used(address, self.inserter.function);
283288

289+
references.expressions.insert(result, Expression::Other(result));
290+
references.aliases.insert(Expression::Other(result), AliasSet::known(result));
291+
references.set_known_value(result, address);
292+
284293
self.last_loads.insert(address, (instruction, block_id));
285294
}
286295
}
@@ -296,6 +305,14 @@ impl<'f> PerFunctionContext<'f> {
296305
self.instructions_to_remove.insert(*last_store);
297306
}
298307

308+
let known_value = references.get_known_value(value);
309+
if let Some(known_value) = known_value {
310+
let known_value_is_address = known_value == address;
311+
if known_value_is_address && !self.inside_rc_reload {
312+
self.instructions_to_remove.insert(instruction);
313+
}
314+
}
315+
299316
references.set_known_value(address, value);
300317
references.last_stores.insert(address, instruction);
301318
}
@@ -350,6 +367,18 @@ impl<'f> PerFunctionContext<'f> {
350367
Instruction::Call { arguments, .. } => self.mark_all_unknown(arguments, references),
351368
_ => (),
352369
}
370+
371+
self.track_rc_reload_state(instruction);
372+
}
373+
374+
fn track_rc_reload_state(&mut self, instruction: InstructionId) {
375+
match &self.inserter.function.dfg[instruction] {
376+
// We just had an increment or decrement to an array's reference counter
377+
Instruction::IncrementRc { .. } | Instruction::DecrementRc { .. } => {
378+
self.inside_rc_reload = true;
379+
}
380+
_ => self.inside_rc_reload = false,
381+
}
353382
}
354383

355384
fn check_array_aliasing(&self, references: &mut Block, array: ValueId) {

0 commit comments

Comments
 (0)