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

Add string literal support in constant folding. #4837

Merged
merged 2 commits into from
Jul 29, 2024
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
31 changes: 23 additions & 8 deletions frontends/common/constantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/p4/enumInstance.h"
#include "lib/big_int_util.h"
#include "lib/exceptions.h"
#include "lib/log.h"

namespace P4 {
Expand Down Expand Up @@ -55,6 +56,7 @@ const IR::Expression *DoConstantFolding::getConstant(const IR::Expression *expr)
CHECK_NULL(expr);
if (expr->is<IR::Constant>()) return expr;
if (expr->is<IR::BoolLiteral>()) return expr;
if (expr->is<IR::StringLiteral>()) return expr;
if (auto list = expr->to<IR::ListExpression>()) {
for (auto e : list->components)
if (getConstant(e) == nullptr) return nullptr;
Expand Down Expand Up @@ -426,21 +428,34 @@ const IR::Node *DoConstantFolding::postorder(IR::Shr *e) { return shift(e); }
const IR::Node *DoConstantFolding::postorder(IR::Shl *e) { return shift(e); }

const IR::Node *DoConstantFolding::compare(const IR::Operation_Binary *e) {
auto eleft = getConstant(e->left);
auto eright = getConstant(e->right);
if (eleft == nullptr || eright == nullptr) return e;
const auto *eleft = getConstant(e->left);
const auto *eright = getConstant(e->right);
if (eleft == nullptr || eright == nullptr) {
return e;
}

bool eqTest = e->is<IR::Equ>();
if (eleft->is<IR::BoolLiteral>()) {
auto left = eleft->to<IR::BoolLiteral>();
auto right = eright->to<IR::BoolLiteral>();
if (left == nullptr || right == nullptr) {
BUG_CHECK(eqTest || e->is<IR::Neq>(),
"compare only supports IR::Eq or IR::Neq operators, got %1%", e->node_type_name());
if (const auto *left = eleft->to<IR::BoolLiteral>()) {
const auto *right = eright->to<IR::BoolLiteral>();
if (right == nullptr) {
::error(ErrorType::ERR_INVALID, "%1%: both operands must be Boolean", e);
return e;
}
bool bresult = (left->value == right->value) == eqTest;
return new IR::BoolLiteral(e->srcInfo, IR::Type_Boolean::get(), bresult);
} else if (typesKnown) {
}
if (const auto *left = eleft->to<IR::StringLiteral>()) {
const auto *right = eright->to<IR::StringLiteral>();
if (right == nullptr) {
::error(ErrorType::ERR_INVALID, "%1%: both operands must be String", e);
return e;
}
bool bresult = (left->value == right->value) == eqTest;
return new IR::BoolLiteral(e->srcInfo, IR::Type_Boolean::get(), bresult);
}
if (typesKnown) {
auto le = EnumInstance::resolve(eleft, typeMap);
auto re = EnumInstance::resolve(eright, typeMap);
if (le != nullptr && re != nullptr) {
Expand Down
Loading