From d64fa7e1f9b157fa5aaf9af4c641dd5b4b7cdb23 Mon Sep 17 00:00:00 2001 From: mlokr Date: Tue, 10 Sep 2024 12:13:01 +0200 Subject: [PATCH] more oportunities to reduce register copies --- hblang/src/codegen.rs | 6 +- ...ts_comptime_function_from_another_file.txt | 2 +- .../tests/codegen_tests_different_types.txt | 30 ++++----- hblang/tests/codegen_tests_generic_types.txt | 62 +++++++++---------- .../tests/codegen_tests_global_variables.txt | 2 +- hblang/tests/codegen_tests_if_statements.txt | 5 +- hblang/tests/codegen_tests_inline.txt | 2 +- hblang/tests/codegen_tests_inline_test.txt | 32 +++++----- ...codegen_tests_integer_inference_issues.txt | 5 +- hblang/tests/codegen_tests_loops.txt | 5 +- hblang/tests/codegen_tests_pointers.txt | 2 +- hblang/tests/codegen_tests_request_page.txt | 5 +- .../tests/codegen_tests_struct_patterns.txt | 11 ++-- 13 files changed, 80 insertions(+), 89 deletions(-) diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index 39bedf5..1c10ef9 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -2209,6 +2209,8 @@ impl Codegen { }, left.loc, ) + } else if left.loc.is_ref() { + (self.loc_to_reg(&left.loc, lsize), dbg!(self.ci.regs.allocate()), left.loc) } else { let lhs = self.loc_to_reg(left.loc, lsize); (lhs.as_ref(), lhs, Loc::default()) @@ -2295,7 +2297,7 @@ impl Codegen { self.output.emit(op_fn(dst.get(), lhs.get(), rhs.get())); self.output.emit(cmpui(dst.get(), dst.get(), against)); if matches!(op, T::Eq | T::Lt | T::Gt) { - self.output.emit(not(lhs.get(), lhs.get())); + self.output.emit(not(dst.get(), dst.get())); } self.ci.regs.free(lhs); @@ -2690,7 +2692,7 @@ impl Codegen { } def_op!(basic_op | a, b, c | a, b, c as _); - def_op!(sub_op | a, b, c | b, a, c.wrapping_neg() as _); + def_op!(sub_op | a, b, c | a, b, c.wrapping_neg() as _); let ops = match op { T::Add => basic_op!(addi8, addi16, addi32, addi64), diff --git a/hblang/tests/codegen_tests_comptime_function_from_another_file.txt b/hblang/tests/codegen_tests_comptime_function_from_another_file.txt index 39a050a..d91d03f 100644 --- a/hblang/tests/codegen_tests_comptime_function_from_another_file.txt +++ b/hblang/tests/codegen_tests_comptime_function_from_another_file.txt @@ -6,6 +6,6 @@ main: LD r31, r254, 0a, 16h ADDI64 r254, r254, 16d JALA r0, r31, 0a -code size: 239 +code size: 236 ret: 50 status: Ok(()) diff --git a/hblang/tests/codegen_tests_different_types.txt b/hblang/tests/codegen_tests_different_types.txt index 3bed30b..fe6b46b 100644 --- a/hblang/tests/codegen_tests_different_types.txt +++ b/hblang/tests/codegen_tests_different_types.txt @@ -34,21 +34,21 @@ main: JMP :1 2: CP r33, r0 LD r33, r254, 4a, 4h - CP r32, r0 - LD r32, r254, 8a, 4h - ADD32 r33, r33, r32 - CP r32, r0 - LD r32, r254, 0a, 1h - ADD32 r33, r33, r32 - CP r32, r0 - LD r32, r254, 1a, 1h - ADD32 r33, r33, r32 - CP r32, r0 - LD r32, r254, 2a, 1h - ADD32 r33, r33, r32 - CP r32, r0 - LD r32, r254, 3a, 1h - ADD32 r1, r33, r32 + CP r34, r0 + LD r34, r254, 8a, 4h + ADD32 r32, r33, r34 + CP r34, r0 + LD r34, r254, 0a, 1h + ADD32 r32, r32, r34 + CP r34, r0 + LD r34, r254, 1a, 1h + ADD32 r32, r32, r34 + CP r34, r0 + LD r34, r254, 2a, 1h + ADD32 r32, r32, r34 + CP r34, r0 + LD r34, r254, 3a, 1h + ADD32 r1, r32, r34 1: LD r31, r254, 12a, 32h ADDI64 r254, r254, 44d JALA r0, r31, 0a diff --git a/hblang/tests/codegen_tests_generic_types.txt b/hblang/tests/codegen_tests_generic_types.txt index d0aafa4..0470233 100644 --- a/hblang/tests/codegen_tests_generic_types.txt +++ b/hblang/tests/codegen_tests_generic_types.txt @@ -18,19 +18,19 @@ main: ADDI64 r254, r254, 48d JALA r0, r31, 0a deinit: - ADDI64 r254, r254, -24d - ST r31, r254, 0a, 24h + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h CP r32, r2 LD r2, r32, 0a, 8h LD r33, r32, 16a, 8h - MULI64 r33, r33, 8d - CP r3, r33 + MULI64 r34, r33, 8d + CP r3, r34 LI64 r4, 8d JAL r31, r0, :free CP r1, r32 JAL r31, r0, :new - LD r31, r254, 0a, 24h - ADDI64 r254, r254, 24d + LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d JALA r0, r31, 0a free: ADDI64 r254, r254, -40d @@ -62,10 +62,10 @@ push: ST r34, r32, 16a, 8h JMP :2 1: LD r34, r32, 16a, 8h - MULI64 r34, r34, 2d - ST r34, r32, 16a, 8h - 2: LD r34, r32, 16a, 8h - MULI64 r34, r34, 8d + MULI64 r35, r34, 2d + ST r35, r32, 16a, 8h + 2: LD r35, r32, 16a, 8h + MULI64 r34, r35, 8d CP r2, r34 LI64 r3, 8d JAL r31, r0, :malloc @@ -77,37 +77,37 @@ push: 3: LD r35, r32, 0a, 8h CP r36, r34 LD r37, r32, 0a, 8h - LD r38, r32, 8a, 8h - MULI64 r38, r38, 8d - ADD64 r37, r37, r38 - 7: JNE r35, r37, :5 + LD r39, r32, 8a, 8h + MULI64 r39, r39, 8d + ADD64 r38, r37, r39 + 7: JNE r35, r38, :5 JMP :6 - 5: CP r38, r36 - CP r39, r35 - BMC r39, r38, 8h + 5: CP r39, r36 + CP r37, r35 + BMC r37, r39, 8h ADDI64 r35, r35, 8d ADDI64 r36, r36, 8d JMP :7 - 6: LD r38, r32, 8a, 8h - LI64 r39, 0d - JEQ r38, r39, :8 + 6: LD r39, r32, 8a, 8h + LI64 r37, 0d + JEQ r39, r37, :8 LD r2, r32, 0a, 8h - LD r39, r32, 8a, 8h - MULI64 r39, r39, 8d + LD r37, r32, 8a, 8h + MULI64 r39, r37, 8d CP r3, r39 LI64 r4, 8d JAL r31, r0, :free 8: ST r34, r32, 0a, 8h 0: LD r34, r32, 0a, 8h - LD r39, r32, 8a, 8h - MULI64 r39, r39, 8d - ADD64 r34, r34, r39 - CP r39, r34 - ST r33, r39, 0a, 8h - LD r39, r32, 8a, 8h - ADDI64 r39, r39, 1d - ST r39, r32, 8a, 8h - CP r1, r34 + LD r37, r32, 8a, 8h + MULI64 r37, r37, 8d + ADD64 r39, r34, r37 + CP r37, r39 + ST r33, r37, 0a, 8h + LD r37, r32, 8a, 8h + ADDI64 r37, r37, 1d + ST r37, r32, 8a, 8h + CP r1, r39 4: LD r31, r254, 0a, 72h ADDI64 r254, r254, 72d JALA r0, r31, 0a diff --git a/hblang/tests/codegen_tests_global_variables.txt b/hblang/tests/codegen_tests_global_variables.txt index 181df45..6a48158 100644 --- a/hblang/tests/codegen_tests_global_variables.txt +++ b/hblang/tests/codegen_tests_global_variables.txt @@ -11,6 +11,6 @@ main: LD r31, r254, 0a, 32h ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 299 +code size: 296 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_if_statements.txt b/hblang/tests/codegen_tests_if_statements.txt index dcdada4..541f436 100644 --- a/hblang/tests/codegen_tests_if_statements.txt +++ b/hblang/tests/codegen_tests_if_statements.txt @@ -14,8 +14,7 @@ fib: JGTS r32, r33, :0 LI64 r1, 1d JMP :1 - 0: CP r33, r32 - ADDI64 r33, r33, -1d + 0: ADDI64 r33, r32, -1d CP r2, r33 JAL r31, r0, :fib CP r33, r1 @@ -27,6 +26,6 @@ fib: 1: LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a -code size: 231 +code size: 228 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_inline.txt b/hblang/tests/codegen_tests_inline.txt index 2cacef0..104a79e 100644 --- a/hblang/tests/codegen_tests_inline.txt +++ b/hblang/tests/codegen_tests_inline.txt @@ -4,7 +4,7 @@ main: LI64 r33, 1d ADDI64 r33, r33, 2d ADDI64 r32, r33, 3d - ADDI64 r32, r1, -6d + ADDI64 r1, r32, -6d LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a diff --git a/hblang/tests/codegen_tests_inline_test.txt b/hblang/tests/codegen_tests_inline_test.txt index 514c31c..be7fea6 100644 --- a/hblang/tests/codegen_tests_inline_test.txt +++ b/hblang/tests/codegen_tests_inline_test.txt @@ -57,11 +57,10 @@ example: CMPUI r35, r35, 0d OR r34, r34, r35 JEQ r34, r0, :0 - CP r34, r33 LI64 r35, 1024d ADDI64 r35, r35, 0d ADDI64 r35, r35, 1d - DIRS64 r0, r34, r34, r35 + DIRS64 r0, r34, r33, r35 ADDI64 r32, r34, 0d JMP :1 0: CP r32, r33 @@ -83,20 +82,17 @@ integer: LI64 r3, 4d ECA CP r34, r1 - CP r35, r32 LI64 r36, 0d - CMPS r35, r35, r36 + CMPS r35, r32, r36 CMPUI r35, r35, 0d - CP r36, r33 LI64 r37, 0d - CMPS r36, r36, r37 + CMPS r36, r33, r37 CMPUI r36, r36, 0d OR r35, r35, r36 JEQ r35, r0, :0 - CP r35, r34 SUB64 r33, r33, r32 ADDI64 r33, r33, 1d - DIRS64 r0, r35, r35, r33 + DIRS64 r0, r35, r34, r33 ADD64 r1, r35, r32 JMP :1 0: CP r1, r34 @@ -118,16 +114,16 @@ rect_line: 0: LD r34, r254, 8a, 8h LD r35, r254, 0a, 8h 4: LD r36, r254, 8a, 8h - LD r37, r254, 16a, 8h - ADD64 r36, r36, r37 - JNE r34, r36, :2 + LD r38, r254, 16a, 8h + ADD64 r37, r36, r38 + JNE r34, r37, :2 JMP :3 - 2: LI64 r36, 1d - LI64 r37, 10d - ADD64 r36, r36, r37 - LI64 r37, 1d - LI64 r38, 2d - ADD64 r36, r37, r38 + 2: LI64 r37, 1d + LI64 r38, 10d + ADD64 r37, r37, r38 + LI64 r38, 1d + LI64 r36, 2d + ADD64 r37, r38, r36 ADDI64 r34, r34, 1d JMP :4 3: ADDI64 r33, r33, 1d @@ -156,6 +152,6 @@ line: 2: LD r31, r254, 48a, 32h ADDI64 r254, r254, 80d JALA r0, r31, 0a -code size: 1416 +code size: 1404 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_integer_inference_issues.txt b/hblang/tests/codegen_tests_integer_inference_issues.txt index 3558654..48b1d3c 100644 --- a/hblang/tests/codegen_tests_integer_inference_issues.txt +++ b/hblang/tests/codegen_tests_integer_inference_issues.txt @@ -16,14 +16,13 @@ integer_range: LI64 r2, 3d LI64 r3, 4d ECA - CP r34, r1 SUB64 r33, r33, r32 ADDI64 r33, r33, 1d - DIRU64 r0, r34, r34, r33 + DIRU64 r0, r34, r1, r33 ADD64 r1, r34, r32 LD r31, r254, 0a, 32h ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 214 +code size: 211 ret: 42 status: Ok(()) diff --git a/hblang/tests/codegen_tests_loops.txt b/hblang/tests/codegen_tests_loops.txt index e82279f..58d0381 100644 --- a/hblang/tests/codegen_tests_loops.txt +++ b/hblang/tests/codegen_tests_loops.txt @@ -15,8 +15,7 @@ fib: 2: LI64 r35, 0d JNE r32, r35, :0 JMP :1 - 0: CP r35, r33 - ADD64 r35, r35, r34 + 0: ADD64 r35, r33, r34 CP r33, r34 CP r34, r35 ADDI64 r32, r32, -1d @@ -25,6 +24,6 @@ fib: LD r31, r254, 0a, 40h ADDI64 r254, r254, 40d JALA r0, r31, 0a -code size: 218 +code size: 215 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_pointers.txt b/hblang/tests/codegen_tests_pointers.txt index 082a314..8ad2d01 100644 --- a/hblang/tests/codegen_tests_pointers.txt +++ b/hblang/tests/codegen_tests_pointers.txt @@ -10,7 +10,7 @@ main: JAL r31, r0, :drop LI64 r33, 0d LD r34, r32, 0a, 8h - ADDI64 r34, r1, -2d + ADDI64 r1, r34, -2d LD r31, r254, 8a, 32h ADDI64 r254, r254, 40d JALA r0, r31, 0a diff --git a/hblang/tests/codegen_tests_request_page.txt b/hblang/tests/codegen_tests_request_page.txt index a85f4d4..c36cfb9 100644 --- a/hblang/tests/codegen_tests_request_page.txt +++ b/hblang/tests/codegen_tests_request_page.txt @@ -43,8 +43,7 @@ request_page: ST r31, r254, 0a, 32h CP r32, r2 LRA r33, r0, :"\0\u{1}xxxxxxxx" - CP r34, r33 - ADDI64 r34, r34, 1d + ADDI64 r34, r33, 1d ST r32, r34, 0a, 1h LI64 r2, 3d LI64 r3, 2d @@ -54,6 +53,6 @@ request_page: LD r31, r254, 0a, 32h ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 443 +code size: 440 ret: 42 status: Ok(()) diff --git a/hblang/tests/codegen_tests_struct_patterns.txt b/hblang/tests/codegen_tests_struct_patterns.txt index aee57f7..da677f2 100644 --- a/hblang/tests/codegen_tests_struct_patterns.txt +++ b/hblang/tests/codegen_tests_struct_patterns.txt @@ -28,8 +28,7 @@ fib_iter: 2: LI64 r35, 0d JNE r32, r35, :0 JMP :1 - 0: CP r35, r33 - ADD64 r35, r35, r34 + 0: ADD64 r35, r33, r34 CP r33, r34 CP r34, r35 ADDI64 r32, r32, -1d @@ -44,13 +43,11 @@ fib: CP r32, r2 LI64 r33, 2d JLTS r32, r33, :0 - CP r33, r32 - ADDI64 r33, r33, -1d + ADDI64 r33, r32, -1d CP r2, r33 JAL r31, r0, :fib CP r33, r1 - CP r34, r32 - ADDI64 r34, r34, -2d + ADDI64 r34, r32, -2d CP r2, r34 JAL r31, r0, :fib CP r34, r1 @@ -60,6 +57,6 @@ fib: 1: LD r31, r254, 0a, 32h ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 464 +code size: 455 ret: 0 status: Ok(())