P4C
The P4 Compiler
Loading...
Searching...
No Matches
simplifyDefUse.h
1/*
2Copyright 2016 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_SIMPLIFYDEFUSE_H_
18#define FRONTENDS_P4_SIMPLIFYDEFUSE_H_
19
20#include "frontends/common/parser_options.h"
21#include "frontends/p4/cloner.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "ir/ir.h"
24
25namespace P4 {
26
28 ReferenceMap *refMap;
29 TypeMap *typeMap;
30
31 const IR::Node *process(const IR::Node *node);
32
33 public:
34 DoSimplifyDefUse(ReferenceMap *refMap, TypeMap *typeMap) : refMap(refMap), typeMap(typeMap) {
35 CHECK_NULL(refMap);
36 CHECK_NULL(typeMap);
37 setName("DoSimplifyDefUse");
38 }
39
40 const IR::Node *postorder(IR::Function *function) override {
41 if (findContext<IR::Declaration_Instance>() == nullptr)
42 // not an abstract function implementation: these
43 // are processed as part of the control body
44 return process(function);
45 return function;
46 }
47 const IR::Node *postorder(IR::P4Parser *parser) override { return process(parser); }
48 const IR::Node *postorder(IR::P4Control *control) override { return process(control); }
49};
50
53class RemoveHidden : public Transform {
54 const IR::Node *postorder(IR::BlockStatement *stat) override {
55 if (!stat->components.empty()) return stat;
56 if (!stat->hasOnlyAnnotation(IR::Annotation::hiddenAnnotation)) return stat;
57 // Lose the annotation.
58 return new IR::BlockStatement(stat->srcInfo);
59 }
60};
61
63 ReferenceMap refMap;
64
65 class Cloner : public CloneExpressions {
66 public:
67 Cloner() { setName("Cloner"); }
68 const IR::Node *postorder(IR::EmptyStatement *stat) override {
69 // You cannot clone an empty statement, since
70 // the visitor claims it's equal to the original one.
71 // So we convert it to an empty block.
72 return new IR::BlockStatement(stat->srcInfo);
73 }
74 const IR::Node *postorder(IR::BlockStatement *stat) override {
75 // If the block statement is empty then we need to clone
76 // it and add an new annotation to force it to be
77 // different from the original one.
78 if (stat->components.empty()) {
79 // We are losing the original annotations, but hopefully these don't
80 // matter on an empty block.
81 auto result = new IR::BlockStatement(
82 stat->srcInfo, {new IR::Annotation(IR::Annotation::hiddenAnnotation, {})});
83 LOG2("Cloning " << getOriginal()->id << " into " << result->id);
84 return result;
85 }
86 // Ideally we'd like CloneExpressions::postorder(stat),
87 // but that doesn't work.
88 return Transform::postorder(stat);
89 }
90 };
91
92 public:
93 explicit SimplifyDefUse(TypeMap *typeMap, TypeChecking *typeChecking = nullptr) {
94 CHECK_NULL(typeMap);
95
96 refMap.setIsV1(P4CContext::get().options().isv1());
97
98 // SimplifyDefUse needs the expression tree *not* to be a DAG,
99 // because it keeps state in hash-maps indexed with PathExpressions.
100 // This is achieved by Cloner.
101 passes.push_back(new Cloner());
102 if (!typeChecking) typeChecking = new TypeChecking(&refMap, typeMap);
103
104 passes.push_back(new PassRepeated({typeChecking, new DoSimplifyDefUse(&refMap, typeMap)}));
105 passes.push_back(new RemoveHidden());
106 setName("SimplifyDefUse");
107 }
108};
109
110} // namespace P4
111
112#endif /* FRONTENDS_P4_SIMPLIFYDEFUSE_H_ */
Definition cloner.h:26
Definition simplifyDefUse.h:27
Definition node.h:94
static P4CContext & get()
Definition parser_options.cpp:539
Definition ir/pass_manager.h:40
Definition ir/pass_manager.h:145
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition simplifyDefUse.h:53
Definition simplifyDefUse.h:62
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24