This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH (3/7)] Widening multiply-and-accumulate pattern matching
- From: Bernd Schmidt <bernds at codesourcery dot com>
- To: Andrew Stubbs <andrew dot stubbs at gmail dot com>
- Cc: Michael Matz <matz at suse dot de>, Richard Guenther <richard dot guenther at gmail dot com>, gcc-patches at gcc dot gnu dot org, patches at linaro dot org
- Date: Fri, 01 Jul 2011 18:40:05 +0200
- Subject: Re: [PATCH (3/7)] Widening multiply-and-accumulate pattern matching
- References: <4E034EF2.3070503@codesourcery.com> <4E03504B.9060305@codesourcery.com> <BANLkTi=a8B-DZZdG2bmOWQ8A+-pCSEsAbQ@mail.gmail.com> <4E044559.5000105@linaro.org> <BANLkTimfKyrARS+eRy+MWYVO2gLqp85JsQ@mail.gmail.com> <1A77B5B39081C241A68E6CF16983025F020906F6@EU1-MAIL.mgc.mentorg.com> <BANLkTi=1TRM7uHWxLD3Se=S-ibe0C13T-Q@mail.gmail.com> <4E09B142.4020402@codesourcery.com> <BANLkTim0wLcSTgAL9isO+dVXxgq-U8B4Sw@mail.gmail.com> <Pine.LNX.4.64.1106281741170.17115@wotan.suse.de> <4E09FDEA.3000004@gmail.com>
On 06/28/11 18:14, Andrew Stubbs wrote:
> unsigned long long
> foo (unsigned long long a, unsigned char b, unsigned char c)
> {
> return a + b * c;
> }
>
> This appears to be entirely unsigned maths with plenty of spare
> precision, and therefore a dead cert for any SI->DI
> multiply-and-accumulate instruction, but not so - it is represented
> internally as:
>
> signed int tmp = (signed int)a * (signed int)b;
> unsigned long long result = a + (unsigned long long)tmp;
>
> Notice the unexpected signed int in the middle! I need to be able to get
> past that to optimize this properly.
Since both inputs are positive in a signed int (they must be, being cast
from a smaller unsigned value), you can infer that it does not matter
whether you treat the result of the multiplication as a signed or an
unsigned value. It is positive in any case.
So, I think the thing to test is: if the accumulate step requires
widening the result of the multiplication, either the cast must be value
preserving (widening unsigned to signed), or you must be able to prove
that the multiplication produces a positive result.
If the accumulate step just casts the multiplication result from signed
to unsigned, keeping the precision the same, you can ignore the cast
since the addition is unaffected by it.
Bernd