5576 Commits

Author SHA1 Message Date
Jorropo
2bb808bfc2 cmd/compile: add Trunc support to known bits
Uniqued by LOC this adds 3 known bits hits when building the std.

Updates #78633

Change-Id: I44f2ae1ed31f798d7881691729cce5d9707e8edc
Reviewed-on: https://go-review.googlesource.com/c/go/+/766120
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
2026-04-23 08:23:47 -07:00
Jorropo
e133fb1569 cmd/compile: add Rsh support to known bits
I havn't searched a real world usecase for this but it seems
fine for completeness.

This doesn't have any hits in the std.

Updates #78633

Change-Id: If7247b7578fe6518aa50a7a7fe71b7901d46aab5
Reviewed-on: https://go-review.googlesource.com/c/go/+/766081
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
2026-04-23 08:23:42 -07:00
Jorropo
767140eff2 cmd/compile: add RshU support to known bits
I havn't searched a real world usecase for this but it seems
fine for completeness.

This doesn't have any hits in the std.

Updates #78633

Change-Id: Id69a3c9ccf945f0d91dac6618afe7dfaf1af8da8
Reviewed-on: https://go-review.googlesource.com/c/go/+/766080
Reviewed-by: Mark Freeman <markfreeman@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Carlos Amedee <carlos@golang.org>
2026-04-23 08:23:40 -07:00
Jorropo
977041b065 cmd/compile: add Lsh support to known bits
I've got a bit carried away with the exhaustive implementation.

I have one private real world code where adding support for
x << aConst to knownBits helps the compilation output.

However the « you can just think about it like a phi of all the shift
amounts » looked pretty easy to implement and it worked first try so
here it is.

Uniqued by LOC this adds 5 known bits hits when building the std.

Updates #78633

Change-Id: I3c4cafe9907004296129a07b2d13086660747a3d
Reviewed-on: https://go-review.googlesource.com/c/go/+/766042
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
2026-04-23 08:23:37 -07:00
Jorropo
9c0a8a2b46 cmd/compile: add Neq support to known bits
When known bits analyze an Neq if it finds any bit that is known
in both sides but different, it also knows the neq will always be true.

Uniqued by LOC this adds 7 known bits hits when building the std.

Updates #78633

Change-Id: I3ec907e2def9d365e6360b6c81dde47920c62bf4
Reviewed-on: https://go-review.googlesource.com/c/go/+/766040
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-04-23 08:23:35 -07:00
Jorropo
8963c303b4 cmd/compile: add Sext support to known bits
This doesn't have any hits in the std.

Updates #78633

Change-Id: I965838f876e90189f546355093997d64ec13cb35
Reviewed-on: https://go-review.googlesource.com/c/go/+/765802
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2026-04-23 08:23:26 -07:00
Jorropo
d75902b195 cmd/compile: add CvtBoolToUint8 support to known bits
This doesn't have any hits in the std.

Updates #78633

Change-Id: I12d1d5c224b5cd6e45fcfbe9680d2fd89ff99e4d
Reviewed-on: https://go-review.googlesource.com/c/go/+/765801
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
2026-04-23 08:23:24 -07:00
Jorropo
1ad012aa6b cmd/compile: add Zext support to known bits
Uniqued by LOC this adds 79 known bits hits when building the std.

Updates #78633

Change-Id: Ib555a49834f848a887d2acea20e89b45613eea30
Reviewed-on: https://go-review.googlesource.com/c/go/+/765800
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-04-23 08:23:21 -07:00
Jorropo
b9e1876c11 cmd/compile: add first boolean and Eq support to known bits
When known bits analyze an Eq if it finds any bit that is known
in both sides but different, it also knows the eq will always be false.

Uniqued by LOC this adds 119 known bits hits when building the std.

Updates #78633

Change-Id: I8299f0ad94383b21aec0cd3794d2f851c1260cef
Reviewed-on: https://go-review.googlesource.com/c/go/+/765780
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
2026-04-23 08:23:17 -07:00
Jorropo
7a8dcab743 cmd/compile: add known bits pass
This pass performs bitwise constant folding.

It's main goal is to optimize bitfields like generated by defer.

You might have 3 defers in a function and the middle one is always taken,
previously we couldn't remove the branch for it, this pass is able to do so.

This is hit 93 times uniqued by LOC when building the std.

