Repository navigation
[AUTOMATED] fix(p4): an argument moved back into its own register is a returned half - #797
Merged
Merged
Conversation
mahaloz
force-pushed
the
fix/own-register-return
branch
from
October 1, 2026 20:32
40209b9 to
daec3be
Compare
…a returned half
A function that keeps an argument in a callee-saved register across a call
and moves it back into its own argument register to return it (ARM
`mov r4,r1; bl ext; mov r1,r4`) lost that half of its returned pair and the
parameter with it: `unsigned long long own(unsigned a, unsigned b)
{ ext(); return (u64)b << 32 | a; }` printed `unsigned int own(unsigned int a0)`.
After copy propagation the half reaches the RETURN as the caller's own
register, which the pair repair's placement test reads as leftover.
Return recovery still sees the moves, so it now records each used return
register whose value at some RETURN walks back to its own input through
another register, and the pair repair skips the placement test for that
register at every RETURN. A move made by an instruction that also writes the
stack pointer (SPARC save/restore, a pop) or an incidental copy (Xtensa's
call8 window swap) does not count, so the GH-6990 SPARC pass-through and a
merely preserved register still read as leftover. The rule lives inside
retinputhalf and is off with it.
Tests: stage test kuna-ownreturn (1/6 on main, 6/6 after), unit tests for
the walk, and kuna-cli own_register_returns, which compiles the printed ARM
and Thumb functions back and compares them with the source and checks the
AArch64 and x86-64 128-bit pairs.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017cAUeUBnQ9xRA9oRJNGWjw
… a call or svc after the move ends it A high register moved back while the low one is the function's untouched argument now keeps the pair instead of returning the high word alone: the zero-shift path of compiler-rt's __ashrdi3 on RISC-V 32 (`mv a3,a1` at entry, `mv a1,a3; ret`) printed `return a1;`, and __ashrti3 on AArch64 and RISC-V 64 `v3 = a1;`. Any other low half takes the strict rule's answer. A move back that a call or a CALLOTHER can follow on a path to the RETURN no longer counts: an inline svc/ecall/sc reads its arguments from registers its p-code does not show, so `r1 = b; svc 0` in an int function turned it into a pair return on ARM, Thumb, AArch64, RISC-V and PowerPC. Tests: kuna-ownreturn gains sys2nr (svc control) and a hand-written __ashrdi3 shape (10/10; the previous head 6/10, main 3/10), unit tests for the call barrier, and own_register_returns round-trips the RISC-V 32 shift through gcc and clang. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…d base Re-recorded on origin/main 30616a8 plus this branch: the 10 OWNRETURN keys are the only additions, none removed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz
force-pushed
the
fix/own-register-return
branch
from
October 1, 2026 22:58
daec3be to
8065612
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
A function that keeps an argument in a callee-saved register across a call and moves it back into its own argument register to return it loses that half of the return value, and the argument drops out of the prototype.
The same happens on Thumb, RISC-V and PowerPC, and to 128-bit pairs on x86-64 and AArch64. A leaf function hits it too. compiler-rt's
__ashrdi3on RISC-V 32 copies the high word aside at entry, and its zero-shift path returnsawithmv a1,a3; ret. kuna printsreturn a0;there, so the high word is lost.The fix
save/restore, apop), an incidental copy (Xtensacall8), and a move that a call or CALLOTHER can follow on its way to the RETURN. The last covers an inlinesvc/ecall/sc, whose p-code reads no registers, soint sys2nr(int a, int b) { ext(); r0 = a; r1 = b; svc 0; return r0 + 1; }staysint. The case where the arguments land in different registers is older and is filed as [AUTOMATED] An inline svc/ecall/sc whose arguments sit in the return registers comes back as a register-pair return #812.__ashrdi3now printsreturn CONCAT44(a1,a0);). Otherwise the strict rule's answer stands.retinputhalfand turns off with it. There is no new option.swapdon PowerPC and armeb printsCONCAT44(a0,a1), and the big-endian output is identical to [AUTOMATED] fix(p4): a value returned in two registers came back with its halves swapped on big-endian targets and AVR (option bejoin) #763 alone.Tests
tests/stages/kuna-ownreturn.xml(ARM) passes 3 of 10 assertions on main and 10 of 10 with the fix. It adds ansvccontrol (sys2nrstaysint4 sys2nr(int4 a0)) and a hand-written__ashrdi3shape.kuna-cli/tests/own_register_returns.rscompiles the printed ARM, Thumb and RISC-V 32 code with gcc and clang at -O0 and -O2 and runs it against the source. The RISC-V 32 case fails on main (return a0;).__umoddi3(ARM),__ashrdi3(RISC-V 32),__ashrti3(AArch64). 18 decbench ARM firmware images (26,448 functions) change none.decompile-all, interleaved min-of-11, 37.40 s on main and 36.97 s here.Fixes #770
🤖 Generated with Claude Code
https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU