On 12 Jan 2005 19:59:05 -0800, "joshc" <> wrote in
comp.lang.c:
> 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.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html