Skip to content

fix(normalize): a flag is a snapshot; SLEIGH's temporary flags are flags - #900

Open
sjkim1127 wants to merge 2 commits into
mainfrom
fix/arm-predicated-select
Open

sjkim1127 wants to merge 2 commits into
mainfrom
fix/arm-predicated-select

Conversation

@sjkim1127

@sjkim1127 sjkim1127 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What

Thumb subs r0, #0; it ne; movne r0, #1 (how a Cortex-M compiler returns a boolean) printed as a branch on a named flag:

r0 = *(uint *)(tmp + 20);
tmpzr = r0 == 0;
if (!tmpzr) {
    r0 = 1;
    tmpzr = tmpov;
}
return r0;

It now prints return (uint)(*(uint *)(tmp + 20) != 0);.

On the full DecBench inlined (-O2) config, 1,511 of 26,012 functions named tmpng/tmpzr/tmpcy/tmpov on main (nine ARM projects); 107 do after this change. An earlier -O0 run has the same names in 9,027 of 33,868 functions, so the shape is not an optimisation artefact.

Three owners, all in normalize

1. flag_recovery: SLEIGH's temporary flags are flags. ARM's SLEIGH computes the condition codes into tmpNG/tmpZR/tmpCY/tmpOV and copies them into NG/ZR/CY/OV only for flag-setting forms. The flag family listed only the architectural names.

2. flag_recovery: a flag definition is a snapshot. Reading the output of (1) turned up conditions that tested a register the previous statement had just replaced. The pass records flag -> defining expression and substitutes the expression at later uses, and nothing dropped a record when an operand was written. This was already wrong on main, for x86 too:

