Skip to content

Commit

Permalink
strength reduction improvement (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDodd authored May 4, 2017
1 parent e6141c4 commit 55ae691
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 11 deletions.
39 changes: 39 additions & 0 deletions frontends/p4/strengthReduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,21 @@ int StrengthReduction::isPowerOf2(const IR::Expression* expr) const {

/// @section Visitor Methods

const IR::Node* StrengthReduction::postorder(IR::Cmpl* expr) {
if (auto a = expr->expr->to<IR::Cmpl>())
return a->expr;
return expr;
}

const IR::Node* StrengthReduction::postorder(IR::BAnd* expr) {
if (isZero(expr->left))
return expr->left;
if (isZero(expr->right))
return expr->right;
auto l = expr->left->to<IR::Cmpl>();
auto r = expr->right->to<IR::Cmpl>();
if (l && r)
return new IR::Cmpl(new IR::BOr(expr->srcInfo, l->expr, r->expr));
return expr;
}

Expand All @@ -77,6 +87,10 @@ const IR::Node* StrengthReduction::postorder(IR::BOr* expr) {
return expr->right;
if (isZero(expr->right))
return expr->left;
auto l = expr->left->to<IR::Cmpl>();
auto r = expr->right->to<IR::Cmpl>();
if (l && r)
return new IR::Cmpl(new IR::BAnd(expr->srcInfo, l->expr, r->expr));
return expr;
}

Expand All @@ -85,6 +99,15 @@ const IR::Node* StrengthReduction::postorder(IR::BXor* expr) {
return expr->right;
if (isZero(expr->right))
return expr->left;
bool cmpl = false;
if (auto l = expr->left->to<IR::Cmpl>()) {
expr->left = l->expr;
cmpl = !cmpl; }
if (auto r = expr->right->to<IR::Cmpl>()) {
expr->right = r->expr;
cmpl = !cmpl; }
if (cmpl)
return new IR::Cmpl(expr);
return expr;
}

Expand All @@ -110,6 +133,22 @@ const IR::Node* StrengthReduction::postorder(IR::LOr* expr) {
return expr;
}

const IR::Node* StrengthReduction::postorder(IR::LNot* expr) {
if (auto e = expr->expr->to<IR::Equ>())
return new IR::Neq(e->left, e->right);
if (auto e = expr->expr->to<IR::Neq>())
return new IR::Equ(e->left, e->right);
if (auto e = expr->expr->to<IR::Leq>())
return new IR::Grt(e->left, e->right);
if (auto e = expr->expr->to<IR::Geq>())
return new IR::Lss(e->left, e->right);
if (auto e = expr->expr->to<IR::Lss>())
return new IR::Geq(e->left, e->right);
if (auto e = expr->expr->to<IR::Grt>())
return new IR::Leq(e->left, e->right);
return expr;
}

const IR::Node* StrengthReduction::postorder(IR::Sub* expr) {
if (isZero(expr->right))
return expr->left;
Expand Down
2 changes: 2 additions & 0 deletions frontends/p4/strengthReduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ class StrengthReduction final : public Transform {

using Transform::postorder;

const IR::Node* postorder(IR::Cmpl* expr) override;
const IR::Node* postorder(IR::BAnd* expr) override;
const IR::Node* postorder(IR::BOr* expr) override;
const IR::Node* postorder(IR::BXor* expr) override;
const IR::Node* postorder(IR::LAnd* expr) override;
const IR::Node* postorder(IR::LOr* expr) override;
const IR::Node* postorder(IR::LNot* expr) override;
const IR::Node* postorder(IR::Sub* expr) override;
const IR::Node* postorder(IR::Add* expr) override;
const IR::Node* postorder(IR::Shl* expr) override;
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_14_samples_outputs/checksum-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ control verifyChecksum(in headers hdr, inout metadata meta) {
bit<16> tmp_4;
@name("ipv4_checksum") Checksum16() ipv4_checksum;
apply {
if (!(hdr.ipv4.ihl == 4w5))
if (hdr.ipv4.ihl != 4w5)
tmp_3 = false;
else {
tmp_4 = ipv4_checksum.get<tuple_0>({ hdr.ipv4.version, hdr.ipv4.ihl, hdr.ipv4.diffserv, hdr.ipv4.totalLen, hdr.ipv4.identification, hdr.ipv4.flags, hdr.ipv4.fragOffset, hdr.ipv4.ttl, hdr.ipv4.protocol, hdr.ipv4.srcAddr, hdr.ipv4.dstAddr });
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_14_samples_outputs/parser_dc_full-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ control verifyChecksum(in headers hdr, inout metadata meta) {
}
if (tmp_11)
mark_to_drop();
if (!(hdr.ipv4.ihl == 4w5))
if (hdr.ipv4.ihl != 4w5)
tmp_14 = false;
else {
tmp_15 = ipv4_checksum.get<tuple_0>({ hdr.ipv4.version, hdr.ipv4.ihl, hdr.ipv4.diffserv, hdr.ipv4.totalLen, hdr.ipv4.identification, hdr.ipv4.flags, hdr.ipv4.fragOffset, hdr.ipv4.ttl, hdr.ipv4.protocol, hdr.ipv4.srcAddr, hdr.ipv4.dstAddr });
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_14_samples_outputs/port_vlan_mapping-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ control verifyChecksum(in headers hdr, inout metadata meta) {
}
if (tmp_11)
mark_to_drop();
if (!(hdr.ipv4.ihl == 4w5))
if (hdr.ipv4.ihl != 4w5)
tmp_14 = false;
else {
tmp_15 = ipv4_checksum.get<tuple_0>({ hdr.ipv4.version, hdr.ipv4.ihl, hdr.ipv4.diffserv, hdr.ipv4.totalLen, hdr.ipv4.identification, hdr.ipv4.flags, hdr.ipv4.fragOffset, hdr.ipv4.ttl, hdr.ipv4.protocol, hdr.ipv4.srcAddr, hdr.ipv4.dstAddr });
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_14_samples_outputs/sai_p4-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ control verifyChecksum(in headers hdr, inout metadata meta) {
bit<16> tmp_4;
@name("ipv4_checksum") Checksum16() ipv4_checksum;
apply {
if (!(hdr.ipv4.ihl == 4w5))
if (hdr.ipv4.ihl != 4w5)
tmp_3 = false;
else {
tmp_4 = ipv4_checksum.get<tuple_0>({ hdr.ipv4.version, hdr.ipv4.ihl, hdr.ipv4.diffserv, hdr.ipv4.ipv4_length, hdr.ipv4.id, hdr.ipv4.flags, hdr.ipv4.offset, hdr.ipv4.ttl, hdr.ipv4.protocol, hdr.ipv4.srcAddr, hdr.ipv4.dstAddr });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5050,15 +5050,15 @@ control verifyChecksum(in headers hdr, inout metadata meta) {
@name("inner_ipv4_checksum") Checksum16() inner_ipv4_checksum;
@name("ipv4_checksum") Checksum16() ipv4_checksum;
apply {
if (!(hdr.inner_ipv4.ihl == 4w5))
if (hdr.inner_ipv4.ihl != 4w5)
tmp_1 = false;
else {
tmp_2 = inner_ipv4_checksum.get<tuple_8>({ hdr.inner_ipv4.version, hdr.inner_ipv4.ihl, hdr.inner_ipv4.diffserv, hdr.inner_ipv4.totalLen, hdr.inner_ipv4.identification, hdr.inner_ipv4.flags, hdr.inner_ipv4.fragOffset, hdr.inner_ipv4.ttl, hdr.inner_ipv4.protocol, hdr.inner_ipv4.srcAddr, hdr.inner_ipv4.dstAddr });
tmp_1 = hdr.inner_ipv4.hdrChecksum == tmp_2;
}
if (tmp_1)
mark_to_drop();
if (!(hdr.ipv4.ihl == 4w5))
if (hdr.ipv4.ihl != 4w5)
tmp_4 = false;
else {
tmp_5 = ipv4_checksum.get<tuple_8>({ hdr.ipv4.version, hdr.ipv4.ihl, hdr.ipv4.diffserv, hdr.ipv4.totalLen, hdr.ipv4.identification, hdr.ipv4.flags, hdr.ipv4.fragOffset, hdr.ipv4.ttl, hdr.ipv4.protocol, hdr.ipv4.srcAddr, hdr.ipv4.dstAddr });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5576,7 +5576,7 @@ control process_fwd_results(inout headers hdr, inout metadata meta, inout standa
default_action = NoAction();
}
apply {
if (!(meta.ingress_metadata.bypass_lookups == 16w0xffff))
if (meta.ingress_metadata.bypass_lookups != 16w0xffff)
fwd_result.apply();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5456,7 +5456,7 @@ control process_fwd_results(inout headers hdr, inout metadata meta, inout standa
default_action = NoAction();
}
apply {
if (!(meta.ingress_metadata.bypass_lookups == 16w0xffff))
if (meta.ingress_metadata.bypass_lookups != 16w0xffff)
fwd_result_0.apply();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5948,7 +5948,7 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
process_ingress_bd_stats_ingress_bd_stats_0.apply();
process_ingress_acl_stats_acl_stats_0.apply();
process_storm_control_stats_storm_control_stats_2.apply();
if (!(meta.ingress_metadata.bypass_lookups == 16w0xffff))
if (meta.ingress_metadata.bypass_lookups != 16w0xffff)
process_fwd_results_fwd_result_0.apply();
if (meta.nexthop_metadata.nexthop_type == 1w1)
process_nexthop_ecmp_group_0.apply();
Expand Down Expand Up @@ -6042,15 +6042,15 @@ control verifyChecksum(in headers hdr, inout metadata meta) {
@name("inner_ipv4_checksum") Checksum16() inner_ipv4_checksum;
@name("ipv4_checksum") Checksum16() ipv4_checksum;
apply {
if (!(hdr.inner_ipv4.ihl == 4w5))
if (hdr.inner_ipv4.ihl != 4w5)
tmp_1 = false;
else {
tmp_2 = inner_ipv4_checksum.get<tuple_10>({ hdr.inner_ipv4.version, hdr.inner_ipv4.ihl, hdr.inner_ipv4.diffserv, hdr.inner_ipv4.totalLen, hdr.inner_ipv4.identification, hdr.inner_ipv4.flags, hdr.inner_ipv4.fragOffset, hdr.inner_ipv4.ttl, hdr.inner_ipv4.protocol, hdr.inner_ipv4.srcAddr, hdr.inner_ipv4.dstAddr });
tmp_1 = hdr.inner_ipv4.hdrChecksum == tmp_2;
}
if (tmp_1)
mark_to_drop();
if (!(hdr.ipv4.ihl == 4w5))
if (hdr.ipv4.ihl != 4w5)
tmp_4 = false;
else {
tmp_5 = ipv4_checksum.get<tuple_10>({ hdr.ipv4.version, hdr.ipv4.ihl, hdr.ipv4.diffserv, hdr.ipv4.totalLen, hdr.ipv4.identification, hdr.ipv4.flags, hdr.ipv4.fragOffset, hdr.ipv4.ttl, hdr.ipv4.protocol, hdr.ipv4.srcAddr, hdr.ipv4.dstAddr });
Expand Down
30 changes: 30 additions & 0 deletions testdata/p4_16_samples/strength2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

control strength() {
apply {
bit<4> x;
bit<4> y;
bit<4> z;
z = ~x ^ ~y;
if (!(x < y)) {
z = ~(x ^ (~y & ~z));
}
if (!(x > y)) {
z = ~(x ^ (~y | ~z));
}
}
}
13 changes: 13 additions & 0 deletions testdata/p4_16_samples_outputs/strength2-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
control strength() {
apply {
bit<4> x;
bit<4> y;
bit<4> z;
z = x ^ y;
if (x >= y)
z = x ^ (y | z);
if (x <= y)
z = x ^ y & z;
}
}

Empty file.
15 changes: 15 additions & 0 deletions testdata/p4_16_samples_outputs/strength2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
control strength() {
apply {
bit<4> x;
bit<4> y;
bit<4> z;
z = ~x ^ ~y;
if (!(x < y)) {
z = ~(x ^ ~y & ~z);
}
if (!(x > y)) {
z = ~(x ^ (~y | ~z));
}
}
}

1 change: 1 addition & 0 deletions testdata/p4_16_samples_outputs/strength2.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
warning: Program does not contain a `main' module

0 comments on commit 55ae691

Please sign in to comment.