P4C
The P4 Compiler
Loading...
Searching...
No Matches
match_reducer.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_PARDE_COMMON_MATCH_REDUCER_H_
20#define BACKENDS_TOFINO_BF_P4C_PARDE_COMMON_MATCH_REDUCER_H_
21
22#include <stdint.h>
23
24#include "lib/match.h"
25#include "lib/ordered_set.h"
26
31 const std::vector<match_t> matches_;
32
38 struct cmp {
39 bool operator()(const match_t &a, const match_t &b) const {
40 return std::tie(a.word1, a.word0) < std::tie(b.word1, b.word0);
41 }
42 };
43
44 bool is_power_of_2(big_int value) { return value && !(value & (value - 1)); }
45
55 std::set<match_t, cmp> reduce(const std::set<match_t, cmp> matches) {
56 std::set<match_t, cmp> transformed_matches;
57 for (const auto &match1 : matches) {
58 bool combined_matches = false;
59 for (const auto &match2 : matches) {
60 big_int xor0 = match1.word0 ^ match2.word0, xor1 = match1.word1 ^ match2.word1;
61
62 if (xor0 == 0 && xor1 == 0) continue;
63 if (xor0 != xor1) continue;
64 if (!is_power_of_2(xor0)) continue;
65
66 match_t combined_match((match1.word0 | xor0), (match1.word1 | xor1));
67 transformed_matches.insert(combined_match);
68 combined_matches = true;
69 }
70 if (!combined_matches) transformed_matches.insert(match1);
71 }
72 if (transformed_matches != matches) return reduce(transformed_matches);
73 return matches;
74 }
75
81 void has_full_match_coverage() {
82 auto is_unconditional_match = [](match_t match) {
83 return match == match_t::dont_care(0) || match == match_t::dont_care(8) ||
84 match == match_t::dont_care(16) || match == match_t::dont_care(24) ||
85 match == match_t::dont_care(32);
86 };
87
88 rv = std::any_of(matches_.begin(), matches_.end(), is_unconditional_match);
89 if (rv) return;
90
91 std::set<match_t, cmp> reduced_matches =
92 reduce(std::set<match_t, cmp>(matches_.begin(), matches_.end()));
93 rv = std::any_of(reduced_matches.begin(), reduced_matches.end(), is_unconditional_match);
94 }
95
96 public:
97 bool rv = false;
98
99 explicit HasFullMatchCoverage(const std::vector<match_t> &matches) : matches_(matches) {
100 has_full_match_coverage();
101 }
102};
103
104#endif /* BACKENDS_TOFINO_BF_P4C_PARDE_COMMON_MATCH_REDUCER_H_ */
Definition match_reducer.h:30