Bug 15618 - Missed bool optimization
Summary: Missed bool optimization
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.0.0
: P2 enhancement
Target Milestone: 4.1.0
Assignee: Andrew Pinski
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: missed-optimization, patch
: 15547 (view as bug list)
Depends on:
Blocks: 15826 19987 14442 15017 18908 19719
  Show dependency treegraph
 
Reported: 2004-05-23 17:20 UTC by Andrew Pinski
Modified: 2005-05-04 01:52 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-11-22 05:10:49


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2004-05-23 17:20:59 UTC
The following three code should be produce the excatly the same asm:
int f(_Bool f1)
{
  if (f1 != 0)
   return 1;
  return 0;
}

int f3(_Bool f1)
{
  int f4;
  if (f1)
   f4 = 1;
  else
    f4 = 0;
  return f4;
}

int f5(_Bool f1)
{
  return f1;
}
Comment 1 Andrew Pinski 2004-05-23 17:22:03 UTC
To fix the second one, you change if (Boolvalue != 0) to be just if (BoolValue)
To fix the first one you have to combine the return statement on the tree level.
Comment 2 Andrew Pinski 2004-05-26 23:34:26 UTC
Here is the x86 asm:
f:
        xorl    %eax, %eax
        cmpb    $0, 4(%esp)
        setne   %al
        ret
        .size   f, .-f
        .p2align 4,,15
.globl f3
        .type   f3, @function
f3:
        xorl    %eax, %eax
        cmpb    $0, 4(%esp)
        setne   %al
        ret
        .size   f3, .-f3
        .p2align 4,,15
.globl f5
        .type   f5, @function
f5:
        movzbl  4(%esp), %eax
        ret
        .size   f5, .-f5

And PPC:
_f:
        addic r2,r3,-1
        subfe r4,r2,r3
        mr r3,r4
        blr
        .align 2
        .globl _f3
.section __TEXT,__text,regular,pure_instructions
        .align 2
_f3:
        addic r2,r3,-1
        subfe r4,r2,r3
        mr r3,r4
        blr
        .align 2
        .globl _f5
.section __TEXT,__text,regular,pure_instructions
        .align 2
_f5:
        blr
Comment 3 Andrew Pinski 2004-06-07 18:05:47 UTC
The only thing left now is the conversion of Bool-value != 0 to Bool-value.
Comment 4 Andrew Pinski 2004-08-07 22:20:08 UTC
*** Bug 15547 has been marked as a duplicate of this bug. ***
Comment 5 Kazu Hirata 2005-02-07 05:40:52 UTC
Here is the last tree SSA form:

;; Function f (f)

f (f1)
{
  _Bool D.1138;
  int D.1120;
  int D.1119;

<bb 0>:
  D.1138_5 = f1_2 != 0;
  D.1120_1 = (int) D.1138_5;
  return D.1120_1;

}

;; Function f3 (f3)

f3 (f1)
{
  _Bool D.1148;
  int f4;
  int D.1126;
  int D.1125;

<bb 0>:
  D.1148_4 = f1_2 != 0;
  f4_1 = (int) D.1148_4;
  return f4_1;

}

;; Function f5 (f5)

f5 (f1)
{
  int D.1130;

<bb 0>:
  D.1130_2 = (int) f1_1;
  return D.1130_2;

}
Comment 6 Andrew Pinski 2005-05-01 17:29:42 UTC
I have a fix which I am testing.  One change to fold_binary to fold "bool_var != 0" to bool_var and one to 
fold_widened_comparison to treat BOOLEAN_TYPE like INTEGER_TYPE.

This improves the gimplification process for PR8361 so we get about 4 less statements.  And it lows the 
memory usage a little.
Comment 7 Paul Schlie 2005-05-01 17:53:38 UTC
(In reply to comment #6)
> I have a fix which I am testing.  One change to fold_binary to fold "bool_var != 0" to bool_var and
> one to  fold_widened_comparison to treat BOOLEAN_TYPE like INTEGER_TYPE.
> 
> This improves the gimplification process for PR8361 so we get about 4 less statements.  And it
> lows the memory usage a little.

As widening operations are a target specific optimization, should any such optimization be in
the target's code generator, where it may decide how to best treat bool types and/or operations?

Comment 8 Andrew Pinski 2005-05-01 19:00:01 UTC
Oh, this now longer will help the C++ front-end as we have "#define bool char".
I really hate that, we should not do it but we do :(.
Comment 9 Andrew Pinski 2005-05-03 01:16:23 UTC
Patch here: <http://gcc.gnu.org/ml/gcc-patches/2005-05/msg00135.html>.
Comment 10 Andrew Pinski 2005-05-04 01:52:15 UTC
Fixed.
Comment 11 CVS Commits 2005-05-04 01:52:32 UTC
Subject: Bug 15618

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	pinskia@gcc.gnu.org	2005-05-04 01:52:07

Modified files:
	gcc            : ChangeLog fold-const.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.dg/tree-ssa: bool-1.c bool-2.c bool-3.c 
	                               bool-4.c bool-5.c bool-6.c 
	                               bool-7.c bool-8.c bool-9.c 

Log message:
	2004-05-03  Andrew Pinski  <pinskia@physics.uc.edu>
	
	PR middle-end/15618
	* fold-const.c (fold_widened_comparison): Treat BOOLEAN_TYPE
	the same as INTEGER_TYPE.
	(fold_binary): Fold "bool_var != 0" to bool_var.
	Fold "bool_var == 1" to bool_var.
	2005-05-03  Andrew Pinski  <pinskia@physics.uc.edu>
	
	PR middle-end/15618
	* gcc.dg/tree-ssa/bool-[1-9].c: New tests.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.8592&r2=2.8593
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fold-const.c.diff?cvsroot=gcc&r1=1.573&r2=1.574
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5438&r2=1.5439
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/bool-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/bool-2.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/bool-3.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/bool-4.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/bool-5.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/bool-6.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/bool-7.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/bool-8.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/bool-9.c.diff?cvsroot=gcc&r1=NONE&r2=1.1