transform: optimize string comparisons against ""

This optimizes a common pattern like:

    if s != "" {
        ...
    }

to:

    if len(s) != 0 {
        ...
    }

This avoids a runtime call and thus produces slightly better code.
This commit is contained in:
Ayke van Laethem
2021-03-08 13:39:46 +01:00
committed by Ron Evans
parent 13db2c13e5
commit f9865a08bc
5 changed files with 77 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
package transform
import (
"testing"
"tinygo.org/x/go-llvm"
)
func TestOptimizeStringToBytes(t *testing.T) {
t.Parallel()
testTransform(t, "testdata/stringtobytes", func(mod llvm.Module) {
// Run optimization pass.
OptimizeStringToBytes(mod)
})
}
func TestOptimizeStringEqual(t *testing.T) {
t.Parallel()
testTransform(t, "testdata/stringequal", func(mod llvm.Module) {
// Run optimization pass.
OptimizeStringEqual(mod)
})
}