Skip to content

Failure to spot scmp(x, 0) idiom #143259

Closed
@Kmeakin

Description

@Kmeakin

https://godbolt.org/z/1xqx65vds

For

int signum(int x) {
    if (x < 0) return -1;
    if (x > 0) return +1;
    return 0;
}

clang produces

signum:
        cmp     w0, #0
        cset    w8, ne
        cmn     w0, #1
        csinv   w0, w8, wzr, gt
        ret

but GCC produces

signum:
        cmp     w0, 0
        cset    w0, ne
        csinv   w0, w0, wzr, ge
        ret

It seems that LLVM is not recognising that signum is equivalent to scmp(x, 0):
https://alive2.llvm.org/ce/z/EYQZwV

define dso_local range(i32 -1, 2) i32 @src(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp ne i32 %0, 0
  %3 = zext i1 %2 to i32
  %4 = icmp sgt i32 %0, -1
  %5 = select i1 %4, i32 %3, i32 -1
  ret i32 %5
}

define dso_local range(i32 -1, 2) i32 @tgt(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @llvm.scmp(i32 %0, i32 0)
  ret i32 %2
}

Metadata

Metadata

Assignees

Labels

good first issuehttps://github.com/llvm/llvm-project/contributellvm:instcombineCovers the InstCombine, InstSimplify and AggressiveInstCombine passesmissed-optimization

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions