From fdd4cb7b11d43c6ff200c9541f8567000a8d4bcd Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 21 Dec 2018 13:19:18 -0800 Subject: LocalCSE: Consider pass options, both size and cost (#1840) With this we can optimize redundant global accesses fairly well (at least locally; licm also works), see #1831 --- src/ir/cost.h | 3 +++ src/passes/LocalCSE.cpp | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/ir/cost.h b/src/ir/cost.h index defd9413e..b64ca5110 100644 --- a/src/ir/cost.h +++ b/src/ir/cost.h @@ -17,6 +17,9 @@ #ifndef wasm_ir_cost_h #define wasm_ir_cost_h +#include +#include + namespace wasm { // Measure the execution cost of an AST. Very handwave-ey diff --git a/src/passes/LocalCSE.cpp b/src/passes/LocalCSE.cpp index 12f0d9d93..8faa5b47c 100644 --- a/src/passes/LocalCSE.cpp +++ b/src/passes/LocalCSE.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -208,8 +209,19 @@ struct LocalCSE : public WalkerPass> { if (EffectAnalyzer(getPassOptions(), value).hasSideEffects()) { return false; // we can't combine things with side effects } - // check what we care about TODO: use optimize/shrink levels? - return Measurer::measure(value) > 1; + auto& options = getPassRunner()->options; + // If the size is at least 3, then if we have two of them we have 6, + // and so adding one set+two gets and removing one of the items itself + // is not detrimental, and may be beneficial. + if (options.shrinkLevel > 0 && Measurer::measure(value) >= 3) { + return true; + } + // If we focus on speed, any reduction in cost is beneficial, as the + // cost of a get is essentially free. + if (options.shrinkLevel == 0 && CostAnalyzer(value).cost > 0) { + return true; + } + return false; } }; -- cgit v1.2.3