This is an archive of the discontinued LLVM Phabricator instance.

[X86] Add custom lowering for lrint/llrint to either cvtss2si/cvtsd2si or fist.
ClosedPublic

Authored by craig.topper on Feb 2 2020, 4:41 PM.

Details

Summary

lrint/llrint are defined as rounding using the current rounding
mode. Numbers that can't be converted raise FE_INVALID and an
implementation defined value is returned. They may also write to
errno.

I believe this means we can use cvtss2si/cvtsd2si or fist to
convert as long as -fno-math-errno is passed on the command line.
Clang will leave them as libcalls if errno is enabled so they
won't become ISD::LRINT/LLRINT in SelectionDAG.

For 64-bit results on a 32-bit target we can't use cvtss2si/cvtsd2si
but we can use fist since it can write to a 64-bit memory location.
Though maybe we could consider using vcvtps2qq/vcvtpd2qq on avx512dq
targets?

gcc also does this optimization.

I think we might be able to do this with STRICT_LRINT/LLRINT as
well, but I've left that for future work.

Event Timeline

craig.topper created this revision.Feb 2 2020, 4:41 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 2 2020, 4:41 PM
Herald added subscribers: jfb, hiraditya. · View Herald Transcript
RKSimon added inline comments.Feb 3 2020, 2:59 AM
llvm/test/CodeGen/X86/llrint-conv.ll
6

Shouldn't that be X64-SSE ?

Fix bad check line

Would it be possible to add vector support/tests as well in this patch or as a followup?

Would it be possible to add vector support/tests as well in this patch or as a followup?

lrint/llrint is not currently allowed for vectors.

RKSimon accepted this revision.Feb 4 2020, 1:30 PM

OK LGTM, cheers

This revision is now accepted and ready to land.Feb 4 2020, 1:30 PM
This revision was automatically updated to reflect the committed changes.