Bug 61056 - strchr (x, 0) is not converted to strlen (x)
Summary: strchr (x, 0) is not converted to strlen (x)
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 5.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2014-05-04 16:22 UTC by Oleg Endo
Modified: 2016-11-02 11:17 UTC (History)
2 users (show)

See Also:
Host:
Target: sh*-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Oleg Endo 2014-05-04 16:22:25 UTC
At least on SH and r210026 the following:

extern "C" const char* strchr (const char*,int);

const char* test (const char* x)
{
  return strrchr (x, 0);
}

does not get converted to a strlen (x).  It seems that tree-ssa-strlen is trying to do something but always fails.
Comment 1 Jakub Jelinek 2014-05-04 16:47:51 UTC
1) your testcase uses strrchr rather than strchr
2) why do you think it is desirable to convert strchr (x, 0) to x + strlen (x)?
Unless you have good library implementation for the latter and bad for the former, strchr (x, 0) should be a win.
What the strlen pass does is that if you e.g. use strchr (x, 0) or strlen (x) several times when the lengths of the strings actually can't change in between, it can avoid the second and following call.  And, it is able to deal even with strlen (x) used in one place and strchr (x, 0) in another or vice versa etc. (plus various other optimizations).
Comment 2 Oleg Endo 2014-05-04 16:59:40 UTC
(In reply to Jakub Jelinek from comment #1)
> 1) your testcase uses strrchr rather than strchr

Sorry, typo/pasto.  It was meant to be "strchr".

> 2) why do you think it is desirable to convert strchr (x, 0) to x + strlen
> (x)?

On SH we have a builtin strlen implementation.  I was just expecting strchr (x, 0) to utilize it, that's all.

> Unless you have good library implementation for the latter and bad for the
> former, strchr (x, 0) should be a win.
> What the strlen pass does is that if you e.g. use strchr (x, 0) or strlen
> (x) several times when the lengths of the strings actually can't change in
> between, it can avoid the second and following call.  And, it is able to
> deal even with strlen (x) used in one place and strchr (x, 0) in another or
> vice versa etc. (plus various other optimizations).

Ah, thanks for the explanation.
BTW, are there any plans to add true builtin strchr support so that it can be expanded by the target code in the same way as strlen?
Comment 3 Oleg Endo 2016-04-19 10:44:13 UTC
Just for reference
https://gcc.gnu.org/ml/gcc-patches/2016-04/msg00870.html
Comment 4 Andrew Pinski 2016-09-06 06:37:03 UTC
See bug 32650 also.
Comment 6 Wilco 2016-11-02 11:17:15 UTC
Fixed in r240568.