go/test/codegen
Timo Friedl f42f2a3bb3 cmd/compile: add boolean absorption laws to SSA rewrite rules
The SSA generic rewrite rules implement DeMorgan's laws but are
missing the closely related boolean absorption laws:

  x & (x | y) == x
  x | (x & y) == x

These are fundamental boolean algebra identities (see
https://en.wikipedia.org/wiki/Absorption_law) that hold for all
bit patterns, all widths, signed and unsigned. Both GCC and LLVM
recognize and optimize these patterns at -O2.

Add two generic rules covering all four widths (8, 16, 32, 64).
Commutativity of AND/OR is handled automatically by the rule
engine, so all argument orderings are matched.

The rules eliminate two redundant ALU instructions per occurrence
and fire on real code (defer bit-manipulation patterns in runtime,
testing, go/parser, and third-party packages).

Fixes #78632

Change-Id: Ib59e839081302ad1635e823309d8aec768c25dcf
GitHub-Last-Rev: 23f8296ece
GitHub-Pull-Request: golang/go#78634
Reviewed-on: https://go-review.googlesource.com/c/go/+/765580
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Jorropo <jorropo.pgm@gmail.com>
2026-04-13 03:42:16 -07:00
..
addrcalc.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
alloc.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
append.go cmd/compile: introduce alias analysis and automatically free non-aliased memory after growslice 2025-11-26 19:04:05 -08:00
append_freegc.go cmd/compile: introduce alias analysis and automatically free non-aliased memory after growslice 2025-11-26 19:04:05 -08:00
arithmetic.go cmd/compile: improve Mul to Left Shift rules 2026-04-06 19:34:21 -07:00
atomics.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
bitfield.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
bits.go cmd/compile: add boolean absorption laws to SSA rewrite rules 2026-04-13 03:42:16 -07:00
bmi.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
bool.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
clobberdead.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
clobberdeadreg.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
compare_and_branch.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
comparisons.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
condmove.go cmd/compile: run CondSelect into math rules on all arches 2026-04-09 04:42:54 -07:00
constants.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
copy.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
divmod.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
floats.go test/codegen: get rid of \s 2026-03-31 11:01:13 -07:00
fuse.go cmd/compile: optimize comparisons with single bit difference 2025-11-14 10:59:56 -08:00
generics.go cmd/compile: eliminate nil checks on .dict arg 2025-09-30 11:22:35 -07:00
ifaces.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
issue22703.go test: port a nil-check interface test from asm_test 2018-03-03 20:20:54 +00:00
issue25378.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
issue31618.go cmd/compile: always mark atColumn1 results as statements 2019-04-23 17:39:11 +00:00
issue33580.go cmd/compile: reuse dead register before reusing register holding constant 2019-10-07 15:16:26 +00:00
issue38554.go cmd/compile: optimize Move with all-zero ro sym src to Zero 2020-04-24 23:58:10 +00:00
issue42610.go test/codegen: combine trivial PPC64 tests into ppc64x 2023-01-27 18:24:12 +00:00
issue48054.go test/codegen: fix package name for test case 2021-09-08 14:51:40 +00:00
issue52635.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
issue54467.go cmd/compile: use stricter rule for possible partial overlap 2022-09-27 20:09:33 +00:00
issue56440.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
issue58166.go cmd/compile: rename some test packages in codegen 2025-03-27 13:54:37 -07:00
issue59297.go test/codegen: get rid of \s 2026-03-31 11:01:13 -07:00
issue60324.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
issue60673.go test/codegen: get rid of \s 2026-03-31 11:01:13 -07:00
issue61356.go cmd/compile: get rid of special case in scheduler for entry block 2023-07-26 17:19:14 +00:00
issue63332.go cmd/compile/internal/walk: copy SSA-able variables 2023-11-21 20:34:12 +00:00
issue66585.go cmd/compile: rename some test packages in codegen 2025-03-27 13:54:37 -07:00
issue68845.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
issue69635.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
issue70409.go cmd/compile: do not treat OpLocalAddr as load in DSE 2025-02-04 12:52:01 -08:00
issue72832.go test/codegen: get rid of \s 2026-03-31 11:01:13 -07:00
issue74485.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
issue74788.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
issue75203.go test/codegen: get rid of \s 2026-03-31 11:01:13 -07:00
issue77375.go cmd/compile: fix slice bounds check elimination after function inlining 2026-02-12 09:29:09 -08:00
load_type_from_itab.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
logic.go test/codegen: get rid of \s 2026-03-31 11:01:13 -07:00
mapaccess.go cmd/compile: stop using VARKILL 2022-08-18 17:36:38 +00:00
maps.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
math.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
mathbits.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
memcombine.go cmd/compile: improve stp merging for non-sequent cases 2026-04-08 14:21:41 -07:00
memcse.go cmd/compile,internal/bytealg: add MemEq intrinsic for runtime.memequal 2025-11-26 09:58:51 -08:00
memops.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
memops_bigoffset.go test/codegen: fix some unbalanced quotes 2026-03-31 11:01:20 -07:00
moveload.go cmd/compile: forward small Load through Move to avoid redundant copies 2026-03-10 20:06:05 -07:00
multiply.go test/codegen: fix some unbalanced quotes 2026-03-31 11:01:20 -07:00
noextend.go test/codegen: get rid of \s 2026-03-31 11:01:13 -07:00
race.go cmd/compile: remove racefuncenterfp when it is not needed 2020-11-02 03:03:16 +00:00
README test/codegen: fix broken syntax 2026-03-31 11:01:09 -07:00
reflect_type.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
regabi_regalloc.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
retpoline.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
rotate.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
schedule.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
select.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
shift.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
shortcircuit.go cmd/compile: handle some additional phis in shortcircuit 2020-04-08 22:13:38 +00:00
simd.go test/codegen: get rid of \s 2026-03-31 11:01:13 -07:00
slices.go test/codegen: fix broken syntax 2026-03-31 11:01:09 -07:00
smallintiface.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
spectre.go test: migrate remaining files to go:build syntax 2023-10-19 23:33:25 +00:00
spills.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
stack.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
strings.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
structs.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
switch.go cmd/internal/tesdir: fix Test/codegen/switch on loong64 2026-04-09 05:10:28 -07:00
typeswitch.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07:00
unique.go cmd/compile/internal/ssa: eliminate string copies for calls to unique.Make 2025-05-21 20:20:31 -07:00
unsafe.go test/codegen: get rid of \s 2026-03-31 11:01:13 -07:00
writebarrier.go test/codegen: replace commas with spaces between regexps 2026-03-31 11:01:16 -07:00
zerosize.go test/codegen: simplify asmcheck pattern matching 2025-10-29 13:55:00 -07: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
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 space-separated list of regexps to be
matched. For example, the following test:

  func TZ8(n uint8) int {
  	   // amd64:"BSFL" "ORL [$]256"
  	   return bits.TrailingZeros8(n)
  }

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

Note that spaces are special - they will match any sequence of
whitespace (including tabs) in the printed assembly.

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

  // amd64:"ADDQ \\$3"
  // amd64:`ADDQ \$3`
  // amd64:"ADDQ [$]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.

The expected number of matches for the regexp can be specified using a
positive number:

  func fb(a [4]int) (r [4]int) {
  	   // amd64:2`MOVUPS[^,]+, X0$` 2`MOVUPS X0,[^\n]+$`
  	   return a
  }

- 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".