Skip to content

Commit a168b24

Browse files
Refactor simplify_floatbv_typecast
* Use to_XXX_expr functions instead of asserts to make sure invariants are not violated * Use named accessors instead of op0, op1 etc * Rename variables (e.g. "casted_expr" instead of "op0") for readability
1 parent bf19b39 commit a168b24

File tree

1 file changed

+53
-57
lines changed

1 file changed

+53
-57
lines changed

src/util/simplify_expr_floatbv.cpp

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Daniel Kroening, [email protected]
1313
#include "ieee_float.h"
1414
#include "invariant.h"
1515
#include "namespace.h"
16+
#include "simplify_expr.h"
1617
#include "std_expr.h"
1718

1819
bool simplify_exprt::simplify_isinf(exprt &expr)
@@ -141,43 +142,43 @@ bool simplify_exprt::simplify_floatbv_typecast(exprt &expr)
141142
{
142143
// These casts usually reduce precision, and thus, usually round.
143144

144-
assert(expr.operands().size()==2);
145+
auto const &floatbv_typecast_expr = to_floatbv_typecast_expr(expr);
145146

146-
const typet &dest_type=ns.follow(expr.type());
147-
const typet &src_type=ns.follow(expr.op0().type());
147+
const typet &dest_type = ns.follow(floatbv_typecast_expr.type());
148+
const typet &src_type = ns.follow(floatbv_typecast_expr.op().type());
148149

149150
// eliminate redundant casts
150151
if(dest_type==src_type)
151152
{
152-
expr=expr.op0();
153+
expr = floatbv_typecast_expr.op();
153154
return false;
154155
}
155156

156-
exprt op0=expr.op0();
157-
exprt op1=expr.op1(); // rounding mode
157+
const exprt &casted_expr = floatbv_typecast_expr.op();
158+
const exprt &rounding_mode = floatbv_typecast_expr.rounding_mode();
158159

159160
// We can soundly re-write (float)((double)x op (double)y)
160161
// to x op y. True for any rounding mode!
161162

162163
#if 0
163-
if(op0.id()==ID_floatbv_div ||
164-
op0.id()==ID_floatbv_mult ||
165-
op0.id()==ID_floatbv_plus ||
166-
op0.id()==ID_floatbv_minus)
164+
if(casted_expr.id()==ID_floatbv_div ||
165+
casted_expr.id()==ID_floatbv_mult ||
166+
casted_expr.id()==ID_floatbv_plus ||
167+
casted_expr.id()==ID_floatbv_minus)
167168
{
168-
if(op0.operands().size()==3 &&
169-
op0.op0().id()==ID_typecast &&
170-
op0.op1().id()==ID_typecast &&
171-
op0.op0().operands().size()==1 &&
172-
op0.op1().operands().size()==1 &&
173-
ns.follow(op0.op0().type())==dest_type &&
174-
ns.follow(op0.op1().type())==dest_type)
169+
if(casted_expr.operands().size()==3 &&
170+
casted_expr.op0().id()==ID_typecast &&
171+
casted_expr.op1().id()==ID_typecast &&
172+
casted_expr.op0().operands().size()==1 &&
173+
casted_expr.op1().operands().size()==1 &&
174+
ns.follow(casted_expr.op0().type())==dest_type &&
175+
ns.follow(casted_expr.op1().type())==dest_type)
175176
{
176-
exprt result(op0.id(), expr.type());
177+
exprt result(casted_expr.id(), floatbv_typecast_expr.type());
177178
result.operands().resize(3);
178-
result.op0()=op0.op0().op0();
179-
result.op1()=op0.op1().op0();
180-
result.op2()=op1;
179+
result.op0()=casted_expr.op0().op0();
180+
result.op1()=casted_expr.op1().op0();
181+
result.op2()=rounding_mode;
181182

182183
simplify_node(result);
183184
expr.swap(result);
@@ -187,18 +188,18 @@ bool simplify_exprt::simplify_floatbv_typecast(exprt &expr)
187188
#endif
188189

189190
// constant folding
190-
if(op0.is_constant() && op1.is_constant())
191+
if(casted_expr.is_constant() && rounding_mode.is_constant())
191192
{
192-
mp_integer rounding_mode;
193-
if(!to_integer(op1, rounding_mode))
193+
mp_integer rounding_mode_index;
194+
if(!to_integer(rounding_mode, rounding_mode_index))
194195
{
195196
if(src_type.id()==ID_floatbv)
196197
{
197198
if(dest_type.id()==ID_floatbv) // float to float
198199
{
199-
ieee_floatt result(to_constant_expr(op0));
200-
result.rounding_mode=
201-
(ieee_floatt::rounding_modet)integer2size_t(rounding_mode);
200+
ieee_floatt result(to_constant_expr(casted_expr));
201+
result.rounding_mode =
202+
(ieee_floatt::rounding_modet)integer2size_t(rounding_mode_index);
202203
result.change_spec(
203204
ieee_float_spect(to_floatbv_type(dest_type)));
204205
expr=result.to_expr();
@@ -207,11 +208,11 @@ bool simplify_exprt::simplify_floatbv_typecast(exprt &expr)
207208
else if(dest_type.id()==ID_signedbv ||
208209
dest_type.id()==ID_unsignedbv)
209210
{
210-
if(rounding_mode==ieee_floatt::ROUND_TO_ZERO)
211+
if(rounding_mode_index == ieee_floatt::ROUND_TO_ZERO)
211212
{
212-
ieee_floatt result(to_constant_expr(op0));
213-
result.rounding_mode=
214-
(ieee_floatt::rounding_modet)integer2size_t(rounding_mode);
213+
ieee_floatt result(to_constant_expr(casted_expr));
214+
result.rounding_mode =
215+
(ieee_floatt::rounding_modet)integer2size_t(rounding_mode_index);
215216
mp_integer value=result.to_integer();
216217
expr=from_integer(value, dest_type);
217218
return false;
@@ -222,13 +223,13 @@ bool simplify_exprt::simplify_floatbv_typecast(exprt &expr)
222223
src_type.id()==ID_unsignedbv)
223224
{
224225
mp_integer value;
225-
if(!to_integer(op0, value))
226+
if(!to_integer(casted_expr, value))
226227
{
227228
if(dest_type.id()==ID_floatbv) // int to float
228229
{
229230
ieee_floatt result(to_floatbv_type(dest_type));
230-
result.rounding_mode=
231-
(ieee_floatt::rounding_modet)integer2size_t(rounding_mode);
231+
result.rounding_mode =
232+
(ieee_floatt::rounding_modet)integer2size_t(rounding_mode_index);
232233
result.from_integer(value);
233234
expr=result.to_expr();
234235
return false;
@@ -239,15 +240,16 @@ bool simplify_exprt::simplify_floatbv_typecast(exprt &expr)
239240
{
240241
// go through underlying type
241242
const auto &enum_type = ns.follow_tag(to_c_enum_tag_type(src_type));
242-
typecast_exprt tmp1(op0, to_c_enum_type(enum_type).subtype());
243-
simplify_typecast(tmp1);
244-
if(tmp1.is_constant())
243+
exprt simplified_typecast = simplify_expr(
244+
typecast_exprt(casted_expr, to_c_enum_type(enum_type).subtype()), ns);
245+
if(simplified_typecast.is_constant())
245246
{
246-
exprt tmp2 = expr;
247-
tmp2.op0() = tmp1;
248-
if(!simplify_floatbv_typecast(tmp2))
247+
floatbv_typecast_exprt new_floatbv_typecast_expr =
248+
floatbv_typecast_expr;
249+
new_floatbv_typecast_expr.op() = simplified_typecast;
250+
if(!simplify_floatbv_typecast(new_floatbv_typecast_expr))
249251
{
250-
expr = tmp2;
252+
expr = new_floatbv_typecast_expr;
251253
return false;
252254
}
253255
}
@@ -257,23 +259,17 @@ bool simplify_exprt::simplify_floatbv_typecast(exprt &expr)
257259

258260
#if 0
259261
// (T)(a?b:c) --> a?(T)b:(T)c
260-
if(expr.op0().id()==ID_if &&
261-
expr.op0().operands().size()==3)
262+
if(casted_expr.id()==ID_if)
262263
{
263-
binary_exprt tmp_op1(
264-
expr.op0().op1(),
265-
ID_floatbv_typecast,
266-
expr.op1(),
267-
dest_type);
268-
binary_exprt tmp_op2(
269-
expr.op0().op2(),
270-
ID_floatbv_typecast,
271-
expr.op1(),
272-
dest_type);
273-
simplify_floatbv_typecast(tmp_op1);
274-
simplify_floatbv_typecast(tmp_op2);
275-
expr=if_exprt(expr.op0().op0(), tmp_op1, tmp_op2, dest_type);
276-
simplify_if(expr);
264+
auto const &casted_if_expr = to_if_expr(casted_expr);
265+
266+
floatbv_typecast_exprt casted_true_case(casted_if_expr.true_case(), rounding_mode, dest_type);
267+
floatbv_typecast_exprt casted_false_case(casted_if_expr.false_case(), rounding_mode, dest_type);
268+
269+
simplify_floatbv_typecast(casted_true_case);
270+
simplify_floatbv_typecast(casted_false_case);
271+
auto simplified_if_expr = simplify_expr(if_exprt(casted_if_expr.cond(), casted_true_case, casted_false_case, dest_type), ns);
272+
expr = simplified_if_expr;
277273
return false;
278274
}
279275
#endif

0 commit comments

Comments
 (0)