@@ -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
21342164simplify_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
2776+ {
2777+ return unchanged (expr);
2778+ }
2779+ else // change, new expression is 'tmp'
27502780 {
27512781 POSTCONDITION (tmp.type () == expr.type ());
2752- expr.swap (tmp);
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
27632793bool 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
27772814bool simplify (exprt &expr, const namespacet &ns)
27782815{
27792816 return simplify_exprt (ns).simplify (expr);
0 commit comments