Skip to content

[InstCombine] icmp with sdiv operand transformed strangely #35588

@rotateright

Description

@rotateright
Bugzilla Link 36240
Version trunk
OS All
CC @majnemer,@RKSimon

Extended Description

define i1 @​cmpdiv(i32 %x) {
%d = sdiv i32 1, %x
%c = icmp slt i32 %d, 0
ret i1 %c
}

$ opt -instcombine cmpsignbit.ll -S
define i1 @​cmpdiv(i32 %x) {
%1 = add i32 %x, 1
%2 = icmp ult i32 %1, 3
%c1 = icmp slt i32 %x, 0
%c = and i1 %2, %c1
ret i1 %c

That's a weird way to write:
%c = icmp eq i32 %x, -1


Note that we don't do anything with this case:

define i1 @​cmpdiv400(i32 %x) {
%d = sdiv i32 400, %x
%c = icmp slt i32 %d, 0
ret i1 %c
}

But we could turn this into a range check:

Name: sdiv400
%d = sdiv i32 400, %x
%c = icmp slt i32 %d, 0
=>
%c1 = icmp sge i32 %x, -400
%c2 = icmp slt i32 %x, 0
%c = and i1 %c1, %c2

https://rise4fun.com/Alive/cDM

...so whatever is trying to improve the 1st case could probably use generalizing and fixing.

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