@@ -43,7 +43,10 @@ bool simplify_exprt::simplify_bswap(bswap_exprt &expr)
43
43
mp_integer new_value=0 ;
44
44
for (std::size_t bit = 0 ; bit < width; bit += bits_per_byte)
45
45
{
46
- assert (!bytes.empty ());
46
+ INVARIANT (
47
+ !bytes.empty (),
48
+ " bytes is not empty because we just pushed just as many elements on "
49
+ " top of it as we are popping now" );
47
50
new_value+=bytes.back ()<<bit;
48
51
bytes.pop_back ();
49
52
}
@@ -180,7 +183,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
180
183
exprt::operandst::iterator constant;
181
184
182
185
// true if we have found a constant
183
- bool found = false ;
186
+ bool constant_found = false ;
184
187
185
188
typet c_sizeof_type=nil_typet ();
186
189
@@ -212,7 +215,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
212
215
c_sizeof_type=
213
216
static_cast <const typet &>(it->find (ID_C_c_sizeof_type));
214
217
215
- if (found )
218
+ if (constant_found )
216
219
{
217
220
// update the constant factor
218
221
if (!mul_expr (to_constant_expr (*constant), to_constant_expr (*it)))
@@ -222,7 +225,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
222
225
{
223
226
// set it as the constant factor if this is the first
224
227
constant=it;
225
- found= true ;
228
+ constant_found = true ;
226
229
}
227
230
}
228
231
@@ -238,7 +241,10 @@ bool simplify_exprt::simplify_mult(exprt &expr)
238
241
239
242
if (c_sizeof_type.is_not_nil ())
240
243
{
241
- assert (found);
244
+ INVARIANT (
245
+ constant_found,
246
+ " c_sizeof_type is only set to a non-nil value "
247
+ " if a constant has been found" );
242
248
constant->set (ID_C_c_sizeof_type, c_sizeof_type);
243
249
}
244
250
@@ -252,7 +258,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
252
258
else
253
259
{
254
260
// if the constant is a one and there are other factors
255
- if (found && constant->is_one ())
261
+ if (constant_found && constant->is_one ())
256
262
{
257
263
// just delete it
258
264
operands.erase (constant);
@@ -440,20 +446,18 @@ bool simplify_exprt::simplify_mod(exprt &expr)
440
446
441
447
bool simplify_exprt::simplify_plus (exprt &expr)
442
448
{
443
- if (! is_number ( expr. type ()) &&
444
- expr .type ().id ()!= ID_pointer)
449
+ auto const &plus_expr = to_plus_expr ( expr);
450
+ if (! is_number (plus_expr. type ()) && plus_expr .type ().id () != ID_pointer)
445
451
return true ;
446
452
447
453
bool result=true ;
448
454
449
455
exprt::operandst &operands=expr.operands ();
450
456
451
- assert (expr.id ()==ID_plus);
452
-
453
457
// floating-point addition is _NOT_ associative; thus,
454
458
// there is special case for float
455
459
456
- if (ns.follow (expr .type ()).id ()== ID_floatbv)
460
+ if (ns.follow (plus_expr .type ()).id () == ID_floatbv)
457
461
{
458
462
// we only merge neighboring constants!
459
463
Forall_expr (it, operands)
@@ -476,14 +480,13 @@ bool simplify_exprt::simplify_plus(exprt &expr)
476
480
else
477
481
{
478
482
// ((T*)p+a)+b -> (T*)p+(a+b)
479
- if (expr.type ().id ()==ID_pointer &&
480
- expr.operands ().size ()==2 &&
481
- expr.op0 ().id ()==ID_plus &&
482
- expr.op0 ().operands ().size ()==2 )
483
+ if (
484
+ plus_expr.type ().id () == ID_pointer && plus_expr.operands ().size () == 2 &&
485
+ plus_expr.op0 ().id () == ID_plus && plus_expr.op0 ().operands ().size () == 2 )
483
486
{
484
- exprt op0=expr .op0 ();
487
+ exprt op0 = plus_expr .op0 ();
485
488
486
- if (expr .op0 ().op1 ().id ()== ID_plus)
489
+ if (plus_expr .op0 ().op1 ().id () == ID_plus)
487
490
op0.op1 ().copy_to_operands (expr.op1 ());
488
491
else
489
492
op0.op1 ()=plus_exprt (op0.op1 (), expr.op1 ());
@@ -498,7 +501,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
498
501
499
502
// count the constants
500
503
size_t count=0 ;
501
- forall_operands (it, expr )
504
+ forall_operands (it, plus_expr )
502
505
if (is_number (it->type ()) && it->is_constant ())
503
506
count++;
504
507
@@ -523,7 +526,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
523
526
to_constant_expr (*it)))
524
527
{
525
528
*it=from_integer (0 , it->type ());
526
- assert (it->is_not_nil ());
529
+ CHECK_RETURN (it->is_not_nil ());
527
530
result=false ;
528
531
}
529
532
}
@@ -556,7 +559,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
556
559
{
557
560
*(itm->second )=from_integer (0 , expr.type ());
558
561
*it=from_integer (0 , expr.type ());
559
- assert (it->is_not_nil ());
562
+ CHECK_RETURN (it->is_not_nil ());
560
563
expr_map.erase (itm);
561
564
result=false ;
562
565
}
@@ -582,8 +585,8 @@ bool simplify_exprt::simplify_plus(exprt &expr)
582
585
583
586
if (operands.empty ())
584
587
{
585
- expr= from_integer (0 , expr .type ());
586
- assert (expr.is_not_nil ());
588
+ expr = from_integer (0 , plus_expr .type ());
589
+ CHECK_RETURN (expr.is_not_nil ());
587
590
return false ;
588
591
}
589
592
else if (operands.size ()==1 )
@@ -598,55 +601,53 @@ bool simplify_exprt::simplify_plus(exprt &expr)
598
601
599
602
bool simplify_exprt::simplify_minus (exprt &expr)
600
603
{
601
- if (! is_number ( expr. type ()) &&
602
- expr .type ().id ()!= ID_pointer)
604
+ auto const &minus_expr = to_minus_expr ( expr);
605
+ if (! is_number (minus_expr. type ()) && minus_expr .type ().id () != ID_pointer)
603
606
return true ;
604
607
605
- exprt::operandst &operands=expr.operands ();
606
-
607
- assert (expr.id ()==ID_minus);
608
+ const exprt::operandst &operands = minus_expr.operands ();
608
609
609
610
if (operands.size ()!=2 )
610
611
return true ;
611
612
612
- if (is_number (expr. type ()) &&
613
- is_number (operands[0 ].type ()) &&
614
- is_number (operands[1 ].type ()))
613
+ if (
614
+ is_number (minus_expr. type ()) && is_number (operands[0 ].type ()) &&
615
+ is_number (operands[1 ].type ()))
615
616
{
616
617
// rewrite "a-b" to "a+(-b)"
617
- unary_minus_exprt tmp2 (operands[1 ]);
618
- simplify_unary_minus (tmp2 );
618
+ unary_minus_exprt rhs_negated (operands[1 ]);
619
+ simplify_unary_minus (rhs_negated );
619
620
620
- plus_exprt tmp (operands[0 ], tmp2 );
621
- simplify_node (tmp );
621
+ plus_exprt plus_expr (operands[0 ], rhs_negated );
622
+ simplify_node (plus_expr );
622
623
623
- expr.swap (tmp );
624
+ expr.swap (plus_expr );
624
625
return false ;
625
626
}
626
- else if (expr. type (). id ()==ID_pointer &&
627
- operands[ 0 ] .type ().id ()== ID_pointer &&
628
- is_number (operands[1 ].type ()))
627
+ else if (
628
+ minus_expr .type ().id () == ID_pointer &&
629
+ operands[ 0 ]. type (). id () == ID_pointer && is_number (operands[1 ].type ()))
629
630
{
630
631
// pointer arithmetic: rewrite "p-i" to "p+(-i)"
631
- unary_minus_exprt tmp2 (operands[1 ]);
632
- simplify_unary_minus (tmp2 );
632
+ unary_minus_exprt negated_pointer_offset (operands[1 ]);
633
+ simplify_unary_minus (negated_pointer_offset );
633
634
634
- plus_exprt tmp (operands[0 ], tmp2 );
635
- simplify_plus (tmp );
635
+ plus_exprt pointer_offset_expr (operands[0 ], negated_pointer_offset );
636
+ simplify_plus (pointer_offset_expr );
636
637
637
- expr.swap (tmp );
638
+ expr.swap (pointer_offset_expr );
638
639
return false ;
639
640
}
640
- else if (is_number (expr. type ()) &&
641
- operands[0 ].type ().id ()== ID_pointer &&
642
- operands[1 ].type ().id ()== ID_pointer)
641
+ else if (
642
+ is_number (minus_expr. type ()) && operands[0 ].type ().id () == ID_pointer &&
643
+ operands[1 ].type ().id () == ID_pointer)
643
644
{
644
645
// pointer arithmetic: rewrite "p-p" to "0"
645
646
646
647
if (operands[0 ]==operands[1 ])
647
648
{
648
- exprt zero= from_integer (0 , expr .type ());
649
- assert (zero.is_not_nil ());
649
+ exprt zero = from_integer (0 , minus_expr .type ());
650
+ CHECK_RETURN (zero.is_not_nil ());
650
651
expr=zero;
651
652
return false ;
652
653
}
@@ -741,7 +742,9 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
741
742
if (expr.op1 ().type ()!=expr.type ())
742
743
break ;
743
744
744
- assert (a_str.size ()==b_str.size ());
745
+ INVARIANT (
746
+ a_str.size () == b_str.size (),
747
+ " bitvectors of the same type have the same size" );
745
748
746
749
std::string new_value;
747
750
new_value.resize (width);
@@ -847,32 +850,40 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
847
850
848
851
bool simplify_exprt::simplify_extractbit (exprt &expr)
849
852
{
850
- const typet &op0_type=expr.op0 ().type ();
853
+ PRECONDITION (expr.id () == ID_extractbit);
854
+ const auto &extractbit_expr = to_extractbit_expr (expr);
851
855
852
- if (!is_bitvector_type (op0_type))
853
- return true ;
856
+ const typet &src_type = extractbit_expr.src ().type ();
854
857
855
- std::size_t width=to_bitvector_type (op0_type).get_width ();
856
-
857
- assert (expr.operands ().size ()==2 );
858
+ if (!is_bitvector_type (src_type))
859
+ return true ;
858
860
859
- mp_integer i ;
861
+ const std:: size_t src_bit_width = to_bitvector_type (src_type). get_width () ;
860
862
861
- if (to_integer (expr.op1 (), i))
863
+ const auto index_converted_to_int =
864
+ numeric_cast<mp_integer>(extractbit_expr.index ());
865
+ if (!index_converted_to_int.has_value ())
866
+ {
862
867
return true ;
863
-
864
- if (!expr.op0 ().is_constant ())
868
+ }
869
+ const mp_integer index_as_int = index_converted_to_int.value ();
870
+ if (!extractbit_expr.src ().is_constant ())
865
871
return true ;
866
872
867
- if (i< 0 || i>=width )
873
+ if (index_as_int < 0 || index_as_int >= src_bit_width )
868
874
return true ;
869
875
870
- const irep_idt &value=expr.op0 ().get (ID_value);
876
+ const irep_idt &src_value =
877
+ to_constant_expr (extractbit_expr.src ()).get_value ();
878
+
879
+ std::string src_value_as_string = id2string (src_value);
871
880
872
- if (value .size ()!=width )
881
+ if (src_value_as_string .size () != src_bit_width )
873
882
return true ;
874
883
875
- bool bit=(id2string (value)[width-integer2size_t (i)-1 ]==' 1' );
884
+ const bool bit =
885
+ (src_value_as_string[src_bit_width -
886
+ numeric_cast_v<std::size_t >(index_as_int) - 1 ] == ' 1' );
876
887
877
888
expr.make_bool (bit);
878
889
@@ -1581,7 +1592,9 @@ bool simplify_exprt::simplify_inequality_not_constant(exprt &expr)
1581
1592
1582
1593
// now we only have >=, =
1583
1594
1584
- assert (expr.id ()==ID_ge || expr.id ()==ID_equal);
1595
+ INVARIANT (
1596
+ expr.id () == ID_ge || expr.id () == ID_equal,
1597
+ " we previously converted all other cases to >= or ==" );
1585
1598
1586
1599
// syntactically equal?
1587
1600
@@ -1663,7 +1676,7 @@ bool simplify_exprt::simplify_inequality_not_constant(exprt &expr)
1663
1676
bool simplify_exprt::simplify_inequality_constant (exprt &expr)
1664
1677
{
1665
1678
// the constant is always on the RHS
1666
- assert (expr.op1 ().is_constant ());
1679
+ PRECONDITION (expr.op1 ().is_constant ());
1667
1680
1668
1681
if (expr.op0 ().id ()==ID_if && expr.op0 ().operands ().size ()==3 )
1669
1682
{
@@ -1767,7 +1780,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
1767
1780
{
1768
1781
constant+=i;
1769
1782
*it=from_integer (0 , it->type ());
1770
- assert (it->is_not_nil ());
1783
+ CHECK_RETURN (it->is_not_nil ());
1771
1784
changed=true ;
1772
1785
}
1773
1786
}
@@ -1907,9 +1920,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
1907
1920
}
1908
1921
else if (expr.id ()==ID_gt)
1909
1922
{
1910
- mp_integer i;
1911
- if (to_integer (expr.op1 (), i))
1912
- throw " bit-vector constant unexpectedly non-integer" ;
1923
+ mp_integer i = numeric_cast_v<mp_integer>(expr.op1 ());
1913
1924
1914
1925
if (i==max)
1915
1926
{
@@ -1933,9 +1944,7 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
1933
1944
}
1934
1945
else if (expr.id ()==ID_le)
1935
1946
{
1936
- mp_integer i;
1937
- if (to_integer (expr.op1 (), i))
1938
- throw " bit-vector constant unexpectedly non-integer" ;
1947
+ mp_integer i = numeric_cast_v<mp_integer>(expr.op1 ());
1939
1948
1940
1949
if (i==max)
1941
1950
{
0 commit comments