S12 unexpected code optimisation

cancel
Showing results for 
Search instead for 
Did you mean: 

S12 unexpected code optimisation

Jump to solution
1,794 Views
colinh
Contributor I
Hi

I am getting some puzzling optimisation from CodeWarrior HC12 4.5

In the following routine the optimiser decides to remove the second test of the resultant read from ESTAT. I get the "Removed dead code" message but I cannot see the rationale behind the optimisation (maybe I've been working too late!) After the code listing I have also shown the listing extract.

Any ideas?

Thanks
Colin

Code:
uint8 EE_Command(uint16* address, uint16 data, uint8 command){uint8 eeStatus;uint8 result = 0x00;*address = data;ECMD = command; ESTAT = ESTAT_CBEIF_MASK;eeStatus = ESTAT;if(eeStatus & ESTAT_PVIOL_MASK){ESTAT = ESTAT_PVIOL_MASK; result = ESTAT_PVIOL_MASK; }else if(eeStatus & ESTAT_ACCERR_MASK){ESTAT = ESTAT_ACCERR_MASK; // *** optimised outresult = ESTAT_ACCERR_MASK; // *** optimised out}else {while(!ESTAT_CCIF){// do nothing }}return result;}42: if(eeStatus & ESTAT_PVIOL_MASK)0017 8420 [1] ANDA #320019 2709 [3/1] BEQ *+11 ;abs = 002443: {44: ESTAT = ESTAT_PVIOL_MASK; 001b 8620 [1] LDAA #32001d 7a0000 [3] STAA _ESTAT45: result = ESTAT_PVIOL_MASK;0020 6a81 [2] STAA 1,SP46: }0022 2005 [3] BRA *+7 ;abs = 002947: else if(eeStatus & ESTAT_ACCERR_MASK)48: {49: ESTAT = ESTAT_ACCERR_MASK; 50: result = ESTAT_ACCERR_MASK;51: }52: else 53: {54: while(!ESTAT_CCIF)0024 1f000040fb [5] BRCLR _ESTAT,#64,*+0 ;abs = 002455: {56: // do nothing 57: }58: }59: 60: return result;0029 e681 [3] LDAB 1,SP61: }

 
(Alban formatted code for lisibility - Use SRC button)

Message Edited by Alban on 2006-08-30 03:36 PM

Labels (1)
Tags (1)
0 Kudos
1 Solution
152 Views
CrasyCat
Specialist III
Hello
  This needs to be investigated by our support team.
  Depending on the way the different elements are defined, the compiler may optimized code out.
 
  Please submit a service request through our on line support site (http://www.freescale.com/webapp/sps/site/homepage.jsp?nodeId=054670&tid=FSH#online_self-help).
 
Make sure to provide a project reproducing the trouble (or at least the preprocessor listing of the source file where the function is implemented), We will also need to know which version of CodeWarrior you are using and the options you are using to build the application.
 
To retrieve CodeWarrior version number:
  - Start the IDE
  - Select "Help" -> "About Metrowerks CodeWarrior " 
  - Click on "Installed Products"
  - Click on Save As and save the information in a text file that you send with your service request.
 
CrasyCat

View solution in original post

0 Kudos
4 Replies
153 Views
CrasyCat
Specialist III
Hello
  This needs to be investigated by our support team.
  Depending on the way the different elements are defined, the compiler may optimized code out.
 
  Please submit a service request through our on line support site (http://www.freescale.com/webapp/sps/site/homepage.jsp?nodeId=054670&tid=FSH#online_self-help).
 
Make sure to provide a project reproducing the trouble (or at least the preprocessor listing of the source file where the function is implemented), We will also need to know which version of CodeWarrior you are using and the options you are using to build the application.
 
To retrieve CodeWarrior version number:
  - Start the IDE
  - Select "Help" -> "About Metrowerks CodeWarrior " 
  - Click on "Installed Products"
  - Click on Save As and save the information in a text file that you send with your service request.
 
CrasyCat

View solution in original post

0 Kudos
152 Views
colinh
Contributor I
Hi Alvin
 
I've just had 2 cans of Coke so I should be at my peak :smileyhappy:  - Correct me if I'm wrong but it is a single & that does a bit wise AND between two entities.  && is used to test that two logical conditions are both true. 
 
Therefore in my code the first test should see if ESTAT_PVIOL_MASK is set, the second test should see if ESTAT_ACCERR_MASK is set.  The & test should not modify eeStatus so as far as I am concerned it should test PVIOL, and if it is not set it should then proceed to test ACCERR, finally if neither was set it should do the while loop.
 
Cheers
Colin
0 Kudos
152 Views
Nabla69
Contributor V
Colin,

I guess I should trade my Frenchie double espresso for a sweeter drink then... :s

Completely agree with you that the single & is for bite wise operation while the double && is for logical ones.

As a workaround to force its compilation, I think there is a switch for the compiler NOT to optimize "dead code". This will work if only the compiler is OK and the optimization is over optimistic :smileyhappy:

Another way would be to change the syntax for the software to do the same thing but in a different manner:

if (eeStatus & ESTAT_ACCERR_MASK) with if (!(~eeStatus | ~ESTAT_ACCERR_MASK))...

You never know :smileywink:
Alvin - Non Coke Drinker
152 Views
Nabla69
Contributor V
Hi Colin,
 
I can confirm you have been working too late.
 
The "elseif" is indeed dead code because of its condition.
You are using & and you probably meant a && to use with a bit mask.
 
Am I right ?
Alvin.

Message Edited by Nabla69 on 2006-08-30 09:42 AM

0 Kudos