diff --git a/src/config/Build.go b/src/config/Build.go index 6779667..93dc629 100644 --- a/src/config/Build.go +++ b/src/config/Build.go @@ -9,5 +9,4 @@ type Build struct { Dry bool FoldConstants bool LintBinaryOps bool - RemoveCopies bool } \ No newline at end of file diff --git a/src/config/Optimize.go b/src/config/Optimize.go index 3570854..2aeb77a 100644 --- a/src/config/Optimize.go +++ b/src/config/Optimize.go @@ -3,5 +3,4 @@ package config // Optimize enables or disables the optimizer. func (build *Build) Optimize(enabled bool) { build.FoldConstants = enabled - build.RemoveCopies = enabled } \ No newline at end of file diff --git a/src/core/optimize.go b/src/core/optimize.go index 4a6873c..b575bec 100644 --- a/src/core/optimize.go +++ b/src/core/optimize.go @@ -24,14 +24,6 @@ func (f *Function) optimize() error { f.Block().Append(&ssa.Return{}) } - // Copies were inserted for assignments to be safe in case - // loops would replace an existing value with a phi inside - // the loop. Now that all the loop replacements happened, - // we can safely remove the copies. - if f.Env.Build.RemoveCopies { - f.removeCopies() - } - // Lint binary operations that can often be reduced to // simpler expressions. if f.Env.Build.LintBinaryOps { diff --git a/src/core/removeCopies.go b/src/core/removeCopies.go deleted file mode 100644 index 9f9d6d0..0000000 --- a/src/core/removeCopies.go +++ /dev/null @@ -1,51 +0,0 @@ -package core - -import "git.urbach.dev/cli/q/src/ssa" - -// removeCopies removes copy operations. -func (f *Function) removeCopies() { - var replacements map[ssa.Value]ssa.Value - - for _, block := range f.Blocks { - if block.Loop != nil { - continue - } - - for i, value := range block.Instructions { - copy, isCopy := value.(*ssa.Copy) - - if !isCopy { - continue - } - - if copy.Typ != copy.Value.Type() { - continue - } - - if replacements == nil { - replacements = make(map[ssa.Value]ssa.Value) - } - - original := copy.Value - - for { - originalCopy, originalIsCopy := original.(*ssa.Copy) - - if !originalIsCopy { - break - } - - original = originalCopy.Value - } - - replacements[copy] = original - block.RemoveAt(i) - } - - block.RemoveNilValues() - } - - for old, new := range replacements { - f.IR.ReplaceAll(old, new) - } -} \ No newline at end of file diff --git a/tests/max.q b/tests/max.q new file mode 100644 index 0000000..5407f00 --- /dev/null +++ b/tests/max.q @@ -0,0 +1,11 @@ +main() { + a := 1 + b := 2 + max := a + + if b > max { + max = b + } + + assert max == 2 +} \ No newline at end of file diff --git a/tests/min.q b/tests/min.q new file mode 100644 index 0000000..53d3646 --- /dev/null +++ b/tests/min.q @@ -0,0 +1,11 @@ +main() { + a := 1 + b := 2 + min := a + + if b < min { + min = b + } + + assert min == 1 +} \ No newline at end of file diff --git a/tests/tests_test.go b/tests/tests_test.go index 9b02aed..f1b50de 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -53,6 +53,8 @@ var tests = []run{ {"phi-advanced", nil, "", "", 0}, {"phi-reassign", nil, "", "", 0}, {"phi-reassign-output", nil, "", "12345", 0}, + {"min", nil, "", "", 0}, + {"max", nil, "", "", 0}, {"hello", nil, "", "Hello\nHello\nHello\n", 0}, {"escape", nil, "", "a\tb\nc\td\n", 0}, {"script", nil, "", "Hello\n", 0}, @@ -131,13 +133,13 @@ var tests = []run{ {"pointer-to-pointer", nil, "", "", 0}, {"stack-pointer", nil, "", "", 0}, {"global", nil, "", "", 0}, + {"c-length", nil, "", "", 0}, + {"c-string", nil, "", "", 0}, {"cli-args", nil, "", "", 0}, {"cli-args", []string{"Hello"}, "", "Hello\n", 0}, {"cli-args", []string{"Hello", "World"}, "", "Hello\nWorld\n", 0}, {"cli-args", []string{"Hello", "世界"}, "", "Hello\n世界\n", 0}, {"cli-env", nil, "", "", 0}, - {"c-length", nil, "", "", 0}, - {"c-string", nil, "", "", 0}, } func TestTests(t *testing.T) {