From: Cuong Manh Le Date: Thu, 22 Sep 2022 11:53:12 +0000 (+0700) Subject: cmd/compile/internal/walk: remove reduceSlice X-Git-Tag: go1.20rc1~959 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=533cd80315904661e3d644a140bdf52c8ac7aad6;p=gostls13.git 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 Auto-Submit: Cuong Manh Le Reviewed-by: Keith Randall Reviewed-by: Matthew Dempsky Reviewed-by: Keith Randall TryBot-Result: Gopher Robot --- diff --git a/src/cmd/compile/internal/walk/expr.go b/src/cmd/compile/internal/walk/expr.go index c12fb20106..ede904c8a3 100644 --- a/src/cmd/compile/internal/walk/expr.go +++ b/src/cmd/compile/internal/walk/expr.go @@ -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() {