What's new

Over optimization

Doomulation

?????????????????????????
I was kinda wondering here...has anyone experienced the compiler optimizing "too much" when using c++? At least the compiler I'm using, as MSVC++.net.
Because I'm having trouble with its over-optimization. Now I don't remember much asm, so I don't really know how to fix it.
The thing is, that the compiler is over optimizing and removing some code used to intialize a structure. At least the bit flag field.

Kindof like this:

Code:
int fnc(..., byte flags)
{
  some_struct struct;
  struct.bytefield  = someflag1|someflag2|flags; <--- here's the problem
  ...
}

According to the assembly generated, the "flags" argument is passed in the cl register. Now, it doesn't save this register, but overwrites it earlier in the function for other purposes. Thus, when it comes to that line, it skips it completly. No code at all.
I was wondering how to fix this.
Perhaps some asm optimization to use another register or push the value to stack somehow? I don't remember how to retrieve data from the stack, however...

Some help would be greatly appreciated.
 
OP
Doomulation

Doomulation

?????????????????????????
Well, that's what I'm currently doing I'm afraid. Disabling global optimization for the function. Still, this means it doesn't get optimized. Some asm would probably be loads helpful, but I'm still an apprentice in that area.
 

smcd

Active member
If you know that it is the CL register that is being used, and that it is being corrupted earlier, you can do some inline assembly in there... __asm PUSH CL before the location where it gets used/corrupted then later (before the initialization) do __asm POP CL to restore the previous value.
 

Top