Commit f0f0bb81 authored by 李晓奇's avatar 李晓奇

get 4.2 files

parent 86cd4103
......@@ -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
......
......@@ -28,44 +28,69 @@ static auto get_const_fp_value = [](Value *v) { return dynamic_cast<ConstantFP *
Constant *ConstFolder::compute(Instruction *instr, Constant *value1, Constant *value2) {
auto op = instr->get_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<CmpInst *>(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<FCmpInst *>(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<CmpInst *>(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<FCmpInst *>(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<ConstantExpression>(lhs, rhs);
case Expression::e_bin: return equiv_as<BinaryExpression>(lhs, rhs);
case Expression::e_phi: return equiv_as<PhiExpression>(lhs, rhs);
case Expression::e_constant:
return equiv_as<ConstantExpression>(lhs, rhs);
case Expression::e_bin:
return equiv_as<BinaryExpression>(lhs, rhs);
case Expression::e_phi:
return equiv_as<PhiExpression>(lhs, rhs);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment