This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH: Eliminate unnecessary address arithmetic
- From: Mark Mitchell <mark at codesourcery dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Mon, 9 May 2005 22:46:18 -0700
- Subject: C++ PATCH: Eliminate unnecessary address arithmetic
- Reply-to: mark at codesourcery dot com
This patch was originally intended to fix PR c++/20607, but it
doesn't. That PR is a wrong-code problem in G++ 3.4, coming from the
fact that we turn "&x->y" into address arithmetic in some cases, which
prevents the aliasing machinery from recognizing that we are using the
type-pun-through-union idiom. The patch isn't safe for 3.4 because
it interacts badly with the old implementation of offsetof. In
mainline, that's done differently, so we can avoid going to address
arithmetic. That should give the optimizers more information, make
the compiler just a wee bit faster, and make the internal
representation look more like the source program.
Tested on x86_64-unknown-linux-gnu, applied on the mainline.
--
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com
2005-05-09 Mark Mitchell <mark@codesourcery.com>
* typeck.c (build_unary_op): Do not resort to address arithmetic
when taking the address of a COMPONENT_REF.
Index: typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.519.2.27
diff -c -5 -p -r1.519.2.27 typeck.c
*** typeck.c 2 May 2005 18:38:43 -0000 1.519.2.27
--- typeck.c 10 May 2005 03:10:05 -0000
*************** build_unary_op (enum tree_code code, tre
*** 4120,4142 ****
TREE_OPERAND (arg, 1));
return error_mark_node;
}
else
{
! /* Unfortunately we cannot just build an address
! expression here, because we would not handle
! address-constant-expressions or offsetof correctly. */
tree field = TREE_OPERAND (arg, 1);
! tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
! tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)),
! decl_type_context (field),
! ba_check, NULL);
!
! rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
! rval = build_nop (argtype, rval);
! addr = fold (build (PLUS_EXPR, argtype, rval,
! cp_convert (argtype, byte_position (field))));
}
if (TREE_CODE (argtype) == POINTER_TYPE
&& TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
{
--- 4120,4135 ----
TREE_OPERAND (arg, 1));
return error_mark_node;
}
else
{
! tree object = TREE_OPERAND (arg, 0);
tree field = TREE_OPERAND (arg, 1);
! if (!same_type_ignoring_top_level_qualifiers_p
! (TREE_TYPE (object), decl_type_context (field)))
! abort ();
! addr = build_address (arg);
}
if (TREE_CODE (argtype) == POINTER_TYPE
&& TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
{