public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* Strange ARM optimisation issue in (android-ndk) gcc 4.8
@ 2014-11-17 14:57 Andrea Martino
  2014-11-18  1:35 ` Joey Ye
  0 siblings, 1 reply; 2+ messages in thread
From: Andrea Martino @ 2014-11-17 14:57 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Strange ARM optimisation issue in (android-ndk) gcc 4.8
  2014-11-17 14:57 Strange ARM optimisation issue in (android-ndk) gcc 4.8 Andrea Martino
@ 2014-11-18  1:35 ` Joey Ye
  0 siblings, 0 replies; 2+ messages in thread
From: Joey Ye @ 2014-11-18  1:35 UTC (permalink / raw)
  To: Andrea Martino; +Cc: gcc-bugs

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-11-18  1:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-17 14:57 Strange ARM optimisation issue in (android-ndk) gcc 4.8 Andrea Martino
2014-11-18  1:35 ` Joey Ye

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).