Skip to content

cmd/compile: fold redundant zero-extension after signed load on arm64#79652

Open
gaul wants to merge 1 commit into
golang:masterfrom
gaul:redundant-zero-extension
Open

cmd/compile: fold redundant zero-extension after signed load on arm64#79652
gaul wants to merge 1 commit into
golang:masterfrom
gaul:redundant-zero-extension

Conversation

@gaul
Copy link
Copy Markdown
Contributor

@gaul gaul commented May 26, 2026

When a sign-extending load (LDRSB/LDRSH/LDRSW) is followed by a single
zero-extension, the upper bits produced by the load are immediately
overwritten and the extension is dead. The load can be switched to its
unsigned form (LDRB/LDRH/LDR) and the extension elided.

For example, code like

func F(s []int8, i int) bool { return s[i] == 0x5d }

previously generated

ldrsb x4, [x1, x3]
ubfx  x4, x4, #0, #8
cmp   w4, #0x5d

and now generates

ldrb  w4, [x1, x3]
cmp   w4, #0x5d

RISCV64 (RISCV64.rules) and MIPS (MIPS.rules) already had the equivalent
rewrite; ARM64 was missing it, including the indexed (loadidx) variants.

Found via armlint: LDRSx + zero-extension findings drop from 40 -> 1 on
gofmt and 547 -> 8 on cmd/go. Text section shrinks by 144 bytes
(0.0121%) on gofmt and 2416 bytes (0.0363%) on cmd/go.

When a sign-extending load (LDRSB/LDRSH/LDRSW) is followed by a single
zero-extension, the upper bits produced by the load are immediately
overwritten and the extension is dead. The load can be switched to its
unsigned form (LDRB/LDRH/LDR) and the extension elided.

For example, code like

	func F(s []int8, i int) bool { return s[i] == 0x5d }

previously generated

	ldrsb x4, [x1, x3]
	ubfx  x4, x4, #0, golang#8
	cmp   w4, #0x5d

and now generates

	ldrb  w4, [x1, x3]
	cmp   w4, #0x5d

RISCV64 (RISCV64.rules) and MIPS (MIPS.rules) already had the equivalent
rewrite; ARM64 was missing it, including the indexed (loadidx) variants.

Found via armlint: LDRSx + zero-extension findings drop from 40 -> 1 on
gofmt and 547 -> 8 on cmd/go. Text section shrinks by 144 bytes
(0.0121%) on gofmt and 2416 bytes (0.0363%) on cmd/go.
@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: f36ef22) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/782960.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/782960.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Keith Randall:

Patch Set 1: Auto-Submit+1 Code-Review+2 Commit-Queue+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/782960.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2026-05-26T15:25:58Z","revision":"90430747559572d0dcf26405458e5d9995c2c0f2"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/782960.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Keith Randall:

Patch Set 1: Code-Review+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/782960.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Keith Randall:

Patch Set 1: -Commit-Queue

(Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_5200>)


Please don’t reply on this GitHub thread. Visit golang.org/cl/782960.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1:

This CL has passed the run


Please don’t reply on this GitHub thread. Visit golang.org/cl/782960.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/782960.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants