This is the mail archive of the gcc@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]

[tree-ssa] Why are bools cast to int in conditionals?


It seems that bools are cast to int when used in a conditional. 

For example for this code:

_Bool foo0 (_Bool a)
{
  if (a)
    return 1;
  else
    return 0;
}

the corresponding .original dump is:

{
   if ((int)a != 0)      <---- why is the cast needed here? 
      return <return-value> = 1;
   else
       return <return-value> = 0;
   
}

An extra variable (T.2) is created in  .gimple: 

foo0 (a)
{
  int T.2;

  T.2 = (int)a;
  if (T.2 != 0)
    {
      return 1;
    }
  else
    {
      return 0;
    }
}


And finally that extra variable is eliminated, in .vars we get: 

foo0 (a)
{
<bb 0>:
  if (a != 0) goto <L0>; else goto <L1>;

<L0>:;
  return 1;

<L1>:;
  return 0;
}

which is exactly what the code could have looked like in the
beginning, if it wasn't for the cast to int.

In more "complex" cases the casts are not be eliminated by the tree
optimizers:

_Bool foo (_Bool a, _Bool b)
{
  if (a != b)
    return 1;
  else
    return 0;
}

will be optimized to:

foo (a, b)
{
<bb 0>:
  if ((int)a != (int)b) goto <L0>; else goto <L1>;

<L0>:;
  return 1;

<L1>:;
  return 0;

}

the casts are translated to zero_extends in RTL:

(insn 12 10 13 (parallel [
            (set (reg:SI 63)
                (zero_extend:SI (reg/v:QI 59 [ a ])))
            (clobber (reg:CC 17 flags))
        ]) -1 (nil)
    (nil))

(insn 13 12 14 (parallel [
            (set (reg:SI 64)
                (zero_extend:SI (reg/v:QI 61 [ b ])))
            (clobber (reg:CC 17 flags))
        ]) -1 (nil)
    (nil))

(insn 14 13 15 (set (reg:CCZ 17 flags)
        (compare:CCZ (reg:SI 63)
            (reg:SI 64))) -1 (nil)
    (nil))

which are eliminated by the RTL optimizers.

The tree dumps from PR8361 contain A LOT of code like the above... 

So is there a need to generate the casts to int for bools? 
Where exactly in the compiler are they generated? 


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