Widely Vision

  • Real History of Israel

  • Poll

  • Get a PDF version

  • Follow This Blog

  • June 2010
    S M T W T F S
    « Feb   Jun »

Optimization in GCC(1)

Posted by aboelnour on June 17, 2010

peace be upon you:

We will talk about the GCC optimization of your code in the incoming posts isA.

At first we have to know what happing when we compile a source code to run it with GCC:

• Preprocessing (to expand Macros)
• Compilation (from source code to assembly language)
• Assembly (from assembly language to machine code)
• Linking (to create the final executable)

There is an addition step the GCC do it Called “Optimization”.Optimization is a complex process. For each high-level command in the source code there are usually many possible combinations of machine instructions that can be used to achieve the appropriate final result. The compiler must consider these possibilities and choose among them.

In general, the generated code differ from CPU to another because Each type of processor also has its own characteristics some CPU’s provide a large number of registers for holding intermediate results of calculations,others must store and fetch intermediate results from memory.

so there is some steps the GCC which follow it to optimize your code:

1) Source-level optimization:

This form of optimization used by GCC occurs at the source code level, and does not require any knowledge of the machine instructions. There are many source-level optimization techniques we will took two common types: common subexpression elimination and function inlining.

1.1) Common subexpression elimination:

One method of source-level optimization which is easy to understand involves computing an expression in the source code with fewer instructions,by reusing already-computed results. For example, the following assignment:

x = cos(v)*(1+sin(u/2)) + sin(w)*(1-sin(u/2))

can be rewritten with a temporary variable t to eliminate an unnecessary extra evaluation of the term sin(u/2):

t = sin(u/2)
x = cos(v)*(1+t) + sin(w)*(1-t)

Common subexpression elimination (CSE) is powerful, because it simultaneously increases
the speed and reduces the size of the code.

1.2) Function inlining:

Another type of source level optimization, called function inlining, increases the efficiency of frequently-called functions.

Whenever a function is used, a certain amount of extra time is required
for the CPU to carry out the call: storing the function arguments, jumping to the start of
the function ,executing the code, and then return to the original point of execution when the function call is complete. This additional work is referred to as function-call overhead.

Function inlining eliminates this overhead by replacing calls to a function
by the code of the function itself (known as placing the code in-line).

The following function sq(x) is a typical example of a function that
would benefit from being inlined. It computes x^2 :

sq (double x)
return x * x;

This function is small, so the overhead of calling it is comparable to the
time taken to execute the single multiplication carried out by the function
itself. If this function is used inside a loop, such as the one below, then
the function-call overhead would become big:

for (i = 0; i < 1000000; i++)
sum += sq (i + 0.5);

Optimization with inlining replaces the inner loop of the program with
the body of the function, giving the following code:

for (i = 0; i < 1000000; i++)
double t = (i + 0.5); /* temporary variable */
sum += t * t;

Eliminating the function call and performing the multiplication in-line allows the loop to run with maximum efficiency.


This is the first part the second one we will know another steps in optimization isA.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: