P4C
The P4 Compiler
Loading...
Searching...
No Matches
strengthReduction.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 P4_STRENGTHREDUCTION_H_
18#define P4_STRENGTHREDUCTION_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/p4/sideEffects.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "frontends/p4/typeMap.h"
24#include "ir/ir.h"
25
26namespace P4 {
27
28using namespace literals;
29
47class DoStrengthReduction final : public Transform {
48 protected:
52
54 bool isOne(const IR::Expression *expr) const;
56 bool isZero(const IR::Expression *expr) const;
58 bool isTrue(const IR::Expression *expr) const;
60 bool isFalse(const IR::Expression *expr) const;
62 bool isAllOnes(const IR::Expression *expr) const;
65 int isPowerOf2(const IR::Expression *expr) const;
66
70 bool hasSideEffects(const IR::Expression *expr) const {
71 return SideEffects::check(expr, this, nullptr, nullptr);
72 }
73
74 public:
76 visitDagOnce = true;
77 setName("StrengthReduction");
78 }
79
80 explicit DoStrengthReduction(bool enableSubConstToAddTransform)
82 // FIXME: This does not call a constructor
83 DoStrengthReduction();
84 }
85
86 using Transform::postorder;
87
88 const IR::Node *postorder(IR::Cmpl *expr) override;
89 const IR::Node *postorder(IR::BAnd *expr) override;
90 const IR::Node *postorder(IR::BOr *expr) override;
91 const IR::Node *postorder(IR::Equ *expr) override;
92 const IR::Node *postorder(IR::Neq *expr) override;
93 const IR::Node *postorder(IR::BXor *expr) override;
94 const IR::Node *postorder(IR::LAnd *expr) override;
95 const IR::Node *postorder(IR::LOr *expr) override;
96 const IR::Node *postorder(IR::LNot *expr) override;
97 const IR::Node *postorder(IR::Sub *expr) override;
98 const IR::Node *postorder(IR::Add *expr) override;
99 const IR::Node *postorder(IR::UPlus *expr) override;
100 const IR::Node *postorder(IR::Shl *expr) override;
101 const IR::Node *postorder(IR::Shr *expr) override;
102 const IR::Node *postorder(IR::Mul *expr) override;
103 const IR::Node *postorder(IR::Div *expr) override;
104 const IR::Node *postorder(IR::Mod *expr) override;
105 const IR::Node *postorder(IR::Mux *expr) override;
106 const IR::Node *postorder(IR::Slice *expr) override;
107 const IR::Node *postorder(IR::PlusSlice *expr) override;
108 const IR::Node *postorder(IR::Mask *expr) override;
109 const IR::Node *postorder(IR::Range *expr) override;
110 const IR::Node *postorder(IR::Concat *expr) override;
111 const IR::Node *postorder(IR::ArrayIndex *expr) override;
112
113 const IR::BlockStatement *preorder(IR::BlockStatement *bs) override {
114 // FIXME: Do we need to check for expression, so we'd be able to fine tune, e.g.
115 // @disable_optimization("strength_reduce")
116 if (bs->hasAnnotation(IR::Annotation::disableOptimizationAnnotation)) prune();
117 return bs;
118 }
119};
120
122 public:
123 explicit StrengthReduction(TypeMap *typeMap, TypeChecking *typeChecking = nullptr,
124 bool enableSubConstToAddTransform = true) {
125 if (typeMap != nullptr) {
126 if (!typeChecking) typeChecking = new TypeChecking(nullptr, typeMap, true);
127 passes.push_back(typeChecking);
128 }
129 passes.push_back(new DoStrengthReduction(enableSubConstToAddTransform));
130 }
131
132 explicit StrengthReduction(TypeMap *typeMap, bool enableSubConstToAddTransform)
133 : StrengthReduction(typeMap, nullptr, enableSubConstToAddTransform) {}
134};
135
136} // namespace P4
137
138#endif /* P4_STRENGTHREDUCTION_H_ */
Definition strengthReduction.h:47
const IR::Node * postorder(IR::Cmpl *expr) override
Definition strengthReduction.cpp:71
bool isZero(const IR::Expression *expr) const
Definition strengthReduction.cpp:29
bool isFalse(const IR::Expression *expr) const
Definition strengthReduction.cpp:44
bool isAllOnes(const IR::Expression *expr) const
Definition strengthReduction.cpp:60
bool isTrue(const IR::Expression *expr) const
Definition strengthReduction.cpp:38
bool isOne(const IR::Expression *expr) const
Definition strengthReduction.cpp:23
bool hasSideEffects(const IR::Expression *expr) const
Definition strengthReduction.h:70
int isPowerOf2(const IR::Expression *expr) const
Definition strengthReduction.cpp:50
bool enableSubConstToAddTransform
Definition strengthReduction.h:51
Definition ir/pass_manager.h:40
static bool check(const IR::Expression *expression, const Visitor *calledBy, DeclarationLookup *refMap, TypeMap *typeMap, const Visitor::Context *ctxt=nullptr)
Definition sideEffects.h:106
Definition strengthReduction.h:121
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24