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
77 public:
78 FunctionsInliner() = default;
79 Visitor::profile_t init_apply(const IR::Node *node) override;
80 void end_apply(const IR::Node *node) override;
81 const IR::Node *preorder(IR::Function *function) override;
82 const IR::Node *preorder(IR::P4Control *control) override;
83 const IR::Node *preorder(IR::P4Parser *parser) override;
84 const IR::Node *preorder(IR::P4Action *action) override;
85 const IR::Node *preorder(IR::MethodCallStatement *statement) override;
86 const IR::Node *preorder(IR::MethodCallExpression *expr) override;
87 const IR::Node *preorder(IR::AssignmentStatement *statement) override;
88};
89
91
95 public:
97 setName("CloneVariableDeclarations");
98 visitDagOnce = false;
99 }
100 const IR::Node *postorder(IR::Declaration_Variable *declaration) override {
101 // this will have a different declid
102 auto result = new IR::Declaration_Variable(declaration->srcInfo, declaration->getName(),
103 declaration->annotations, declaration->type,
104 declaration->initializer);
105 LOG3("Cloned " << dbp(result));
106 return result;
107 }
108};
109
111 FunctionsInlineList functionsToInline;
112
113 public:
114 InlineFunctions(ReferenceMap *refMap, TypeMap *typeMap, const RemoveUnusedPolicy &policy) {
115 passes.push_back(new PassRepeated(
116 {new TypeChecking(nullptr, typeMap),
117 new DiscoverFunctionsInlining(&functionsToInline, typeMap),
118 new InlineFunctionsDriver(&functionsToInline, new FunctionsInliner()),
119 new ResolveReferences(refMap), new RemoveAllUnusedDeclarations(refMap, policy)}));
120 passes.push_back(new CloneVariableDeclarations());
121 setName("InlineFunctions");
122 }
123};
124
125} // namespace P4
126
127#endif /* FRONTENDS_P4_FUNCTIONSINLINING_H_ */
Definition commonInlining.h:157
Definition functionsInlining.h:94
Definition functionsInlining.h:32
Definition functionsInlining.h:51
Definition node.h:52
Definition node.h:95
Definition commonInlining.h:178
Definition functionsInlining.h:110
Definition visitor.h:400
Definition pass_manager.h:40
Definition pass_manager.h:145
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Iterates RemoveUnusedDeclarations until convergence.
Definition unusedDeclarations.h:146
Definition unusedDeclarations.h:28
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:33
Definition resolveReferences.h:121
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