mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:17:44 +00:00
JSSpecCompiler: Add bare-bones DCE pass
Right now the only dead code it eliminates is the unused phi nodes.
This commit is contained in:
parent
162c334508
commit
5338cdd153
6 changed files with 286 additions and 0 deletions
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "Compiler/Passes/DeadCodeEliminationPass.h"
|
||||
#include "AST/AST.h"
|
||||
#include "Compiler/ControlFlowGraph.h"
|
||||
#include "Compiler/StronglyConnectedComponents.h"
|
||||
#include "Function.h"
|
||||
|
||||
namespace JSSpecCompiler {
|
||||
|
||||
void DeadCodeEliminationPass::process_function()
|
||||
{
|
||||
with_graph(m_function->m_local_ssa_variables.size(), [&] {
|
||||
remove_unused_phi_nodes();
|
||||
});
|
||||
m_function->reindex_ssa_variables();
|
||||
}
|
||||
|
||||
DeadCodeEliminationPass::Vertex DeadCodeEliminationPass::as_vertex(Variable* variable)
|
||||
{
|
||||
return Vertex(variable->m_ssa);
|
||||
}
|
||||
|
||||
RecursionDecision DeadCodeEliminationPass::on_entry(Tree tree)
|
||||
{
|
||||
if (tree->is_statement())
|
||||
TODO();
|
||||
return RecursionDecision::Recurse;
|
||||
}
|
||||
|
||||
void DeadCodeEliminationPass::on_leave(Tree tree)
|
||||
{
|
||||
if (auto variable = as<Variable>(tree); variable)
|
||||
as_vertex(variable)->is_referenced = true;
|
||||
}
|
||||
|
||||
void DeadCodeEliminationPass::remove_unused_phi_nodes()
|
||||
{
|
||||
for (auto const& block : m_function->m_cfg->blocks) {
|
||||
for (auto const& phi_node : block->m_phi_nodes) {
|
||||
auto to = as_vertex(phi_node.var);
|
||||
for (auto const& branch : phi_node.branches) {
|
||||
auto from = as_vertex(branch.value);
|
||||
|
||||
from->outgoing_edges.append(to);
|
||||
to->incoming_edges.append(from);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& expr : block->m_expressions)
|
||||
run_in_subtree(expr);
|
||||
run_in_const_subtree(block->m_continuation);
|
||||
}
|
||||
|
||||
// FIXME?: There surely must be a way to do this in a linear time without finding strongly
|
||||
// connected components.
|
||||
for (auto const& component : find_strongly_connected_components(m_nodes)) {
|
||||
bool is_referenced = false;
|
||||
|
||||
for (Vertex u : component)
|
||||
for (Vertex v : u->outgoing_edges)
|
||||
is_referenced |= v->is_referenced;
|
||||
|
||||
if (is_referenced)
|
||||
for (Vertex u : component)
|
||||
u->is_referenced = true;
|
||||
}
|
||||
|
||||
for (auto const& block : m_function->m_cfg->blocks) {
|
||||
block->m_phi_nodes.remove_all_matching([&](auto const& node) {
|
||||
return !as_vertex(node.var)->is_referenced;
|
||||
});
|
||||
}
|
||||
|
||||
m_function->m_local_ssa_variables.remove_all_matching([&](auto const& variable) {
|
||||
return !Vertex(variable)->is_referenced;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Compiler/EnableGraphPointers.h"
|
||||
#include "Compiler/GenericASTPass.h"
|
||||
#include "Compiler/StronglyConnectedComponents.h"
|
||||
|
||||
namespace JSSpecCompiler {
|
||||
|
||||
class DeadCodeEliminationPass
|
||||
: public IntraproceduralCompilerPass
|
||||
, private RecursiveASTVisitor
|
||||
, private EnableGraphPointers<DeadCodeEliminationPass, SSAVariableDeclarationRef> {
|
||||
public:
|
||||
inline static constexpr StringView name = "dce"sv;
|
||||
|
||||
using IntraproceduralCompilerPass::IntraproceduralCompilerPass;
|
||||
|
||||
protected:
|
||||
void process_function() override;
|
||||
|
||||
private:
|
||||
friend EnableGraphPointers;
|
||||
|
||||
static Vertex as_vertex(Variable* variable);
|
||||
RecursionDecision on_entry(Tree tree) override;
|
||||
void on_leave(Tree tree) override;
|
||||
void remove_unused_phi_nodes();
|
||||
|
||||
struct NodeData {
|
||||
Vector<Vertex> outgoing_edges;
|
||||
Vector<Vertex> incoming_edges;
|
||||
|
||||
bool is_referenced = false;
|
||||
};
|
||||
|
||||
Vector<NodeData> m_nodes;
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Vector.h>
|
||||
|
||||
#include "Compiler/EnableGraphPointers.h"
|
||||
|
||||
namespace JSSpecCompiler {
|
||||
|
||||
namespace Detail {
|
||||
template<typename GraphVertex, typename GraphNode>
|
||||
class StronglyConnectedComponents
|
||||
: private EnableGraphPointers<StronglyConnectedComponents<GraphVertex, GraphNode>> {
|
||||
using Self = StronglyConnectedComponents<GraphVertex, GraphNode>;
|
||||
using Vertex = typename EnableGraphPointers<Self>::Vertex;
|
||||
|
||||
public:
|
||||
StronglyConnectedComponents(Vector<GraphNode> const& graph)
|
||||
: m_graph(graph)
|
||||
{
|
||||
}
|
||||
|
||||
Vector<Vector<GraphVertex>> find()
|
||||
{
|
||||
Vector<Vector<GraphVertex>> result;
|
||||
size_t n = m_graph.size();
|
||||
Self::with_graph(n, [&] {
|
||||
for (size_t i = 0; i < m_graph.size(); ++i)
|
||||
find_order(i);
|
||||
for (size_t i = n; i--;) {
|
||||
if (!m_order[i]->is_processed) {
|
||||
result.empend();
|
||||
find_component(GraphVertex(m_order[i]), result.last());
|
||||
}
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
friend EnableGraphPointers<Self>;
|
||||
|
||||
void find_order(Vertex u)
|
||||
{
|
||||
if (u->is_visited)
|
||||
return;
|
||||
u->is_visited = true;
|
||||
|
||||
for (auto v : GraphVertex(u)->incoming_edges)
|
||||
find_order(Vertex(v));
|
||||
m_order.append(u);
|
||||
}
|
||||
|
||||
void find_component(GraphVertex u, Vector<GraphVertex>& current_scc)
|
||||
{
|
||||
current_scc.empend(u);
|
||||
Vertex(u)->is_processed = true;
|
||||
for (auto v : u->outgoing_edges)
|
||||
if (!Vertex(v)->is_processed)
|
||||
find_component(v, current_scc);
|
||||
}
|
||||
|
||||
struct NodeData {
|
||||
bool is_visited = false;
|
||||
bool is_processed = false;
|
||||
};
|
||||
|
||||
Vector<GraphNode> const& m_graph;
|
||||
Vector<NodeData> m_nodes;
|
||||
Vector<Vertex> m_order;
|
||||
};
|
||||
}
|
||||
|
||||
template<typename NodeData>
|
||||
auto find_strongly_connected_components(Vector<NodeData> const& graph)
|
||||
{
|
||||
using Vertex = RemoveCVReference<decltype(graph[0].outgoing_edges[0])>;
|
||||
return Detail::StronglyConnectedComponents<Vertex, NodeData>(graph).find();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue