@@ -1106,7 +1106,7 @@ bool simplify_exprt::simplify_if_cond(exprt &expr)
1106
1106
1107
1107
/* ******************************************************************\
1108
1108
1109
- Function: simplify_exprt::simplify_if
1109
+ Function: simplify_exprt::simplify_if_preorder
1110
1110
1111
1111
Inputs:
1112
1112
@@ -1116,25 +1116,24 @@ Function: simplify_exprt::simplify_if
1116
1116
1117
1117
\*******************************************************************/
1118
1118
1119
- bool simplify_exprt::simplify_if (exprt &expr)
1119
+ bool simplify_exprt::simplify_if_preorder (if_exprt &expr)
1120
1120
{
1121
- exprt::operandst &operands=expr.operands ();
1122
- if (operands.size ()!=3 ) return true ;
1121
+ exprt &cond=expr.cond ();
1122
+ exprt &truevalue=expr.true_case ();
1123
+ exprt &falsevalue=expr.false_case ();
1123
1124
1124
- exprt &cond=operands[0 ];
1125
- exprt &truevalue=operands[1 ];
1126
- exprt &falsevalue=operands[2 ];
1125
+ // we first want to look at the condition
1126
+ bool result=simplify_rec (cond);
1127
1127
1128
- if (truevalue==falsevalue)
1128
+ // 1 ? a : b -> a and 0 ? a : b -> b
1129
+ if (cond.is_constant ())
1129
1130
{
1130
- exprt tmp;
1131
- tmp. swap (truevalue );
1131
+ exprt tmp=cond. is_true ()?truevalue:falsevalue ;
1132
+ simplify_rec (tmp );
1132
1133
expr.swap (tmp);
1133
1134
return false ;
1134
1135
}
1135
1136
1136
- bool result=true ;
1137
-
1138
1137
if (do_simplify_if)
1139
1138
{
1140
1139
if (cond.id ()==ID_not)
@@ -1146,6 +1145,84 @@ bool simplify_exprt::simplify_if(exprt &expr)
1146
1145
result=false ;
1147
1146
}
1148
1147
1148
+ #if 0
1149
+ replace_mapt map_before(local_replace_map);
1150
+
1151
+ // a ? b : c --> a ? b[a/true] : c
1152
+ if(cond.id()==ID_and)
1153
+ {
1154
+ forall_operands(it, cond)
1155
+ {
1156
+ if(it->id()==ID_not)
1157
+ local_replace_map.insert(
1158
+ std::make_pair(it->op0(), false_exprt()));
1159
+ else
1160
+ local_replace_map.insert(
1161
+ std::make_pair(*it, true_exprt()));
1162
+ }
1163
+ }
1164
+ else
1165
+ local_replace_map.insert(std::make_pair(cond, true_exprt()));
1166
+
1167
+ result=simplify_rec(truevalue) && result;
1168
+
1169
+ local_replace_map=map_before;
1170
+
1171
+ // a ? b : c --> a ? b : c[a/false]
1172
+ if(cond.id()==ID_or)
1173
+ {
1174
+ forall_operands(it, cond)
1175
+ {
1176
+ if(it->id()==ID_not)
1177
+ local_replace_map.insert(
1178
+ std::make_pair(it->op0(), true_exprt()));
1179
+ else
1180
+ local_replace_map.insert(
1181
+ std::make_pair(*it, false_exprt()));
1182
+ }
1183
+ }
1184
+ else
1185
+ local_replace_map.insert(std::make_pair(cond, false_exprt()));
1186
+
1187
+ result=simplify_rec(falsevalue) && result;
1188
+
1189
+ local_replace_map.swap(map_before);
1190
+ #else
1191
+ result=simplify_rec (truevalue) && result;
1192
+ result=simplify_rec (falsevalue) && result;
1193
+ #endif
1194
+ }
1195
+ else
1196
+ {
1197
+ result=simplify_rec (truevalue) && result;
1198
+ result=simplify_rec (falsevalue) && result;
1199
+ }
1200
+
1201
+ return result;
1202
+ }
1203
+
1204
+ /* ******************************************************************\
1205
+
1206
+ Function: simplify_exprt::simplify_if
1207
+
1208
+ Inputs:
1209
+
1210
+ Outputs:
1211
+
1212
+ Purpose:
1213
+
1214
+ \*******************************************************************/
1215
+
1216
+ bool simplify_exprt::simplify_if (if_exprt &expr)
1217
+ {
1218
+ exprt &cond=expr.cond ();
1219
+ exprt &truevalue=expr.true_case ();
1220
+ exprt &falsevalue=expr.false_case ();
1221
+
1222
+ bool result=true ;
1223
+
1224
+ if (do_simplify_if)
1225
+ {
1149
1226
#if 0
1150
1227
result = simplify_if_cond(cond) && result;
1151
1228
result = simplify_if_branch(truevalue, falsevalue, cond) && result;
@@ -1210,35 +1287,34 @@ bool simplify_exprt::simplify_if(exprt &expr)
1210
1287
return false ;
1211
1288
}
1212
1289
}
1213
-
1214
- #if 0
1215
- // a ? b : c --> a ? b[a/true] : c
1216
- exprt tmp_true=truevalue;
1217
- replace_expr(cond, true_exprt(), tmp_true);
1218
- if(tmp_true!=truevalue)
1219
- { truevalue=tmp_true; simplify_rec(truevalue); result=false; }
1220
-
1221
- // a ? b : c --> a ? b : c[a/false]
1222
- exprt tmp_false=falsevalue;
1223
- replace_expr(cond, false_exprt(), tmp_false);
1224
- if(tmp_false!=falsevalue)
1225
- { falsevalue=tmp_false; simplify_rec(falsevalue); result=false; }
1226
- #endif
1227
1290
}
1228
1291
1229
- if (cond. is_true () )
1292
+ if (truevalue==falsevalue )
1230
1293
{
1231
1294
exprt tmp;
1232
1295
tmp.swap (truevalue);
1233
1296
expr.swap (tmp);
1234
1297
return false ;
1235
1298
}
1236
1299
1237
- if (cond.is_false ())
1300
+ if (((truevalue.id ()==ID_struct && falsevalue.id ()==ID_struct) ||
1301
+ (truevalue.id ()==ID_array && falsevalue.id ()==ID_array)) &&
1302
+ truevalue.operands ().size ()==falsevalue.operands ().size ())
1238
1303
{
1239
- exprt tmp;
1240
- tmp.swap (falsevalue);
1241
- expr.swap (tmp);
1304
+ exprt cond_copy=cond;
1305
+ exprt falsevalue_copy=falsevalue;
1306
+ expr.swap (truevalue);
1307
+
1308
+ exprt::operandst::const_iterator f_it=
1309
+ falsevalue_copy.operands ().begin ();
1310
+ Forall_operands (it, expr)
1311
+ {
1312
+ if_exprt if_expr (cond_copy, *it, *f_it);
1313
+ it->swap (if_expr);
1314
+ simplify_if (to_if_expr (*it));
1315
+ ++f_it;
1316
+ }
1317
+
1242
1318
return false ;
1243
1319
}
1244
1320
@@ -2331,25 +2407,7 @@ bool simplify_exprt::simplify_node_preorder(exprt &expr)
2331
2407
// the argument of this expression needs special treatment
2332
2408
}
2333
2409
else if (expr.id ()==ID_if)
2334
- {
2335
- // we first want to look at the condition
2336
- if_exprt &if_expr=to_if_expr (expr);
2337
- if (!simplify_rec (if_expr.cond ())) result=false ;
2338
-
2339
- // 1 ? a : b -> a and 0 ? a : b -> b
2340
- if (if_expr.cond ().is_constant ())
2341
- {
2342
- expr=if_expr.cond ().is_true ()?
2343
- if_expr.true_case ():if_expr.false_case ();
2344
- simplify_rec (expr);
2345
- result=false ;
2346
- }
2347
- else
2348
- {
2349
- if (!simplify_rec (if_expr.true_case ())) result=false ;
2350
- if (!simplify_rec (if_expr.false_case ())) result=false ;
2351
- }
2352
- }
2410
+ result=simplify_if_preorder (to_if_expr (expr));
2353
2411
else
2354
2412
{
2355
2413
if (expr.has_operands ())
@@ -2545,6 +2603,22 @@ bool simplify_exprt::simplify_rec(exprt &expr)
2545
2603
2546
2604
if (!simplify_node (tmp)) result=false ;
2547
2605
2606
+ #if 1
2607
+ replace_mapt::const_iterator it=local_replace_map.find (tmp);
2608
+ if (it!=local_replace_map.end ())
2609
+ {
2610
+ tmp=it->second ;
2611
+ result=false ;
2612
+ }
2613
+ #else
2614
+ if(!local_replace_map.empty() &&
2615
+ !replace_expr(local_replace_map, tmp))
2616
+ {
2617
+ simplify_rec(tmp);
2618
+ result=false;
2619
+ }
2620
+ #endif
2621
+
2548
2622
if (!result)
2549
2623
{
2550
2624
expr.swap (tmp);
0 commit comments