public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: Joey Ye <joey.ye.cc@gmail.com>
To: Andrea Martino <ciaccia@gmail.com>
Cc: gcc-bugs@gcc.gnu.org
Subject: Re: Strange ARM optimisation issue in (android-ndk) gcc 4.8
Date: Tue, 18 Nov 2014 01:35:00 -0000	[thread overview]
Message-ID: <CAL0py24GsP-jY8JvSheNGX2QZeOpLUWx5ZCZRfDfLaFosuOCrw@mail.gmail.com> (raw)
In-Reply-To: <CANr8AhKNT7NdjK_YyeGC80CT81iQAea=TE2ZxpkWfHzr2tjtuQ@mail.gmail.com>

This doesn't seem like a bug to me, unless they run with unexpected
results. My guess of the reason of different generated code can be the
mapping of registers and variables. s0, s1, s2 are not symmetric as
they are mapping to different parameter registers. GCC may choose
different sequence, in order to reduce number of register copying from
incoming registers to outgoing registers (parameter of foo).

Thanks,
Joey

On Mon, Nov 17, 2014 at 10:57 PM, Andrea Martino <ciaccia@gmail.com> wrote:
> Hi all,
> Today I noticed something strange with the way gcc optimises ternary
> operations in c (and c++). Consider the following example where I call
> foo(int) passing the clamped value of (s0, s1, s2):
>
> /*----------------------------------------------------------------------------*/
> #include <algorithm>
>
> extern "C" {
> void foo(int i);
> }
>
> void bar(int s0, int s1, int s2)
> {
>     foo(std::max(s0, std::min(s1, s2)));
> }
> /*----------------------------------------------------------------------------*/
>
> When I compile the above class with -O1 -S, the generated assembly
> contains 2 conditional moves and 0 branches:
>
> stmfd sp!, {r3, lr}
> .save {r3, lr}
> cmp r2, r1
> movge r2, r1
> cmp r0, r2
> movlt r0, r2
> bl foo(PLT)
> ldmfd sp!, {r3, pc}
>
> If I replace the call to foo with one of the following:
>
> foo(std::min(std::max(s0, s1), s2));
> foo(s1 < s2 ? (s1 > s0 ? s1 : s0) : s2);
> foo(s1 < s2 ? (s1 < s0 ? s0 : s1) : s2);
> foo(s1 > s2 ? s2 : (s1 > s0 ? s1 : s0));
> foo(s1 > s2 ? s2 : (s1 < s0 ? s0 : s1));
>
> the generated output is the same. On the other side, when I replace
> the foo call with one of the following:
>
> foo(s0 < s1 ? (s2 < s1 ? s2 : s1) : s0);
> foo(s0 < s1 ? (s2 > s1 ? s1 : s2) : s0);
> foo(s0 > s1 ? s0 : (s2 < s1 ? s2 : s1));
> foo(s0 > s1 ? s0 : (s2 > s1 ? s1 : s2));
>
> The generated assembly contains 1 branch instruction + 2 conditional moves:
>
> stmfd sp!, {r3, lr}
> .save {r3, lr}
> cmp r0, r1
> bge .L2
> cmp r1, r2
> movlt r0, r1
> movge r0, r2
> .L2:
> bl foo(PLT)
> ldmfd sp!, {r3, pc}
>
> I expected all the different combinations of "clamp" should generate
> the same assembly.
>
> Is there a reason for this? Is this a GCC "bug"?
>
> Thanks
> Andrea


      reply	other threads:[~2014-11-18  1:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-17 14:57 Andrea Martino
2014-11-18  1:35 ` Joey Ye [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAL0py24GsP-jY8JvSheNGX2QZeOpLUWx5ZCZRfDfLaFosuOCrw@mail.gmail.com \
    --to=joey.ye.cc@gmail.com \
    --cc=ciaccia@gmail.com \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).