Re: Possible ANSI C Optimisation Done in Practice?

nmm1@cus.cam.ac.uk (Nick Maclaren)
20 Dec 2001 10:10:51 -0500

          From comp.compilers

Related articles
[4 earlier articles]
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)
Re: Possible ANSI C Optimisation Done in Practice? ralph@inputplus.demon.co.uk (2001-12-20)
Re: Possible ANSI C Optimisation Done in Practice? RLWatkins@CompuServe.Com (R. L. Watkins) (2001-12-20)
Re: Possible ANSI C Optimisation Done in Practice? ralph@inputplus.demon.co.uk (2001-12-20)
Re: Possible ANSI C Optimisation Done in Practice? nmm1@cus.cam.ac.uk (2001-12-20)
Re: Possible ANSI C Optimisation Done in Practice? nmm1@cus.cam.ac.uk (2001-12-22)
Re: Possible ANSI C Optimisation Done in Practice? ralph@inputplus.demon.co.uk (2001-12-22)
Re: Possible ANSI C Optimisation Done in Practice? ralph@inputplus.demon.co.uk (2001-12-24)
Re: Possible ANSI C Optimisation Done in Practice? nmm1@cus.cam.ac.uk (2001-12-24)
Re: Possible ANSI C Optimisation Done in Practice? toon@moene.indiv.nluug.nl (Toon Moene) (2001-12-24)
Re: Possible ANSI C Optimisation Done in Practice? nmm1@cus.cam.ac.uk (2001-12-27)
[3 later articles]
| List of all articles for this month |
From: nmm1@cus.cam.ac.uk (Nick Maclaren)
Newsgroups: comp.compilers
Date: 20 Dec 2001 10:10:51 -0500
Organization: University of Cambridge, England
References: 01-12-050 <200112120335.fBC3ZMg01140@budgie.cs.uwa.edu.au> 01-12-065 01-12-104 01-12-118
Keywords: C, optimize
Posted-Date: 20 Dec 2001 10:10:51 EST

ralph@inputplus.demon.co.uk (Ralph Corderoy) writes:
|>
|> > Some compilers will replace calls to functions such as 'strlen()'
|> > with in-line code. In such a case, the optimization phase isn't even
|> > aware that 'strlen()' is being called. It simply optimizes the
|> > in-line code as if the programmer had written it that way.
|>
|> Agreed, but that doesn't change my question. Either the compiler
|> *knows* what strlen is and that it is pure, or in addition replaces the
|> call to strlen with inline code. That still leaves the compiler to
|> determine that modifying *t doesn't affect s so the strlen call, or its
|> equivalent inline code, is invariant to the loop.


That is correct, and is why C is so hard to optimise. There are a lot
of ways in which even local variables can become aliased, so the
compiler has to assume that they are even if no sane programmer would
ever have let them become so. To take your example:


        #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;
        }


Add to it the following call:


        #include <errno.h>


        if (sizeof(int) >= 2) {
                strcpy((char *)errno,"A");
                foo((char *)errno);
        }


This is legal, if insane. What is worse, is that strlen is allowed to
set errno at whim (C99 7.5 paragraph 3), which makes the whole
function effectively unoptimisable.


The compiler is allowed to know about strlen, but this sort of
thing means that it is usually almost impossible to determine
whether aliasing can occur without at least global analysis (which,
inter alia, means no pre-compiled, external libraries). Not nice.


Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1@cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
[It seems to me that since the compiler is allowed to treat strlen() as
a special case, part of the special case could include knowledge that its
version of strlen doesn't change errno. -John]



Post a followup to this message

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