CompilerSysY/include/pass.h

106 lines
3.0 KiB
C++

#pragma once
#include "common.h"
#include "llir_module.h"
#include "machcode.h"
#define DEBUG_REGALLOC
namespace CompSysY {
class Pass {
public:
std::string pass_name;
Pass(const std::string &name) : pass_name(name) {}
virtual void run(const Module &module) = 0;
};
class PassMem2Reg : public Pass {
public:
PassMem2Reg() : Pass("mem2reg") {}
virtual void run(const Module &module) override;
};
class PassBuildCFG : public Pass {
public:
PassBuildCFG() : Pass("build_cfg") {}
virtual void run(const Module &module) override;
};
class MCPass {
public:
std::string pass_name;
MCPass(const std::string &name) : pass_name(name) {}
virtual void run(const MCModule &module) = 0;
};
class PassRegAlloc : public MCPass {
public:
PassRegAlloc() : MCPass("regalloc") {}
virtual void run(const MCModule &module) override;
const static int K = 32 - 4; // Not Allocate-able: x0, gp, tp; Reserve: sp
private:
void reg_alloc(sptr(MFunction));
void build(sptr(MFunction));
void add_edge(const MOperand &u, const MOperand &v);
void make_work_list(sptr(MFunction) func);
bool move_related(const MOperand &n);
std::set<sptr(MInstMove)> node_moves(const MOperand &n);
std::set<MOperand> adjacent(const MOperand &n);
void decrement_degree(const MOperand &m);
void enable_moves(const MOperand &n);
void simplify();
void coalesce();
void add_work_list(const MOperand &u);
bool OK(const MOperand &t, const MOperand &r);
bool conservative(const std::set<MOperand> &nodes);
MOperand get_alias(MOperand n);
void combine(const MOperand &u, const MOperand &v);
void freeze();
void freeze_moves(const MOperand &u);
void select_spill();
void assign_colors(sptr(MFunction) func);
void rewrite_program(sptr(MFunction) func);
std::map<MOperand, std::set<MOperand>> adj_list;
std::set<std::pair<MOperand, MOperand>> adj_set;
std::map<MOperand, unsigned> degree;
std::map<MOperand, std::set<sptr(MInstMove)>> move_list;
std::map<MOperand, MOperand> color;
std::map<MOperand, MOperand> alias;
std::set<MOperand> simplify_worklist;
std::set<MOperand> freeze_worklist;
std::set<MOperand> spill_worklist;
std::set<MOperand> spilled_nodes;
std::set<MOperand> coalesced_nodes;
std::set<MOperand> colored_nodes;
std::vector<MOperand> select_stack;
std::set<sptr(MInstMove)> coalesced_moves;
std::set<sptr(MInstMove)> constrained_moves;
std::set<sptr(MInstMove)> frozen_moves;
std::set<sptr(MInstMove)> worklist_moves;
std::set<sptr(MInstMove)> active_moves;
void clear() {
adj_list.clear();
adj_set.clear();
degree.clear();
move_list.clear();
color.clear();
alias.clear();
simplify_worklist.clear();
freeze_worklist.clear();
spill_worklist.clear();
spilled_nodes.clear();
coalesced_nodes.clear();
colored_nodes.clear();
select_stack.clear();
coalesced_moves.clear();
constrained_moves.clear();
frozen_moves.clear();
worklist_moves.clear();
active_moves.clear();
}
};
} // namespace CompSysY