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>
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
(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])
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:
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:
}
// amd64:"CMOVQLT"
// arm64:"CSEL\tLT"
+ // wasm:"Select"
return x
}
}
// amd64:"CMOVQNE"
// arm64:"CSEL\tNE"
+ // wasm:"Select"
return x
}
}
// amd64:"CMOVQCS"
// arm64:"CSEL\tLO"
+ // wasm:"Select"
return x
}
}
// amd64:"CMOVLCS"
// arm64:"CSEL\tLO"
+ // wasm:"Select"
return x
}
}
// amd64:"CMOVWCS"
// arm64:"CSEL\tLO"
+ // wasm:"Select"
return x
}
}
// amd64:"CMOVQNE","CMOVQPC"
// arm64:"CSEL\tEQ"
+ // wasm:"Select"
return a
}
}
// amd64:"CMOVQNE","CMOVQPS"
// arm64:"CSEL\tNE"
+ // wasm:"Select"
return a
}
}
// amd64:"CMOVQHI"
// arm64:"CSEL\tMI"
+ // wasm:"Select"
r = r - ldexp(y, (rexp-yexp))
}
return r
}
// amd64:"CMOVQNE"
// arm64:"CSEL\tNE"
+ // wasm:"Select"
return y
}
}
// amd64:"CMOVQEQ"
// arm64:"CSEL\tEQ"
+ // wasm:"Select"
return a
}
}
// amd64:-"CMOV"
// arm64:-"CSEL"
+ // wasm:-"Select"
return a
}