Skip to content

[InstCombine] Failure to recognise CTTZ pattern using CTLZ #164436

@RKSimon

Description

@RKSimon

While we correctly fold this hackers delight ctpop pattern into ctz:

define i4 @src_ctpop(i4 %a0) {
  %dec = add i4 %a0, -1
  %not = xor i4 %a0, -1
  %and = and i4 %dec, %not
  %clz = tail call i4 @llvm.ctpop.i4(i4 %and)
  ret i4 %clz
}
define i4 @tgt_ctpop(i4 %a0) {
  %ctz = tail call i4 @llvm.cttz.i4(i4 %a0, i1 false)
  ret i4 %ctz
}

we fail to recognise the related clz pattern:

define i4 @src_lz(i4 %a0) {
  %dec = add i4 %a0, -1
  %not = xor i4 %a0, -1
  %and = and i4 %dec, %not
  %clz = tail call i4 @llvm.ctlz.i4(i4 %and, i1 false) ; undef = true is ok as well
  %ctz = sub i4 4, %clz
  ret i4 %ctz
}
define i4 @tgt_lz(i4 %a0) {
  %ctz = tail call i4 @llvm.cttz.i4(i4 %a0, i1 false)
  ret i4 %ctz
}

Alive2: https://alive2.llvm.org/ce/z/AdPVa5

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