P4C
The P4 Compiler
Loading...
Searching...
No Matches
constantFolding.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_COMMON_CONSTANTFOLDING_H_
9#define FRONTENDS_COMMON_CONSTANTFOLDING_H_
10
11#include "frontends/common/resolveReferences/referenceMap.h"
12#include "frontends/common/resolveReferences/resolveReferences.h"
13#include "frontends/p4/typeChecking/typeChecker.h"
14#include "ir/ir.h"
15
16namespace P4 {
17
18using namespace literals;
19
27 public:
28 virtual ~ConstantFoldingPolicy() = default;
30 virtual const IR::Node *hook(Visitor &, IR::PathExpression *) { return nullptr; }
31};
32
53class DoConstantFolding : public Transform, public ResolutionContext {
54 protected:
55 std::shared_ptr<ConstantFoldingPolicy> defaultPolicy;
57
61
65
68
71
73 std::map<const IR::Declaration_Constant *, const IR::Expression *> constants;
74 // True if we are processing a left side of an assignment; we should not
75 // we substituting constants there.
76 bool assignmentTarget;
77
80 const IR::Type *resolveType(const IR::Type *t);
81
83 const IR::Expression *getConstant(const IR::Expression *expr) const;
84
86 const IR::Constant *cast(const IR::Constant *node, unsigned base, const IR::Type_Bits *type,
87 bool noWarning = false) 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:
115 ConstantFoldingPolicy *policy = nullptr)
116 : defaultPolicy(policy ? nullptr : std::make_shared<ConstantFoldingPolicy>()),
117 policy(policy ? policy : defaultPolicy.get()),
118 refMap(refMap),
120 typesKnown(typeMap != nullptr),
122 visitDagOnce = true;
123 setName("DoConstantFolding");
124 assignmentTarget = false;
125 }
126
127 // If DeclarationLookup is not passed, then resolve by our own. We might
128 // need proper context for this
129 explicit DoConstantFolding(const TypeMap *typeMap, bool warnings = true,
130 ConstantFoldingPolicy *policy = nullptr)
131 : DoConstantFolding(this, typeMap, warnings, policy) {}
132
133 DoConstantFolding() : DoConstantFolding(nullptr, nullptr) {}
134
135 const IR::Node *postorder(IR::Declaration_Constant *d) override;
136 const IR::Node *postorder(IR::PathExpression *e) override;
137 const IR::Node *postorder(IR::Cmpl *e) override;
138 const IR::Node *postorder(IR::Neg *e) override;
139 const IR::Node *postorder(IR::UPlus *e) override;
140 const IR::Node *postorder(IR::LNot *e) override;
141 const IR::Node *postorder(IR::LAnd *e) override;
142 const IR::Node *postorder(IR::LOr *e) override;
143 const IR::Node *postorder(IR::Slice *e) override;
144 const IR::Node *postorder(IR::PlusSlice *e) override;
145 const IR::Node *postorder(IR::Add *e) override;
146 const IR::Node *postorder(IR::AddSat *e) override;
147 const IR::Node *postorder(IR::Sub *e) override;
148 const IR::Node *postorder(IR::SubSat *e) override;
149 const IR::Node *postorder(IR::Mul *e) override;
150 const IR::Node *postorder(IR::Div *e) override;
151 const IR::Node *postorder(IR::Mod *e) override;
152 const IR::Node *postorder(IR::BXor *e) override;
153 const IR::Node *postorder(IR::BAnd *e) override;
154 const IR::Node *postorder(IR::BOr *e) override;
155 const IR::Node *postorder(IR::Equ *e) override;
156 const IR::Node *postorder(IR::Neq *e) override;
157 const IR::Node *postorder(IR::Lss *e) override;
158 const IR::Node *postorder(IR::Grt *e) override;
159 const IR::Node *postorder(IR::Leq *e) override;
160 const IR::Node *postorder(IR::Geq *e) override;
161 const IR::Node *postorder(IR::Shl *e) override;
162 const IR::Node *postorder(IR::Shr *e) override;
163 const IR::Node *postorder(IR::Concat *e) override;
164 const IR::Node *postorder(IR::Member *e) override;
165 const IR::Node *postorder(IR::Cast *e) override;
166 const IR::Node *postorder(IR::Mux *e) override;
167 const IR::Node *postorder(IR::Type_Bits *type) override;
168 const IR::Node *postorder(IR::Type_Varbits *type) override;
169 const IR::Node *postorder(IR::SelectExpression *e) override;
170 const IR::Node *postorder(IR::IfStatement *statement) override;
171 const IR::Node *preorder(IR::BaseAssignmentStatement *statement) override;
172 const IR::Node *preorder(IR::ArrayIndex *e) override;
173 const IR::Node *preorder(IR::SwitchCase *c) override;
174 const IR::BlockStatement *preorder(IR::BlockStatement *bs) override {
175 if (bs->hasAnnotation(IR::Annotation::disableOptimizationAnnotation)) prune();
176 return bs;
177 }
178};
179
184class ConstantFolding : public PassManager {
185 public:
186 ConstantFolding(TypeMap *typeMap, ConstantFoldingPolicy *policy)
187 : ConstantFolding(typeMap, true, nullptr, policy) {}
188
189 explicit ConstantFolding(ConstantFoldingPolicy *policy)
190 : ConstantFolding(nullptr, true, nullptr, policy) {}
191
192 explicit ConstantFolding(TypeMap *typeMap, bool warnings = true,
193 TypeChecking *typeChecking = nullptr,
194 ConstantFoldingPolicy *policy = nullptr) {
195 if (typeMap != nullptr) {
196 if (!typeChecking) typeChecking = new TypeChecking(nullptr, typeMap);
197 passes.push_back(typeChecking);
198 }
199 passes.push_back(new DoConstantFolding(typeMap, warnings, policy));
200 if (typeMap != nullptr) passes.push_back(new ClearTypeMap(typeMap));
201 setName("ConstantFolding");
202 }
203};
204
205} // namespace P4
206
207#endif /* FRONTENDS_COMMON_CONSTANTFOLDING_H_ */
Definition typeChecker.h:23
Definition constantFolding.h:26
virtual const IR::Node * hook(Visitor &, IR::PathExpression *)
The default hook does not modify anything.
Definition constantFolding.h:30
Definition referenceMap.h:48
statically evaluates many constant expressions.
Definition constantFolding.h:53
Result
Result type for setContains.
Definition constantFolding.h:100
const DeclarationLookup * refMap
Definition constantFolding.h:60
const IR::Node * shift(const IR::Operation_Binary *op)
Statically evaluate shift operation e.
Definition constantFolding.cpp:855
std::map< const IR::Declaration_Constant *, const IR::Expression * > constants
Maps declaration constants to constant expressions.
Definition constantFolding.h:73
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:508
const TypeMap * typeMap
Definition constantFolding.h:64
bool warnings
If true then emit warnings.
Definition constantFolding.h:70
Result setContains(const IR::Expression *keySet, const IR::Expression *constant) const
Definition constantFolding.cpp:1003
bool typesKnown
Set to true iff typeMap is not nullptr.
Definition constantFolding.h:67
const IR::Type * resolveType(const IR::Type *t)
Definition constantFolding.cpp:44
const IR::Constant * cast(const IR::Constant *node, unsigned base, const IR::Type_Bits *type, bool noWarning=false) const
Statically cast constant node to type represented in the specified base.
Definition constantFolding.cpp:349
const IR::Expression * getConstant(const IR::Expression *expr) const
Definition constantFolding.cpp:57
const IR::Node * compare(const IR::Operation_Binary *op)
Definition constantFolding.cpp:440
Definition node.h:44
Definition visitor.h:433
Definition typeChecker.h:46
Definition typeMap.h:32
Definition visitor.h:66
Definition typeMap.h:32
Definition cstring.h:71
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
STL namespace.