diff --git a/CMakeLists.txt b/CMakeLists.txt index 0020c88354760069d8fb71afc8f2de7289487fc7..3aefb71f83453342fb498d86f61b2dbca704826e 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,5 +49,6 @@ include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_BINARY_DIR}) include_directories(include/lightir) include_directories(include/optimization) +include_directories(include/codegen) add_subdirectory(src) add_subdirectory(tests) diff --git a/include/codegen.hpp b/include/codegen/codegen.hpp similarity index 95% rename from include/codegen.hpp rename to include/codegen/codegen.hpp index a78f5d47496564b40d3844104004ace997f1cf80..3b3b66ce65dd5784290ec33818ac4cf1555744ff 100644 --- a/include/codegen.hpp +++ b/include/codegen/codegen.hpp @@ -2,13 +2,17 @@ #define CODEGEN_HPP #include "BasicBlock.h" +#include "Constant.h" #include "Function.h" #include "IRprinter.h" #include "Instruction.h" #include "Module.h" #include "Value.h" +#include "liverange.hpp" #include "logging.hpp" +#define __RO_PART__ + #include #include #include @@ -23,7 +27,7 @@ using std::vector; class CodeGen { public: - CodeGen(Module *module) : m(module) {} + CodeGen(Module *m_) : m(m_), LRA(m_, phi_map) {} string print() { string result; @@ -143,12 +147,16 @@ class CodeGen { std::map> func_arg_off; // to $sp std::map func_arg_N; // total space for args std::map>> phi_map; + std::map ROdata; unsigned int stackN; // function local vars and so on Function *cur_func; Module *m; vector output; + + // register allocation + LiveRangeAnalyzer LRA; }; #endif diff --git a/include/codegen/liverange.hpp b/include/codegen/liverange.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f16010dc840e59acbcd49c43c3781e5e7bcb1d18 --- /dev/null +++ b/include/codegen/liverange.hpp @@ -0,0 +1,76 @@ +#ifndef LIVERANGE_HPP +#define LIVERANGE_HPP + +#include "BasicBlock.h" +#include "Function.h" +#include "Instruction.h" +#include "Module.h" +#include "Value.h" + +#include +#include +#include +#include +#include +#include + +using std::map; +using std::pair; +using std::set; +using std::string; +using std::vector; + +#define __LRA_PRINT__ + +class LiveRangeAnalyzer { + friend class CodeGen; + using LiveSet = set; + using PhiMap = map>>; + + public: + struct Interval { + Interval(int a, int b) : i(a), j(b) {} + Interval() = delete; + int i, j; + }; + LiveRangeAnalyzer(Module *m_, PhiMap &phi_map_) + : m(m_), phi_map(phi_map_) {} + LiveRangeAnalyzer() = delete; + + void run(); + void clear(); + void print(Function *func, bool printSet = true); + string print_liveSet(LiveSet &ls) { + string s = "[ "; + for (auto k : ls) + s += k->get_name() + " "; + s += "]"; + return s; + } + + private: + Module *m; + // Function *func; + map liverange; + map IN, OUT; + map instr_id; + map, int> cpstmt_id; + const PhiMap &phi_map; + + void make_id(Function *); + void run(Function *); + LiveSet joinFor(BasicBlock *bb); + void union_ip(LiveSet &dest, LiveSet &src) { + LiveSet res; + set_union(dest.begin(), + dest.end(), + src.begin(), + src.end(), + std::inserter(res, res.begin())); + dest = res; + } + // Require: out-set is already set + // Return: the in-set(will not set IN-map) + LiveSet transferFunction(Instruction *); +}; +#endif diff --git a/src/codegen/CMakeLists.txt b/src/codegen/CMakeLists.txt index 2698e820d078912d1219f418416bfcb93ee67dc6..302c048c9dfee88dc5d8678788fc46b925591409 100644 --- a/src/codegen/CMakeLists.txt +++ b/src/codegen/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(codegen STATIC codegen.cpp + liverange.cpp ) target_link_libraries(common)