diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-06-13 20:40:20 +0430 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-06-15 22:06:33 +0430 |
commit | 1414c7b049c5a8858f400a5e677cd20894463b32 (patch) | |
tree | 3b5255b2688222739f515819bed19c982a45baca /Userland/Libraries/LibJS/Bytecode/Pass/PlaceBlocks.cpp | |
parent | e81fd7106b47d93345e11059bb80600a66b4daf3 (diff) | |
download | serenity-1414c7b049c5a8858f400a5e677cd20894463b32.zip |
LibJS: Add a basic pass manager and add some basic passes
This commit adds a bunch of passes, the most interesting of which is a
pass that merges blocks together, and a pass that places blocks that
flow into each other next to each other, and a very simply pass that
removes duplicate basic blocks.
Note that this does not remove the jump at the end of each block in that
pass to avoid scope creep in the passes.
Diffstat (limited to 'Userland/Libraries/LibJS/Bytecode/Pass/PlaceBlocks.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/Pass/PlaceBlocks.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Bytecode/Pass/PlaceBlocks.cpp b/Userland/Libraries/LibJS/Bytecode/Pass/PlaceBlocks.cpp new file mode 100644 index 0000000000..4a320ac470 --- /dev/null +++ b/Userland/Libraries/LibJS/Bytecode/Pass/PlaceBlocks.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibJS/Bytecode/PassManager.h> + +namespace JS::Bytecode::Passes { + +void PlaceBlocks::perform(PassPipelineExecutable& executable) +{ + started(); + + VERIFY(executable.cfg.has_value()); + auto cfg = executable.cfg.release_value(); + + Vector<BasicBlock&> replaced_blocks; + HashTable<BasicBlock const*> reachable_blocks; + + // Visit the blocks in CFG order + AK::Function<void(BasicBlock const*)> visit = [&](auto* block) { + if (reachable_blocks.contains(block)) + return; + + reachable_blocks.set(block); + replaced_blocks.append(*const_cast<BasicBlock*>(block)); + + for (auto& entry : cfg.get(block).value_or({})) + visit(entry); + }; + + // Make sure to visit the entry block first + visit(&executable.executable.basic_blocks.first()); + + for (auto& entry : cfg) + visit(entry.key); + + // Put the unreferenced blocks back in at the end + for (auto& entry : static_cast<Vector<NonnullOwnPtr<BasicBlock>>&>(executable.executable.basic_blocks)) { + if (reachable_blocks.contains(entry.ptr())) + (void)entry.leak_ptr(); + else + replaced_blocks.append(*entry.leak_ptr()); // Don't try to do DCE here. + } + + executable.executable.basic_blocks.clear(); + for (auto& block : replaced_blocks) + executable.executable.basic_blocks.append(adopt_own(block)); + + finished(); +} + +} |