Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid adding an unnecessary move from the body(ies) of an if/else block #894

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions sway-core/src/asm_generation/expression/if_exp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::asm_generation::{convert_expression_to_asm, AsmNamespace, RegisterSequencer};
use crate::asm_generation::{
convert_expression_to_asm, AsmNamespace,
Either::{Left, Right},
RegisterSequencer,
};
use crate::asm_lang::{ConstantRegister, Op, VirtualRegister};
use crate::error::*;

Expand Down Expand Up @@ -65,13 +69,27 @@ pub(crate) fn convert_if_exp_to_asm(
warnings,
errors
);

// has then_branch_result register been defined?
let return_reg_defined = then_branch.iter().any(|op| {
match &op.opcode {
Left(inst) => inst.def_registers().contains(&then_branch_result),
Right(_) => false, // Organizational Ops don't def registers
}
});

asm_buf.append(&mut then_branch);
// move the result of the then branch into the return register
asm_buf.push(Op::register_move(
return_register.clone(),
then_branch_result,
then.clone().span,
));

// move the result of the then branch into the return register if then_branch_result has been
// defined
if return_reg_defined {
asm_buf.push(Op::register_move(
return_register.clone(),
then_branch_result,
then.clone().span,
));
}

asm_buf.push(Op::jump_to_label_comment(
after_else_label.clone(),
"end of then branch",
Expand All @@ -89,14 +107,26 @@ pub(crate) fn convert_if_exp_to_asm(
warnings,
errors
);

// Has else_branch_result register been defined?
let return_reg_defined = else_branch.iter().any(|op| {
match &op.opcode {
Left(inst) => inst.def_registers().contains(&else_branch_result),
Right(_) => false, // Organizational Ops don't def registers
}
});

asm_buf.append(&mut else_branch);

// move the result of the else branch into the return register
asm_buf.push(Op::register_move(
return_register.clone(),
else_branch_result,
r#else.clone().span,
));
// move the result of the else branch into the return register if else_branch_result been
// defined
if return_reg_defined {
asm_buf.push(Op::register_move(
return_register.clone(),
else_branch_result,
r#else.clone().span,
));
}
}

asm_buf.push(Op::unowned_jump_label_comment(
Expand Down