Commit 88dc4aee authored by Richard Musiol's avatar Richard Musiol Committed by Cherry Zhang

cmd/compile: fix OffPtr with negative offset on wasm

The wasm archtecture was missing a rule to handle OffPtr with a
negative offset. This commit makes it so OffPtr always gets lowered
to I64AddConst.

Fixes #25741

Change-Id: I1d48e2954e3ff31deb8cba9a9bf0cab7c4bab71a
Reviewed-on: https://go-review.googlesource.com/116595Reviewed-by: default avatarCherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
parent 6decd3d9
...@@ -46,8 +46,7 @@ ...@@ -46,8 +46,7 @@
(Not x) -> (I64Eqz x) (Not x) -> (I64Eqz x)
// Lowering pointer arithmetic // Lowering pointer arithmetic
(OffPtr [0] ptr) -> ptr (OffPtr [off] ptr) -> (I64AddConst [off] ptr)
(OffPtr [off] ptr) && off > 0 -> (I64AddConst [off] ptr)
// Lowering extension // Lowering extension
// It is unnecessary to extend loads // It is unnecessary to extend loads
...@@ -388,6 +387,7 @@ ...@@ -388,6 +387,7 @@
(I64Ne x (I64Const [0])) -> (I64Eqz (I64Eqz x)) (I64Ne x (I64Const [0])) -> (I64Eqz (I64Eqz x))
(I64Add x (I64Const [y])) -> (I64AddConst [y] x) (I64Add x (I64Const [y])) -> (I64AddConst [y] x)
(I64AddConst [0] x) -> x
(I64Eqz (I64Eqz (I64Eqz x))) -> (I64Eqz x) (I64Eqz (I64Eqz (I64Eqz x))) -> (I64Eqz x)
((I64Load|I64Load32U|I64Load32S|I64Load16U|I64Load16S|I64Load8U|I64Load8S) [off] (I64AddConst [off2] ptr) mem) ((I64Load|I64Load32U|I64Load32S|I64Load16U|I64Load16S|I64Load8U|I64Load8S) [off] (I64AddConst [off2] ptr) mem)
......
...@@ -463,6 +463,8 @@ func rewriteValueWasm(v *Value) bool { ...@@ -463,6 +463,8 @@ func rewriteValueWasm(v *Value) bool {
return rewriteValueWasm_OpWasmF64Mul_0(v) return rewriteValueWasm_OpWasmF64Mul_0(v)
case OpWasmI64Add: case OpWasmI64Add:
return rewriteValueWasm_OpWasmI64Add_0(v) return rewriteValueWasm_OpWasmI64Add_0(v)
case OpWasmI64AddConst:
return rewriteValueWasm_OpWasmI64AddConst_0(v)
case OpWasmI64And: case OpWasmI64And:
return rewriteValueWasm_OpWasmI64And_0(v) return rewriteValueWasm_OpWasmI64And_0(v)
case OpWasmI64Eq: case OpWasmI64Eq:
...@@ -3688,34 +3690,17 @@ func rewriteValueWasm_OpNot_0(v *Value) bool { ...@@ -3688,34 +3690,17 @@ func rewriteValueWasm_OpNot_0(v *Value) bool {
} }
} }
func rewriteValueWasm_OpOffPtr_0(v *Value) bool { func rewriteValueWasm_OpOffPtr_0(v *Value) bool {
// match: (OffPtr [0] ptr)
// cond:
// result: ptr
for {
if v.AuxInt != 0 {
break
}
ptr := v.Args[0]
v.reset(OpCopy)
v.Type = ptr.Type
v.AddArg(ptr)
return true
}
// match: (OffPtr [off] ptr) // match: (OffPtr [off] ptr)
// cond: off > 0 // cond:
// result: (I64AddConst [off] ptr) // result: (I64AddConst [off] ptr)
for { for {
off := v.AuxInt off := v.AuxInt
ptr := v.Args[0] ptr := v.Args[0]
if !(off > 0) {
break
}
v.reset(OpWasmI64AddConst) v.reset(OpWasmI64AddConst)
v.AuxInt = off v.AuxInt = off
v.AddArg(ptr) v.AddArg(ptr)
return true return true
} }
return false
} }
func rewriteValueWasm_OpOr16_0(v *Value) bool { func rewriteValueWasm_OpOr16_0(v *Value) bool {
// match: (Or16 x y) // match: (Or16 x y)
...@@ -5211,6 +5196,22 @@ func rewriteValueWasm_OpWasmI64Add_0(v *Value) bool { ...@@ -5211,6 +5196,22 @@ func rewriteValueWasm_OpWasmI64Add_0(v *Value) bool {
} }
return false return false
} }
func rewriteValueWasm_OpWasmI64AddConst_0(v *Value) bool {
// match: (I64AddConst [0] x)
// cond:
// result: x
for {
if v.AuxInt != 0 {
break
}
x := v.Args[0]
v.reset(OpCopy)
v.Type = x.Type
v.AddArg(x)
return true
}
return false
}
func rewriteValueWasm_OpWasmI64And_0(v *Value) bool { func rewriteValueWasm_OpWasmI64And_0(v *Value) bool {
b := v.Block b := v.Block
_ = b _ = b
......
// compile
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
var s []int
func main() {
i := -1
s[i] = 0
}
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