My first thought was to implement this as parts of the limits code.
However the way limits allows to set knownBits tighter and vice-versa
means the code complexity between the two is multiplicative.
Thus I have avoided this, someone might change it in the future
but I don't have a good usecase now and this simple pass is sufficient.

I have tried multiple places for the pass,
we need it before any opt (here late opt) since we need the generic rules
to optimize any user of a constant folded value.

We also want one run of known bits after prove since prove removing some
never / always taken branches allows known bits to do a better job.

This yields real optimizations when you have a defer inside an always
taken branch.

I've thought prove might do a better job if some branches were removed by
running an early known bits first.
However after trying it, this never helped.

I am sure you can build an example where this becomes true, but at least
in the code I've looked at it didn't help.
Thus I decided against running known bits twice (before and after prove).

Fixes #78633

Change-Id: I90a46875cc11d5d26367f00ac83c29fed433cb6d
Reviewed-on: https://go-review.googlesource.com/c/go/+/765560
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-04-23 08:17:49 -07:00
Alexander Musman
13cab13f78 cmd/compile: recognize OpVarDef and OpZero in cse isMemDef
recognize OpVarDef (width 0, always skippable) and OpZero (similar to
OpStore, it should be disjoint), so these ops do not prevent loads cse.

This change slightly improves code size:

Executable           Base .text linux_arm64     Change
----------------------------------------------------
asm                     2133284     2132900     -0.02%
cgo                     1742996     1742868     -0.01%
compile                10567620    10566852     -0.01%
cover                   1906740     1906100     -0.03%
fix                     3131284     3131012     -0.01%
link                    2667604     2667076     -0.02%
preprofile               877908      877876     -0.00%
vet                     3010372     3010084     -0.01%

Change-Id: I428f73008a817d0e302d438c020504c560ae1653
Reviewed-on: https://go-review.googlesource.com/c/go/+/769000
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
2026-04-23 08:12:54 -07:00
khr@golang.org
9c688e3f4d cmd/compile: don't lift divide instructions out of loops
We can't lift things that might panic out of loops.
(In particular, for divides, out from underneath their divisor==0 check.)

Technically, it's not "things that might panic", but "instructions
that might fault". We have to ensure that we issue those instructions
only after we've checked their preconditions. It would be great if
there was an integer divide instruction that didn't fault on
divisor==0. Then it would be ok to lift it, as we already guarantee
we don't use the result. (I'm presuming here all the archs we support
only have faulting integer divides. If we have nonfaulting divides,
we could use them and revert this CL for that arch.)

Fixes #78892

Change-Id: Ie09d1f3d28ae320f6835730c2debcf0dd3df6a04
Reviewed-on: https://go-review.googlesource.com/c/go/+/769700
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-04-22 06:46:27 -07:00
Derek Parker
b77ae50ad1 cmd/compile: improve filtering in preInliningDcls
Debug var filtering was unified, however that updated filtering did not
make it into preInliningDcls. Add the check there for consistent DWARF
variable filtering.

Fixes #78599

Change-Id: I94bd354bb92b584837efdd6aaa4a8ffe49e44d50
Reviewed-on: https://go-review.googlesource.com/c/go/+/764720
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
2026-04-15 10:55:10 -07:00
Mark Freeman
dd8efe4b96 cmd/compile/interna/noder: correct generic method pointer receivers
Damien reported a bug whereby we were shaping qualified identifiers
for generic methods with pointer receivers as T.m[...]. The type for
the pointer receiver is encoded as (*T), and when the method name is
appended, it looks for (*T).m[...], finding nothing.

This updates qualified identifiers for generic methods to recognize
pointer receivers as a separate format.

Change-Id: Iecee9bdd45e3d99400ae61f96dc28fa892511bb7
Reviewed-on: https://go-review.googlesource.com/c/go/+/765600
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
2026-04-14 09:10:26 -07:00
Cuong Manh Le
e7c75c3ae8 cmd/compile: handle min integer step in loop
Since negating min int will overflows back to itself, causing a panic
inside subWillUnderflow check.

Fixes #78641

