Skip to content

Commit

Permalink
cmd/compile: optimize unsafe.Slice generated code
Browse files Browse the repository at this point in the history
We don't need a multiply when the element type is size 0 or 1.

The panic functions don't return, so we don't need any post-call
code (register restores, etc.).

Change-Id: I0dcea5df56d29d7be26554ddca966b3903c672e5
Reviewed-on: https://go-review.googlesource.com/c/go/+/419754
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Cuong Manh Le <[email protected]>
Run-TryBot: Keith Randall <[email protected]>
Reviewed-by: Keith Randall <[email protected]>
Reviewed-by: Than McIntosh <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
  • Loading branch information
randall77 authored and jproberts committed Aug 10, 2022
1 parent 9762122 commit f29d7d2
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/cmd/compile/internal/ssa/gen/generic.rules
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@

// Convert x * 1 to x.
(Mul(8|16|32|64) (Const(8|16|32|64) [1]) x) => x
(Select0 (Mul(32|64)uover (Const(32|64) [1]) x)) => x
(Select1 (Mul(32|64)uover (Const(32|64) [1]) x)) => (ConstBool [false])

// Convert x * -1 to -x.
(Mul(8|16|32|64) (Const(8|16|32|64) [-1]) x) => (Neg(8|16|32|64) x)
Expand Down Expand Up @@ -531,6 +533,8 @@
(Add(64|32|16|8) (Const(64|32|16|8) [0]) x) => x
(Sub(64|32|16|8) x x) => (Const(64|32|16|8) [0])
(Mul(64|32|16|8) (Const(64|32|16|8) [0]) _) => (Const(64|32|16|8) [0])
(Select0 (Mul(64|32)uover (Const(64|32) [0]) x)) => (Const(64|32) [0])
(Select1 (Mul(64|32)uover (Const(64|32) [0]) x)) => (ConstBool [false])

(Com(64|32|16|8) (Com(64|32|16|8) x)) => x
(Com(64|32|16|8) (Const(64|32|16|8) [c])) => (Const(64|32|16|8) [^c])
Expand Down
152 changes: 152 additions & 0 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.

2 changes: 1 addition & 1 deletion src/cmd/compile/internal/ssagen/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ func (s *state) stmt(n ir.Node) {
s.callResult(n, callNormal)
if n.Op() == ir.OCALLFUNC && n.X.Op() == ir.ONAME && n.X.(*ir.Name).Class == ir.PFUNC {
if fn := n.X.Sym().Name; base.Flag.CompilingRuntime && fn == "throw" ||
n.X.Sym().Pkg == ir.Pkgs.Runtime && (fn == "throwinit" || fn == "gopanic" || fn == "panicwrap" || fn == "block" || fn == "panicmakeslicelen" || fn == "panicmakeslicecap") {
n.X.Sym().Pkg == ir.Pkgs.Runtime && (fn == "throwinit" || fn == "gopanic" || fn == "panicwrap" || fn == "block" || fn == "panicmakeslicelen" || fn == "panicmakeslicecap" || fn == "panicunsafeslicelen" || fn == "panicunsafeslicenilptr") {
m := s.mem()
b := s.endBlock()
b.Kind = ssa.BlockExit
Expand Down
15 changes: 15 additions & 0 deletions test/codegen/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package codegen

import "unsafe"

// This file contains code generation tests related to the handling of
// slice types.

Expand Down Expand Up @@ -368,3 +370,16 @@ func SliceWithSubtractBound(a []int, b int) []int {
// ppc64:"SUBC",-"NEG"
return a[(3 - b):]
}

// --------------------------------------- //
// Code generation for unsafe.Slice //
// --------------------------------------- //

func Slice1(p *byte, i int) []byte {
// amd64:-"MULQ"
return unsafe.Slice(p, i)
}
func Slice0(p *struct{}, i int) []struct{} {
// amd64:-"MULQ"
return unsafe.Slice(p, i)
}

0 comments on commit f29d7d2

Please sign in to comment.