On 6/13/19 5:50 PM, Martin Sebor wrote:
> While integrating the strlen and sprintf passes and investigating
> optimization opportunities that it opens up I noticed a few related
> to a strcmp optimization implemented in GCC 9.  One is to take
> advantage of the fact that a nul-terminated string of a known
> length cannot be equal to a string whose length is greater, even
> if it isn't known exactly.  For example, the equality below must
> be false:
> 
>   int f (char * restrict a, char * restrict b)
>   {
>     memcpy (a, "1234", 4);       // length >= 4
>     strcpy (b, "123");           // length == 3
> 
>     return strcmp (a, b) == 0;   // must be false
>   }
> 
> The attached patch enhances the existing optimization to exploit
> this opportunity.
> 
> Tested on x86_64-linux.
> 
> Martin
> 
> PS There are several more improvements to be made here.  I've been
> raising bugs for them and I plan to submit patches for them in
> the near future.  (Incidentally, they are all in the spirit of
> the suggestion made back in 2012 in pr52171.)
> 
> gcc-90626.diff
> 
> PR tree-optimization/90626 - fold strcmp(a, b) == 0 to zero when one string 
> length is exact and the other is unequal
> 
> gcc/ChangeLog:
> 
>       PR tree-optimization/90626
>       * tree-ssa-strlen.c (strxcmp_unequal): New function.
>       (handle_builtin_string_cmp): Call it.
> 
> gcc/testsuite/ChangeLog:
> 
>       PR tree-optimization/90626
>       * gcc.dg/strlenopt-65.c: New test.
>       * gcc.dg/strlenopt-66.c: New test.
>       * gcc.dg/strlenopt.h (strcmp, strncmp): Declare.
OK.  Good to see we're able to extend Qing's work to handle more cases
without major surgery.

jeff

Reply via email to