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