CompilerSysY/include/pass.h
2023-07-10 15:04:56 +08:00

96 lines
2.0 KiB
C++

#pragma once
#include "pass_base.h"
namespace CompSysY
{
class PassSCCP final : public Pass
{
public:
PassSCCP() : Pass("constant propagation") {}
void run(const Module &module) override;
enum class ConstLatTag
{
Top = 0,
Const,
Bottom
};
struct ConstLat
{
ConstLatTag tag = ConstLatTag::Top;
int value = ~0;
bool is_top() const
{
return tag == ConstLatTag::Top;
}
bool is_bot() const
{
return tag == ConstLatTag::Bottom;
}
bool is_const() const
{
return tag == ConstLatTag::Const;
}
bool operator==(const ConstLat &op2) const
{
return tag == op2.tag && value == op2.value;
}
bool operator!=(const ConstLat &op2) const
{
return !(tag == op2.tag && value == op2.value);
}
static ConstLat get_bot()
{
return {ConstLatTag::Bottom, ~0};
}
};
private:
typedef std::pair<int, int> edge_type;
std::set<edge_type> FLowWL;
std::set<edge_type> SSAWL;
std::map<edge_type, bool> ExecFlag;
std::map<Value *, ConstLat> LatCell;
std::unordered_map<Instruction *, int> inst_id;
std::unordered_map<int, Instruction *> id_inst;
std::set<std::pair<int, int>> edge_set;
std::unordered_map<int, std::unordered_set<int>> edge_list;
void build_single_inst_block(Function *func);
void Initialize(Function *);
void SCCP(Function *);
void Visit_Phi(InstPhi *);
void Visit_Inst(Instruction *);
ConstLat Lat_Eval(Instruction *inst);
void post_sccp();
};
class PassMem2Reg final : public Pass
{
public:
PassMem2Reg() : Pass("mem2reg") {}
void run(const Module &module) override;
};
class PassBuildCFG final : public Pass
{
public:
PassBuildCFG() : Pass("build control flow graph") {}
void run(const Module &module) override;
};
class PassConstFold final : public Pass
{
public:
PassConstFold() : Pass("constant fold") {}
void run(const Module &module) override;
};
class PassDCE final : public Pass
{
public:
PassDCE() : Pass("dead code elimination") {}
void run(const Module &module) override;
};
} // namespace CompSysY