Re: Compiler code optimization: see code below
From: E. Robert Tisdale (E.Robert.Tisdale_at_jpl.nasa.gov)
Date: 01/13/05
- Next message: CBFalconer: "Re: Where do pointers point to?"
- Previous message: Michael Wojcik: "Re: is NULL-checking redundant in accessor-functions?"
- In reply to: Jack Klein: "Re: Compiler code optimization: see code below"
- Next in thread: Lawrence Kirby: "Re: Compiler code optimization: see code below"
- Reply: Lawrence Kirby: "Re: Compiler code optimization: see code below"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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."
- Next message: CBFalconer: "Re: Where do pointers point to?"
- Previous message: Michael Wojcik: "Re: is NULL-checking redundant in accessor-functions?"
- In reply to: Jack Klein: "Re: Compiler code optimization: see code below"
- Next in thread: Lawrence Kirby: "Re: Compiler code optimization: see code below"
- Reply: Lawrence Kirby: "Re: Compiler code optimization: see code below"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|