mirror of
https://github.com/golang/go
synced 2024-11-16 21:44:52 -07:00
8fc043ccfa
The compiler is currently sign extending 32 bit signed integers to 64 bits before right shifting them using a 64 bit shift instruction. There's no need to do this as RISC-V has instructions for right shifting 32 bit signed values (sraw and sraiw) which sign extend the result of the shift to 64 bits. Change the compiler so that it uses sraw and sraiw for shifts of signed 32 bit integers reducing in most cases the number of instructions needed to perform the shift. Here are some examples of code sequences that are changed by this patch: int32(a) >> 2 before: sll x5,x10,0x20 sra x10,x5,0x22 after: sraw x10,x10,0x2 int32(v) >> int(s) before: sext.w x5,x10 sltiu x6,x11,64 add x6,x6,-1 or x6,x11,x6 sra x10,x5,x6 after: sltiu x5,x11,32 add x5,x5,-1 or x5,x11,x5 sraw x10,x10,x5 int32(v) >> (int(s) & 31) before: sext.w x5,x10 and x6,x11,63 sra x10,x5,x6 after: and x5,x11,31 sraw x10,x10,x5 int32(100) >> int(a) before: bltz x10,<target address calls runtime.panicshift> sltiu x5,x10,64 add x5,x5,-1 or x5,x10,x5 li x6,100 sra x10,x6,x5 after: bltz x10,<target address calls runtime.panicshift> sltiu x5,x10,32 add x5,x5,-1 or x5,x10,x5 li x6,100 sraw x10,x6,x5 int32(v) >> (int(s) & 63) before: sext.w x5,x10 and x6,x11,63 sra x10,x5,x6 after: and x5,x11,63 sltiu x6,x5,32 add x6,x6,-1 or x5,x5,x6 sraw x10,x10,x5 In most cases we eliminate one instruction. In the case where we shift a int32 constant by a variable the number of instructions generated is identical. A sra is simply replaced by a sraw. In the unusual case where we shift right by a variable anded with a constant > 31 but < 64, we generate two additional instructions. As this is an unusual case we do not try to optimize for it. Some improvements can be seen in some of the existing benchmarks, notably in the utf8 package which performs right shifts of runes which are signed 32 bit integers. | utf8-old | utf8-new | | sec/op | sec/op vs base | EncodeASCIIRune-4 17.68n ± 0% 17.67n ± 0% ~ (p=0.312 n=10) EncodeJapaneseRune-4 35.34n ± 0% 34.53n ± 1% -2.31% (p=0.000 n=10) AppendASCIIRune-4 3.213n ± 0% 3.213n ± 0% ~ (p=0.318 n=10) AppendJapaneseRune-4 36.14n ± 0% 35.35n ± 0% -2.19% (p=0.000 n=10) DecodeASCIIRune-4 28.11n ± 0% 27.36n ± 0% -2.69% (p=0.000 n=10) DecodeJapaneseRune-4 38.55n ± 0% 38.58n ± 0% ~ (p=0.612 n=10) Change-Id: I60a91cbede9ce65597571c7b7dd9943eeb8d3cc2 Reviewed-on: https://go-review.googlesource.com/c/go/+/535115 Run-TryBot: Joel Sing <joel@sing.id.au> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Joel Sing <joel@sing.id.au> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: M Zhuo <mzh@golangcn.org> Reviewed-by: David Chase <drchase@google.com> |
||
---|---|---|
.. | ||
addrcalc.go | ||
alloc.go | ||
arithmetic.go | ||
bitfield.go | ||
bits.go | ||
bmi.go | ||
bool.go | ||
clobberdead.go | ||
clobberdeadreg.go | ||
compare_and_branch.go | ||
comparisons.go | ||
condmove.go | ||
constants.go | ||
copy.go | ||
floats.go | ||
fuse.go | ||
ifaces.go | ||
issue22703.go | ||
issue25378.go | ||
issue31618.go | ||
issue33580.go | ||
issue38554.go | ||
issue42610.go | ||
issue48054.go | ||
issue52635.go | ||
issue54467.go | ||
issue56440.go | ||
issue58166.go | ||
issue60324.go | ||
issue60673.go | ||
issue61356.go | ||
logic.go | ||
mapaccess.go | ||
maps.go | ||
math.go | ||
mathbits.go | ||
memcombine.go | ||
memops_bigoffset.go | ||
memops.go | ||
noextend.go | ||
race.go | ||
README | ||
regabi_regalloc.go | ||
retpoline.go | ||
rotate.go | ||
select.go | ||
shift.go | ||
shortcircuit.go | ||
slices.go | ||
smallintiface.go | ||
spectre.go | ||
stack.go | ||
strings.go | ||
structs.go | ||
switch.go | ||
writebarrier.go | ||
zerosize.go |
// 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 an action within the GOROOT/test test 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, and only on GOOS=linux. To perform comprehensive tests for all the supported architectures (even on a non-Linux system), one can run the following command: $ ../../bin/go test cmd/internal/testdir -run='Test/codegen' -all_codegen -v 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 the test. 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-running 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 than 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".