]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: generate Select on WASM
authorBen Shi <powerman1st@163.com>
Tue, 20 Aug 2019 09:03:41 +0000 (09:03 +0000)
committerBen Shi <powerman1st@163.com>
Wed, 28 Aug 2019 02:29:25 +0000 (02:29 +0000)
This CL performs the branchelim optimization on WASM with its
select instruction. And the total size of pkg/js_wasm decreased
about 80KB by this optimization.

Change-Id: I868eb146120a1cac5c4609c8e9ddb07e4da8a1d9
Reviewed-on: https://go-review.googlesource.com/c/go/+/190957
Run-TryBot: Ben Shi <powerman1st@163.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Richard Musiol <neelance@gmail.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
src/cmd/compile/internal/ssa/branchelim.go
src/cmd/compile/internal/ssa/gen/Wasm.rules
src/cmd/compile/internal/ssa/rewriteWasm.go
test/codegen/condmove.go

index c543686b3d3a2ba5192bffd6359ed06194ec1df0..71c947d0d52770bc58ae43449a9157c4d2c74ca1 100644 (file)
@@ -20,7 +20,7 @@ package ssa
 func branchelim(f *Func) {
        // FIXME: add support for lowering CondSelects on more architectures
        switch f.Config.arch {
-       case "arm64", "amd64":
+       case "arm64", "amd64", "wasm":
                // implemented
        default:
                return
index 965e4a8bb6afdbf00b4f720dca6e6b417f104d79..72bf01ba99f17fc8300d5597ee3b66bbc61e247d 100644 (file)
 (PopCount16 x) -> (I64Popcnt (ZeroExt16to64 x))
 (PopCount8  x) -> (I64Popcnt (ZeroExt8to64  x))
 
+(CondSelect <t> x y cond) -> (Select <t> x y cond)
+
 // --- Optimizations ---
 (I64Add (I64Const [x]) (I64Const [y])) -> (I64Const [x + y])
 (I64Mul (I64Const [x]) (I64Const [y])) -> (I64Const [x * y])
index 98ca6f69a33729fcd173da338439d6c3d61e5f1c..4bded4606573c82fc99994b97b10de2510410948 100644 (file)
@@ -50,6 +50,8 @@ func rewriteValueWasm(v *Value) bool {
                return rewriteValueWasm_OpCom64_0(v)
        case OpCom8:
                return rewriteValueWasm_OpCom8_0(v)
+       case OpCondSelect:
+               return rewriteValueWasm_OpCondSelect_0(v)
        case OpConst16:
                return rewriteValueWasm_OpConst16_0(v)
        case OpConst32:
@@ -865,6 +867,23 @@ func rewriteValueWasm_OpCom8_0(v *Value) bool {
                return true
        }
 }
+func rewriteValueWasm_OpCondSelect_0(v *Value) bool {
+       // match: (CondSelect <t> x y cond)
+       // cond:
+       // result: (Select <t> x y cond)
+       for {
+               t := v.Type
+               cond := v.Args[2]
+               x := v.Args[0]
+               y := v.Args[1]
+               v.reset(OpWasmSelect)
+               v.Type = t
+               v.AddArg(x)
+               v.AddArg(y)
+               v.AddArg(cond)
+               return true
+       }
+}
 func rewriteValueWasm_OpConst16_0(v *Value) bool {
        // match: (Const16 [val])
        // cond:
index 3690a54618656e0f1aaff86aabf7b690417c6c4d..bd3fe59427e1d33479d2eeb1ba04d7f05f9c980b 100644 (file)
@@ -13,6 +13,7 @@ func cmovint(c int) int {
        }
        // amd64:"CMOVQLT"
        // arm64:"CSEL\tLT"
+       // wasm:"Select"
        return x
 }
 
@@ -22,6 +23,7 @@ func cmovchan(x, y chan int) chan int {
        }
        // amd64:"CMOVQNE"
        // arm64:"CSEL\tNE"
+       // wasm:"Select"
        return x
 }
 
@@ -31,6 +33,7 @@ func cmovuintptr(x, y uintptr) uintptr {
        }
        // amd64:"CMOVQCS"
        // arm64:"CSEL\tLO"
+       // wasm:"Select"
        return x
 }
 
@@ -40,6 +43,7 @@ func cmov32bit(x, y uint32) uint32 {
        }
        // amd64:"CMOVLCS"
        // arm64:"CSEL\tLO"
+       // wasm:"Select"
        return x
 }
 
@@ -49,6 +53,7 @@ func cmov16bit(x, y uint16) uint16 {
        }
        // amd64:"CMOVWCS"
        // arm64:"CSEL\tLO"
+       // wasm:"Select"
        return x
 }
 
@@ -61,6 +66,7 @@ func cmovfloateq(x, y float64) int {
        }
        // amd64:"CMOVQNE","CMOVQPC"
        // arm64:"CSEL\tEQ"
+       // wasm:"Select"
        return a
 }
 
@@ -71,6 +77,7 @@ func cmovfloatne(x, y float64) int {
        }
        // amd64:"CMOVQNE","CMOVQPS"
        // arm64:"CSEL\tNE"
+       // wasm:"Select"
        return a
 }
 
@@ -96,6 +103,7 @@ func cmovfloatint2(x, y float64) float64 {
                }
                // amd64:"CMOVQHI"
                // arm64:"CSEL\tMI"
+               // wasm:"Select"
                r = r - ldexp(y, (rexp-yexp))
        }
        return r
@@ -109,6 +117,7 @@ func cmovloaded(x [4]int, y int) int {
        }
        // amd64:"CMOVQNE"
        // arm64:"CSEL\tNE"
+       // wasm:"Select"
        return y
 }
 
@@ -119,6 +128,7 @@ func cmovuintptr2(x, y uintptr) uintptr {
        }
        // amd64:"CMOVQEQ"
        // arm64:"CSEL\tEQ"
+       // wasm:"Select"
        return a
 }
 
@@ -130,6 +140,7 @@ func cmovfloatmove(x, y int) float64 {
        }
        // amd64:-"CMOV"
        // arm64:-"CSEL"
+       // wasm:-"Select"
        return a
 }