forked from KPGPMC/furlang
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
a262014348
|
|||
|
986015bc09
|
|||
|
ac1e226b4b
|
|||
|
d0f3637539
|
|||
|
f1f62fad8c
|
|||
|
065a2af699
|
|||
|
767970c69b
|
|||
|
97c01bc96c
|
|||
|
abd975be23
|
|||
|
be386cf230
|
|||
|
a14252bd1d
|
|||
|
6b92e19b23
|
|||
|
865691df26
|
|||
|
b4fda8d7d0
|
|||
|
3f6fcc56ff
|
|||
|
c1b65838b0
|
|||
| eaa74fd573 | |||
| 9cd54c1c60 |
+1
-1
@@ -1 +1 @@
|
|||||||
TBD
|
The commits must be as good as CHatingPython's.
|
||||||
|
|||||||
@@ -235,7 +235,13 @@ using compound_statement_node_p =
|
|||||||
|
|
||||||
using compound_statement_node_r = node_r<compound_statement_node>; /**< Alias for compound_statement_node result */
|
using compound_statement_node_r = node_r<compound_statement_node>; /**< Alias for compound_statement_node result */
|
||||||
|
|
||||||
|
class while_statement_node;
|
||||||
|
|
||||||
|
using while_statement_node_p = node_p<while_statement_node>; /**< Alias for a shared pointer to while_statement_node. */
|
||||||
|
|
||||||
|
using while_statement_node_r = node_r<while_statement_node>; /**< Alias for while_statement_node result */
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
} // namespace furc
|
} // namespace furc
|
||||||
|
|
||||||
#endif // FURC_AST_FWD_HPP
|
#endif // FURC_AST_FWD_HPP
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef FURC_AST_STATEMENT_HPP
|
#ifndef FURC_AST_STATEMENT_HPP
|
||||||
#define FURC_AST_STATEMENT_HPP
|
#define FURC_AST_STATEMENT_HPP
|
||||||
|
|
||||||
|
#include "furc/ast/fwd.hpp"
|
||||||
#include "furc/ast/node.hpp"
|
#include "furc/ast/node.hpp"
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@@ -17,6 +18,7 @@ enum class statement_node_t {
|
|||||||
Return, /**< Return statement */
|
Return, /**< Return statement */
|
||||||
If, /**< If statement */
|
If, /**< If statement */
|
||||||
Compound, /**< Compound statement */
|
Compound, /**< Compound statement */
|
||||||
|
While, /**< While loop statement. */
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -191,7 +193,52 @@ private:
|
|||||||
struct body m_body; /**< The body handle. */
|
struct body m_body; /**< The body handle. */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief while statement AST node.
|
||||||
|
*/
|
||||||
|
class while_statement_node final : public statement_node, public abstract_node {
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Construct a new while statement AST node.
|
||||||
|
*
|
||||||
|
* @param location Node location.
|
||||||
|
* @param body Body handle.
|
||||||
|
*/
|
||||||
|
while_statement_node(struct location location, expression_node_p&& cond, statement_node_p&& body)
|
||||||
|
: abstract_node(location), m_cond(std::move(cond)), m_body(std::move(body)) {}
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Returns this node's condition expression.
|
||||||
|
*
|
||||||
|
* @return The condition expression.
|
||||||
|
*/
|
||||||
|
const expression_node_p& condition() const { return m_cond; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns this node's body handle.
|
||||||
|
*
|
||||||
|
* @return The body handle.
|
||||||
|
*/
|
||||||
|
const statement_node_p& body() const { return m_body; }
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Returns this node's statement type.
|
||||||
|
*
|
||||||
|
* @return statement_node_t::while.
|
||||||
|
*/
|
||||||
|
statement_node_t statement_type() const override { return statement_node_t::While; }
|
||||||
|
public:
|
||||||
|
void accept(visitor& visitor) const override;
|
||||||
|
|
||||||
|
std::ostream& print(std::ostream& os) const override;
|
||||||
|
protected:
|
||||||
|
bool equal(const node& rhs) const override;
|
||||||
|
private:
|
||||||
|
expression_node_p m_cond; /**< The condition expression. */
|
||||||
|
statement_node_p m_body; /**< The body handle. */
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
} // namespace furc
|
} // namespace furc
|
||||||
|
|
||||||
#endif // FURC_AST_STATEMENT_HPP
|
#endif // FURC_AST_STATEMENT_HPP
|
||||||
|
|||||||
@@ -122,6 +122,14 @@ public:
|
|||||||
*/
|
*/
|
||||||
virtual void visit(const compound_statement_node& node) {}
|
virtual void visit(const compound_statement_node& node) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Visit a while_statement_node.
|
||||||
|
* @see while_statement_node
|
||||||
|
*
|
||||||
|
* @param node Node.
|
||||||
|
*/
|
||||||
|
virtual void visit(const while_statement_node& node) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Visit an AST error.
|
* @brief Visit an AST error.
|
||||||
*
|
*
|
||||||
@@ -133,4 +141,4 @@ public:
|
|||||||
} // namespace ast
|
} // namespace ast
|
||||||
} // namespace furc
|
} // namespace furc
|
||||||
|
|
||||||
#endif // FURC_AST_VISITOR_HPP
|
#endif // FURC_AST_VISITOR_HPP
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ public:
|
|||||||
void visit(const ast::function_definition_node& funcDef) override;
|
void visit(const ast::function_definition_node& funcDef) override;
|
||||||
void visit(const ast::return_statement_node& returnStmt) override;
|
void visit(const ast::return_statement_node& returnStmt) override;
|
||||||
void visit(const ast::if_statement_node& node) override;
|
void visit(const ast::if_statement_node& node) override;
|
||||||
|
void visit(const ast::while_statement_node& node) override;
|
||||||
void visit(const ast::compound_statement_node& node) override;
|
void visit(const ast::compound_statement_node& node) override;
|
||||||
void visit(const ast::string_literal_node& node) override;
|
void visit(const ast::string_literal_node& node) override;
|
||||||
void visit(const ast::integer_literal_node& node) override;
|
void visit(const ast::integer_literal_node& node) override;
|
||||||
@@ -42,7 +43,7 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
furlang::ir::block_index push_block();
|
furlang::ir::block_index push_block(bool validate = true);
|
||||||
private:
|
private:
|
||||||
furlang::ir::module m_module;
|
furlang::ir::module m_module;
|
||||||
std::unique_ptr<furlang::ir::function> m_currentFunction;
|
std::unique_ptr<furlang::ir::function> m_currentFunction;
|
||||||
@@ -55,4 +56,4 @@ private:
|
|||||||
} // namespace front
|
} // namespace front
|
||||||
} // namespace furc
|
} // namespace furc
|
||||||
|
|
||||||
#endif // FURC_FRONT_IR_GENERATOR_HPP
|
#endif // FURC_FRONT_IR_GENERATOR_HPP
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
#ifndef FURC_FRONT_SSA_HPP
|
||||||
|
#define FURC_FRONT_SSA_HPP
|
||||||
|
|
||||||
|
#include "furlang/ir/function.hpp"
|
||||||
|
#include "furlang/ir/instruction.hpp"
|
||||||
|
#include "furlang/ir/module.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace furc {
|
||||||
|
namespace front {
|
||||||
|
|
||||||
|
class ssa {
|
||||||
|
using block_map_t = std::unordered_map<furlang::ir::block_index, std::vector<furlang::ir::block_index>>;
|
||||||
|
public:
|
||||||
|
static void optimize(furlang::ir::module& mod);
|
||||||
|
private:
|
||||||
|
static void optimize(const std::unique_ptr<furlang::ir::function>& func);
|
||||||
|
|
||||||
|
static void de_ssa(const std::unique_ptr<furlang::ir::function>& func);
|
||||||
|
|
||||||
|
static void constant_propagation(const std::unique_ptr<furlang::ir::function>& func);
|
||||||
|
|
||||||
|
static void dead_code_elimination(const std::unique_ptr<furlang::ir::function>& func);
|
||||||
|
|
||||||
|
static void copy_propagation(const std::unique_ptr<furlang::ir::function>& func);
|
||||||
|
|
||||||
|
static void dfs_rpo(furlang::ir::block_index block,
|
||||||
|
const block_map_t& successors,
|
||||||
|
std::unordered_set<furlang::ir::block_index>& visited,
|
||||||
|
std::vector<furlang::ir::block_index>& rpo);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace front
|
||||||
|
} // namespace furc
|
||||||
|
|
||||||
|
#endif // FURC_FRONT_SSA_HPP
|
||||||
@@ -145,6 +145,7 @@ enum class keyword_token {
|
|||||||
Return, /**< `return` */
|
Return, /**< `return` */
|
||||||
If, /**< `if` */
|
If, /**< `if` */
|
||||||
Else, /**< `else` */
|
Else, /**< `else` */
|
||||||
|
While, /**< `while` */
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
|
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
|
||||||
@@ -154,6 +155,7 @@ static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword)
|
|||||||
case keyword_token::Return: return os << "return";
|
case keyword_token::Return: return os << "return";
|
||||||
case keyword_token::If: return os << "if";
|
case keyword_token::If: return os << "if";
|
||||||
case keyword_token::Else: return os << "else";
|
case keyword_token::Else: return os << "else";
|
||||||
|
case keyword_token::While: return os << "while";
|
||||||
}
|
}
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
@@ -165,6 +167,7 @@ static inline std::string operator+(const std::string& str, keyword_token keywor
|
|||||||
case keyword_token::Return: return str + "return";
|
case keyword_token::Return: return str + "return";
|
||||||
case keyword_token::If: return str + "if";
|
case keyword_token::If: return str + "if";
|
||||||
case keyword_token::Else: return str + "else";
|
case keyword_token::Else: return str + "else";
|
||||||
|
case keyword_token::While: return str + "while";
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
@@ -377,4 +380,4 @@ using token_r = furlang::result<token, token_error>; /**< Alias to a token resul
|
|||||||
} // namespace front
|
} // namespace front
|
||||||
} // namespace furc
|
} // namespace furc
|
||||||
|
|
||||||
#endif // FURC_FRONT_TOKEN_HPP
|
#endif // FURC_FRONT_TOKEN_HPP
|
||||||
|
|||||||
+13
-1
@@ -186,6 +186,18 @@ bool compound_statement_node::equal(const node& rhs) const {
|
|||||||
return statement_node::equal(rhs) && m_body == dynamic_cast<const compound_statement_node&>(rhs).m_body;
|
return statement_node::equal(rhs) && m_body == dynamic_cast<const compound_statement_node&>(rhs).m_body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void while_statement_node::accept(visitor& visitor) const {
|
||||||
|
visitor.visit(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& while_statement_node::print(std::ostream& os) const {
|
||||||
|
return os << m_body;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool while_statement_node::equal(const node& rhs) const {
|
||||||
|
return statement_node::equal(rhs) && m_body == dynamic_cast<const while_statement_node&>(rhs).m_body;
|
||||||
|
}
|
||||||
|
|
||||||
void program_node::accept(visitor& visitor) const {
|
void program_node::accept(visitor& visitor) const {
|
||||||
for (const auto& decl : m_declarations) {
|
for (const auto& decl : m_declarations) {
|
||||||
decl->accept(visitor);
|
decl->accept(visitor);
|
||||||
@@ -212,4 +224,4 @@ std::ostream& operator<<(std::ostream& os, const body& body) {
|
|||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace furc::ast
|
} // namespace furc::ast
|
||||||
|
|||||||
@@ -4,8 +4,10 @@
|
|||||||
#include "furc/ast/expression.hpp" // IWYU pragma: keep
|
#include "furc/ast/expression.hpp" // IWYU pragma: keep
|
||||||
#include "furc/ast/literal.hpp" // IWYU pragma: keep
|
#include "furc/ast/literal.hpp" // IWYU pragma: keep
|
||||||
#include "furc/ast/statement.hpp" // IWYU pragma: keep
|
#include "furc/ast/statement.hpp" // IWYU pragma: keep
|
||||||
|
#include "furlang/ir/instruction.hpp"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace furc::front {
|
namespace furc::front {
|
||||||
|
|
||||||
@@ -55,6 +57,33 @@ void ir_generator::visit(const ast::if_statement_node& node) {
|
|||||||
push_block(); // merge block
|
push_block(); // merge block
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ir_generator::visit(const ast::while_statement_node& node) {
|
||||||
|
node.condition()->accept(*this);
|
||||||
|
ir_register cond = m_registerCounter - 1;
|
||||||
|
std::shared_ptr<ir::block> entry = m_currentBlock;
|
||||||
|
ir::block_index headerIdx = m_currentFunction->blocks().size();
|
||||||
|
|
||||||
|
push_block(false); // loop header
|
||||||
|
push<ir::branch_instruction>(m_currentFunction->blocks().size());
|
||||||
|
|
||||||
|
push_block(); // loop condition
|
||||||
|
node.condition()->accept(*this);
|
||||||
|
std::shared_ptr<ir::block> condBlock = m_currentBlock;
|
||||||
|
ir_register cond2 = m_registerCounter - 1;
|
||||||
|
|
||||||
|
push_block(false); // loop body
|
||||||
|
node.body()->accept(*this);
|
||||||
|
push<ir::branch_instruction>(headerIdx);
|
||||||
|
|
||||||
|
entry->emplace<ir::branch_cond_instruction>(ir::operand::new_reg(cond),
|
||||||
|
headerIdx,
|
||||||
|
m_currentFunction->blocks().size());
|
||||||
|
condBlock->emplace<ir::branch_cond_instruction>(ir::operand::new_reg(cond2),
|
||||||
|
m_currentFunction->blocks().size() - 1,
|
||||||
|
m_currentFunction->blocks().size());
|
||||||
|
push_block(); // merge block
|
||||||
|
}
|
||||||
|
|
||||||
void ir_generator::visit(const ast::compound_statement_node& node) {
|
void ir_generator::visit(const ast::compound_statement_node& node) {
|
||||||
for (const auto& stmt : node.body().statements) {
|
for (const auto& stmt : node.body().statements) {
|
||||||
stmt.value()->accept(*this);
|
stmt.value()->accept(*this);
|
||||||
@@ -138,8 +167,8 @@ void ir_generator::visit(const ast::var_assign_expression_node& node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
furlang::ir::block_index ir_generator::push_block() {
|
furlang::ir::block_index ir_generator::push_block(bool validate) {
|
||||||
if (!m_currentFunction->blocks().empty() && !m_currentFunction->blocks().back()->has_exit()) {
|
if (validate && !m_currentFunction->blocks().empty() && !m_currentFunction->blocks().back()->has_exit()) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"block " + std::to_string(m_currentFunction->blocks().size() - 1) + " is lacking an exit");
|
"block " + std::to_string(m_currentFunction->blocks().size() - 1) + " is lacking an exit");
|
||||||
}
|
}
|
||||||
@@ -148,4 +177,4 @@ furlang::ir::block_index ir_generator::push_block() {
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace furc::front
|
} // namespace furc::front
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "furc/front/lexer.hpp"
|
#include "furc/front/lexer.hpp"
|
||||||
|
|
||||||
|
#include "furc/front/token.hpp"
|
||||||
|
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <map>
|
#include <map>
|
||||||
@@ -97,6 +99,7 @@ token_r lexer::next_token() {
|
|||||||
{ "return", keyword_token::Return },
|
{ "return", keyword_token::Return },
|
||||||
{ "if", keyword_token::If },
|
{ "if", keyword_token::If },
|
||||||
{ "else", keyword_token::Else },
|
{ "else", keyword_token::Else },
|
||||||
|
{ "while", keyword_token::While },
|
||||||
};
|
};
|
||||||
|
|
||||||
if (auto it = s_keywords.find(value); it != s_keywords.end()) return { location, it->second };
|
if (auto it = s_keywords.find(value); it != s_keywords.end()) return { location, it->second };
|
||||||
@@ -185,4 +188,4 @@ location lexer::current_location() {
|
|||||||
return { m_filename, m_row, m_cursor - m_lineStart };
|
return { m_filename, m_row, m_cursor - m_lineStart };
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace furc::front
|
} // namespace furc::front
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
|
|
||||||
#include "furc/ast/declaration.hpp" // IWYU pragma: keep
|
#include "furc/ast/declaration.hpp" // IWYU pragma: keep
|
||||||
#include "furc/ast/expression.hpp" // IWYU pragma: keep
|
#include "furc/ast/expression.hpp" // IWYU pragma: keep
|
||||||
#include "furc/ast/literal.hpp" // IWYU pragma: keep
|
#include "furc/ast/fwd.hpp"
|
||||||
#include "furc/ast/program.hpp" // IWYU pragma: keep
|
#include "furc/ast/literal.hpp" // IWYU pragma: keep
|
||||||
#include "furc/ast/statement.hpp" // IWYU pragma: keep
|
#include "furc/ast/program.hpp" // IWYU pragma: keep
|
||||||
|
#include "furc/ast/statement.hpp" // IWYU pragma: keep
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -145,6 +146,24 @@ ast::statement_node_r parser::parse_statement() {
|
|||||||
std::move(cond.value()),
|
std::move(cond.value()),
|
||||||
std::move(then.value()));
|
std::move(then.value()));
|
||||||
}
|
}
|
||||||
|
case keyword_token::While: {
|
||||||
|
auto tok = next_token();
|
||||||
|
auto err = eat_token(token_t::LParen);
|
||||||
|
if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
|
||||||
|
|
||||||
|
auto cond = parse_expression();
|
||||||
|
if (cond.has_error()) return ast::statement_node_r(ast::error{ cond.error().location });
|
||||||
|
|
||||||
|
err = eat_token(token_t::RParen);
|
||||||
|
if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
|
||||||
|
|
||||||
|
auto body = parse_statement();
|
||||||
|
if (body.has_error()) return ast::statement_node_r(ast::error{ body.error().location });
|
||||||
|
|
||||||
|
return m_arena.allocate_shared<ast::while_statement_node>(location,
|
||||||
|
std::move(cond.value()),
|
||||||
|
std::move(body.value()));
|
||||||
|
}
|
||||||
case keyword_token::None:
|
case keyword_token::None:
|
||||||
case keyword_token::Func:
|
case keyword_token::Func:
|
||||||
default: break;
|
default: break;
|
||||||
@@ -438,4 +457,4 @@ token_r parser::eat_token(token_t type) {
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace furc::front
|
} // namespace furc::front
|
||||||
|
|||||||
@@ -0,0 +1,640 @@
|
|||||||
|
#include "furc/front/ssa.hpp"
|
||||||
|
|
||||||
|
#include "furlang/ir/instruction.hpp"
|
||||||
|
#include "furlang/ir/operand.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <queue>
|
||||||
|
#include <set>
|
||||||
|
#include <stack>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace furc::front {
|
||||||
|
|
||||||
|
void ssa::optimize(furlang::ir::module& mod) {
|
||||||
|
for (const auto& func : mod.functions()) {
|
||||||
|
ssa::optimize(func);
|
||||||
|
ssa::constant_propagation(func);
|
||||||
|
ssa::dead_code_elimination(func);
|
||||||
|
ssa::copy_propagation(func);
|
||||||
|
ssa::dead_code_elimination(func);
|
||||||
|
ssa::de_ssa(func);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ssa::optimize(const std::unique_ptr<furlang::ir::function>& func) {
|
||||||
|
block_map_t predecessors;
|
||||||
|
block_map_t successors;
|
||||||
|
|
||||||
|
std::unordered_map<furlang::ir::register_t, std::unordered_set<furlang::ir::block_index>> regSites;
|
||||||
|
|
||||||
|
std::unordered_map<furlang::ir::block_index, furlang::ir::block_index> idoms;
|
||||||
|
|
||||||
|
std::unordered_map<furlang::ir::register_t, std::uint32_t> regVers;
|
||||||
|
std::unordered_map<furlang::ir::register_t, std::stack<std::uint32_t>> regVerStacks;
|
||||||
|
std::unordered_map<furlang::ir::block_index, std::vector<furlang::ir::block_index>> domTree;
|
||||||
|
|
||||||
|
for (furlang::ir::block_index i = 0; i < func->blocks().size(); ++i) {
|
||||||
|
const auto& block = func->blocks()[i];
|
||||||
|
|
||||||
|
for (const auto& instr : block->instructions()) {
|
||||||
|
if (instr->has_destination()) {
|
||||||
|
if (instr->destination().type() == furlang::ir::operand_t::Register) {
|
||||||
|
regSites[instr->destination().reg()].insert(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& exit = block->exit();
|
||||||
|
switch (exit->type()) {
|
||||||
|
case furlang::ir::instruction_t::Branch: {
|
||||||
|
const auto& br = dynamic_cast<const furlang::ir::branch_instruction&>(*exit);
|
||||||
|
predecessors[br.block()].push_back(i);
|
||||||
|
successors[i].push_back(br.block());
|
||||||
|
} break;
|
||||||
|
case furlang::ir::instruction_t::BranchCond: {
|
||||||
|
const auto& br = dynamic_cast<const furlang::ir::branch_cond_instruction&>(*exit);
|
||||||
|
predecessors[br.if_block()].push_back(i);
|
||||||
|
predecessors[br.else_block()].push_back(i);
|
||||||
|
successors[i].push_back(br.if_block());
|
||||||
|
successors[i].push_back(br.else_block());
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unordered_set<furlang::ir::register_t> globalRegs;
|
||||||
|
for (furlang::ir::block_index i = 0; i < func->blocks().size(); ++i) {
|
||||||
|
const auto& block = func->blocks()[i];
|
||||||
|
for (const auto& instr : block->instructions()) {
|
||||||
|
for (const auto& operand : instr->sources()) {
|
||||||
|
if (operand->type() == furlang::ir::operand_t::Register) {
|
||||||
|
auto reg = operand->reg();
|
||||||
|
if (regSites[reg].find(i) == regSites[reg].end()) {
|
||||||
|
globalRegs.insert(reg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const auto& operand : block->exit()->sources()) {
|
||||||
|
if (operand->type() == furlang::ir::operand_t::Register) {
|
||||||
|
auto reg = operand->reg();
|
||||||
|
if (regSites[reg].find(i) == regSites[reg].end()) {
|
||||||
|
globalRegs.insert(reg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unordered_set<furlang::ir::block_index> visited;
|
||||||
|
std::vector<furlang::ir::block_index> rpoOrder;
|
||||||
|
dfs_rpo(0, successors, visited, rpoOrder);
|
||||||
|
std::reverse(rpoOrder.begin(), rpoOrder.end());
|
||||||
|
|
||||||
|
std::unordered_map<furlang::ir::block_index, std::size_t> rpoIndex;
|
||||||
|
for (std::size_t i = 0; i < rpoOrder.size(); ++i) {
|
||||||
|
rpoIndex[rpoOrder[i]] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto intersect = [&](furlang::ir::block_index block1, furlang::ir::block_index block2) {
|
||||||
|
while (block1 != block2) {
|
||||||
|
while (rpoIndex[block1] > rpoIndex[block2]) {
|
||||||
|
block1 = idoms[block1];
|
||||||
|
}
|
||||||
|
while (rpoIndex[block2] > rpoIndex[block1]) {
|
||||||
|
block2 = idoms[block2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return block1;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (rpoOrder.empty()) return;
|
||||||
|
auto entry = rpoOrder.front();
|
||||||
|
idoms[entry] = entry;
|
||||||
|
|
||||||
|
bool changed = true;
|
||||||
|
while (changed) {
|
||||||
|
changed = false;
|
||||||
|
|
||||||
|
for (std::size_t i = 1; i < rpoOrder.size(); ++i) {
|
||||||
|
auto block = rpoOrder[i];
|
||||||
|
|
||||||
|
furlang::ir::block_index newIdom = 0;
|
||||||
|
bool found = false;
|
||||||
|
for (auto pred : predecessors[block]) {
|
||||||
|
if (idoms.find(pred) == idoms.end()) continue;
|
||||||
|
if (!found) {
|
||||||
|
newIdom = pred;
|
||||||
|
found = true;
|
||||||
|
} else {
|
||||||
|
newIdom = intersect(pred, newIdom);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (idoms.find(block) == idoms.end() || idoms[block] != newIdom) {
|
||||||
|
idoms[block] = newIdom;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unordered_map<furlang::ir::block_index, std::unordered_set<furlang::ir::block_index>> df;
|
||||||
|
for (auto block : rpoOrder) {
|
||||||
|
df[block] = std::unordered_set<furlang::ir::block_index>{};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto block : rpoOrder) {
|
||||||
|
if (predecessors[block].size() < 2) continue;
|
||||||
|
|
||||||
|
for (auto pred : predecessors[block]) {
|
||||||
|
auto runner = pred;
|
||||||
|
while (runner != idoms[block]) {
|
||||||
|
df[runner].insert(block);
|
||||||
|
runner = idoms[runner];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unordered_map<furlang::ir::block_index, std::unordered_set<furlang::ir::register_t>> phis;
|
||||||
|
|
||||||
|
for (const auto& [reg, blocks] : regSites) {
|
||||||
|
if (globalRegs.find(reg) == globalRegs.end()) continue;
|
||||||
|
|
||||||
|
std::vector<furlang::ir::block_index> worklist(blocks.begin(), blocks.end());
|
||||||
|
std::unordered_set<furlang::ir::block_index> added;
|
||||||
|
while (!worklist.empty()) {
|
||||||
|
auto block = worklist.back();
|
||||||
|
worklist.pop_back();
|
||||||
|
for (auto frontier : df[block]) {
|
||||||
|
if (added.find(frontier) != added.end()) continue;
|
||||||
|
phis[frontier].insert(reg);
|
||||||
|
added.insert(frontier);
|
||||||
|
if (blocks.find(frontier) == blocks.end()) {
|
||||||
|
worklist.push_back(frontier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (furlang::ir::block_index i = 0; i < func->blocks().size(); ++i) {
|
||||||
|
if (phis.find(i) == phis.end()) continue;
|
||||||
|
const auto& block = func->blocks()[i];
|
||||||
|
const auto& preds = predecessors[i];
|
||||||
|
|
||||||
|
for (auto reg : phis[i]) {
|
||||||
|
auto phiInstr = std::make_unique<furlang::ir::phi_instruction>(reg);
|
||||||
|
for (auto pred : preds) {
|
||||||
|
phiInstr->labels().emplace_back(furlang::ir::operand::new_reg(reg), pred);
|
||||||
|
}
|
||||||
|
block->instructions().emplace(block->instructions().begin(), std::move(phiInstr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& [block, idom] : idoms) {
|
||||||
|
if (block != idom) domTree[idom].push_back(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<void(furlang::ir::block_index)> renameBlock = [&](furlang::ir::block_index blockIndex) -> void {
|
||||||
|
std::unordered_map<furlang::ir::register_t, std::uint32_t> pushed;
|
||||||
|
|
||||||
|
const auto& block = func->blocks()[blockIndex];
|
||||||
|
for (auto& instr : block->instructions()) {
|
||||||
|
if (instr->type() != furlang::ir::instruction_t::Phi) continue;
|
||||||
|
auto orig = instr->destination().reg();
|
||||||
|
std::uint32_t newVer = regVers[orig]++;
|
||||||
|
instr->destination().reg().ver = newVer;
|
||||||
|
regVerStacks[orig].push(newVer);
|
||||||
|
++pushed[orig];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& instr : block->instructions()) {
|
||||||
|
if (instr->type() == furlang::ir::instruction_t::Phi) continue;
|
||||||
|
|
||||||
|
for (auto& operand : instr->sources()) {
|
||||||
|
if (operand->type() != furlang::ir::operand_t::Register) continue;
|
||||||
|
auto orig = operand->reg();
|
||||||
|
if (!regVerStacks[orig].empty()) {
|
||||||
|
operand->reg().ver = regVerStacks[orig].top();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instr->has_destination() && instr->destination().type() == furlang::ir::operand_t::Register) {
|
||||||
|
auto orig = instr->destination().reg();
|
||||||
|
std::uint32_t newVer = regVers[orig]++;
|
||||||
|
instr->destination().reg().ver = newVer;
|
||||||
|
regVerStacks[orig].push(newVer);
|
||||||
|
++pushed[orig];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& operand : block->exit()->sources()) {
|
||||||
|
if (operand->type() != furlang::ir::operand_t::Register) continue;
|
||||||
|
auto orig = operand->reg();
|
||||||
|
if (!regVerStacks[orig].empty()) {
|
||||||
|
operand->reg().ver = regVerStacks[orig].top();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto succIndex : successors[blockIndex]) {
|
||||||
|
const auto& succ = func->blocks()[succIndex];
|
||||||
|
for (auto& instr : succ->instructions()) {
|
||||||
|
if (instr->type() != furlang::ir::instruction_t::Phi) break;
|
||||||
|
auto& phi = dynamic_cast<furlang::ir::phi_instruction&>(*instr);
|
||||||
|
for (auto& [op, bl] : phi.labels()) {
|
||||||
|
if (bl != blockIndex) continue;
|
||||||
|
if (regVerStacks[op.reg()].empty()) continue;
|
||||||
|
op.reg().ver = regVerStacks[op.reg()].top();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& child : domTree[blockIndex]) {
|
||||||
|
renameBlock(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& [reg, count] : pushed) {
|
||||||
|
for (std::uint32_t i = 0; i < count; ++i) {
|
||||||
|
regVerStacks[reg].pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
renameBlock(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ssa::de_ssa(const std::unique_ptr<furlang::ir::function>& func) {
|
||||||
|
using namespace furlang::ir;
|
||||||
|
|
||||||
|
std::unordered_map<block_index, std::vector<std::unique_ptr<instruction>>> copies;
|
||||||
|
for (block_index blockIdx = 0; blockIdx < func->blocks().size(); ++blockIdx) {
|
||||||
|
const auto& block = func->blocks()[blockIdx];
|
||||||
|
auto& instrs = block->instructions();
|
||||||
|
for (auto it = instrs.begin(); it != instrs.end();) {
|
||||||
|
if ((*it)->type() != furlang::ir::instruction_t::Phi) {
|
||||||
|
++it;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto& phi = dynamic_cast<phi_instruction&>(**it);
|
||||||
|
auto dstReg = phi.destination().reg();
|
||||||
|
for (auto& [srcOp, label] : phi.labels()) {
|
||||||
|
copies[label].emplace_back(
|
||||||
|
std::make_unique<assign_instruction>(std::move(srcOp), operand::new_reg(dstReg)));
|
||||||
|
}
|
||||||
|
it = instrs.erase(it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& [blockIdx, cpys] : copies) {
|
||||||
|
if (blockIdx >= func->blocks().size()) continue;
|
||||||
|
const auto& block = func->blocks()[blockIdx];
|
||||||
|
auto& instrs = block->instructions();
|
||||||
|
for (auto& copy : cpys) {
|
||||||
|
instrs.push_back(std::move(copy));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class lattice_t {
|
||||||
|
Top,
|
||||||
|
Constant,
|
||||||
|
Bottom,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct lattice {
|
||||||
|
lattice_t type = lattice_t::Top;
|
||||||
|
std::uint64_t value = 0;
|
||||||
|
|
||||||
|
bool operator==(const lattice& rhs) const {
|
||||||
|
if (type != rhs.type) return false;
|
||||||
|
return type != lattice_t::Constant || value == rhs.value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void ssa::constant_propagation(const std::unique_ptr<furlang::ir::function>& func) {
|
||||||
|
using block_idx = furlang::ir::block_index;
|
||||||
|
using reg_t = furlang::ir::register_operand;
|
||||||
|
using reg2_t = furlang::ir::register_t;
|
||||||
|
|
||||||
|
std::unordered_map<reg_t, lattice> latVals;
|
||||||
|
std::unordered_map<reg2_t, std::vector<furlang::ir::instruction*>> edges;
|
||||||
|
std::unordered_set<block_idx> executableBlocks;
|
||||||
|
std::set<std::pair<block_idx, block_idx>> executedEdges;
|
||||||
|
|
||||||
|
std::queue<std::pair<block_idx, block_idx>> cfgWorklist;
|
||||||
|
std::queue<furlang::ir::instruction*> ssaWorklist;
|
||||||
|
|
||||||
|
std::unordered_map<furlang::ir::instruction*, block_idx> blockMap;
|
||||||
|
|
||||||
|
auto getOperandLattice = [&](const furlang::ir::operand& op) -> lattice {
|
||||||
|
if (op.type() == furlang::ir::operand_t::Integer) {
|
||||||
|
lattice lat;
|
||||||
|
lat.type = lattice_t::Constant;
|
||||||
|
lat.value = op.integer();
|
||||||
|
return lat;
|
||||||
|
}
|
||||||
|
if (op.type() == furlang::ir::operand_t::Register) {
|
||||||
|
auto reg = op.reg();
|
||||||
|
if (latVals.find(reg) == latVals.end()) return { lattice_t::Top };
|
||||||
|
return latVals[reg];
|
||||||
|
}
|
||||||
|
return { lattice_t::Bottom };
|
||||||
|
};
|
||||||
|
|
||||||
|
for (block_idx blockIdx = 0; blockIdx < func->blocks().size(); ++blockIdx) {
|
||||||
|
const auto& block = func->blocks()[blockIdx];
|
||||||
|
auto& instrs = block->instructions();
|
||||||
|
for (auto it = instrs.begin(); it != instrs.end(); ++it) {
|
||||||
|
const auto& instr = *it;
|
||||||
|
blockMap[instr.get()] = blockIdx;
|
||||||
|
for (const auto& op : instr->sources()) {
|
||||||
|
if (op->type() != furlang::ir::operand_t::Register) continue;
|
||||||
|
edges[op->reg()].push_back(instr.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
blockMap[block->exit().get()] = blockIdx;
|
||||||
|
for (const auto& op : block->exit()->sources()) {
|
||||||
|
if (op->type() != furlang::ir::operand_t::Register) continue;
|
||||||
|
edges[op->reg()].push_back(block->exit().get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cfgWorklist.push({ 0, 0 });
|
||||||
|
while (!cfgWorklist.empty() || !ssaWorklist.empty()) {
|
||||||
|
if (!cfgWorklist.empty()) {
|
||||||
|
auto edge = cfgWorklist.front();
|
||||||
|
cfgWorklist.pop();
|
||||||
|
block_idx from = edge.first;
|
||||||
|
block_idx to = edge.second;
|
||||||
|
|
||||||
|
if (executedEdges.count(edge) != 0) continue;
|
||||||
|
executedEdges.insert(edge);
|
||||||
|
|
||||||
|
bool firstVisit = (executableBlocks.find(to) == executableBlocks.end());
|
||||||
|
executableBlocks.insert(to);
|
||||||
|
|
||||||
|
const auto& block = func->blocks()[to];
|
||||||
|
if (firstVisit) {
|
||||||
|
for (auto& instr : block->instructions()) {
|
||||||
|
ssaWorklist.push(instr.get());
|
||||||
|
}
|
||||||
|
ssaWorklist.push(block->exit().get());
|
||||||
|
} else {
|
||||||
|
for (auto& instr : block->instructions()) {
|
||||||
|
if (instr->type() != furlang::ir::instruction_t::Phi) break;
|
||||||
|
ssaWorklist.push(instr.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ssaWorklist.empty()) {
|
||||||
|
auto* instr = ssaWorklist.front();
|
||||||
|
ssaWorklist.pop();
|
||||||
|
|
||||||
|
block_idx blockIdx = blockMap[instr];
|
||||||
|
if (executableBlocks.find(blockIdx) == executableBlocks.end()) continue;
|
||||||
|
|
||||||
|
lattice newLat = { lattice_t::Top };
|
||||||
|
|
||||||
|
switch (instr->type()) {
|
||||||
|
case furlang::ir::instruction_t::Phi: {
|
||||||
|
auto& phi = dynamic_cast<furlang::ir::phi_instruction&>(*instr);
|
||||||
|
for (const auto& [op, label] : phi.labels()) {
|
||||||
|
if (executedEdges.count({ label, blockIdx }) == 0) continue;
|
||||||
|
lattice opLat = getOperandLattice(op);
|
||||||
|
if (opLat.type == lattice_t::Bottom) newLat.type = lattice_t::Bottom;
|
||||||
|
if (opLat.type == lattice_t::Constant) {
|
||||||
|
if (newLat.type == lattice_t::Top) {
|
||||||
|
newLat = opLat;
|
||||||
|
} else if (newLat.type == lattice_t::Constant && newLat.value != opLat.value) {
|
||||||
|
newLat.type = lattice_t::Bottom;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case furlang::ir::instruction_t::Assign: {
|
||||||
|
newLat = getOperandLattice(*instr->sources().front());
|
||||||
|
} break;
|
||||||
|
case furlang::ir::instruction_t::BinaryOp: {
|
||||||
|
lattice lhs = getOperandLattice(*instr->sources()[0]);
|
||||||
|
lattice rhs = getOperandLattice(*instr->sources()[1]);
|
||||||
|
|
||||||
|
if (lhs.type == lattice_t::Bottom || rhs.type == lattice_t::Bottom) {
|
||||||
|
newLat.type = lattice_t::Bottom;
|
||||||
|
} else if (lhs.type == lattice_t::Constant && rhs.type == lattice_t::Constant) {
|
||||||
|
newLat.type = lattice_t::Constant;
|
||||||
|
switch (dynamic_cast<const furlang::ir::binary_op_instruction&>(*instr).op_type()) {
|
||||||
|
case furlang::ir::binary_op_instruction_t::Add: newLat.value = lhs.value + rhs.value; break;
|
||||||
|
case furlang::ir::binary_op_instruction_t::Sub: newLat.value = lhs.value - rhs.value; break;
|
||||||
|
case furlang::ir::binary_op_instruction_t::Mul: newLat.value = lhs.value * rhs.value; break;
|
||||||
|
case furlang::ir::binary_op_instruction_t::Div: newLat.value = lhs.value / rhs.value; break;
|
||||||
|
case furlang::ir::binary_op_instruction_t::Mod: newLat.value = lhs.value % rhs.value; break;
|
||||||
|
case furlang::ir::binary_op_instruction_t::Eq:
|
||||||
|
newLat.value = (lhs.value == rhs.value) ? 1 : 0;
|
||||||
|
break;
|
||||||
|
case furlang::ir::binary_op_instruction_t::NotEq:
|
||||||
|
newLat.value = (lhs.value != rhs.value) ? 1 : 0;
|
||||||
|
break;
|
||||||
|
case furlang::ir::binary_op_instruction_t::LessThan:
|
||||||
|
newLat.value = (lhs.value < rhs.value) ? 1 : 0;
|
||||||
|
break;
|
||||||
|
case furlang::ir::binary_op_instruction_t::GreaterThan:
|
||||||
|
newLat.value = (lhs.value > rhs.value) ? 1 : 0;
|
||||||
|
break;
|
||||||
|
case furlang::ir::binary_op_instruction_t::LessEq:
|
||||||
|
newLat.value = (lhs.value <= rhs.value) ? 1 : 0;
|
||||||
|
break;
|
||||||
|
case furlang::ir::binary_op_instruction_t::GreaterEq:
|
||||||
|
newLat.value = (lhs.value >= rhs.value) ? 1 : 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instr->has_destination() && instr->destination().type() == furlang::ir::operand_t::Register) {
|
||||||
|
auto dst = instr->destination().reg();
|
||||||
|
if (!(latVals[dst] == newLat)) {
|
||||||
|
latVals[dst] = newLat;
|
||||||
|
for (auto* uInstr : edges[dst])
|
||||||
|
ssaWorklist.push(uInstr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (instr == func->blocks()[blockIdx]->exit().get()) {
|
||||||
|
auto* exit = func->blocks()[blockIdx]->exit().get();
|
||||||
|
if (exit->type() == furlang::ir::instruction_t::Branch) {
|
||||||
|
auto& br = dynamic_cast<furlang::ir::branch_instruction&>(*exit);
|
||||||
|
cfgWorklist.push({ blockIdx, br.block() });
|
||||||
|
} else if (exit->type() == furlang::ir::instruction_t::BranchCond) {
|
||||||
|
auto& br = dynamic_cast<furlang::ir::branch_cond_instruction&>(*exit);
|
||||||
|
lattice cond = getOperandLattice(*exit->sources()[0]);
|
||||||
|
|
||||||
|
if (cond.type == lattice_t::Constant) {
|
||||||
|
if (cond.value != 0)
|
||||||
|
cfgWorklist.push({ blockIdx, br.if_block() });
|
||||||
|
else
|
||||||
|
cfgWorklist.push({ blockIdx, br.else_block() });
|
||||||
|
} else {
|
||||||
|
cfgWorklist.push({ blockIdx, br.if_block() });
|
||||||
|
cfgWorklist.push({ blockIdx, br.else_block() });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (block_idx i = 0; i < func->blocks().size(); ++i) {
|
||||||
|
if (executableBlocks.find(i) == executableBlocks.end()) {
|
||||||
|
func->blocks()[i]->instructions().clear();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& block = func->blocks()[i];
|
||||||
|
|
||||||
|
for (auto& instr : block->instructions()) {
|
||||||
|
for (auto& op : instr->sources()) {
|
||||||
|
if (op->type() != furlang::ir::operand_t::Register) continue;
|
||||||
|
auto reg = op->reg();
|
||||||
|
if (latVals[reg].type != lattice_t::Constant) continue;
|
||||||
|
*op = furlang::ir::operand::new_integer(latVals[reg].value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto* exit = block->exit().get();
|
||||||
|
if (exit->type() != furlang::ir::instruction_t::BranchCond) continue;
|
||||||
|
auto& br = dynamic_cast<furlang::ir::branch_cond_instruction&>(*exit);
|
||||||
|
lattice cond = getOperandLattice(*exit->sources()[0]);
|
||||||
|
if (cond.type != lattice_t::Constant) continue;
|
||||||
|
block_idx target = (cond.value != 0) ? br.if_block() : br.else_block();
|
||||||
|
block->exit() = std::make_unique<furlang::ir::branch_instruction>(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ssa::dead_code_elimination(const std::unique_ptr<furlang::ir::function>& func) {
|
||||||
|
using block_idx = furlang::ir::block_index;
|
||||||
|
using reg_t = furlang::ir::register_operand;
|
||||||
|
|
||||||
|
std::unordered_map<reg_t, furlang::ir::instruction*> defMap;
|
||||||
|
std::unordered_set<furlang::ir::instruction*> alive;
|
||||||
|
std::queue<furlang::ir::instruction*> worklist;
|
||||||
|
|
||||||
|
for (block_idx blockIdx = 0; blockIdx < func->blocks().size(); ++blockIdx) {
|
||||||
|
const auto& block = func->blocks()[blockIdx];
|
||||||
|
|
||||||
|
for (auto& instr : block->instructions()) {
|
||||||
|
if (instr->has_destination() && instr->destination().type() == furlang::ir::operand_t::Register) {
|
||||||
|
defMap[instr->destination().reg()] = instr.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto* exit = block->exit().get();
|
||||||
|
alive.insert(exit);
|
||||||
|
worklist.push(exit);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!worklist.empty()) {
|
||||||
|
const auto* instr = worklist.front();
|
||||||
|
worklist.pop();
|
||||||
|
|
||||||
|
for (const auto& op : instr->sources()) {
|
||||||
|
if (op->type() != furlang::ir::operand_t::Register) continue;
|
||||||
|
auto src = op->reg();
|
||||||
|
|
||||||
|
if (defMap.find(src) == defMap.end()) continue;
|
||||||
|
auto* defInstr = defMap[src];
|
||||||
|
if (alive.insert(defInstr).second) {
|
||||||
|
worklist.push(defInstr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (block_idx blockIdx = 0; blockIdx < func->blocks().size(); ++blockIdx) {
|
||||||
|
const auto& block = func->blocks()[blockIdx];
|
||||||
|
auto& instrs = block->instructions();
|
||||||
|
|
||||||
|
for (auto it = instrs.begin(); it != instrs.end();) {
|
||||||
|
if (alive.find(it->get()) == alive.end()) {
|
||||||
|
it = instrs.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ssa::copy_propagation(const std::unique_ptr<furlang::ir::function>& func) {
|
||||||
|
using block_idx = furlang::ir::block_index;
|
||||||
|
using reg_t = furlang::ir::register_operand;
|
||||||
|
|
||||||
|
std::unordered_map<reg_t, reg_t> aliasMap;
|
||||||
|
|
||||||
|
std::function<reg_t(const reg_t&)> findRep = [&](const reg_t& reg) -> reg_t {
|
||||||
|
auto it = aliasMap.find(reg);
|
||||||
|
if (it == aliasMap.end()) return reg;
|
||||||
|
reg_t act = findRep(it->second);
|
||||||
|
aliasMap[reg] = act;
|
||||||
|
return act;
|
||||||
|
};
|
||||||
|
|
||||||
|
for (block_idx blockIdx = 0; blockIdx < func->blocks().size(); ++blockIdx) {
|
||||||
|
const auto& block = func->blocks()[blockIdx];
|
||||||
|
for (auto& instr : block->instructions()) {
|
||||||
|
if (instr->type() != furlang::ir::instruction_t::Assign) continue;
|
||||||
|
auto& srcOp = *instr->sources().front();
|
||||||
|
if (srcOp.type() != furlang::ir::operand_t::Register ||
|
||||||
|
instr->destination().type() != furlang::ir::operand_t::Register)
|
||||||
|
continue;
|
||||||
|
reg_t dstReg = instr->destination().reg();
|
||||||
|
reg_t srcReg = srcOp.reg();
|
||||||
|
reg_t repSrc = findRep(srcReg);
|
||||||
|
reg_t repDst = findRep(dstReg);
|
||||||
|
if (repSrc == repDst) continue;
|
||||||
|
aliasMap[repDst] = repSrc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (block_idx blockIdx = 0; blockIdx < func->blocks().size(); ++blockIdx) {
|
||||||
|
const auto& block = func->blocks()[blockIdx];
|
||||||
|
for (auto& instr : block->instructions()) {
|
||||||
|
for (auto& op : instr->sources()) {
|
||||||
|
if (op->type() != furlang::ir::operand_t::Register) continue;
|
||||||
|
op->reg() = findRep(op->reg());
|
||||||
|
}
|
||||||
|
if (instr->type() != furlang::ir::instruction_t::Phi) continue;
|
||||||
|
auto& phi = dynamic_cast<furlang::ir::phi_instruction&>(*instr);
|
||||||
|
for (auto& [op, label] : phi.labels()) {
|
||||||
|
if (op.type() != furlang::ir::operand_t::Register) continue;
|
||||||
|
op.reg() = findRep(op.reg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& op : block->exit()->sources()) {
|
||||||
|
if (op->type() != furlang::ir::operand_t::Register) continue;
|
||||||
|
op->reg() = findRep(op->reg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ssa::dfs_rpo(furlang::ir::block_index block,
|
||||||
|
const block_map_t& successors,
|
||||||
|
std::unordered_set<furlang::ir::block_index>& visited,
|
||||||
|
std::vector<furlang::ir::block_index>& rpo) {
|
||||||
|
visited.insert(block);
|
||||||
|
if (auto it = successors.find(block); it != successors.end()) {
|
||||||
|
for (auto successor : it->second) {
|
||||||
|
if (visited.find(successor) == visited.end()) {
|
||||||
|
dfs_rpo(successor, successors, visited, rpo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rpo.push_back(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace furc::front
|
||||||
+8
-9
@@ -3,6 +3,7 @@
|
|||||||
#include "furc/ast/program.hpp"
|
#include "furc/ast/program.hpp"
|
||||||
#include "furc/front/ir_generator.hpp"
|
#include "furc/front/ir_generator.hpp"
|
||||||
#include "furc/front/parser.hpp"
|
#include "furc/front/parser.hpp"
|
||||||
|
#include "furc/front/ssa.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@@ -10,16 +11,12 @@ int main(void) {
|
|||||||
try {
|
try {
|
||||||
std::string programStr = R"(
|
std::string programStr = R"(
|
||||||
func main() {
|
func main() {
|
||||||
x = 5;
|
x = 0;
|
||||||
x -= 3;
|
y = 10;
|
||||||
if (x < 3) {
|
z = 1;
|
||||||
y = x * 2;
|
while (x < y) {
|
||||||
w = y;
|
x = x + z;
|
||||||
} else {
|
|
||||||
y = x - 3;
|
|
||||||
}
|
}
|
||||||
w = x - y;
|
|
||||||
z = x + y;
|
|
||||||
}
|
}
|
||||||
)";
|
)";
|
||||||
furc::front::parser parser("<TEMP>", programStr);
|
furc::front::parser parser("<TEMP>", programStr);
|
||||||
@@ -34,6 +31,8 @@ int main(void) {
|
|||||||
program->accept(generator);
|
program->accept(generator);
|
||||||
|
|
||||||
auto module = std::move(generator.move_module());
|
auto module = std::move(generator.move_module());
|
||||||
|
furc::front::ssa::optimize(module);
|
||||||
|
|
||||||
std::cout << "Generated IR:\n";
|
std::cout << "Generated IR:\n";
|
||||||
for (const auto& function : module.functions()) {
|
for (const auto& function : module.functions()) {
|
||||||
std::cout << function->name() << ":\n";
|
std::cout << function->name() << ":\n";
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace furlang {
|
namespace furlang {
|
||||||
namespace ir {
|
namespace ir {
|
||||||
@@ -21,6 +24,7 @@ enum class instruction_t {
|
|||||||
Branch, /**< Branch */
|
Branch, /**< Branch */
|
||||||
BranchCond, /**< Conditional branch */
|
BranchCond, /**< Conditional branch */
|
||||||
Return, /**< Return */
|
Return, /**< Return */
|
||||||
|
Phi, /**< Phi function */
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,6 +70,41 @@ public:
|
|||||||
* @return The type.
|
* @return The type.
|
||||||
*/
|
*/
|
||||||
virtual instruction_t type() const = 0;
|
virtual instruction_t type() const = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns whether this instruction has a destination operand.
|
||||||
|
*
|
||||||
|
* @return true if this instruction has the destination operand.
|
||||||
|
*/
|
||||||
|
virtual bool has_destination() const { return false; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns destination operand of this instruction.
|
||||||
|
*
|
||||||
|
* @return The destination operand.
|
||||||
|
*/
|
||||||
|
virtual operand& destination() { throw std::runtime_error("instruction type mismatch"); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns destination operand of this instruction.
|
||||||
|
*
|
||||||
|
* @return The destination operand.
|
||||||
|
*/
|
||||||
|
virtual const operand& destination() const { throw std::runtime_error("instruction type mismatch"); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns a list of source operands of this instruction.
|
||||||
|
*
|
||||||
|
* @return The list of source operands.
|
||||||
|
*/
|
||||||
|
virtual std::vector<operand*> sources() { return {}; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns a list of source operands of this instruction.
|
||||||
|
*
|
||||||
|
* @return The list of source operands.
|
||||||
|
*/
|
||||||
|
virtual std::vector<const operand*> sources() const { return {}; }
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Prints an instruction to an output stream.
|
* @brief Prints an instruction to an output stream.
|
||||||
@@ -158,18 +197,39 @@ public:
|
|||||||
instruction_t type() const override { return instruction_t::Assign; }
|
instruction_t type() const override { return instruction_t::Assign; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns this instruction's source.
|
* @brief Returns whether this instruction has a destination operand.
|
||||||
*
|
*
|
||||||
* @return The source.
|
* @return true
|
||||||
*/
|
*/
|
||||||
const operand& source() const { return m_source; }
|
bool has_destination() const override { return true; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns this instruction's destination.
|
* @brief Returns this instruction's destination.
|
||||||
*
|
*
|
||||||
* @return The destination.
|
* @return The destination.
|
||||||
*/
|
*/
|
||||||
const operand& destination() const { return m_destination; }
|
operand& destination() override { return m_destination; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns this instruction's destination.
|
||||||
|
*
|
||||||
|
* @return The destination.
|
||||||
|
*/
|
||||||
|
const operand& destination() const override { return m_destination; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns a list of this instruction's source operands.
|
||||||
|
*
|
||||||
|
* @return The list of source operands.
|
||||||
|
*/
|
||||||
|
std::vector<operand*> sources() override { return { &m_source }; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns a list of this instruction's source operands.
|
||||||
|
*
|
||||||
|
* @return The list of source operands.
|
||||||
|
*/
|
||||||
|
std::vector<const operand*> sources() const override { return { &m_source }; }
|
||||||
private:
|
private:
|
||||||
operand m_source;
|
operand m_source;
|
||||||
operand m_destination;
|
operand m_destination;
|
||||||
@@ -253,6 +313,16 @@ public:
|
|||||||
*/
|
*/
|
||||||
instruction_t type() const override { return instruction_t::BinaryOp; }
|
instruction_t type() const override { return instruction_t::BinaryOp; }
|
||||||
|
|
||||||
|
bool has_destination() const override { return true; }
|
||||||
|
|
||||||
|
operand& destination() override { return m_dst; }
|
||||||
|
|
||||||
|
const operand& destination() const override { return m_dst; }
|
||||||
|
|
||||||
|
std::vector<operand*> sources() override { return { &m_lhs, &m_rhs }; }
|
||||||
|
|
||||||
|
std::vector<const operand*> sources() const override { return { &m_lhs, &m_rhs }; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns this instruction's operation type.
|
* @brief Returns this instruction's operation type.
|
||||||
*
|
*
|
||||||
@@ -374,6 +444,9 @@ public:
|
|||||||
public:
|
public:
|
||||||
instruction_t type() const override { return instruction_t::BranchCond; }
|
instruction_t type() const override { return instruction_t::BranchCond; }
|
||||||
|
|
||||||
|
std::vector<operand*> sources() override { return { &m_condition }; }
|
||||||
|
std::vector<const operand*> sources() const override { return { &m_condition }; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns this instruction's condition operand.
|
* @brief Returns this instruction's condition operand.
|
||||||
*
|
*
|
||||||
@@ -442,6 +515,16 @@ public:
|
|||||||
*/
|
*/
|
||||||
instruction_t type() const override { return instruction_t::Return; }
|
instruction_t type() const override { return instruction_t::Return; }
|
||||||
|
|
||||||
|
std::vector<operand*> sources() override {
|
||||||
|
if (m_value.has_value()) return { &*m_value };
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<const operand*> sources() const override {
|
||||||
|
if (m_value.has_value()) return { &*m_value };
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns this instruction's return value operand.
|
* @brief Returns this instruction's return value operand.
|
||||||
*
|
*
|
||||||
@@ -458,7 +541,93 @@ protected:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Phi instruction
|
||||||
|
*
|
||||||
|
* Used to implement the phi node in the SSA graph.
|
||||||
|
*/
|
||||||
|
class phi_instruction final : public instruction {
|
||||||
|
public:
|
||||||
|
phi_instruction(register_operand dst)
|
||||||
|
: m_dst(operand::new_reg(dst)) {}
|
||||||
|
|
||||||
|
~phi_instruction() override = default;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Move constructor.
|
||||||
|
*/
|
||||||
|
phi_instruction(phi_instruction&&) noexcept = default;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Move constructor.
|
||||||
|
*/
|
||||||
|
phi_instruction& operator=(phi_instruction&&) noexcept = default;
|
||||||
|
|
||||||
|
phi_instruction(const phi_instruction&) = delete;
|
||||||
|
|
||||||
|
phi_instruction& operator=(const phi_instruction&) = delete;
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Returns this instruction's type.
|
||||||
|
*
|
||||||
|
* @return instruction_t::Phi.
|
||||||
|
*/
|
||||||
|
instruction_t type() const override { return instruction_t::Phi; }
|
||||||
|
|
||||||
|
bool has_destination() const override { return true; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns this instruction's destination register.
|
||||||
|
*
|
||||||
|
* @return The register.
|
||||||
|
*/
|
||||||
|
operand& destination() override { return m_dst; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns this instruction's destination register.
|
||||||
|
*
|
||||||
|
* @return The register.
|
||||||
|
*/
|
||||||
|
const operand& destination() const override { return m_dst; }
|
||||||
|
|
||||||
|
std::vector<const operand*> sources() const override {
|
||||||
|
std::vector<const operand*> srcs;
|
||||||
|
srcs.reserve(m_labels.size());
|
||||||
|
for (const auto& [op, _block] : m_labels)
|
||||||
|
srcs.push_back(&op);
|
||||||
|
return srcs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns this instruction's labels.
|
||||||
|
*
|
||||||
|
* @return The labels.
|
||||||
|
*/
|
||||||
|
std::vector<std::pair<operand, block_index>>& labels() { return m_labels; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns this instruction's labels.
|
||||||
|
*
|
||||||
|
* @return The labels.
|
||||||
|
*/
|
||||||
|
const std::vector<std::pair<operand, block_index>>& labels() const { return m_labels; }
|
||||||
|
private:
|
||||||
|
operand m_dst;
|
||||||
|
std::vector<std::pair<operand, block_index>> m_labels;
|
||||||
|
protected:
|
||||||
|
std::ostream& print(std::ostream& os) const override {
|
||||||
|
os << "phi " << m_dst << " =";
|
||||||
|
bool first = true;
|
||||||
|
for (const auto& pair : m_labels) {
|
||||||
|
if (!first) os << ',';
|
||||||
|
first = false;
|
||||||
|
os << ' ' << pair.second << ": " << pair.first;
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace ir
|
} // namespace ir
|
||||||
} // namespace furlang
|
} // namespace furlang
|
||||||
|
|
||||||
#endif // FURLANG_IR_INSTRUCTION_HPP
|
#endif // FURLANG_IR_INSTRUCTION_HPP
|
||||||
|
|||||||
@@ -19,11 +19,37 @@ enum class operand_t {
|
|||||||
String, /**< String */
|
String, /**< String */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Alias to a register type.
|
||||||
|
*/
|
||||||
|
using register_t = std::uint32_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Register operand alias.
|
* @brief Register operand alias.
|
||||||
* @see operand_t::Register
|
* @see operand_t::Register
|
||||||
*/
|
*/
|
||||||
using register_operand = std::uint32_t;
|
struct register_operand {
|
||||||
|
register_t reg = 0;
|
||||||
|
std::uint32_t ver = 0;
|
||||||
|
|
||||||
|
register_operand() = default;
|
||||||
|
|
||||||
|
register_operand(register_t reg)
|
||||||
|
: reg(reg) {}
|
||||||
|
|
||||||
|
register_operand(register_t reg, std::uint32_t ver)
|
||||||
|
: reg(reg), ver(ver) {}
|
||||||
|
|
||||||
|
operator std::uint32_t&() { return reg; }
|
||||||
|
operator const std::uint32_t&() const { return reg; }
|
||||||
|
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, const register_operand& op) {
|
||||||
|
return os << op.reg << '_' << op.ver;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const register_operand& rhs) const { return reg == rhs.reg && ver == rhs.ver; }
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Variable operand alias.
|
* @brief Variable operand alias.
|
||||||
* @see operand_t::Variable
|
* @see operand_t::Variable
|
||||||
@@ -102,6 +128,19 @@ public:
|
|||||||
operand(const operand&) = delete;
|
operand(const operand&) = delete;
|
||||||
operand& operator=(const operand&) = delete;
|
operand& operator=(const operand&) = delete;
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Construct a new register operand.
|
||||||
|
*
|
||||||
|
* @param value Value of the new register operand.
|
||||||
|
* @return The register operand.
|
||||||
|
*/
|
||||||
|
static operand new_reg(register_t value) {
|
||||||
|
operand operand;
|
||||||
|
operand.m_type = operand_t::Register;
|
||||||
|
operand.m_value.reg = { value, 0 };
|
||||||
|
return operand;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Construct a new register operand.
|
* @brief Construct a new register operand.
|
||||||
*
|
*
|
||||||
@@ -163,6 +202,13 @@ public:
|
|||||||
*/
|
*/
|
||||||
operand_t type() const { return m_type; }
|
operand_t type() const { return m_type; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns this operand's register value.
|
||||||
|
*
|
||||||
|
* @return The register value.
|
||||||
|
*/
|
||||||
|
register_operand& reg() { return m_value.reg; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns this operand's register value.
|
* @brief Returns this operand's register value.
|
||||||
*
|
*
|
||||||
@@ -228,4 +274,17 @@ private:
|
|||||||
} // namespace ir
|
} // namespace ir
|
||||||
} // namespace furlang
|
} // namespace furlang
|
||||||
|
|
||||||
#endif // FURLANG_IR_OPERAND_HPP
|
namespace std {
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct hash<furlang::ir::register_operand> {
|
||||||
|
std::size_t operator()(const furlang::ir::register_operand& op) const noexcept {
|
||||||
|
std::size_t h1 = std::hash<decltype(op.reg)>{}(op.reg);
|
||||||
|
std::size_t h2 = std::hash<std::uint32_t>{}(op.ver);
|
||||||
|
return h1 ^ (h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace std
|
||||||
|
|
||||||
|
#endif // FURLANG_IR_OPERAND_HPP
|
||||||
|
|||||||
Reference in New Issue
Block a user