Skip to content

Missed optimization combining saturating-add with min #100277

Open
@jrose-signal

Description

@jrose-signal

The following IR (simplified from optimized output of rustc)

; min(x.saturating_add(1), 5)
define noundef i32 @test(i32 noundef %x) {
start:
  %0 = tail call i32 @llvm.uadd.sat.i32(i32 %x, i32 1)
  %1 = tail call noundef i32 @llvm.umin.i32(i32 %0, i32 5)
  ret i32 %1
}

produces the following assembly

test:
	incl	%edi
	movl	$-1, %ecx
	cmovnel	%edi, %ecx
	cmpl	$5, %ecx
	movl	$5, %eax
	cmovbl	%ecx, %eax
	retq

but it could just be

test:
	cmpl	$5, %edi
	movl	$4, %eax
	cmovbl	%edi, %eax
	incl	%eax
	retq

or similar.

(Very similar to #92433, but with a constant as an argument to min instead of another addition or variable.)

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