1
0
mirror of https://github.com/golang/go synced 2024-09-29 09:34:28 -06:00
go/test/codegen
Matthew Dempsky 85fc765341 cmd/compile: optimize switch on strings
When compiling expression switches, we try to optimize runs of
constants into binary searches. The ordering used isn't visible to the
application, so it's unimportant as long as we're consistent between
sorting and searching.

For strings, it's much cheaper to compare string lengths than strings
themselves, so instead of ordering strings by "si <= sj", we currently
order them by "len(si) < len(sj) || len(si) == len(sj) && si <= sj"
(i.e., the lexicographical ordering on the 2-tuple (len(s), s)).

However, it's also somewhat cheaper to compare strings for equality
(i.e., ==) than for ordering (i.e., <=). And if there were two or
three string constants of the same length in a switch statement, we
might unnecessarily emit ordering comparisons.

For example, given:

    switch s {
    case "", "1", "2", "3": // ordered by length then content
        goto L
    }

we currently compile this as:

    if len(s) < 1 || len(s) == 1 && s <= "1" {
        if s == "" { goto L }
        else if s == "1" { goto L }
    } else {
        if s == "2" { goto L }
        else if s == "3" { goto L }
    }

This CL switches to using a 2-level binary search---first on len(s),
then on s itself---so that string ordering comparisons are only needed
when there are 4 or more strings of the same length. (4 being the
cut-off for when using binary search is actually worthwhile.)

So the above switch instead now compiles to:

    if len(s) == 0 {
        if s == "" { goto L }
    } else if len(s) == 1 {
        if s == "1" { goto L }
        else if s == "2" { goto L }
        else if s == "3" { goto L }
    }

which is better optimized by walk and SSA. (Notably, because there are
only two distinct lengths and no more than three strings of any
particular length, this example ends up falling back to simply using
linear search.)

Test case by khr@ from CL 195138.

Fixes #33934.

Change-Id: I8eeebcaf7e26343223be5f443d6a97a0daf84f07
Reviewed-on: https://go-review.googlesource.com/c/go/+/195340
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2019-09-18 05:33:05 +00:00
..
alloc.go cmd/compile: don't generate newobject call for 0-sized types 2019-02-26 23:08:15 +00:00
arithmetic.go cmd/compile: add signed divisibility rules 2019-04-30 22:02:07 +00:00
bitfield.go test/codegen: port last remaining misc bit/arithmetic tests 2018-04-10 07:58:35 +00:00
bits.go cmd/compile: optimize bitset tests 2019-08-27 18:01:16 +00:00
comparisons.go cmd/compile: eliminate WASM's redundant extension & wrapping 2019-08-30 21:20:03 +00:00
condmove.go cmd/compile: generate Select on WASM 2019-08-28 02:29:25 +00:00
copy.go test/codegen: enable more tests for ppc64/ppc64le 2018-10-16 19:00:53 +00:00
floats.go cmd/compile: fix the error of absorbing boolean tests into block(FGE, FGT) 2019-05-16 13:46:25 +00:00
issue22703.go test: port a nil-check interface test from asm_test 2018-03-03 20:20:54 +00:00
issue25378.go cmd/compile/internal/ssa: remove useless zero extension 2018-08-20 21:38:20 +00:00
issue31618.go cmd/compile: always mark atColumn1 results as statements 2019-04-23 17:39:11 +00:00
mapaccess.go cmd/compile/internal/gc: handle arith ops in samesafeexpr 2018-09-19 12:03:58 +00:00
maps.go cmd/compile: avoid string allocations when map key is struct or array literal 2018-10-15 19:22:07 +00:00
math.go Revert "compile: prefer an AND instead of SHR+SHL instructions" 2019-09-09 07:33:25 +00:00
mathbits.go cmd/compile: add math/bits.Mul64 intrinsic on s390x 2019-09-13 09:04:48 +00:00
memcombine.go cmd/compile: improve s390x sign/zero extension removal 2019-09-10 13:17:24 +00:00
memops.go cmd/compile: fix rule for combining loads with compares 2018-10-27 00:59:54 +00:00
noextend.go test/codegen: gofmt 2019-03-13 21:44:45 +00:00
race.go cmd/compile: handle new panicindex/slice names in optimizations 2019-04-03 21:24:17 +00:00
README Revert "test/codegen: document -all_codegen option in README" 2019-09-16 17:31:37 +00:00
rotate.go test/codegen: enable more tests for ppc64/ppc64le 2018-10-16 19:00:53 +00:00
shift.go cmd/compile: optimize bounded shifts on wasm 2019-08-28 04:44:21 +00:00
slices.go cmd/compile: support more length types for slice extension optimization 2019-09-17 17:18:17 +00:00
stack.go Revert "Revert "cmd/compile,runtime: allocate defer records on the stack"" 2019-06-10 16:19:39 +00:00
strings.go cmd/compile: apply optimization for readonly globals on wasm 2019-08-28 05:55:52 +00:00
structs.go test/codegen: port structs test to codegen 2018-03-18 16:53:53 +00:00
switch.go cmd/compile: optimize switch on strings 2019-09-18 05:33:05 +00:00
zerosize.go cmd/compile: pad zero-sized stack variables 2018-12-22 01:16:00 +00:00

