I reduced my code to the next one, which is as simple as I could do it, while preserving the compiler that interests me.
void foo(const uint64_t used) { uint64_t ar[100]; for(int i = 0; i < 100; ++i) { ar[i] = some_global_array[i]; } const uint64_t mask = ar[0]; if((used & mask) != 0) { return; } bar(ar);
Using VC10 with / O 2 and / Ob1, the generated assembly largely reflects the order of instructions in the above C ++ code. Since the local ar array is only passed to bar() when the condition fails and is not used otherwise, I expected the compiler to optimize something like the following.
if((used & some_global_array[0]) != 0) { return; } // Now do the copying to ar and call bar(ar)...
Does the compiler not do this because it is too difficult to define such optimizations in the general case? Or does this follow from some strict rule prohibiting it? If so, why, and is there any way I can give him a hint that this will not change the semantics of my program?
Note: obviously, it would be trivial to get optimized output by simply rearranging the code, but I wonder why the compiler will not optimize in such cases, and not how to do it in this (intentionally simplified) case.
c ++ optimization compiler-optimization visual-c ++
kamrann
source share