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>;
31
32class DiscoverFunctionsInlining : public Inspector, public ResolutionContext {
33 FunctionsInlineList *toInline; // output
34 P4::TypeMap *typeMap; // input
35
36 public:
37 DiscoverFunctionsInlining(FunctionsInlineList *toInline, P4::TypeMap *typeMap)
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 const IR::Node *preorder(IR::IfStatement *statement) override;
90};
91
93
96class CloneVariableDeclarations : public Transform {
97 public:
98 CloneVariableDeclarations() {
99 setName("CloneVariableDeclarations");
100 visitDagOnce = false;
101 }
102 const IR::Node *postorder(IR::Declaration_Variable *declaration) override {
103 // this will have a different declid
104 auto result = new IR::Declaration_Variable(declaration->srcInfo, declaration->getName(),
105 declaration->annotations, declaration->type,
106 declaration->initializer);
107 LOG3("Cloned " << dbp(result));
108 return result;
109 }
110};
111
112class InlineFunctions : public PassManager {
113 FunctionsInlineList functionsToInline;
114
115 public:
116 InlineFunctions(TypeMap *typeMap, const RemoveUnusedPolicy &policy) {
117 passes.push_back(
118 new PassRepeated({new TypeChecking(nullptr, typeMap),
119 new DiscoverFunctionsInlining(&functionsToInline, typeMap),
120 new InlineFunctionsDriver(&functionsToInline, new FunctionsInliner()),
121 new RemoveAllUnusedDeclarations(policy)}));
122 passes.push_back(new CloneVariableDeclarations());
123 setName("InlineFunctions");
124 }
125};
126
127} // namespace P4
128
129#endif /* FRONTENDS_P4_FUNCTIONSINLINING_H_ */
Definition functionsInlining.h:96
Definition functionsInlining.h:32
Definition functionsInlining.cpp:78
Definition functionsInlining.h:51
Definition indexed_vector.h:40
Definition node.h:94
Definition commonInlining.h:178
Definition visitor.h:413
Definition ir/pass_manager.h:145
Iterates RemoveUnusedDeclarations until convergence.
Definition unusedDeclarations.h:189
Definition unusedDeclarations.h:48
Definition commonInlining.h:39
Definition commonInlining.h:92
Definition commonInlining.h:66
Definition visitor.h:437
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