Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify applying bitwise operators on sign extended values #1111

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions smt/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,14 @@ expr expr::operator&(const expr &rhs) const {
if (isAllOnes() || rhs.isZero())
return rhs;

{
expr lhsVal, rhsVal;
if (isSignExt(lhsVal) && rhs.isSignExt(rhsVal) &&
lhsVal.bits() == rhsVal.bits()) {
return (lhsVal & rhsVal).sext(bits() - lhsVal.bits());
}
}

auto fold_extract = [](auto &a, auto &b) {
uint64_t n;
if (!a.isUInt(n) || n == 0 || n == numeric_limits<uint64_t>::max())
Expand Down Expand Up @@ -1391,6 +1399,14 @@ expr expr::operator|(const expr &rhs) const {
if (isZero() || rhs.isAllOnes())
return rhs;

{
expr lhsVal, rhsVal;
if (isSignExt(lhsVal) && rhs.isSignExt(rhsVal) &&
lhsVal.bits() == rhsVal.bits()) {
return (lhsVal | rhsVal).sext(bits() - lhsVal.bits());
}
}

if (bits() == 1) {
if (auto a = get_bool(*this);
a.isValid())
Expand All @@ -1409,6 +1425,15 @@ expr expr::operator^(const expr &rhs) const {
return bits() == 1 ? (rhs == 0).toBVBool() : ~rhs;
if (rhs.isAllOnes())
return bits() == 1 ? (*this == 0).toBVBool() : ~*this;

{
expr lhsVal, rhsVal;
if (isSignExt(lhsVal) && rhs.isSignExt(rhsVal) &&
lhsVal.bits() == rhsVal.bits()) {
return (lhsVal ^ rhsVal).sext(bits() - lhsVal.bits());
}
}

return binopc(Z3_mk_bvxor, operator^, Z3_OP_BXOR, isZero, alwaysFalse);
}

Expand All @@ -1426,6 +1451,11 @@ expr expr::operator!() const {
}

expr expr::operator~() const {
expr val;
if (isSignExt(val)) {
return (~val).sext(bits() - val.bits());
}

return unop_fold(Z3_mk_bvnot);
}

Expand Down