C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 7th, 2012, 09:10 PM
KansaiRobot KansaiRobot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 18 KansaiRobot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 17 m 30 sec
Reputation Power: 0
Strange Things with your Optimizer! (help)

Have you ever had problems with the way your compiler optimize your code?
I have very strange things happening to my code and I suspect is the optimizer
(I am programming with HEW for a SuperH architecture)

For example a function like this:

Code:
static char bintochar(const int num)

{
char ch;
int elnum;

elnum=num&0x0F;
//num &= 0x0F;

if (0 <= elnum && elnum <= 9) 
ch = '0' + elnum;
    else if (10 <= elnum && elnum <= 15) 
ch = 'A' + elnum - 10;

return ch;
}


originally it only used num (not elnum). Since it is passed by value it is supposed that num wouldnt change even if its copy were modified inside the function. Well, it changed!

Then I put the const (as above) and used another variable (elnum) so as to assure that num would not change but the optimizer eliminated my variable and again made the parameter (passed by value) changed! Isnt that against all rules in C?!

Another example

Code:
static void afunction(const int media)
{
int i;
char buf[16];
unsigned long *mc;
unsigned char uc;
unsigned long mcd;

mc = (unsigned long*) 0xa00000F0;
mcd = *mc;  //HERE!!!!

buf[0] = 'M';

if(media==1)
 //more code
else
  //more code
//....more code
}


