AnsweredAssumed Answered

Inlined method not optimized ideally

Question asked by Haje on Mar 7, 2017
Latest reply on Apr 18, 2017 by kennie

Hi

I have a large method that I would like to break up into smaller methods for reuse. Unfortunately, the resulting code is less efficient (code size).

Basically, the methods are range checks of process values. If a value is out of range, the method shall return with an appropriate code. If I use methods, I add a comparison (if) but I would expect the compiler to be able to merge those comparisons into one but looking at the assembly, it does not.

 

Here is a condensed example:

-------------------------------------------------------------------------------

// All in one method

int Checks() {

   if (a > b)

      return 1;

  

   if (c > d) 

      return 2;

  

   ... more checks

 

   return 0;   // everything ok

}

-------------------------------------------------------------------------------

// Checks extracted into small methods

inline int CheckAB()

{

   if (a > b) 

      return 1; 

   return 0;

}

 

int Checks() {

   int returnCode = CheckAB();

   if (returnCode != 0) return returnCode;

 

   returnCode = CheckCD();

   if (returnCode != 0) return returnCode;

 

   ... more checks

 

   return 0;   // everything ok

}

 

I attached an example project. It gives following assembly output:

Disassembly with one large method:

    r2=dm(_testVal);
    r1=0x40e00000;
    comp(f1,f2), r0=m5;
    if lt r0=m6;
    r2=pass r0;

 

Disassembly with inlined method:

    r2=dm(_testVal);
    r1=0x40e00000;
    comp(f1,f2), r0=m5;
    if lt r0=m6;
    r2=pass r0;
    if eq jump (pc, 0x4);

 

Does anyone know why? Is there a better way to write this code or a compiler setting that would give the same results as when writing one large method?

I'm using VisualDSP++ 5.1.0.0, Target is SHARC 21371.

 

I appreciate any input.

Thanks!

hans

Outcomes