Change-Id: Ibbf2fa3228b9890a1a76ac6f4ff504b7e125b29f
Reviewed-on: https://go-review.googlesource.com/c/go/+/766260
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
2026-04-13 11:56:30 -07:00
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: 23f8296ece08c77fcaeeaf59c2c2d8ce23d1202c
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
Mark Freeman
b7db5702b0 cmd/compile/internal/noder: revert to UIR V3
Upgrading to V4 early doesn't seem to make much sense while we refine
things. Any change to encoding would require another version gate and
propagation; this gets tedious fast.

Instead, let's keep on V3 and test locally with V4. We can commit
skipped tests as we refine and then turn them all on *with* the bump
up to V4.

Change-Id: I571715e5da75095612f68b224c82e6d22ad3ab25
Reviewed-on: https://go-review.googlesource.com/c/go/+/765444
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Mark Freeman <markfreeman@google.com>
2026-04-10 14:20:24 -07:00
Mark Freeman
8624133024 test: more tests for generic methods
These tests verify that generic method type arguments are ordered
correctly for a variety of expression forms and dictionary kinds.

Change-Id: I067206d1e97d5350d6fcc3983f5a6ef24a782214
Reviewed-on: https://go-review.googlesource.com/c/go/+/764200
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Mark Freeman <markfreeman@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-04-09 08:50:41 -07:00
qmuntal
c4cb9a90f6 cmd/internal/tesdir: fix Test/codegen/switch on loong64
loong64 uses ALSLV the compute the lookup index.

Fixes #78575

Change-Id: Ied90a4f811cc19ffec4d304333546d1fa430ccc0
Reviewed-on: https://go-review.googlesource.com/c/go/+/764180
Reviewed-by: abner chenc <chenguoqi@loongson.cn>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
2026-04-09 05:10:28 -07:00
Jorropo
455d4f41fb cmd/compile: run CondSelect into math rules on all arches
Fixes #78558

I've also added tests to make sure PPC still generate ISEL when
the constant isn't 1.
This is to make sure we aren't generating a sequence that wouldn't
work right now.

But it does not mean we couldn't try to optimize other constants
on PPC64 if a fast sequence exists; for example like arm64's
inline register shifts.

Change-Id: Ic241d593149b7a11533948f5d4c52db357cc134f
Reviewed-on: https://go-review.googlesource.com/c/go/+/763340
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jayanth Krishnamurthy <jayanth.krishnamurthy@ibm.com>
Reviewed-by: Paul Murphy <paumurph@redhat.com>
2026-04-09 04:42:54 -07:00
Melnikov Denis
996b985008 cmd/compile: improve stp merging for non-sequent cases
Original algorithm merges stores with the first
mergeable store in the chain, but it misses some
cases. Additionally, creating list of STs, which
store data to adjacent memory cells allows merging them
according to the direction of increase of their addresses.

I have already tried another algorithm in CL 698097,
but it was reverted. This algorithm works differently
and fixes bug, generated by variant from another CL.

Fixes #71987, #75365

There are the results of sweet benchmarks
                      │  base.stat  │              opt.stat              │
                       │   sec/op    │   sec/op     vs base               │
ESBuildThreeJS-4          1.088 ± 2%    1.086 ± 1%       ~ (p=1.000 n=10)
ESBuildRomeTS-4          263.0m ± 2%   260.8m ± 1%       ~ (p=0.105 n=10)
EtcdPut-4                73.08m ± 1%   73.16m ± 1%       ~ (p=0.971 n=10)
EtcdSTM-4                414.9m ± 1%   415.4m ± 1%       ~ (p=0.393 n=10)
GoBuildKubelet-4          203.3 ± 0%    203.5 ± 0%       ~ (p=0.393 n=10)
GoBuildKubeletLink-4      19.06 ± 1%    19.05 ± 0%       ~ (p=0.280 n=10)
GoBuildIstioctl-4         156.6 ± 0%    156.6 ± 0%       ~ (p=0.796 n=10)
GoBuildIstioctlLink-4     14.16 ± 1%    14.18 ± 1%       ~ (p=0.853 n=10)
GoBuildFrontend-4         56.45 ± 1%    56.57 ± 0%       ~ (p=0.579 n=10)
GoBuildFrontendLink-4     3.635 ± 1%    3.646 ± 0%       ~ (p=0.436 n=10)
GoBuildTsgo-4             103.0 ± 1%    103.4 ± 1%       ~ (p=0.529 n=10)
GoBuildTsgoLink-4         1.865 ± 1%    1.860 ± 1%       ~ (p=0.684 n=10)
GopherLuaKNucleotide-4    33.55 ± 0%    33.58 ± 0%       ~ (p=0.075 n=10)
MarkdownRenderXHTML-4    281.0m ± 0%   280.3m ± 0%  -0.23% (p=0.019 n=10)
Tile38QueryLoad-4        970.0µ ± 1%   969.3µ ± 0%       ~ (p=0.436 n=10)
geomean                   3.128         3.128       -0.01%

