This is an archive of the discontinued LLVM Phabricator instance.

[MIPS GlobalISel] Select count leading zeros
ClosedPublic

Authored by Petar.Avramovic on Jan 22 2020, 10:11 AM.

Details

Summary

llvm.ctlz.<type> intrinsic has additional i1 argument is_zero_undef,
it tells whether zero as the first argument produces a defined result.
MIPS clz instruction returns 32 for zero input.
G_CTLZ is generated from llvm.ctlz.<type> (<type> <src>, i1 false)
intrinsics, clang generates these intrinsics from builtin_clz and
builtin_clzll.
G_CTLZ_ZERO_UNDEF can also be generated from llvm.ctlz with true as
second argument. It is also traditionally part of and many algorithms
that are now predicated on avoiding zero-value inputs.

Add narrow scalar for G_CTLZ (algorithm uses G_CTLZ_ZERO_UNDEF).
Lower G_CTLZ_ZERO_UNDEF and select G_CTLZ for MIPS32.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJan 22 2020, 10:11 AM
arsenm added a subscriber: arsenm.Jan 22 2020, 10:18 AM
arsenm added inline comments.
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
3862

This assumes that NarrowTy == OrigType/2. This should check that or do something else in that case.
This also won't work with a vector

3865

You can do just buildUnmerge(NarrowTy)

Add check that OrigType is scalar and NarrowTy == OrigType/2. Fix build unmerge.

atanasyan added inline comments.Jan 23 2020, 4:35 AM
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
980–986

Is it possible that TypeIdx != 0 && TypeIdx != 1 ?

arsenm added inline comments.Jan 23 2020, 4:45 AM
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
980–986

That would be an invalid case as these instructions only have 2 type indexes

arsenm added inline comments.Jan 23 2020, 4:48 AM
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
3869

Can you add a comment like the DAG has for what this is doing like

// ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
atanasyan added inline comments.Jan 23 2020, 4:59 AM
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
980–986

Yeah, I understand that this case is invalid. Does it make a sense to note this fact and add some sort of assert?

arsenm added inline comments.Jan 23 2020, 5:07 AM
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
980–986

I think it's kind of a waste of time, and the we inconsistently assert or not on these. It should be possible to write a generic assert that catches invalid type indices for every instruction so every helper doesn't need to worry about it

Add comment, leave assert for another patch.

This revision is now accepted and ready to land.Jan 25 2020, 1:50 AM
This revision was automatically updated to reflect the committed changes.