Commit 731e6fc3 authored by Ben Shi's avatar Ben Shi

cmd/compile: generate Select on WASM

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: default avatarRichard Musiol <neelance@gmail.com>
Reviewed-by: default avatarCherry Zhang <cherryyz@google.com>
parent 9c67516e
...@@ -20,7 +20,7 @@ package ssa ...@@ -20,7 +20,7 @@ package ssa
func branchelim(f *Func) { func branchelim(f *Func) {
// FIXME: add support for lowering CondSelects on more architectures // FIXME: add support for lowering CondSelects on more architectures
switch f.Config.arch { switch f.Config.arch {
case "arm64", "amd64": case "arm64", "amd64", "wasm":
// implemented // implemented
default: default:
return return
......
...@@ -390,6 +390,8 @@ ...@@ -390,6 +390,8 @@
(PopCount16 x) -> (I64Popcnt (ZeroExt16to64 x)) (PopCount16 x) -> (I64Popcnt (ZeroExt16to64 x))
(PopCount8 x) -> (I64Popcnt (ZeroExt8to64 x)) (PopCount8 x) -> (I64Popcnt (ZeroExt8to64 x))
(CondSelect <t> x y cond) -> (Select <t> x y cond)
// --- Optimizations --- // --- Optimizations ---
(I64Add (I64Const [x]) (I64Const [y])) -> (I64Const [x + y]) (I64Add (I64Const [x]) (I64Const [y])) -> (I64Const [x + y])
(I64Mul (I64Const [x]) (I64Const [y])) -> (I64Const [x * y]) (I64Mul (I64Const [x]) (I64Const [y])) -> (I64Const [x * y])
......
...@@ -50,6 +50,8 @@ func rewriteValueWasm(v *Value) bool { ...@@ -50,6 +50,8 @@ func rewriteValueWasm(v *Value) bool {
return rewriteValueWasm_OpCom64_0(v) return rewriteValueWasm_OpCom64_0(v)
case OpCom8: case OpCom8:
return rewriteValueWasm_OpCom8_0(v) return rewriteValueWasm_OpCom8_0(v)
case OpCondSelect:
return rewriteValueWasm_OpCondSelect_0(v)
case OpConst16: case OpConst16:
return rewriteValueWasm_OpConst16_0(v) return rewriteValueWasm_OpConst16_0(v)
case OpConst32: case OpConst32:
...@@ -865,6 +867,23 @@ func rewriteValueWasm_OpCom8_0(v *Value) bool { ...@@ -865,6 +867,23 @@ func rewriteValueWasm_OpCom8_0(v *Value) bool {
return true 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 { func rewriteValueWasm_OpConst16_0(v *Value) bool {
// match: (Const16 [val]) // match: (Const16 [val])
// cond: // cond:
......
...@@ -13,6 +13,7 @@ func cmovint(c int) int { ...@@ -13,6 +13,7 @@ func cmovint(c int) int {
} }
// amd64:"CMOVQLT" // amd64:"CMOVQLT"
// arm64:"CSEL\tLT" // arm64:"CSEL\tLT"
// wasm:"Select"
return x return x
} }
...@@ -22,6 +23,7 @@ func cmovchan(x, y chan int) chan int { ...@@ -22,6 +23,7 @@ func cmovchan(x, y chan int) chan int {
} }
// amd64:"CMOVQNE" // amd64:"CMOVQNE"
// arm64:"CSEL\tNE" // arm64:"CSEL\tNE"
// wasm:"Select"
return x return x
} }
...@@ -31,6 +33,7 @@ func cmovuintptr(x, y uintptr) uintptr { ...@@ -31,6 +33,7 @@ func cmovuintptr(x, y uintptr) uintptr {
} }
// amd64:"CMOVQCS" // amd64:"CMOVQCS"
// arm64:"CSEL\tLO" // arm64:"CSEL\tLO"
// wasm:"Select"
return x return x
} }
...@@ -40,6 +43,7 @@ func cmov32bit(x, y uint32) uint32 { ...@@ -40,6 +43,7 @@ func cmov32bit(x, y uint32) uint32 {
} }
// amd64:"CMOVLCS" // amd64:"CMOVLCS"
// arm64:"CSEL\tLO" // arm64:"CSEL\tLO"
// wasm:"Select"
return x return x
} }
...@@ -49,6 +53,7 @@ func cmov16bit(x, y uint16) uint16 { ...@@ -49,6 +53,7 @@ func cmov16bit(x, y uint16) uint16 {
} }
// amd64:"CMOVWCS" // amd64:"CMOVWCS"
// arm64:"CSEL\tLO" // arm64:"CSEL\tLO"
// wasm:"Select"
return x return x
} }
...@@ -61,6 +66,7 @@ func cmovfloateq(x, y float64) int { ...@@ -61,6 +66,7 @@ func cmovfloateq(x, y float64) int {
} }
// amd64:"CMOVQNE","CMOVQPC" // amd64:"CMOVQNE","CMOVQPC"
// arm64:"CSEL\tEQ" // arm64:"CSEL\tEQ"
// wasm:"Select"
return a return a
} }
...@@ -71,6 +77,7 @@ func cmovfloatne(x, y float64) int { ...@@ -71,6 +77,7 @@ func cmovfloatne(x, y float64) int {
} }
// amd64:"CMOVQNE","CMOVQPS" // amd64:"CMOVQNE","CMOVQPS"
// arm64:"CSEL\tNE" // arm64:"CSEL\tNE"
// wasm:"Select"
return a return a
} }
...@@ -96,6 +103,7 @@ func cmovfloatint2(x, y float64) float64 { ...@@ -96,6 +103,7 @@ func cmovfloatint2(x, y float64) float64 {
} }
// amd64:"CMOVQHI" // amd64:"CMOVQHI"
// arm64:"CSEL\tMI" // arm64:"CSEL\tMI"
// wasm:"Select"
r = r - ldexp(y, (rexp-yexp)) r = r - ldexp(y, (rexp-yexp))
} }
return r return r
...@@ -109,6 +117,7 @@ func cmovloaded(x [4]int, y int) int { ...@@ -109,6 +117,7 @@ func cmovloaded(x [4]int, y int) int {
} }
// amd64:"CMOVQNE" // amd64:"CMOVQNE"
// arm64:"CSEL\tNE" // arm64:"CSEL\tNE"
// wasm:"Select"
return y return y
} }
...@@ -119,6 +128,7 @@ func cmovuintptr2(x, y uintptr) uintptr { ...@@ -119,6 +128,7 @@ func cmovuintptr2(x, y uintptr) uintptr {
} }
// amd64:"CMOVQEQ" // amd64:"CMOVQEQ"
// arm64:"CSEL\tEQ" // arm64:"CSEL\tEQ"
// wasm:"Select"
return a return a
} }
...@@ -130,6 +140,7 @@ func cmovfloatmove(x, y int) float64 { ...@@ -130,6 +140,7 @@ func cmovfloatmove(x, y int) float64 {
} }
// amd64:-"CMOV" // amd64:-"CMOV"
// arm64:-"CSEL" // arm64:-"CSEL"
// wasm:-"Select"
return a return a
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment