Skip to content

Simplify add-with-overflow pattern (uaddo) #65863

@chfast

Description

@chfast

For unsigned integers to check if an addition overflowed (get carry bit) it is enough to compare the sum with one of its arguments carry = sum < a. However, it is not obvious that it is enough to only check one of the arguments and we may encounter a pattern where both arguments are checked.

define i1 @src(i64 %a, i64 %b){ %s = add i64 %a, %b %cond_a = icmp ult i64 %s, %a %cond_b = icmp ult i64 %s, %b %cond = or i1 %cond_a, %cond_b ret i1 %cond } define i1 @tgt(i64 %a, i64 %b){ %s = add i64 %a, %b %cond_a = icmp ult i64 %s, %a ret i1 %cond_a }

Playground: https://godbolt.org/z/TW8enc6zo
Proof: https://alive2.llvm.org/ce/z/iPAE7t

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions