28#include "bf-p4c/device.h"
29#include "bf-p4c/midend/path_linearizer.h"
30#include "bf-p4c/midend/type_categories.h"
31#include "bf-p4c/midend/type_checker.h"
32#include "frontends/common/resolveReferences/resolveReferences.h"
33#include "frontends/p4/simplify.h"
34#include "frontends/p4/strengthReduction.h"
35#include "frontends/p4/typeChecking/typeChecker.h"
36#include "frontends/p4/typeMap.h"
38#include "ir/pattern.h"
40#ifndef _BACKENDS_TOFINO_BF_P4C_MIDEND_SIMPLIFY_NESTED_IF_H_
41#define _BACKENDS_TOFINO_BF_P4C_MIDEND_SIMPLIFY_NESTED_IF_H_
55 virtual bool convert(
const IR::P4Control *control)
const = 0;
64 bool convert(
const IR::P4Control *control)
const override {
65 return control->is<IR::BFN::TnaDeparser>();
75 std::vector<const IR::Expression *> stack_;
76 std::vector<int> *mirrorType, *resubmitType, *digestType;
77 std::map<const IR::Statement *, std::vector<const IR::Expression *>> extraStmts;
82 if (Device::currentDevice() == Device::TOFINO) {
85 mirrorType =
new std::vector<int>(size);
86 resubmitType =
new std::vector<int>(size);
87 digestType =
new std::vector<int>(size);
91 const IR::Node *preorder(IR::IfStatement *statement)
override;
92 const IR::Node *preorder(IR::P4Control *control)
override;
93 void setExtraStmts(std::vector<int> *typeVec,
const IR::Statement *stmt,
94 const IR::Expression *condition);
95 std::vector<int> *checkTypeAndGetVec(
const IR::Statement *stmt);
96 void addInArray(std::vector<int> *typeVec,
const IR::Expression *condition,
97 const IR::IfStatement *ifstmt);
106 virtual void reset() = 0;
107 virtual bool check(
const IR::Expression *) = 0;
116 const std::set<cstring> *valid_fields;
117 std::set<cstring> unique_fields;
121 const std::set<cstring> *valid_fields)
122 : refMap(refMap), typeMap(typeMap), valid_fields(valid_fields) {
125 CHECK_NULL(valid_fields);
128 void reset()
override { unique_fields.clear(); }
129 bool check(
const IR::Expression *dest)
override {
132 if (!path.linearPath) {
133 error(
"Destination %1% is too complex ", dest);
138 auto *paramType = typeMap->getType(param);
140 error(
"Destination %1% must be intrinsic metadata ", dest);
144 if (
auto mem = path.linearPath->components[0]->to<IR::Member>()) {
145 if (!valid_fields->count(mem->member.name)) {
147 "Invalid field name %1%, the valid fields to use are "
148 "digest_type, resubmit_type and mirror_type");
152 unique_fields.insert(path.linearPath->to_cstring());
154 if (unique_fields.size() != 1)
return false;
177 std::stack<const IR::Expression *> stack_;
178 const IR::Expression *unique_dest =
nullptr;
182 : policy(policy), skip(skip) {
187 void do_equ(
bitvec &val,
const IR::Equ *eq);
188 void do_neq(
bitvec &val,
const IR::Neq *neq);
189 const IR::Node *preorder(IR::LAnd *expr)
override;
190 const IR::Node *preorder(IR::P4Control *control)
override;
201 std::set<cstring> valid_fields;
202 valid_fields = {
"digest_type"_cs,
"resubmit_type"_cs,
"mirror_type"_cs};
205 if (!typeChecking) typeChecking =
new TypeChecking(refMap, typeMap);
206 passes.push_back(typeChecking);
212 setName(
"SimplifyNestedIf");
Definition ir/pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition strengthReduction.h:119
Definition typeChecker.h:55
Definition ordered_map.h:32
virtual bool convert(const IR::P4Control *control) const =0
bool convert(const IR::P4Control *control) const override
Definition simplify_nested_if.h:64
Pass that tries to simplify the conditions to a simple comparison of constants.
Definition simplify_nested_if.h:172
Definition simplify_nested_if.h:72
Definition simplify_nested_if.h:61
Definition simplify_nested_if.h:103
Top level PassManager that governs simplification of nested if statements in the deparser control blo...
Definition simplify_nested_if.h:198
Definition simplify_nested_if.h:48
Definition simplify_nested_if.h:113
const IR::Parameter * getContainingParameter(const LinearPath &path, P4::ReferenceMap *refMap)
Definition midend/type_categories.cpp:131
bool isIntrinsicMetadataType(const IR::Type *type)
Definition midend/type_categories.cpp:29
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51
Definition path_linearizer.h:75