-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathsimplify_expr_int.cpp
2093 lines (1803 loc) · 55.3 KB
/
simplify_expr_int.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************\
Module:
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#include "simplify_expr_class.h"
#include "arith_tools.h"
#include "bitvector_expr.h"
#include "c_types.h"
#include "config.h"
#include "expr_util.h"
#include "fixedbv.h"
#include "ieee_float.h"
#include "invariant.h"
#include "mathematical_expr.h"
#include "mathematical_types.h"
#include "namespace.h"
#include "pointer_expr.h"
#include "pointer_offset_size.h"
#include "rational.h"
#include "rational_tools.h"
#include "simplify_utils.h"
#include "std_expr.h"
#include "threeval.h"
#include <algorithm>
simplify_exprt::resultt<>
simplify_exprt::simplify_bswap(const bswap_exprt &expr)
{
if(expr.type().id() == ID_unsignedbv && expr.op().is_constant())
{
auto bits_per_byte = expr.get_bits_per_byte();
std::size_t width=to_bitvector_type(expr.type()).get_width();
const mp_integer value =
numeric_cast_v<mp_integer>(to_constant_expr(expr.op()));
std::vector<mp_integer> bytes;
// take apart
for(std::size_t bit = 0; bit < width; bit += bits_per_byte)
bytes.push_back((value >> bit)%power(2, bits_per_byte));
// put back together, but backwards
mp_integer new_value=0;
for(std::size_t bit = 0; bit < width; bit += bits_per_byte)
{
INVARIANT(
!bytes.empty(),
"bytes is not empty because we just pushed just as many elements on "
"top of it as we are popping now");
new_value+=bytes.back()<<bit;
bytes.pop_back();
}
return from_integer(new_value, expr.type());
}
return unchanged(expr);
}
//! produce a sum of two constant expressions of the same type
//! \return 'false' iff this was successful
static bool sum_expr(
constant_exprt &dest,
const constant_exprt &expr)
{
if(dest.type()!=expr.type())
return true;
const irep_idt &type_id=dest.type().id();
if(
type_id == ID_integer || type_id == ID_natural ||
type_id == ID_unsignedbv || type_id == ID_signedbv)
{
mp_integer a, b;
if(!to_integer(dest, a) && !to_integer(expr, b))
{
dest = from_integer(a + b, dest.type());
return false;
}
}
else if(type_id==ID_rational)
{
rationalt a, b;
if(!to_rational(dest, a) && !to_rational(expr, b))
{
dest=from_rational(a+b);
return false;
}
}
else if(type_id==ID_fixedbv)
{
fixedbvt f(dest);
f += fixedbvt(expr);
dest = f.to_expr();
return false;
}
else if(type_id==ID_floatbv)
{
ieee_floatt f(dest);
f += ieee_floatt(expr);
dest=f.to_expr();
return false;
}
return true;
}
//! produce a product of two expressions of the same type
//! \return 'false' iff this was successful
static bool mul_expr(
constant_exprt &dest,
const constant_exprt &expr)
{
if(dest.type()!=expr.type())
return true;
const irep_idt &type_id=dest.type().id();
if(
type_id == ID_integer || type_id == ID_natural ||
type_id == ID_unsignedbv || type_id == ID_signedbv)
{
mp_integer a, b;
if(!to_integer(dest, a) && !to_integer(expr, b))
{
dest = from_integer(a * b, dest.type());
return false;
}
}
else if(type_id==ID_rational)
{
rationalt a, b;
if(!to_rational(dest, a) && !to_rational(expr, b))
{
dest=from_rational(a*b);
return false;
}
}
else if(type_id==ID_fixedbv)
{
fixedbvt f(to_constant_expr(dest));
f*=fixedbvt(to_constant_expr(expr));
dest=f.to_expr();
return false;
}
else if(type_id==ID_floatbv)
{
ieee_floatt f(to_constant_expr(dest));
f*=ieee_floatt(to_constant_expr(expr));
dest=f.to_expr();
return false;
}
return true;
}
simplify_exprt::resultt<> simplify_exprt::simplify_mult(const mult_exprt &expr)
{
// check to see if it is a number type
if(!is_number(expr.type()))
return unchanged(expr);
// vector of operands
exprt::operandst new_operands = expr.operands();
// result of the simplification
bool no_change = true;
// position of the constant
exprt::operandst::iterator constant;
// true if we have found a constant
bool constant_found = false;
std::optional<typet> c_sizeof_type;
// scan all the operands
for(exprt::operandst::iterator it = new_operands.begin();
it != new_operands.end();)
{
// if one of the operands is not a number return
if(!is_number(it->type()))
return unchanged(expr);
// if one of the operands is zero the result is zero
// note: not true on IEEE floating point arithmetic
if(it->is_zero() &&
it->type().id()!=ID_floatbv)
{
return from_integer(0, expr.type());
}
// true if the given operand has to be erased
bool do_erase = false;
// if this is a constant of the same time as the result
if(it->is_constant() && it->type()==expr.type())
{
// preserve the sizeof type annotation
if(!c_sizeof_type.has_value())
{
const typet &sizeof_type =
static_cast<const typet &>(it->find(ID_C_c_sizeof_type));
if(sizeof_type.is_not_nil())
c_sizeof_type = sizeof_type;
}
if(constant_found)
{
// update the constant factor
if(!mul_expr(to_constant_expr(*constant), to_constant_expr(*it)))
do_erase=true;
}
else
{
// set it as the constant factor if this is the first
constant=it;
constant_found = true;
}
}
// erase the factor if necessary
if(do_erase)
{
it = new_operands.erase(it);
no_change = false;
}
else
it++; // move to the next operand
}
if(c_sizeof_type.has_value())
{
INVARIANT(
constant_found,
"c_sizeof_type is only set to a non-nil value "
"if a constant has been found");
constant->set(ID_C_c_sizeof_type, *c_sizeof_type);
}
if(new_operands.size() == 1)
{
return new_operands.front();
}
else
{
// if the constant is a one and there are other factors
if(constant_found && constant->is_one())
{
// just delete it
new_operands.erase(constant);
no_change = false;
if(new_operands.size() == 1)
return new_operands.front();
}
}
if(no_change)
return unchanged(expr);
else
{
exprt tmp = expr;
tmp.operands() = std::move(new_operands);
return std::move(tmp);
}
}
simplify_exprt::resultt<> simplify_exprt::simplify_div(const div_exprt &expr)
{
if(!is_number(expr.type()))
return unchanged(expr);
const typet &expr_type=expr.type();
if(expr_type!=expr.op0().type() ||
expr_type!=expr.op1().type())
{
return unchanged(expr);
}
if(expr_type.id()==ID_signedbv ||
expr_type.id()==ID_unsignedbv ||
expr_type.id()==ID_natural ||
expr_type.id()==ID_integer)
{
const auto int_value0 = numeric_cast<mp_integer>(expr.op0());
const auto int_value1 = numeric_cast<mp_integer>(expr.op1());
// division by zero?
if(int_value1.has_value() && *int_value1 == 0)
return unchanged(expr);
// x/1?
if(int_value1.has_value() && *int_value1 == 1)
{
return expr.op0();
}
// 0/x?
if(int_value0.has_value() && *int_value0 == 0)
{
return expr.op0();
}
if(int_value0.has_value() && int_value1.has_value())
{
mp_integer result = *int_value0 / *int_value1;
return from_integer(result, expr_type);
}
}
else if(expr_type.id()==ID_rational)
{
rationalt rat_value0, rat_value1;
bool ok0, ok1;
ok0=!to_rational(expr.op0(), rat_value0);
ok1=!to_rational(expr.op1(), rat_value1);
if(ok1 && rat_value1.is_zero())
return unchanged(expr);
if((ok1 && rat_value1.is_one()) ||
(ok0 && rat_value0.is_zero()))
{
return expr.op0();
}
if(ok0 && ok1)
{
rationalt result=rat_value0/rat_value1;
exprt tmp=from_rational(result);
if(tmp.is_not_nil())
return std::move(tmp);
}
}
else if(expr_type.id()==ID_fixedbv)
{
// division by one?
if(expr.op1().is_constant() &&
expr.op1().is_one())
{
return expr.op0();
}
if(expr.op0().is_constant() &&
expr.op1().is_constant())
{
fixedbvt f0(to_constant_expr(expr.op0()));
fixedbvt f1(to_constant_expr(expr.op1()));
if(!f1.is_zero())
{
f0/=f1;
return f0.to_expr();
}
}
}
return unchanged(expr);
}
simplify_exprt::resultt<> simplify_exprt::simplify_mod(const mod_exprt &expr)
{
if(!is_number(expr.type()))
return unchanged(expr);
if(expr.type().id()==ID_signedbv ||
expr.type().id()==ID_unsignedbv ||
expr.type().id()==ID_natural ||
expr.type().id()==ID_integer)
{
if(expr.type()==expr.op0().type() &&
expr.type()==expr.op1().type())
{
const auto int_value0 = numeric_cast<mp_integer>(expr.op0());
const auto int_value1 = numeric_cast<mp_integer>(expr.op1());
if(int_value1.has_value() && *int_value1 == 0)
return unchanged(expr); // division by zero
if(
(int_value1.has_value() && *int_value1 == 1) ||
(int_value0.has_value() && *int_value0 == 0))
{
return from_integer(0, expr.type());
}
if(int_value0.has_value() && int_value1.has_value())
{
mp_integer result = *int_value0 % *int_value1;
return from_integer(result, expr.type());
}
}
}
return unchanged(expr);
}
simplify_exprt::resultt<> simplify_exprt::simplify_plus(const plus_exprt &expr)
{
if(!is_number(expr.type()) && expr.type().id() != ID_pointer)
return unchanged(expr);
bool no_change = true;
exprt::operandst new_operands = expr.operands();
// floating-point addition is _NOT_ associative; thus,
// there is special case for float
if(expr.type().id() == ID_floatbv)
{
// we only merge neighboring constants!
Forall_expr(it, new_operands)
{
const exprt::operandst::iterator next = std::next(it);
if(next != new_operands.end())
{
if(it->type()==next->type() &&
it->is_constant() &&
next->is_constant())
{
sum_expr(to_constant_expr(*it), to_constant_expr(*next));
new_operands.erase(next);
no_change = false;
}
}
}
}
else
{
// ((T*)p+a)+b -> (T*)p+(a+b)
if(
expr.type().id() == ID_pointer && expr.operands().size() == 2 &&
expr.op0().id() == ID_plus && expr.op0().type().id() == ID_pointer &&
expr.op0().operands().size() == 2)
{
plus_exprt result = to_plus_expr(expr.op0());
if(as_const(result).op0().type().id() != ID_pointer)
result.op0().swap(result.op1());
const exprt &op1 = as_const(result).op1();
if(op1.id() == ID_plus)
{
plus_exprt new_op1 = to_plus_expr(op1);
new_op1.add_to_operands(
typecast_exprt::conditional_cast(expr.op1(), new_op1.op0().type()));
result.op1() = simplify_plus(new_op1);
}
else
{
plus_exprt new_op1{
op1, typecast_exprt::conditional_cast(expr.op1(), op1.type())};
result.op1() = simplify_plus(new_op1);
}
return changed(simplify_plus(result));
}
// count the constants
size_t count=0;
for(const auto &op : expr.operands())
{
if(is_number(op.type()) && op.is_constant())
count++;
}
// merge constants?
if(count>=2)
{
exprt::operandst::iterator const_sum;
bool const_sum_set=false;
for(auto it = new_operands.begin(); it != new_operands.end(); it++)
{
if(is_number(it->type()) && it->is_constant())
{
if(!const_sum_set)
{
const_sum=it;
const_sum_set=true;
}
else
{
if(!sum_expr(to_constant_expr(*const_sum),
to_constant_expr(*it)))
{
*it=from_integer(0, it->type());
no_change = false;
}
}
}
}
}
// search for a and -a
// first gather all the a's with -a
typedef std::unordered_map<exprt, exprt::operandst::iterator, irep_hash>
expr_mapt;
expr_mapt expr_map;
for(auto it = new_operands.begin(); it != new_operands.end(); it++)
if(it->id() == ID_unary_minus)
{
expr_map.insert(std::make_pair(to_unary_minus_expr(*it).op(), it));
}
// now search for a
for(auto it = new_operands.begin(); it != new_operands.end(); it++)
{
if(expr_map.empty())
break;
else if(it->id()==ID_unary_minus)
continue;
expr_mapt::iterator itm=expr_map.find(*it);
if(itm!=expr_map.end())
{
*(itm->second)=from_integer(0, expr.type());
*it=from_integer(0, expr.type());
expr_map.erase(itm);
no_change = false;
}
}
// delete zeros
// (can't do for floats, as the result of 0.0 + (-0.0)
// need not be -0.0 in std rounding)
for(exprt::operandst::iterator it = new_operands.begin();
it != new_operands.end();
/* no it++ */)
{
if(is_number(it->type()) && it->is_zero())
{
it = new_operands.erase(it);
no_change = false;
}
else
it++;
}
}
if(new_operands.empty())
{
return from_integer(0, expr.type());
}
else if(new_operands.size() == 1)
{
return new_operands.front();
}
if(no_change)
return unchanged(expr);
else
{
auto tmp = expr;
tmp.operands() = std::move(new_operands);
return std::move(tmp);
}
}
simplify_exprt::resultt<>
simplify_exprt::simplify_minus(const minus_exprt &expr)
{
auto const &minus_expr = to_minus_expr(expr);
if(!is_number(minus_expr.type()) && minus_expr.type().id() != ID_pointer)
return unchanged(expr);
const exprt::operandst &operands = minus_expr.operands();
if(
is_number(minus_expr.type()) && is_number(operands[0].type()) &&
is_number(operands[1].type()))
{
// rewrite "a-b" to "a+(-b)"
unary_minus_exprt rhs_negated(operands[1]);
plus_exprt plus_expr(operands[0], simplify_unary_minus(rhs_negated));
return changed(simplify_plus(plus_expr));
}
else if(
minus_expr.type().id() == ID_pointer &&
operands[0].type().id() == ID_pointer && is_number(operands[1].type()))
{
// pointer arithmetic: rewrite "p-i" to "p+(-i)"
unary_minus_exprt negated_pointer_offset(operands[1]);
plus_exprt pointer_offset_expr(
operands[0], simplify_unary_minus(negated_pointer_offset));
return changed(simplify_plus(pointer_offset_expr));
}
else if(
is_number(minus_expr.type()) && operands[0].type().id() == ID_pointer &&
operands[1].type().id() == ID_pointer)
{
exprt ptr_op0 = simplify_object(operands[0]).expr;
exprt ptr_op1 = simplify_object(operands[1]).expr;
auto address_of = expr_try_dynamic_cast<address_of_exprt>(ptr_op0);
if(ptr_op0 == ptr_op1 && address_of)
{
exprt offset_op0 = simplify_pointer_offset(
pointer_offset_exprt{operands[0], minus_expr.type()});
exprt offset_op1 = simplify_pointer_offset(
pointer_offset_exprt{operands[1], minus_expr.type()});
const auto object_size =
pointer_offset_size(address_of->object().type(), ns);
auto element_size =
size_of_expr(to_pointer_type(operands[0].type()).base_type(), ns);
if(
offset_op0.is_constant() && offset_op1.is_constant() &&
object_size.has_value() && element_size.has_value() &&
element_size->is_constant() && !element_size->is_zero() &&
numeric_cast_v<mp_integer>(to_constant_expr(offset_op0)) <=
*object_size &&
numeric_cast_v<mp_integer>(to_constant_expr(offset_op1)) <=
*object_size)
{
return changed(simplify_rec(div_exprt{
minus_exprt{offset_op0, offset_op1},
typecast_exprt{*element_size, minus_expr.type()}}));
}
}
const exprt &ptr_op0_skipped_tc = skip_typecast(ptr_op0);
const exprt &ptr_op1_skipped_tc = skip_typecast(ptr_op1);
if(
is_number(ptr_op0_skipped_tc.type()) &&
is_number(ptr_op1_skipped_tc.type()))
{
exprt offset_op0 = simplify_pointer_offset(
pointer_offset_exprt{operands[0], minus_expr.type()});
exprt offset_op1 = simplify_pointer_offset(
pointer_offset_exprt{operands[1], minus_expr.type()});
auto element_size =
size_of_expr(to_pointer_type(operands[0].type()).base_type(), ns);
if(
element_size.has_value() && element_size->is_constant() &&
!element_size->is_zero())
{
return changed(simplify_rec(div_exprt{
minus_exprt{offset_op0, offset_op1},
typecast_exprt{*element_size, minus_expr.type()}}));
}
}
}
return unchanged(expr);
}
simplify_exprt::resultt<>
simplify_exprt::simplify_bitwise(const multi_ary_exprt &expr)
{
if(!can_cast_type<bitvector_typet>(expr.type()))
return unchanged(expr);
// check if these are really boolean
if(!expr.is_boolean())
{
bool all_bool=true;
for(const auto &op : expr.operands())
{
if(op.id() == ID_typecast && to_typecast_expr(op).op().is_boolean())
{
}
else if(op.is_zero() || op.is_one())
{
}
else
all_bool=false;
}
if(all_bool)
{
// re-write to boolean+typecast
exprt new_expr=expr;
if(expr.id()==ID_bitand)
new_expr.id(ID_and);
else if(expr.id()==ID_bitor)
new_expr.id(ID_or);
else if(expr.id()==ID_bitxor)
new_expr.id(ID_xor);
else
UNREACHABLE;
Forall_operands(it, new_expr)
{
if(it->id()==ID_typecast)
*it = to_typecast_expr(*it).op();
else if(it->is_zero())
*it=false_exprt();
else if(it->is_one())
*it=true_exprt();
}
new_expr.type()=bool_typet();
new_expr = simplify_boolean(new_expr);
return changed(simplify_typecast(typecast_exprt(new_expr, expr.type())));
}
}
bool no_change = true;
auto new_expr = expr;
// try to merge constants
const std::size_t width = to_bitvector_type(expr.type()).get_width();
while(new_expr.operands().size() >= 2)
{
if(!new_expr.op0().is_constant())
break;
if(!new_expr.op1().is_constant())
break;
if(new_expr.op0().type() != new_expr.type())
break;
if(new_expr.op1().type() != new_expr.type())
break;
const auto &a_val = to_constant_expr(new_expr.op0()).get_value();
const auto &b_val = to_constant_expr(new_expr.op1()).get_value();
std::function<bool(bool, bool)> f;
if(new_expr.id() == ID_bitand)
f = [](bool a, bool b) { return a && b; };
else if(new_expr.id() == ID_bitor)
f = [](bool a, bool b) { return a || b; };
else if(new_expr.id() == ID_bitxor)
f = [](bool a, bool b) { return a != b; };
else if(new_expr.id() == ID_bitxnor)
f = [](bool a, bool b) { return a == b; };
else
UNREACHABLE;
const irep_idt new_value =
make_bvrep(width, [&a_val, &b_val, &width, &f](std::size_t i) {
return f(
get_bvrep_bit(a_val, width, i), get_bvrep_bit(b_val, width, i));
});
constant_exprt new_op(new_value, expr.type());
// erase first operand
new_expr.operands().erase(new_expr.operands().begin());
new_expr.op0().swap(new_op);
no_change = false;
}
// now erase 'all zeros' out of bitor, bitxor
if(new_expr.id() == ID_bitor || new_expr.id() == ID_bitxor)
{
for(exprt::operandst::iterator it = new_expr.operands().begin();
it != new_expr.operands().end();) // no it++
{
if(it->is_zero() && new_expr.operands().size() > 1)
{
it = new_expr.operands().erase(it);
no_change = false;
}
else if(
it->is_constant() && it->type().id() == ID_bv &&
to_constant_expr(*it).value_is_zero_string() &&
new_expr.operands().size() > 1)
{
it = new_expr.operands().erase(it);
no_change = false;
}
else
it++;
}
}
// now erase 'all ones' out of bitand
if(new_expr.id() == ID_bitand)
{
const auto all_ones = power(2, width) - 1;
for(exprt::operandst::iterator it = new_expr.operands().begin();
it != new_expr.operands().end();) // no it++
{
if(
it->is_constant() &&
bvrep2integer(to_constant_expr(*it).get_value(), width, false) ==
all_ones &&
new_expr.operands().size() > 1)
{
it = new_expr.operands().erase(it);
no_change = false;
}
else
it++;
}
}
// two operands that are syntactically the same
if(new_expr.operands().size() == 2 && new_expr.op0() == new_expr.op1())
{
if(new_expr.id() == ID_bitand || new_expr.id() == ID_bitor)
{
return new_expr.op0();
}
else if(new_expr.id() == ID_bitxor)
{
return constant_exprt(integer2bvrep(0, width), new_expr.type());
}
}
if(new_expr.operands().size() == 1)
return new_expr.op0();
if(no_change)
return unchanged(expr);
else
return std::move(new_expr);
}
simplify_exprt::resultt<>
simplify_exprt::simplify_extractbit(const extractbit_exprt &expr)
{
const typet &src_type = expr.src().type();
if(!can_cast_type<bitvector_typet>(src_type))
return unchanged(expr);
const std::size_t src_bit_width = to_bitvector_type(src_type).get_width();
const auto index_converted_to_int = numeric_cast<mp_integer>(expr.index());
if(
!index_converted_to_int.has_value() || *index_converted_to_int < 0 ||
*index_converted_to_int >= src_bit_width)
{
return unchanged(expr);
}
if(!expr.src().is_constant())
return unchanged(expr);
const bool bit = get_bvrep_bit(
to_constant_expr(expr.src()).get_value(),
src_bit_width,
numeric_cast_v<std::size_t>(*index_converted_to_int));
return make_boolean_expr(bit);
}
simplify_exprt::resultt<>
simplify_exprt::simplify_concatenation(const concatenation_exprt &expr)
{
bool no_change = true;
concatenation_exprt new_expr = expr;
if(can_cast_type<bitvector_typet>(new_expr.type()))
{
// first, turn bool into bvec[1]
Forall_operands(it, new_expr)
{
exprt &op=*it;
if(op.is_true() || op.is_false())
{
const bool value = op.is_true();
op = from_integer(value, unsignedbv_typet(1));
no_change = false;
}
}
// search for neighboring constants to merge
size_t i=0;
while(i < new_expr.operands().size() - 1)
{
exprt &opi = new_expr.operands()[i];
exprt &opn = new_expr.operands()[i + 1];
if(
opi.is_constant() && opn.is_constant() &&
can_cast_type<bitvector_typet>(opi.type()) &&
can_cast_type<bitvector_typet>(opn.type()))
{
// merge!
const auto &value_i = to_constant_expr(opi).get_value();
const auto &value_n = to_constant_expr(opn).get_value();
const auto width_i = to_bitvector_type(opi.type()).get_width();
const auto width_n = to_bitvector_type(opn.type()).get_width();
const auto new_width = width_i + width_n;
const auto new_value = make_bvrep(
new_width, [&value_i, &value_n, width_i, width_n](std::size_t x) {
return x < width_n ? get_bvrep_bit(value_n, width_n, x)
: get_bvrep_bit(value_i, width_i, x - width_n);
});
to_constant_expr(opi).set_value(new_value);
to_bitvector_type(opi.type()).set_width(new_width);
// erase opn
new_expr.operands().erase(new_expr.operands().begin() + i + 1);
no_change = false;
}
else if(
skip_typecast(opi).id() == ID_extractbits &&
skip_typecast(opn).id() == ID_extractbits)
{
const extractbits_exprt &eb_i = to_extractbits_expr(skip_typecast(opi));
const extractbits_exprt &eb_n = to_extractbits_expr(skip_typecast(opn));
if(
eb_i.src() == eb_n.src() && eb_i.index().is_constant() &&
eb_n.index().is_constant() &&
numeric_cast_v<mp_integer>(to_constant_expr(eb_i.index())) ==
numeric_cast_v<mp_integer>(to_constant_expr(eb_n.index())) +
to_bitvector_type(eb_n.type()).get_width())
{
extractbits_exprt eb_merged = eb_i;
eb_merged.index() = eb_n.index();
to_bitvector_type(eb_merged.type())
.set_width(
to_bitvector_type(eb_i.type()).get_width() +
to_bitvector_type(eb_n.type()).get_width());
if(expr.type().id() != eb_merged.type().id())
{
bitvector_typet bt = to_bitvector_type(expr.type());
bt.set_width(to_bitvector_type(eb_merged.type()).get_width());
opi = simplify_typecast(typecast_exprt{eb_merged, bt});
}
else
opi = eb_merged;
// erase opn
new_expr.operands().erase(new_expr.operands().begin() + i + 1);
no_change = false;
}
else
++i;
}
else
i++;
}
}
else if(new_expr.type().id() == ID_verilog_unsignedbv)
{
// search for neighboring constants to merge
size_t i=0;
while(i < new_expr.operands().size() - 1)
{
exprt &opi = new_expr.operands()[i];
exprt &opn = new_expr.operands()[i + 1];
if(
opi.is_constant() && opn.is_constant() &&
can_cast_type<bitvector_typet>(opi.type()) &&
can_cast_type<bitvector_typet>(opn.type()))
{
// merge!
const std::string new_value=
opi.get_string(ID_value)+opn.get_string(ID_value);
opi.set(ID_value, new_value);
to_bitvector_type(opi.type()).set_width(new_value.size());
opi.type().id(ID_verilog_unsignedbv);
// erase opn
new_expr.operands().erase(new_expr.operands().begin() + i + 1);
no_change = false;
}
else
i++;
}
}
// { x } = x
if(
new_expr.operands().size() == 1 && new_expr.op0().type() == new_expr.type())
{
return new_expr.op0();
}
if(no_change)
return unchanged(expr);
else
return std::move(new_expr);