This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold select with ctlz to cttz
ClosedPublic

Authored by xbolva00 on Aug 15 2019, 11:27 AM.

Details

Summary

Handle pattern [0]:

int ctz(unsigned int a)
{

int c = __clz(a & -a);
return a ? 31 - c : c;

}

In reality, the compiler can generate much better code for cttz, so fold away this pattern.

https://godbolt.org/z/c5kPtV

[0] https://community.arm.com/community-help/f/discussions/2114/count-trailing-zeros

Diff Detail

Repository
rL LLVM

Event Timeline

xbolva00 created this revision.Aug 15 2019, 11:27 AM
xbolva00 edited the summary of this revision. (Show Details)Aug 15 2019, 11:33 AM

Is there a opposite pattern?

hfinkel accepted this revision.Aug 26 2019, 8:52 PM
hfinkel added a subscriber: hfinkel.

LGTM

lib/Transforms/InstCombine/InstCombineSelect.cpp
790 ↗(On Diff #215447)

I'd prefer that the comments use the same abbreviations as the intrinsics, ctlz and cttz (instead of clz and ctz).

This revision is now accepted and ready to land.Aug 26 2019, 8:52 PM
xbolva00 updated this revision to Diff 217340.Aug 27 2019, 3:00 AM

Updated comments.

xbolva00 marked an inline comment as done.Aug 27 2019, 3:03 AM

Thank you for the review

This revision was automatically updated to reflect the committed changes.