I debugged the code and found out that the optimizer made that the local variable mcd and the parameter media share the same memory region! (in fact the same register! therefore when I assign "mcd=*mc" not only mcd changes but media also changes!

I am finding all kinds of these strange things. Anyone has any idea why this is happening?

Thanks a lot in advance

Kansai

Reply With Quote
  #2  
Old May 7th, 2012, 11:34 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
Have you tried various optimization levels?

And no. Your example does not violate every rule in c.

About your code, since elnum is a positive integer less than 16 why do you test to make sure it is bigger than or equal to 0?
if (0 <= elnum && elnum <= 9)

And if the previous condition failed is it possible for this condition to fail?
else if (10 <= elnum && elnum <= 15)

No, so don't bother with the tests.

But if both tests failed, then you'd return ch as an uninitialized data.

And that's just wrong.

How about
Code:
static char bintochar(const int num) {
  return "0123456789ABCDEF"[num&0xF];
}
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old May 8th, 2012, 12:10 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
> Anyone has any idea why this is happening?
Yes, your code is likely to broken somewhere.

Post a complete example which breaks when optimised. We can't tell anything from uncompilable snippets.

> (I am programming with HEW for a SuperH architecture)
It could be a compiler bug since you're using an embedded compiler. I've found a few myself when turning on optimisations.

But if it is a compiler bug, you should focus on producing a really simple test case you can send back to them.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #4  
Old May 8th, 2012, 12:55 AM
KansaiRobot KansaiRobot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 18 KansaiRobot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 17 m 30 sec
Reputation Power: 0
Thanks for your replies

Well, I found what was the mistake. Apparently it was one of these simple to make hard to find errors once you make them.
I was comparing the parameter "media" to see if it was one of two values but in the middle I mixed the values (0 or 1) which threw off the optimizer.

So I defined two values

Code:
#define FIRSTV 0
#define SECONDV 1


and use those instead of 0 and 1 consistently. This corrected the bug I had created. Guess I should have listened to people who says never to use just numbers but well defined constants instead...

Reply With Quote
  #5  
Old May 8th, 2012, 01:10 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
Yeah, I just found your cross-post

Reply With Quote
  #6  
Old May 8th, 2012, 02:06 AM
KansaiRobot KansaiRobot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 18 KansaiRobot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 17 m 30 sec
Reputation Power: 0
Thank you but that does not qualify as a crosspost.
A crosspost is for example posting the same question in DevShed C forum and Java Forum.

Being that your post and this one is off-topic, that is the last I will say about this.

Thanks for your help again

Reply With Quote
  #7  
Old May 8th, 2012, 02:22 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
Quote:
Originally Posted by KansaiRobot
Have you ever had problems with the way your compiler optimize your code?

If the optimizer changes the behaviour of your code it is likley that your code is flawed or uses undefined behaviour. The most comon cause of failure when using an optimiser in embedded systems is failure to correctly declare variables volatile when necessary.

Quote:
Originally Posted by KansaiRobot
Isnt that against all rules in C?!
No, not at all. The const is an indication by you to the compiler that the function will not explicitly modify the variable. If you write code that does that, the compiler will complain. However the optimizer performs abstract analysis to mathematically prove the redundancy of any variable allowing it to be eliminated entirely. If it does not change the semantics of the code, it is allowed.

Quote:
Originally Posted by KansaiRobot
found out that the optimizer made that the local variable mcd and the parameter media share the same memory region! (in fact the same register! therefore when I assign "mcd=*mc" not only mcd changes but media also changes!
Optimisers are clever and complex things, and will eliminate variables, reorder code and re-use memory in ways that are hard to fathom. Attempting to step optimised code even at the assembler level in a debugger is seldom profitable for any purpose other than debugging the optimiser, and at the source level it is unlikely to make any sense whatsoever - debuggers cannot usually cope with optimised code in any meaningful manner.

The only test that is valid is whether the function generates the correct result for all valid input. If it does then your analysis of the optimiser behaviour is incomplete, if it does not then there either the code is semantically flawed or uses undefined behaviour, or possible, but least likely; the optimiser has a bug.

To get a feel for the complexity of optimisation take a look at this overview for example.

Reply With Quote
  #8  
Old May 8th, 2012, 03:51 PM
SimonB2 SimonB2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 249 SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Days 17 h 55 m 47 sec
Reputation Power: 110
Quote:
Originally Posted by KansaiRobot
Thank you but that does not qualify as a crosspost.
A crosspost is for example posting the same question in DevShed C forum and Java Forum.


Go home and dry behind your ears.
Cross Posting

Reply With Quote
  #9  
Old May 8th, 2012, 07:38 PM
KansaiRobot KansaiRobot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 18 KansaiRobot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 17 m 30 sec
Reputation Power: 0
Thank you for your answer.

As you said, the code was flawed. (I indicated the flaw as well: by using numbers instead of constants, I have mistakenly mixed the two of them. Corrected it by using properly defined constants).

Since this is a forum for developers, I was hoping that someone would have had a similar experience, and would have indicated a likely mistake as you did. Thanks again.

(I am having another strange behaviour in a function in which some memory is being modified instead of another, but I suppose that is some other flaw somewhere...)


Quote:
Originally Posted by clifford
If the optimizer changes the behaviour of your code it is likley that your code is flawed or uses undefined behaviour. The most comon cause of failure when using an optimiser in embedded systems is failure to correctly declare variables volatile when necessary.

No, not at all. The const is an indication by you to the compiler that the function will not explicitly modify the variable. If you write code that does that, the compiler will complain. However the optimizer performs abstract analysis to mathematically prove the redundancy of any variable allowing it to be eliminated entirely. If it does not change the semantics of the code, it is allowed.

Optimisers are clever and complex things, and will eliminate variables, reorder code and re-use memory in ways that are hard to fathom. Attempting to step optimised code even at the assembler level in a debugger is seldom profitable for any purpose other than debugging the optimiser, and at the source level it is unlikely to make any sense whatsoever - debuggers cannot usually cope with optimised code in any meaningful manner.

The only test that is valid is whether the function generates the correct result for all valid input. If it does then your analysis of the optimiser behaviour is incomplete, if it does not then there either the code is semantically flawed or uses undefined behaviour, or possible, but least likely; the optimiser has a bug.

To get a feel for the complexity of optimisation take a look at this overview for example.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Strange Things with your Optimizer! (help)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap