This is an archive of the discontinued LLVM Phabricator instance.

[X86][InstCombine] Add constant folding and simplification support for pdep and pext
ClosedPublic

Authored by craig.topper on Dec 27 2019, 4:48 PM.

Details

Summary

The instructions use a mask to either pack disjoint bits together(pext) or spread bits to disjoint locations(pdep). If the mask is all 0s then no bits are extracted or deposited. If the mask is all ones, then the source value is written to the result since no compression or expansion happens. Otherwise if both the source and mask are constant we can walk the bits in the source/mask and calculate the result.

There other crazier things we could do like computeKnownBits or turning pext into shift/and if only a single contiguous range of bits is extracted.

Fixes PR44389

Diff Detail

Event Timeline

craig.topper created this revision.Dec 27 2019, 4:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 27 2019, 4:48 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
RKSimon added inline comments.Dec 28 2019, 1:28 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2511

clang format comment?

2532

Can you confirm this is correct? It doesn't seem to match:

https://www.felixcloutier.com/x86/pdep
https://www.chessprogramming.org/BMI2#PDEP

But it might just be that my brain is still in a Christmas blur......

2539

clang format comment?

llvm/test/Transforms/InstCombine/X86/x86-bmi-tbm.ll
283

Worth also testing with a non-constant arg0 for the zero/allbits mask cases?

craig.topper marked an inline comment as done.Dec 28 2019, 1:38 AM
craig.topper added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2532

I think its right. BitToTest here is equivalent to "bb" from the chessprogramming page. BitToSet is the mask & -mask. I reused it clear the bit in Mask where they used mask &= mask - 1.

Use non-constants for the all 0s and all ones 1s cases.

Move comment to own line. Use Mask &= Mask - 1 instead of the Mask ^=.

xbolva00 added a comment.EditedDec 29 2019, 3:24 AM

Looks fine to me

xbolva00 accepted this revision.Dec 31 2019, 2:00 PM
This revision is now accepted and ready to land.Dec 31 2019, 2:00 PM
This revision was automatically updated to reflect the committed changes.