On 5/6/19 5:58 AM, JunMa wrote:
在 2019/5/6 下午6:02, Richard Biener 写道:
On Thu, Mar 21, 2019 at 5:57 AM 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?
It's probably better if gimple_fold_builtin_memchr uses string_constant
directly instead?
We can use string_constant in gimple_fold_builtin_memchr, however it is a
bit complex to use it in memchr/memcmp constant folding.
You are changing memcmp constant folding but only have a testcase
for memchr.
I'll add later.
If we decide to amend c_getstr I would rather make it return the
total length instead of the number of trailing zeros.
I think return the total length is safe in other place as well.
I used the argument in patch since I do not want any impact on
other part at all.
Using c_getstr is simpler than string_constant so it seems fine
to me but I agree that returning a size of the array rather than
the number of trailing nuls would make the API more intuitive.
I would also suggest to use a name for the variable/parameter
that makes that clear. E.g., string_size or array_size.
(Since trailing nuls don't contribute to the length of a string
referring to length in the name is misleading.)
Martin
Thanks
JunMa
Richard.
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.