This is an archive of the discontinued LLVM Phabricator instance.

[LegalizeTypes] Expand FNEG to bitwise op for IEEE FP types
ClosedPublic

Authored by apazos on Feb 6 2019, 8:15 PM.

Details

Summary

Except for custom floating point types x86_fp80 and ppc_fp128,
expand Y = FNEG(X) to Y = X ^ sign mask to avoid library call.
Using bitwise operation can improve code size and performance.

Diff Detail

Repository
rL LLVM

Event Timeline

apazos created this revision.Feb 6 2019, 8:15 PM
arsenm added a subscriber: arsenm.Feb 6 2019, 8:48 PM

This is already done in DAGCombiner, why do you need to do this here?

apazos added a comment.Feb 6 2019, 9:50 PM

For the cases it does not transform, we still do not need to make lib calls.

Try:
llc -mtriple=arm-eabi -float-abi=soft legalize-fneg.ll

It generates:

.save   {r4, lr}
push    {r4, lr}
ldr     r1, [r1]
mov     r4, r0
mov     r0, #-2147483648
bl      __aeabi_fsub
str     r0, [r4]
pop     {r4, lr}
mov     pc, lr
kpn added a subscriber: kpn.Feb 8 2019, 12:33 PM

Without this patch is the fneg instruction getting turned into a subtraction instruction? I hope not on any platform!

apazos added a comment.Feb 8 2019, 2:55 PM

Without the patch, the fneg use in the given test case becomes a sub lib call in the targets I checked when using soft abi (riscv, arm)

DAGCombiner::visitFNEG is transforming fneg(bitconvert(x)) to bitwise operation. So for other patterns, we end up with a lib call which is wasteful.

I also need someone to confirm whether for x86 and ppc FP types it is valid to use bitwise operation. The patch now excludes these formats.

efriedma accepted this revision.Feb 8 2019, 3:09 PM
efriedma added a subscriber: efriedma.

LGTM

Without this patch is the fneg instruction getting turned into a subtraction instruction? I hope not on any platform!

Without this patch we generate calls to __subsf3 etc. There's also equivalent code in LegalizeDAG, but I think in practice it doesn't get used because targets with floating-point support generally have a native fneg instruction.

This revision is now accepted and ready to land.Feb 8 2019, 3:09 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 11 2019, 2:10 PM
Herald added a subscriber: jrtc27. · View Herald Transcript