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/p4/cloner.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "frontends/p4/unusedDeclarations.h"
24#include "ir/ir.h"
25
26namespace P4 {
27
28typedef SimpleCallInfo<IR::Node, IR::Statement> FunctionCallInfo;
29typedef SimpleInlineWorkList<IR::Node, IR::Statement, FunctionCallInfo> FunctionsInlineWorkList;
30typedef SimpleInlineList<IR::Node, FunctionCallInfo, FunctionsInlineWorkList> FunctionsInlineList;
31
32class DiscoverFunctionsInlining : public Inspector {
33 FunctionsInlineList *toInline; // output
34 P4::ReferenceMap *refMap; // input
35 P4::TypeMap *typeMap; // input
36
37 public:
39 P4::TypeMap *typeMap)
40 : toInline(toInline), refMap(refMap), typeMap(typeMap) {
41 CHECK_NULL(toInline);
42 CHECK_NULL(refMap);
43 CHECK_NULL(typeMap);
44 setName("DiscoverFunctionsInlining");
45 }
46 void postorder(const IR::MethodCallExpression *mce) override;
47};
48
53class FunctionsInliner : public AbstractInliner<FunctionsInlineList, FunctionsInlineWorkList> {
54 P4::ReferenceMap *refMap;
55 // All elements in the replacement map are actually IR::Function objects
56 typedef std::map<const IR::Statement *, const IR::Node *> ReplacementMap;
58 std::vector<ReplacementMap *> replacementStack;
59
64 const IR::Expression *cloneBody(const IR::IndexedVector<IR::StatOrDecl> &src,
65 IR::IndexedVector<IR::StatOrDecl> &dest);
67 const IR::Node *inlineBefore(const IR::Node *calleeNode, const IR::MethodCallExpression *call,
68 const IR::Statement *before);
69 bool preCaller();
70 const IR::Node *postCaller(const IR::Node *caller);
71 const ReplacementMap *getReplacementMap() const;
72 void dumpReplacementMap() const;
73
74 public:
75 explicit FunctionsInliner(bool isv1) : refMap(new P4::ReferenceMap()) { refMap->setIsV1(isv1); }
76 Visitor::profile_t init_apply(const IR::Node *node) override;
77 void end_apply(const IR::Node *node) override;
78 const IR::Node *preorder(IR::Function *function) override;
79 const IR::Node *preorder(IR::P4Control *control) override;
80 const IR::Node *preorder(IR::P4Parser *parser) override;
81 const IR::Node *preorder(IR::P4Action *action) override;
82 const IR::Node *preorder(IR::MethodCallStatement *statement) override;
83 const IR::Node *preorder(IR::AssignmentStatement *statement) override;
84};
85
87
90class CloneVariableDeclarations : public Transform {
91 public:
93 setName("CloneVariableDeclarations");
94 visitDagOnce = false;
95 }
96 const IR::Node *postorder(IR::Declaration_Variable *declaration) override {
97 // this will have a different declid
98 auto result = new IR::Declaration_Variable(declaration->srcInfo, declaration->getName(),
99 declaration->annotations, declaration->type,
100 declaration->initializer);
101 LOG3("Cloned " << dbp(result));
102 return result;
103 }
104};
105
106class InlineFunctions : public PassManager {
107 FunctionsInlineList functionsToInline;
108
109 public:
110 InlineFunctions(ReferenceMap *refMap, TypeMap *typeMap, const RemoveUnusedPolicy &policy) {
111 passes.push_back(new PassRepeated(
112 {new TypeChecking(refMap, typeMap),
113 new DiscoverFunctionsInlining(&functionsToInline, refMap, typeMap),
114 new InlineFunctionsDriver(&functionsToInline, new FunctionsInliner(refMap->isV1())),
115 new RemoveAllUnusedDeclarations(refMap, policy)}));
116 passes.push_back(new CloneVariableDeclarations());
117 setName("InlineFunctions");
118 }
119};
120
121} // namespace P4
122
123#endif /* FRONTENDS_P4_FUNCTIONSINLINING_H_ */
Definition commonInlining.h:137
Definition functionsInlining.h:90
Definition functionsInlining.h:32
Definition functionsInlining.h:53
Definition commonInlining.h:158
Definition functionsInlining.h:106
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
void setIsV1(bool isv1)
Set boolean indicating whether map is for a P4_14 program to isV1.
Definition referenceMap.h:105
bool isV1() const
Definition referenceMap.h:115
Iterates RemoveUnusedDeclarations until convergence.
Definition unusedDeclarations.h:146
Definition unusedDeclarations.h:28
Definition typeChecker.h:60
Definition typeMap.h:42
Definition applyOptionsPragmas.cpp:24