Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will an optimizing compiler remove calls to a method whose result will be multiplied by zero?

Tags:

optimization

Suppose you have a computationally expensive method, Compute(p), which returns some float, and another method, Falloff(p), which returns another float from zero to one.

If you compute Falloff(p) * Compute(p), will Compute(p) still run when Falloff(p) returns zero? Or would you need to write a special case to prevent Compute(p) from running unnecessarily?

Theoretically, an optimizing compiler could determine that omitting Compute when Falloff returns zero would have no effect on the program. However, this is kind of hard to test, since if you have Compute output some debug data to determine whether it is running, the compiler would know not to omit it because of that debug info, resulting in sort of a Schrodinger's cat situation.

I know the safe solution to this problem is just to add the special case, but I'm just curious.

like image 663
Tim R. Avatar asked Dec 22 '10 07:12

Tim R.


1 Answers

Generally speaking a compiler will know that a function call could have side-effects (infinite loops, exceptions, etc.) and will not optimize it out. On the other hand, there are such things as whole-program optimizers that can determine that a function has no side-effects and thus omit it when its return value is not used.

Note that if your function returns an IEEE float and it is multiplied by 0, you cannot safely omit the function call unless you can determine that it always returns a real number. If it can return an Inf or NaN, the multiplication by 0 is not a nop and must be performed.

like image 68
Gabe Avatar answered Sep 25 '22 02:09

Gabe