8 ms·
> Using separate variables instead of reusing the same variable will sometimes actually increase the optimizations that compilers can do LLVM optimizations, fo
by lou1306 7y ago
> Using separate variables instead of reusing the same variable will sometimes actually increase the optimizations that compilers can do
LLVM optimizations, for instance, often need the IR to be in SSA form (static single Assignment).
- strbean 7y agoIf you're converting it to SSA form, is there really a difference between int a = 5 int b = 2 * a // don't use a again and > int a = 5 > a = 2 * a ?
- lou1306 7y agoI don't think I really understand the question, but please bear with me. Indeed both code snippets are the same computation. The first snippet is SSA, the second is not. Notice that doing, for instance, constant propagation on the first snippet is straightforward: int a = 5 int b = 2 * 5 You can do this because SSA form guarantees that the value of a is 5. In non-SSA code, there is no such guarantee: you need to check the "history" of each variable before replacing it with a value. I'm not totally convinced that SSA "increases the optimizations that compilers can do". However, it definitely makes optimization algorithms easier.
- strbean 7y agoI guess my point was that the optimizations available are the same (between those two snippets of code) if you end up in SSA form. Thinking about it now, I don't know what optimizations would be done before going to SSA and how they would be affected.