P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
actionsInlining.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 FRONTENDS_P4_ACTIONSINLINING_H_
18#define FRONTENDS_P4_ACTIONSINLINING_H_
19
20#include "commonInlining.h"
21#include "frontends/p4/typeChecking/typeChecker.h"
22#include "frontends/p4/unusedDeclarations.h"
23#include "ir/ir.h"
24
25namespace P4 {
26
27typedef SimpleCallInfo<IR::P4Action, IR::MethodCallStatement> ActionCallInfo;
28typedef SimpleInlineWorkList<IR::P4Action, IR::MethodCallStatement, ActionCallInfo> AInlineWorkList;
29typedef SimpleInlineList<IR::P4Action, ActionCallInfo, AInlineWorkList> ActionsInlineList;
30
31class DiscoverActionsInlining : public Inspector {
32 ActionsInlineList *toInline; // output
33 P4::ReferenceMap *refMap; // input
34 P4::TypeMap *typeMap; // input
35 public:
37 P4::TypeMap *typeMap)
38 : toInline(toInline), refMap(refMap), typeMap(typeMap) {
39 CHECK_NULL(toInline);
40 CHECK_NULL(refMap);
41 CHECK_NULL(typeMap);
42 setName("DiscoverActionsInlining");
43 }
44 bool preorder(const IR::P4Parser *) override { return false; } // skip
45 void postorder(const IR::MethodCallStatement *mcs) override;
46};
47
48// General-purpose actions inliner.
49class ActionsInliner : public AbstractInliner<ActionsInlineList, AInlineWorkList> {
50 P4::ReferenceMap *refMap;
51 std::map<const IR::MethodCallStatement *, const IR::P4Action *> *replMap;
52
53 public:
54 explicit ActionsInliner(P4::ReferenceMap *refMap) : refMap(refMap), replMap(nullptr) {}
55 Visitor::profile_t init_apply(const IR::Node *node) override;
56 const IR::Node *preorder(IR::P4Parser *cont) override {
57 prune();
58 return cont;
59 } // skip
60 const IR::Node *preorder(IR::P4Action *action) override;
61 const IR::Node *postorder(IR::P4Action *action) override;
62 const IR::Node *preorder(IR::MethodCallStatement *statement) override;
63};
64
66
67class InlineActions : public PassManager {
68 ActionsInlineList actionsToInline;
69
70 public:
71 InlineActions(ReferenceMap *refMap, TypeMap *typeMap, const RemoveUnusedPolicy &policy) {
72 passes.push_back(new TypeChecking(refMap, typeMap));
73 passes.push_back(new DiscoverActionsInlining(&actionsToInline, refMap, typeMap));
74 passes.push_back(new InlineActionsDriver(&actionsToInline, new ActionsInliner(refMap)));
75 passes.push_back(new RemoveAllUnusedDeclarations(refMap, policy));
76 setName("InlineActions");
77 }
78};
79
80} // namespace P4
81
82namespace P4_14 {
83
85class InlineActions : public Transform {
86 const IR::V1Program *global;
87 class SubstActionArgs : public Transform {
88 const IR::ActionFunction *function;
89 const IR::Primitive *callsite;
90 const IR::Node *postorder(IR::ActionArg *arg) override {
91 for (unsigned i = 0; i < function->args.size(); ++i)
92 if (function->args[i] == getOriginal()) return callsite->operands[i];
93 BUG("Action arg not argument of action");
94 return arg;
95 }
96
97 public:
98 SubstActionArgs(const IR::ActionFunction *f, const IR::Primitive *c)
99 : function(f), callsite(c) {}
100 };
101 const IR::V1Program *preorder(IR::V1Program *gl) override { return global = gl; }
102 const IR::Node *preorder(IR::Primitive *p) override {
103 if (auto af = global->get<IR::ActionFunction>(p->name)) {
104 SubstActionArgs saa(af, p);
105 saa.setCalledBy(this);
106 return af->action.clone()->apply(saa);
107 }
108 return p;
109 }
110};
111
112} // namespace P4_14
113
114#endif /* FRONTENDS_P4_ACTIONSINLINING_H_ */
Definition commonInlining.h:137
Definition actionsInlining.h:49
Definition actionsInlining.h:31
Definition actionsInlining.h:67
Definition commonInlining.h:158
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
Definition typeChecker.h:60
Definition typeMap.h:42
Special inliner which works directly on P4-14 representation.
Definition actionsInlining.h:85
Definition applyOptionsPragmas.cpp:24