From 0610c020b1544820be9898a285ab6c9066490552 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 26 Jan 2017 19:16:50 +0800 Subject: optimise binary operands with evaluate() (#1427) - remove call to evaluate() in is_constant() and let nested optimize() does its job instead - reject RegExp in is_constant() and remove special case logic under collapse_vars - operands to conditionals optimisation are now always evaluate()-ed - throw error in constant_value() instead of returning undefined to catch possible bugs, similar to make_node_from_constant() - optimise binary boolean operators under `evaluate` instead of `conditionals` --- test/compress/conditionals.js | 183 ++++++----------------------------------- test/compress/evaluate.js | 184 ++++++++++++++++++++++++++++++++++++++++++ test/compress/reduce_vars.js | 24 +++--- 3 files changed, 219 insertions(+), 172 deletions(-) (limited to 'test/compress') diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js index 35cb26f7..d88c5b90 100644 --- a/test/compress/conditionals.js +++ b/test/compress/conditionals.js @@ -635,166 +635,6 @@ ternary_boolean_alternative: { } } -conditional_and: { - options = { - conditionals: true, - evaluate : true - }; - input: { - var a; - // compress these - - a = true && condition; - a = 1 && console.log("a"); - a = 2 * 3 && 2 * condition; - a = 5 == 5 && condition + 3; - a = "string" && 4 - condition; - a = 5 + "" && condition / 5; - a = -4.5 && 6 << condition; - a = 6 && 7; - - a = false && condition; - a = NaN && console.log("b"); - a = 0 && console.log("c"); - a = undefined && 2 * condition; - a = null && condition + 3; - a = 2 * 3 - 6 && 4 - condition; - a = 10 == 7 && condition / 5; - a = !"string" && 6 % condition; - a = 0 && 7; - - // don't compress these - - a = condition && true; - a = console.log("a") && 2; - a = 4 - condition && "string"; - a = 6 << condition && -4.5; - - a = condition && false; - a = console.log("b") && NaN; - a = console.log("c") && 0; - a = 2 * condition && undefined; - a = condition + 3 && null; - - } - expect: { - var a; - - a = condition; - a = console.log("a"); - a = 2 * condition; - a = condition + 3; - a = 4 - condition; - a = condition / 5; - a = 6 << condition; - a = 7; - - a = false; - a = NaN; - a = 0; - a = void 0; - a = null; - a = 0; - a = false; - a = false; - a = 0; - - a = condition && true; - a = console.log("a") && 2; - a = 4 - condition && "string"; - a = 6 << condition && -4.5; - - a = condition && false; - a = console.log("b") && NaN; - a = console.log("c") && 0; - a = 2 * condition && void 0; - a = condition + 3 && null; - } -} - -conditional_or: { - options = { - conditionals: true, - evaluate : true - }; - input: { - var a; - // compress these - - a = true || condition; - a = 1 || console.log("a"); - a = 2 * 3 || 2 * condition; - a = 5 == 5 || condition + 3; - a = "string" || 4 - condition; - a = 5 + "" || condition / 5; - a = -4.5 || 6 << condition; - a = 6 || 7; - - a = false || condition; - a = 0 || console.log("b"); - a = NaN || console.log("c"); - a = undefined || 2 * condition; - a = null || condition + 3; - a = 2 * 3 - 6 || 4 - condition; - a = 10 == 7 || condition / 5; - a = !"string" || 6 % condition; - a = null || 7; - - a = console.log(undefined && condition || null); - a = console.log(undefined || condition && null); - - // don't compress these - - a = condition || true; - a = console.log("a") || 2; - a = 4 - condition || "string"; - a = 6 << condition || -4.5; - - a = condition || false; - a = console.log("b") || NaN; - a = console.log("c") || 0; - a = 2 * condition || undefined; - a = condition + 3 || null; - - } - expect: { - var a; - - a = true; - a = 1; - a = 6; - a = true; - a = "string"; - a = "5"; - a = -4.5; - a = 6; - - a = condition; - a = console.log("b"); - a = console.log("c"); - a = 2 * condition; - a = condition + 3; - a = 4 - condition; - a = condition / 5; - a = 6 % condition; - a = 7; - - a = console.log(null); - a = console.log(condition && null); - - a = condition || true; - a = console.log("a") || 2; - a = 4 - condition || "string"; - a = 6 << condition || -4.5; - - a = condition || false; - a = console.log("b") || NaN; - a = console.log("c") || 0; - a = 2 * condition || void 0; - a = condition + 3 || null; - } -} - trivial_boolean_ternary_expressions : { options = { conditionals: true, @@ -906,3 +746,26 @@ issue_1154: { function g6() { return g(), "number"; } } } + +no_evaluate: { + options = { + conditionals: true, + evaluate : false + } + input: { + function f(b) { + a = b ? !0 : !0; + a = b ? ~1 : ~1; + a = b ? -2 : -2; + a = b ? +3 : +3; + } + } + expect: { + function f(b) { + a = !0; + a = ~1; + a = -2; + a = +3; + } + } +} diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index c74c7b24..0ff157dc 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -1,3 +1,187 @@ +and: { + options = { + evaluate: true + } + input: { + var a; + // compress these + + a = true && condition; + a = 1 && console.log("a"); + a = 2 * 3 && 2 * condition; + a = 5 == 5 && condition + 3; + a = "string" && 4 - condition; + a = 5 + "" && condition / 5; + a = -4.5 && 6 << condition; + a = 6 && 7; + + a = false && condition; + a = NaN && console.log("b"); + a = 0 && console.log("c"); + a = undefined && 2 * condition; + a = null && condition + 3; + a = 2 * 3 - 6 && 4 - condition; + a = 10 == 7 && condition / 5; + a = !"string" && 6 % condition; + a = 0 && 7; + + // don't compress these + + a = condition && true; + a = console.log("a") && 2; + a = 4 - condition && "string"; + a = 6 << condition && -4.5; + + a = condition && false; + a = console.log("b") && NaN; + a = console.log("c") && 0; + a = 2 * condition && undefined; + a = condition + 3 && null; + + } + expect: { + var a; + + a = condition; + a = console.log("a"); + a = 2 * condition; + a = condition + 3; + a = 4 - condition; + a = condition / 5; + a = 6 << condition; + a = 7; + + a = false; + a = NaN; + a = 0; + a = void 0; + a = null; + a = 0; + a = false; + a = false; + a = 0; + + a = condition && true; + a = console.log("a") && 2; + a = 4 - condition && "string"; + a = 6 << condition && -4.5; + + a = condition && false; + a = console.log("b") && NaN; + a = console.log("c") && 0; + a = 2 * condition && void 0; + a = condition + 3 && null; + } +} + +or: { + options = { + evaluate: true + } + input: { + var a; + // compress these + + a = true || condition; + a = 1 || console.log("a"); + a = 2 * 3 || 2 * condition; + a = 5 == 5 || condition + 3; + a = "string" || 4 - condition; + a = 5 + "" || condition / 5; + a = -4.5 || 6 << condition; + a = 6 || 7; + + a = false || condition; + a = 0 || console.log("b"); + a = NaN || console.log("c"); + a = undefined || 2 * condition; + a = null || condition + 3; + a = 2 * 3 - 6 || 4 - condition; + a = 10 == 7 || condition / 5; + a = !"string" || 6 % condition; + a = null || 7; + + a = console.log(undefined && condition || null); + a = console.log(undefined || condition && null); + + // don't compress these + + a = condition || true; + a = console.log("a") || 2; + a = 4 - condition || "string"; + a = 6 << condition || -4.5; + + a = condition || false; + a = console.log("b") || NaN; + a = console.log("c") || 0; + a = 2 * condition || undefined; + a = condition + 3 || null; + + } + expect: { + var a; + + a = true; + a = 1; + a = 6; + a = true; + a = "string"; + a = "5"; + a = -4.5; + a = 6; + + a = condition; + a = console.log("b"); + a = console.log("c"); + a = 2 * condition; + a = condition + 3; + a = 4 - condition; + a = condition / 5; + a = 6 % condition; + a = 7; + + a = console.log(null); + a = console.log(condition && null); + + a = condition || true; + a = console.log("a") || 2; + a = 4 - condition || "string"; + a = 6 << condition || -4.5; + + a = condition || false; + a = console.log("b") || NaN; + a = console.log("c") || 0; + a = 2 * condition || void 0; + a = condition + 3 || null; + } +} + +unary_prefix: { + options = { + evaluate: true + } + input: { + a = !0 && b; + a = !0 || b; + a = ~1 && b; + a = ~1 || b; + a = -2 && b; + a = -2 || b; + a = +3 && b; + a = +3 || b; + } + expect: { + a = b; + a = !0; + a = b; + a = -2; + a = b; + a = -2; + a = b; + a = 3; + } +} + negative_zero: { options = { evaluate: true } input: { diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index c401ac66..2301a92a 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -136,30 +136,30 @@ modified: { } function f2() { - var a = 1, b = 2, c = 3; + var b = 2, c = 3; b = c; - console.log(a + b); - console.log(b + c); + console.log(1 + b); + console.log(b + 3); console.log(4); - console.log(a + b + c); + console.log(1 + b + 3); } function f3() { - var a = 1, b = 2, c = 3; + var b = 2, c = 3; b *= c; - console.log(a + b); - console.log(b + c); + console.log(1 + b); + console.log(b + 3); console.log(4); - console.log(a + b + c); + console.log(1 + b + 3); } function f4() { - var a = 1, b = 2, c = 3; + var b = 2, c = 3; b = c; - console.log(a + b); + console.log(1 + b); console.log(b + c); - console.log(a + c); - console.log(a + b + c); + console.log(1 + c); + console.log(1 + b + c); } function f5(a) { -- cgit v1.2.3