Change-Id: Ia548b43601b1bdb1c1723d300a4b8b907ab0c040
Reviewed-on: https://go-review.googlesource.com/c/go/+/760100
Reviewed-by: Mark Freeman <markfreeman@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@golang.org>
2026-04-08 14:21:41 -07:00
Junyang Shao
f5b77a7e2f cmd/compile: fix loopbce overflow check logic
addWillOverflow and subWillOverflow has an implicit assumption that y is
positive, using it outside of addU and subU is really incorrect. This CL
fixes those incorrect usage to use the correct logic in place.

Thanks to Jakub Ciolek for reporting this issue.

Fixes #78333
Fixes CVE-2026-27143

Change-Id: I263e8e7ac227e2a68109eb7bbd45f66569ed22ec
Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/3700
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Neal Patel <nealpatel@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/763765
Reviewed-by: Jakub Ciolek <jakub@ciolek.dev>
Reviewed-by: Russ Cox <rsc@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: David Chase <drchase@google.com>
2026-04-08 05:23:56 -07:00
qmuntal
6797caf71a cmd/compile: support all constant return types in switch lookup tables
Lookup tables for switch statements can be generalized to also support
bools, strings, floats, and complex numbers.

Change-Id: Ic3ece41fe2009050fbf08ba6f06ea8a567407974
Reviewed-on: https://go-review.googlesource.com/c/go/+/763320
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
2026-04-08 03:47:21 -07:00
Mark Freeman
6f662188c2 test: add a simple test using generic methods
Change-Id: Ia22aff4d7b6d6f9250c3915fba947fcc3bd800d5
Reviewed-on: https://go-review.googlesource.com/c/go/+/763180
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-04-07 13:49:28 -07:00
gulmix
3b5954c634 cmd/compile: report error instead of ICE for oversized map element
When a map element or key type is 2GB or larger, the compiler crashed
with "internal compiler error: map elem too big" in reflectdata.ZeroAddr.

Add a size check in types.CalcSize for TMAP, similar to the existing
check for channel element types, so that oversized map elements are
reported as a normal error before reaching ZeroAddr.

Fixes #78355

Change-Id: I281a5f0ec7dda6dac084787859156d4409dc860c
Reviewed-on: https://go-review.googlesource.com/c/go/+/760600
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-04-06 19:36:35 -07:00
Jorropo
a93560b70a cmd/compile: optimize CondSelect to math on arm64 with inline register shifts
Change-Id: I27696b1a5fa0593d9f36743efa3559a36d23ec4b
Reviewed-on: https://go-review.googlesource.com/c/go/+/760844
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
2026-04-06 19:34:25 -07:00
Jorropo
666e8c59c9 cmd/compile: improve Mul to Left Shift rules
- fix a bug where it wouldn't recognize 1<<63 as a power of two
- remove the IsSigned check; there is no such thing as a signed Mul
  If the rule works for signed numbers it works for unsigned ones too.
  Even if the intermediary steps makes no sense, it ends up wrapping
  the right way around in the end.

Change-Id: I86182762aec5eff784e2d9bc49ee028825fb9ea0
Reviewed-on: https://go-review.googlesource.com/c/go/+/760843
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
2026-04-06 19:34:21 -07:00
Jorropo
68ee544e87 cmd/compile: extend condselect into math code to handle other constants than 1
On amd64 along:
  if b { x += 1 } => x += b

We can also implement constants 2 4 and 8:
  if b { x += 2 } => x += b * 2

This compiles to a displacement LEA.

Change-Id: Ib00fcc5059acb0ebb346e056c4a656f164cc63df
Reviewed-on: https://go-review.googlesource.com/c/go/+/760841
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
2026-04-06 10:04:56 -07:00
Jayanth Krishnamurthy jayanth.krishnamurthy@ibm.com
d74de3ce79 cmd/compile: improve uint8/uint16 logical immediates on PPC64
Logical ops on uint8/uint16 (AND/OR/XOR) with constants sometimes
materialized the mask via MOVD (often as a negative immediate), even
when the value fit in the UI-immediate range. This prevented the backend
from selecting andi. / ori / xori forms.

