Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

gcc code optimization

Posted on 2014-03-07
3
Medium Priority
?
177 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 46

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 46

Accepted Solution

by:
Kent Olsen earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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 while-loops in the C programming language.

971 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