Skip to content

Simplify sadd_overflow+assume to add nsw [InstCombine] #80637

Closed
@scottmcm

Description

@scottmcm

The probably never comes up in C++, but in Rust people can easily end up there from things that boil down to

x.checked_add(y).unwrap_unchecked()

Proof: https://alive2.llvm.org/ce/z/m-n_PG

define i32 @src(i32 noundef %x, i32 noundef %y) noundef {
start:
  %#0 = sadd_overflow i32 noundef %x, noundef %y
  %_7.1 = extractvalue {i32, i1, i24} %#0, 1
  %_10 = xor i1 %_7.1, 1
  %_7.0 = extractvalue {i32, i1, i24} %#0, 0
  assume i1 %_10
  ret i32 %_7.0
}
=>
define i32 @tgt(i32 noundef %x, i32 noundef %y) noundef {
start:
  %r = add nsw i32 noundef %x, noundef %y
  ret i32 %r
}
Transformation seems to be correct!

And of course the uadd_overflow+assumeadd nuw too.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions