Optimization

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Optimization

1,525 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by jdurand on Mon Sep 23 18:10:24 MST 2013
I have to use some optimization on my current code to make it fit (-O1 saves about half the space!), the problem is lately optimizers throw away a LOT of meaningful code.  For a couple of decades I ran compilers with optimization on and they worked fine, but lately (mostly since I moved to ARM processors) they're throwing away variables, even globals that are needed (written to by one function, read by another).

Is there one of the optimization levels that will remove redundant code but leave the variables alone?
0 Kudos
Reply
9 Replies

1,495 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by jdurand on Thu Sep 26 10:43:31 MST 2013
Thanks for pointing to the list in help, it looks like you've actually done a pretty good job of fleshing out the help file.  That's unusual.

Anyway, I may have missed it but I didn't see an option to NOT assume any variable is a constant (essentially treat every variable as volatile without having to tag every one and then deal with the warnings).  I have no problem with a variable being moved around between static memory, stack, and registers.  My issue comes where I change a variable in one part of a function and find it no longer has any effect later on because the compiler thought it was a constant and tossed it.

In the case I mentioned, I read a structure from EEPROM and then act on the data in the structure.  The compiler tossed the whole structure which sort of makes the program pointless.

Marking all my variables VOLATILE has fixed it, so I'll just keep that in mind when I need to use optimization.
0 Kudos
Reply

1,495 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Wed Sep 25 09:38:07 MST 2013
A complete list of all of the options enabled by each optimizations level can be found in the Help. Each of the optimizations is also described. You can disable an optimization by prefixing it with "no-".
0 Kudos
Reply

1,495 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by jdurand on Wed Sep 25 09:09:15 MST 2013
program is a structure loaded from EEPROM, so no idea what value is in it.  The structure is loaded by a call to the EEPROM READ function just before the SWITCH is done on program.command

A command of 'L' begins a loop and 'N' is the NEXT like you might find in BASIC.  The LOOP array allows nesting of loops.

I've sprinkled VOLATILE all over and I can now optimize it. Well, after fixing all the warnings VOLATILE created all through the program.

I STILL like the option I've had on other compilers to optimize without deleting variables, especially ones I'm obviously using.  After all, I only had it on -O1, it's not like I said to do a full optimization.  I guess it's the modern way, remove options for the user.
0 Kudos
Reply

1,495 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by wmues on Tue Sep 24 12:50:37 MST 2013
Hmmm... what value has program.data32 ?
If the value is <= 0, then "if(loop.count > 0)" can not be true, and
all code has no effect.

It's difficult to see the cause of the problem with incomplete listings...
0 Kudos
Reply

1,495 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Tue Sep 24 11:21:54 MST 2013
As has been mentioned in this thread already, you must use volatile if you don't want the compiler to optimise this away. The compiler 'knows' that the variables have no use and no side effects and so discards them. Use volatile to stop this. You will find the behavior in any half-decent optimizing compiler.

volatile struct {...} loop ;
0 Kudos
Reply

1,495 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by jdurand on Tue Sep 24 07:25:41 MST 2013
Here's the latest one I ran across, not a global but still used:

struct {
  int16_t start;
  int count;
} loop;

// init
loop.count = 0;
loop.start = 1;

...

switch(...

case 'L' : {
  loop.count = program.data32;
  loop.start = Record;
  break;
}
...

case 'N' : {
  loop.count--;
  if(loop.count > 0) {
    Record = loop.start;
  } else {
    loop.count = 0;
    loop.start = 1;
  }
  break;
}



when I was stepping through the code I had it set up so I hit the L case first and then the N case...only to find the loop had been optimized out.  I've added nesting so now loop is an array (loop[4]) and the L and N cases push and pop the loop stack.  Not the most efficient but fine for this part of the code.

With optimization off, it all works fine.  With it on, loop is gone and the program gets rather boring.

I'm using "LPCXpresso v6.0.2 [Build 151] [2013-09-18]" under Ubuntu 12.04LTS
0 Kudos
Reply

1,495 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by wmues on Tue Sep 24 02:00:30 MST 2013
Please give an example. Do you use gcc?

I have not seen gcc to produce errors here. In all cases I have seen, the programmer has made mistakes.
0 Kudos
Reply

1,495 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by graynomad on Tue Sep 24 01:53:52 MST 2013
Yes anything marked as "volatile" should not be touched by the compiler/optimiser.

But once all that returns you won't have the increase in space you had before of course :)

_____
Rob
0 Kudos
Reply

1,495 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by js-nxp on Mon Sep 23 23:44:34 MST 2013

Quote:
lately optimizers throw away a LOT of meaningful code

Usually what the programmer deems as useful the compiler thinks otherwise. :-)

Making those variables "volatile" should fix things, at least that's the case with other compilers I have worked with, particularly if variables are shared between interrupt service routine and main code.
0 Kudos
Reply