From 6cc2bb302d5729c76da42dc0815d6dbba645d952 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 21 Nov 2018 08:59:13 -0800 Subject: Relooper CFG optimizations (#1759) Previously the relooper would do some optimizations when deciding when to use an if vs a switch, how to group blocks, etc. This PR adds an additional pre-optimization phase with some basic but useful simplify-cfg style passes, * Skip empty blocks when they have just one exit. * Merge exiting branches when they are equivalent. * Canonicalize block contents to make such comparisons more useful. * Turn a trivial one-target switch into a simple branch. This can help in noticeable ways when running the rereloop pass, e.g. on LLVM wasm backend output. Also: * Binaryen C API changes to the relooper, which now gets a Module for its constructor. It needs it for the optimizations, as it may construct new nodes. * Many relooper-fuzzer improvements. * Clean up HashType usage. --- src/binaryen-c.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/binaryen-c.cpp') diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 8e9ea8589..b0250a270 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -2373,12 +2373,13 @@ const char* BinaryenExportGetValue(BinaryenExportRef export_) { // ========== CFG / Relooper ========== // -RelooperRef RelooperCreate(void) { +RelooperRef RelooperCreate(BinaryenModuleRef module) { if (tracing) { - std::cout << " the_relooper = RelooperCreate();\n"; + std::cout << " the_relooper = RelooperCreate(the_module);\n"; } - return RelooperRef(new CFG::Relooper()); + auto* wasm = (Module*)module; + return RelooperRef(new CFG::Relooper(wasm)); } RelooperBlockRef RelooperAddBlock(RelooperRef relooper, BinaryenExpressionRef code) { @@ -2440,15 +2441,15 @@ void RelooperAddBranchForSwitch(RelooperBlockRef from, RelooperBlockRef to, Bina fromBlock->AddSwitchBranchTo(toBlock, std::move(values), (Expression*)code); } -BinaryenExpressionRef RelooperRenderAndDispose(RelooperRef relooper, RelooperBlockRef entry, BinaryenIndex labelHelper, BinaryenModuleRef module) { +BinaryenExpressionRef RelooperRenderAndDispose(RelooperRef relooper, RelooperBlockRef entry, BinaryenIndex labelHelper) { auto* R = (CFG::Relooper*)relooper; R->Calculate((CFG::Block*)entry); - CFG::RelooperBuilder builder(*(Module*)module, labelHelper); + CFG::RelooperBuilder builder(*R->Module, labelHelper); auto* ret = R->Render(builder); if (tracing) { auto id = noteExpression(ret); - std::cout << " expressions[" << id << "] = RelooperRenderAndDispose(the_relooper, relooperBlocks[" << relooperBlocks[entry] << "], " << labelHelper << ", the_module);\n"; + std::cout << " expressions[" << id << "] = RelooperRenderAndDispose(the_relooper, relooperBlocks[" << relooperBlocks[entry] << "], " << labelHelper << ");\n"; relooperBlocks.clear(); } -- cgit v1.2.3