Fold does not fold two comparisons in a row if the intermediate type differs
in being a pointer vs. an integer variable. This also is the reason for not
folding (Foo *)(char *)foo with foo being of Foo& type (we can fold that to
There's code to do that I think, but it's just missing adjustment for pointers:
/* In addition to the cases of two conversions in a row
handled below, if we are converting something to its own
type via an object of identical or wider precision, neither
conversion is needed. */
if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
&& ((inter_int && final_int)
|| (inter_float && final_float))
&& inter_prec >= final_prec)
return fold_build1 (code, type, TREE_OPERAND (op0, 0));
I have a patch. Of course RTL doesn't care.
Subject: Bug 27529
Date: Thu May 11 08:29:40 2006
New Revision: 113692
2006-05-11 Richard Guenther <firstname.lastname@example.org>
* fold-const.c (fold_unary): Handle intermediate conversion
to a pointer type like intermediate conversion to an integer
type in folding of (T1)(T2)var to var.
Match the code to the comment in the final conversion for
(T1)(T2)var to (T1)var regarding to type precision. Rather
than disallow T1 being of pointer type, assert that both T1
and var are of pointer type or not. Make sure not to fall
over the frontends lazyness wrt array to pointer decay though.
* gcc.dg/tree-ssa/foldcast-1.c: New testcase.
Subject: Bug number PR27529
A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-05/msg00415.html