Skip to content

[AUTOMATED] ARM: keep a constant returned through a conditional return beside a call (condexeretuse) - #948

Merged
mahaloz merged 6 commits into
mainfrom
fix/fix874
Oct 6, 2026
Merged

mahaloz merged 6 commits into
mainfrom
fix/fix874

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

On ARM, a function that returns a constant through a conditional return (bxeq lr, popeq {...,pc}) and otherwise tail-calls or returns a call's result prints as void. Run against main:

cat > condret.c <<'EOF2'
int g;
__attribute__((noinline)) int getv(void) { return g * 3; }
int pick(int a) { if (a) return getv(); return 7; }
EOF2
clang --target=armv7-linux-gnueabihf -O2 -c condret.c -o condret.o
kuna decompile-all condret.o
void pick(int a0)
{
  if (a0) {
    getv(a0); // tail-call
    return;
  }
}

With this change it prints int pick(int a0) { if (a0) return getv(a0); return 7; }.

The fix

  • pick is cmp r0,#0; moveq r0,#7; bxeq lr; b getv. The bxeq block merges the two arms of the moveq and branches on Z again, so in the graph the 7 reaches both the RETURN and the tail call's r0, though the execution that writes it always returns. Return recovery scores r0 before ActionConditionalExe removes that merge, and the sole-use check rejects it for the call.
  • New option condexeretuse (default on, P4). When the sole-use walk for a RETURN fails after passing through such a merge, it is repeated once, skipping the uses the merge's forced branch cannot reach. Two merges in a row (a 64-bit moveq r0,#7; moveq r1,#0; bxeq lr) are followed.
  • Uses the value can reach still reject it: a second test on another register, a store on the returning path, an inverted return. The repeated walk must also still reach the RETURN, so a 7 sent into a spin loop or a call that does not return (moveq r0,#7; beq spin / bleq exit) leaves the function void. A value reached both through such a merge and another way keeps the old answer.
  • The extra getv(a0) argument comes from the same impossible path on the call side. It is left alone here: rescoring it would also drop the real argument from if (a) return getw(a); to an unprototyped callee.

Tests

  • tests/stages/kuna-condexeretuse.xml: ARM bxeq, 64-bit pair, popeq, and a Thumb itt eq block, plus six shapes that must stay void (including the spin loop and bleq exit in ARM and Thumb). Pass 1 (option off) is the bug; the file fails on main. Unit tests cover the forced out block (and its complement), the reachability cap, and the forced-away case.
  • decompile-all before/after: 13 decbench ARM firmware images (O0/O2) change 3 functions, all u-boot EFI helpers that now return their status (efi_locate_handle_buffer_int returns 0x80000002 on a null argument, as the source does). 120 u-boot objects rebuilt with clang for ARM and Thumb change only ubi_major2num, which now returns -19 or the device number. 96 ARM/AArch64 fixtures and 8 x86-64 binaries are unchanged.
  • Speed, decompile-all min of 11 interleaved runs: total +0.6% (gzip 0.0%, kmod -1.3%, grep -O0 +1.4%, ls +0.7%, nuttx +1.3%, chibios +2.3%), within run-to-run noise. The second walk only runs when the first one fails after passing such a merge.

Fixes #874
Part of #890

🤖 Generated with Claude Code

https://claude.ai/code/session_01Qihrih8NnnexavqvZxp8CF

@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Oct 6, 2026
mahaloz and others added 6 commits October 6, 2026 06:53
… scores

[AUTOMATED]

A value whose forced branch leads away from the RETURN (a spin loop, a call
that does not return) never reaches it; skipping every use it cannot reach
passed the walk with nothing checked. Adds the spin / bleq exit stage cases,
unit tests for forced_out_block, the reachability cap and the forced-away
case, and re-derives the counts on the rebased base.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Qihrih8NnnexavqvZxp8CF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[AUTOMATED] ARM: a constant returned through bxeq lr beside a tail call is lost and the call gains an argument

1 participant