[PATCH] PR middle-end/14531: Call fold in g++'s build_base_path

Roger Sayle roger@eyesopen.com
Mon Apr 19 18:12:00 GMT 2004


The following patch is my proposed solution to PR middle-end/14531.
As diagnosed by Andrew Pinski, the problem is that a COND_EXPR is
not getting constant folded during the creation of a C++ initializer.
The problem is that build_base_path isn't calling fold on expression
trees as its building them, so the constant folder never gets to see
the problematic COND_EXPR for the NULL pointer check.

A minor cosmetic change is to flip the sense of the NULL-pointer
check, using NE_EXPR instead of EQ_EXPR, which saves a line in the
source code, placing the constant in the COND_EXPR's third operand.
It also changes calls to build to use the new preferred buildN APIs.

The following patch has been tested on i686-pc-linux-gnu, all languages
except treelang, and regression tested with a top-level "make -k check"
with no new failures.

Ok for mainline?


2004-04-19  Roger Sayle  <roger@eyesopen.com>

	PR middle-end/14531
	* class.c (build_base_path): Call fold whilst building the NULL
	pointer check expression trees.


Index: class.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/class.c,v
retrieving revision 1.606
diff -c -3 -p -r1.606 class.c
*** class.c	1 Apr 2004 13:41:36 -0000	1.606
--- class.c	19 Apr 2004 04:08:49 -0000
*************** build_base_path (enum tree_code code,
*** 290,296 ****
      expr = save_expr (expr);

    if (want_pointer && !nonnull)
!     null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);

    offset = BINFO_OFFSET (binfo);

--- 290,297 ----
      expr = save_expr (expr);

    if (want_pointer && !nonnull)
!     null_test = fold (build2 (NE_EXPR, boolean_type_node,
! 			      expr, integer_zero_node));

    offset = BINFO_OFFSET (binfo);

*************** build_base_path (enum tree_code code,
*** 365,373 ****
      expr = build_indirect_ref (expr, NULL);

    if (null_test)
!     expr = build (COND_EXPR, target_type, null_test,
! 		  build1 (NOP_EXPR, target_type, integer_zero_node),
! 		  expr);

    return expr;
  }
--- 366,374 ----
      expr = build_indirect_ref (expr, NULL);

    if (null_test)
!     expr = fold (build3 (COND_EXPR, target_type, null_test, expr,
! 			 fold (build1 (NOP_EXPR, target_type,
! 				       integer_zero_node))));

    return expr;
  }


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



More information about the Gcc-patches mailing list