diff --git a/tools/js_optimizer.py b/tools/js_optimizer.py index db7228efe..8252a8387 100644 --- a/tools/js_optimizer.py +++ b/tools/js_optimizer.py @@ -9,7 +9,7 @@ __rootpath__ = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) def path_from_root(*pathelems): return os.path.join(__rootpath__, *pathelems) -NATIVE_PASSES = set(['asm', 'asmPreciseF32', 'receiveJSON', 'emitJSON', 'eliminate', 'eliminateMemSafe', 'simplifyExpressions', 'simplifyIfs', 'optimizeFrounds']) +NATIVE_PASSES = set(['asm', 'asmPreciseF32', 'receiveJSON', 'emitJSON', 'eliminate', 'eliminateMemSafe', 'simplifyExpressions', 'simplifyIfs', 'optimizeFrounds', 'registerize']) JS_OPTIMIZER = path_from_root('tools', 'js-optimizer.js') diff --git a/tools/optimizer/optimizer.cpp b/tools/optimizer/optimizer.cpp index ec73185be..da60d02a4 100644 --- a/tools/optimizer/optimizer.cpp +++ b/tools/optimizer/optimizer.cpp @@ -829,6 +829,19 @@ void clearEmptyNodes(Ref arr) { } if (skip) arr->setSize(arr->size() - skip); } +void clearUselessNodes(Ref arr) { + int skip = 0; + for (int i = 0; i < arr->size(); i++) { + Ref curr = arr[i]; + if (skip) { + arr[i-skip] = curr; + } + if (isEmpty(deStat(curr)) || (curr[0] == STAT && !hasSideEffects(curr[1]))) { + skip++; + } + } + if (skip) arr->setSize(arr->size() - skip); +} void removeAllEmptySubNodes(Ref ast) { traversePre(ast, [](Ref node) { @@ -842,6 +855,18 @@ void removeAllEmptySubNodes(Ref ast) { } }); } +void removeAllUselessSubNodes(Ref ast) { + traversePre(ast, [](Ref node) { + int index = -1; + if (node[0] == DEFUN) { + clearUselessNodes(node[3]); + } else if (node[0] == BLOCK && !!node[1]) { + clearUselessNodes(node[1]); + } else if (node[0] == SEQ && isEmpty(node[1])) { + safeCopy(node, node[2]); + } + }); +} Ref unVarify(Ref vars) { // transform var x=1, y=2 etc. into (x=1, y=2), i.e., the same assigns, but without a var definition Ref ret = makeArray(); @@ -2348,7 +2373,7 @@ void registerize(Ref ast) { allVars.insert(node[1]->getIString()); } }); - removeAllEmptySubNodes(fun); // vacuum? + removeAllUselessSubNodes(fun); // vacuum? StringTypeMap regTypes; // reg name -> type auto getNewRegName = [&](int num, IString name) { const char *str;