Re: Compiler code optimization: see code below

From: E. Robert Tisdale (E.Robert.Tisdale_at_jpl.nasa.gov)
Date: 01/13/05


Date: Wed, 12 Jan 2005 22:10:34 -0800

Jack Klein wrote:

> joshc wrote:
>
>>I'm writing some C to be used in an embedded environment and the code
>>needs to be optimized. I have a question about optimizing compilers in
>>general. I'm using GCC for the workstation and Diab compiler for the
>>embedded target.
>>
>>My question is about how compilers optimize certain code sequences.
>
> The C standard does not define how compilers optimize certain code
> sequences, nor does it require them to do so. It is entirely up to
> the compiler and the options with which you invoke it.
>
>>As an example, take the code below. Will the compiler eliminate the
>>actual function call to foo() in the object code generated and just
>>store the value '3' in temp(obviously somewhere on stack)? I think
>>based on my experiments with GCC the value '3' actually wont' be stored
>>at all since it's obviously not being used anywhere. My tests showed
>>that no function call is made in the object code and neither is any
>>value being stored in temp.
>>
>>int main() {
>>int temp;
>>
>>temp = foo(3);
>>
>>return 0;
>>}
>>
>>int foo(int x) {
>>return x;
>>}
>>
>>Now as a general question, if I have function calls in my code in which
>>the arguments I am passing to the function are known at compile time,
>>will these function calls be eliminated by the compiler?
>>
>>I guess since I don't have much of a background in compilers I'm not
>>sure what exactly a good optimizing compiler can optimize away. Please
>>point me to any references on this topic as well if you have some.
>>Thanks.
>
>
> news:comp.compilers would probably be a good reference to what "a good
> optimizing compiler can optimize away", but that won't necessarily do
> you any good at all. Note also that the issue is off-topic here, as
> optimization is always an implementation issue, not a language issue.
>
> The simple fact is that if it is important to you know what your
> compiler does to your code, then compile your code with your compiler
> and examine the generated object code or its assembly language
> equivalent. That will give you exact answers, as opposed to
> hypothetical indications of what a compiler might do.
>
> Optimization with a specific compiler like gcc will vary greatly with
> version, target architecture, and compiler options.
>
> Neither optimization nor efficiency is defined by the C standard.
> Only the observable output of a strictly conforming program. An
> implementation that generated a Perl script from your source code and
> invoked a Perl interpreter to execute the script could be a strictly
> conforming C implementation if the observable output of the Perl
> script was correct.

That is certainly all true but I don't think that's the issue here.
It isn't about any particular implementation
but implementations in general.

Too many C programmers write bad code
while attempting to out-wit their compilers
or appease deficient optimizing compilers.
The result is code that is unreliable,
hard to read, understand and maintain
and that frustrates optimizing C compilers
when C programmers attempt to port the code.

Perhaps it would have been better if Josh Curtz had asked,
"What optimizations do the ANSI/ISO standards allow?"

My personal approach is to write code assuming that
my C compiler or some C compiler for my target platform(s)
will [eventually] perform *all* of the optimizations
that the ANSI/ISO C standards allow.
That is to say that, "I avoid premature optimization."



Relevant Pages

  • Re: pointer one past malloc.ed memory
    ... Specifically, the compiler transforms: ... When not optimizing, the compiler ... to make this work (in Standard C). ... the system as a whole can still conform. ...
    (comp.lang.c)
  • Re: "Sorting" assignment
    ... too slow, other times for optimizing too much, some times for being too ... That is a rather bizarre, not-quite-C compiler, but ok. ... The overlap issue is a different problem, but performing a "swap" on ... CPU hardware feature differences that can be incredibly important to ...
    (comp.programming)
  • Re: I wasnt expected that !
    ... > Standard version of the software. ... But Borland compiler never behaved so badly ... You guys are lucky to have optimizing version of the compiler ... I wouldn't expect the casual programmer to ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Why INFINITE loop in a thread occupy so much CPU time??
    ... measuring code quality or program efficiency. ... You stated the K&R compiler did the silly thing of testing ... In debug mode, nothing, repeat nothing, matters. ... my Ph.D. is in optimizing compiler technology. ...
    (microsoft.public.vc.mfc)
  • Re: How about this syntactic candy?
    ... Unless the code in the loop is so simple that it is guaranteed that the list.Count property won't change during the execution of the loop, optimizing the expression to avoid reevaluating list.Count would be wrong. ... I once saw a demo of a for loop all compressed into one line with ingenious shortcuts and contractions, and after the optimising compiler had been through it, it generated EXACTLY the same machine code as an alternative loop written out in full. ... In that case, I could see the utility in providing the compiler with an explicit statement to that effect, but it seems to me that the current method required is so easy and simple, I can't imagine the point in adding something extra to the language to support the behavior. ...
    (microsoft.public.dotnet.languages.csharp)