Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions third_party/move/mono-move/core/src/instruction/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ impl HasCfgInfo for MicroOp {
| MicroOp::JumpNotZeroU64 { target, .. }
| MicroOp::JumpGreaterEqualU64Imm { target, .. }
| MicroOp::JumpLessU64Imm { target, .. }
| MicroOp::JumpGreaterU64Imm { target, .. }
| MicroOp::JumpLessEqualU64Imm { target, .. }
| MicroOp::JumpLessU64 { target, .. }
| MicroOp::JumpGreaterEqualU64 { target, .. }
| MicroOp::JumpNotEqualU64 { target, .. } => Some(target.0 as usize),
Expand Down Expand Up @@ -101,6 +103,16 @@ impl RemapTargets for MicroOp {
src,
imm,
},
MicroOp::JumpGreaterU64Imm { target, src, imm } => MicroOp::JumpGreaterU64Imm {
target: co(target),
src,
imm,
},
MicroOp::JumpLessEqualU64Imm { target, src, imm } => MicroOp::JumpLessEqualU64Imm {
target: co(target),
src,
imm,
},
MicroOp::JumpGreaterEqualU64 { target, lhs, rhs } => MicroOp::JumpGreaterEqualU64 {
target: co(target),
lhs,
Expand Down Expand Up @@ -181,6 +193,8 @@ impl GasSchedule<MicroOp> for MicroOpGasSchedule {
MicroOp::JumpNotZeroU64 { .. }
| MicroOp::JumpGreaterEqualU64Imm { .. }
| MicroOp::JumpLessU64Imm { .. }
| MicroOp::JumpGreaterU64Imm { .. }
| MicroOp::JumpLessEqualU64Imm { .. }
| MicroOp::JumpLessU64 { .. }
| MicroOp::JumpGreaterEqualU64 { .. }
| MicroOp::JumpNotEqualU64 { .. } => 3,
Expand Down
24 changes: 24 additions & 0 deletions third_party/move/mono-move/core/src/instruction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,20 @@ pub enum MicroOp {
imm: u64,
},

/// Jump to `target` if the u64 at `src` is **>** `imm`.
JumpGreaterU64Imm {
target: CodeOffset,
src: FrameOffset,
imm: u64,
},

/// Jump to `target` if the u64 at `src` is **<=** `imm`.
JumpLessEqualU64Imm {
target: CodeOffset,
src: FrameOffset,
imm: u64,
},

/// Jump to `target` if u64 at `lhs` < u64 at `rhs`.
JumpLessU64 {
target: CodeOffset,
Expand Down Expand Up @@ -633,6 +647,16 @@ impl fmt::Display for MicroOp {
target.0, src.0, imm
)
},
MicroOp::JumpGreaterU64Imm { target, src, imm } => {
write!(f, "JumpGreaterU64Imm @{} [{}] > #{}", target.0, src.0, imm)
},
MicroOp::JumpLessEqualU64Imm { target, src, imm } => {
write!(
f,
"JumpLessEqualU64Imm @{} [{}] <= #{}",
target.0, src.0, imm
)
},
MicroOp::JumpLessU64 { target, lhs, rhs } => {
write!(f, "JumpLessU64 @{} [{}] < [{}]", target.0, lhs.0, rhs.0)
},
Expand Down
18 changes: 18 additions & 0 deletions third_party/move/mono-move/runtime/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,24 @@ impl<G: GasMeter> InterpreterContext<'_, G> {
return Ok(StepResult::Continue);
},

MicroOp::JumpGreaterU64Imm { target, src, imm } => {
self.pc = if read_u64(fp, src) > imm {
target.into()
} else {
self.pc + 1
};
return Ok(StepResult::Continue);
},

MicroOp::JumpLessEqualU64Imm { target, src, imm } => {
self.pc = if read_u64(fp, src) <= imm {
target.into()
} else {
self.pc + 1
};
return Ok(StepResult::Continue);
},

MicroOp::JumpLessU64 { target, lhs, rhs } => {
self.pc = if read_u64(fp, lhs) < read_u64(fp, rhs) {
target.into()
Expand Down
18 changes: 18 additions & 0 deletions third_party/move/mono-move/runtime/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,24 @@ impl FunctionVerifier<'_> {
self.check_jump(pc, target);
},

MicroOp::JumpGreaterU64Imm {
target,
src,
imm: _,
} => {
self.check_frame_access_8(pc, src);
self.check_jump(pc, target);
},

MicroOp::JumpLessEqualU64Imm {
target,
src,
imm: _,
} => {
self.check_frame_access_8(pc, src);
self.check_jump(pc, target);
},

MicroOp::JumpLessU64 { target, lhs, rhs }
| MicroOp::JumpGreaterEqualU64 { target, lhs, rhs }
| MicroOp::JumpNotEqualU64 { target, lhs, rhs } => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ pub(crate) fn extract_imm_value(instr: &Instr) -> Option<(Slot, ImmValue)> {
| Instr::Branch(_)
| Instr::BrTrue(_, _)
| Instr::BrFalse(_, _)
| Instr::BrCmp(_, _, _, _)
| Instr::BrCmpImm(_, _, _, _)
| Instr::Ret(_)
| Instr::Abort(_)
| Instr::AbortMsg(_, _) => None,
Expand All @@ -189,15 +191,16 @@ pub(crate) fn extract_imm_value(instr: &Instr) -> Option<(Slot, ImmValue)> {
/// without changing the result).
#[inline]
pub(crate) fn is_commutative(op: &BinaryOp) -> bool {
use crate::stackless_exec_ir::CmpOp;
matches!(
op,
BinaryOp::Add
| BinaryOp::Mul
| BinaryOp::BitOr
| BinaryOp::BitAnd
| BinaryOp::Xor
| BinaryOp::Eq
| BinaryOp::Neq
| BinaryOp::Cmp(CmpOp::Eq)
| BinaryOp::Cmp(CmpOp::Neq)
| BinaryOp::Or
| BinaryOp::And
)
Expand Down Expand Up @@ -391,6 +394,11 @@ fn visit_slots<const DEFS: bool, const USES: bool>(

Instr::Branch(_) => {},
Instr::BrTrue(_, cond) | Instr::BrFalse(_, cond) => used::<USES>(*cond, &mut f),
Instr::BrCmp(_, _, lhs, rhs) => {
used::<USES>(*lhs, &mut f);
used::<USES>(*rhs, &mut f);
},
Instr::BrCmpImm(_, _, src, _) => used::<USES>(*src, &mut f),
Instr::Ret(rets) => uses::<USES>(rets, &mut f),
Instr::Abort(code) => used::<USES>(*code, &mut f),
Instr::AbortMsg(code, msg) => {
Expand Down Expand Up @@ -662,6 +670,17 @@ fn rewrite_instr_slots<const DEFS: bool, const USES: bool, const SKIP_BORROW_LOC
rewrite_slot(cond, &mut f);
}
},
Instr::BrCmp(_, _, lhs, rhs) => {
if USES {
rewrite_slot(lhs, &mut f);
rewrite_slot(rhs, &mut f);
}
},
Instr::BrCmpImm(_, _, src, _) => {
if USES {
rewrite_slot(src, &mut f);
}
},
Instr::Ret(rets) => {
if USES {
rewrite_slots(rets, &mut f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{
ssa_function::SSAFunction,
type_conversion::{convert_sig_token, convert_sig_tokens},
};
use crate::stackless_exec_ir::{BasicBlock, BinaryOp, Instr, Label, Slot, UnaryOp};
use crate::stackless_exec_ir::{BasicBlock, BinaryOp, CmpOp, Instr, Label, Slot, UnaryOp};
use anyhow::{bail, ensure, Context, Result};
use move_binary_format::{
access::ModuleAccess,
Expand Down Expand Up @@ -441,12 +441,12 @@ impl<'a> SsaConverter<'a> {
B::Shl => self.convert_binop(BinaryOp::Shl, false)?,
B::Shr => self.convert_binop(BinaryOp::Shr, false)?,
// --- Comparisons / logical (result type = bool) ---
B::Lt => self.convert_binop(BinaryOp::Lt, true)?,
B::Gt => self.convert_binop(BinaryOp::Gt, true)?,
B::Le => self.convert_binop(BinaryOp::Le, true)?,
B::Ge => self.convert_binop(BinaryOp::Ge, true)?,
B::Eq => self.convert_binop(BinaryOp::Eq, true)?,
B::Neq => self.convert_binop(BinaryOp::Neq, true)?,
B::Lt => self.convert_binop(BinaryOp::Cmp(CmpOp::Lt), true)?,
B::Gt => self.convert_binop(BinaryOp::Cmp(CmpOp::Gt), true)?,
B::Le => self.convert_binop(BinaryOp::Cmp(CmpOp::Le), true)?,
B::Ge => self.convert_binop(BinaryOp::Cmp(CmpOp::Ge), true)?,
B::Eq => self.convert_binop(BinaryOp::Cmp(CmpOp::Eq), true)?,
B::Neq => self.convert_binop(BinaryOp::Cmp(CmpOp::Neq), true)?,
B::Or => self.convert_binop(BinaryOp::Or, true)?,
B::And => self.convert_binop(BinaryOp::And, true)?,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! once within its block and never crosses a block boundary.

use super::instr_utils::{extract_imm_value, is_commutative};
use crate::stackless_exec_ir::{BasicBlock, Instr};
use crate::stackless_exec_ir::{BasicBlock, BinaryOp, Instr};
use move_vm_types::loaded_data::runtime_types::Type;

/// Intermediate SSA representation of a single function, before slot allocation.
Expand All @@ -31,6 +31,9 @@ impl SSAFunction {
for block in &mut self.blocks {
fuse_pairs(&mut block.instrs, try_fuse_field_access);
fuse_pairs(&mut block.instrs, try_fuse_immediate_binop);
// Must run after try_fuse_immediate_binop so that BinaryOpImm is
// available for the BrCmpImm variant.
fuse_pairs(&mut block.instrs, try_fuse_compare_branch);
}
self
}
Expand Down Expand Up @@ -112,15 +115,48 @@ fn try_fuse_field_access(first: &Instr, second: &Instr) -> Option<Instr> {
}
}

/// Try to fuse a comparison + conditional branch pair into a single `BrCmp`/`BrCmpImm`.
///
/// Handles both `BrTrue` (keeps the comparison operator) and `BrFalse` (negates it).
fn try_fuse_compare_branch(first: &Instr, second: &Instr) -> Option<Instr> {
match (first, second) {
// BinaryOp(dst, Cmp(cmp), lhs, rhs) + BrTrue(label, dst)
(Instr::BinaryOp(dst, BinaryOp::Cmp(cmp), lhs, rhs), Instr::BrTrue(label, cond))
if *dst == *cond =>
{
Some(Instr::BrCmp(*label, *cmp, *lhs, *rhs))
},
// BinaryOp(dst, Cmp(cmp), lhs, rhs) + BrFalse(label, dst)
(Instr::BinaryOp(dst, BinaryOp::Cmp(cmp), lhs, rhs), Instr::BrFalse(label, cond))
if *dst == *cond =>
{
Some(Instr::BrCmp(*label, cmp.negate(), *lhs, *rhs))
},
// BinaryOpImm(dst, Cmp(cmp), src, imm) + BrTrue(label, dst)
(Instr::BinaryOpImm(dst, BinaryOp::Cmp(cmp), src, imm), Instr::BrTrue(label, cond))
if *dst == *cond =>
{
Some(Instr::BrCmpImm(*label, *cmp, *src, *imm))
},
// BinaryOpImm(dst, Cmp(cmp), src, imm) + BrFalse(label, dst)
(Instr::BinaryOpImm(dst, BinaryOp::Cmp(cmp), src, imm), Instr::BrFalse(label, cond))
if *dst == *cond =>
{
Some(Instr::BrCmpImm(*label, cmp.negate(), *src, *imm))
},
_ => None,
}
}

/// Try to fuse a `Ld*` + `BinaryOp` pair into a `BinaryOpImm` instruction.
fn try_fuse_immediate_binop(first: &Instr, second: &Instr) -> Option<Instr> {
let (tmp, imm) = extract_imm_value(first)?;
match second {
Instr::BinaryOp(dst, op, lhs, rhs) if *rhs == tmp => {
Some(Instr::BinaryOpImm(*dst, op.clone(), *lhs, imm))
Some(Instr::BinaryOpImm(*dst, *op, *lhs, imm))
},
Instr::BinaryOp(dst, op, lhs, rhs) if *lhs == tmp && is_commutative(op) => {
Some(Instr::BinaryOpImm(*dst, op.clone(), *rhs, imm))
Some(Instr::BinaryOpImm(*dst, *op, *rhs, imm))
},
_ => None,
}
Expand Down
Loading
Loading