• C

gcc code optimization

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

effiquaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
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
effiquaAuthor Commented:
The cond1 is determined dynamically. In this case cond1 is passed as false while calling GetValue().
0
Kent OlsenDBACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.