Bug 27529 - Does not fold (char *)(size_t)char_ptr or (size_t)(char *)size_t_var
Summary: Does not fold (char *)(size_t)char_ptr or (size_t)(char *)size_t_var
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.2.0
: P3 enhancement
Target Milestone: 4.2.0
Assignee: Richard Biener
URL:
Keywords: missed-optimization, TREE
Depends on:
Blocks:
 
Reported: 2006-05-10 10:01 UTC by Richard Biener
Modified: 2006-05-11 08:30 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-05-10 15:47:38


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Biener 2006-05-10 10:01:45 UTC
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
(Foo *)foo).

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.
Comment 1 Andrew Pinski 2006-05-10 15:47:38 UTC
Confirmed.
Comment 2 Richard Biener 2006-05-11 08:30:19 UTC
Subject: Bug 27529

Author: rguenth
Date: Thu May 11 08:29:40 2006
New Revision: 113692

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=113692
Log:
2006-05-11  Richard Guenther  <rguenther@suse.de>

	PR middle-end/27529
	* 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.

Added:
    trunk/gcc/testsuite/gcc.dg/tree-ssa/foldcast-1.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog

Comment 3 Richard Biener 2006-05-11 08:30:28 UTC
Fixed.
Comment 4 patchapp@dberlin.org 2006-05-15 19:39:56 UTC
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