Skip to content

Commit 07f7d1e

Browse files
Use to_plus_expr instead of assert in simplify_plus
to_plus_expr should check if an expression is a plus_exprt, this avoids duplicating checks across the codebase
1 parent 8b95abe commit 07f7d1e

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/util/simplify_expr_int.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -446,20 +446,19 @@ bool simplify_exprt::simplify_mod(exprt &expr)
446446

447447
bool simplify_exprt::simplify_plus(exprt &expr)
448448
{
449-
if(!is_number(expr.type()) &&
450-
expr.type().id()!=ID_pointer)
449+
auto const& plus_expr = to_plus_expr(expr);
450+
if(!is_number(plus_expr.type()) &&
451+
plus_expr.type().id()!=ID_pointer)
451452
return true;
452453

453454
bool result=true;
454455

455456
exprt::operandst &operands=expr.operands();
456457

457-
assert(expr.id()==ID_plus);
458-
459458
// floating-point addition is _NOT_ associative; thus,
460459
// there is special case for float
461460

462-
if(ns.follow(expr.type()).id()==ID_floatbv)
461+
if(ns.follow(plus_expr.type()).id()==ID_floatbv)
463462
{
464463
// we only merge neighboring constants!
465464
Forall_expr(it, operands)
@@ -482,14 +481,14 @@ bool simplify_exprt::simplify_plus(exprt &expr)
482481
else
483482
{
484483
// ((T*)p+a)+b -> (T*)p+(a+b)
485-
if(expr.type().id()==ID_pointer &&
486-
expr.operands().size()==2 &&
487-
expr.op0().id()==ID_plus &&
488-
expr.op0().operands().size()==2)
484+
if(plus_expr.type().id()==ID_pointer &&
485+
plus_expr.operands().size()==2 &&
486+
plus_expr.op0().id()==ID_plus &&
487+
plus_expr.op0().operands().size()==2)
489488
{
490-
exprt op0=expr.op0();
489+
exprt op0=plus_expr.op0();
491490

492-
if(expr.op0().op1().id()==ID_plus)
491+
if(plus_expr.op0().op1().id()==ID_plus)
493492
op0.op1().copy_to_operands(expr.op1());
494493
else
495494
op0.op1()=plus_exprt(op0.op1(), expr.op1());
@@ -504,7 +503,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
504503

505504
// count the constants
506505
size_t count=0;
507-
forall_operands(it, expr)
506+
forall_operands(it, plus_expr)
508507
if(is_number(it->type()) && it->is_constant())
509508
count++;
510509

@@ -588,7 +587,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
588587

589588
if(operands.empty())
590589
{
591-
expr=from_integer(0, expr.type());
590+
expr=from_integer(0, plus_expr.type());
592591
CHECK_RETURN(expr.is_not_nil());
593592
return false;
594593
}

0 commit comments

Comments
 (0)