From befecb47b00110adc07b1b30148b65362a73459c Mon Sep 17 00:00:00 2001 From: fruffy Date: Sun, 20 Nov 2022 19:06:13 -0500 Subject: [PATCH] Move optimizeExpression out of IR. --- backends/p4tools/common/lib/model.cpp | 1 + backends/p4tools/common/lib/symbolic_env.cpp | 1 + .../exploration_strategy.cpp | 1 + .../core/small_step/abstract_stepper.cpp | 1 + .../testgen/core/small_step/small_step.cpp | 1 + backends/p4tools/testgen/lib/test_backend.cpp | 1 + frontends/p4/optimizeExpressions.h | 24 +++++++++++++++++++ ir/dbprint-expression.cpp | 9 ++++--- ir/declaration.h | 3 --- ir/irutils.h | 13 ---------- ir/visitor.cpp | 2 ++ tools/run-clang-format.sh | 0 12 files changed, 36 insertions(+), 21 deletions(-) create mode 100644 frontends/p4/optimizeExpressions.h delete mode 100755 tools/run-clang-format.sh diff --git a/backends/p4tools/common/lib/model.cpp b/backends/p4tools/common/lib/model.cpp index b05710e5025..322e9f40c20 100644 --- a/backends/p4tools/common/lib/model.cpp +++ b/backends/p4tools/common/lib/model.cpp @@ -8,6 +8,7 @@ #include #include +#include "frontends/p4/optimizeExpressions.h" #include "ir/indexed_vector.h" #include "ir/ir-inline.h" #include "ir/irutils.h" diff --git a/backends/p4tools/common/lib/symbolic_env.cpp b/backends/p4tools/common/lib/symbolic_env.cpp index 7f39ee8c3f7..2270f5e7761 100644 --- a/backends/p4tools/common/lib/symbolic_env.cpp +++ b/backends/p4tools/common/lib/symbolic_env.cpp @@ -8,6 +8,7 @@ #include #include "backends/p4tools/common/lib/zombie.h" +#include "frontends/p4/optimizeExpressions.h" #include "ir/indexed_vector.h" #include "ir/irutils.h" #include "ir/vector.h" diff --git a/backends/p4tools/testgen/core/exploration_strategy/exploration_strategy.cpp b/backends/p4tools/testgen/core/exploration_strategy/exploration_strategy.cpp index d2ce5e0907c..c6ca73750be 100644 --- a/backends/p4tools/testgen/core/exploration_strategy/exploration_strategy.cpp +++ b/backends/p4tools/testgen/core/exploration_strategy/exploration_strategy.cpp @@ -13,6 +13,7 @@ #include "backends/p4tools/common/lib/symbolic_env.h" #include "backends/p4tools/common/lib/timer.h" #include "backends/p4tools/common/lib/util.h" +#include "frontends/p4/optimizeExpressions.h" #include "gsl/gsl-lite.hpp" #include "ir/ir.h" #include "ir/irutils.h" diff --git a/backends/p4tools/testgen/core/small_step/abstract_stepper.cpp b/backends/p4tools/testgen/core/small_step/abstract_stepper.cpp index b60cc2149a4..e05024fbe53 100644 --- a/backends/p4tools/testgen/core/small_step/abstract_stepper.cpp +++ b/backends/p4tools/testgen/core/small_step/abstract_stepper.cpp @@ -13,6 +13,7 @@ #include "backends/p4tools/common/lib/model.h" #include "backends/p4tools/common/lib/symbolic_env.h" #include "backends/p4tools/common/lib/util.h" +#include "frontends/p4/optimizeExpressions.h" #include "ir/dump.h" #include "ir/id.h" #include "ir/indexed_vector.h" diff --git a/backends/p4tools/testgen/core/small_step/small_step.cpp b/backends/p4tools/testgen/core/small_step/small_step.cpp index 546f754c6ac..48d97ba207e 100644 --- a/backends/p4tools/testgen/core/small_step/small_step.cpp +++ b/backends/p4tools/testgen/core/small_step/small_step.cpp @@ -13,6 +13,7 @@ #include "backends/p4tools/common/lib/symbolic_env.h" #include "backends/p4tools/common/lib/trace_events.h" +#include "frontends/p4/optimizeExpressions.h" #include "gsl/gsl-lite.hpp" #include "ir/ir.h" #include "ir/irutils.h" diff --git a/backends/p4tools/testgen/lib/test_backend.cpp b/backends/p4tools/testgen/lib/test_backend.cpp index e9ae19a0ab9..70abebd8489 100644 --- a/backends/p4tools/testgen/lib/test_backend.cpp +++ b/backends/p4tools/testgen/lib/test_backend.cpp @@ -17,6 +17,7 @@ #include "backends/p4tools/common/lib/taint.h" #include "backends/p4tools/common/lib/timer.h" #include "backends/p4tools/common/lib/util.h" +#include "frontends/p4/optimizeExpressions.h" #include "ir/irutils.h" #include "lib/error.h" #include "lib/exceptions.h" diff --git a/frontends/p4/optimizeExpressions.h b/frontends/p4/optimizeExpressions.h new file mode 100644 index 00000000000..e613159d9bc --- /dev/null +++ b/frontends/p4/optimizeExpressions.h @@ -0,0 +1,24 @@ +#ifndef FRONTENDS_P4_OPTIMIZEEXPRESSIONS_H_ +#define FRONTENDS_P4_OPTIMIZEEXPRESSIONS_H_ + +#include "frontends/common/constantFolding.h" +#include "frontends/p4/strengthReduction.h" + +namespace IR { + +/// Applies expression optimizations to the input node. +/// Currently, performs constant folding and strength reduction. +template +const T* optimizeExpression(const T* node) { + auto pass = PassRepeated({ + new P4::StrengthReduction(nullptr, nullptr, nullptr), + new P4::ConstantFolding(nullptr, nullptr, false), + }); + node = node->apply(pass); + BUG_CHECK(::errorCount() == 0, "Encountered errors while trying to optimize expressions."); + return node; +} + +} // namespace IR + +#endif /* FRONTENDS_P4_OPTIMIZEEXPRESSIONS_H_ */ diff --git a/ir/dbprint-expression.cpp b/ir/dbprint-expression.cpp index e673003b183..f82234a8650 100644 --- a/ir/dbprint-expression.cpp +++ b/ir/dbprint-expression.cpp @@ -268,11 +268,10 @@ void IR::ListExpression::dbprint(std::ostream& out) const { if (prec == 0) out << ';'; } -void IR::P4ListExpression::dbprint(std::ostream &out) const { +void IR::P4ListExpression::dbprint(std::ostream& out) const { int prec = getprec(out); if (prec > Prec_Postfix) out << '('; - out << setprec(Prec_Postfix) << "list<" << elementType << - ">{" << setprec(Prec_Low); + out << setprec(Prec_Postfix) << "list<" << elementType << ">{" << setprec(Prec_Low); bool first = true; for (auto a : components) { if (!first) out << ", "; @@ -284,9 +283,9 @@ void IR::P4ListExpression::dbprint(std::ostream &out) const { if (prec == 0) out << ';'; } -void IR::DefaultExpression::dbprint(std::ostream &out) const { out << "default"; } +void IR::DefaultExpression::dbprint(std::ostream& out) const { out << "default"; } -void IR::This::dbprint(std::ostream &out) const { out << "this"; } +void IR::This::dbprint(std::ostream& out) const { out << "this"; } void IR::StringLiteral::dbprint(std::ostream& out) const { out << '"' << value << '"'; } diff --git a/ir/declaration.h b/ir/declaration.h index 44ea09f6d2f..f3e15dca2f1 100644 --- a/ir/declaration.h +++ b/ir/declaration.h @@ -22,9 +22,6 @@ limitations under the License. namespace IR { -// Forward declare ID. -struct ID; - /// The Declaration interface, representing objects with names. class IDeclaration : public virtual INode { public: diff --git a/ir/irutils.h b/ir/irutils.h index 824f42c92f3..5eb22b7f3f2 100644 --- a/ir/irutils.h +++ b/ir/irutils.h @@ -39,19 +39,6 @@ const Constant* getConstant(const Type* type, big_int v); /// @returns a bool literal. The value is cached. const BoolLiteral* getBoolLiteral(bool value); -// /// Applies expression optimizations to the input node. -// /// Currently, performs constant folding and strength reduction. -// template -// const T* optimizeExpression(const T* node) { -// auto pass = PassRepeated({ -// new P4::StrengthReduction(nullptr, nullptr, nullptr), -// new P4::ConstantFolding(nullptr, nullptr, false), -// }); -// node = node->apply(pass); -// BUG_CHECK(::errorCount() == 0, "Encountered errors while trying to optimize expressions."); -// return node; -// } - /// @returns the "default" value for a given type. The current mapping is /// Type_Bits 0 /// Type_Boolean false diff --git a/ir/visitor.cpp b/ir/visitor.cpp index 4b56e650f8a..874b70b3f60 100644 --- a/ir/visitor.cpp +++ b/ir/visitor.cpp @@ -19,7 +19,9 @@ limitations under the License. #include #include +#if HAVE_LIBGC #include +#endif #include "ir/id.h" #include "ir/indexed_vector.h" diff --git a/tools/run-clang-format.sh b/tools/run-clang-format.sh deleted file mode 100755 index e69de29bb2d..00000000000