public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Which pass optimizes if (x != x + 10) to if (1)?
@ 2012-03-20  3:09 Zhenqiang Chen
  2012-03-20 16:41 ` Ian Lance Taylor
  0 siblings, 1 reply; 6+ messages in thread
From: Zhenqiang Chen @ 2012-03-20  3:09 UTC (permalink / raw)
  To: gcc-help

Hi,

For the following case: -O0/-O2 can optimize if (x != x + 10) to if
(1) at the beginning. But -Os can not. All options can optimize  if (x
+ 10 != x) to if (1).

To reproduce it, check test.c.003t.original for the two commands.

gcc test.c -fdump-tree-all -c -O0.
gcc test.c -fdump-tree-all -c -Os.

void test (int x, unsigned int y)
{
  if (x != x + 10)
    ;
  if (x + 10 != x)
    ;
}

Which pass optimizes if (x != x + 10) to if (1)? Why is it not applied to -Os?

Thanks!
-Zhenqiang

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

* Re: Which pass optimizes if (x != x + 10) to if (1)?
  2012-03-20  3:09 Which pass optimizes if (x != x + 10) to if (1)? Zhenqiang Chen
@ 2012-03-20 16:41 ` Ian Lance Taylor
  2012-03-21  7:00   ` Zhenqiang Chen
  0 siblings, 1 reply; 6+ messages in thread
From: Ian Lance Taylor @ 2012-03-20 16:41 UTC (permalink / raw)
  To: Zhenqiang Chen; +Cc: gcc-help

Zhenqiang Chen <zhenqiang.chen@linaro.org> writes:

> For the following case: -O0/-O2 can optimize if (x != x + 10) to if
> (1) at the beginning. But -Os can not. All options can optimize  if (x
> + 10 != x) to if (1).
>
> To reproduce it, check test.c.003t.original for the two commands.
>
> gcc test.c -fdump-tree-all -c -O0.
> gcc test.c -fdump-tree-all -c -Os.
>
> void test (int x, unsigned int y)
> {
>   if (x != x + 10)
>     ;
>   if (x + 10 != x)
>     ;
> }
>
> Which pass optimizes if (x != x + 10) to if (1)? Why is it not applied to -Os?

It's not a separate pass.  It's code in fold-const.c.

I don't know why -Os makes a difference here.  It does seem odd.  I
encourage you to investigate what is happening.

Ian

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

* Re: Which pass optimizes if (x != x + 10) to if (1)?
  2012-03-20 16:41 ` Ian Lance Taylor
@ 2012-03-21  7:00   ` Zhenqiang Chen
  2012-03-21  9:25     ` Andrew Haley
  2012-03-21 13:48     ` Ian Lance Taylor
  0 siblings, 2 replies; 6+ messages in thread
From: Zhenqiang Chen @ 2012-03-21  7:00 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

>> Which pass optimizes if (x != x + 10) to if (1)? Why is it not applied to -Os?
>
> It's not a separate pass.  It's code in fold-const.c.
>
> I don't know why -Os makes a difference here.  It does seem odd.  I
> encourage you to investigate what is happening.
>

Thank you for the comment. I found the root cause. In function
tree_swap_operands_p (fold-const.c), there is a check

if (optimize_function_for_size_p (cfun))
    return 0;

which blocks to swap (x != x + 10) to (x + 10 != x). And the following
optimization can only handle (x + 10 != x).

In most cases, constant-folding will benefit for code size. Any reason
to add this check?

Thanks!
-Zhenqiang

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

* Re: Which pass optimizes if (x != x + 10) to if (1)?
  2012-03-21  7:00   ` Zhenqiang Chen
@ 2012-03-21  9:25     ` Andrew Haley
  2012-03-21 13:48     ` Ian Lance Taylor
  1 sibling, 0 replies; 6+ messages in thread
From: Andrew Haley @ 2012-03-21  9:25 UTC (permalink / raw)
  To: gcc-help

