-
Notifications
You must be signed in to change notification settings - Fork 449
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
Constant-fold isValid calls when possible #3794
Conversation
auto ne = si->components.getDeclaration<IR::NamedExpression>(e->member.name); | ||
BUG_CHECK(ne != nullptr, "Could not find field %1% in initializer %2%", e->member, si); | ||
return CloneConstants::clone(ne->expression, this); | ||
} else if (expr->is<IR::InvalidHeader>() && e->member.name == IR::Type_Header::isValid) { | ||
return e; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "else if" conditions are unnecessary because return
would prevent the execution of subsequent statements. Perhaps we can simplify this method to something like:
const IR::Node* DoConstantFolding::postorder(IR::Member* e) {
if (!typesKnown) return e;
auto orig = getOriginal<IR::Member>();
auto type = typeMap->getType(orig->expr, true);
auto origtype = typeMap->getType(orig);
if (type->is<IR::Type_Stack>() && e->member == IR::Type_Stack::arraySize) {
auto st = type->to<IR::Type_Stack>();
auto size = st->getSize();
return new IR::Constant(st->size->srcInfo, origtype, size);
}
auto expr = getConstant(e->expr);
if (expr == nullptr) return e;
auto structType = type->to<IR::Type_StructLike>();
if (structType == nullptr) BUG("Expected a struct type, got %1%", type);
if (auto list = expr->to<IR::ListExpression>()) {
bool found = false;
int index = 0;
for (auto f : structType->fields) {
if (f->name.name == e->member.name) {
found = true;
break;
}
index++;
}
if (!found) BUG("Could not find field %1% in type %2%", e->member, type);
return CloneConstants::clone(list->components.at(index), this);
}
if (auto si = expr->to<IR::StructExpression>()) {
if (type->is<IR::Type_Header>() && e->member.name == IR::Type_Header::isValid) return e;
auto ne = si->components.getDeclaration<IR::NamedExpression>(e->member.name);
BUG_CHECK(ne != nullptr, "Could not find field %1% in initializer %2%", e->member, si);
return CloneConstants::clone(ne->expression, this);
}
if (expr->is<IR::InvalidHeader>() && e->member.name == IR::Type_Header::isValid) {
return e;
}
BUG("Unexpected initializer: %1%", expr);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. As usual I am only reviewing the test program and its expected outputs, not the code changes within p4c.
Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
975f87f
to
1af4be1
Compare
Thank you @rst0git, I will leave such improvements for a future PR. |
Signed-off-by: Mihai Budiu mbudiu@vmware.com
Fixes #3779