P4C
The P4 Compiler
Loading...
Searching...
No Matches
unusedCodeMetric.h
1/*
2Counts the number of unused code instances by collecting data
3before, and after frontend optimalizations. In the first run,
4the instance counts, as well as variable and action names are
5saved into helper variables in the metrics structure. During
6the second run, the unused code counts are determined by
7comparing collected data from the previous run with the new data.
8*/
9
10#ifndef FRONTENDS_P4_METRICS_UNUSEDCODEMETRIC_H_
11#define FRONTENDS_P4_METRICS_UNUSEDCODEMETRIC_H_
12
13#include <algorithm>
14#include <string>
15#include <vector>
16
17#include "frontends/p4/metrics/metricsStructure.h"
18#include "ir/ir.h"
19#include "lib/log.h"
20
21using namespace P4::literals;
22
23namespace P4 {
24
25class UnusedCodeMetricPass : public Inspector {
26 private:
27 Metrics &metrics;
28 UnusedCodeInstances currentInstancesCount;
29 std::vector<cstring> scope;
30 bool isBefore;
31
32 void recordBefore();
33 void recordAfter();
34
35 public:
36 explicit UnusedCodeMetricPass(Metrics &metrics, bool isBefore)
37 : metrics(metrics), isBefore(isBefore) {
38 setName("UnusedCodeMetricPass");
39 }
40
41 // Scope handling.
42
43 bool scope_enter(cstring name);
44 void scope_leave();
45 bool preorder(const IR::P4Control *control) override;
46 bool preorder(const IR::P4Parser *parser) override;
47 bool preorder(const IR::Function *function) override;
48 bool preorder(const IR::IfStatement *stmt) override;
49 bool preorder(const IR::SwitchCase *caseNode) override;
50 void postorder(const IR::P4Control * /*control*/) override;
51 void postorder(const IR::P4Parser * /*parser*/) override;
52 void postorder(const IR::Function * /*function*/) override;
53 void postorder(const IR::IfStatement *stmt) override;
54 void postorder(const IR::SwitchCase * /*case*/) override;
55
56 // Data collection.
57
58 bool preorder(const IR::P4Action *action) override;
59 void postorder(const IR::ParserState * /*state*/) override;
60 void postorder(const IR::Declaration_Variable *node) override;
61 void postorder(const IR::Type_Enum * /*node*/) override;
62 void postorder(const IR::Type_SerEnum * /*node*/) override;
63 void postorder(const IR::Parameter * /*node*/) override;
64 void postorder(const IR::ReturnStatement * /*node*/) override;
65 void postorder(const IR::P4Program * /*node*/) override;
66};
67
68} // namespace P4
69
70#endif /* FRONTENDS_P4_METRICS_UNUSEDCODEMETRIC_H_ */
Definition visitor.h:413
Definition cstring.h:85
Definition cstring.h:80
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition metricsStructure.h:119
Definition metricsStructure.h:27