diff --git a/src/backend/program.cpp b/src/backend/program.cpp index 3a1d37f471b87528958e9118a4f5bd4c980ce669..63c4ea7916bf522fc4d913613d1bfd18565d7fdc 100644 --- a/src/backend/program.cpp +++ b/src/backend/program.cpp @@ -195,17 +195,12 @@ bool BasicBlock::construct(ir::BasicBlock *ir_bb, BasicBlock *exit_bb, Function const Reg lhs = Reg(ir_binary->getlhs()->get_index(), false, ir_binary->getlhs()->get_type().base_type == ScalarType::Int); const Reg rhs = Reg(ir_binary->getrhs()->get_index(), false, ir_binary->getrhs()->get_type().base_type == ScalarType::Int); if (ir_binary->get_type() == BinaryOp::Add) { -<<<<<<< HEAD - if (lhs.is_gp()) - instrs_.push_back(new RiscvInstr::BinaryInt(RiscvInstr::RvBinaryOp::ADD, dst, lhs, rhs)); -======= if (lhs.is_gp()) { if (lhs == rhs) instrs_.push_back(new RiscvInstr::BinaryImm(RiscvInstr::RvBinaryOp::SLL, dst, lhs, 1)); else instrs_.push_back(new RiscvInstr::BinaryInt(RiscvInstr::RvBinaryOp::ADD, dst, lhs, rhs)); } ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 else instrs_.push_back(new RiscvInstr::BinaryInt(RiscvInstr::RvBinaryOp::FADDS, dst, lhs, rhs)); } else if (ir_binary->get_type() == BinaryOp::Sub) { @@ -902,8 +897,6 @@ void BasicBlock::gen_asm(std::ostream &out, int stack_object_size_, std::unorder curr_bb = empty_bbs[curr_bb]; } ir_jump->set_target(curr_bb); -<<<<<<< HEAD -======= if(bbs_used_no_branch.find(curr_bb) != bbs_used_no_branch.end()){ need_gen = false; curr_bb->gen_asm(out, stack_object_size_, empty_bbs, bbs_used_no_branch, false, next_bb); @@ -912,7 +905,6 @@ void BasicBlock::gen_asm(std::ostream &out, int stack_object_size_, std::unorder need_gen = false; } instr_it--; ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 } if (auto ir_branch = dynamic_cast<RiscvInstr::Branch*>(*instr_it)) { BasicBlock* curr_bb = ir_branch->get_target(); @@ -920,13 +912,10 @@ void BasicBlock::gen_asm(std::ostream &out, int stack_object_size_, std::unorder curr_bb = empty_bbs[curr_bb]; } ir_branch->set_target(curr_bb); -<<<<<<< HEAD -======= if(++instr_it == instrs_after_alloc_.end() && next_bb == curr_bb){ need_gen = false; } instr_it--; ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 } if(need_gen)(*instr_it)->gen_asm(out); // if (auto ir_call = dynamic_cast<RiscvInstr::Call*>(*instr_it)) { @@ -1306,10 +1295,7 @@ Function::Function(Program *prog, ir::Function *ir_func): name_(ir_func->get_nam if(auto ir_phi = dynamic_cast<ir::instruction::Phi*>(ir_instr)) { for(int i = 0; i < ir_phi->getsize(); i++) { auto ori_bb = (*ir_phi->getbbs())[i]; -<<<<<<< HEAD -======= //ir_loadimm->getdst()->get_type().base_type == ScalarType::Int ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 bool is_gp_reg = (*ir_phi->getvalues())[i]->get_type().base_type == ScalarType::Int; auto phi_src = Reg((*ir_phi->getvalues())[i]->get_index(), false, is_gp_reg); auto phi_mid = Reg(this->reg_n++, false, is_gp_reg); diff --git a/src/backend/program.hpp b/src/backend/program.hpp index 4226819989ff369a98ac9a335ec3dcd49daac0d1..12adaf93a6141bd7577bd7c32ffb9611ad8b1fc8 100644 --- a/src/backend/program.hpp +++ b/src/backend/program.hpp @@ -84,14 +84,9 @@ public: void analyzeLivenessForEachInstr(); void analyzeLivenessForEachInstrAfterAlloc(); BasicBlock* check_no_use(); -<<<<<<< HEAD - /// generate asm for this BasicBlock - void gen_asm(std::ostream &out, int stack_object_size_, std::unordered_map<BasicBlock *, BasicBlock *> empty_bbs); -======= void update_bbs_used(std::unordered_map<BasicBlock *, int>& bbs_used_no_branch, std::unordered_map<BasicBlock *, BasicBlock *> empty_bbs); /// generate asm for this BasicBlock void gen_asm(std::ostream &out, int stack_object_size_, std::unordered_map<BasicBlock *, BasicBlock *> empty_bbs, std::unordered_map<BasicBlock *, int> bbs_used_no_branch, bool from_function, BasicBlock*& next_bb); ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 void gen_asm_ori(std::ostream &out); }; diff --git a/src/frontend/main.cpp b/src/frontend/main.cpp index 4f47842743b39bb8ad5a045b94a9d1a9b85f71cc..bb64087ba7ebde64787f16e7514fea39b74e7dcd 100644 --- a/src/frontend/main.cpp +++ b/src/frontend/main.cpp @@ -1,4 +1,5 @@ #include <iostream> +#include <fstream> #include <exception> #include "frontend/SysYLexer.h" diff --git a/src/middleend/cfg.cpp b/src/middleend/cfg.cpp index d4c9c4f21630d66b2299da4bf65f521566e69aec..d9bdb7b3908faa422e6677330227d6bb272eac96 100644 --- a/src/middleend/cfg.cpp +++ b/src/middleend/cfg.cpp @@ -120,157 +120,4 @@ namespace middleend { build_bb_edge(); } - // void CFG::compute_rpo() { - // for (auto &bb : *func->get_basic_blocks()) { - // bb->visit = false; - // } - // rpo.clear(); - // func->bbs.front()->rpo_dfs(rpo); - // for (auto bb : rpo) { - // bb->rpo_num = rpo.size() - bb->rpo_num; // reverse - // } - // std::reverse(rpo.begin(), rpo.end()); - // } - - // void CFG::compute_dom_level(ir::BasicBlock *bb, int dom_level) { - // bb->domlevel = dom_level; - // for (ir::BasicBlock *succ : bb->dom) { - // compute_dom_level(succ, dom_level + 1); - // } - // } - - // void CFG::compute_dom() { - // for (auto &bb : *func->get_basic_blocks()) { - // bb->dom.clear(); - // bb->domby.clear(); - // bb->idom = nullptr; - // } - // ir::BasicBlock *entry = (*func->get_basic_blocks()).front(); - // entry->domby = {entry}; - // std::unordered_set<ir::BasicBlock *> all_bb; - // for (auto &bb : *func->get_basic_blocks()) { - // all_bb.insert(bb); - // } - // for (auto iter = ++(*func->get_basic_blocks()).begin(); iter != (*func->get_basic_blocks()).end(); iter++) { - // (*iter)->domby = all_bb; - // } - // this->compute_rpo(); - // bool modify = true; - // while (modify) { - // modify = false; - // for (auto bb : rpo) { - // auto &domby_bb = bb->domby; - // auto &dom_bb = bb->dom; - // for (auto iter = domby_bb.begin(); iter != domby_bb.end();) { - // ir::BasicBlock *x = *iter; - // auto &prev_bb = bb->prev; - // if (x != bb) { - // bool find = false; - // for (auto &pre : prev_bb) { - // if (pre->domby.find(x) == pre->domby.end()) { - // modify = true; - // find = true; - // iter = domby_bb.erase(iter); - // break; - // } - // } - // if (!find) { - // ++iter; - // } - // } else { - // ++iter; - // } - // } - // } - // } - - // entry->idom = nullptr; - // for (auto iter = std::next((*func->get_basic_blocks()).begin()); iter != (*func->get_basic_blocks()).end(); - // iter++) { - // auto bb = *iter; - // auto &domby_bb = bb->domby; - // for (ir::BasicBlock *d : domby_bb) { - // if (d != bb) { - // bool all_true = true; - // for (auto &pre : domby_bb) { - // if (pre == bb || pre == d || pre->domby.find(d) == pre->domby.end()) { - // continue; - // } - // all_true = false; - // break; - // } - // if (all_true) { - // bb->idom = d; - // d->dom.insert(bb); - // break; - // } - // } - // } - // } - // compute_dom_level(entry, 0); - // } - - // void BasicBlock::loop_dfs() { - // // dfs on dom tree - // this->loop = nullptr; - // for (auto next : dom) { - // next->loop_dfs(); - // } - // std::vector<BasicBlock *> bbs; - // for (auto p : prev) { - // if (p->domby.count(this)) { // find back edge to header - // bbs.emplace_back(p); - // } - // } - // if (!bbs.empty()) { // form 1 loop ( TODO: nested loops with same header? ) - // Loop *new_loop = new Loop(this); - // func->loops.emplace_back(new_loop); - // while (bbs.size() > 0) { - // auto bb = bbs.back(); - // bbs.pop_back(); - // if (!bb->loop) { - // bb->loop = new_loop; - // if (bb != this) { - // bbs.insert(bbs.end(), bb->prev.begin(), bb->prev.end()); - // } - // } else { - // Loop *inner_loop = bb->loop; - // while (inner_loop->outer) - // inner_loop = inner_loop->outer; - // if (inner_loop == new_loop) - // continue; - // new_loop->no_inner = false; - // inner_loop->outer = new_loop; - // bbs.insert(bbs.end(), inner_loop->header->prev.begin(), - // inner_loop->header->prev.end()); - // } - // } - // } - // } - - // void Function::loop_analysis() { - // this->clear_visit(); - // this->cfg->compute_dom(); - // this->loops.clear(); - // this->bbs.front()->loop_dfs(); - // for (auto &bb : this->bbs) { - // calc_loop_level(bb->loop); - // } - // } - - // void calc_loop_level(Loop *loop) { - // if (!loop) { - // return; - // } - // if (loop->level != -1) - // return; - // if (loop->outer == nullptr) - // loop->level = 1; - // else { - // calc_loop_level(loop->outer); - // loop->level = loop->outer->level + 1; - // } - // } - - } // namespace middleend \ No newline at end of file diff --git a/src/middleend/cfg.hpp b/src/middleend/cfg.hpp index 24f098caacf123d9367af8fc6583e68268ddd812..f66c8157b884de7090bf565ebd4fc9b519a65a98 100644 --- a/src/middleend/cfg.hpp +++ b/src/middleend/cfg.hpp @@ -50,10 +50,7 @@ public: void compute_dom_level(ir::BasicBlock *bb, int dom_level); void compute_dom(); void compute_rpo(); -<<<<<<< HEAD -======= void loop_analysis(); ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 }; } // namespace middleend \ No newline at end of file diff --git a/src/middleend/constant_propagation.cpp b/src/middleend/constant_propagation.cpp index 1e8f83c4ae6c0112b4ceea843b6cfae588745021..f4b9984e0a7509a50df0c8a0bc05f29f8fc78777 100644 --- a/src/middleend/constant_propagation.cpp +++ b/src/middleend/constant_propagation.cpp @@ -28,11 +28,7 @@ void update_phi(ir::BasicBlock* bb, int now_bb){ } bool isPowerOfTwo(int x) { -<<<<<<< HEAD - return x > 0 && (x & (x-1) == 0); -======= return x > 0 && ((x & (x-1)) == 0); ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 } int log2(int x) { @@ -42,40 +38,6 @@ int log2(int x) { return res; } -<<<<<<< HEAD -ir::instruction::BinaryImm* convertToBinaryImm(ir::instruction::Binary *binary, ConstValue imm) { - // std::cout << "binary to imm " << binary->to_str() << "\n"; - int32_t constant_value = imm.iv; - if (binary->get_type() == BinaryOp::Mul && isPowerOfTwo(constant_value)) { - int shift_amount = log2(constant_value); - auto inst = new ir::instruction::BinaryImm(BinaryOp::Ashl, binary->getdst(), binary->getlhs(), ConstValue(shift_amount)); - return inst; - } - else if ((binary->get_type() == BinaryOp::Div || binary->get_type() == BinaryOp::Mod) && isPowerOfTwo(constant_value)) { - int shift_amount = log2(constant_value); - if (binary->get_type() == BinaryOp::Div) { - auto right_shift_op = BinaryOp::Shr; - auto inst = new ir::instruction::BinaryImm(right_shift_op, binary->getdst(), binary->getlhs(), ConstValue(shift_amount)); - return inst; - } - else if (binary->get_type() == BinaryOp::Mod) { - int mask = constant_value - 1; - auto inst = new ir::instruction::BinaryImm(BinaryOp::And, binary->getdst(), binary->getlhs(), ConstValue(mask)); - return inst; - } - } - else if (binary->get_type() == BinaryOp::Sub) { - auto inst = new ir::instruction::BinaryImm(BinaryOp::Add, binary->getdst(), binary->getlhs(), ConstValue(-constant_value)); - return inst; - } - switch (binary->get_type()) { - case BinaryOp::Add: - case BinaryOp::And: - case BinaryOp::Or: - auto inst = new ir::instruction::BinaryImm(binary->get_type(), binary->getdst(), binary->getlhs(), imm); - return inst; - } -======= bool couldDoMul(int x) { if (isPowerOfTwo(x) || isPowerOfTwo(x+1) || isPowerOfTwo(x-1)) return true; @@ -134,7 +96,6 @@ ir::instruction::BinaryImm* convertToBinaryImm(ir::instruction::Binary *binary, } else return nullptr; ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 return nullptr; } @@ -145,7 +106,6 @@ void ConstantPropagation::run() { flag = false; for (auto bb : *func_->get_basic_blocks()) { for (auto & inst : *bb->get_instructions()) { - // std::cout << inst->to_str() << "\n"; if (auto loadimm = dynamic_cast<ir::instruction::LoadImm4 *>(inst)) { auto dst = loadimm->getdst(); if(!constant_map.count(dst->get_index())) flag = true; @@ -178,17 +138,6 @@ void ConstantPropagation::run() { flag = true; delete binary; } -<<<<<<< HEAD - // else if (constant_map.count(rhs->get_index())) { - // auto imm = constant_map[rhs->get_index()]; - // auto binary_imm_instr = convertToBinaryImm(binary, imm); - // if (binary_imm_instr) { - // inst = binary_imm_instr; - // flag = true; - // delete binary; - // } - // } -======= else if (constant_map.count(rhs->get_index())) { auto imm = constant_map[rhs->get_index()]; auto binary_imm_instr = convertToBinaryImm(binary, imm, rhs->get_type().base_type == ScalarType::Int); @@ -198,7 +147,6 @@ void ConstantPropagation::run() { delete binary; } } ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 } else if (auto condbranch = dynamic_cast<ir::instruction::CondBranch *>(inst)) { auto cond = condbranch->getcond(); @@ -217,7 +165,6 @@ void ConstantPropagation::run() { } } } - //std::cout << "returns\n"; } } \ No newline at end of file diff --git a/src/middleend/function_inline.cpp b/src/middleend/function_inline.cpp index 0e5ba4c5c434e9e753a368a3ac8620ce1a2aa761..8e73057f971fdd592e2997e8add355bbf1097f1f 100644 --- a/src/middleend/function_inline.cpp +++ b/src/middleend/function_inline.cpp @@ -203,14 +203,6 @@ void FunctionInline::run() { } } } - // åˆ åŽ»å†…è”函数 - for (auto it = (*module_->get_functions()).begin(); it != (*module_->get_functions()).end(); it++) { - auto func = *it; - if (can_inline_func.find(func->get_name()) != can_inline_func.end()) { - module_->remove_function(func); - it--; - } - } // åˆ é™¤å†…è”ä¹‹åŽæ²¡æœ‰è¢«ç”¨åˆ°è¿‡çš„函数 std::unordered_map<ir::Function *, bool> func_used; diff --git a/src/middleend/ir.hpp b/src/middleend/ir.hpp index 1df727f776550533ebd1be341f705d76ea56d4e8..9231d239968a8ded2cc7e8f73bd2bcfc5fdd98a3 100644 --- a/src/middleend/ir.hpp +++ b/src/middleend/ir.hpp @@ -7,11 +7,8 @@ #include <variant> #include <unordered_set> -<<<<<<< HEAD -======= #include <iostream> #include <unordered_map> ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 #define GetValue(x) (x.type == Int ? x.iv : x.fv) @@ -209,15 +206,8 @@ public: } } inline int get_index() const { return index_; } -<<<<<<< HEAD - int domlevel; - std::unordered_set<ir::BasicBlock *> dom; - std::unordered_set<ir::BasicBlock *> domby; - ir::BasicBlock *idom; -======= Function* get_parent() { return parent_; } void set_parent(Function* parent) { parent_ = parent; } ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 protected: int index_; Function* parent_; @@ -474,11 +464,6 @@ public: if (type_ == BinaryOp::Shl) return dsts_[0]->to_str() + " = " + srcs_[0]->to_str() + " << " + std::to_string(imm_); return "error"; } -<<<<<<< HEAD - inline Temp* getdst() const { return dsts_[0]; } - inline Temp* getlhs() const { return srcs_[0]; } - inline uint32_t getimm() const { return imm_; } -======= inline std::string op_str() { if (type_ == BinaryOp::And) return " & " ; if (type_ == BinaryOp::Or ) return " | " ; @@ -503,7 +488,6 @@ public: inline Temp* getdst() const { return dsts_[0]; } inline Temp* getlhs() const { return srcs_[0]; } inline int32_t getimm() const { return imm_; } ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 inline BinaryOp get_type() const { return type_; } protected: BinaryOp type_; @@ -767,9 +751,6 @@ public: // func_map.emplace(function->get_name(), function); functions_.push_back(function); } - inline void remove_function(Function * function) { - functions_.erase(std::remove(functions_.begin(), functions_.end(), function), functions_.end()); - } inline void add_global_variable(DataMeta * global_variable) { global_variables_.push_back(global_variable); } bool check_global_variables() { for (auto global_variable : global_variables_) { diff --git a/src/middleend/optimizer.cpp b/src/middleend/optimizer.cpp index 1d79428445c750c02835df621ef7dd9fc634149a..3464aceaf984b6aa4e81fc92f9d6c747e4b00583 100644 --- a/src/middleend/optimizer.cpp +++ b/src/middleend/optimizer.cpp @@ -26,9 +26,6 @@ Optimizer::Optimizer(ir::Module *module) { cfg->rebuild(); RemoveRecursiveTailCall rrtc(func); } -<<<<<<< HEAD - //FunctionInline fi(module); -======= FunctionInline fi(module); ValueNumbering vn1(module); for (auto func : *module->get_functions()) { @@ -54,7 +51,6 @@ Optimizer::Optimizer(ir::Module *module) { GlobalCodeMotion gcm(cfg); } AlgebraSimplification as3(module); ->>>>>>> 5530fac6257ec478e5386eb5bd228c0f214c7890 for (auto func : *module->get_functions()) { CFG *cfg = new CFG(func); ConstantPropagation cp(func);