This is not an easy answer. But let it try anyway.
In a more βgeneralβ approach, you ask how to know what will / will be optimized and what will not? - Of course, as a responsible developer, you should assume that nothing will be optimized for you, and the creation of optimized code completely depends on your responsibility.
Having said that, we, of course, know that some things are automatically optimized.
Optimization can occur in 3 distinct cases:
During build version of Debug.
During build version of Release.
By running the NGEN utility in an already created file.
Optimization can be described as a bow - the outer shell will always include inner shells and add something of its own.
So,
What types of optimization can occur during debugging?
Only optimizations that will not change the separation of code for the user who is debugging.
In the example: if(a==!true && a==false)
a==!true is identical to a==false - therefore, having only one of them is a good idea, but if such optimization is performed, the developer will not be able to step by step expose the expression, as it was before optimization - therefore, such optimization will not be performed in the debug version.
However .. a==!true - only this autonomous part can be replaced with a==false below, without violating the separation of expressions - this means that from the point of view of the compiler this will be fine:
if(a==!true && a==false) to is possibly a faster set of instructions, which is actually 26>, since the separation of the expression will not be removed, and the debugging or intervention will not change.
What types of optimizations may occur in a release?
If we return to the same example - in the release - the expression if(a==!true && a==false) can be optimized to if(a==false) , since it is predetermined that the release is not intended for debugging, so the compiler has nothing worry about doing this optimization.
What type of optimization can happen in NGEN?
This is a completely different vertical ...
NGEN can be done for debugging or for release, that is, the same rules apply as before, and this is actually executed by the JIT when creating the code. NGEN Job - optimize the code that it finds for its own (current) processor processor architecture. Here I will not talk about it, you can read all of this from here .
Keep in mind: if an expression cannot be superimposed on parts of it, it is already optimized.