r12 += param_2;
if (__scarry(r12, param_2)) {        // coreutils dd: overflow of the *updated* r12

A definition is now killed by a write to a variable it reads, by a memory write if it loads, by a redefinition of the flag on some path, and by a label that lets control in from elsewhere. A definition that calls is not recorded. The whole-function single-definition fallback takes only definitions that read nothing the function assigns.

3. conditional_move: the default/override merge keeps evaluation order. x = d; if (c) x = o; became x = c ? o : d, which evaluates c before the default is stored:

rax = rax ? rax + 1 : strrchr(param_1, 47);     // sysvinit mybasename, on main

The merge now requires that c and o do not read x, that d has no effect to lose, and that c has none that could change what d reads. Separately, x = d; if (x != 0) x = 1; folds to x = (d != 0).

Plus scratch_liveness: a flag is scratch. With (2) in place, x86 kept 42% more flag writes. Half of them were never read: a flag an instruction carries through itself (of = keep ? of : new) has a reader for every write and no test, and the unsound substitution had been breaking those cycles by accident. The post-structuring reachability pass that already retires closed scratch cycles now counts flag variables among the names it may retire. x86 flag-write statements end at 16,356 -> 14,556.

Measurement

Full inlined config, the same 24,186 measured rows on both sides, caches off, one run each with nothing else on the machine:

main (1c10eda) this PR
total structure distance 621,358 620,160 (−1,198)
perfect 7,378 7,452 (+74)
Union 7,827 7,894
ARM projects: distance / perfect 178,504 / 4,623 177,328 / 4,700
x86 projects: distance / perfect 442,854 / 2,755 442,832 / 2,752
type perfect / byte perfect 624 / 500 622 / 500

Full unoptimized config, the same 31,987 measured rows on both sides, caches off, no decompile failure on either side:

main (1c10eda) this PR
total structure distance 394,148 393,462 (−686)
perfect 12,976 13,007 (+31)
Union (of 34,406) 15,131 15,166
type perfect / byte perfect 3,383 / 780 3,387 / 780
functions naming a temporary flag 1,467 129

509 rows moved: 284 down (−2,454), 225 up (+1,768). The gain is betaflight (−440, +35 perfect) and cleanflight (−286, +14); crazyflie (+302, −10 perfect) and openssh-portable (+234, −9) got worse.

36 rows lose a perfect score there (17 distinct functions), all read:

  • 14 were perfect with wrong text on main: a select that reads its own result before defining it (_rs_rekey, linked into ten binaries, and one more), return v ? 1 : r0 for a v that is always 0, an if whose whole body was two dead flag writes.
  • 5 now print a store or branch main dropped and the source has.
  • 12 are the default/override merge reaching ARM. main already turned x = a; if (c) x = b; into x = c ? b : a on x86; on ARM a flag assignment sat between the two statements and blocked it. The source wrote an if, so these score worse.
  • 5 are return-tail duplication. tail_dup copies a label's tail into each jump when the tail is at most six statements. In the function traced pass by pass (20 → 120), main's tail held four dead flag assignments and was nine statements; now it is four and is copied into six arms, if included. This is the largest family among the rows that got worse (18 rows, +392). The pass is untouched here.

Sample-set gate (233 rows, mixed optimisation levels): perfect 67 -> 67, total distance 7,603 -> 7,625 (+22), types 17/222 -> 17/222. The sample set did not improve. Four rows moved, all up, all read:

  • gnutls buffer_append now prints xsum() twice. The source does call it twice: xtimes(xsum(capacity, 1), 2) is a macro that evaluates its argument twice. main printed one call because the merge in (3) folded rax = xsum(); if (rax >= 0) rax = xsum(); into a select with two equal arms.
  • shadow is_owner loses a dead zf write and the branch shape it happened to supply.
  • Two rows print a flag test inline (|| costs nodes).

Twelve rows of the full config lose a perfect score. Eight had been perfect with wrong text (five read-before-definition selects, two returning v ? 1 : r0 for a v that is always 0, one if whose whole body was two dead flag writes), three now print a stack store main dropped, one duplicates a return 0 into each arm.

Why the output is better without the metric: the old text tested a value on the far side of the write that changed it, and named registers the machine never tests.

Not fixed

  • tail_dup duplicates a tail that contains a branch, and more ARM tails now fit under its limit (above). Restricting what a duplicated tail may hold is a separate change with its own measurement.
  • An unsigned compare moved into a loop condition prints without its cast (while (i < 10) for an int i compared unsigned). The cast-restoring pass skips loop conditions and stops at the first statement it changes. Both are on main; fixed in fix: unsigned casts restored in every statement and loop condition; truncation survives a widening cast #901.
  • cleanup_after_constant_folding_init drops a stack store that is read only through its neighbour's address (buf[1] of a buffer passed as &buf[0]). It runs only when constant folding changed something; with the temporary flags gone it runs less often, so some of those stores now survive. That is a side effect of this PR, not a fix.
  • Two predicates on one compare that write the same register (movne r0, #1; moveq r0, #0) keep the flag as a variable rather than becoming one if/else.
  • A dead default/override pair directly followed by the select that recomputes it: 9 occurrences in the sweep, 2 before.

Tests

  • New tests in flag_recovery, conditional_move and scratch_liveness. Each guard was disabled in turn and the matching test failed.
  • cargo nextest run -p fission-midend-normalize -p fission-pcode: 1,804 passed in debug, 1,803 in release.
  • cargo clippy --workspace --locked -- -D warnings -A clippy::style -A clippy::complexity and cargo fmt --all --check pass.

Proposal: docs/proposals/2026-11-14-flag-snapshots-and-predicated-set.md.

🤖 Generated with Claude Code

Thumb `subs r0, #0; it ne; movne r0, #1` printed as a branch on a named flag
with the flag writes kept inside it. Three owners in normalize:

flag_recovery
- ARM's SLEIGH computes the condition codes into tmpNG/tmpZR/tmpCY/tmpOV and
  copies them into the architectural flags only for flag-setting forms. The
  temporaries join the flag family, so substitution and dead-flag cleanup see
  them.
- A recorded flag definition is a snapshot of an expression. It was never
  dropped when something the expression reads was written, so
  `of = __scarry(a, b); a += b; if (of)` became `a += b; if (__scarry(a, b))`,
  and `cmp; movne r0, #1; moveq r0, #0` would test the register the first
  predicate replaced. Definitions are now killed by a write to a variable they
  read, by a memory write if they load, by a redefinition of the flag on some
  path, and by a label that lets control in from elsewhere; loops and switch
  arms take their own writes as in effect at entry. A definition that calls is
  not recorded. The whole-function single-definition fallback takes only
  definitions that read nothing the function assigns.

conditional_move
- `x = d; if (c) x = o;` became `x = c ? o : d`, which evaluates `c` before
  the default is stored: `rax = rax ? rax + 1 : strrchr(s, '/')`. The merge now
  requires that `c` and `o` do not read `x`, that `d` has no effect to lose,
  and that `c` has none that could change what `d` reads.
- `x = d; if (x != 0) x = 1;` folds to `x = (d != 0)`; `d` is still evaluated
  once.

scratch_liveness
- A flag is scratch: its value matters only where something observable reads
  it. A flag an instruction carries through itself (`of = keep ? of : new`)
  has a reader for every write and no test; the unsound substitution used to
  break such cycles by accident. Flag variables are now among the names the
  post-structuring reachability pass may retire, and the flag primitives
  (`__carry`, `__sborrow`, ...) count as computations there, not calls.

Measured on the full DecBench `inlined` (-O2) config, the same 24,186 rows on
both sides, caches off: total structure distance 621,358 -> 620,160, perfect
7,378 -> 7,452, Union 7,827 -> 7,894 (ARM projects 178,504 -> 177,328 and
4,623 -> 4,700; x86 projects 442,854 -> 442,832 and 2,755 -> 2,752). Sample
set gate: perfect 67 -> 67, distance 7,603 -> 7,625. The `unoptimized` full
config is not measured. Details and the rows that got worse are in
docs/proposals/2026-11-14-flag-snapshots-and-predicated-set.md.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 14, 2026 14:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

…e flag-snapshot change

Distance 394,148 -> 393,462, perfect 12,976 -> 13,007, Union 15,131 ->
15,166 on the same 31,987 rows. Lists the 36 rows that lose a perfect
score by cause, and the two rules that now reach ARM (default/override
merge, return-tail duplication).

Co-Authored-By: Claude Fable 5.2 <[email protected]>

This branch has not been deployed

No deployments
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