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/p4/cloner.h"
21#include "frontends/p4/typeChecking/typeChecker.h"
22#include "ir/ir.h"
23
24namespace P4 {
25
27 ReferenceMap *refMap;
28 TypeMap *typeMap;
29
30 const IR::Node *process(const IR::Node *node);
31
32 public:
33 DoSimplifyDefUse(ReferenceMap *refMap, TypeMap *typeMap) : refMap(refMap), typeMap(typeMap) {
34 CHECK_NULL(refMap);
35 CHECK_NULL(typeMap);
36 setName("DoSimplifyDefUse");
37 }
38
39 const IR::Node *postorder(IR::Function *function) override {
40 if (findContext<IR::Declaration_Instance>() == nullptr)
41 // not an abstract function implementation: these
42 // are processed as part of the control body
43 return process(function);
44 return function;
45 }
46 const IR::Node *postorder(IR::P4Parser *parser) override { return process(parser); }
47 const IR::Node *postorder(IR::P4Control *control) override { return process(control); }
48};
49
52class RemoveHidden : public Transform {
53 const IR::Node *postorder(IR::BlockStatement *stat) override {
54 if (!stat->components.empty()) return stat;
55 if (stat->annotations->size() != 1) return stat;
56 auto anno = stat->annotations->getSingle(IR::Annotation::hiddenAnnotation);
57 if (!anno) return stat;
58 // Lose the annotation.
59 return new IR::BlockStatement(stat->srcInfo);
60 }
61};
62
64 class Cloner : public CloneExpressions {
65 public:
66 Cloner() { setName("Cloner"); }
67 const IR::Node *postorder(IR::EmptyStatement *stat) override {
68 // You cannot clone an empty statement, since
69 // the visitor claims it's equal to the original one.
70 // So we convert it to an empty block.
71 return new IR::BlockStatement(stat->srcInfo);
72 }
73 const IR::Node *postorder(IR::BlockStatement *stat) override {
74 // If the block statement is empty then we need to clone
75 // it and add an new annotation to force it to be
76 // different from the original one.
77 if (stat->components.empty()) {
78 auto annos = new IR::Annotations();
79 // We are losing the original annotations, but hopefully these don't
80 // matter on an empty block.
81 annos->add(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
82 auto result = new IR::BlockStatement(stat->srcInfo, annos);
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 SimplifyDefUse(ReferenceMap *refMap, TypeMap *typeMap, TypeChecking *typeChecking = nullptr) {
94 CHECK_NULL(refMap);
95 CHECK_NULL(typeMap);
96
97 // SimplifyDefUse needs the expression tree *not* to be a DAG,
98 // because it keeps state in hash-maps indexed with PathExpressions.
99 // This is achieved by Cloner.
100 passes.push_back(new Cloner());
101 if (!typeChecking) typeChecking = new TypeChecking(refMap, typeMap);
102
103 auto repeated = new PassRepeated({typeChecking, new DoSimplifyDefUse(refMap, typeMap)});
104 passes.push_back(repeated);
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:26
Definition node.h:95
Definition pass_manager.h:40
Definition pass_manager.h:145
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition simplifyDefUse.h:52
Definition simplifyDefUse.h:63
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