Skip to content

[InstCombine] Missed @llvm.usub.sat opportunity #44505

@LebedevRI

Description

@LebedevRI
Bugzilla Link 45160
Version trunk
OS Linux
CC @RKSimon,@nikic,@rotateright

Extended Description

Given https://godbolt.org/z/gGcybp

using size_type = unsigned;

size_type t0(size_type pos, size_type size) {
        size_type bytesRemaining = (pos < size) ? size - pos : 0;
    return ((bytesRemaining > 4)
                       ? (size - pos - 4)
                       : 0);
}

we currently produce

define dso_local i32 @_Z2t0jj(i32 %0, i32 %1) local_unnamed_addr #0 {
  %3 = tail call i32 @llvm.usub.sat.i32(i32 %1, i32 %0)
  %4 = icmp ugt i32 %3, 4
  %5 = sub i32 -4, %0
  %6 = add i32 %5, %1
  %7 = select i1 %4, i32 %6, i32 0
  ret i32 %7
}

declare i32 @llvm.usub.sat.i32(i32, i32) #1

but we should instead produce

define dso_local i32 @_Z6squarejj(i32 %0, i32 %1) local_unnamed_addr #0 {
  %3 = tail call i32 @llvm.usub.sat.i32(i32 %1, i32 %0)
  %4 = tail call i32 @llvm.usub.sat.i32(i32 %3, i32 4)
  ret i32 %4
}
declare i32 @llvm.usub.sat.i32(i32, i32) #1
----------------------------------------
define i8 @_Z6squarejj(i8 %0, i8 %1) {
%2:
  %3 = usub_sat i8 %1, %0
  %4 = icmp ugt i8 %3, 4
  %5 = sub i8 252, %0
  %6 = add i8 %5, %1
  %7 = select i1 %4, i8 %6, i8 0
  ret i8 %7
}
=>
define i8 @_Z6squarejj(i8 %0, i8 %1) {
%2:
  %3 = usub_sat i8 %1, %0
  %4 = usub_sat i8 %3, 4
  ret i8 %4
}
Transformation seems to be correct!

Summary:
  1 correct transformations
  0 incorrect transformations
  0 Alive2 errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillallvm:instcombineCovers the InstCombine, InstSimplify and AggressiveInstCombine passes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions