Skip to content

Commit 62b52ba

Browse files
Merge pull request diffblue#1598 from peterschrammel/simplify-string-const-equalities
Simplify equalities of constants
2 parents 81cc65f + a656e7b commit 62b52ba

File tree

17 files changed

+102
-42
lines changed

17 files changed

+102
-42
lines changed

regression/cbmc-java/virtual8/A.class

222 Bytes
Binary file not shown.

regression/cbmc-java/virtual8/B.class

207 Bytes
Binary file not shown.

regression/cbmc-java/virtual8/C.class

207 Bytes
Binary file not shown.

regression/cbmc-java/virtual8/D.class

164 Bytes
Binary file not shown.

regression/cbmc-java/virtual8/E.class

164 Bytes
Binary file not shown.
354 Bytes
Binary file not shown.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Test {
2+
public static void runF(A x) {
3+
x.f();
4+
}
5+
6+
public static void main() {
7+
A y = new D();
8+
runF(y);
9+
}
10+
}
11+
12+
class A {
13+
void f() { }
14+
}
15+
16+
17+
class B extends A {
18+
void f() { }
19+
}
20+
21+
class C extends A {
22+
void f() { }
23+
}
24+
25+
class D extends C {
26+
}
27+
28+
class E extends B {
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
Test.class
3+
--program-only
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
C\.f
7+
--
8+
A\.f
9+
B\.f
10+
D\.f
11+
E\.f

regression/cbmc-java/virtual9/A.class

222 Bytes
Binary file not shown.

regression/cbmc-java/virtual9/B.class

207 Bytes
Binary file not shown.

regression/cbmc-java/virtual9/C.class

207 Bytes
Binary file not shown.

regression/cbmc-java/virtual9/D.class

164 Bytes
Binary file not shown.

regression/cbmc-java/virtual9/E.class

164 Bytes
Binary file not shown.
456 Bytes
Binary file not shown.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Test {
2+
public static void runF(A x) {
3+
x.f();
4+
}
5+
6+
public static void main(String[] args) {
7+
A y = null;
8+
if(args.length==3)
9+
y = new C();
10+
else
11+
y = new E();
12+
runF(y);
13+
}
14+
}
15+
16+
class A {
17+
void f() { }
18+
}
19+
20+
21+
class B extends A {
22+
void f() { }
23+
}
24+
25+
class C extends A {
26+
void f() { }
27+
}
28+
29+
class D extends C {
30+
}
31+
32+
class E extends B {
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
KNOWNBUG
2+
Test.class
3+
--program-only
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
B\.f
7+
C\.f
8+
--
9+
A\.f
10+
D\.f
11+
E\.f
12+
--
13+
could be pruned by a value set analysis

src/util/simplify_expr_int.cpp

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,11 +1215,6 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
12151215
(expr.id()==ID_equal || expr.id()==ID_notequal))
12161216
return simplify_inequality_pointer_object(expr);
12171217

1218-
// first see if we compare to a constant
1219-
1220-
bool op0_is_const=tmp0.is_constant();
1221-
bool op1_is_const=tmp1.is_constant();
1222-
12231218
ns.follow_symbol(tmp0.type());
12241219
ns.follow_symbol(tmp1.type());
12251220

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

1227+
const auto tmp0_const = expr_try_dynamic_cast<constant_exprt>(tmp0);
1228+
const auto tmp1_const = expr_try_dynamic_cast<constant_exprt>(tmp1);
1229+
12321230
// are _both_ constant?
1233-
if(op0_is_const && op1_is_const)
1231+
if(tmp0_const && tmp1_const)
12341232
{
1235-
if(tmp0.type().id()==ID_bool)
1233+
if(expr.id() == ID_equal || expr.id() == ID_notequal)
12361234
{
1237-
bool v0=tmp0.is_true();
1238-
bool v1=tmp1.is_true();
12391235

1240-
if(expr.id()==ID_equal)
1241-
{
1242-
expr.make_bool(v0==v1);
1243-
return false;
1244-
}
1245-
else if(expr.id()==ID_notequal)
1246-
{
1247-
expr.make_bool(v0!=v1);
1248-
return false;
1249-
}
1236+
bool equal = (tmp0_const->get_value() == tmp1_const->get_value());
1237+
expr.make_bool(expr.id() == ID_equal ? equal : !equal);
1238+
return false;
12501239
}
1251-
else if(tmp0.type().id()==ID_fixedbv)
1240+
1241+
if(tmp0.type().id() == ID_fixedbv)
12521242
{
12531243
fixedbvt f0(to_constant_expr(tmp0));
12541244
fixedbvt f1(to_constant_expr(tmp1));
12551245

1256-
if(expr.id()==ID_notequal)
1257-
expr.make_bool(f0!=f1);
1258-
else if(expr.id()==ID_equal)
1259-
expr.make_bool(f0==f1);
1260-
else if(expr.id()==ID_ge)
1246+
if(expr.id() == ID_ge)
12611247
expr.make_bool(f0>=f1);
12621248
else if(expr.id()==ID_le)
12631249
expr.make_bool(f0<=f1);
@@ -1275,11 +1261,7 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
12751261
ieee_floatt f0(to_constant_expr(tmp0));
12761262
ieee_floatt f1(to_constant_expr(tmp1));
12771263

1278-
if(expr.id()==ID_notequal)
1279-
expr.make_bool(f0!=f1);
1280-
else if(expr.id()==ID_equal)
1281-
expr.make_bool(f0==f1);
1282-
else if(expr.id()==ID_ge)
1264+
if(expr.id() == ID_ge)
12831265
expr.make_bool(f0>=f1);
12841266
else if(expr.id()==ID_le)
12851267
expr.make_bool(f0<=f1);
@@ -1302,11 +1284,7 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
13021284
if(to_rational(tmp1, r1))
13031285
return true;
13041286

1305-
if(expr.id()==ID_notequal)
1306-
expr.make_bool(r0!=r1);
1307-
else if(expr.id()==ID_equal)
1308-
expr.make_bool(r0==r1);
1309-
else if(expr.id()==ID_ge)
1287+
if(expr.id() == ID_ge)
13101288
expr.make_bool(r0>=r1);
13111289
else if(expr.id()==ID_le)
13121290
expr.make_bool(r0<=r1);
@@ -1329,11 +1307,7 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
13291307
if(to_integer(tmp1, v1))
13301308
return true;
13311309

1332-
if(expr.id()==ID_notequal)
1333-
expr.make_bool(v0!=v1);
1334-
else if(expr.id()==ID_equal)
1335-
expr.make_bool(v0==v1);
1336-
else if(expr.id()==ID_ge)
1310+
if(expr.id() == ID_ge)
13371311
expr.make_bool(v0>=v1);
13381312
else if(expr.id()==ID_le)
13391313
expr.make_bool(v0<=v1);
@@ -1347,7 +1321,7 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
13471321
return false;
13481322
}
13491323
}
1350-
else if(op0_is_const)
1324+
else if(tmp0_const)
13511325
{
13521326
// we want the constant on the RHS
13531327

@@ -1366,7 +1340,7 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
13661340
simplify_inequality_constant(expr);
13671341
return false;
13681342
}
1369-
else if(op1_is_const)
1343+
else if(tmp1_const)
13701344
{
13711345
// one is constant
13721346
return simplify_inequality_constant(expr);

0 commit comments

Comments
 (0)