/
bv_rewriter.cpp
3137 lines (2842 loc) · 99.5 KB
/
bv_rewriter.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
/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
bv_rewriter.cpp
Abstract:
Basic rewriting rules for bit-vectors
Author:
Leonardo (leonardo) 2011-04-14
Notes:
--*/
#include "params/bv_rewriter_params.hpp"
#include "ast/rewriter/bv_rewriter.h"
#include "ast/rewriter/poly_rewriter_def.h"
#include "ast/rewriter/bool_rewriter.h"
#include "ast/ast_lt.h"
#include "ast/ast_pp.h"
void bv_rewriter::updt_local_params(params_ref const & _p) {
bv_rewriter_params p(_p);
m_hi_div0 = p.hi_div0();
m_elim_sign_ext = p.elim_sign_ext();
m_mul2concat = p.mul2concat();
m_bit2bool = p.bit2bool();
m_blast_eq_value = p.blast_eq_value();
m_split_concat_eq = p.split_concat_eq();
m_bvnot_simpl = p.bv_not_simpl();
m_bv_sort_ac = p.bv_sort_ac();
m_extract_prop = p.bv_extract_prop();
m_ite2id = p.bv_ite2id();
m_le_extra = p.bv_le_extra();
m_le2extract = p.bv_le2extract();
set_sort_sums(p.bv_sort_ac());
}
void bv_rewriter::updt_params(params_ref const & p) {
poly_rewriter<bv_rewriter_core>::updt_params(p);
updt_local_params(p);
}
void bv_rewriter::get_param_descrs(param_descrs & r) {
poly_rewriter<bv_rewriter_core>::get_param_descrs(r);
bv_rewriter_params::collect_param_descrs(r);
}
br_status bv_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) {
SASSERT(f->get_family_id() == get_fid());
br_status st = BR_FAILED;
switch(f->get_decl_kind()) {
case OP_BIT0: SASSERT(num_args == 0); result = mk_zero(1); return BR_DONE;
case OP_BIT1: SASSERT(num_args == 0); result = mk_one(1); return BR_DONE;
case OP_ULEQ:
SASSERT(num_args == 2);
st = mk_ule(args[0], args[1], result);
break;
case OP_UGEQ:
SASSERT(num_args == 2);
st = mk_uge(args[0], args[1], result);
break;
case OP_ULT:
SASSERT(num_args == 2);
st = mk_ult(args[0], args[1], result);
break;
case OP_UGT:
SASSERT(num_args == 2);
st = mk_ult(args[1], args[0], result);
break;
case OP_SLEQ:
SASSERT(num_args == 2);
st = mk_sle(args[0], args[1], result);
break;
case OP_SGEQ:
SASSERT(num_args == 2);
st = mk_sge(args[0], args[1], result);
break;
case OP_SLT:
SASSERT(num_args == 2);
st = mk_slt(args[0], args[1], result);
break;
case OP_SGT:
SASSERT(num_args == 2);
st = mk_slt(args[1], args[0], result);
break;
case OP_BADD:
SASSERT(num_args > 0);
st = mk_bv_add(num_args, args, result);
break;
case OP_BMUL:
SASSERT(num_args > 0);
st = mk_bv_mul(num_args, args, result);
break;
case OP_BSUB:
SASSERT(num_args > 0);
st = mk_sub(num_args, args, result);
break;
case OP_BNEG:
SASSERT(num_args == 1);
st = mk_uminus(args[0], result);
break;
case OP_BNEG_OVFL:
SASSERT(num_args == 1);
return mk_bvneg_overflow(args[0], result);
case OP_BSHL:
SASSERT(num_args == 2);
return mk_bv_shl(args[0], args[1], result);
case OP_BLSHR:
SASSERT(num_args == 2);
return mk_bv_lshr(args[0], args[1], result);
case OP_BASHR:
SASSERT(num_args == 2);
return mk_bv_ashr(args[0], args[1], result);
case OP_BSDIV:
SASSERT(num_args == 2);
return mk_bv_sdiv(args[0], args[1], result);
case OP_BUDIV:
SASSERT(num_args == 2);
return mk_bv_udiv(args[0], args[1], result);
case OP_BSREM:
SASSERT(num_args == 2);
return mk_bv_srem(args[0], args[1], result);
case OP_BUREM:
SASSERT(num_args == 2);
return mk_bv_urem(args[0], args[1], result);
case OP_BSMOD:
SASSERT(num_args == 2);
return mk_bv_smod(args[0], args[1], result);
case OP_BSDIV_I:
SASSERT(num_args == 2);
return mk_bv_sdiv_i(args[0], args[1], result);
case OP_BUDIV_I:
SASSERT(num_args == 2);
return mk_bv_udiv_i(args[0], args[1], result);
case OP_BSREM_I:
SASSERT(num_args == 2);
return mk_bv_srem_i(args[0], args[1], result);
case OP_BUREM_I:
SASSERT(num_args == 2);
return mk_bv_urem_i(args[0], args[1], result);
case OP_BSMOD_I:
SASSERT(num_args == 2);
return mk_bv_smod_i(args[0], args[1], result);
case OP_CONCAT:
return mk_concat(num_args, args, result);
case OP_EXTRACT:
SASSERT(num_args == 1);
return mk_extract(m_util.get_extract_high(f), m_util.get_extract_low(f), args[0], result);
case OP_REPEAT:
SASSERT(num_args == 1);
return mk_repeat(f->get_parameter(0).get_int(), args[0], result);
case OP_ZERO_EXT:
SASSERT(num_args == 1);
return mk_zero_extend(f->get_parameter(0).get_int(), args[0], result);
case OP_SIGN_EXT:
SASSERT(num_args == 1);
return mk_sign_extend(f->get_parameter(0).get_int(), args[0], result);
case OP_BOR:
return mk_bv_or(num_args, args, result);
case OP_BXOR:
return mk_bv_xor(num_args, args, result);
case OP_BNOT:
SASSERT(num_args == 1);
return mk_bv_not(args[0], result);
case OP_BAND:
return mk_bv_and(num_args, args, result);
case OP_BNAND:
return mk_bv_nand(num_args, args, result);
case OP_BNOR:
return mk_bv_nor(num_args, args, result);
case OP_BXNOR:
return mk_bv_xnor(num_args, args, result);
case OP_ROTATE_LEFT:
SASSERT(num_args == 1);
return mk_bv_rotate_left(f->get_parameter(0).get_int(), args[0], result);
case OP_ROTATE_RIGHT:
SASSERT(num_args == 1);
return mk_bv_rotate_right(f->get_parameter(0).get_int(), args[0], result);
case OP_EXT_ROTATE_LEFT:
SASSERT(num_args == 2);
return mk_bv_ext_rotate_left(args[0], args[1], result);
case OP_EXT_ROTATE_RIGHT:
SASSERT(num_args == 2);
return mk_bv_ext_rotate_right(args[0], args[1], result);
case OP_BV2INT:
SASSERT(num_args == 1);
return mk_bv2int(args[0], result);
case OP_INT2BV:
SASSERT(num_args == 1);
return mk_int2bv(m_util.get_bv_size(f->get_range()), args[0], result);
case OP_BREDOR:
SASSERT(num_args == 1);
return mk_bv_redor(args[0], result);
case OP_BREDAND:
SASSERT(num_args == 1);
return mk_bv_redand(args[0], result);
case OP_BCOMP:
SASSERT(num_args == 2);
return mk_bv_comp(args[0], args[1], result);
case OP_MKBV:
return mk_mkbv(num_args, args, result);
case OP_BIT2BOOL:
SASSERT(num_args == 1);
return mk_bit2bool(args[0], f->get_parameter(0).get_int(), result);
case OP_BSMUL_NO_OVFL:
return mk_bvsmul_no_overflow(num_args, args, true, result);
case OP_BSMUL_NO_UDFL:
return mk_bvsmul_no_overflow(num_args, args, false, result);
case OP_BUMUL_NO_OVFL:
return mk_bvumul_no_overflow(num_args, args, result);
case OP_BSMUL_OVFL:
return mk_bvsmul_overflow(num_args, args, result);
case OP_BUMUL_OVFL:
return mk_bvumul_overflow(num_args, args, result);
case OP_BSDIV_OVFL:
return mk_bvsdiv_overflow(num_args, args, result);
case OP_BUADD_OVFL:
return mk_bvuadd_overflow(num_args, args, result);
case OP_BSADD_OVFL:
return mk_bvsadd_over_underflow(num_args, args, result);
case OP_BUSUB_OVFL:
return mk_bvusub_underflow(num_args, args, result);
case OP_BSSUB_OVFL:
return mk_bvssub_under_overflow(num_args, args, result);
default:
return BR_FAILED;
}
CTRACE("bv", st != BR_FAILED, tout << mk_pp(f, m) << "\n";
for (unsigned i = 0; i < num_args; ++i)
tout << " " << mk_bounded_pp(args[i], m) << "\n";
tout << mk_bounded_pp(result, m, 3) << "\n");
return st;
}
br_status bv_rewriter::mk_ule(expr * a, expr * b, expr_ref & result) {
return mk_leq_core(false, a, b, result);
}
br_status bv_rewriter::mk_uge(expr * a, expr * b, expr_ref & result) {
br_status st = mk_ule(b, a, result);
if (st != BR_FAILED)
return st;
result = m_util.mk_ule(b, a);
return BR_DONE;
}
br_status bv_rewriter::mk_ult(expr * a, expr * b, expr_ref & result) {
result = m.mk_not(m_util.mk_ule(b, a));
return BR_REWRITE2;
}
br_status bv_rewriter::mk_sle(expr * a, expr * b, expr_ref & result) {
return mk_leq_core(true, a, b, result);
}
br_status bv_rewriter::mk_sge(expr * a, expr * b, expr_ref & result) {
br_status st = mk_sle(b, a, result);
if (st != BR_FAILED)
return st;
result = m_util.mk_sle(b, a);
return BR_DONE;
}
br_status bv_rewriter::mk_slt(expr * a, expr * b, expr_ref & result) {
result = m.mk_not(m_util.mk_sle(b, a));
return BR_REWRITE2;
}
// short-circuited concat
expr * bv_rewriter::concat(unsigned num_args, expr * const * args) {
SASSERT(num_args);
switch (num_args) {
case 0: return m_util.mk_concat(num_args, args);
case 1: return args[0];
default: return m_util.mk_concat(num_args, args);
}
}
// finds a commonality in sums, e.g. 2 + x + y and 5 + x + y
bool bv_rewriter::are_eq_upto_num(expr * _a, expr * _b,
expr_ref& common,
numeral& a0_val, numeral& b0_val) {
const bool aadd = m_util.is_bv_add(_a);
const bool badd = m_util.is_bv_add(_b);
const bool has_num_a = aadd && to_app(_a)->get_num_args() && is_numeral(to_app(_a)->get_arg(0));
const bool has_num_b = badd && to_app(_b)->get_num_args() && is_numeral(to_app(_b)->get_arg(0));
a0_val = numeral::zero();
b0_val = numeral::zero();
if (!aadd && !badd) {
if (_a == _b) {
common = _a;
return true;
} else {
return false;
}
}
if (!aadd && badd) {
if (!is_app(_a) || to_app(_a)->get_num_args() != 2 || !has_num_a || to_app(_a)->get_arg(0) != _b)
return false;
common = _b;
return true;
}
if (aadd && !badd) {
if (!is_app(_b) || to_app(_b)->get_num_args() != 2 || !has_num_b || to_app(_b)->get_arg(0) != _a)
return false;
common = _a;
return true;
}
SASSERT(aadd && badd);
app * const a = to_app(_a);
app * const b = to_app(_b);
const unsigned numa = a->get_num_args();
const unsigned numb = b->get_num_args();
if (!numa || !numb) return false;
if ((numa - (has_num_a ? 1 : 0)) != (numb - (has_num_b ? 1 : 0))) return false;
unsigned ai = has_num_a ? 1 : 0;
unsigned bi = has_num_b ? 1 : 0;
while (ai < numa) {
if (a->get_arg(ai) != b->get_arg(bi)) return false;
++ai;
++bi;
}
a0_val = numeral::zero();
b0_val = numeral::zero();
const unsigned sz = m_util.get_bv_size(a);
unsigned a0_sz(sz), b0_sz(sz);
if (has_num_a) is_numeral(a->get_arg(0), a0_val, a0_sz);
if (has_num_b) is_numeral(b->get_arg(0), b0_val, b0_sz);
SASSERT(a0_sz == m_util.get_bv_size(a) && b0_sz == m_util.get_bv_size(a));
if (has_num_a && numa > 2) {
common = m.mk_app(m_util.get_fid(), add_decl_kind(), numa - 1, a->get_args() + 1);
}
else {
common = has_num_a ? a->get_arg(1) : a;
}
return true;
}
// simplifies expressions as (bvuleq (X + c1) (X + c2)) for some common expression X and numerals c1, c2
br_status bv_rewriter::rw_leq_overflow(bool is_signed, expr * a, expr * b, expr_ref & result) {
if (is_signed) return BR_FAILED;
expr_ref common(m);
numeral a0_val, b0_val;
if (!are_eq_upto_num(a, b, common, a0_val, b0_val)) return BR_FAILED;
SASSERT(a0_val.is_nonneg() && b0_val.is_nonneg());
const unsigned sz = m_util.get_bv_size(a);
if (a0_val == b0_val) {
result = m.mk_true();
return BR_DONE;
}
if (a0_val < b0_val) {
result = m_util.mk_ule(m_util.mk_numeral(b0_val - a0_val, sz), b);
return BR_REWRITE2;
}
SASSERT(a0_val > b0_val);
SASSERT(!a0_val.is_zero());
const numeral lower = rational::power_of_two(sz) - a0_val;
const numeral upper = rational::power_of_two(sz) - b0_val - numeral::one();
if (lower == upper) {
result = m.mk_eq(common, mk_numeral(lower, sz));
}
else if (b0_val.is_zero()) {
result = m_util.mk_ule(mk_numeral(lower, sz), common);
}
else {
SASSERT(lower.is_pos());
result = m.mk_and(m_util.mk_ule(mk_numeral(lower, sz), common),
m_util.mk_ule(common, mk_numeral(upper, sz)));
}
return BR_REWRITE2;
}
// simplification for leq comparison between two concatenations
br_status bv_rewriter::rw_leq_concats(bool is_signed, expr * _a, expr * _b, expr_ref & result) {
if (!m_util.is_concat(_a) || !m_util.is_concat(_b))
return BR_FAILED;
const app * const a = to_app(_a);
const app * const b = to_app(_b);
const unsigned numa = a->get_num_args();
const unsigned numb = b->get_num_args();
const unsigned num_min = std::min(numa, numb);
if (numa && numb) { // first arg numeral
numeral af, bf;
unsigned af_sz, bf_sz;
if ( is_numeral(a->get_arg(0), af, af_sz)
&& is_numeral(b->get_arg(0), bf, bf_sz) ) {
const unsigned sz_min = std::min(af_sz, bf_sz);
const numeral hi_af = m_util.norm(af_sz > sz_min ? div(af, rational::power_of_two(af_sz - sz_min)) : af,
sz_min, is_signed);
const numeral hi_bf = m_util.norm(bf_sz > sz_min ? div(bf, rational::power_of_two(bf_sz - sz_min)) : bf,
sz_min, is_signed);
if (hi_af != hi_bf) {
result = hi_af < hi_bf ? m.mk_true() : m.mk_false();
return BR_DONE;
}
expr_ref new_a(m);
expr_ref new_b(m);
if (af_sz > sz_min) {
ptr_buffer<expr> new_args;
new_args.push_back(mk_numeral(af, af_sz - sz_min));
for (unsigned i = 1; i < numa; ++i) new_args.push_back(a->get_arg(i));
new_a = concat(new_args.size(), new_args.data());
} else {
new_a = concat(numa - 1, a->get_args() + 1);
}
if (bf_sz > sz_min) {
ptr_buffer<expr> new_args;
new_args.push_back(mk_numeral(bf, bf_sz - sz_min));
for (unsigned i = 1; i < numb; ++i) new_args.push_back(b->get_arg(i));
new_b = concat(new_args.size(), new_args.data());
} else {
new_b = concat(numb - 1, b->get_args() + 1);
}
result = m_util.mk_ule(new_a, new_b);
return BR_REWRITE2;
}
}
{ // common prefix
unsigned common = 0;
while (common < num_min && m.are_equal(a->get_arg(common), b->get_arg(common))) ++common;
SASSERT((common == numa) == (common == numb));
if (common == numa) {
SASSERT(0); // shouldn't get here as both sides are equal
result = m.mk_true();
return BR_DONE;
}
if (common > 0) {
result = m_util.mk_ule(concat(numa - common, a->get_args() + common),
concat(numb - common, b->get_args() + common));
return BR_REWRITE2;
}
}
{ // common postfix
unsigned new_numa = a->get_num_args();
unsigned new_numb = b->get_num_args();
while (new_numa && new_numb) {
expr * const last_a = a->get_arg(new_numa - 1);
expr * const last_b = b->get_arg(new_numb - 1);
if (!m.are_equal(last_a, last_b)) break;
new_numa--;
new_numb--;
}
if (new_numa == 0) {
SASSERT(0); // shouldn't get here as both sides are equal
result = m.mk_true();
return BR_DONE;
}
if (new_numa != numa) {
result = is_signed ? m_util.mk_sle(concat(new_numa, a->get_args()), concat(new_numb, b->get_args()))
: m_util.mk_ule(concat(new_numa, a->get_args()), concat(new_numb, b->get_args()));
return BR_REWRITE2;
}
}
return BR_FAILED;
}
br_status bv_rewriter::mk_leq_core(bool is_signed, expr * a, expr * b, expr_ref & result) {
numeral r1, r2, r3;
unsigned sz;
bool is_num1 = is_numeral(a, r1, sz);
bool is_num2 = is_numeral(b, r2, sz);
if (a == b) {
result = m.mk_true();
return BR_DONE;
}
if (is_num1)
r1 = m_util.norm(r1, sz, is_signed);
if (is_num2)
r2 = m_util.norm(r2, sz, is_signed);
if (is_num1 && is_num2) {
result = m.mk_bool_val(r1 <= r2);
return BR_DONE;
}
numeral lower, upper;
if (is_num1 || is_num2) {
if (is_signed) {
lower = - rational::power_of_two(sz - 1);
upper = rational::power_of_two(sz - 1) - numeral(1);
}
else {
lower = numeral(0);
upper = rational::power_of_two(sz) - numeral(1);
}
}
if (is_num2) {
if (r2 == lower) {
result = m.mk_eq(a, b);
return BR_REWRITE1;
}
if (r2 == upper) {
result = m.mk_true();
return BR_DONE;
}
}
if (is_num1) {
// 0 <= b is true
if (r1 == lower) {
result = m.mk_true();
return BR_DONE;
}
// 2^n-1 <= b is a = b
if (r1 == upper) {
result = m.mk_eq(a, b);
return BR_REWRITE1;
}
}
expr* a1, *a2, *a3, *a4, *a5, *a6;
// (bvsle (- x (srem x c1)) c2) -> (bvsle x (+ c1 c2 - 1))
// (bvsle (+ x (* -1 (srem_i x c1))) c2)
// pre: (and (> c1 0) (> c2 0) (= c2 % c1 0) (<= (+ c1 c2 -1) max_int))
if (is_signed && is_num2 &&
m_util.is_bv_add(a, a1, a2) &&
m_util.is_bv_mul(a2, a3, a4) && is_numeral(a3, r1, sz) &&
m_util.norm(r1, sz, is_signed).is_minus_one() &&
m_util.is_bv_sremi(a4, a5, a6) && is_numeral(a6, r1, sz) &&
(r1 = m_util.norm(r1, sz, is_signed), r1.is_pos()) &&
r2.is_pos() &&
(a1 == a5) &&
(r2 % r1).is_zero() && r1 + r2 - rational::one() < rational::power_of_two(sz-1)) {
result = m_util.mk_sle(a1, m_util.mk_numeral(r1 + r2 - rational::one(), sz));
return BR_REWRITE2;
}
// (bvule r1 (+ r2 a)) ->
// for r1 = r2, (bvule a (2^n - r2 - 1))
// other cases r1 > r2, r1 < r2 are TBD
if (!is_signed && is_num1 && m_util.is_bv_add(b, a1, a2) && is_numeral(a1, r2, sz)) {
result = m_util.mk_ule(a2, m_util.mk_numeral(-r2 - 1, sz));
if (r1 > r2)
result = m.mk_and(result, m_util.mk_ule(m_util.mk_numeral(r1-r2, sz), a2));
else if (r1 < r2)
result = m.mk_or(result, m_util.mk_ule(m_util.mk_numeral(r1-r2, sz), a2));
return BR_REWRITE2;
}
if (m_le_extra) {
const br_status cst = rw_leq_concats(is_signed, a, b, result);
if (cst != BR_FAILED) {
TRACE("le_extra", tout << (is_signed ? "bv_sle\n" : "bv_ule\n")
<< mk_pp(a, m, 2) << "\n" << mk_pp(b, m, 2) << "\n--->\n"<< mk_pp(result, m, 2) << "\n";);
return cst;
}
}
if (m_le_extra) {
const br_status cst = rw_leq_overflow(is_signed, a, b, result);
if (cst != BR_FAILED) {
TRACE("le_extra", tout << (is_signed ? "bv_sle\n" : "bv_ule\n")
<< mk_pp(a, m, 2) << "\n" << mk_pp(b, m, 2) << "\n--->\n"<< mk_pp(result, m, 2) << "\n";);
return cst;
}
}
#if 0
if (!is_signed && m_util.is_concat(b) && to_app(b)->get_num_args() == 2 && m_util.is_zero(to_app(b)->get_arg(0))) {
//
// a <=_u (concat 0 c) ---> a[h:l] = 0 && a[l-1:0] <=_u c
//
expr * b_1 = to_app(b)->get_arg(0);
expr * b_2 = to_app(b)->get_arg(1);
unsigned sz1 = get_bv_size(b_1);
unsigned sz2 = get_bv_size(b_2);
result = m.mk_and(m.mk_eq(m_mk_extract(sz2+sz1-1, sz2, a), b_1),
m_util.mk_ule(m_mk_extract(sz2-1, 0, a), b_2));
return BR_REWRITE3;
}
#else
if (!is_signed) {
// Extended version of the rule above using is_zero_bit.
// It also catches examples atoms such as:
//
// a <=_u #x000f
//
unsigned bv_sz = m_util.get_bv_size(b);
unsigned i = bv_sz;
unsigned first_non_zero = UINT_MAX;
while (i > 0) {
--i;
if (!is_zero_bit(b, i)) {
first_non_zero = i;
break;
}
}
if (first_non_zero == UINT_MAX) {
// all bits are zero
result = m.mk_eq(a, mk_zero(bv_sz));
return BR_REWRITE1;
}
else if (first_non_zero < bv_sz - 1 && m_le2extract) {
result = m.mk_and(m.mk_eq(m_mk_extract(bv_sz - 1, first_non_zero + 1, a), mk_zero(bv_sz - first_non_zero - 1)),
m_util.mk_ule(m_mk_extract(first_non_zero, 0, a), m_mk_extract(first_non_zero, 0, b)));
return BR_REWRITE3;
}
}
#endif
// Investigate if we need:
//
// k <=_s (concat 0 a) <=> (k[u:l] = 0 && k[l-1:0] <=_u a) || k[u:u] = bv1
//
// (concat 0 a) <=_s k <=> k[u:u] = bv0 && (k[u:l] != 0 || a <=_u k[l-1:0])
//
// (concat 0 a) <=_u k <=> k[u:l] != 0 || a <=_u k[l-1:0]
//
return BR_FAILED;
}
// attempt to chop off bits that are above the position high for bv_mul and bv_add,
// returns how many bits were chopped off
// e.g. (bvadd(concat #b11 p) #x1)) with high=1, returns 2 and sets result = p + #b01
// the sz of results is the sz of arg minus the return value
unsigned bv_rewriter::propagate_extract(unsigned high, expr * arg, expr_ref & result) {
if (!m_util.is_bv_add(arg) && !m_util.is_bv_mul(arg))
return 0;
const unsigned sz = m_util.get_bv_size(arg);
const unsigned to_remove = high + 1 < sz ? sz - high - 1 : 0;
if (to_remove == 0)
return 0; // high goes to the top, nothing to do
const app * const a = to_app(arg);
const unsigned num = a->get_num_args();
bool all_numerals = true;
unsigned removable = to_remove;
numeral val;
unsigned curr_first_sz = -1;
// calculate how much can be removed
for (unsigned i = 0; i < num; i++) {
expr * const curr = a->get_arg(i);
const bool curr_is_conc = m_util.is_concat(curr);
if (curr_is_conc && to_app(curr)->get_num_args() == 0) continue;
expr * const curr_first = curr_is_conc ? to_app(curr)->get_arg(0) : curr;
if (!all_numerals) {
if (removable != m_util.get_bv_size(curr_first))
return 0;
continue;
}
if (is_numeral(curr_first, val, curr_first_sz)) {
removable = std::min(removable, curr_first_sz);
} else {
all_numerals = false;
curr_first_sz = m_util.get_bv_size(curr_first);
if (curr_first_sz > removable) return 0;
removable = curr_first_sz;
}
if (removable == 0) return 0;
}
// perform removal
SASSERT(removable <= to_remove);
ptr_buffer<expr> new_args;
ptr_buffer<expr> new_concat_args;
for (unsigned i = 0; i < num; i++) {
expr * const curr = a->get_arg(i);
const bool curr_is_conc = m_util.is_concat(curr);
if (curr_is_conc && to_app(curr)->get_num_args() == 0) continue;
expr * const curr_first = curr_is_conc ? to_app(curr)->get_arg(0) : curr;
expr * new_first = nullptr;
if (is_numeral(curr_first, val, curr_first_sz)) {
SASSERT(curr_first_sz >= removable);
const unsigned new_num_sz = curr_first_sz - removable;
new_first = new_num_sz ? mk_numeral(val, new_num_sz) : nullptr;
}
expr * new_arg = nullptr;
if (curr_is_conc) {
const unsigned conc_num = to_app(curr)->get_num_args();
if (new_first) {
new_concat_args.reset();
new_concat_args.push_back(new_first);
for (unsigned j = 1; j < conc_num; ++j)
new_concat_args.push_back(to_app(curr)->get_arg(j));
new_arg = m_util.mk_concat(new_concat_args.size(), new_concat_args.data());
} else {
// remove first element of concat
expr * const * const old_conc_args = to_app(curr)->get_args();
switch (conc_num) {
case 0: UNREACHABLE(); break;
case 1: new_arg = nullptr; break;
case 2: new_arg = to_app(curr)->get_arg(1); break;
default: new_arg = m_util.mk_concat(conc_num - 1, old_conc_args + 1);
}
}
} else {
new_arg = new_first;
}
if (new_arg) new_args.push_back(new_arg);
}
result = m.mk_app(get_fid(), a->get_decl()->get_decl_kind(), new_args.size(), new_args.data());
SASSERT(m_util.is_bv(result));
return removable;
}
br_status bv_rewriter::mk_extract(unsigned high, unsigned low, expr * arg, expr_ref & result) {
unsigned sz = get_bv_size(arg);
SASSERT(sz > 0);
if (low == 0 && high == sz - 1) {
result = arg;
return BR_DONE;
}
numeral v;
if (is_numeral(arg, v, sz)) {
sz = high - low + 1;
if (v.is_neg())
mod(v, rational::power_of_two(sz), v);
if (v.is_uint64()) {
uint64_t u = v.get_uint64();
uint64_t e = shift_right(u, low) & (shift_left(1ull, sz) - 1ull);
result = mk_numeral(numeral(e, numeral::ui64()), sz);
return BR_DONE;
}
div(v, rational::power_of_two(low), v);
result = mk_numeral(v, sz);
return BR_DONE;
}
// (extract[high:low] (extract[high2:low2] x)) == (extract[high+low2 : low+low2] x)
if (m_util.is_extract(arg)) {
unsigned low2 = m_util.get_extract_low(arg);
result = m_mk_extract(high + low2, low + low2, to_app(arg)->get_arg(0));
return BR_DONE;
}
// (extract (concat ....)) --> (concat (extract ...) ... (extract ...) )
if (m_util.is_concat(arg)) {
unsigned num = to_app(arg)->get_num_args();
unsigned idx = sz;
for (unsigned i = 0; i < num; i++) {
expr * curr = to_app(arg)->get_arg(i);
unsigned curr_sz = get_bv_size(curr);
idx -= curr_sz;
if (idx > high)
continue;
// found first argument
if (idx <= low) {
// result is a fragment of this argument
if (low == idx && high - idx == curr_sz - 1) {
result = curr;
return BR_DONE;
}
else {
result = m_mk_extract(high - idx, low - idx, curr);
return BR_REWRITE1;
}
}
else {
// look for remaining arguments
ptr_buffer<expr> new_args;
bool used_extract = false;
if (high - idx == curr_sz - 1) {
new_args.push_back(curr);
}
else {
used_extract = true;
new_args.push_back(m_mk_extract(high - idx, 0, curr));
}
for (unsigned j = i + 1; j < num; j++) {
curr = to_app(arg)->get_arg(j);
unsigned curr_sz = get_bv_size(curr);
idx -= curr_sz;
if (idx > low) {
new_args.push_back(curr);
continue;
}
if (idx == low) {
new_args.push_back(curr);
result = m_util.mk_concat(new_args.size(), new_args.data());
return used_extract ? BR_REWRITE2 : BR_DONE;
}
new_args.push_back(m_mk_extract(curr_sz - 1, low - idx, curr));
result = m_util.mk_concat(new_args.size(), new_args.data());
return BR_REWRITE2;
}
UNREACHABLE();
}
}
UNREACHABLE();
}
if (m_util.is_bv_not(arg) ||
m_util.is_bv_or(arg) ||
m_util.is_bv_xor(arg) ||
(low == 0 && (m_util.is_bv_add(arg) ||
m_util.is_bv_mul(arg)))) {
ptr_buffer<expr> new_args;
unsigned num = to_app(arg)->get_num_args();
for (unsigned i = 0; i < num; i++) {
expr * curr = to_app(arg)->get_arg(i);
new_args.push_back(m_mk_extract(high, low, curr));
}
result = m.mk_app(get_fid(), to_app(arg)->get_decl()->get_decl_kind(), new_args.size(), new_args.data());
return BR_REWRITE2;
}
if (m_extract_prop && (high >= low)) {
expr_ref ep_res(m);
const unsigned ep_rm = propagate_extract(high, arg, ep_res);
if (ep_rm != 0) {
result = m_mk_extract(high, low, ep_res);
TRACE("extract_prop", tout << mk_pp(arg, m) << "\n[" << high <<"," << low << "]\n" << ep_rm << "---->\n"
<< mk_pp(result.get(), m) << "\n";);
return BR_REWRITE2;
}
}
// issue #2359 led to relaxing condition for propagating extract over ite.
// It is propagted inwards only in the case that it leads to at most one
// branch of ite to be expanded or if one of the expanded ite branches have a single
// reference count.
expr* c = nullptr, *t = nullptr, *e = nullptr;
if (m.is_ite(arg, c, t, e) &&
(t->get_ref_count() == 1 || e->get_ref_count() == 1 || !m.is_ite(t) || !m.is_ite(e))) {
result = m.mk_ite(c, m_mk_extract(high, low, t), m_mk_extract(high, low, e));
return BR_REWRITE2;
}
return BR_FAILED;
}
br_status bv_rewriter::mk_bv_shl(expr * arg1, expr * arg2, expr_ref & result) {
numeral r1, r2;
unsigned bv_size = get_bv_size(arg1);
unsigned sz;
if (is_numeral(arg2, r2, sz)) {
if (r2.is_zero()) {
// x << 0 == x
result = arg1;
return BR_DONE;
}
if (r2 >= numeral(bv_size)) {
result = mk_zero(bv_size);
return BR_DONE;
}
if (is_numeral(arg1, r1, sz)) {
if (bv_size <= 64) {
SASSERT(r1.is_uint64() && r2.is_uint64());
SASSERT(r2.get_uint64() < bv_size);
uint64_t r = shift_left(r1.get_uint64(), r2.get_uint64());
numeral rn(r, numeral::ui64());
rn = m_util.norm(rn, bv_size);
result = mk_numeral(rn, bv_size);
return BR_DONE;
}
SASSERT(r2 < numeral(bv_size));
SASSERT(r2.is_unsigned());
r1 = m_util.norm(r1 * rational::power_of_two(r2.get_unsigned()), bv_size);
result = mk_numeral(r1, bv_size);
return BR_DONE;
}
SASSERT(r2.is_pos());
SASSERT(r2 < numeral(bv_size));
// (bvshl x k) -> (concat (extract [n-1-k:0] x) bv0:k)
unsigned k = r2.get_unsigned();
expr * new_args[2] = { m_mk_extract(bv_size - k - 1, 0, arg1),
mk_zero(k) };
result = m_util.mk_concat(2, new_args);
return BR_REWRITE2;
}
expr* x = nullptr, *y = nullptr;
if (m_util.is_bv_shl(arg1, x, y)) {
expr_ref sum(m_util.mk_bv_add(y, arg2), m);
expr_ref cond(m_util.mk_ule(y, sum), m);
result = m.mk_ite(cond,
m_util.mk_bv_shl(x, sum),
mk_zero(bv_size));
return BR_REWRITE3;
}
return BR_FAILED;
}
br_status bv_rewriter::mk_bv_lshr(expr * arg1, expr * arg2, expr_ref & result) {
numeral r1, r2;
unsigned bv_size = get_bv_size(arg1);
unsigned sz;
if (is_numeral(arg2, r2, sz)) {
if (r2.is_zero()) {
// x >> 0 == x
result = arg1;
return BR_DONE;
}
if (r2 >= numeral(bv_size)) {
result = mk_zero(bv_size);
return BR_DONE;
}
if (is_numeral(arg1, r1, sz)) {
if (bv_size <= 64) {
SASSERT(r1.is_uint64());
SASSERT(r2.is_uint64());
uint64_t r = shift_right(r1.get_uint64(), r2.get_uint64());
numeral rn(r, numeral::ui64());
rn = m_util.norm(rn, bv_size);
result = mk_numeral(rn, bv_size);
return BR_DONE;
}
SASSERT(r2.is_unsigned());
unsigned sh = r2.get_unsigned();
div(r1, rational::power_of_two(sh), r1);
result = mk_numeral(r1, bv_size);
return BR_DONE;
}
SASSERT(r2.is_pos());
SASSERT(r2 < numeral(bv_size));
// (bvlshr x k) -> (concat bv0:k (extract [n-1:k] x))
SASSERT(r2.is_unsigned());
unsigned k = r2.get_unsigned();
expr * new_args[2] = { mk_zero(k),
m_mk_extract(bv_size - 1, k, arg1) };
result = m_util.mk_concat(2, new_args);
return BR_REWRITE2;
}
if (arg1 == arg2) {
result = mk_zero(bv_size);
return BR_DONE;
}
return BR_FAILED;
}
br_status bv_rewriter::mk_bv_ashr(expr * arg1, expr * arg2, expr_ref & result) {
numeral r1, r2;
unsigned bv_size = get_bv_size(arg1);
SASSERT(bv_size > 0);
bool is_num2 = is_numeral(arg2, r2, bv_size);
if (is_num2 && r2.is_zero()) {
result = arg1;
return BR_DONE;
}
bool is_num1 = is_numeral(arg1, r1, bv_size);
if (bv_size <= 64 && is_num1 && is_num2) {
uint64_t n1 = r1.get_uint64();
uint64_t n2_orig = r2.get_uint64();
uint64_t n2 = n2_orig % bv_size;
SASSERT(n2 < bv_size);
uint64_t r = shift_right(n1, n2);
bool sign = (n1 & shift_left(1ull, bv_size - 1ull)) != 0;
if (n2_orig > n2) {
if (sign) {
r = shift_left(1ull, bv_size) - 1ull;
}
else {
r = 0;
}
}
else if (sign) {
uint64_t allone = shift_left(1ull, bv_size) - 1ull;
uint64_t mask = ~(shift_left(1ull, bv_size - n2) - 1ull);
mask &= allone;
r |= mask;
}
result = mk_numeral(numeral(r, numeral::ui64()), bv_size);
return BR_DONE;
}
if (is_num1 && is_num2 && numeral(bv_size) <= r2) {
if (m_util.has_sign_bit(r1, bv_size))
result = mk_numeral(rational::power_of_two(bv_size) - numeral(1), bv_size);