Skip to content

Commit

Permalink
cmd/compile: use MakeResult in empty MakeSlice elimination
Browse files Browse the repository at this point in the history
This gets eliminated by thoses rules above:
  // for rewriting results of some late-expanded rewrites (below)
  (SelectN [0] (MakeResult x ___)) => x
  (SelectN [1] (MakeResult x y ___)) => y
  (SelectN [2] (MakeResult x y z ___)) => z

Fixes #58161

Change-Id: I4fbfd52c72c06b6b3db906bd9910b6dbb7fe8975
Reviewed-on: https://go-review.googlesource.com/c/go/+/463846
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
  • Loading branch information
Jorropo authored and randall77 committed Feb 8, 2023
1 parent 9d81ccb commit 0d8d181
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 108 deletions.
12 changes: 3 additions & 9 deletions src/cmd/compile/internal/ssa/_gen/generic.rules
Original file line number Diff line number Diff line change
Expand Up @@ -2077,15 +2077,9 @@
=> (Zero {types.Types[types.TUINT8]} [int64(c)] sptr mem)

// Recognise make([]T, 0) and replace it with a pointer to the zerobase
(SelectN [0] call:(StaticLECall _ (Const(64|32) [0]) (Const(64|32) [0]) _))
&& isSameCall(call.Aux, "runtime.makeslice")
&& clobberIfDead(call)
=> (Addr {ir.Syms.Zerobase} (SB))

(SelectN [1] call:(StaticLECall _ (Const(64|32) [0]) (Const(64|32) [0]) mem))
&& isSameCall(call.Aux, "runtime.makeslice")
&& clobberIfDead(call)
=> mem
(StaticLECall {callAux} _ (Const(64|32) [0]) (Const(64|32) [0]) mem)
&& isSameCall(callAux, "runtime.makeslice")
=> (MakeResult (Addr <v.Type.FieldType(0)> {ir.Syms.Zerobase} (SB)) mem)

// Evaluate constant address comparisons.
(EqPtr x x) => (ConstBool [true])
Expand Down
149 changes: 50 additions & 99 deletions src/cmd/compile/internal/ssa/rewritegeneric.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test/fixedbugs/issue58161.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// compile -d=ssa/check/seed=1

// Copyright 2023 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

func F[G int]() int {
return len(make([]int, copy([]G{}, []G{})))
}

func main() {
F[int]()
}

0 comments on commit 0d8d181

Please sign in to comment.