Skip to content

[AUTOMATED] Make the test that is a loop's condition the loop's exit (loopcondhoist off|auto|on, default on) - #1964

Merged
mahaloz merged 26 commits into
mainfrom
feat/loopexit
Nov 25, 2026
Merged

mahaloz merged 26 commits into
mainfrom
feat/loopexit

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

A loop that has its test and also a break or return inside often comes out as while( true ): the loop's condition is an if inside it, and the code written before the break is printed after the loop. gcc -O0 output of
for (i = 0; i < nents; i++) { if (ents[i].type & 1) cur = ents[i].name; if (i == id) { *group = cur; *name = ents[i].name; break; } }:

printf '554889e5897dec488975e0488955d848c745f800000000c745f400000000eb608b45f4489848c1e0044805201010008b0083e00185c074168b45f4489848c1e004480528101000488b00488945f88b45f43b45ec7526488b45e0488b55f84889108b45f4489848c1e004480528101000488b10488b45d8488910eb108345f4018b057a0f00003945f47c9590905dc3' | xxd -r -p > lookup.bin
kuna decompile lookup.bin 0x100000 --raw-image --target x86:LE:64:default:gcc --base 0x100000

Before:

  v1 = 0;
  while( true ) {
    if (dat_101000 <= v1)
      return;
    if (*(unsigned int *)(v1 * 0x10L + 0x101020) & 1)
      v2 = *(unsigned long *)(v1 * 0x10L + 0x101028);
    if (v1 == a0) break;
    v1 += 1;
  }
  *a1 = v2;
  *a2 = *(unsigned long *)(v1 * 0x10L + 0x101028);

After:

  for (v1 = 0; v1 < dat_101000; v1 = v1 + 1) {
    if (*(unsigned int *)(v1 * 0x10L + 0x101020) & 1)
      v2 = *(unsigned long *)(v1 * 0x10L + 0x101028);
    if (v1 == a0) {
      *a1 = v2;
      *a2 = *(unsigned long *)(v1 * 0x10L + 0x101028);
      break;
    }
  }
  • p8_structure/kuna_loopcond.rs names the block whose test is a loop's condition. In unoptimized functions it reads the layout: the compiler enters a while/for by jumping to its condition, placed after the body, so a head laid out after every latch is the condition and a head laid out first with a conditional latch is a do … while. In optimized code it takes a single conditional latch (a rotated loop, which looptest then moves back to the top), else a head test. A test whose arm only reaches a no-return call does not count.
  • CollapseStructure takes that block's exit as the loop exit and folds its returning arm last in the deferred ruleBlockIfNoExit scan; the region structurer keeps that exit edge. In unoptimized functions ActionReturnSplit no longer copies the return block for a break that the returndup auto record says shares the return after the loop.
  • This extends the existing loopcondhoist option to off|auto|on, default on (auto = unoptimized functions only). The old on skipped every loop head and was off by default. When the rule changed a choice and the structuring holds a goto, it is structured again without the rule and the version with fewer gotos is kept.
  • The loopcontinue auto rewrite that prints a recorded continue; freed the old if node while a goto could still name it; it now re-points such gotos, which a loop condition at the head made reachable (a renderer panic).
  • On the decbench corpus, against option loopcondhoist off, 65 more O2-noinline functions (66 gained, 1 lost) and 143 more O0 functions (146 gained, 3 lost) match the source's control flow exactly.

Tests: tests/stages/kuna-loopcondhoist.xml covers three gcc -O0 loops (a break that carries stores, an early return, a break to the shared return) and one gcc -O2 rotated loop. Each prints while( true ) under option loopcondhoist off, and the -O2 one also under auto. The vendored promotecompare.xml, three stage tests, one CLI probe and the tailform round-trip test, which check other features, pin option loopcondhoist off; the ghidra-sim line-count pin for the faillog function moves from 281 to 279. Unit tests cover the condition rule and the option parse.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

mahaloz and others added 26 commits November 25, 2026 07:19
[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
[AUTOMATED] Extends looptest proven: a guard drops when the skip path equals the
exit path run zero times (returns, return blocks with calls, joins); empty-body
loops; recomputed latch inputs; linear sums and constant comparisons in the
entry normal form; wider fresh loads; the frame-counter rule limited to
unoptimized functions. Stage tests, unit tests, spec prose, catalog row.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	decompiler/crates/kuna-decomp/src/infra/architecture.rs
#	decompiler/crates/kuna-decomp/src/substrate/funcdata_block.rs
#	decompiler/crates/kuna-decomp/tests/fixtures/phase_catalog.json
#	docs/baseline-stages.json
#	docs/options.md
[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
…frees a node

kuna_o0jumps::continue_once removed the old if block and its clause while a goto could still name them,
which panicked the renderer once loopcondhoist made a loop's head test its condition.

[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
The probe checks the label of a switch default's goto into a no-return tail; with the loop condition
rule the default arm prints that tail itself.

[AUTOMATED]

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
@mahaloz
mahaloz merged commit fb88f58 into main Nov 25, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/loopexit branch November 25, 2026 14:04
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.

1 participant