From f0f0bb815497bddf0fed4202f4eecfcaa4f3b646 Mon Sep 17 00:00:00 2001 From: LiXiaoQi Date: Tue, 29 Nov 2022 19:41:50 +0800 Subject: [PATCH] get 4.2 files --- README.md | 1 + src/optimization/GVN.cpp | 96 ++++++++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index aed566c..cd2bcdb 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ * [lab4.2](./Documentations/4.2-gvn/) + DDL: 2022-12-12 23:59:59 (UTC+8) + - [report](./Reports/4.2-gvn/report.md) ## FAQ: How to merge upstream remote branches diff --git a/src/optimization/GVN.cpp b/src/optimization/GVN.cpp index e10dda3..8c4ce9b 100644 --- a/src/optimization/GVN.cpp +++ b/src/optimization/GVN.cpp @@ -28,44 +28,69 @@ static auto get_const_fp_value = [](Value *v) { return dynamic_castget_instr_type(); switch (op) { - case Instruction::add: return ConstantInt::get(get_const_int_value(value1) + get_const_int_value(value2), module_); - case Instruction::sub: return ConstantInt::get(get_const_int_value(value1) - get_const_int_value(value2), module_); - case Instruction::mul: return ConstantInt::get(get_const_int_value(value1) * get_const_int_value(value2), module_); - case Instruction::sdiv: return ConstantInt::get(get_const_int_value(value1) / get_const_int_value(value2), module_); - case Instruction::fadd: return ConstantFP::get(get_const_fp_value(value1) + get_const_fp_value(value2), module_); - case Instruction::fsub: return ConstantFP::get(get_const_fp_value(value1) - get_const_fp_value(value2), module_); - case Instruction::fmul: return ConstantFP::get(get_const_fp_value(value1) * get_const_fp_value(value2), module_); - case Instruction::fdiv: return ConstantFP::get(get_const_fp_value(value1) / get_const_fp_value(value2), module_); + case Instruction::add: + return ConstantInt::get(get_const_int_value(value1) + get_const_int_value(value2), module_); + case Instruction::sub: + return ConstantInt::get(get_const_int_value(value1) - get_const_int_value(value2), module_); + case Instruction::mul: + return ConstantInt::get(get_const_int_value(value1) * get_const_int_value(value2), module_); + case Instruction::sdiv: + return ConstantInt::get(get_const_int_value(value1) / get_const_int_value(value2), module_); + case Instruction::fadd: + return ConstantFP::get(get_const_fp_value(value1) + get_const_fp_value(value2), module_); + case Instruction::fsub: + return ConstantFP::get(get_const_fp_value(value1) - get_const_fp_value(value2), module_); + case Instruction::fmul: + return ConstantFP::get(get_const_fp_value(value1) * get_const_fp_value(value2), module_); + case Instruction::fdiv: + return ConstantFP::get(get_const_fp_value(value1) / get_const_fp_value(value2), module_); - case Instruction::cmp: - switch (dynamic_cast(instr)->get_cmp_op()) { - case CmpInst::EQ: return ConstantInt::get(get_const_int_value(value1) == get_const_int_value(value2), module_); - case CmpInst::NE: return ConstantInt::get(get_const_int_value(value1) != get_const_int_value(value2), module_); - case CmpInst::GT: return ConstantInt::get(get_const_int_value(value1) > get_const_int_value(value2), module_); - case CmpInst::GE: return ConstantInt::get(get_const_int_value(value1) >= get_const_int_value(value2), module_); - case CmpInst::LT: return ConstantInt::get(get_const_int_value(value1) < get_const_int_value(value2), module_); - case CmpInst::LE: return ConstantInt::get(get_const_int_value(value1) <= get_const_int_value(value2), module_); - } - case Instruction::fcmp: - switch (dynamic_cast(instr)->get_cmp_op()) { - case FCmpInst::EQ: return ConstantInt::get(get_const_fp_value(value1) == get_const_fp_value(value2), module_); - case FCmpInst::NE: return ConstantInt::get(get_const_fp_value(value1) != get_const_fp_value(value2), module_); - case FCmpInst::GT: return ConstantInt::get(get_const_fp_value(value1) > get_const_fp_value(value2), module_); - case FCmpInst::GE: return ConstantInt::get(get_const_fp_value(value1) >= get_const_fp_value(value2), module_); - case FCmpInst::LT: return ConstantInt::get(get_const_fp_value(value1) < get_const_fp_value(value2), module_); - case FCmpInst::LE: return ConstantInt::get(get_const_fp_value(value1) <= get_const_fp_value(value2), module_); - } - default: return nullptr; + case Instruction::cmp: + switch (dynamic_cast(instr)->get_cmp_op()) { + case CmpInst::EQ: + return ConstantInt::get(get_const_int_value(value1) == get_const_int_value(value2), module_); + case CmpInst::NE: + return ConstantInt::get(get_const_int_value(value1) != get_const_int_value(value2), module_); + case CmpInst::GT: + return ConstantInt::get(get_const_int_value(value1) > get_const_int_value(value2), module_); + case CmpInst::GE: + return ConstantInt::get(get_const_int_value(value1) >= get_const_int_value(value2), module_); + case CmpInst::LT: + return ConstantInt::get(get_const_int_value(value1) < get_const_int_value(value2), module_); + case CmpInst::LE: + return ConstantInt::get(get_const_int_value(value1) <= get_const_int_value(value2), module_); + } + case Instruction::fcmp: + switch (dynamic_cast(instr)->get_cmp_op()) { + case FCmpInst::EQ: + return ConstantInt::get(get_const_fp_value(value1) == get_const_fp_value(value2), module_); + case FCmpInst::NE: + return ConstantInt::get(get_const_fp_value(value1) != get_const_fp_value(value2), module_); + case FCmpInst::GT: + return ConstantInt::get(get_const_fp_value(value1) > get_const_fp_value(value2), module_); + case FCmpInst::GE: + return ConstantInt::get(get_const_fp_value(value1) >= get_const_fp_value(value2), module_); + case FCmpInst::LT: + return ConstantInt::get(get_const_fp_value(value1) < get_const_fp_value(value2), module_); + case FCmpInst::LE: + return ConstantInt::get(get_const_fp_value(value1) <= get_const_fp_value(value2), module_); + } + default: + return nullptr; } } Constant *ConstFolder::compute(Instruction *instr, Constant *value1) { auto op = instr->get_instr_type(); switch (op) { - case Instruction::sitofp: return ConstantFP::get((float)get_const_int_value(value1), module_); - case Instruction::fptosi: return ConstantInt::get((int)get_const_fp_value(value1), module_); - case Instruction::zext: return ConstantInt::get((int)get_const_int_value(value1), module_); - default: return nullptr; + case Instruction::sitofp: + return ConstantFP::get((float)get_const_int_value(value1), module_); + case Instruction::fptosi: + return ConstantInt::get((int)get_const_fp_value(value1), module_); + case Instruction::zext: + return ConstantInt::get((int)get_const_int_value(value1), module_); + default: + return nullptr; } } @@ -275,9 +300,12 @@ bool GVNExpression::operator==(const Expression &lhs, const Expression &rhs) { if (lhs.get_expr_type() != rhs.get_expr_type()) return false; switch (lhs.get_expr_type()) { - case Expression::e_constant: return equiv_as(lhs, rhs); - case Expression::e_bin: return equiv_as(lhs, rhs); - case Expression::e_phi: return equiv_as(lhs, rhs); + case Expression::e_constant: + return equiv_as(lhs, rhs); + case Expression::e_bin: + return equiv_as(lhs, rhs); + case Expression::e_phi: + return equiv_as(lhs, rhs); } } -- GitLab