avoid generating casts for bools

Dan Nicolaescu dann@godzilla.ics.uci.edu
Fri Apr 30 07:38:00 GMT 2004


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. 

Any comments? Is there a better approach?


Extra casts are also generated for the return value for functions that
return bool/char/short. 

For example:

static _Bool bar (_Bool a){return a;}

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

the .generic dump for "bar" is: 
bar (a)
{
  return (int)a;
}

foo1 gets optimized to:
foo1 (a) {
  if ((_Bool)(int)(a != 0) != 0) goto <L4>; else goto <L5>;
<L4>:;
  return 1;
<L5>:;
  return 0;
}

This is because in start_function the DECL_RESULT is cast to int. 
Would it be correct to just delete the 
"if (c_promoting_integer_type_p (restype)) { ... }" code in 
"start_function" ? 


2004-04-29  Dan Nicolaescu  <dann@ics.uci.edu>

	* convert.c (convert_to_integer): Special case booleans.



*** convert.c.~1.18.2.14.~	Sat Apr 17 10:50:04 2004
--- convert.c	Thu Apr 29 22:43:53 2004
***************
*** 396,405 ****
  	     for the types is 8-bit QImode.  In that case, the
  	     conversion necessitates an explicit sign-extension.  In
  	     the signed-to-unsigned case the high-order bits have to
! 	     be cleared.  */
! 	  if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
! 	      && (TYPE_PRECISION (TREE_TYPE (expr))
! 		  != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
  	    code = CONVERT_EXPR;
  	  else
  	    code = NOP_EXPR;
--- 396,409 ----
  	     for the types is 8-bit QImode.  In that case, the
  	     conversion necessitates an explicit sign-extension.  In
  	     the signed-to-unsigned case the high-order bits have to
! 	     be cleared. Also boolean types do not need to be
! 	     converted.  */
! 	  if ((TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
!                && (TYPE_PRECISION (TREE_TYPE (expr))
!                    != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
!               && !((type == integer_type_node)
!                    && (TREE_TYPE (expr) == boolean_type_node)
!                    && integer_onep (boolean_true_node)))
  	    code = CONVERT_EXPR;
  	  else
  	    code = NOP_EXPR;



More information about the Gcc-patches mailing list