Skip to content

Commit ab2f3c3

Browse files
authored
Merge pull request #1438 from reuk/reuk/simplify-expr-improvement
Remove tautological typecasts
2 parents 6be5fd5 + 1d87928 commit ab2f3c3

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/util/simplify_expr.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,18 @@ bool simplify_exprt::simplify_typecast(exprt &expr)
240240
if(expr_type.id()==ID_c_bool &&
241241
op_type.id()!=ID_bool)
242242
{
243+
// casts from boolean to a signed int and back:
244+
// (boolean)(int)boolean -> boolean
245+
if(expr.op0().id()==ID_typecast && op_type.id()==ID_signedbv)
246+
{
247+
const auto &typecast=to_typecast_expr(expr.op0());
248+
if(typecast.op().type().id()==ID_c_bool)
249+
{
250+
expr=typecast.op0();
251+
return false;
252+
}
253+
}
254+
243255
// rewrite (_Bool)x to (_Bool)(x!=0)
244256
binary_relation_exprt inequality;
245257
inequality.id(op_type.id()==ID_floatbv?ID_ieee_float_notequal:ID_notequal);

unit/util/simplify_expr.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
#include <catch.hpp>
1010

11+
#include <java_bytecode/java_types.h>
1112
#include <util/arith_tools.h>
1213
#include <util/c_types.h>
14+
#include <util/config.h>
1315
#include <util/namespace.h>
1416
#include <util/pointer_predicates.h>
1517
#include <util/simplify_expr.h>
@@ -18,6 +20,8 @@
1820

1921
TEST_CASE("Simplify pointer_offset(address of array index)")
2022
{
23+
config.set_arch("none");
24+
2125
symbol_tablet symbol_table;
2226
namespacet ns(symbol_table);
2327

@@ -38,6 +42,8 @@ TEST_CASE("Simplify pointer_offset(address of array index)")
3842

3943
TEST_CASE("Simplify const pointer offset")
4044
{
45+
config.set_arch("none");
46+
4147
symbol_tablet symbol_table;
4248
namespacet ns(symbol_table);
4349

@@ -54,3 +60,23 @@ TEST_CASE("Simplify const pointer offset")
5460
REQUIRE(!to_integer(simp, offset_value));
5561
REQUIRE(offset_value==1234);
5662
}
63+
64+
TEST_CASE("Simplify Java boolean -> int -> boolean casts")
65+
{
66+
config.set_arch("none");
67+
68+
const exprt simplified=simplify_expr(
69+
typecast_exprt(
70+
typecast_exprt(
71+
symbol_exprt(
72+
"foo",
73+
java_boolean_type()),
74+
java_int_type()),
75+
java_boolean_type()),
76+
namespacet(symbol_tablet()));
77+
78+
REQUIRE(simplified.id()==ID_symbol);
79+
REQUIRE(simplified.type()==java_boolean_type());
80+
const auto &symbol=to_symbol_expr(simplified);
81+
REQUIRE(symbol.get_identifier()=="foo");
82+
}

0 commit comments

Comments
 (0)