P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
constantFolding.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef COMMON_CONSTANTFOLDING_H_
18#define COMMON_CONSTANTFOLDING_H_
19
20#include "frontends/p4/typeChecking/typeChecker.h"
21#include "ir/ir.h"
22#include "lib/big_int_util.h"
23
24namespace P4 {
25
33 public:
35 virtual const IR::Node *hook(Visitor &, IR::PathExpression *) { return nullptr; }
36};
37
58class DoConstantFolding : public Transform {
59 protected:
61
65
69
72
75
77 std::map<const IR::Declaration_Constant *, const IR::Expression *> constants;
78 // True if we are processing a left side of an assignment; we should not
79 // we substituting constants there.
80 bool assignmentTarget;
81
83 const IR::Expression *getConstant(const IR::Expression *expr) const;
84
86 const IR::Constant *cast(const IR::Constant *node, unsigned base,
87 const IR::Type_Bits *type) const;
88
90 const IR::Node *binary(const IR::Operation_Binary *op,
91 std::function<big_int(big_int, big_int)> func, bool saturating = false);
94 const IR::Node *compare(const IR::Operation_Binary *op);
95
97 const IR::Node *shift(const IR::Operation_Binary *op);
98
100 enum class Result { Yes, No, DontKnow };
101
111 Result setContains(const IR::Expression *keySet, const IR::Expression *constant) const;
112
113 public:
114 DoConstantFolding(const ReferenceMap *refMap, const TypeMap *typeMap, bool warnings = true,
115 ConstantFoldingPolicy *policy = nullptr)
117 if (policy) {
118 this->policy = policy;
119 } else {
120 this->policy = new ConstantFoldingPolicy();
121 }
122 visitDagOnce = true;
123 setName("DoConstantFolding");
124 assignmentTarget = false;
125 }
126
127 const IR::Node *postorder(IR::Declaration_Constant *d) override;
128 const IR::Node *postorder(IR::PathExpression *e) override;
129 const IR::Node *postorder(IR::Cmpl *e) override;
130 const IR::Node *postorder(IR::Neg *e) override;
131 const IR::Node *postorder(IR::UPlus *e) override;
132 const IR::Node *postorder(IR::LNot *e) override;
133 const IR::Node *postorder(IR::LAnd *e) override;
134 const IR::Node *postorder(IR::LOr *e) override;
135 const IR::Node *postorder(IR::Slice *e) override;
136 const IR::Node *postorder(IR::Add *e) override;
137 const IR::Node *postorder(IR::AddSat *e) override;
138 const IR::Node *postorder(IR::Sub *e) override;
139 const IR::Node *postorder(IR::SubSat *e) override;
140 const IR::Node *postorder(IR::Mul *e) override;
141 const IR::Node *postorder(IR::Div *e) override;
142 const IR::Node *postorder(IR::Mod *e) override;
143 const IR::Node *postorder(IR::BXor *e) override;
144 const IR::Node *postorder(IR::BAnd *e) override;
145 const IR::Node *postorder(IR::BOr *e) override;
146 const IR::Node *postorder(IR::Equ *e) override;
147 const IR::Node *postorder(IR::Neq *e) override;
148 const IR::Node *postorder(IR::Lss *e) override;
149 const IR::Node *postorder(IR::Grt *e) override;
150 const IR::Node *postorder(IR::Leq *e) override;
151 const IR::Node *postorder(IR::Geq *e) override;
152 const IR::Node *postorder(IR::Shl *e) override;
153 const IR::Node *postorder(IR::Shr *e) override;
154 const IR::Node *postorder(IR::Concat *e) override;
155 const IR::Node *postorder(IR::Member *e) override;
156 const IR::Node *postorder(IR::Cast *e) override;
157 const IR::Node *postorder(IR::Mux *e) override;
158 const IR::Node *postorder(IR::Type_Bits *type) override;
159 const IR::Node *postorder(IR::Type_Varbits *type) override;
160 const IR::Node *postorder(IR::SelectExpression *e) override;
161 const IR::Node *postorder(IR::IfStatement *statement) override;
162 const IR::Node *preorder(IR::AssignmentStatement *statement) override;
163 const IR::Node *preorder(IR::ArrayIndex *e) override;
164 const IR::BlockStatement *preorder(IR::BlockStatement *bs) override {
165 if (bs->annotations->getSingle("disable_optimization")) prune();
166 return bs;
167 }
168};
169
174class ConstantFolding : public PassManager {
175 public:
177 : ConstantFolding(refMap, typeMap, true, nullptr, policy) {}
178
179 ConstantFolding(ReferenceMap *refMap, TypeMap *typeMap, bool warnings = true,
180 TypeChecking *typeChecking = nullptr, ConstantFoldingPolicy *policy = nullptr) {
181 if (typeMap != nullptr) {
182 if (!typeChecking) typeChecking = new TypeChecking(refMap, typeMap);
183 passes.push_back(typeChecking);
184 }
185 passes.push_back(new DoConstantFolding(refMap, typeMap, warnings, policy));
186 if (typeMap != nullptr) passes.push_back(new ClearTypeMap(typeMap));
187 setName("ConstantFolding");
188 }
189};
190
191} // namespace P4
192
193#endif /* COMMON_CONSTANTFOLDING_H_ */
Definition typeChecker.h:37
Definition constantFolding.h:174
Definition constantFolding.h:32
virtual const IR::Node * hook(Visitor &, IR::PathExpression *)
The default hook does not modify anything.
Definition constantFolding.h:35
statically evaluates many constant expressions.
Definition constantFolding.h:58
const ReferenceMap * refMap
Definition constantFolding.h:64
Result
Result type for setContains.
Definition constantFolding.h:100
const IR::Node * shift(const IR::Operation_Binary *op)
Statically evaluate shift operation e.
Definition constantFolding.cpp:729
std::map< const IR::Declaration_Constant *, const IR::Expression * > constants
Maps declaration constants to constant expressions.
Definition constantFolding.h:77
const IR::Node * binary(const IR::Operation_Binary *op, std::function< big_int(big_int, big_int)> func, bool saturating=false)
Statically evaluate binary operation e implemented by func.
Definition constantFolding.cpp:445
const TypeMap * typeMap
Definition constantFolding.h:68
bool warnings
If true then emit warnings.
Definition constantFolding.h:74
Result setContains(const IR::Expression *keySet, const IR::Expression *constant) const
Definition constantFolding.cpp:857
bool typesKnown
Set to true iff typeMap is not nullptr.
Definition constantFolding.h:71
const IR::Constant * cast(const IR::Constant *node, unsigned base, const IR::Type_Bits *type) const
Statically cast constant node to type represented in the specified base.
Definition constantFolding.cpp:301
const IR::Expression * getConstant(const IR::Expression *expr) const
Definition constantFolding.cpp:53
const IR::Node * compare(const IR::Operation_Binary *op)
Definition constantFolding.cpp:390
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeChecker.h:60
Definition typeMap.h:42
Definition applyOptionsPragmas.cpp:24