Adam Oldham

Optimize Code Incorrect.... CW 7.1.1

Discussion created by Adam Oldham on Dec 4, 2009
Latest reply on Dec 5, 2009 by CompilerGuru

Hi All, I have a rather large project (30 million lines of code) in C and C++ that I use CW 7.1.1 with.  In fixing a defect in the code, I have found a rather annoying problem in one of my classes, here's some sample code.

 

In all my functions across the clases for a specific external data type (that is more like a static, than it is belonging to a class), the optimizer always optimizes out the code.  For example, thiis is the code:

 

enum

{

   value1 = 1,

   value2 = 2

};

 

int val;

 

GetVal(&val);

 

if (val == value1)

{

   // do this, assume valid code here

}

else

{

  // do that, assume valid code here

}

 

For this, when I call GetVal, the value in the running code can be 1 or 2 depending on conditions.  However, the IF portion above is always optimized out while the else is the code that is left to run every time.

 

If tried switch it so that it is if (val == value2) and it gives me the same thing.  I tried using the values such as 1 and 2 instead of an enum, but still uses the else only (in fact when debugging, you can't even set a breakpoint in the if portion).

 

This is the same in all 7 locations in the code that the above is used.  We use many other get functions performing a similar task and then do comparions to an enum, but they always are left intact (if or else would run).

 

Can anyone offer suggestions to why the compiler is optimizing it out?  The if statement is valid, the data stored in the variable is valid, the enum is valid, the if statement itself is valid and the code in the IF and ELSE is valid.

 

I can't figure it out.

Outcomes