diff --git a/lang/src/son.rs b/lang/src/son.rs index 74bcaad..7c88d38 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -943,11 +943,51 @@ impl Nodes { return Some(self.new_node(self[lhs].ty, Kind::Stre, vc)); } } + K::Stck => { + if let &[mut a, mut b] = self[target].outputs.as_slice() { + if self[a].kind == Kind::Load { + mem::swap(&mut a, &mut b); + } + + if matches!(self[a].kind, Kind::Call { .. }) + && self[a].inputs.last() == Some(&target) + && self[b].kind == Kind::Load + && let &[store] = self[b].outputs.as_slice() + && self[store].kind == Kind::Stre + { + let len = self[a].inputs.len(); + let stre = self[store].inputs[3]; + if stre != MEM { + self[a].inputs.push(stre); + self[a].inputs.swap(len - 2, len - 1); + self[stre].outputs.push(a); + } + return Some(self[store].inputs[2]); + } + } + } K::Stre => { let &[_, value, region, store, ..] = self[target].inputs.as_slice() else { unreachable!() }; + if self[value].kind == Kind::Load && self[value].inputs[1] == region { + return Some(store); + } + + let mut cursor = target; + while self[cursor].kind == Kind::Stre + && self[cursor].inputs[1] != VOID + && let &[next_store] = self[cursor].outputs.as_slice() + { + if self[next_store].inputs[2] == region + && self[next_store].ty == self[target].ty + { + return Some(store); + } + cursor = next_store; + } + 'eliminate: { if self[target].outputs.is_empty() { break 'eliminate; diff --git a/lang/tests/son_tests_generic_types.txt b/lang/tests/son_tests_generic_types.txt index 183d7b2..ab49696 100644 --- a/lang/tests/son_tests_generic_types.txt +++ b/lang/tests/son_tests_generic_types.txt @@ -1,20 +1,17 @@ deinit: - ADDI64 r254, r254, -48d - ST r31, r254, 24a, 24h - LD r5, r2, 16a, 8h + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h CP r32, r2 + LD r5, r2, 16a, 8h LI64 r4, 8d MUL64 r3, r5, r4 CP r5, r32 LD r2, r5, 0a, 8h JAL r31, r0, :free - ADDI64 r33, r254, 0d - CP r1, r33 + CP r1, r32 JAL r31, r0, :new - CP r2, r32 - BMC r33, r2, 24h - LD r31, r254, 24a, 24h - ADDI64 r254, r254, 48d + LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d JALA r0, r31, 0a free: CP r10, r2 @@ -26,23 +23,21 @@ free: ECA JALA r0, r31, 0a main: - ADDI64 r254, r254, -80d - ST r31, r254, 48a, 32h - ADDI64 r32, r254, 24d + ADDI64 r254, r254, -48d + ST r31, r254, 24a, 24h + ADDI64 r32, r254, 0d CP r1, r32 JAL r31, r0, :new - ADDI64 r33, r254, 0d - BMC r32, r33, 24h LI64 r3, 69d - CP r2, r33 + CP r2, r32 JAL r31, r0, :push - LD r12, r254, 0a, 8h - LD r34, r12, 0a, 8h - CP r2, r33 + LD r9, r254, 0a, 8h + LD r33, r9, 0a, 8h + CP r2, r32 JAL r31, r0, :deinit - CP r1, r34 - LD r31, r254, 48a, 32h - ADDI64 r254, r254, 80d + CP r1, r33 + LD r31, r254, 24a, 24h + ADDI64 r254, r254, 48d JALA r0, r31, 0a malloc: CP r9, r2 @@ -126,6 +121,6 @@ push: 4: LD r31, r254, 0a, 72h ADDI64 r254, r254, 72d JALA r0, r31, 0a -code size: 980 +code size: 945 ret: 69 status: Ok(()) diff --git a/lang/tests/son_tests_overwrite_aliasing_overoptimization.txt b/lang/tests/son_tests_overwrite_aliasing_overoptimization.txt index 84be8f7..d807551 100644 --- a/lang/tests/son_tests_overwrite_aliasing_overoptimization.txt +++ b/lang/tests/son_tests_overwrite_aliasing_overoptimization.txt @@ -1,25 +1,22 @@ main: - ADDI64 r254, r254, -72d - ST r31, r254, 56a, 16h - ADDI64 r32, r254, 0d + ADDI64 r254, r254, -56d + ST r31, r254, 40a, 16h + LI64 r4, 4d + ADDI64 r3, r254, 24d + ADDI64 r6, r254, 0d + ST r4, r254, 24a, 8h + LI64 r32, 1d + ST r32, r254, 32a, 8h + ST r32, r254, 16a, 8h + BMC r3, r6, 16h JAL r31, r0, :opaque ST r1, r254, 0a, 16h - LI64 r6, 4d - ADDI64 r5, r254, 40d - ADDI64 r8, r254, 16d - ST r6, r254, 40a, 8h - LI64 r7, 1d - ST r7, r254, 48a, 8h - ST r7, r254, 32a, 8h - BMC r5, r8, 16h - BMC r32, r8, 16h - LD r7, r254, 24a, 8h - LD r9, r254, 32a, 8h - ADD64 r11, r9, r7 - LD r9, r254, 16a, 8h - SUB64 r1, r9, r11 - LD r31, r254, 56a, 16h - ADDI64 r254, r254, 72d + LD r4, r254, 8a, 8h + ADD64 r7, r4, r32 + LD r5, r254, 0a, 8h + SUB64 r1, r5, r7 + LD r31, r254, 40a, 16h + ADDI64 r254, r254, 56d JALA r0, r31, 0a opaque: ADDI64 r254, r254, -16d @@ -31,6 +28,6 @@ opaque: LD r1, r2, 0a, 16h ADDI64 r254, r254, 16d JALA r0, r31, 0a -code size: 339 +code size: 310 ret: 0 status: Ok(()) diff --git a/lang/tests/son_tests_returning_global_struct.txt b/lang/tests/son_tests_returning_global_struct.txt index 485cc96..6f3bca0 100644 --- a/lang/tests/son_tests_returning_global_struct.txt +++ b/lang/tests/son_tests_returning_global_struct.txt @@ -1,29 +1,27 @@ main: - ADDI64 r254, r254, -24d - ST r31, r254, 8a, 16h - ADDI64 r32, r254, 4d + ADDI64 r254, r254, -12d + ST r31, r254, 4a, 8h + ADDI64 r2, r254, 0d JAL r31, r0, :random_color - ST r1, r254, 4a, 4h - ADDI64 r5, r254, 0d - BMC r32, r5, 4h - LD r8, r254, 0a, 1h - LD r11, r254, 1a, 1h - LD r3, r254, 2a, 1h - ANDI r12, r8, 255d - ANDI r4, r11, 255d - LD r9, r254, 3a, 1h - ANDI r8, r3, 255d - ADD64 r7, r4, r12 - ANDI r1, r9, 255d - ADD64 r12, r7, r8 - ADD64 r1, r12, r1 - LD r31, r254, 8a, 16h - ADDI64 r254, r254, 24d + ST r1, r254, 0a, 4h + LD r5, r254, 0a, 1h + LD r8, r254, 1a, 1h + LD r12, r254, 2a, 1h + ANDI r9, r5, 255d + ANDI r1, r8, 255d + LD r6, r254, 3a, 1h + ANDI r5, r12, 255d + ADD64 r4, r1, r9 + ANDI r10, r6, 255d + ADD64 r9, r4, r5 + ADD64 r1, r9, r10 + LD r31, r254, 4a, 8h + ADDI64 r254, r254, 12d JALA r0, r31, 0a random_color: LRA r1, r0, :white LD r1, r1, 0a, 4h JALA r0, r31, 0a -code size: 257 +code size: 241 ret: 1020 status: Ok(()) diff --git a/lang/tests/son_tests_small_struct_assignment.txt b/lang/tests/son_tests_small_struct_assignment.txt index dc49aef..27ddda1 100644 --- a/lang/tests/son_tests_small_struct_assignment.txt +++ b/lang/tests/son_tests_small_struct_assignment.txt @@ -1,14 +1,12 @@ main: ADDI64 r254, r254, -4d - LRA r1, r0, :black + LRA r2, r0, :white ADDI64 r3, r254, 0d - LRA r5, r0, :white - BMC r1, r3, 4h - BMC r5, r3, 4h - LD r9, r254, 3a, 1h - ANDI r1, r9, 255d + BMC r2, r3, 4h + LD r6, r254, 3a, 1h + ANDI r1, r6, 255d ADDI64 r254, r254, 4d JALA r0, r31, 0a -code size: 108 +code size: 92 ret: 255 status: Ok(()) diff --git a/lang/tests/son_tests_structs.txt b/lang/tests/son_tests_structs.txt index 2c7edad..baa5202 100644 --- a/lang/tests/son_tests_structs.txt +++ b/lang/tests/son_tests_structs.txt @@ -1,24 +1,23 @@ main: - ADDI64 r254, r254, -72d - ST r31, r254, 48a, 24h + ADDI64 r254, r254, -56d + ST r31, r254, 32a, 24h LI64 r3, 4d - ADDI64 r2, r254, 32d - ST r3, r254, 32a, 8h + ADDI64 r2, r254, 16d + ST r3, r254, 16a, 8h LI64 r32, 3d - ST r32, r254, 40a, 8h - ADDI64 r33, r254, 16d + ST r32, r254, 24a, 8h + ADDI64 r33, r254, 0d LD r3, r2, 0a, 16h JAL r31, r0, :odher_pass - ST r1, r254, 16a, 16h - ADDI64 r2, r254, 0d - BMC r33, r2, 16h - LD r4, r254, 8a, 8h - JNE r4, r32, :0 + ST r1, r254, 0a, 16h + LD r2, r254, 8a, 8h + JNE r2, r32, :0 + CP r2, r33 JAL r31, r0, :pass JMP :1 0: LI64 r1, 0d - 1: LD r31, r254, 48a, 24h - ADDI64 r254, r254, 72d + 1: LD r31, r254, 32a, 24h + ADDI64 r254, r254, 56d JALA r0, r31, 0a odher_pass: ADDI64 r254, r254, -16d @@ -30,6 +29,6 @@ odher_pass: pass: LD r1, r2, 0a, 8h JALA r0, r31, 0a -code size: 318 +code size: 305 ret: 4 status: Ok(()) diff --git a/lang/tests/son_tests_wide_ret.txt b/lang/tests/son_tests_wide_ret.txt index 05632f1..7950daa 100644 --- a/lang/tests/son_tests_wide_ret.txt +++ b/lang/tests/son_tests_wide_ret.txt @@ -1,19 +1,17 @@ main: - ADDI64 r254, r254, -48d - ST r31, r254, 32a, 16h - ADDI64 r32, r254, 16d + ADDI64 r254, r254, -24d + ST r31, r254, 16a, 8h + ADDI64 r3, r254, 0d LI64 r4, 0d CP r3, r4 JAL r31, r0, :maina - ST r1, r254, 16a, 16h - ADDI64 r7, r254, 0d - BMC r32, r7, 16h - LD r11, r254, 12a, 1h - LD r12, r254, 3a, 1h - SUB8 r2, r12, r11 - ANDI r1, r2, 255d - LD r31, r254, 32a, 16h - ADDI64 r254, r254, 48d + ST r1, r254, 0a, 16h + LD r8, r254, 12a, 1h + LD r9, r254, 3a, 1h + SUB8 r11, r9, r8 + ANDI r1, r11, 255d + LD r31, r254, 16a, 8h + ADDI64 r254, r254, 24d JALA r0, r31, 0a maina: ADDI64 r254, r254, -36d @@ -50,6 +48,6 @@ small_struct: LD r1, r3, 0a, 4h ADDI64 r254, r254, 4d JALA r0, r31, 0a -code size: 514 +code size: 498 ret: 2 status: Ok(())