Solved

gcc code optimization

Posted on 2014-03-07
3
168 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:Kdo
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:
Kdo 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now