Skip to content

Commit f1e14a8

Browse files
author
Daniel Kroening
committed
simplifier: simplify_rec has new interface
This improves type safety.
1 parent 2ec0181 commit f1e14a8

5 files changed

+116
-87
lines changed

src/util/simplify_expr.cpp

Lines changed: 87 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
535535
from_integer(*sub_size, size_type()),
536536
typecast_exprt(expr.op().op1(), size_type()));
537537

538-
simplify_rec(new_expr.op()); // rec. call
538+
new_expr.op() = simplify_rec(new_expr.op()); // rec. call
539539

540540
return changed(simplify_typecast(new_expr)); // rec. call
541541
}
@@ -619,8 +619,7 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
619619
}
620620
}
621621

622-
simplify_rec(new_expr);
623-
return std::move(new_expr);
622+
return changed(simplify_rec(new_expr)); // recursive call
624623
}
625624
}
626625

@@ -959,8 +958,7 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
959958
auto result =
960959
address_of_exprt(index_exprt(o, from_integer(0, size_type())));
961960

962-
simplify_rec(result);
963-
return std::move(result);
961+
return changed(simplify_rec(result)); // recursive call
964962
}
965963
}
966964

@@ -996,8 +994,7 @@ simplify_exprt::simplify_dereference(const dereference_exprt &expr)
996994
{
997995
exprt tmp=to_address_of_expr(pointer).object();
998996
// one address_of is gone, try again
999-
simplify_rec(tmp);
1000-
return tmp;
997+
return changed(simplify_rec(tmp));
1001998
}
1002999
// rewrite *(&a[0] + x) to a[x]
10031000
else if(pointer.id()==ID_plus &&
@@ -1015,8 +1012,7 @@ simplify_exprt::simplify_dereference(const dereference_exprt &expr)
10151012
old.array(),
10161013
pointer_offset_sum(old.index(), pointer.op1()),
10171014
old.array().type().subtype());
1018-
simplify_rec(idx);
1019-
return idx;
1015+
return changed(simplify_rec(idx));
10201016
}
10211017
}
10221018
}
@@ -1179,9 +1175,9 @@ bool simplify_exprt::simplify_if_branch(
11791175
}
11801176

11811177
if(!tresult)
1182-
simplify_rec(trueexpr);
1178+
trueexpr = simplify_rec(trueexpr); // recursive call
11831179
if(!fresult)
1184-
simplify_rec(falseexpr);
1180+
falseexpr = simplify_rec(falseexpr); // recursive call
11851181

11861182
return tresult && fresult;
11871183
}
@@ -1214,7 +1210,7 @@ bool simplify_exprt::simplify_if_cond(exprt &expr)
12141210
}
12151211

12161212
if(!tmp)
1217-
simplify_rec(expr);
1213+
expr = simplify_rec(expr); // recursive call
12181214

12191215
result=tmp && result;
12201216
}
@@ -1228,14 +1224,21 @@ bool simplify_exprt::simplify_if_preorder(if_exprt &expr)
12281224
exprt &truevalue=expr.true_case();
12291225
exprt &falsevalue=expr.false_case();
12301226

1227+
bool result = true;
1228+
12311229
// we first want to look at the condition
1232-
bool result=simplify_rec(cond);
1230+
auto r_cond = simplify_rec(cond);
1231+
if(r_cond.has_changed())
1232+
{
1233+
cond = r_cond.expr;
1234+
result = false;
1235+
}
12331236

12341237
// 1 ? a : b -> a and 0 ? a : b -> b
12351238
if(cond.is_constant())
12361239
{
12371240
exprt tmp=cond.is_true()?truevalue:falsevalue;
1238-
simplify_rec(tmp);
1241+
tmp = simplify_rec(tmp);
12391242
expr.swap(tmp);
12401243
return false;
12411244
}
@@ -1270,7 +1273,12 @@ bool simplify_exprt::simplify_if_preorder(if_exprt &expr)
12701273
else
12711274
local_replace_map.insert(std::make_pair(cond, true_exprt()));
12721275

1273-
result=simplify_rec(truevalue) && result;
1276+
auto r_truevalue = simplify_rec(truevalue);
1277+
if(r_truevalue.has_changed())
1278+
{
1279+
truevalue = r_truevalue.expr;
1280+
result = false;
1281+
}
12741282

12751283
local_replace_map=map_before;
12761284

@@ -1290,18 +1298,43 @@ bool simplify_exprt::simplify_if_preorder(if_exprt &expr)
12901298
else
12911299
local_replace_map.insert(std::make_pair(cond, false_exprt()));
12921300

1293-
result=simplify_rec(falsevalue) && result;
1301+
auto r_falsevalue = simplify_rec(falsevalue);
1302+
if(r_falsevalue.has_changed())
1303+
{
1304+
falsevalue = r_falsevalue.expr;
1305+
result = false;
1306+
}
12941307

