Re: Eliminating unused code (Compiler/language design)
Posted: Tue Jun 24, 2014 2:11 pm
I wrote up a simple example that has many potential optimization cases:
somethingNotPure has side effects (it modifies i), while somethingPure does not. 'c' is never used, so 'c += somethingPure(q);' should be optimized away.
That's 176 instructions, unoptimized, in SSA form, here: http://pastebin.com/yAJeaTSQ The push/parameter instructions indicate variables coming in or transferring to another basic block (my form of the SSA phi symbol) - also used for calling functions. Most of these should be optimized away, as value that aren't needed shouldn't be passed around. 'var one = 3 - 2;' can be turned into a constant and does not need to be passed around either.
I've never tackled SSA optimization before, so I feel like I'm jumping into the deep end of the pool to learn how to swim.
My plan is to optimize on a per-function basis, marking every SSA instruction as 'unused', then iterate, linearly over each basic block and instruction, and when I encounter either: a return, a memory write, or a non-pure function call, I will mark each parameter, which in turn will either a) recursively mark it's dependencies, or b) propagate up a constant value.
Code: Select all
var i = 0;
var somethingNotPure = function(b) { i += b; };
var somethingPure = function(abc) { return abc; };
var f = function(a) {
var q = 2;
var one = 3 - 2;
var b = somethingPure(a + one) * q;
var c = 0;
for(var i = 0; i < 10; i++) {
b += somethingPure(a + one);
somethingNotPure(b);
c += somethingPure(q);
}
return b;
};
f(100);
That's 176 instructions, unoptimized, in SSA form, here: http://pastebin.com/yAJeaTSQ The push/parameter instructions indicate variables coming in or transferring to another basic block (my form of the SSA phi symbol) - also used for calling functions. Most of these should be optimized away, as value that aren't needed shouldn't be passed around. 'var one = 3 - 2;' can be turned into a constant and does not need to be passed around either.
I've never tackled SSA optimization before, so I feel like I'm jumping into the deep end of the pool to learn how to swim.
My plan is to optimize on a per-function basis, marking every SSA instruction as 'unused', then iterate, linearly over each basic block and instruction, and when I encounter either: a return, a memory write, or a non-pure function call, I will mark each parameter, which in turn will either a) recursively mark it's dependencies, or b) propagate up a constant value.