Skip to content

Commit

Permalink
Move optimizeExpression out of IR.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed Nov 22, 2022
1 parent b8f6ece commit befecb4
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 21 deletions.
1 change: 1 addition & 0 deletions backends/p4tools/common/lib/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <boost/container/vector.hpp>
#include <boost/format.hpp>

#include "frontends/p4/optimizeExpressions.h"
#include "ir/indexed_vector.h"
#include "ir/ir-inline.h"
#include "ir/irutils.h"
Expand Down
1 change: 1 addition & 0 deletions backends/p4tools/common/lib/symbolic_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <boost/container/vector.hpp>

#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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions backends/p4tools/testgen/core/small_step/small_step.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions backends/p4tools/testgen/lib/test_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions frontends/p4/optimizeExpressions.h
Original file line number Diff line number Diff line change
@@ -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 <class T>
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_ */
9 changes: 4 additions & 5 deletions ir/dbprint-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << ", ";
Expand All @@ -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 << '"'; }

Expand Down
3 changes: 0 additions & 3 deletions ir/declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 0 additions & 13 deletions ir/irutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class T>
// 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
Expand Down
2 changes: 2 additions & 0 deletions ir/visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ limitations under the License.
#include <stdlib.h>
#include <time.h>

#if HAVE_LIBGC
#include <gc/gc.h>
#endif

#include "ir/id.h"
#include "ir/indexed_vector.h"
Expand Down
Empty file removed tools/run-clang-format.sh
Empty file.

0 comments on commit befecb4

Please sign in to comment.