Possible ANSI C Optimisation Done in Practice?

ralph@inputplus.demon.co.uk (Ralph Corderoy)
11 Dec 2001 21:33:29 -0500

          From comp.compilers

Related articles
Possible ANSI C Optimisation Done in Practice? ralph@inputplus.demon.co.uk (2001-12-11)
Re: Possible ANSI C Optimisation Done in Practice? ralph@inputplus.demon.co.uk (Ralph Corderoy) (2001-12-15)
Re: Possible ANSI C Optimisation Done in Practice? rsherry8@home.com (Robert Sherry) (2001-12-15)
Re: Possible ANSI C Optimisation Done in Practice? rbates@southwind.net (Rodney M. Bates) (2001-12-15)
Re: Possible ANSI C Optimisation Done in Practice? nmm1@cus.cam.ac.uk (2001-12-19)
Re: Possible ANSI C Optimisation Done in Practice? ralph@inputplus.demon.co.uk (2001-12-20)
Re: Possible ANSI C Optimisation Done in Practice? ralph@inputplus.demon.co.uk (2001-12-20)
[13 later articles]
| List of all articles for this month |

From: ralph@inputplus.demon.co.uk (Ralph Corderoy)
Newsgroups: comp.compilers
Date: 11 Dec 2001 21:33:29 -0500
Organization: InputPlus Ltd.
Keywords: optimize, question, comment
Posted-Date: 11 Dec 2001 21:33:29 EST
Originator: ralph@inputplus.demon.co.uk (Ralph Corderoy)

Hi,


Given this ANSI C source file


        #include <string.h>


        void foo(char *s)
        {
                char tmp[10];
                char *t;
                int i;


                t = tmp;
                for (i = 0; i < strlen(s); i++) {
                        *t++ = s[i + 1];
                }


                return;
        }


it seems to a group of us that the compiler could determine that
`strlen(s)' is invariant within the loop and hence just call strlen()
once. This is because, AIUI, the object pointed to by s cannot overlap
with that of tmp, and since t is initially assigned to tmp, and it
isn't legal for t to proceed past tmp + 10, assigning to *t can't be
changing s.


First minor question, is our understanding correct?


Secondly, and why we're in comp.compilers, does anyone know of an ANSI
C compiler, for any target, that does carry out this optimisation?


(I know the code should just have `len = strlen(s)' before the loop but
this is just an example.)


Cheers,




Ralph.
[I know I've seen plenty of compilers that expanded strlen() inline since
it was a large part of the old dhrystone benchmark. Dunno if they then
did constant hoisting on the expanded expression. -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.