[PATCH] handle vla plus offset in strlen (PR 90662)

Jeff Law law@redhat.com
Thu Jun 6 21:53:00 GMT 2019


On 6/5/19 4:51 PM, Martin Sebor wrote:
> One of my new tests for the strlen/sprintf integration tripped
> over an incomplete handling of VLAs by the strlen pass.  Where
> it can determine the length of a substring at some offset with
> other kinds of arrays, the pass fails with VLAs because they
> are represented as pointers to arrays.
> 
> The attached patch adds the missing handling so that code like
> the following can be fully folded even for VLAs.
> 
>   int f (int n)
>   {
>     char a[n];
>     strcpy (a, "12345");
>     if (strlen (&a[2]) != 3)
>       abort ();
>   }
> 
> Tested on x86_64-linux.
> 
> Martin
> 
> gcc-90662.diff
> 
> PR tree-optimization/90662 - strlen of a string in a vla plus offset not folded
> 
> gcc/ChangeLog:
> 
> 	PR tree-optimization/90662
> 	* tree-ssa-strlen.c (get_stridx): Handle simple VLAs and pointers
> 	to arrays.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR tree-optimization/90662
> 	* gcc.dg/strlenopt-62.c: New test.
> 	* gcc.dg/strlenopt-63.c: New test.
We're relying on the fact that in a MEM_REF the offset is always a byte
offset, so no scaling for wchars needed, right?

OK for the trunk.
THanks,
Jeff



More information about the Gcc-patches mailing list