Solved

gcc code optimization

Posted on 2014-03-07
3
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

623 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