P4C
The P4 Compiler
Loading...
Searching...
No Matches
functionsInlining.h
1/*
2Copyright 2018 VMware, 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 FRONTENDS_P4_FUNCTIONSINLINING_H_
18#define FRONTENDS_P4_FUNCTIONSINLINING_H_
19
20#include "commonInlining.h"
21#include "frontends/common/resolveReferences/referenceMap.h"
22#include "frontends/common/resolveReferences/resolveReferences.h"
23#include "frontends/p4/typeChecking/typeChecker.h"
24#include "frontends/p4/unusedDeclarations.h"
25#include "ir/ir.h"
26
27namespace P4 {
28using FunctionCallInfo = SimpleCallInfo<IR::Node, IR::Statement>;
29using FunctionsInlineWorkList = SimpleInlineWorkList<FunctionCallInfo>;
30using FunctionsInlineList = SimpleInlineList<IR::Node, FunctionCallInfo, FunctionsInlineWorkList>;
31
33 FunctionsInlineList *toInline; // output
34 P4::TypeMap *typeMap; // input
35
36 public:
38 : toInline(toInline), typeMap(typeMap) {
39 CHECK_NULL(toInline);
40 CHECK_NULL(typeMap);
41 setName("DiscoverFunctionsInlining");
42 }
43 Visitor::profile_t init_apply(const IR::Node *node) override;
44 void postorder(const IR::MethodCallExpression *mce) override;
45};
46
51class FunctionsInliner : public AbstractInliner<FunctionsInlineList, FunctionsInlineWorkList> {
52 std::unique_ptr<MinimalNameGenerator> nameGen;
53
54 // All elements in the replacement map are actually IR::Function objects
55 using ReplacementMap = FunctionsInlineWorkList::ReplacementMap;
57 std::vector<ReplacementMap *> replacementStack;
59 std::unordered_map<const IR::MethodCallExpression *, const IR::Expression *>
60 pendingReplacements;
61
66 const IR::Expression *cloneBody(const IR::IndexedVector<IR::StatOrDecl> &src,
69 const IR::Statement *inlineBefore(const IR::Node *calleeNode,
70 const IR::MethodCallExpression *call,
71 const IR::Statement *before);
72 bool preCaller();
73 const IR::Node *postCaller(const IR::Node *caller);
74 const ReplacementMap *getReplacementMap() const;
75 void dumpReplacementMap() const;
76 class isLocalExpression; // functor to test actual arguments scope use
77
78 public:
79 FunctionsInliner() = default;
80 Visitor::profile_t init_apply(const IR::Node *node) override;
81 void end_apply(const IR::Node *node) override;
82 const IR::Node *preorder(IR::Function *function) override;
83 const IR::Node *preorder(IR::P4Control *control) override;
84 const IR::Node *preorder(IR::P4Parser *parser) override;
85 const IR::Node *preorder(IR::P4Action *action) override;
86 const IR::Node *preorder(IR::MethodCallStatement *statement) override;
87 const IR::Node *preorder(IR::MethodCallExpression *expr) override;
88 const IR::Node *preorder(IR::AssignmentStatement *statement) override;
89};
90
92
96 public:
98 setName("CloneVariableDeclarations");
99 visitDagOnce = false;
100 }
101 const IR::Node *postorder(IR::Declaration_Variable *declaration) override {
102 // this will have a different declid
103 auto result = new IR::Declaration_Variable(declaration->srcInfo, declaration->getName(),
104 declaration->annotations, declaration->type,
105 declaration->initializer);
106 LOG3("Cloned " << dbp(result));
107 return result;
108 }
109};
110
112 FunctionsInlineList functionsToInline;
113
114 public:
115 InlineFunctions(TypeMap *typeMap, const RemoveUnusedPolicy &policy) {
116 passes.push_back(
117 new PassRepeated({new TypeChecking(nullptr, typeMap),
118 new DiscoverFunctionsInlining(&functionsToInline, typeMap),
119 new InlineFunctionsDriver(&functionsToInline, new FunctionsInliner()),
120 new RemoveAllUnusedDeclarations(policy)}));
121 passes.push_back(new CloneVariableDeclarations());
122 setName("InlineFunctions");
123 }
124};
125
126} // namespace P4
127
128#endif /* FRONTENDS_P4_FUNCTIONSINLINING_H_ */
Definition commonInlining.h:157
Definition functionsInlining.h:95
Definition functionsInlining.h:32
Definition functionsInlining.cpp:72
Definition functionsInlining.h:51
Definition node.h:52
Definition node.h:95
Definition commonInlining.h:178
Definition functionsInlining.h:111
Definition visitor.h:400
Definition ir/pass_manager.h:40
Definition ir/pass_manager.h:145
Iterates RemoveUnusedDeclarations until convergence.
Definition unusedDeclarations.h:189
Definition unusedDeclarations.h:48
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition visitor.h:78
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24