Problems with the Compiler Optimizer?

This is a discussion on Problems with the Compiler Optimizer? within the C Programming forums, part of the General Programming Boards category; Have you ever had problems with the way your compiler optimize your code? I have very strange things happening to ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    19

    Problems with the Compiler Optimizer?

    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';
    
    
    	//....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

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Compile (with whatever changes are necessary) and run this program.
    Post the exact code that you ran and the disassembly.
    Code:
    static char bintochar(int num)
    {
    	char ch = 'X';
    
    	num &= 0x0F;
    
    	if (0 <= num && num <= 9)
    		ch = '0' + elnum;
    	else if (10 <= num && num <= 15)
    		ch = 'A' + num - 10;
    
    	return ch;
    }
    
    int main(void)
    {
    	char ch;
    	int num = 11;
    
    	ch = bintochar(num);
    	putchar(ch);
    	putchar(num + 'a'); // you could try with and without this statement
    
    	return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Thank you for your response

    Well, I found what was the mistake. Apparently it was one of these hard to find errors once you made it.

    In the second example 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. In other words I didnt use the values consistently.


    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.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    Given masking with 0x0F cannot produce a result less than zero or greater than 15, I would write the function body as:
    Code:
    {
        num &= 0x0F;
        return num < 0xA ? num + '0' : num - 0xA + 'A';
    }
    I also make a point of using 0xA since it's clearer that you're translating from 0xA to 'A'.
    Last edited by iMalc; 05-08-2012 at 12:15 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    regarding what happens to your variables under optimization in your first two examples, if your parameter 'num' or 'media' is not used later in the function, the optimizer can decide that the variable is no longer 'live' and doesn't need to save its value. if you used the const variable again at the end of your function, you would find that it still had the value you expect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL / EXE optimizer
    By audinue in forum Tech Board
    Replies: 3
    Last Post: 01-22-2010, 10:34 PM
  2. compiler problems.
    By durr256 in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2006, 02:09 PM
  3. CC and GCC compiler problems
    By gayomard_mehta in forum Linux Programming
    Replies: 3
    Last Post: 12-07-2004, 02:49 PM
  4. Help- Problems running outside of compiler
    By Derek5272 in forum C++ Programming
    Replies: 17
    Last Post: 05-07-2003, 08:19 AM
  5. compiler problems
    By stormswift in forum C Programming
    Replies: 1
    Last Post: 02-25-2002, 02:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21