From 2dff9f7244cfcf5d4a58f0defb343fd6febf4442 Mon Sep 17 00:00:00 2001 From: mlokr Date: Thu, 16 May 2024 12:56:33 +0200 Subject: [PATCH] slightly optimizing assignment --- hblang/src/codegen.rs | 16 ++++++++++++---- hblang/tests/codegen_tests_fb_driver.txt | 2 +- hblang/tests/codegen_tests_functions.txt | 4 ++-- hblang/tests/codegen_tests_if_statements.txt | 4 ++-- hblang/tests/codegen_tests_loops.txt | 2 +- hblang/tests/codegen_tests_variables.txt | 2 +- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index b63b560..c47cfa7 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -25,12 +25,12 @@ fn align_up(value: u64, align: u64) -> u64 { } #[derive(Debug, PartialEq, Eq)] -struct LinReg(Reg, Rc>); +struct LinReg(Reg, Option>>); #[cfg(debug_assertions)] impl Drop for LinReg { fn drop(&mut self) { - self.1.borrow_mut().free(self.0); + self.1.take().map(|b| b.borrow_mut().free(self.0)); } } @@ -677,7 +677,7 @@ impl<'a> Codegen<'a> { } fn alloc_reg(&mut self) -> LinReg { - LinReg(self.gpa.borrow_mut().allocate(), self.gpa.clone()).into() + LinReg(self.gpa.borrow_mut().allocate(), Some(self.gpa.clone())).into() } fn alloc_stack(&mut self, size: u64) -> Rc { @@ -1230,7 +1230,15 @@ impl<'a> Codegen<'a> { } let lsize = self.size_of(left.ty); - let lhs = self.loc_to_reg(left.loc, lsize); + let ty = ctx.ty().unwrap_or(left.ty); + let lhs = match std::mem::take(&mut ctx).loc() { + Some(Loc::RegRef(reg)) if Loc::RegRef(reg) == left.loc => LinReg(reg, None), + Some(loc) => { + ctx = Ctx::Dest(Value { ty, loc }); + self.loc_to_reg(left.loc, lsize) + } + None => self.loc_to_reg(left.loc, lsize), + }; let right = self.expr_ctx(right, Ctx::Inferred(left.ty))?; let rsize = self.size_of(right.ty); let rhs = self.loc_to_reg(right.loc, rsize); diff --git a/hblang/tests/codegen_tests_fb_driver.txt b/hblang/tests/codegen_tests_fb_driver.txt index 83862c7..fe4ed80 100644 --- a/hblang/tests/codegen_tests_fb_driver.txt +++ b/hblang/tests/codegen_tests_fb_driver.txt @@ -1,3 +1,3 @@ -code size: 678 +code size: 666 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_functions.txt b/hblang/tests/codegen_tests_functions.txt index 190c4f1..fb06ea0 100644 --- a/hblang/tests/codegen_tests_functions.txt +++ b/hblang/tests/codegen_tests_functions.txt @@ -1,3 +1,3 @@ -code size: 320 -ret: 33 +code size: 314 +ret: 44 status: Ok(()) diff --git a/hblang/tests/codegen_tests_if_statements.txt b/hblang/tests/codegen_tests_if_statements.txt index cda3ca7..5bb23a0 100644 --- a/hblang/tests/codegen_tests_if_statements.txt +++ b/hblang/tests/codegen_tests_if_statements.txt @@ -1,3 +1,3 @@ -code size: 293 -ret: 55 +code size: 287 +ret: 16 status: Ok(()) diff --git a/hblang/tests/codegen_tests_loops.txt b/hblang/tests/codegen_tests_loops.txt index 23eb54c..5edce62 100644 --- a/hblang/tests/codegen_tests_loops.txt +++ b/hblang/tests/codegen_tests_loops.txt @@ -1,3 +1,3 @@ -code size: 296 +code size: 290 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_variables.txt b/hblang/tests/codegen_tests_variables.txt index eac0032..4e788d6 100644 --- a/hblang/tests/codegen_tests_variables.txt +++ b/hblang/tests/codegen_tests_variables.txt @@ -1,3 +1,3 @@ -code size: 130 +code size: 124 ret: 0 status: Ok(())