[PATCH] Improve "str" + 2 > "str" folding (PR c++/71448)
Richard Biener
rguenther@suse.de
Wed Jun 8 12:49:00 GMT 2016
On Wed, 8 Jun 2016, Jakub Jelinek wrote:
> Hi!
>
> For the purposes of fold_comparison, various constants (in this case
> STRING_CST) work the same as decls, in particular we know the objects
> extents and can determine possible pointer wrapping.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk
> (and maybe later for 6.x)?
Ok.
Thanks,
Richard.
>
> 2016-06-08 Jakub Jelinek <jakub@redhat.com>
> Richard Biener <rguenther@suse.de>
>
> PR c++/71448
> * fold-const.c (fold_comparison): Handle CONSTANT_CLASS_P (base0)
> the same as DECL_P (base0) for indirect_base0. Use equality_code
> in one further place.
>
> * g++.dg/torture/pr71448.C: New test.
>
> --- gcc/fold-const.c.jj 2016-06-06 19:39:40.000000000 +0200
> +++ gcc/fold-const.c 2016-06-08 10:23:15.129178865 +0200
> @@ -8527,9 +8527,9 @@ fold_comparison (location_t loc, enum tr
> if ((offset0 == offset1
> || (offset0 && offset1
> && operand_equal_p (offset0, offset1, 0)))
> - && (code == EQ_EXPR
> - || code == NE_EXPR
> - || (indirect_base0 && DECL_P (base0))
> + && (equality_code
> + || (indirect_base0
> + && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
> || POINTER_TYPE_OVERFLOW_UNDEFINED))
>
> {
> @@ -8568,7 +8568,8 @@ fold_comparison (location_t loc, enum tr
> 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
> else if (bitpos0 == bitpos1
> && (equality_code
> - || (indirect_base0 && DECL_P (base0))
> + || (indirect_base0
> + && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
> || POINTER_TYPE_OVERFLOW_UNDEFINED))
> {
> /* By converting to signed sizetype we cover middle-end pointer
> --- gcc/testsuite/g++.dg/torture/pr71448.C.jj 2016-06-08 10:32:17.409952602 +0200
> +++ gcc/testsuite/g++.dg/torture/pr71448.C 2016-06-08 10:33:38.396872265 +0200
> @@ -0,0 +1,27 @@
> +// PR c++/71448
> +// { dg-do compile }
> +// { dg-additional-options "-std=c++11" }
> +
> +static constexpr const char foo[] = "foo";
> +static constexpr const char *bar = "bar";
> +
> +static_assert ((foo + 3 - foo) == 3, "check");
> +static_assert (foo + 2 != foo, "check");
> +static_assert (foo + 2 >= foo, "check");
> +static_assert (3 + foo >= foo, "check");
> +static_assert (foo <= foo + 2, "check");
> +static_assert (foo <= 3 + foo, "check");
> +static_assert (foo + 2 > foo, "check");
> +static_assert (3 + foo > foo, "check");
> +static_assert (foo < 2 + foo, "check");
> +static_assert (foo < foo + 3, "check");
> +static_assert ((bar + 3 - bar) == 3, "check");
> +static_assert (bar + 2 != bar, "check");
> +static_assert (2 + bar >= bar, "check");
> +static_assert (bar + 3 >= bar, "check");
> +static_assert (bar <= bar + 2, "check");
> +static_assert (bar <= 3 + bar, "check");
> +static_assert (bar + 2 > bar, "check");
> +static_assert (3 + bar > bar, "check");
> +static_assert (bar < 2 + bar, "check");
> +static_assert (bar < bar + 3, "check");
>
> Jakub
>
>
--
Richard Biener <rguenther@suse.de>
SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nuernberg)
More information about the Gcc-patches
mailing list