MCPcopy Create free account
hub / github.com/dylan-sutton-chavez/edge-python / try_fold_neg

Function try_fold_neg

compiler/src/modules/vm/optimizer.rs:242–262  ·  view source on GitHub ↗
(chunk: &mut SSAChunk, dead: &mut [bool], ip: usize)

Source from the content-addressed store, hash-verified

240}
241
242fn try_fold_neg(chunk: &mut SSAChunk, dead: &mut [bool], ip: usize) {
243 let Some(prev1_ip) = prev_live(dead, ip) else { return };
244 let p1 = chunk.instructions[prev1_ip];
245 if p1.opcode != OpCode::LoadConst { return; }
246 let Some(v) = const_to_val(&chunk.constants, p1.operand) else { return };
247
248 let folded = if v.is_int() {
249 let r = -(v.as_int() as i128);
250 if (Val::INT_MIN as i128..=Val::INT_MAX as i128).contains(&r) {
251 Some(Val::int(r as i64))
252 } else { None }
253 } else if v.is_float() {
254 Some(Val::float(-v.as_float()))
255 } else { None };
256
257 if let Some(r) = folded
258 && write_const_load(chunk, prev1_ip, r)
259 {
260 dead[ip] = true;
261 }
262}
263
264fn fold_binop(op: OpCode, a: Val, b: Val) -> Option<Val> {
265 if matches!(op, OpCode::Eq | OpCode::NotEq | OpCode::Lt | OpCode::Gt | OpCode::LtEq | OpCode::GtEq) {

Callers 1

constant_foldFunction · 0.85

Calls 8

prev_liveFunction · 0.85
const_to_valFunction · 0.85
write_const_loadFunction · 0.85
is_intMethod · 0.80
as_intMethod · 0.80
is_floatMethod · 0.80
as_floatMethod · 0.80
containsMethod · 0.45

Tested by

no test coverage detected