Skip to content

[ConstraintElimination] Missed optimization: fold max(SExt(b), SExt(a) + 1) to SExt(b) if a s< b #82271

Closed
@XChy

Description

@XChy

Alive2 proof: https://alive2.llvm.org/ce/z/A8dtGp

Motivating example

Similar to #75155

define i64 @src(i32 %a, i32 %b){
entry:
  %cmp = icmp slt i32 %a, %b
  br i1 %cmp, label %then, label %else

then:
  %sa = sext i32 %a to i64
  %sb = sext i32 %b to i64
  %add = add i64 %sa, 1
  %smax = call i64 @llvm.smax.i64(i64 %sb, i64 %add)
  call void @dummy()
  ret i64 %smax

else:
  ret i64 0
}

can be folded to:

define i64 @tgt(i32 %a, i32 %b){
entry:
  %cmp = icmp slt i32 %a, %b
  br i1 %cmp, label %then, label %else

then:
  %sb = sext i32 %b to i64
  call void @dummy()
  ret i64 %sb

else:
  ret i64 0
}

Real-world motivation

This snippet of IR is derived from redis/src/db.c@xreadGetKeys (after O3 pipeline).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, see also:https://godbolt.org/z/Kz4M6WPxn

Let me know if you can confirm that it's an optimization opportunity, thanks.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions