Skip to content

InstCombine: lshr + "sext" => ashr canonicalization #41734

@LebedevRI

Description

@LebedevRI
Bugzilla Link 42389
Version trunk
OS Linux
Depends On #41736 #42909 #42940
CC @RKSimon,@nikic,@rotateright
Fixed by commit(s) 373964, 375378

Extended Description

I have not triaged yet what folds are missing, but this gigantic fold is missing:
https://godbolt.org/z/E9-txU

int bad(unsigned input, unsigned len) {
    unsigned diff = input >> (32 - len);
    // If the first bit is 1 we need to turn this into a negative number
    if (diff >> (len - 1))
        diff -= (1 << len);
    return diff;
}
int good(unsigned input, unsigned len) {
    return int(input) >> (32 - len);
}

https://rise4fun.com/Alive/l3k

Name: ashr
  %o3 = sub i32 32, %nbits
  %o4 = lshr i32 %x, %o3
  %o5 = add i32 %nbits, -1
  %o6 = lshr i32 %o4, %o5
  %o7 = icmp eq i32 %o6, 0
  %o8 = shl i32 1, %nbits
  %o9 = select i1 %o7, i32 0, i32 %o8
  %r = sub i32 %o4, %o9
=>
  %n0 = sub i32 32, %nbits
  %r = ashr i32 %x, %n0

I'm seeing this in real code.

Metadata

Metadata

Assignees

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