]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix loong64 constant folding in division rules
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Sat, 21 May 2022 12:00:18 +0000 (19:00 +0700)
committerGopher Robot <gobot@golang.org>
Mon, 23 May 2022 23:54:07 +0000 (23:54 +0000)
The divisor must be non-zero for the rule to be triggered.

Fixes #53018

Change-Id: Id56b8d986945bbb66e13131d11264ee438de5cb2
Reviewed-on: https://go-review.googlesource.com/c/go/+/407655
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: xiaodong liu <teaofmoli@gmail.com>
Reviewed-by: WANG Xuerui <git@xen0n.name>
src/cmd/compile/internal/ssa/gen/LOONG64.rules
src/cmd/compile/internal/ssa/rewriteLOONG64.go
test/fixedbugs/issue53018.go [new file with mode: 0644]

index 1ea5effb3cf369d5050cb0de2fec59229fc2e1ee..3ba25e0a95bda56a47677656fc096e08342f59f8 100644 (file)
 (SRLVconst [c] (MOVVconst [d]))  => (MOVVconst [int64(uint64(d)>>uint64(c))])
 (SRAVconst [c] (MOVVconst [d]))  => (MOVVconst [d>>uint64(c)])
 (Select1 (MULVU (MOVVconst [c]) (MOVVconst [d]))) => (MOVVconst [c*d])
-(Select1 (DIVV  (MOVVconst [c]) (MOVVconst [d]))) => (MOVVconst [c/d])
-(Select1 (DIVVU (MOVVconst [c]) (MOVVconst [d]))) => (MOVVconst [int64(uint64(c)/uint64(d))])
-(Select0 (DIVV  (MOVVconst [c]) (MOVVconst [d]))) => (MOVVconst [c%d])   // mod
-(Select0 (DIVVU (MOVVconst [c]) (MOVVconst [d]))) => (MOVVconst [int64(uint64(c)%uint64(d))]) // mod
+(Select1 (DIVV  (MOVVconst [c]) (MOVVconst [d]))) && d != 0 => (MOVVconst [c/d])
+(Select1 (DIVVU (MOVVconst [c]) (MOVVconst [d]))) && d != 0 => (MOVVconst [int64(uint64(c)/uint64(d))])
+(Select0 (DIVV  (MOVVconst [c]) (MOVVconst [d]))) && d != 0 => (MOVVconst [c%d])   // mod
+(Select0 (DIVVU (MOVVconst [c]) (MOVVconst [d]))) && d != 0 => (MOVVconst [int64(uint64(c)%uint64(d))]) // mod
 (ANDconst [c] (MOVVconst [d])) => (MOVVconst [c&d])
 (ANDconst [c] (ANDconst [d] x)) => (ANDconst [c&d] x)
 (ORconst [c] (MOVVconst [d])) => (MOVVconst [c|d])
index 6163f5577b1a1c13c5226c51217b869a3120d7cf..3fc10104b940b92d54079b447061791aa46bc9d6 100644 (file)
@@ -6828,6 +6828,7 @@ func rewriteValueLOONG64_OpSelect0(v *Value) bool {
                return true
        }
        // match: (Select0 (DIVV (MOVVconst [c]) (MOVVconst [d])))
+       // cond: d != 0
        // result: (MOVVconst [c%d])
        for {
                if v_0.Op != OpLOONG64DIVV {
@@ -6844,11 +6845,15 @@ func rewriteValueLOONG64_OpSelect0(v *Value) bool {
                        break
                }
                d := auxIntToInt64(v_0_1.AuxInt)
+               if !(d != 0) {
+                       break
+               }
                v.reset(OpLOONG64MOVVconst)
                v.AuxInt = int64ToAuxInt(c % d)
                return true
        }
        // match: (Select0 (DIVVU (MOVVconst [c]) (MOVVconst [d])))
+       // cond: d != 0
        // result: (MOVVconst [int64(uint64(c)%uint64(d))])
        for {
                if v_0.Op != OpLOONG64DIVVU {
@@ -6865,6 +6870,9 @@ func rewriteValueLOONG64_OpSelect0(v *Value) bool {
                        break
                }
                d := auxIntToInt64(v_0_1.AuxInt)
+               if !(d != 0) {
+                       break
+               }
                v.reset(OpLOONG64MOVVconst)
                v.AuxInt = int64ToAuxInt(int64(uint64(c) % uint64(d)))
                return true
@@ -7040,6 +7048,7 @@ func rewriteValueLOONG64_OpSelect1(v *Value) bool {
                break
        }
        // match: (Select1 (DIVV (MOVVconst [c]) (MOVVconst [d])))
+       // cond: d != 0
        // result: (MOVVconst [c/d])
        for {
                if v_0.Op != OpLOONG64DIVV {
@@ -7056,11 +7065,15 @@ func rewriteValueLOONG64_OpSelect1(v *Value) bool {
                        break
                }
                d := auxIntToInt64(v_0_1.AuxInt)
+               if !(d != 0) {
+                       break
+               }
                v.reset(OpLOONG64MOVVconst)
                v.AuxInt = int64ToAuxInt(c / d)
                return true
        }
        // match: (Select1 (DIVVU (MOVVconst [c]) (MOVVconst [d])))
+       // cond: d != 0
        // result: (MOVVconst [int64(uint64(c)/uint64(d))])
        for {
                if v_0.Op != OpLOONG64DIVVU {
@@ -7077,6 +7090,9 @@ func rewriteValueLOONG64_OpSelect1(v *Value) bool {
                        break
                }
                d := auxIntToInt64(v_0_1.AuxInt)
+               if !(d != 0) {
+                       break
+               }
                v.reset(OpLOONG64MOVVconst)
                v.AuxInt = int64ToAuxInt(int64(uint64(c) / uint64(d)))
                return true
diff --git a/test/fixedbugs/issue53018.go b/test/fixedbugs/issue53018.go
new file mode 100644 (file)
index 0000000..439d9d5
--- /dev/null
@@ -0,0 +1,30 @@
+// compile
+
+// Copyright 2022 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 p
+
+var V []int
+
+func f(i int, c chan int) int {
+       arr := []int{0, 1}
+       for range c {
+               for a2 := range arr {
+                       var a []int
+                       V = V[:1/a2]
+                       a[i] = 0
+               }
+               return func() int {
+                       arr = []int{}
+                       return func() int {
+                               return func() int {
+                                       return func() int { return 4 }()
+                               }()
+                       }()
+               }()
+       }
+
+       return 0
+}