Skip to content

LLVM should better understand subtraction of zext i1s #42543

@llvmbot

Description

@llvmbot
Bugzilla Link 43198
Version trunk
OS Windows NT
Reporter LLVM Bugzilla Contributor
CC @RKSimon,@rotateright

Extended Description

I found this trying to make a better implementation of Rust's Ord::cmp for integers.

C++ repro https://godbolt.org/z/tL0-oW

int spaceship(int a, int b) {
    return (a > b) - (a < b);
}

bool my_lt(int a, int b) {
    return spaceship(a, b) == -1;
}

my_lt there should be foldable down to just a < b, but that doesn't happen

define dso_local zeroext i1 @&#8203;_Z5my_ltii(i32 %0, i32 %1) local_unnamed_addr #&#8203;0 {
  %3 = icmp sgt i32 %0, %1
  %4 = zext i1 %3 to i32
  %5 = icmp slt i32 %0, %1
  %6 = zext i1 %5 to i32
  %7 = sub nsw i32 %4, %6
  %8 = icmp eq i32 %7, -1
  ret i1 %8
}

(And running it through opt again doesn't help either, https://godbolt.org/z/-hl5H0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzilla

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions