From e8a8fa3eb18a5b728f12b00aa8e483a4c1d7daa8 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Sun, 27 Oct 2024 18:21:33 +0100 Subject: [PATCH] simplifiing upcasts, hwich conincidentally allowed more optimizations --- lang/src/son.rs | 38 ++++++++++++-------------------------- lang/src/son/hbvm.rs | 10 ---------- 2 files changed, 12 insertions(+), 36 deletions(-) diff --git a/lang/src/son.rs b/lang/src/son.rs index f073dac..783d359 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -567,11 +567,6 @@ impl Nodes { return Some(self[self[target].inputs[2]].inputs[1]); } } - K::Extend => { - if self[target].ty.simple_size() == self[self[target].inputs[1]].ty.simple_size() { - return Some(self[target].inputs[1]); - } - } K::Loop => { if self[target].inputs[0] == NEVER { return Some(NEVER); @@ -684,7 +679,6 @@ impl Nodes { Kind::Stre => write!(out, "stre: "), Kind::Mem => write!(out, " mem: "), Kind::Loops => write!(out, " loops: "), - Kind::Extend => write!(out, " ext: "), }?; if self[node].kind != Kind::Loop && self[node].kind != Kind::Region { @@ -1042,8 +1036,6 @@ pub enum Kind { Phi, Arg, // [ctrl, oper] - Extend, - // [ctrl, oper] UnOp { op: lexer::TokenKind, }, @@ -2151,22 +2143,11 @@ impl<'a> Codegen<'a> { ); } - match self.tys.size_of(val.ty).cmp(&self.tys.size_of(ty)) { - core::cmp::Ordering::Less => { - self.extend(&mut val, ty); - Some(val) - } - core::cmp::Ordering::Equal => Some(val.ty(ty)), - core::cmp::Ordering::Greater => { - let value = (1i64 << (self.tys.size_of(ty) * 8)) - 1; - let mask = self.ci.nodes.new_node_nop(val.ty, Kind::CInt { value }, [VOID]); - let inps = [VOID, val.id, mask]; - Some(self.ci.nodes.new_node_lit( - ty, - Kind::BinOp { op: TokenKind::Band }, - inps, - )) - } + if self.tys.size_of(val.ty) < self.tys.size_of(ty) { + self.extend(&mut val, ty); + Some(val) + } else { + Some(val.ty(ty)) } } Expr::Directive { name: "as", args: [ty, expr], .. } => { @@ -3164,7 +3145,9 @@ impl<'a> Codegen<'a> { }; if let Some(oper) = to_correct { - self.extend(oper, upcasted); + if self.tys.size_of(upcasted) > self.tys.size_of(oper.ty) { + self.extend(oper, upcasted); + } if matches!(op, TokenKind::Add | TokenKind::Sub) && let Some(elem) = self.tys.base_of(upcasted) { @@ -3224,8 +3207,11 @@ impl<'a> Codegen<'a> { fn extend(&mut self, value: &mut Value, to: ty::Id) { self.strip_ptr(value); + let val = (1i64 << (self.tys.size_of(value.ty) * 8)) - 1; value.ty = to; - value.id = self.ci.nodes.new_node(to, Kind::Extend, [VOID, value.id]); + let mask = self.ci.nodes.new_node_nop(to, Kind::CInt { value: val }, [VOID]); + let inps = [VOID, value.id, mask]; + *value = self.ci.nodes.new_node_lit(to, Kind::BinOp { op: TokenKind::Band }, inps); } #[track_caller] diff --git a/lang/src/son/hbvm.rs b/lang/src/son/hbvm.rs index 18d758a..79ca6f8 100644 --- a/lang/src/son/hbvm.rs +++ b/lang/src/son/hbvm.rs @@ -312,12 +312,6 @@ impl ItemCtx { 4 => instrs::li32(atr(allocs[0]), value as _), _ => instrs::li64(atr(allocs[0]), value as _), }), - Kind::Extend => { - let base = fuc.nodes[node.inputs[1]].ty; - let dest = node.ty; - - self.emit(extend(base, dest, 1, 0)) - } Kind::UnOp { op } => { let op = op.unop().expect("TODO: unary operator not supported"); let &[dst, oper] = allocs else { unreachable!() }; @@ -812,10 +806,6 @@ impl<'a> Function<'a> { let ops = vec![self.drg(nid)]; self.add_instr(nid, ops); } - Kind::Extend => { - let ops = vec![self.drg(nid), self.urg(node.inputs[1])]; - self.add_instr(nid, ops); - } Kind::Entry => { self.nodes[nid].ralloc_backref = self.add_block(nid);