This CL makes:
UI-immediate truncation is performed only at the use-site of
logical-immediate ops, and only when the constant does not fit in the
8- or 16-bit unsigned domain (m != uint8(m) / m != uint16(m)).

This avoids negative-mask materialization and enables correct emission of
UI-form logical instructions. Arithmetic SI-immediate instructions (addi, subfic, etc.) and other
use-patterns are unchanged.

Codegen tests are added to ensure the expected andi./ori/xori
patterns appear and that MOVD is not emitted for valid 8/16-bit masks.

Change-Id: I9fcdf4498c4e984c7587814fb9019a75865c4a0d
Cq-Include-Trybots: luci.golang.try:gotip-linux-ppc64_power10,gotip-linux-ppc64_power8,gotip-linux-ppc64le_power8,gotip-linux-ppc64le_power9,gotip-linux-ppc64le_power10
Reviewed-on: https://go-review.googlesource.com/c/go/+/704015
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Paul Murphy <paumurph@redhat.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
2026-04-06 01:13:27 -07:00
qmuntal
2a902c8a8a cmd/compile: optimize switch statements using lookup tables
Switch statement containing integer constant cases and case bodies just
returning a constant should be optimizable to a simpler and faster table
lookup instead of a jump table.

That is, a switch like this:

    switch x {
    case 0: return 10
    case 1: return 20
    case 2: return 30
    case 3: return 40
    default: return -1
    }

Could be optimized to this:

    var table = [4]int{10, 20, 30, 40}
    if uint(x) < 4 { return table[x] }
    return -1

The resulting code is smaller and faster, especially on platforms where
jump tables are not supported.

goos: windows
goarch: arm64
pkg: cmd/compile/internal/test
                               │  .\old.txt  │              .\new.txt              │
                               │   sec/op    │   sec/op     vs base                │
SwitchLookup8Predictable-12      2.708n ± 6%   2.249n ± 5%  -16.97% (p=0.000 n=10)
SwitchLookup8Unpredictable-12    8.758n ± 7%   3.272n ± 4%  -62.65% (p=0.000 n=10)
SwitchLookup32Predictable-12     2.672n ± 5%   2.373n ± 6%  -11.21% (p=0.000 n=10)
SwitchLookup32Unpredictable-12   9.372n ± 7%   3.385n ± 6%  -63.89% (p=0.000 n=10)
geomean                          4.937n        2.772n       -43.84%

Fixes #78203

Change-Id: I74fa3d77ef618412951b2e5c3cb6ebc760ce4ff1
Reviewed-on: https://go-review.googlesource.com/c/go/+/756340
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-04-03 16:46:16 -07:00
Jorropo
0a36b58888 cmd/compile: extend all the cmov into math generic rules with their contrary
If the bool comes from a local operation this is foldable into the comparison.
  if a == b {
  } else {
    x++
  }
becomes:
  x += !(a == b)
becomes:
  x += a != b

If the bool is passed in or loaded rather than being locally computed
this adds an extra XOR ^1 to invert it.

But at worst it should make the math equal to the compute + CMP + CMOV
which is a tie on modern CPUs which can execute CMOV on all int ALUs
and a win on the cheaper or older ones which can't.

Change-Id: Idd2566c7a3826ec432ebfbba7b3898aa0db4b812
Reviewed-on: https://go-review.googlesource.com/c/go/+/760922
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
2026-04-03 13:21:37 -07:00
Robert Griesemer
238d7bddeb go/types, types2: in range-over-func, the yield function cannot be variadic
Fixes #78483.
For #78314.

Change-Id: If83983c0bf79840aa02dc0d2fa8945f5e8b4e969
Reviewed-on: https://go-review.googlesource.com/c/go/+/761682
Auto-Submit: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jakub Ciolek <jakub@ciolek.dev>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
2026-04-01 16:39:05 -07:00
Keith Randall
14a6bf0e90 test/codegen: remove unneeded commas
After CL 760780, commas aren't allowed.
But some CLs that were already in flight don't know that.

