Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve type information in constant folding #3094

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions frontends/common/constantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ const IR::Node* DoConstantFolding::postorder(IR::Declaration_Constant* d) {
} else {
// Destination type is InfInt; we must "erase" the width of the source
if (!cst->type->is<IR::Type_InfInt>()) {
init = new IR::Constant(cst->srcInfo, cst->value, cst->base);
init = new IR::Constant(cst->srcInfo, new IR::Type_InfInt(), cst->value,
cst->base);
}
}
}
Expand Down Expand Up @@ -403,15 +404,15 @@ DoConstantFolding::compare(const IR::Operation_Binary* e) {
return e;
}
bool bresult = (left->value == right->value) == eqTest;
return new IR::BoolLiteral(e->srcInfo, bresult);
return new IR::BoolLiteral(e->srcInfo, IR::Type_Boolean::get(), bresult);
} else if (typesKnown) {
auto le = EnumInstance::resolve(eleft, typeMap);
auto re = EnumInstance::resolve(eright, typeMap);
if (le != nullptr && re != nullptr) {
BUG_CHECK(le->type == re->type,
"%1%: different enum types in comparison", e);
bool bresult = (le->name == re->name) == eqTest;
return new IR::BoolLiteral(e->srcInfo, bresult);
return new IR::BoolLiteral(e->srcInfo, IR::Type_Boolean::get(), bresult);
}

auto llist = eleft->to<IR::ListExpression>();
Expand All @@ -437,7 +438,7 @@ DoConstantFolding::compare(const IR::Operation_Binary* e) {
if (boolLit->value != eqTest)
return boolLit;
}
return new IR::BoolLiteral(e->srcInfo, eqTest);
return new IR::BoolLiteral(e->srcInfo, IR::Type_Boolean::get(), eqTest);
}
}

Expand Down Expand Up @@ -533,7 +534,7 @@ DoConstantFolding::binary(const IR::Operation_Binary* e,
}

if (e->is<IR::Operation_Relation>())
return new IR::BoolLiteral(e->srcInfo, value != 0);
return new IR::BoolLiteral(e->srcInfo, IR::Type_Boolean::get(), value != 0);
else
return new IR::Constant(e->srcInfo, resultType, value, left->base, true);
}
Expand All @@ -551,7 +552,7 @@ const IR::Node* DoConstantFolding::postorder(IR::LAnd* e) {
if (lcst->value) {
return e->right;
}
return new IR::BoolLiteral(left->srcInfo, false);
return new IR::BoolLiteral(left->srcInfo, IR::Type_Boolean::get(), false);
}

const IR::Node* DoConstantFolding::postorder(IR::LOr* e) {
Expand All @@ -567,7 +568,7 @@ const IR::Node* DoConstantFolding::postorder(IR::LOr* e) {
if (!lcst->value) {
return e->right;
}
return new IR::BoolLiteral(left->srcInfo, true);
return new IR::BoolLiteral(left->srcInfo, IR::Type_Boolean::get(), true);
}

static bool overflowWidth(const IR::Node* node, int width) {
Expand Down Expand Up @@ -721,7 +722,7 @@ const IR::Node* DoConstantFolding::postorder(IR::LNot* e) {
::error(ErrorType::ERR_EXPECTED, "%1%: Expected a boolean value", op);
return e;
}
return new IR::BoolLiteral(cst->srcInfo, !cst->value);
return new IR::BoolLiteral(cst->srcInfo, IR::Type_Boolean::get(), !cst->value);
}

const IR::Node* DoConstantFolding::postorder(IR::Mux* e) {
Expand Down Expand Up @@ -841,7 +842,7 @@ const IR::Node *DoConstantFolding::postorder(IR::Cast *e) {
::error(ErrorType::ERR_INVALID, "%1%: Only 0 and 1 can be cast to booleans", e);
return e;
}
return new IR::BoolLiteral(e->srcInfo, v == 1);
return new IR::BoolLiteral(e->srcInfo, IR::Type_Boolean::get(), v == 1);
}
} else if (etype->is<IR::Type_StructLike>()) {
return CloneConstants::clone(expr, this);
Expand Down