On Fri, Aug 7, 2009 at 9:24 PM, bala chandar<[email protected]> wrote:
>     I am Balachandar.Final year B.Tech - IT student.I want to develop
> a c language code optimizer.

Good choice!  Code optimization is a fascinating topic.  I did this
during my college days too :-)

I must warn you that compiler design is a difficult topic which needs
lots of time and guidance.  You could spend many years on this subject.


> I know the compiler's can optimize the code but its not effective always.

There's a vast body of theory behind compiler optimization -- trees,
control graphs, basic blocks, code invariants, dead code elimination,
loop optimizations, array traversal optimizations, intermediate code
generation, peep-hole optimization, machine specific optimization,
etc. which need to be carefully understood before attempting to build
a code optimizer.

See the internal implementation docs for GCC for more:
    http://gcc.gnu.org/onlinedocs/gccint/

I also suggest that you read the Dragon Book (new cover looks nice):
    http://en.wikipedia.org/wiki/Dragon_Book_(computer_science)


> I did a small experiment with the
> below code and got a positive result for me.I want to highlight the
> codes which can be optimized to get quicker execution.
...
>      If i execute this each time ( j/100 ) is calculated and takes
> more amount of time.
...
>      Here j /100 is calculated only once and the execution time
> reduces automatically.So i want to highlight these kind of places in a
> code.

In optimization parlance, this technique is called "constant folding".
    http://en.wikipedia.org/wiki/Constant_folding

If you use

    const double pi = 22/7
    ...
    double area = 2 * pi * r

in your code, the compiler will calculate 22/7 at compile time itself
and just substitute the values for all the constants. The second line
will be optimized to:

    double area = XX * r
XX is whatever the output of 2 * 22 / 7 for that specific 16/32/64 bit ISA


Compiler optimization must be used with care, as it may introduce
subtle bugs.  Here is an example:

You may be tempted to optimize the below code:

for (i = 0; i < 1000; i++)
    c = i;

to just

    i = 1000
    c = 999

But if "c" refers to the memory mapped address of a serial port, then
you've obviously left out a lot of data.

Compiler optimization also has problems when you are debugging. See
the man page for gcc's "-g" option.  Best to disable optimization
entirely while debugging "gcc -O0 -g".


While code optimization is a fairly advanced topic the moment, the
constant advances in programming languages and paradigms offers
an ever increasing opportunities and areas of study for the keen student.

Enjoy the ride!

- Raja
_______________________________________________
To unsubscribe, email [email protected] with
"unsubscribe <password> <address>"
in the subject or body of the message.
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to