Skip to content

Commit 582cc2a

Browse files
authored
Merge pull request #8944 from tautschnig/simplify/bitand-bitor-constants
Simplify all-zero or all-one constants in bitand/bitor
2 parents c740a89 + 6e0af33 commit 582cc2a

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

src/util/simplify_expr_int.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,33 @@ simplify_exprt::simplify_bitwise(const multi_ary_exprt &expr)
768768
no_change = false;
769769
}
770770

771+
// 'all zeros' in bitand yields zero
772+
if(new_expr.id() == ID_bitand)
773+
{
774+
const constant_exprt zero = new_expr.type().id() == ID_bv
775+
? to_bv_type(new_expr.type()).all_zeros_expr()
776+
: from_integer(0, new_expr.type());
777+
for(const auto &op : new_expr.operands())
778+
{
779+
if(op == zero)
780+
return zero;
781+
}
782+
}
783+
784+
// 'all ones' in bitor yields all-ones
785+
if(new_expr.id() == ID_bitor)
786+
{
787+
const constant_exprt all_ones =
788+
new_expr.type().id() == ID_bv
789+
? to_bv_type(new_expr.type()).all_ones_expr()
790+
: from_integer(power(2, width) - 1, new_expr.type());
791+
for(const auto &op : new_expr.operands())
792+
{
793+
if(op == all_ones)
794+
return all_ones;
795+
}
796+
}
797+
771798
// now erase 'all zeros' out of bitor, bitxor
772799

773800
if(new_expr.id() == ID_bitor || new_expr.id() == ID_bitxor)

unit/util/simplify_expr.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: Michael Tautschnig
88

99
#include <util/arith_tools.h>
1010
#include <util/bitvector_expr.h>
11+
#include <util/bitvector_types.h>
1112
#include <util/byte_operators.h>
1213
#include <util/c_types.h>
1314
#include <util/cmdline.h>
@@ -652,3 +653,51 @@ TEST_CASE("Simplify quantifier", "[core][util]")
652653
REQUIRE(simplify_expr(forall_exprt{a, true_exprt{}}, ns) == true_exprt{});
653654
}
654655
}
656+
657+
TEST_CASE("Simplify all-zero constant in bitand", "[core][util]")
658+
{
659+
const symbol_tablet symbol_table;
660+
const namespacet ns{symbol_table};
661+
662+
const unsignedbv_typet u8{8};
663+
const symbol_exprt x{"x", u8};
664+
const auto zero = from_integer(0, u8);
665+
const bitand_exprt band{x, zero};
666+
REQUIRE(simplify_expr(band, ns) == zero);
667+
}
668+
669+
TEST_CASE("Simplify all-ones constant in bitor", "[core][util]")
670+
{
671+
const symbol_tablet symbol_table;
672+
const namespacet ns{symbol_table};
673+
674+
const unsignedbv_typet u8{8};
675+
const symbol_exprt x{"x", u8};
676+
const auto ones = from_integer(255, u8);
677+
const bitor_exprt bor{x, ones};
678+
REQUIRE(simplify_expr(bor, ns) == ones);
679+
}
680+
681+
TEST_CASE("Simplify all-zero constant in bitand with bv_typet", "[core][util]")
682+
{
683+
const symbol_tablet symbol_table;
684+
const namespacet ns{symbol_table};
685+
686+
const bv_typet bv8{8};
687+
const symbol_exprt x{"x", bv8};
688+
const auto zero = bv8.all_zeros_expr();
689+
const bitand_exprt band{x, zero};
690+
REQUIRE(simplify_expr(band, ns) == zero);
691+
}
692+
693+
TEST_CASE("Simplify all-ones constant in bitor with bv_typet", "[core][util]")
694+
{
695+
const symbol_tablet symbol_table;
696+
const namespacet ns{symbol_table};
697+
698+
const bv_typet bv8{8};
699+
const symbol_exprt x{"x", bv8};
700+
const auto ones = bv8.all_ones_expr();
701+
const bitor_exprt bor{x, ones};
702+
REQUIRE(simplify_expr(bor, ns) == ones);
703+
}

0 commit comments

Comments
 (0)