Variable Optimization

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello,

   

PSoc creator has an issue where so-called unused variables are optimized out despite the fact they are written to more than once. C compilers are not supposed to do this. Is there something I can do without having to turn off optimization?

0 Likes
6 Replies
Anonymous
Not applicable

A further note, making the variable volatile does not help, weird.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

It doesn't matter how often you write to a variable - when you never read from it, it is unused and compiler can optimize if. The same is true for all writes except the last one - they are useless and will be optimized out.

   

So when you want to see, during debugging, the value, make sure you use the variable somewhere later on.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

In Creator under

   

Project -> Build Settings -> Compiler

   

you'll find options to set optimization levels depending on the chip you use PSoC3 or 5. I would suggest to minimize the optimization for the debugging build and maximize them for the release.

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Precaution, keep in mind SW based timing loops can be affected by

   

optimization settings, so be diligent. At minimum look at .lst file before

   

and after. Or convert them to HW, or hard code them in ASM if in doubt.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 As per KEIL's KB article on various Optimization levels, unused code fragments are getting eliminated if set to level 1 or above. So, if you really want to keep a variable undeleted eventhough you are not going to use it anywhere, you might want to set the Optimization level to 0 and try it out.

0 Likes
Anonymous
Not applicable

 Moreover, you might want to take a look at this Application Note on PSoC 3 - 8051 code Optimization.

0 Likes