LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 41952 - [x86] shiftAmountMod should be more eager?
Summary: [x86] shiftAmountMod should be more eager?
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Backend: X86 (show other bugs)
Version: trunk
Hardware: PC Linux
: P enhancement
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-05-20 15:03 PDT by Roman Lebedev
Modified: 2019-06-04 04:07 PDT (History)
6 users (show)

See Also:
Fixed By Commit(s): 362488


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Roman Lebedev 2019-05-20 15:03:48 PDT
unsigned test(unsigned val, unsigned a, unsigned b) {
    unsigned shift = (32 - a) - b;
    return val << shift;
}

clang produces 

        mov     eax, 32
        sub     eax, esi
        sub     eax, edx
        shlx    eax, edi, eax

icc produces

        neg       esi      # <- yay
        sub       esi, edx
        mov       ecx, esi
        shl       edi, cl

https://godbolt.org/z/gdGx_u
Comment 1 Roman Lebedev 2019-05-21 01:25:41 PDT
https://rise4fun.com/Alive/jHD

Hm, i wonder if instead of trying to do all this in `X86DAGToDAGISel::tryShiftAmountMod()`,
we should do this more generally: https://rise4fun.com/Alive/L8sU
1. transform `imm - x` to `neg(x) + imm`, which *may* be good anyway
   since there is no subtraction from an immediate, 
2. and then transform `(x + C) - y`  -> `(x - y) + c`
3. That results in `z + c` which `X86DAGToDAGISel::tryShiftAmountMod()` will already handle.

How does this sound?
Comment 2 Roman Lebedev 2019-05-21 02:11:21 PDT
(In reply to Roman Lebedev from comment #1)
> https://rise4fun.com/Alive/jHD
> 
> Hm, i wonder if instead of trying to do all this in
> `X86DAGToDAGISel::tryShiftAmountMod()`,
> we should do this more generally: https://rise4fun.com/Alive/L8sU
> 1. transform `imm - x` to `neg(x) + imm`, which *may* be good anyway
>    since there is no subtraction from an immediate, 

> 2. and then transform `(x + C) - y`  -> `(x - y) + c`
> 3. That results in `z + c` which `X86DAGToDAGISel::tryShiftAmountMod()` will
> already handle.
Ah, good news, we seem to already do that in backend: (not middle-end though)
https://godbolt.org/z/U9pdPl 

So transform `imm - x` to `neg(x) + imm` is the only missing piece.

> How does this sound?
Comment 3 Roman Lebedev 2019-05-21 04:14:54 PDT
Added tests specific to shift amount masking in r361241.
Will add more generic `imm - x`->`neg(x) + imm` tests later.
Comment 4 Roman Lebedev 2019-05-26 05:46:32 PDT
(In reply to Roman Lebedev from comment #2)
> (In reply to Roman Lebedev from comment #1)
> > https://rise4fun.com/Alive/jHD
> > 
> > Hm, i wonder if instead of trying to do all this in
> > `X86DAGToDAGISel::tryShiftAmountMod()`,
> > we should do this more generally: https://rise4fun.com/Alive/L8sU
> > 1. transform `imm - x` to `neg(x) + imm`, which *may* be good anyway
> >    since there is no subtraction from an immediate, 
 
> > 2. and then transform `(x + C) - y`  -> `(x - y) + c`
> > 3. That results in `z + c` which `X86DAGToDAGISel::tryShiftAmountMod()` will
> > already handle.
> Ah, good news, we seem to already do that in backend: (not middle-end though)
> https://godbolt.org/z/U9pdPl 
No, apparently we do not. 
This ended up being 5 patches, +3 patches to address some regressions
(more regressions to be addressed, for latter patches in series).
Before i look any further, would be awesome to reduce that queue.

First patch in series: https://reviews.llvm.org/D62223 (look at Stack)

> So transform `imm - x` to `neg(x) + imm` is the only missing piece.
> 
> > How does this sound?
Comment 5 Roman Lebedev 2019-06-01 08:48:56 PDT
Much to my surprise, https://reviews.llvm.org/D62774#change-GFoeh6tuzX9e
fixes the motivational case. Not sure if other transforms i mentioned in
https://bugs.llvm.org/show_bug.cgi?id=41952#c1 will be helpful.
Comment 6 Roman Lebedev 2019-06-04 04:07:42 PDT
Fixed by adding dagcombine folds to hoist add/sub from/to constant.