On 03/21/2012 07:00 AM, Zhenqiang Chen wrote:
> Thank you for the comment. I found the root cause. In function
> tree_swap_operands_p (fold-const.c), there is a check
> 
> if (optimize_function_for_size_p (cfun))
>     return 0;
> 
> which blocks to swap (x != x + 10) to (x + 10 != x). And the following
> optimization can only handle (x + 10 != x).
> 
> In most cases, constant-folding will benefit for code size. Any reason
> to add this check?

If you search the history you'll probably find a bug that was
fixed by this (admittedly rather odd) test.

Andrew.

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

* Re: Which pass optimizes if (x != x + 10) to if (1)?
  2012-03-21  7:00   ` Zhenqiang Chen
  2012-03-21  9:25     ` Andrew Haley
@ 2012-03-21 13:48     ` Ian Lance Taylor
  2012-03-22  1:44       ` Zhenqiang Chen
  1 sibling, 1 reply; 6+ messages in thread
From: Ian Lance Taylor @ 2012-03-21 13:48 UTC (permalink / raw)
  To: Zhenqiang Chen; +Cc: gcc-help

Zhenqiang Chen <zhenqiang.chen@linaro.org> writes:

>>> Which pass optimizes if (x != x + 10) to if (1)? Why is it not applied to -Os?
>>
>> It's not a separate pass.  It's code in fold-const.c.
>>
>> I don't know why -Os makes a difference here.  It does seem odd.  I
>> encourage you to investigate what is happening.
>>
>
> Thank you for the comment. I found the root cause. In function
> tree_swap_operands_p (fold-const.c), there is a check
>
> if (optimize_function_for_size_p (cfun))
>     return 0;
>
> which blocks to swap (x != x + 10) to (x + 10 != x). And the following
> optimization can only handle (x + 10 != x).
>
> In most cases, constant-folding will benefit for code size. Any reason
> to add this check?

Thanks for looking into this.  The test was added here:

http://gcc.gnu.org/ml/gcc-patches/2003-10/msg01208.html

When I tried your test case, I agree that the test was not optimized out
initially, but it was optimized out in the generated code.  Do you have
a test case in which the final generated code is worse?  If you do,
please open a bug report.

Ian

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

* Re: Which pass optimizes if (x != x + 10) to if (1)?
  2012-03-21 13:48     ` Ian Lance Taylor
@ 2012-03-22  1:44       ` Zhenqiang Chen
  0 siblings, 0 replies; 6+ messages in thread
From: Zhenqiang Chen @ 2012-03-22  1:44 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

On 21 March 2012 21:48, Ian Lance Taylor <iant@google.com> wrote:
> Zhenqiang Chen <zhenqiang.chen@linaro.org> writes:
>
>>>> Which pass optimizes if (x != x + 10) to if (1)? Why is it not applied to -Os?
>>>
>>> It's not a separate pass.  It's code in fold-const.c.
>>>
>>> I don't know why -Os makes a difference here.  It does seem odd.  I
>>> encourage you to investigate what is happening.
>>>
>>
>> Thank you for the comment. I found the root cause. In function
>> tree_swap_operands_p (fold-const.c), there is a check
>>
>> if (optimize_function_for_size_p (cfun))
>>     return 0;
>>
>> which blocks to swap (x != x + 10) to (x + 10 != x). And the following
>> optimization can only handle (x + 10 != x).
>>
>> In most cases, constant-folding will benefit for code size. Any reason
>> to add this check?
>
> Thanks for looking into this.  The test was added here:
>
> http://gcc.gnu.org/ml/gcc-patches/2003-10/msg01208.html
>
> When I tried your test case, I agree that the test was not optimized out
> initially, but it was optimized out in the generated code.  Do you have
> a test case in which the final generated code is worse?  If you do,
> please open a bug report.

Here is the test case:

gcc/testsuite/gcc.dg/pr30951.c.

-Zhenqiang

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

end of thread, other threads:[~2012-03-22  1:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-20  3:09 Which pass optimizes if (x != x + 10) to if (1)? Zhenqiang Chen
2012-03-20 16:41 ` Ian Lance Taylor
2012-03-21  7:00   ` Zhenqiang Chen
2012-03-21  9:25     ` Andrew Haley
2012-03-21 13:48     ` Ian Lance Taylor
2012-03-22  1:44       ` Zhenqiang Chen

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