Bug 25128 - [m68k] Suboptimal comparisons against 65536
Summary: [m68k] Suboptimal comparisons against 65536
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.2.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2005-11-27 23:28 UTC by Kazu Hirata
Modified: 2016-11-21 18:20 UTC (History)
2 users (show)

See Also:
Host:
Target: m68k-elf
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-11-28 05:04:23


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kazu Hirata 2005-11-27 23:28:39 UTC
Consider:

unsigned int bar (void);

void
foo (void)
{
  unsigned int a = bar ();
  if (65536 > a)
    bar ();
}

./cc1 -quiet -O2 -fomit-frame-pointer generates

foo:
        move.l %a2,-(%sp)
        lea bar,%a2
        jbsr (%a2)
        cmp.l #65535,%d0
        jbhi .L10
        jbsr (%a2)
.L10:
        move.l (%sp)+,%a2
        rts

Note that

        cmp.l #65535,%d0
        jbhi .L10

can be replaced with

        swap %d0
        tst.w %d0
        jbne .L10

A similar trick can be applied to signed comparisons as well.
Comment 1 Andrew Pinski 2005-11-28 05:04:23 UTC
Confirmed.
Comment 2 Gunnar von Boehn 2008-06-10 16:02:09 UTC
> Note that
> 
>         cmp.l #65535,%d0
>         jbhi .L10
> 
> can be replaced with
> 
>         swap %d0
>         tst.w %d0
>         jbne .L10
> 
> A similar trick can be applied to signed comparisons as well.

But this "trick" will run slower on the higher 68k CPUs.
On 68040 or 68060 or SuperScalar Coldfire its better to generate less instructions that do not have dependancies.

I think "cmp.l #65535,%d0" is the code that should be generated by "O2" as its faster on many 68K models.
The shorter two instruction trick might be an option for compile optiont "Os"


Kind regards

Gunnar von Boehn
Comment 3 Jeffrey A. Law 2016-11-21 18:19:44 UTC
Author: law
Date: Mon Nov 21 18:19:12 2016
New Revision: 242676

URL: https://gcc.gnu.org/viewcvs?rev=242676&root=gcc&view=rev
Log:
	PR target/25128
	* config/m68k/predicates.md (swap_peephole_relational_operator): New
	predicate.
	* config/m68k/m68k.md (relational tests against 65535/65536): New
	peephole2.

	PR target/25128
	* gcc.target/m68k/pr25128.c: New test.

Added:
    trunk/gcc/testsuite/gcc.target/m68k/pr25128.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/m68k/m68k.md
    trunk/gcc/config/m68k/predicates.md
    trunk/gcc/testsuite/ChangeLog
Comment 4 Jeffrey A. Law 2016-11-21 18:20:28 UTC
Fixed on the trunk.