diff --git a/frontends/common/constantFolding.cpp b/frontends/common/constantFolding.cpp index 2ba8c18ae9..35b14dd016 100644 --- a/frontends/common/constantFolding.cpp +++ b/frontends/common/constantFolding.cpp @@ -1108,15 +1108,36 @@ const IR::Node *DoConstantFolding::postorder(IR::SelectExpression *expression) { return result; } +class FilterLikelyAnnot : public Transform { + IR::Statement *preorder(IR::Statement *stmt) { + prune(); + return stmt; + } + IR::BlockStatement *preorder(IR::BlockStatement *stmt) { + prune(); + visit(stmt->annotations, "annotations"); + return stmt; + } + IR::Annotation *preorder(IR::Annotation *annot) { + prune(); + if (annot->name == IR::Annotation::likelyAnnotation) return nullptr; + if (annot->name == IR::Annotation::unlikelyAnnotation) { + warning(ErrorType::WARN_IGNORE, "ignoring %1% on always taken statement", annot); + return nullptr; + } + return annot; + } +}; + const IR::Node *DoConstantFolding::postorder(IR::IfStatement *ifstmt) { if (auto cond = ifstmt->condition->to()) { if (cond->value) { - return ifstmt->ifTrue; + return ifstmt->ifTrue->apply(FilterLikelyAnnot()); } else { if (ifstmt->ifFalse == nullptr) { return new IR::EmptyStatement(ifstmt->srcInfo); } else { - return ifstmt->ifFalse; + return ifstmt->ifFalse->apply(FilterLikelyAnnot()); } } } diff --git a/testdata/p4_16_samples/annotation-likely.p4 b/testdata/p4_16_samples/annotation-likely.p4 new file mode 100644 index 0000000000..ba9726f6c8 --- /dev/null +++ b/testdata/p4_16_samples/annotation-likely.p4 @@ -0,0 +1,22 @@ +#include + +struct Headers { + bit<8> a; + bit<8> b; +} + +control ingress(inout Headers h) { + apply { + if (true) @likely { + h.a = 0; + } + if (true) @unlikely { + h.b = 0; + } + } +} + +control c(inout T d); +package top(c _c); + +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/annotation-likely-first.p4 b/testdata/p4_16_samples_outputs/annotation-likely-first.p4 new file mode 100644 index 0000000000..8661c2d5be --- /dev/null +++ b/testdata/p4_16_samples_outputs/annotation-likely-first.p4 @@ -0,0 +1,17 @@ +#include + +struct Headers { + bit<8> a; + bit<8> b; +} + +control ingress(inout Headers h) { + apply { + h.a = 8w0; + h.b = 8w0; + } +} + +control c(inout T d); +package top(c _c); +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/annotation-likely-frontend.p4 b/testdata/p4_16_samples_outputs/annotation-likely-frontend.p4 new file mode 100644 index 0000000000..8661c2d5be --- /dev/null +++ b/testdata/p4_16_samples_outputs/annotation-likely-frontend.p4 @@ -0,0 +1,17 @@ +#include + +struct Headers { + bit<8> a; + bit<8> b; +} + +control ingress(inout Headers h) { + apply { + h.a = 8w0; + h.b = 8w0; + } +} + +control c(inout T d); +package top(c _c); +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/annotation-likely-midend.p4 b/testdata/p4_16_samples_outputs/annotation-likely-midend.p4 new file mode 100644 index 0000000000..3c46706ac0 --- /dev/null +++ b/testdata/p4_16_samples_outputs/annotation-likely-midend.p4 @@ -0,0 +1,26 @@ +#include + +struct Headers { + bit<8> a; + bit<8> b; +} + +control ingress(inout Headers h) { + @hidden action annotationlikely11() { + h.a = 8w0; + h.b = 8w0; + } + @hidden table tbl_annotationlikely11 { + actions = { + annotationlikely11(); + } + const default_action = annotationlikely11(); + } + apply { + tbl_annotationlikely11.apply(); + } +} + +control c(inout T d); +package top(c _c); +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/annotation-likely.p4 b/testdata/p4_16_samples_outputs/annotation-likely.p4 new file mode 100644 index 0000000000..4ea67d2590 --- /dev/null +++ b/testdata/p4_16_samples_outputs/annotation-likely.p4 @@ -0,0 +1,21 @@ +#include + +struct Headers { + bit<8> a; + bit<8> b; +} + +control ingress(inout Headers h) { + apply { + if (true) @likely { + h.a = 0; + } + if (true) @unlikely { + h.b = 0; + } + } +} + +control c(inout T d); +package top(c _c); +top(ingress()) main; diff --git a/testdata/p4_16_samples_outputs/annotation-likely.p4-stderr b/testdata/p4_16_samples_outputs/annotation-likely.p4-stderr new file mode 100644 index 0000000000..fe7c594fa6 --- /dev/null +++ b/testdata/p4_16_samples_outputs/annotation-likely.p4-stderr @@ -0,0 +1,3 @@ +annotation-likely.p4(13): [--Wwarn=ignore] warning: ignoring @unlikely on always taken statement + if (true) @unlikely { + ^