12951308
local_replace_map.swap(map_before);
12961309
#else
1297-
result=simplify_rec(truevalue) && result;
1298-
result=simplify_rec(falsevalue) && result;
1310+
auto r_truevalue = simplify_rec(truevalue);
1311+
if(r_truevalue.has_changed())
1312+
{
1313+
truevalue = r_truevalue.expr;
1314+
result = false;
1315+
}
1316+
auto r_falsevalue = simplify_rec(falsevalue);
1317+
if(r_falsevalue.has_changed())
1318+
{
1319+
falsevalue = r_falsevalue.expr;
1320+
result = false;
1321+
}
12991322
#endif
13001323
}
13011324
else
13021325
{
1303-
result=simplify_rec(truevalue) && result;
1304-
result=simplify_rec(falsevalue) && result;
1326+
auto r_truevalue = simplify_rec(truevalue);
1327+
if(r_truevalue.has_changed())
1328+
{
1329+
truevalue = r_truevalue.expr;
1330+
result = false;
1331+
}
1332+
auto r_falsevalue = simplify_rec(falsevalue);
1333+
if(r_falsevalue.has_changed())
1334+
{
1335+
falsevalue = r_falsevalue.expr;
1336+
result = false;
1337+
}
13051338
}
13061339

13071340
return result;
@@ -2079,9 +2112,7 @@ simplify_exprt::simplify_byte_extract(const byte_extract_exprt &expr)
20792112
auto tmp = expr;
20802113
tmp.op() = index_exprt(expr.op(), expr.offset());
20812114
tmp.offset() = from_integer(0, expr.offset().type());
2082-
simplify_rec(tmp);
2083-
2084-
return std::move(tmp);
2115+
return changed(simplify_rec(tmp));
20852116
}
20862117

20872118
// extract bits of a constant
@@ -2127,8 +2158,7 @@ simplify_exprt::simplify_byte_extract(const byte_extract_exprt &expr)
21272158
if(!subexpr.has_value() || subexpr.value() == expr)
21282159
return unchanged(expr);
21292160

2130-
simplify_rec(subexpr.value());
2131-
return subexpr.value();
2161+
return changed(simplify_rec(subexpr.value())); // recursive call
21322162
}
21332163

21342164
simplify_exprt::resultt<>
@@ -2351,16 +2381,7 @@ simplify_exprt::simplify_byte_update(const byte_update_exprt &expr)
23512381
}
23522382

23532383
if(result_expr.is_not_nil())
2354-
{
2355-
simplify_rec(result_expr);
2356-
return result_expr;
2357-
}
2358-
2359-
if(result_expr.is_not_nil())
2360-
{
2361-
simplify_rec(result_expr);
2362-
return result_expr;
2363-
}
2384+
return changed(simplify_rec(result_expr));
23642385
}
23652386

23662387
// replace elements of array or struct expressions, possibly using
@@ -2404,7 +2425,7 @@ simplify_exprt::simplify_byte_update(const byte_update_exprt &expr)
24042425
*offset_int + val_offset - m_offset_bits / 8, offset.type()),
24052426
new_val);
24062427

2407-
simplify_rec(*it);
2428+
*it = simplify_rec(*it); // recursive call
24082429

24092430
val_offset+=bytes_req;
24102431
}
@@ -2455,8 +2476,14 @@ bool simplify_exprt::simplify_node_preorder(exprt &expr)
24552476
if(expr.has_operands())
24562477
{
24572478
Forall_operands(it, expr)
2458-
if(!simplify_rec(*it)) // recursive call
2479+
{
2480+
auto r_it = simplify_rec(*it); // recursive call
2481+
if(r_it.has_changed())
2482+
{
2483+
*it = r_it.expr;
24592484
result=false;
2485+
}
2486+
}
24602487
}
24612488
}
24622489

@@ -2699,8 +2726,7 @@ bool simplify_exprt::simplify_node(exprt &expr)
26992726
return no_change;
27002727
}
27012728

2702-
/// \return returns true if expression unchanged; returns false if changed
2703-
bool simplify_exprt::simplify_rec(exprt &expr)
2729+
simplify_exprt::resultt<> simplify_exprt::simplify_rec(const exprt &expr)
27042730
{
27052731
// look up in cache
27062732

@@ -2746,34 +2772,45 @@ bool simplify_exprt::simplify_rec(exprt &expr)
27462772
#endif
27472773
#endif
27482774

2749-
if(!result)
2775+
if(result) // no change
27502776
{
2751-
POSTCONDITION(tmp.type() == expr.type());
2752-
expr.swap(tmp);
2777+
return unchanged(expr);
2778+
}
2779+
else // change, new expression is 'tmp'
2780+
{
2781+
POSTCONDITION(as_const(tmp).type() == expr.type());
27532782

2754-
#ifdef USE_CACHE
2783+
#ifdef USE_CACHE
27552784
// save in cache
2756-
cache_result.first->second=expr;
2757-
#endif
2758-
}
2785+
cache_result.first->second = tmp;
2786+
#endif
27592787

2760-
return result;
2788+
return std::move(tmp);
2789+
}
27612790
}
27622791

