RFC: folding of literal array refs

Richard Guenther richard.guenther@gmail.com
Thu Apr 27 14:41:00 GMT 2006


On 4/27/06, Dirk Mueller <dmueller@suse.de> wrote:
>
> Hi,
>
> recently I noticed that code like
>
>  if ("foo"[0] == 'g')
>    return 1;
>  return 0;
>
> is properly const folded, while
>
>  if (((const char*)"foo")[0] == 'g')
>    return 1;
>  return 0;
>
> is not. This is because the types don't match. the first variant has a
> "readonly" in the type node, the 2nd one doesn't.
>
>
> the patch below fixes this, but I'm unsure if its the most correct approach
> (no regtest failures):

The function in effect is fold_read_from_constant_string (remember to use
-p for the patches).  The patch looks sensible, but please provide a testcase
for the testsuite.

Richard.

> --- fold-const.c        (revision 113296)
> +++ fold-const.c        (working copy)
> @@ -12220,7 +12220,8 @@
>         }
>
>        if (string
> -         && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (string))
> +         && lang_hooks.types_compatible_p (TREE_TYPE (exp),
> +               TREE_TYPE (TREE_TYPE (string)))
>           && TREE_CODE (string) == STRING_CST
>           && TREE_CODE (index) == INTEGER_CST
>           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
>
>
> Comments?
>
> Thanks,
> Dirk
>



More information about the Gcc-patches mailing list