]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/walk: remove reduceSlice
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Thu, 22 Sep 2022 11:53:12 +0000 (18:53 +0700)
committerGopher Robot <gobot@golang.org>
Thu, 22 Sep 2022 21:36:44 +0000 (21:36 +0000)
After CL 22425, there're two optimizations for slice expr which are
never applied during walk pass:

s[i:len(s)]
        s[i:j:cap(s)]

The order pass have already rewritten len/cap expression to use autotmp,
thus the same safe expression check will never fire. The code can now be
simplified by moving the only case left from reduceSlice to walkSlice,
then removing reduceSlice entirely.

Passes toolstash-check.

Change-Id: Ia8cfb15c8e96c186a214c17b42d0fee51b0d3a1c
Reviewed-on: https://go-review.googlesource.com/c/go/+/432695
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/walk/expr.go

index c12fb20106dc3cd0c1b56196182080410e34b83c..ede904c8a37d93e554f310d3a939ccb27f8dfc26 100644 (file)
@@ -842,19 +842,14 @@ func walkSlice(n *ir.SliceExpr, init *ir.Nodes) ir.Node {
        n.High = walkExpr(n.High, init)
        n.Max = walkExpr(n.Max, init)
 
-       if n.Op().IsSlice3() {
-               if n.Max != nil && n.Max.Op() == ir.OCAP && ir.SameSafeExpr(n.X, n.Max.(*ir.UnaryExpr).X) {
-                       // Reduce x[i:j:cap(x)] to x[i:j].
-                       if n.Op() == ir.OSLICE3 {
-                               n.SetOp(ir.OSLICE)
-                       } else {
-                               n.SetOp(ir.OSLICEARR)
-                       }
-                       return reduceSlice(n)
+       if (n.Op() == ir.OSLICE || n.Op() == ir.OSLICESTR) && n.Low == nil && n.High == nil {
+               // Reduce x[:] to x.
+               if base.Debug.Slice > 0 {
+                       base.Warn("slice: omit slice operation")
                }
-               return n
+               return n.X
        }
-       return reduceSlice(n)
+       return n
 }
 
 // walkSliceHeader walks an OSLICEHEADER node.
@@ -872,22 +867,6 @@ func walkStringHeader(n *ir.StringHeaderExpr, init *ir.Nodes) ir.Node {
        return n
 }
 
-// TODO(josharian): combine this with its caller and simplify
-func reduceSlice(n *ir.SliceExpr) ir.Node {
-       if n.High != nil && n.High.Op() == ir.OLEN && ir.SameSafeExpr(n.X, n.High.(*ir.UnaryExpr).X) {
-               // Reduce x[i:len(x)] to x[i:].
-               n.High = nil
-       }
-       if (n.Op() == ir.OSLICE || n.Op() == ir.OSLICESTR) && n.Low == nil && n.High == nil {
-               // Reduce x[:] to x.
-               if base.Debug.Slice > 0 {
-                       base.Warn("slice: omit slice operation")
-               }
-               return n.X
-       }
-       return n
-}
-
 // return 1 if integer n must be in range [0, max), 0 otherwise
 func bounded(n ir.Node, max int64) bool {
        if n.Type() == nil || !n.Type().IsInteger() {