vamsi3 opened PR #14717 from vamsi3:x64-overflow-flag-branches to bytecodealliance:main:
Closes #14716
Adds the x64 flag-branching rules for
usub_overflow,sadd_overflowandssub_overflow, matching what #14228 and #14254 did foruadd_overflow. Abrifon the overflow result now jumps on the flags of theadd/subinstead of going throughsetcc+test.Each op gets the same two rules as
uadd_overflow:
- an
is_nonzerorule ininst.isleusing theadd/subflags (CC.Bforusub_overflow,CC.Ofor the signed ops), with the value result as an opportunistic def;- a
lowerrule inlower.islethat emits only theadd/subwhen the overflow result has no other uses.Up to 64 bits only, like
uadd_overflow;i128is unchanged. As withuadd_overflow, if the value is used before the branch, the branch is in another block, or the flags have several uses, theadd/subgets emitted again for the branch. This relies on #14324 so a sunk load isn't duplicated. aarch64 can follow separately.Example (
usub_overflowat i32, value used in the taken block), 42 -> 35 bytes:subl %esi, %r8d -setb %r9b -testb %r9b, %r9b -jne 0x21 +jb 0x1aTests:
isa/x64/sadd_ssub_usub_overflow_brif.clif: precise-output, modeled onuadd_overflow_brif.clif. Covers each op at i8 and i64, the value used/unused, the value used before the branch, the flags with several uses, the branch in another block, a load that must not be sunk (#14272, #14324), andtrapz.runtests/sadd_ssub_usub_overflow_brif.clif: interpret + run on x86_64, modeled onmul_overflow_flag_consumers.clif: each op at all four widths around the overflow edges, checking both the branch and the value, plus the flags both branched on and returned.No existing expectations changed.
I measured this in a compiler of mine where every integer add/sub is overflow-checked, on Cranelift 0.136 with the
is_nonzerorules backported. Instruction counts over its 11 benchmarks went down 2.3% overall (fasta -12.4%, the rest 0 to -5%).
vamsi3 requested cfallin for a review on PR #14717.
vamsi3 requested wasmtime-compiler-reviewers for a review on PR #14717.
github-actions[bot] added the label cranelift on PR #14717.
github-actions[bot] added the label cranelift:area:x64 on PR #14717.
:thumbs_up: cfallin submitted PR review:
Thanks!
cfallin added PR #14717 Cranelift: x64: branch on flags for usub_overflow, sadd_overflow and ssub_overflow to the merge queue
:check: cfallin merged PR #14717.
cfallin removed PR #14717 Cranelift: x64: branch on flags for usub_overflow, sadd_overflow and ssub_overflow from the merge queue
Last updated: Oct 31 2026 at 23:03 UTC