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]

[Ada] Fix potential logic error in max_size


I believe the following patch fixes a potential logic problem in the
gnat front-end's max_size function.  When calculating the maximum
possible size of a type, this function assumes the worst and for
variant types, returns the MAX of the maximum values of the branches
of a COND_EXPR.  However, when calculating the minimum size of
a type, it currently returns the MAX of the minimum values of the
branches, rather than the MIN of the minimum branch values.

For example, consider the equivalent expressions "X == 1" and
"X == 1 ? 1 : 0" (which should behave identically according to fold).
Gnat's max_size returns 1 and 0 for the upper and lower bounds of
the first expression respectively, but returns 1 and 1 for the second.

This minor bug doesn't cause incorrect code, but may result in
allocating more memory for a structure than is strictly required.


The following patch has been tested on i686-pc-linux-gnu with a full
bootstrap including Ada, and regression tested with a top-level
"make -k check" with no new regressions (including ACATS).

Ok for mainline?


2004-05-10  Roger Sayle  <roger@eyesopen.com>

	* utils.c (max_size): Use MIN_EXPR to find the minimum value of a
	COND_EXPR.


Index: utils.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/ada/utils.c,v
retrieving revision 1.55
diff -c -3 -p -r1.55 utils.c
*** utils.c	27 Apr 2004 10:49:37 -0000	1.55
--- utils.c	10 May 2004 03:50:56 -0000
*************** max_size (tree exp, int max_p)
*** 2217,2223 ****
  	  if (code == SAVE_EXPR)
  	    return exp;
  	  else if (code == COND_EXPR)
! 	    return fold (build (MAX_EXPR, type,
  				max_size (TREE_OPERAND (exp, 1), max_p),
  				max_size (TREE_OPERAND (exp, 2), max_p)));
  	  else if (code == CALL_EXPR && TREE_OPERAND (exp, 1) != 0)
--- 2217,2223 ----
  	  if (code == SAVE_EXPR)
  	    return exp;
  	  else if (code == COND_EXPR)
! 	    return fold (build (max_p ? MAX_EXPR : MIN_EXPR, type,
  				max_size (TREE_OPERAND (exp, 1), max_p),
  				max_size (TREE_OPERAND (exp, 2), max_p)));
  	  else if (code == CALL_EXPR && TREE_OPERAND (exp, 1) != 0)


Roger
--
Roger Sayle,                         E-mail: roger@eyesopen.com
OpenEye Scientific Software,         WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833


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