User account creation filtered due to spam.

Bug 10817 - gcc does not optimize result=0;if(t!=0)result=t; to result=t;
Summary: gcc does not optimize result=0;if(t!=0)result=t; to result=t;
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.4.0
: P3 enhancement
Target Milestone: 3.4.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2003-05-16 14:16 UTC by Andrew Pinski
Modified: 2003-11-02 21:34 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2003-05-23 20:22:03


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2003-05-16 14:16:01 UTC
gcc does not optimize
  result=0;
  if(t!=0)
    result=t;
to
  result=t;
or
  if(!t)
    result = t;
  else
    result = 0;
to result = 0;

Release:
GNU C version 3.4 20030516 (experimental) (powerpc-apple-darwin6.6)

Environment:
powerpc-apple-darwin6.6

How-To-Repeat:
int h(int t)
{
        if (!t)
                return 0;
        return t;
}

int h1(int t)
{
        int result = 0;
        if (t)
                result = t;
        return result;

}
Comment 1 Andrew Pinski 2003-05-16 14:16:01 UTC
Fix:
Unknown.
Comment 2 Andrew Pinski 2003-05-24 00:49:46 UTC
confirmed on mainline (20030523).
Comment 3 Falk Hueffner 2003-05-25 00:14:26 UTC
Works for me on Alpha with mainline 20030524. Maybe it's powerpc specific?
Comment 4 Andrew Pinski 2003-05-25 00:22:31 UTC
It also happens on i686-pc-linux-gnu:
.globl h
        .type   h, @function
h:
        movl    4(%esp), %eax
        xorl    %edx, %edx
        testl   %eax, %eax
        je      .L1
        movl    %eax, %edx
.L1:
        movl    %edx, %eax
        ret
        .size   h, .-h
        .p2align 4,,15
.globl h1
        .type   h1, @function
h1:
        movl    4(%esp), %eax
        xorl    %edx, %edx
        testl   %eax, %eax
        je      .L5
        movl    %eax, %edx
.L5:
        movl    %edx, %eax
        ret
        .size   h1, .-h1
        .ident  "GCC: (GNU) 3.4 20030524 (experimental)"

So maybe the optimization is Alpha specific.
Comment 5 Kazu Hirata 2003-07-15 06:08:08 UTC
I created a very simple patch for the if conversion pass to optimize
this for most, if not all, targets.  The problem is that the
optimization doesn't trigger in several hundreds real world source
files that I have.
Comment 6 CVS Commits 2003-11-02 13:56:45 UTC
Subject: Bug 10817

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	sayle@gcc.gnu.org	2003-11-02 13:56:42

Modified files:
	gcc            : ChangeLog ifcvt.c simplify-rtx.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.c-torture/compile: 20031102-1.c 

Log message:
	PR optimization/10817
	* ifcvt.c (noce_emit_move_insn): Improve documentation comment.
	(noce_try_move): New function to optimize an if-the-else into an
	unconditional move, i.e. "if (a!=b) x=a; else x=b" into "x=a".
	(noce_process_if_block): Attempt simplification with noce_try_move.
	
	* simplify-rtx.c (simplify_ternary_operation): Some minor fixes
	and improvements to the optimizations of IF_THEN_ELSE expressions.
	(simplify_subreg): Silence signed/unsigned comparison warning.
	
	* gcc.c-torture/compile/20031102-1.c: New test case.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.1652&r2=2.1653
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ifcvt.c.diff?cvsroot=gcc&r1=1.128&r2=1.129
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/simplify-rtx.c.diff?cvsroot=gcc&r1=1.164&r2=1.165
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.3164&r2=1.3165
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/compile/20031102-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 7 Andrew Pinski 2003-11-02 21:34:37 UTC
Fixed on the mainline.