public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Kaveh R. GHAZI" <ghazi@caip.rutgers.edu>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH]: middle-end, fix bug in fold_builtin_cexp using COMPLEX_FLOAT_TYPE_P
Date: Thu, 22 Oct 2009 06:18:00 -0000	[thread overview]
Message-ID: <Pine.GSO.4.58.0910220123040.20057@caipclassic.rutgers.edu> (raw)

While reading the source code, I noticed a latent bug in builtins.c
fold_builtin_cexp.  I say "latent" because these are sanity checks and I
don't know of a way to trigger it just now.  The argument check does
something like this pseudo-code:

	if (! (type == COMPLEX_TYPE) && subtype == REAL_TYPE)
	  return NULL_TREE;

whereas it really should do:

	if (! (type == COMPLEX_TYPE && subtype == REAL_TYPE))
	  return NULL_TREE;

I.e. the '!' should cover both sides of the &&.

I decided to convert builtins.c to use the more compact and readable
COMPLEX_FLOAT_TYPE_P() macro which will hopefully avoid these kinds of
errors.  While doing it, I noticed that builtin cimag() was missing the
subtype check, so by doing this change that's fixed as well.

Note some of the places check the TREE_CODE manually, while others use
validate_arg(). They're essentially the same thing when considering
COMPLEX_TYPE.

Since these changes touch code using MPC, I tested it with MPC svn
revision 706, mpc-0.7 and without MPC.  No regressions.

Okay for mainline?

		Thanks,
		--Kaveh


2009-10-21  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* builtins.c (fold_builtin_cabs): Use COMPLEX_FLOAT_TYPE_P to
	check for a complex float type.
	(fold_builtin_ccos): Likewise.
	(fold_builtin_cexp): Likewise.
	(fold_builtin_carg): Likewise.
	(fold_builtin_1): Likewise.
	(fold_builtin_2): Likewise.

diff -rup orig/egcc-SVN20091021/gcc/builtins.c egcc-SVN20091021/gcc/builtins.c
--- orig/egcc-SVN20091021/gcc/builtins.c	2009-10-14 03:30:12.000000000 +0200
+++ egcc-SVN20091021/gcc/builtins.c	2009-10-21 22:41:22.000000000 +0200
@@ -7194,8 +7194,7 @@ fold_builtin_cabs (location_t loc, tree
 {
   tree res;

-  if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE
-      || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
+  if (! COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg)))
     return NULL_TREE;

   /* Calculate the result when the argument is a constant.  */
@@ -7484,8 +7483,7 @@ fold_builtin_ccos (location_t loc,
 		   tree arg, tree type ATTRIBUTE_UNUSED, tree fndecl,
 		   bool hyper ATTRIBUTE_UNUSED)
 {
-  if (validate_arg (arg, COMPLEX_TYPE)
-      && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
+  if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg)))
     {
       tree tmp;

@@ -7582,8 +7580,7 @@ fold_builtin_cexp (location_t loc, tree
   tree res;
 #endif

-  if (!validate_arg (arg0, COMPLEX_TYPE)
-      && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+  if (! COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
     return NULL_TREE;

 #ifdef HAVE_mpc
@@ -9363,8 +9360,7 @@ fold_builtin_fmin_fmax (location_t loc,
 static tree
 fold_builtin_carg (location_t loc, tree arg, tree type)
 {
-  if (validate_arg (arg, COMPLEX_TYPE)
-      && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
+  if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg)))
     {
       tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);

@@ -10005,19 +10001,17 @@ fold_builtin_1 (location_t loc, tree fnd
       return fold_builtin_abs (loc, arg0, type);

     CASE_FLT_FN (BUILT_IN_CONJ):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	&& TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
     break;

     CASE_FLT_FN (BUILT_IN_CREAL):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	&& TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
     break;

     CASE_FLT_FN (BUILT_IN_CIMAG):
-      if (validate_arg (arg0, COMPLEX_TYPE))
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
     break;

@@ -10029,75 +10023,63 @@ fold_builtin_1 (location_t loc, tree fnd

 #ifdef HAVE_mpc
     CASE_FLT_FN (BUILT_IN_CSIN):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_sin);
     break;

     CASE_FLT_FN (BUILT_IN_CSINH):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_sinh);
     break;

     CASE_FLT_FN (BUILT_IN_CTAN):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_tan);
     break;

     CASE_FLT_FN (BUILT_IN_CTANH):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_tanh);
     break;

     CASE_FLT_FN (BUILT_IN_CLOG):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_log);
     break;

     CASE_FLT_FN (BUILT_IN_CSQRT):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_sqrt);
     break;

 #ifdef HAVE_mpc_arc
     CASE_FLT_FN (BUILT_IN_CASIN):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_asin);
     break;

     CASE_FLT_FN (BUILT_IN_CACOS):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_acos);
     break;

     CASE_FLT_FN (BUILT_IN_CATAN):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_atan);
     break;

     CASE_FLT_FN (BUILT_IN_CASINH):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_asinh);
     break;

     CASE_FLT_FN (BUILT_IN_CACOSH):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_acosh);
     break;

     CASE_FLT_FN (BUILT_IN_CATANH):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
 	return do_mpc_arg1 (arg0, type, mpc_atanh);
     break;
 #endif /* HAVE_mpc_arc */
@@ -10421,10 +10403,8 @@ fold_builtin_2 (location_t loc, tree fnd

 #ifdef HAVE_mpc_pow
     CASE_FLT_FN (BUILT_IN_CPOW):
-      if (validate_arg (arg0, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
-	  && validate_arg (arg1, COMPLEX_TYPE)
-	  && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
+      if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
+	  && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg1)))
 	return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
     break;
 #endif

             reply	other threads:[~2009-10-22  5:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-22  6:18 Kaveh R. GHAZI [this message]
2009-10-22  9:59 ` Richard Guenther
2009-10-23  9:09   ` Kaveh R. GHAZI

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.GSO.4.58.0910220123040.20057@caipclassic.rutgers.edu \
    --to=ghazi@caip.rutgers.edu \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).