Solved

gcc code optimization

Posted on 2014-03-07
3
173 Views
Last Modified: 2014-07-09
I have a C function like below which is part of a file xyz.c and the file is compile using O2 optimization using gcc. The function GetValue() is called almost 200million times  to generate specific data. But for this specific operation cond1 is passed as false, hence code under this condition is never exercise. But, why there is lot of CPU difference in executing GetValue function, when the code chunk 'abc' is commented vs un-commented eventhough the code in that block under (cond1 == true) is not exercised at all.

if the code is commented then GetValue() take - 100 cpu seconds
                     uncomented then GetValue() take  - 120 cpu seconds why ? Is there any problem with GCC optimization ?

inline GetValue(int cond1)
{

   if (cond1 == true)
  {

      // -- code chunk 'abc'--

   }
}

Open in new window

0
Comment
Question by:effiqua
  • 2
3 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39913538
Hi effiqua,

Is --code chunk-- the only thing in the *if* statement?  If so, the optimization may well be eliminating the test as it's do-nothing code.


Kent
0
 

Author Comment

by:effiqua
ID: 39914182
The cond1 is determined dynamically. In this case cond1 is passed as false while calling GetValue().
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 39914701
That's not quite the question...

does your function look like this:

inline GetValue(int cond1)
{
   if (cond1 == true)
  {

      // -- code chunk 'abc'--

   }
}

Open in new window

or this:
inline GetValue(int cond1)
{

   if (cond1 == true)
  {
      some code here
      // -- code chunk 'abc'--
      or maybe some code here
   }
}

Open in new window

                                                                   
The optimizer should reduce the code to its simplest form.  If your code looks like the first example (there is no executable code in the *if* block, except the actual true/false test) the optimizer will detect that the block is meaningless code and generate no executable instructions for it.  And because the function is declares as *inline*, there isn't even a function call.  The entire block reduces to no executable code.

In a lot of ways, an inline function behaves more like a macro than a function.  It's written as a function, which makes it easy to write complex logic that looks like any other function.  But at compile time, it expands much like a macro (with parameter substitution).

Consider this example:

inline GetValue(int cond1)
{
   if (cond1 == true)
  {
      printf ("cond1 is true\n");
   }
}

inline GetValue2(int cond2)
{
   if (cond2 == true)
  {
//      printf ("cond2 is true\n");
   }
}
#define false=0
#define true=(!false)

int  iVar;

main ()
{
  GetValue (0);
  GetValue (1);
  GetValue (true);
  GetValue (false);
  GetValue (iVar);

  GetValue2 (0);
  GetValue2 (1);
  GetValue2 (true);
  GetValue2 (false);
  GetValue2 (iVar);
}

Open in new window


The compiler' optimized code is something like this:

// main
// GetValue (0);
// GetValue (1);
      printf ("cond1 is true\n");
// GetValue (true);
      printf ("cond1 is true\n");
// GetValue (false);
// GetValue (iVar);
  if (iVar)
    printf ("cond1 is true\n");
//  GetValue2 (0);
//  GetValue2 (1);
//  GetValue2 (true);
//  GetValue2 (false);
//  GetValue2 (iVar);

Of the 10 inline calls, only 3 result in executable code.  And only 1 actually execute the *if* statement.

When a constant is passed to the inline function, it can be evaluated at compile time and the code generated or not generated depending on the value of the constant.  There's no need to retest the constant at execution time.

When the inline function has no executable code other than the *if* statement, the test is superfluous so it's optimized out.  Since that's the only thing in GetValue2, no executable code is generated.


Good Luck,
Kent
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question