Bug 19703 - [4.0 Regression] Poor optimisation of loop test, DOM causing unsigned to int and missing combine compares
Summary: [4.0 Regression] Poor optimisation of loop test, DOM causing unsigned to int ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.0.0
: P3 minor
Target Milestone: 4.1.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2005-01-30 02:59 UTC by andy hutchinson
Modified: 2005-11-04 15:36 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 3.4.0 4.1.0
Known to fail: 4.0.0
Last reconfirmed: 2005-06-26 18:03:23


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description andy hutchinson 2005-01-30 02:59:05 UTC
Missed optimization

gcc version 4.0.0 20041205 (experimental)
4.0.0/cc1.exe -quiet -v looprv.c -quiet -dumpbase
looprv.c -mmcu=atmega169 -auxbase looprv -Os -Wall -version -funsigned-char
-funsigned-bitfields -fpack-struct -fshort-enums -o looprv.s

Down counting loop, uses expensive compare EQ (-n) instead of compare >=0.
Testcase as follows:

volatile char  value6;
extern void foo6(char);
void testloop6(void)
{
int i;
for (i=100;i>= 0;i-=10)
{
   if (!value6)
   {
       foo6(i);
   } 
}

Loop test in RTL is compare NE -10. It should be compare GE 0 - which
is(generally) free. First dump of Expanded RTL show the compare.
Comment 1 Andrew Pinski 2005-01-30 03:17:22 UTC
Hmm, on most targets it is true that != is the same case as >=.
Comment 2 Andrew Pinski 2005-01-30 03:17:41 UTC
(In reply to comment #1)
> Hmm, on most targets it is true that != is the same case as >=.
s/case/cost/.
Comment 3 andy hutchinson 2005-01-30 04:58:39 UTC
Subject: Re:  Poor optimisation of loop test

I am not sure what makes you think that. Compare with ZERO is 
invariabley cheaper than compare with "n".
The former is "free" sign status following any conditioning setting 
instruction - like subtract!
Its even the sign bit of the result!

subi r28,10
cpi   r28, -10
brpl  looptop

subi r28,10
brpl looptop

or did I miss something?

pinskia at gcc dot gnu dot org wrote:

>------- Additional Comments From pinskia at gcc dot gnu dot org  2005-01-30 03:17 -------
>Hmm, on most targets it is true that != is the same case as >=.
>
>  
>



Comment 4 Andrew Pinski 2005-01-30 05:25:45 UTC
Confirmed (sorry about that I was thinking of something different at the time).
Here is a better example for normal targets (PPC and x86 too):
extern void foo6(int);
void testloop6 (void)
{
  int i;
  i = 100;
  do {
      foo6 ( i);
    i-=10;
  } while (i != 0);
}

The tree dump for the loop on x86:
<L0>:;
  foo6 (i);
  D.1592 = (unsigned int) i + 0fffffff6;
  i = (int) D.1592;
  if (D.1592 != 0) goto <L0>; else goto <L1>;

Note this is not IV-OPTS messing up but DOM changing the if statement.
Comment 5 Andrew Pinski 2005-01-30 19:20:25 UTC
Even though DOM is the problem, IV-OPTS can be improved so we don't create the casts in the first 
place, patch here to do that: <http://gcc.gnu.org/ml/gcc-patches/2005-01/msg02209.html>.

I will try get a testcase where DOM messes up even without IV-OPTs doing anything.
And here is that testcase, yes it is werid for someone to do this but ...:
extern void foo6(int);
void testloop6 (void)
{
  unsigned int D1133;
  int i;
  i = 100;
  do {
    foo6 (i);
    i = ((unsigned int) i) + 4294967286U;
  }while (i != 0);
}
Comment 6 Andrew Pinski 2005-02-10 21:04:43 UTC
Lowering the priority as IV-OPTs has worked around DOM's problems.
Comment 7 Andrew Pinski 2005-08-17 12:37:01 UTC
This has been fixed on the mainline now.
Comment 8 Andrew Pinski 2005-11-04 15:36:24 UTC
Since this is only a missed optimization, closing as fixed for 4.1.0.