1
0
mirror of https://github.com/golang/go synced 2024-11-17 05:44:52 -07:00

cmd/compile/internal/walk: remove reduceSlice

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>
This commit is contained in:
Cuong Manh Le 2022-09-22 18:53:12 +07:00 committed by Gopher Robot
parent 42a46206b9
commit 533cd80315

View 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() {