Change-Id: I31f586c87def4a9746dc2c055923fce8bad6647e
Reviewed-on: https://go-review.googlesource.com/c/go/+/761620
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
2026-03-31 12:43:09 -07:00
Keith Randall
1582ad4105 test/codegen: fix some unbalanced quotes
Change-Id: I081da8c79f0264118e079af21ff58c511ae37e6c
Reviewed-on: https://go-review.googlesource.com/c/go/+/760682
Reviewed-by: Junyang Shao <shaojunyang@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Joel Sing <joel@sing.id.au>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@google.com>
2026-03-31 11:01:20 -07:00
Keith Randall
d5b6d583c1 test/codegen: replace commas with spaces between regexps
Change-Id: Ia7a955833d761e08c1b8081fb29a2e6317de004c
Reviewed-on: https://go-review.googlesource.com/c/go/+/760681
Auto-Submit: Keith Randall <khr@google.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Paul Murphy <paumurph@redhat.com>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-03-31 11:01:16 -07:00
Keith Randall
d6492e284b test/codegen: get rid of \s
Replace \s with a space in backtick-quoted strings
Replace \\s with a space in double-quoted strings

Change-Id: I0c8b249bb12c2c8ca69e683e4bc6f27544fd6094
Reviewed-on: https://go-review.googlesource.com/c/go/+/760680
Auto-Submit: Keith Randall <khr@google.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Paul Murphy <paumurph@redhat.com>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-03-31 11:01:13 -07:00
Keith Randall
1673075d4b test/codegen: fix broken syntax
A bunch of tests had broken yet undetected syntax errors
in their assembly output regexps. Things like mismatched quotes,
using ^ instead of - for negation, etc.

In addition, since CL 716060 using commas as separators between
regexps doesn't work, and ends up just silently dropping every
regexp after the comma.

Fix all these things, and add a test to make sure that we're not
silently dropping regexps on the floor.

