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 equalities of constants #1598

Merged
Show file tree
Hide file tree
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
Binary file added regression/cbmc-java/virtual8/A.class
Binary file not shown.
Binary file added regression/cbmc-java/virtual8/B.class
Binary file not shown.
Binary file added regression/cbmc-java/virtual8/C.class
Binary file not shown.
Binary file added regression/cbmc-java/virtual8/D.class
Binary file not shown.
Binary file added regression/cbmc-java/virtual8/E.class
Binary file not shown.
Binary file added regression/cbmc-java/virtual8/Test.class
Binary file not shown.
29 changes: 29 additions & 0 deletions regression/cbmc-java/virtual8/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class Test {
public static void runF(A x) {
x.f();
}

public static void main() {
A y = new D();
runF(y);
}
}

class A {
void f() { }
}


class B extends A {
void f() { }
}

class C extends A {
void f() { }
}

class D extends C {
}

class E extends B {
}
11 changes: 11 additions & 0 deletions regression/cbmc-java/virtual8/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
Test.class
--program-only
^EXIT=0$
^SIGNAL=0$
C\.f
--
A\.f
B\.f
D\.f
E\.f
Binary file added regression/cbmc-java/virtual9/A.class
Binary file not shown.
Binary file added regression/cbmc-java/virtual9/B.class
Binary file not shown.
Binary file added regression/cbmc-java/virtual9/C.class
Binary file not shown.
Binary file added regression/cbmc-java/virtual9/D.class
Binary file not shown.
Binary file added regression/cbmc-java/virtual9/E.class
Binary file not shown.
Binary file added regression/cbmc-java/virtual9/Test.class
Binary file not shown.
33 changes: 33 additions & 0 deletions regression/cbmc-java/virtual9/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Test {
public static void runF(A x) {
x.f();
}

public static void main(String[] args) {
A y = null;
if(args.length==3)
y = new C();
else
y = new E();
runF(y);
}
}

class A {
void f() { }
}


class B extends A {
void f() { }
}

class C extends A {
void f() { }
}

class D extends C {
}

class E extends B {
}
13 changes: 13 additions & 0 deletions regression/cbmc-java/virtual9/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
KNOWNBUG
Test.class
--program-only
^EXIT=0$
^SIGNAL=0$
B\.f
C\.f
--
A\.f
D\.f
E\.f
--
could be pruned by a value set analysis
58 changes: 16 additions & 42 deletions src/util/simplify_expr_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1215,11 +1215,6 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
(expr.id()==ID_equal || expr.id()==ID_notequal))
return simplify_inequality_pointer_object(expr);

// first see if we compare to a constant

bool op0_is_const=tmp0.is_constant();
bool op1_is_const=tmp1.is_constant();

ns.follow_symbol(tmp0.type());
ns.follow_symbol(tmp1.type());

Expand All @@ -1229,35 +1224,26 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
if(tmp1.type().id()==ID_c_enum_tag)
tmp1.type()=ns.follow_tag(to_c_enum_tag_type(tmp1.type()));

const auto tmp0_const = expr_try_dynamic_cast<constant_exprt>(tmp0);
const auto tmp1_const = expr_try_dynamic_cast<constant_exprt>(tmp1);

// are _both_ constant?
if(op0_is_const && op1_is_const)
if(tmp0_const && tmp1_const)
{
if(tmp0.type().id()==ID_bool)
if(expr.id() == ID_equal || expr.id() == ID_notequal)
{
bool v0=tmp0.is_true();
bool v1=tmp1.is_true();

if(expr.id()==ID_equal)
{
expr.make_bool(v0==v1);
return false;
}
else if(expr.id()==ID_notequal)
{
expr.make_bool(v0!=v1);
return false;
}
bool equal = (tmp0_const->get_value() == tmp1_const->get_value());
expr.make_bool(expr.id() == ID_equal ? equal : !equal);
return false;
}
else if(tmp0.type().id()==ID_fixedbv)

if(tmp0.type().id() == ID_fixedbv)
{
fixedbvt f0(to_constant_expr(tmp0));
fixedbvt f1(to_constant_expr(tmp1));

if(expr.id()==ID_notequal)
expr.make_bool(f0!=f1);
else if(expr.id()==ID_equal)
expr.make_bool(f0==f1);
else if(expr.id()==ID_ge)
if(expr.id() == ID_ge)
expr.make_bool(f0>=f1);
else if(expr.id()==ID_le)
expr.make_bool(f0<=f1);
Expand All @@ -1275,11 +1261,7 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
ieee_floatt f0(to_constant_expr(tmp0));
ieee_floatt f1(to_constant_expr(tmp1));

if(expr.id()==ID_notequal)
expr.make_bool(f0!=f1);
else if(expr.id()==ID_equal)
expr.make_bool(f0==f1);
else if(expr.id()==ID_ge)
if(expr.id() == ID_ge)
expr.make_bool(f0>=f1);
else if(expr.id()==ID_le)
expr.make_bool(f0<=f1);
Expand All @@ -1302,11 +1284,7 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
if(to_rational(tmp1, r1))
return true;

if(expr.id()==ID_notequal)
expr.make_bool(r0!=r1);
else if(expr.id()==ID_equal)
expr.make_bool(r0==r1);
else if(expr.id()==ID_ge)
if(expr.id() == ID_ge)
expr.make_bool(r0>=r1);
else if(expr.id()==ID_le)
expr.make_bool(r0<=r1);
Expand All @@ -1329,11 +1307,7 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
if(to_integer(tmp1, v1))
return true;

if(expr.id()==ID_notequal)
expr.make_bool(v0!=v1);
else if(expr.id()==ID_equal)
expr.make_bool(v0==v1);
else if(expr.id()==ID_ge)
if(expr.id() == ID_ge)
expr.make_bool(v0>=v1);
else if(expr.id()==ID_le)
expr.make_bool(v0<=v1);
Expand All @@ -1347,7 +1321,7 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
return false;
}
}
else if(op0_is_const)
else if(tmp0_const)
{
// we want the constant on the RHS

Expand All @@ -1366,7 +1340,7 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
simplify_inequality_constant(expr);
return false;
}
else if(op1_is_const)
else if(tmp1_const)
{
// one is constant
return simplify_inequality_constant(expr);
Expand Down