Skip to content

[AUTOMATED] Print one loop where gcc copied a loop's step and test into both paths (dedupitetail latch) - #1966

Merged
mahaloz merged 17 commits into
mainfrom
feat/latchjoin
Nov 25, 2026
Merged

mahaloz merged 17 commits into
mainfrom
feat/latchjoin

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

gcc -O2 copies a loop's step and test into each path through the loop body, and kuna printed such a loop as a while loop inside a do loop. The copies are now joined back into one latch, so the loop prints once with its if:

$ cat > lj.c <<'EOF'
struct node { struct node *next; short cls; };
void hit(struct node *p);
void visit(struct node *p, short cls)
{ while (p) { if (p->cls == cls) hit(p); p = p->next; } }
EOF
$ gcc -O2 -fno-inline -c lj.c && kuna decompile lj.o visit

Before:

  if (!a0)
    return;
  do {
    while (*(short *)&a0[1] == a1) {
      hit(a0);
      a0 = (long *)*a0;
      if (!a0)
        return;
    }
    a0 = (long *)*a0;
  } while (a0);

After:

  while (a0) {
    if (*(short *)&a0[1] == a1)
      hit(a0);
    a0 = (long *)*a0;
  }
  • New value latch of dedupitetail (now off|on|latch), the default. on keeps only the existing if/else tail merge and prints the copies as before.
  • p8_structure/kuna_latchjoin.rs runs on the p-code graph next to ActionNodeJoin. When a loop header has exactly two back edges from blocks ending in the same conditional branch to the same two targets, it pairs their operations from the branch backwards and joins the longest run that is operation-for-operation identical: same opcodes and constants, inputs that are the same value, corresponding outputs of the run, or values from before it (which meet in a new MULTIEQUAL); a test of the opposite sense with swapped targets is the same test. The run must contain the op computing the condition; calls, stores, user ops and unoptimized functions are refused. Every path still runs the same operations in the same order.
  • A function of at most 1000 ops that prints a goto inside a joined loop is decompiled again without the join, and the result with fewer gotos is kept.
  • On the decbench O2-noinline corpus 32 more functions match the source's control flow exactly and none fewer (FIT +24, CHECK +8); no function gains a goto, and no -O0 output changes.

Tests: tests/stages/kuna-latchjoin.xml (two loops whose latch gcc duplicates are joined, a busy-wait inside a for loop stays nested, option dedupitetail on prints the old form); unit tests in p8_structure/kuna_latchjoin/tests.rs.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

mahaloz and others added 17 commits November 25, 2026 15:39
apply_mode_aggressive_flips_offdefault_flags_but_not_dwarf_lines restores the
captured load-option environment while
apply_mode_aggressive_sets_ppc32picimports_abi_for_the_next_load_only reads
KUNA_PPC32PICIMPORTS back, so the second failed about one run in four.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
@mahaloz
mahaloz merged commit f5d049c into main Nov 25, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/latchjoin branch November 25, 2026 18:55
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