This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: avoid generating casts for bools


law@redhat.com writes:

  > In message <200404301646.i3UGk3GK009717@scanner2.ics.uci.edu>, Dan Nicolaescu w
  > rites:
  >  >Dan Nicolaescu <dann@godzilla.ics.uci.edu> writes:
  >  >
  >  >  > As shown in the thread starting at: 
  >  >  > http://gcc.gnu.org/ml/gcc/2004-04/msg01027.html
  >  >  > bools are unnecessarily cast to int. The tree optimizers cannot get
  >  >  > rid of the extra casts. 
  >  >>
  >  >  > Special casing bools in convert_to_integer seems to do the trick, 
  >  >  > the spurious casts are avoided. The patch to do this is below, it
  >  >  > might be more conservative than it needs to be. 
  >  >
  >  >This patch together with the one avoiding the casts for return values
  >  >in "start_function" was bootstrapped on x84_64-linux. 
  >  >
  >  >There is one regression for C: an extra warning for c99-bool-1.c
  >  >
  >  >./cc1 -O2 c99-bool-1.c
  >  >c99-bool-1.c: In function `main':
  >  >c99-bool-1.c:231: warning: comparison is always true due to limited range of 
  >  >data type
  >  >
  >  >The code in question is:
  >  >
  >  >  _Bool u;
  >  >  u  = 1;
  >  >  if ((u ^= 3) != 1)
  >  >    abort ();
  >  > 
  >  >I am not sure if the warning is correct.
  > Let's break this down (assuming I remember how bools work...)

[snip explanation]

  > Should always be the same as
  >
  > 1 != 1
  >
  > Which is always false.
  >
  > Thus, if the compiler is claiming the test is always true, then something
  > quite bad has happened.

Note that the code generated is correct!

The warning comes from shorten_compare:

(gdb) p op0
$1 = 0x2a9563f240
(gdb) pt
 <nop_expr 0x2a9563f240
    type <integer_type 0x2a955a69a0 int SI
        size <integer_cst 0x2a955a47e0 constant invariant 32>
        unit size <integer_cst 0x2a955a4930 constant invariant 4>
        align 32 symtab 0 alias set -1 precision 32 min <integer_cst 0x2a955a48d0 -2147483648> max <integer_cst 0x2a955a4900 2147483647>
        pointer_to_this <pointer_type 0x2a955b1d20>>
   
    arg 0 <var_decl 0x2a95989c40 u
        type <boolean_type 0x2a955a6ee0 _Bool unsigned QI
            size <integer_cst 0x2a955a4540 constant invariant 8>
            unit size <integer_cst 0x2a955a4570 constant invariant 1>
            align 8 symtab 0 alias set -1 precision 1 min <integer_cst 0x2a955a4cf0 0> max <integer_cst 0x2a955a4d80 1>>
        used unsigned QI file c99-bool-1.c line 5 size <integer_cst 0x2a955a4540 8> unit size <integer_cst 0x2a955a4570 1>
        align 8 context <function_decl 0x2a959899a0 main>>>
(gdb) p op1
$2 = 0x2a95995db0
(gdb) pt
 <integer_cst 0x2a95995db0 type <integer_type 0x2a955a69a0 int> constant invariant 3>
(gdb) p code
$3 = NE_EXPR

so the comparison _is_ always true....


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]