diff --git a/src/codegen/executeBinaryOp.go b/src/codegen/executeBinaryOp.go index 3f0ec35..0d4fefe 100644 --- a/src/codegen/executeBinaryOp.go +++ b/src/codegen/executeBinaryOp.go @@ -11,6 +11,10 @@ import ( ) func (f *Function) executeBinaryOp(step *Step, instr *ssa.BinaryOp) { + if step.Register == -1 && !instr.Op.IsComparison() { + return + } + left := f.ValueToStep[instr.Left] right := f.ValueToStep[instr.Right] live := slices.Concat(step.Live, []*Step{left, right}) diff --git a/src/codegen/executeUnaryOp.go b/src/codegen/executeUnaryOp.go index 3e354f9..4b453bc 100644 --- a/src/codegen/executeUnaryOp.go +++ b/src/codegen/executeUnaryOp.go @@ -7,6 +7,10 @@ import ( ) func (f *Function) executeUnaryOp(step *Step, instr *ssa.UnaryOp) { + if step.Register == -1 { + return + } + operand := f.ValueToStep[instr.Operand] source := f.resolveOperand(operand, step.Live) destination := step.Register @@ -36,4 +40,4 @@ func (f *Function) executeUnaryOp(step *Step, instr *ssa.UnaryOp) { if isSpilled { f.storeSpill(step, destination) } -} +} \ No newline at end of file diff --git a/src/codegen/hintDestination.go b/src/codegen/hintDestination.go index 443903d..0c755a8 100644 --- a/src/codegen/hintDestination.go +++ b/src/codegen/hintDestination.go @@ -6,6 +6,10 @@ import ( // hintDestination recommends the destination register to its inputs. func (f *Function) hintDestination(step *Step) { + if step.Register == -1 { + return + } + switch instr := step.Value.(type) { case *ssa.BinaryOp: if instr.Op.IsComparison() { diff --git a/tests/dead-value-call.q b/tests/dead-value-call.q new file mode 100644 index 0000000..d364b38 --- /dev/null +++ b/tests/dead-value-call.q @@ -0,0 +1,13 @@ +main() { + total := 0 + + loop i := 0..4 { + total += i + } + + total += f(3) +} + +f(x int) -> int { + return x * 2 +} \ No newline at end of file diff --git a/tests/tests_test.go b/tests/tests_test.go index df76895..ff694b7 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -196,6 +196,7 @@ var tests = []run{ {"dead-value", nil, "", "", 0}, {"dead-value-load", nil, "", "", 0}, {"dead-value-orphan", nil, "", "", 0}, + {"dead-value-call", nil, "", "", 0}, } func TestTests(t *testing.T) {