More compiler optimizations
This commit is contained in:
@@ -172,4 +172,37 @@ mod tests {
|
||||
let output = compile_with_and_without_optimization(source);
|
||||
insta::assert_snapshot!(output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuples() {
|
||||
let source = indoc! {r#"
|
||||
device self = "db";
|
||||
device day = "d0";
|
||||
|
||||
fn getSomethingElse(input) {
|
||||
return input + 1;
|
||||
}
|
||||
|
||||
fn getSensorData() {
|
||||
return (
|
||||
day.Vertical,
|
||||
day.Horizontal,
|
||||
getSomethingElse(3)
|
||||
);
|
||||
}
|
||||
|
||||
loop {
|
||||
yield();
|
||||
|
||||
let (vertical, horizontal, _) = getSensorData();
|
||||
|
||||
(horizontal, _, _) = getSensorData();
|
||||
|
||||
self.Setting = horizontal;
|
||||
}
|
||||
"#};
|
||||
|
||||
let output = compile_with_and_without_optimization(source);
|
||||
insta::assert_snapshot!(output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ move r9 r1
|
||||
|
||||
## Optimized Output
|
||||
|
||||
j 1
|
||||
move r8 5
|
||||
move r1 5
|
||||
move r9 5
|
||||
|
||||
@@ -43,7 +43,6 @@ move r13 r3
|
||||
add r4 r11 r12
|
||||
add r5 r4 r13
|
||||
move r15 r5
|
||||
j 16
|
||||
pop ra
|
||||
pop sp
|
||||
j ra
|
||||
|
||||
@@ -10,5 +10,4 @@ move r8 15
|
||||
|
||||
## Optimized Output
|
||||
|
||||
j 1
|
||||
move r8 15
|
||||
|
||||
@@ -26,7 +26,6 @@ push sp
|
||||
push ra
|
||||
add r1 r8 1
|
||||
move r15 r1
|
||||
j 7
|
||||
pop ra
|
||||
pop sp
|
||||
j ra
|
||||
|
||||
@@ -38,7 +38,6 @@ push sp
|
||||
push ra
|
||||
add r1 r9 r8
|
||||
move r15 r1
|
||||
j 8
|
||||
pop ra
|
||||
pop sp
|
||||
j ra
|
||||
|
||||
@@ -19,4 +19,5 @@ j ra
|
||||
## Optimized Output
|
||||
|
||||
j main
|
||||
pop r8
|
||||
j ra
|
||||
|
||||
@@ -68,16 +68,15 @@ push sp
|
||||
push ra
|
||||
add r1 r9 r8
|
||||
move r15 r1
|
||||
j 8
|
||||
pop ra
|
||||
pop sp
|
||||
j ra
|
||||
pop r8
|
||||
pop r9
|
||||
push sp
|
||||
push ra
|
||||
add r1 r9 r9
|
||||
move r15 r1
|
||||
j 17
|
||||
pop ra
|
||||
pop sp
|
||||
j ra
|
||||
@@ -99,9 +98,11 @@ push r10
|
||||
push r10
|
||||
push 2
|
||||
jal 11
|
||||
pop r10
|
||||
pop r9
|
||||
pop r8
|
||||
move r11 r15
|
||||
move r15 r11
|
||||
j 41
|
||||
pop ra
|
||||
pop sp
|
||||
j ra
|
||||
|
||||
@@ -31,7 +31,6 @@ push sp
|
||||
push ra
|
||||
select r9 r8 10 20
|
||||
move r15 r9
|
||||
j 7
|
||||
pop ra
|
||||
pop sp
|
||||
j ra
|
||||
|
||||
@@ -25,7 +25,6 @@ push sp
|
||||
push ra
|
||||
add r1 r8 r8
|
||||
move r15 r1
|
||||
j 7
|
||||
pop ra
|
||||
pop sp
|
||||
j ra
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
---
|
||||
source: libs/integration_tests/src/lib.rs
|
||||
expression: output
|
||||
---
|
||||
## Unoptimized Output
|
||||
|
||||
j main
|
||||
getSomethingElse:
|
||||
pop r8
|
||||
push sp
|
||||
push ra
|
||||
add r1 r8 1
|
||||
move r15 r1
|
||||
j __internal_L1
|
||||
__internal_L1:
|
||||
pop ra
|
||||
pop sp
|
||||
j ra
|
||||
getSensorData:
|
||||
push sp
|
||||
push ra
|
||||
l r1 d0 Vertical
|
||||
push r1
|
||||
l r2 d0 Horizontal
|
||||
push r2
|
||||
push 3
|
||||
jal getSomethingElse
|
||||
move r3 r15
|
||||
push r3
|
||||
sub r0 sp 5
|
||||
get r0 db r0
|
||||
move r15 r0
|
||||
j __internal_L2
|
||||
__internal_L2:
|
||||
sub r0 sp 4
|
||||
get ra db r0
|
||||
j ra
|
||||
main:
|
||||
__internal_L3:
|
||||
yield
|
||||
jal getSensorData
|
||||
pop r0
|
||||
pop r9
|
||||
pop r8
|
||||
move sp r15
|
||||
jal getSensorData
|
||||
pop r0
|
||||
pop r0
|
||||
pop r9
|
||||
move sp r15
|
||||
s db Setting r9
|
||||
j __internal_L3
|
||||
__internal_L4:
|
||||
|
||||
## Optimized Output
|
||||
|
||||
j 27
|
||||
pop r8
|
||||
push sp
|
||||
push ra
|
||||
add r1 r8 1
|
||||
move r15 r1
|
||||
pop ra
|
||||
pop sp
|
||||
j ra
|
||||
push sp
|
||||
push ra
|
||||
l r1 d0 Vertical
|
||||
push r1
|
||||
l r2 d0 Horizontal
|
||||
push r2
|
||||
push 3
|
||||
jal 1
|
||||
move r3 r15
|
||||
push r3
|
||||
sub r0 sp 5
|
||||
get r0 db r0
|
||||
move r15 r0
|
||||
sub r0 sp 4
|
||||
get ra db r0
|
||||
j ra
|
||||
yield
|
||||
jal 10
|
||||
pop r0
|
||||
pop r9
|
||||
pop r8
|
||||
move sp r15
|
||||
jal 10
|
||||
pop r0
|
||||
pop r0
|
||||
pop r9
|
||||
move sp r15
|
||||
s db Setting r9
|
||||
j 27
|
||||
@@ -1,4 +1,4 @@
|
||||
use il::{Instruction, InstructionNode};
|
||||
use il::{Instruction, InstructionNode, Operand};
|
||||
|
||||
/// Pass: Redundant Move Elimination
|
||||
/// Removes moves where source and destination are the same: `move rx rx`
|
||||
@@ -43,6 +43,31 @@ pub fn remove_unreachable_code<'a>(
|
||||
(output, changed)
|
||||
}
|
||||
|
||||
/// Pass: Remove Redundant Jumps
|
||||
/// Removes jumps to the next instruction (after label resolution).
|
||||
/// Must run AFTER label resolution since it needs line numbers.
|
||||
pub fn remove_redundant_jumps<'a>(
|
||||
input: Vec<InstructionNode<'a>>,
|
||||
) -> (Vec<InstructionNode<'a>>, bool) {
|
||||
let mut output = Vec::with_capacity(input.len());
|
||||
let mut changed = false;
|
||||
|
||||
for (i, node) in input.iter().enumerate() {
|
||||
// Check if this is a jump to the next line number
|
||||
if let Instruction::Jump(Operand::Number(target)) = &node.instruction {
|
||||
// Current line number is i, next line number is i+1
|
||||
// If jump target equals the next line, it's redundant
|
||||
if target.to_string().parse::<usize>().ok() == Some(i + 1) {
|
||||
changed = true;
|
||||
continue; // Skip this redundant jump
|
||||
}
|
||||
}
|
||||
output.push(node.clone());
|
||||
}
|
||||
|
||||
(output, changed)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -13,13 +13,20 @@ pub fn dead_store_elimination<'a>(
|
||||
|
||||
// Scan for dead writes
|
||||
for (i, node) in input.iter().enumerate() {
|
||||
// Never remove Pop instructions - they have critical side effects on the stack pointer
|
||||
if matches!(node.instruction, Instruction::Pop(_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(dest_reg) = get_destination_reg(&node.instruction) {
|
||||
// If this register was written before and hasn't been read, previous write is dead
|
||||
if let Some(&prev_idx) = last_write.get(&dest_reg) {
|
||||
// Check if the value was ever used between prev_idx and current
|
||||
let was_used = input[prev_idx + 1..i]
|
||||
.iter()
|
||||
.any(|n| reg_is_read_or_affects_control(&n.instruction, dest_reg));
|
||||
.any(|n| reg_is_read_or_affects_control(&n.instruction, dest_reg))
|
||||
// Also check if current instruction reads the register before overwriting it
|
||||
|| reg_is_read_or_affects_control(&node.instruction, dest_reg);
|
||||
|
||||
if !was_used {
|
||||
// Previous write was dead
|
||||
|
||||
@@ -17,7 +17,7 @@ mod strength_reduction;
|
||||
|
||||
use algebraic_simplification::algebraic_simplification;
|
||||
use constant_propagation::constant_propagation;
|
||||
use dead_code::{remove_redundant_moves, remove_unreachable_code};
|
||||
use dead_code::{remove_redundant_jumps, remove_redundant_moves, remove_unreachable_code};
|
||||
use dead_store_elimination::dead_store_elimination;
|
||||
use function_call_optimization::optimize_function_calls;
|
||||
use label_resolution::resolve_labels;
|
||||
@@ -91,5 +91,10 @@ pub fn optimize<'a>(instructions: Instructions<'a>) -> Instructions<'a> {
|
||||
}
|
||||
|
||||
// Final Pass: Resolve Labels to Line Numbers
|
||||
Instructions::new(resolve_labels(instructions))
|
||||
let mut instructions = resolve_labels(instructions);
|
||||
|
||||
// Post-resolution Pass: Remove redundant jumps (must run after label resolution)
|
||||
let (instructions, _) = remove_redundant_jumps(instructions);
|
||||
|
||||
Instructions::new(instructions)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user