diff options
Diffstat (limited to 'tools/js-optimizer.js')
-rw-r--r-- | tools/js-optimizer.js | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 222bd0f4..e62402a9 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -1396,13 +1396,16 @@ function registerize(ast) { // FUNCTION_TABLE[x](); // // to be optimized (f could replace FUNCTION_TABLE, so in general JS eliminating x is not valid). +// +// In memSafe mode, we are more careful and assume functions can replace HEAP and FUNCTION_TABLE, which +// can happen in ALLOW_MEMORY_GROWTH mode var ELIMINATION_SAFE_NODES = set('var', 'assign', 'call', 'if', 'toplevel', 'do', 'return'); // do is checked carefully, however var NODES_WITHOUT_ELIMINATION_SIDE_EFFECTS = set('name', 'num', 'string', 'binary', 'sub', 'unary-prefix'); var IGNORABLE_ELIMINATOR_SCAN_NODES = set('num', 'toplevel', 'string', 'break', 'continue', 'dot'); // dot can only be STRING_TABLE.* var ABORTING_ELIMINATOR_SCAN_NODES = set('new', 'object', 'function', 'defun', 'switch', 'for', 'while', 'array', 'throw'); // we could handle some of these, TODO, but nontrivial (e.g. for while, the condition is hit multiple times after the body) -function eliminate(ast) { +function eliminate(ast, memSafe) { // Find variables that have a single use, and if they can be eliminated, do so traverseGeneratedFunctions(ast, function(func, type) { //printErr('eliminate in ' + func[1]); @@ -1613,7 +1616,7 @@ function eliminate(ast) { } } } else if (type == 'sub') { - traverseInOrder(node[1], false, true); // evaluate inner + traverseInOrder(node[1], false, !memSafe); // evaluate inner traverseInOrder(node[2]); // evaluate outer if (!ignoreSub) { // ignoreSub means we are a write (happening later), not a read // do the memory access @@ -1834,6 +1837,10 @@ function eliminate(ast) { new ExpressionOptimizer(ast).run(); } +function eliminateMemSafe(ast) { + eliminate(ast, true); +} + // Passes table var compress = false, printMetadata = true; @@ -1852,6 +1859,7 @@ var passes = { loopOptimizer: loopOptimizer, registerize: registerize, eliminate: eliminate, + eliminateMemSafe: eliminateMemSafe, compress: function() { compress = true; }, noPrintMetadata: function() { printMetadata = false; } }; |