P4C
The P4 Compiler
Loading...
Searching...
No Matches
eliminateUnreachable.h
1/*
2SPDX-FileCopyrightText: 2026 Nvidia Corporation
3
4SPDX-License-Identifier: Apache-2.0
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17*/
18
19#ifndef MIDEND_ELIMINATEUNREACHABLE_H_
20#define MIDEND_ELIMINATEUNREACHABLE_H_
21
22namespace P4 {
23
24class ElimUnreachableStmts : public Transform {
25 private:
26 bool unreachable = false;
27
28 // top-level things that are always reachable
29 IR::Node *preorder(IR::Function *fn) {
30 unreachable = false;
31 return fn;
32 }
33 IR::Node *preorder(IR::Method *fn) {
34 unreachable = false;
35 return fn;
36 }
37 IR::Node *preorder(IR::P4Action *act) {
38 unreachable = false;
39 return act;
40 }
41 IR::Node *preorder(IR::P4Control *c) {
42 unreachable = false;
43 return c;
44 }
45 IR::Node *preorder(IR::P4Parser *p) {
46 unreachable = false;
47 return p;
48 }
49 IR::Node *preorder(IR::ParserState *s) {
50 unreachable = false;
51 return s;
52 }
53
54 // reset flag after declarations
55 IR::Node *postorder(IR::Declaration *d) {
56 unreachable = false;
57 return d;
58 }
59
60 // prune visiting expressions as they can't contain statements
61 IR::Node *postorder(IR::Expression *e) {
62 prune();
63 return e;
64 }
65
66 IR::Node *preorder(IR::Statement *s) {
67 if (unreachable) return nullptr;
68 return s;
69 }
70 IR::Node *preorder(IR::Declaration_Variable *v) {
71 if (unreachable) return nullptr;
72 return v;
73 }
74 IR::Node *preorder(IR::BreakStatement *s) {
75 if (unreachable) return nullptr;
76 unreachable = true;
77 return s;
78 }
79 IR::Node *preorder(IR::ContinueStatement *s) {
80 if (unreachable) return nullptr;
81 unreachable = true;
82 return s;
83 }
84 IR::Node *preorder(IR::ExitStatement *s) {
85 if (unreachable) return nullptr;
86 unreachable = true;
87 return s;
88 }
89 IR::Node *preorder(IR::ReturnStatement *s) {
90 if (unreachable) return nullptr;
91 unreachable = true;
92 return s;
93 }
94 IR::Node *preorder(IR::IfStatement *ifstmt) {
95 if (unreachable) return nullptr;
96 prune();
97 visit(ifstmt->ifTrue, "ifTrue");
98 bool jumps = unreachable;
99 unreachable = false;
100 visit(ifstmt->ifFalse, "ifTrue");
101 unreachable &= jumps;
102 if (!ifstmt->ifTrue) ifstmt->ifTrue = new IR::EmptyStatement;
103 return ifstmt;
104 }
105 IR::Node *preorder(IR::SwitchStatement *swtch) {
106 if (unreachable) return nullptr;
107 prune();
108 bool final_unreachable = true;
109 bool has_default = false;
110 for (auto &c : swtch->cases) {
111 if (c->label->is<IR::DefaultExpression>()) has_default = true;
112 if (c->statement) {
113 unreachable = false;
114 visit(c, "cases");
115 BUG_CHECK(c->statement, "bogus eliminated case");
116 final_unreachable &= unreachable;
117 }
118 }
119 unreachable = final_unreachable & has_default;
120 return swtch;
121 }
122 IR::Node *preorder(IR::ForStatement *loop) {
123 if (unreachable) return nullptr;
124 prune();
125 visit(loop->init, "init");
126 BUG_CHECK(!unreachable, "jump in loop init statements");
127 visit(loop->body, "body");
128 unreachable = false;
129 visit(loop->updates, "updates");
130 unreachable = false;
131 if (!loop->body) loop->body = new IR::EmptyStatement;
132 return loop;
133 }
134 IR::Node *preorder(IR::ForInStatement *loop) {
135 if (unreachable) return nullptr;
136 prune();
137 visit(loop->body, "body");
138 unreachable = false;
139 if (!loop->body) loop->body = new IR::EmptyStatement;
140 return loop;
141 }
142
143 public:
144 explicit ElimUnreachableStmts() {}
145};
146
147} // namespace P4
148
149#endif /* MIDEND_ELIMINATEUNREACHABLE_H_ */
Definition node.h:44
Definition visitor.h:433
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13