// Copyright 2018 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.

The codegen directory contains code generation tests for the gc
compiler.


- Introduction

The test harness compiles Go code inside files in this directory and
matches the generated assembly (the output of `go tool compile -S`)
against a set of regexps to be specified in comments that follow a
special syntax (described below). The test driver is implemented as a
step of the top-level test/run.go suite, called "asmcheck".

The codegen harness is part of the all.bash test suite, but for
performance reasons only the codegen tests for the host machine's
GOARCH are enabled by default. To perform comprehensive tests for all
the supported architectures, one can run the following command

  $ ../bin/go run run.go -all_codegen -v codegen

in the top-level test directory. This is recommended after any change
that affect the compiler's code.

The test harness compiles the tests with the same go toolchain that is
used to run run.go. After writing tests for a newly added codegen
transformation, it can be useful to first run the test harness with a
toolchain from a released Go version (and verify that the new tests
fail), and then re-runnig the tests using the devel toolchain.


- Regexps comments syntax

Instructions to match are specified inside plain comments that start
with an architecture tag, followed by a colon and a quoted Go-style
regexp to be matched. For example, the following test:

  func Sqrt(x float64) float64 {
  	   // amd64:"SQRTSD"
  	   // arm64:"FSQRTD"
  	   return math.Sqrt(x)
  }

verifies that math.Sqrt calls are intrinsified to a SQRTSD instruction
on amd64, and to a FSQRTD instruction on arm64.

It is possible to put multiple architectures checks into the same
line, as:

  // amd64:"SQRTSD" arm64:"FSQRTD"

although this form should be avoided when doing so would make the
regexps line excessively long and difficult to read.

Comments that are on their own line will be matched against the first
subsequent non-comment line. Inline comments are also supported; the
regexp will be matched against the code found on the same line:

  func Sqrt(x float64) float64 {
  	   return math.Sqrt(x) // arm:"SQRTD"
  }

It's possible to specify a comma-separated list of regexps to be
matched. For example, the following test:

  func TZ8(n uint8) int {
  	   // amd64:"BSFQ","ORQ\t\\$256"
  	   return bits.TrailingZeros8(n)
  }

verifies that the code generated for a bits.TrailingZeros8 call on
amd64 contains both a "BSFQ" instruction and an "ORQ $256".

Note how the ORQ regex includes a tab char (\t). In the Go assembly
syntax, operands are separated from opcodes by a tabulation.

Regexps can be quoted using either " or `. Special characters must be
escaped accordingly. Both of these are accepted, and equivalent:

  // amd64:"ADDQ\t\\$3"
  // amd64:`ADDQ\t\$3`

and they'll match this assembly line:

  ADDQ	$3

Negative matches can be specified using a - before the quoted regexp.
For example:

  func MoveSmall() {
  	   x := [...]byte{1, 2, 3, 4, 5, 6, 7}
  	   copy(x[1:], x[:]) // arm64:-".*memmove"
  }

verifies that NO memmove call is present in the assembly generated for
the copy() line.


- Architecture specifiers

There are three different ways to specify on which architecture a test
should be run:

* Specify only the architecture (eg: "amd64"). This indicates that the
  check should be run on all the supported architecture variants. For
  instance, arm checks will be run against all supported GOARM
  variations (5,6,7).
* Specify both the architecture and a variant, separated by a slash
  (eg: "arm/7"). This means that the check will be run only on that
  specific variant.
* Specify the operating system, the architecture and the variant,
  separated by slashes (eg: "plan9/386/sse2", "plan9/amd64/"). This is
  needed in the rare case that you need to do a codegen test affected
  by a specific operating system; by default, tests are compiled only
  targeting linux.


- Remarks, and Caveats

-- Write small test functions

As a general guideline, test functions should be small, to avoid
possible interactions between unrelated lines of code that may be
introduced, for example, by the compiler's optimization passes.

Any given line of Go code could get assigned more instructions that it
may appear from reading the source. In particular, matching all MOV
instructions should be avoided; the compiler may add them for
unrelated reasons and this may render the test ineffective.

-- Line matching logic

Regexps are always matched from the start of the instructions line.
This means, for example, that the "MULQ" regexp is equivalent to
"^MULQ" (^ representing the start of the line), and it will NOT match
the following assembly line:

  IMULQ	$99, AX

To force a match at any point of the line, ".*MULQ" should be used.

For the same reason, a negative regexp like -"memmove" is not enough
to make sure that no memmove call is included in the assembly. A
memmove call looks like this:

  CALL	runtime.memmove(SB)

To make sure that the "memmove" symbol does not appear anywhere in the
assembly, the negative regexp to be used is -".*memmove".