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->annotations->size() != 1) return stat;
57 auto anno = stat->annotations->getSingle(IR::Annotation::hiddenAnnotation);
58 if (!anno) return stat;
59 // Lose the annotation.
60 return new IR::BlockStatement(stat->srcInfo);
61 }
62};
63
65 ReferenceMap refMap;
66
67 class Cloner : public CloneExpressions {
68 public:
69 Cloner() { setName("Cloner"); }
70 const IR::Node *postorder(IR::EmptyStatement *stat) override {
71 // You cannot clone an empty statement, since
72 // the visitor claims it's equal to the original one.
73 // So we convert it to an empty block.
74 return new IR::BlockStatement(stat->srcInfo);
75 }
76 const IR::Node *postorder(IR::BlockStatement *stat) override {
77 // If the block statement is empty then we need to clone
78 // it and add an new annotation to force it to be
79 // different from the original one.
80 if (stat->components.empty()) {
81 auto annos = new IR::Annotations();
82 // We are losing the original annotations, but hopefully these don't
83 // matter on an empty block.
84 annos->add(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
85 auto result = new IR::BlockStatement(stat->srcInfo, annos);
86 LOG2("Cloning " << getOriginal()->id << " into " << result->id);
87 return result;
88 }
89 // Ideally we'd like CloneExpressions::postorder(stat),
90 // but that doesn't work.
91 return Transform::postorder(stat);
92 }
93 };
94
95 public:
96 explicit SimplifyDefUse(TypeMap *typeMap, TypeChecking *typeChecking = nullptr) {
97 CHECK_NULL(typeMap);
98
99 refMap.setIsV1(P4CContext::get().options().isv1());
100
101 // SimplifyDefUse needs the expression tree *not* to be a DAG,
102 // because it keeps state in hash-maps indexed with PathExpressions.
103 // This is achieved by Cloner.
104 passes.push_back(new Cloner());
105 if (!typeChecking) typeChecking = new TypeChecking(&refMap, typeMap);
106
107 passes.push_back(new PassRepeated({typeChecking, new DoSimplifyDefUse(&refMap, typeMap)}));
108 passes.push_back(new RemoveHidden());
109 setName("SimplifyDefUse");
110 }
111};
112
113} // namespace P4
114
115#endif /* FRONTENDS_P4_SIMPLIFYDEFUSE_H_ */
Definition cloner.h:26
Definition simplifyDefUse.h:27
Definition node.h:95
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
void setIsV1(bool isv1)
Set boolean indicating whether map is for a P4_14 program to isV1.
Definition referenceMap.h:105
Definition simplifyDefUse.h:53
Definition simplifyDefUse.h:64
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