On Thu, Mar 21, 2019 at 12:57 PM JunMa <ju...@linux.alibaba.com> wrote: > > Hi > For now, gcc can not fold code like: > > const char a[5] = "123" > __builtin_memchr (a, '7', sizeof a) > > It tries to avoid folding out of string length although length of a is 5. > This is a bit conservative, it's safe to folding memchr/bcmp/memcmp > builtins when constant string stores in array with some trailing nuls. > > This patch folds these cases by exposing additional length of > trailing nuls in c_getstr(). > Bootstrapped/regtested on x86_64-linux, ok for trunk? I suppose that it's for GCC10?
Thanks, bin > > Regards > JunMa > > > gcc/ChangeLog > > 2019-03-21 Jun Ma <ju...@linux.alibaba.com> > > PR Tree-optimization/89772 > * fold-const.c (c_getstr): Add new parameter to get length of > additional > trailing nuls after constant string. > * gimple-fold.c (gimple_fold_builtin_memchr): consider trailing nuls in > out-of-bound accesses checking. > * fold-const-call.c (fold_const_call): Likewise. > > > gcc/testsuite/ChangeLog > > 2019-03-21 Jun Ma <ju...@linux.alibaba.com> > > PR Tree-optimization/89772 > * gcc.dg/builtin-memchr-4.c: New test.