After this CL I will do some cleanup to align with CL 716060, like
replacing commas and \s with spaces (which was the point of that CL,
but wasn't consistently rewritten everywhere).

Change-Id: I54f226120a311ead0c6c62eaf5d152ceed106034
Reviewed-on: https://go-review.googlesource.com/c/go/+/760521
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Paul Murphy <paumurph@redhat.com>
Auto-Submit: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-03-31 11:01:09 -07:00
Cuong Manh Le
f665ff8bc1 cmd/compile: fix wrong label loop during rangefunc rewrite
During the rangefunc rewrite, the compiler must correctly identify
the target of branch statements. When a label is defined within a
nested scope - such as inside a function literal or a closure - it
can shadow a label with the same name in the outer scope.

If the rewrite logic does not account for this shadowing, it may
incorrectly associate a branch with a nested label rather than the
intended loop label. Since the typechecker already guarantees that
labels are unique within their respective scopes, any duplicate label
name encountered must belong to a nested scope. These should be
skipped to ensure branch computing correctly targets the current
range-loop scope.

Fixes #78408

Change-Id: I4dce8a4d956f41b3a717a509f8c3f7478720be9f
Reviewed-on: https://go-review.googlesource.com/c/go/+/761420
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jakub Ciolek <jakub@ciolek.dev>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Keith Randall <khr@google.com>
2026-03-31 10:58:57 -07:00
Cuong Manh Le
9002bd9fa1 cmd/compile: ensure map/slice clearing expressions are walked
The order pass ensures that initialization operations for clear(expr)
are scheduled. However, if 'expr' is a conversion that the walk pass
subsequently optimizes away or transforms, the resulting nodes can
be left in an un-walked state.

These un-walked nodes reach the SSA backend, which does not expect
high-level IR, resulting in an ICE.

This change ensures the expression is always walked during the
transformation of the 'clear' builtin.

Fixes #78410

Change-Id: I1997a28af020f39b2d325a58429eff9495048b1f
Reviewed-on: https://go-review.googlesource.com/c/go/+/760981
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2026-03-31 10:58:53 -07:00
Jorropo
d9fbe4c90d cmd/compile: convert some condmoves in XOR
Similar to CL 685676 but for XOR.

Change-Id: Ib5ffd4c13348f176a808b3218fdbbafc2c42794f
Reviewed-on: https://go-review.googlesource.com/c/go/+/760921
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2026-03-31 10:58:48 -07:00
Jorropo
de7f006df2 cmd/compile: convert some condmoves in OR
Similar to CL 685676 but for OR.

Change-Id: I0ddfd457ed9e8888462306138a251ac48ad42084
Reviewed-on: https://go-review.googlesource.com/c/go/+/760920
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
2026-03-31 10:58:45 -07:00
Cuong Manh Le
1f8aff4386 simd/archsimd: mark pa* unsafe helpers as nocheckptr
Despite the cast from smaller to bigger slice, the elements after the
input slice length are not actually read. Thus they should have
nocheckptr annotation to make checkptr happy.

Fixes #78413

Change-Id: I3e27eab8a69b37bb11632b130a87e6625e06e8ac
Reviewed-on: https://go-review.googlesource.com/c/go/+/761120
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2026-03-30 21:19:14 -07:00
Mateusz Poliwczak
09031d907c cmd/compile/internal/devirtualize: use pointer identity for type comparison
Fixes #78404

Change-Id: I6adc1fb42ad6a3acce21333c6819d0796a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/760161
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
2026-03-30 07:27:52 -07:00
Keith Randall
1fd68799c3 test/codegen: check mips64 sqrt/abs code only for hardfloat
Softfloat doesn't use the hardware instructions.

Followon to CL 739520 + CL 757300

Change-Id: Ic271cd5567c62933d2d0c01d8834f9bf07e31061
Reviewed-on: https://go-review.googlesource.com/c/go/+/760520
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Julian Zhu <jz531210@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2026-03-27 21:32:35 -07:00
Jorropo
47c0cd9929 test/codegen: remove noop Ands from test cases
Theses test cases search for an AND that gets
optimized away after CL 760307.

Should help to fix riscv64 (coudn't check as
builders don't appear on https://build.golang.org/ )
and loong64 CI on master.

Change-Id: I57e4e5ab7d3003f239355137472585e46493d8dc
Reviewed-on: https://go-review.googlesource.com/c/go/+/760640
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
2026-03-27 21:20:13 -07:00
Paul Murphy
880f126233 test/codegen: fix ppc64x rldicl bit test
The prove pass removes superfluous bit masking. This was meant to
test the edge cases of the ppc64 folding rules which are exactly
the cases the prove pass now removes.

Fixes #78403

Change-Id: I45eeac58e01b42e19b8a06bb0d7af96c616ccbff
Reviewed-on: https://go-review.googlesource.com/c/go/+/760307
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Paul Murphy <paumurph@redhat.com>
Reviewed-by: Jorropo <jorropo.pgm@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2026-03-27 18:07:27 -07:00
Youlin Feng
faeffecf86 cmd/compile/internal/ssa: prove support induction variable pair
We have two induction variables i and j in the following loop:

for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
    // loop body
}

This CL enables the prove pass to handle cases where one if block
uses two induction variables.

Updates #45078

Change-Id: I8b8dc8b7b2d160a796dab1d1e29a00ef4e8e8157
Reviewed-on: https://go-review.googlesource.com/c/go/+/757700
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-03-27 06:29:34 -07:00
Jorropo
e7b0a53f31 cmd/compile: use prove to remove no-op Ors
This is hit 3 times (unique by LOC) when building the std.

Change-Id: Ic1fc7b60a129e73470d9bc4f603f4be12d154b0f
Reviewed-on: https://go-review.googlesource.com/c/go/+/750342
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2026-03-27 06:27:39 -07:00
Jorropo
2df8c060e8 cmd/compile: remove 68857 And flowLimit workaround in prove
Change-Id: Id8baeb89e6e11a01d53cd63c665f0b2966f50392
Reviewed-on: https://go-review.googlesource.com/c/go/+/750341
Reviewed-by: Mark Freeman <markfreeman@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-03-27 06:27:35 -07:00
Jorropo
33504a8696 cmd/compile: use prove to remove no-op Ands
This is hit 308 times (unique by LOC) when building the std.

There are many hits in defer generated code.

My original intent was to optimize cryptographic code that
uses And to implement modulus by a power of two but the
number is always smaller than the modulus,
it also works there but there (unsurprisingly) far fewer hits.

Change-Id: Ia7a9a57099b98de966673c6e8231ef09f7c80964
Reviewed-on: https://go-review.googlesource.com/c/go/+/750200
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2026-03-27 06:27:30 -07:00