2792+
/// \return returns true if expression unchanged; returns false if changed
27632793
bool simplify_exprt::simplify(exprt &expr)
27642794
{
27652795
#ifdef DEBUG_ON_DEMAND
27662796
if(debug_on)
27672797
std::cout << "TO-SIMP " << format(expr) << "\n";
27682798
#endif
2769-
bool res=simplify_rec(expr);
2799+
auto result = simplify_rec(expr);
27702800
#ifdef DEBUG_ON_DEMAND
27712801
if(debug_on)
2772-
std::cout << "FULLSIMP " << format(expr) << "\n";
2802+
std::cout << "FULLSIMP " << format(result.expr) << "\n";
27732803
#endif
2774-
return res;
2804+
if(result.has_changed())
2805+
{
2806+
expr = result.expr;
2807+
return false; // change
2808+
}
2809+
else
2810+
return true; // no change
27752811
}
27762812

2813+
/// \return returns true if expression unchanged; returns false if changed
27772814
bool simplify(exprt &expr, const namespacet &ns)
27782815
{
27792816
return simplify_exprt(ns).simplify(expr);

src/util/simplify_expr_array.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ simplify_exprt::resultt<> simplify_exprt::simplify_index(const exprt &expr)
5959
{
6060
exprt tmp = comprehension.body();
6161
replace_expr(comprehension.arg(), index, tmp);
62-
simplify_rec(tmp);
63-
return std::move(tmp);
62+
return changed(simplify_rec(tmp));
6463
}
6564
}
6665
else if(array.id()==ID_with)
@@ -188,9 +187,7 @@ simplify_exprt::resultt<> simplify_exprt::simplify_index(const exprt &expr)
188187
exprt result_expr(array.id(), expr.type());
189188
result_expr.add_to_operands(array.op0(), final_offset);
190189

191-
simplify_rec(result_expr);
192-
193-
return std::move(result_expr);
190+
return changed(simplify_rec(result_expr));
194191
}
195192
}
196193
else if(array.id()==ID_if)
@@ -204,9 +201,7 @@ simplify_exprt::resultt<> simplify_exprt::simplify_index(const exprt &expr)
204201
index_expr.array() = if_expr.true_case();
205202

206203
exprt result = if_exprt(cond, index_expr, idx_false, expr.type());
207-
simplify_rec(result);
208-
209-
return std::move(result);
204+
return changed(simplify_rec(result));
210205
}
211206

212207
if(no_change)

src/util/simplify_expr_class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class simplify_exprt
205205
// main recursion
206206
bool simplify_node(exprt &expr);
207207
bool simplify_node_preorder(exprt &expr);
208-
bool simplify_rec(exprt &expr);
208+
NODISCARD resultt<> simplify_rec(const exprt &);
209209

210210
virtual bool simplify(exprt &expr);
211211

src/util/simplify_expr_pointer.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ simplify_exprt::simplify_address_of_arg(const exprt &expr)
6969
new_index_expr.array() = array_result.expr;
7070
}
7171

72-
if(!simplify_rec(new_index_expr.index()))
72+
auto index_result = simplify_rec(new_index_expr.index());
73+
74+
if(index_result.has_changed())
75+
{
7376
no_change = false;
77+
new_index_expr.index() = index_result.expr;
78+
}
7479

7580
// rewrite (*(type *)int) [index] by
7681
// pushing the index inside
@@ -148,17 +153,25 @@ simplify_exprt::simplify_address_of_arg(const exprt &expr)
148153
else if(expr.id()==ID_dereference)
149154
{
150155
auto new_expr = to_dereference_expr(expr);
151-
if(!simplify_rec(new_expr.pointer()))
156+
auto r_pointer = simplify_rec(new_expr.pointer());
157+
if(r_pointer.has_changed())
158+
{
159+
new_expr.pointer() = r_pointer.expr;
152160
return std::move(new_expr);
161+
}
153162
}
154163
else if(expr.id()==ID_if)
155164
{
156165
auto new_if_expr = to_if_expr(expr);
157166

158167
bool no_change = true;
159168

160-
if(!simplify_rec(new_if_expr.cond()))
169+
auto r_cond = simplify_rec(new_if_expr.cond());
170+
if(r_cond.has_changed())
171+
{
172+
new_if_expr.cond() = r_cond.expr;
161173
no_change = false;
174+
}
162175

163176
auto true_result = simplify_address_of_arg(new_if_expr.true_case());
164177
if(true_result.has_changed())
@@ -547,9 +560,7 @@ simplify_exprt::simplify_pointer_object(const exprt &expr)
547560
p_o_true.op0() = if_expr.true_case();
548561

549562
auto new_expr = if_exprt(cond, p_o_true, p_o_false, expr.type());
550-
simplify_rec(new_expr);
551-
552-
return new_expr;
563+
return changed(simplify_rec(new_expr));
553564
}
554565

555566
if(op_result.has_changed())

0 commit comments

Comments
 (0)