@@ -535,7 +535,7 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
535
535
from_integer (*sub_size, size_type ()),
536
536
typecast_exprt (expr.op ().op1 (), size_type ()));
537
537
538
- simplify_rec (new_expr.op ()); // rec. call
538
+ new_expr. op () = simplify_rec (new_expr.op ()); // rec. call
539
539
540
540
return changed (simplify_typecast (new_expr)); // rec. call
541
541
}
@@ -619,8 +619,7 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
619
619
}
620
620
}
621
621
622
- simplify_rec (new_expr);
623
- return std::move (new_expr);
622
+ return changed (simplify_rec (new_expr)); // recursive call
624
623
}
625
624
}
626
625
@@ -959,8 +958,7 @@ simplify_exprt::simplify_typecast(const typecast_exprt &expr)
959
958
auto result =
960
959
address_of_exprt (index_exprt (o, from_integer (0 , size_type ())));
961
960
962
- simplify_rec (result);
963
- return std::move (result);
961
+ return changed (simplify_rec (result)); // recursive call
964
962
}
965
963
}
966
964
@@ -996,8 +994,7 @@ simplify_exprt::simplify_dereference(const dereference_exprt &expr)
996
994
{
997
995
exprt tmp=to_address_of_expr (pointer).object ();
998
996
// one address_of is gone, try again
999
- simplify_rec (tmp);
1000
- return tmp;
997
+ return changed (simplify_rec (tmp));
1001
998
}
1002
999
// rewrite *(&a[0] + x) to a[x]
1003
1000
else if (pointer.id ()==ID_plus &&
@@ -1015,8 +1012,7 @@ simplify_exprt::simplify_dereference(const dereference_exprt &expr)
1015
1012
old.array (),
1016
1013
pointer_offset_sum (old.index (), pointer.op1 ()),
1017
1014
old.array ().type ().subtype ());
1018
- simplify_rec (idx);
1019
- return idx;
1015
+ return changed (simplify_rec (idx));
1020
1016
}
1021
1017
}
1022
1018
}
@@ -1179,9 +1175,9 @@ bool simplify_exprt::simplify_if_branch(
1179
1175
}
1180
1176
1181
1177
if (!tresult)
1182
- simplify_rec (trueexpr);
1178
+ trueexpr = simplify_rec (trueexpr); // recursive call
1183
1179
if (!fresult)
1184
- simplify_rec (falseexpr);
1180
+ falseexpr = simplify_rec (falseexpr); // recursive call
1185
1181
1186
1182
return tresult && fresult;
1187
1183
}
@@ -1214,7 +1210,7 @@ bool simplify_exprt::simplify_if_cond(exprt &expr)
1214
1210
}
1215
1211
1216
1212
if (!tmp)
1217
- simplify_rec (expr);
1213
+ expr = simplify_rec (expr); // recursive call
1218
1214
1219
1215
result=tmp && result;
1220
1216
}
@@ -1228,14 +1224,21 @@ bool simplify_exprt::simplify_if_preorder(if_exprt &expr)
1228
1224
exprt &truevalue=expr.true_case ();
1229
1225
exprt &falsevalue=expr.false_case ();
1230
1226
1227
+ bool result = true ;
1228
+
1231
1229
// 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
+ }
1233
1236
1234
1237
// 1 ? a : b -> a and 0 ? a : b -> b
1235
1238
if (cond.is_constant ())
1236
1239
{
1237
1240
exprt tmp=cond.is_true ()?truevalue:falsevalue;
1238
- simplify_rec (tmp);
1241
+ tmp = simplify_rec (tmp);
1239
1242
expr.swap (tmp);
1240
1243
return false ;
1241
1244
}
@@ -1270,7 +1273,12 @@ bool simplify_exprt::simplify_if_preorder(if_exprt &expr)
1270
1273
else
1271
1274
local_replace_map.insert (std::make_pair (cond, true_exprt ()));
1272
1275
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
+ }
1274
1282
1275
1283
local_replace_map=map_before;
1276
1284
@@ -1290,18 +1298,43 @@ bool simplify_exprt::simplify_if_preorder(if_exprt &expr)
1290
1298
else
1291
1299
local_replace_map.insert (std::make_pair (cond, false_exprt ()));
1292
1300
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
+ }
1294
1307
1295
1308
local_replace_map.swap (map_before);
1296
1309
#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
+ }
1299
1322
#endif
1300
1323
}
1301
1324
else
1302
1325
{
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
+ }
1305
1338
}
1306
1339
1307
1340
return result;
@@ -2079,9 +2112,7 @@ simplify_exprt::simplify_byte_extract(const byte_extract_exprt &expr)
2079
2112
auto tmp = expr;
2080
2113
tmp.op () = index_exprt (expr.op (), expr.offset ());
2081
2114
tmp.offset () = from_integer (0 , expr.offset ().type ());
2082
- simplify_rec (tmp);
2083
-
2084
- return std::move (tmp);
2115
+ return changed (simplify_rec (tmp));
2085
2116
}
2086
2117
2087
2118
// extract bits of a constant
@@ -2127,8 +2158,7 @@ simplify_exprt::simplify_byte_extract(const byte_extract_exprt &expr)
2127
2158
if (!subexpr.has_value () || subexpr.value () == expr)
2128
2159
return unchanged (expr);
2129
2160
2130
- simplify_rec (subexpr.value ());
2131
- return subexpr.value ();
2161
+ return changed (simplify_rec (subexpr.value ())); // recursive call
2132
2162
}
2133
2163
2134
2164
simplify_exprt::resultt<>
@@ -2351,16 +2381,7 @@ simplify_exprt::simplify_byte_update(const byte_update_exprt &expr)
2351
2381
}
2352
2382
2353
2383
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));
2364
2385
}
2365
2386
2366
2387
// replace elements of array or struct expressions, possibly using
@@ -2404,7 +2425,7 @@ simplify_exprt::simplify_byte_update(const byte_update_exprt &expr)
2404
2425
*offset_int + val_offset - m_offset_bits / 8 , offset.type ()),
2405
2426
new_val);
2406
2427
2407
- simplify_rec (*it);
2428
+ *it = simplify_rec (*it); // recursive call
2408
2429
2409
2430
val_offset+=bytes_req;
2410
2431
}
@@ -2455,8 +2476,14 @@ bool simplify_exprt::simplify_node_preorder(exprt &expr)
2455
2476
if (expr.has_operands ())
2456
2477
{
2457
2478
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 ;
2459
2484
result=false ;
2485
+ }
2486
+ }
2460
2487
}
2461
2488
}
2462
2489
@@ -2699,8 +2726,7 @@ bool simplify_exprt::simplify_node(exprt &expr)
2699
2726
return no_change;
2700
2727
}
2701
2728
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)
2704
2730
{
2705
2731
// look up in cache
2706
2732
@@ -2746,34 +2772,45 @@ bool simplify_exprt::simplify_rec(exprt &expr)
2746
2772
#endif
2747
2773
#endif
2748
2774
2749
- if (!result)
2775
+ if (result) // no change
2776
+ {
2777
+ return unchanged (expr);
2778
+ }
2779
+ else // change, new expression is 'tmp'
2750
2780
{
2751
2781
POSTCONDITION (tmp.type () == expr.type ());
2752
- expr.swap (tmp);
2753
2782
2754
- #ifdef USE_CACHE
2783
+ #ifdef USE_CACHE
2755
2784
// save in cache
2756
- cache_result.first ->second =expr;
2757
- #endif
2758
- }
2785
+ cache_result.first ->second = tmp;
2786
+ #endif
2759
2787
2760
- return result;
2788
+ return std::move (tmp);
2789
+ }
2761
2790
}
2762
2791
2792
+ // / \return returns true if expression unchanged; returns false if changed
2763
2793
bool simplify_exprt::simplify (exprt &expr)
2764
2794
{
2765
2795
#ifdef DEBUG_ON_DEMAND
2766
2796
if (debug_on)
2767
2797
std::cout << " TO-SIMP " << format (expr) << " \n " ;
2768
2798
#endif
2769
- bool res= simplify_rec (expr);
2799
+ auto result = simplify_rec (expr);
2770
2800
#ifdef DEBUG_ON_DEMAND
2771
2801
if (debug_on)
2772
- std::cout << " FULLSIMP " << format (expr) << " \n " ;
2802
+ std::cout << " FULLSIMP " << format (result. expr ) << " \n " ;
2773
2803
#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
2775
2811
}
2776
2812
2813
+ // / \return returns true if expression unchanged; returns false if changed
2777
2814
bool simplify (exprt &expr, const namespacet &ns)
2778
2815
{
2779
2816
return simplify_exprt (ns).simplify (expr);
0 commit comments