-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathinterval.cpp
More file actions
1975 lines (1652 loc) · 40.9 KB
/
interval.cpp
File metadata and controls
1975 lines (1652 loc) · 40.9 KB
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: intervals
Author: Daniel Neville (2017), Diffblue Ltd
\*******************************************************************/
/*
*
* Types: Should we store a type for the entire interval?
* IMO: I think YES (for the case where we have -inf -> inf, we don't know otherwise
*
* This initial implementation only implements support for integers.
*/
#include "interval.h"
#include "arith_tools.h"
#include "bitvector_expr.h"
#include "c_types.h"
#include "namespace.h"
#include "simplify_expr.h"
#include "std_expr.h"
#include "symbol_table.h"
const exprt &constant_interval_exprt::get_lower() const
{
return op0();
}
const exprt &constant_interval_exprt::get_upper() const
{
return op1();
}
constant_interval_exprt constant_interval_exprt::unary_plus() const
{
return *this;
}
constant_interval_exprt constant_interval_exprt::unary_minus() const
{
if(is_single_value_interval())
{
handle_constant_unary_expression(ID_unary_minus);
}
exprt lower;
exprt upper;
if(has_no_upper_bound())
{
lower = min();
}
else
{
lower = simplified_expr(unary_minus_exprt(get_upper()));
}
if(has_no_lower_bound())
{
upper = max();
}
else
{
upper = simplified_expr(unary_minus_exprt(get_lower()));
}
return constant_interval_exprt(lower, upper);
}
constant_interval_exprt
constant_interval_exprt::plus(const constant_interval_exprt &o) const
{
if(o.is_single_value_interval() && is_single_value_interval())
{
handle_constant_binary_expression(o, ID_plus);
}
exprt lower = min();
exprt upper = max();
if(is_max(get_upper()) || is_max(o.get_upper()))
{
upper = max_value_exprt(type());
}
else
{
INVARIANT(
!is_max(get_upper()) && !is_max(o.get_upper()),
"We just excluded this case");
upper = simplified_expr(plus_exprt(get_upper(), o.get_upper()));
}
if(is_min(get_lower()) || is_min(o.get_lower()))
{
lower = min_value_exprt(type());
}
else
{
INVARIANT(
!is_min(get_lower()) && !is_min(o.get_lower()),
"We just excluded that case");
lower = simplified_expr(plus_exprt(get_lower(), o.get_lower()));
}
return simplified_interval(lower, upper);
}
constant_interval_exprt
constant_interval_exprt::minus(const constant_interval_exprt &other) const
{
if(other.is_single_value_interval() && is_single_value_interval())
{
handle_constant_binary_expression(other, ID_minus);
}
// [this.lower - other.upper, this.upper - other.lower]
return plus(other.unary_minus());
}
constant_interval_exprt
constant_interval_exprt::multiply(const constant_interval_exprt &o) const
{
if(o.is_single_value_interval() && is_single_value_interval())
{
handle_constant_binary_expression(o, ID_mult);
}
return get_extremes(*this, o, ID_mult);
}
constant_interval_exprt
constant_interval_exprt::divide(const constant_interval_exprt &o) const
{
if(o.is_single_value_interval() && is_single_value_interval())
{
handle_constant_binary_expression(o, ID_div);
}
// If other might be division by zero, set everything to top.
if(o.contains_zero())
{
return top();
}
return get_extremes(*this, o, ID_div);
}
constant_interval_exprt
constant_interval_exprt::modulo(const constant_interval_exprt &o) const
{
// SEE https://stackoverflow.com/questions/11720656/modulo-operation-with-negative-numbers
if(o.is_single_value_interval() && is_single_value_interval())
{
handle_constant_binary_expression(o, ID_mod);
}
if(o.is_bottom())
{
return top();
}
// If the RHS is 1, or -1 (signed only), then return zero.
if(
o.is_single_value_interval() &&
(o.get_lower() == from_integer(1, o.type()) ||
(o.is_signed() && o.get_lower() == from_integer(-1, o.type()))))
{
return constant_interval_exprt::singleton(zero());
}
// If other might be modulo by zero, set everything to top.
if(o.contains_zero())
{
return top();
}
if(is_zero())
{
return constant_interval_exprt::singleton(zero());
}
exprt lower = min();
exprt upper = max();
// Positive case (cannot have zero on RHS).
// 10 % 5 = [0, 4], 3 % 5 = [0, 3]
if(!is_negative() && o.is_positive())
{
lower = zero();
upper = get_min(get_upper(), o.decrement().get_upper());
}
// [-5, 5] % [3]
if(is_negative(get_lower()) && is_positive(get_upper()))
{
INVARIANT(
contains_zero(),
"Zero should be between a negative and a positive value");
// This can be done more accurately.
lower = get_min(o.get_lower(), get_lower());
upper = get_max(o.get_upper(), get_upper());
}
if(is_negative() && o.is_negative())
{
lower = get_min(o.get_lower(), o.get_lower());
upper = zero();
}
return constant_interval_exprt(lower, upper);
}
tvt constant_interval_exprt::is_definitely_true() const
{
// tvt not
return !is_definitely_false();
}
tvt constant_interval_exprt::is_definitely_false() const
{
if(is_boolean())
{
if(is_single_value_interval())
{
return tvt(get_lower() == false_exprt());
}
else
{
return tvt::unknown();
}
}
if(is_single_value_interval() && get_lower() == zero())
{
return tvt(true);
}
if(contains(constant_interval_exprt::singleton(zero())))
{
INVARIANT(
is_positive(get_upper()) || is_negative(get_lower()),
"If an interval contains zero its lower bound can't be positive"
" and its upper bound can't be negative");
return tvt::unknown();
}
return tvt(false);
}
tvt constant_interval_exprt::logical_or(const constant_interval_exprt &o) const
{
PRECONDITION(is_boolean());
PRECONDITION(o.is_boolean());
tvt a = is_definitely_true();
tvt b = o.is_definitely_true();
return (a || b);
}
tvt constant_interval_exprt::logical_and(const constant_interval_exprt &o) const
{
PRECONDITION(is_boolean());
PRECONDITION(o.is_boolean());
return (is_definitely_true() && o.is_definitely_true());
}
tvt constant_interval_exprt::logical_xor(const constant_interval_exprt &o) const
{
PRECONDITION(is_boolean());
PRECONDITION(o.is_boolean());
return (
(is_definitely_true() && !o.is_definitely_true()) ||
(!is_definitely_true() && o.is_definitely_true()));
}
tvt constant_interval_exprt::logical_not() const
{
PRECONDITION(is_boolean());
if(is_definitely_true().is_true())
{
return tvt(false);
}
if(is_definitely_false().is_true())
{
return tvt(true);
}
return tvt::unknown();
}
constant_interval_exprt
constant_interval_exprt::left_shift(const constant_interval_exprt &o) const
{
if(o.is_single_value_interval() && is_single_value_interval())
{
return handle_constant_binary_expression(o, ID_shl);
}
if(is_negative(o.get_lower()))
{
return top();
}
return get_extremes(*this, o, ID_shl);
}
// Arithmetic
constant_interval_exprt
constant_interval_exprt::right_shift(const constant_interval_exprt &o) const
{
if(o.is_single_value_interval() && is_single_value_interval())
{
return handle_constant_binary_expression(o, ID_ashr);
}
if(is_negative(o.get_lower()))
{
return top();
}
return get_extremes(*this, o, ID_ashr);
}
constant_interval_exprt
constant_interval_exprt::bitwise_xor(const constant_interval_exprt &o) const
{
if(o.is_single_value_interval() && is_single_value_interval())
{
return handle_constant_binary_expression(o, ID_bitxor);
}
return top();
}
constant_interval_exprt
constant_interval_exprt::bitwise_or(const constant_interval_exprt &o) const
{
if(o.is_single_value_interval() && is_single_value_interval())
{
return handle_constant_binary_expression(o, ID_bitor);
}
return top();
}
constant_interval_exprt
constant_interval_exprt::bitwise_and(const constant_interval_exprt &o) const
{
if(o.is_single_value_interval() && is_single_value_interval())
{
return handle_constant_binary_expression(o, ID_bitand);
}
return top();
}
constant_interval_exprt constant_interval_exprt::bitwise_not() const
{
if(is_single_value_interval())
{
return handle_constant_unary_expression(ID_bitnot);
}
return top();
}
tvt constant_interval_exprt::less_than(const constant_interval_exprt &o) const
{
// [get_lower, get_upper] < [o.get_lower(), o.get_upper()]
if(is_single_value_interval() && o.is_single_value_interval())
{
return tvt(less_than(get_lower(), o.get_lower()));
}
if(less_than(get_upper(), o.get_lower()))
{
return tvt(true);
}
if(greater_than(get_lower(), o.get_upper()))
{
return tvt(false);
}
return tvt::unknown();
}
tvt constant_interval_exprt::greater_than(
const constant_interval_exprt &o) const
{
return o.less_than(*this);
}
tvt constant_interval_exprt::less_than_or_equal(
const constant_interval_exprt &o) const
{
if(is_single_value_interval() && o.is_single_value_interval())
{
return tvt(less_than_or_equal(get_lower(), o.get_lower()));
}
// [get_lower, get_upper] <= [o.get_lower(), o.get_upper()]
if(less_than_or_equal(get_upper(), o.get_lower()))
{
return tvt(true);
}
if(greater_than(get_lower(), o.get_upper()))
{
return tvt(false);
}
return tvt::unknown();
}
tvt constant_interval_exprt::greater_than_or_equal(
const constant_interval_exprt &o) const
{
return o.less_than_or_equal(*this);
}
tvt constant_interval_exprt::equal(const constant_interval_exprt &o) const
{
if(is_single_value_interval() && o.is_single_value_interval())
{
return tvt(equal(get_lower(), o.get_lower()));
}
if(
less_than(o).is_true() || greater_than(o).is_true() ||
o.less_than(*this).is_true() || o.greater_than(*this).is_true())
{
return tvt(false);
}
// Overlapping intervals: some values may be equal, some may not.
return tvt::unknown();
}
tvt constant_interval_exprt::not_equal(const constant_interval_exprt &o) const
{
return !equal(o);
}
constant_interval_exprt constant_interval_exprt::increment() const
{
return plus(
constant_interval_exprt::singleton(from_integer(mp_integer(1), type())));
}
constant_interval_exprt constant_interval_exprt::decrement() const
{
return minus(
constant_interval_exprt::singleton(from_integer(mp_integer(1), type())));
}
constant_interval_exprt constant_interval_exprt::get_extremes(
const constant_interval_exprt &a,
const constant_interval_exprt &b,
const irep_idt &operation)
{
std::vector<exprt> results;
generate_expression(a.get_lower(), b.get_lower(), operation, results);
generate_expression(a.get_lower(), b.get_upper(), operation, results);
generate_expression(a.get_upper(), b.get_lower(), operation, results);
generate_expression(a.get_upper(), b.get_upper(), operation, results);
for(auto result : results)
{
if(!is_extreme(result) && contains_extreme(result))
{
return constant_interval_exprt(result.type());
}
}
exprt min = get_min(results);
exprt max = get_max(results);
return simplified_interval(min, max);
}
exprt constant_interval_exprt::get_extreme(
std::vector<exprt> values,
bool min_value)
{
symbol_tablet symbol_table;
namespacet ns(symbol_table); // Empty
if(values.size() == 0)
{
return nil_exprt();
}
if(values.size() == 1)
{
return *(values.begin());
}
if(values.size() == 2)
{
if(min_value)
{
return get_min(values.front(), values.back());
}
else
{
return get_max(values.front(), values.back());
}
}
typet type = values.begin()->type();
for(auto v : values)
{
if((min_value && is_min(v)) || (!min_value && is_max(v)))
{
return v;
}
}
for(auto left : values)
{
bool all_left_OP_right = true;
for(auto right : values)
{
if(
(min_value && less_than_or_equal(left, right)) ||
(!min_value && greater_than_or_equal(left, right)))
{
continue;
}
all_left_OP_right = false;
break;
}
if(all_left_OP_right)
{
return left;
}
}
/* Return top */
if(min_value)
{
return min_value_exprt(type);
}
else
{
return max_value_exprt(type);
}
UNREACHABLE;
}
void constant_interval_exprt::generate_expression(
const exprt &lhs,
const exprt &rhs,
const irep_idt &operation,
std::vector<exprt> &collection)
{
if(operation == ID_mult)
{
append_multiply_expression(lhs, rhs, collection);
}
else if(operation == ID_div)
{
collection.push_back(generate_division_expression(lhs, rhs));
}
else if(operation == ID_mod)
{
collection.push_back(generate_modulo_expression(lhs, rhs));
}
else if(operation == ID_shl || operation == ID_ashr)
{
collection.push_back(generate_shift_expression(lhs, rhs, operation));
}
}
/// Adds all possible values that may arise from multiplication (more than one,
/// in case of past the type boundary results).
/// \param lower lhs of multiplication
/// \param upper rhs of multiplication
/// \param collection vector of possible values
void constant_interval_exprt::append_multiply_expression(
const exprt &lower,
const exprt &upper,
std::vector<exprt> &collection)
{
PRECONDITION(lower.type().is_not_nil() && is_numeric(lower.type()));
if(is_max(lower))
{
append_multiply_expression_max(upper, collection);
}
else if(is_max(upper))
{
append_multiply_expression_max(lower, collection);
}
else if(is_min(lower))
{
append_multiply_expression_min(lower, upper, collection);
}
else if(is_min(upper))
{
append_multiply_expression_min(upper, lower, collection);
}
else
{
INVARIANT(
!is_extreme(lower) && !is_extreme(upper),
"We ruled out extreme cases beforehand");
auto result = mult_exprt(lower, upper);
collection.push_back(simplified_expr(result));
}
}
/// Appends interval bounds that could arise from MAX * expr. Accommodates for
/// overflows by over-approximating.
/// \param expr the unknown side of multiplication
/// \param collection vector of collected bounds
void constant_interval_exprt::append_multiply_expression_max(
const exprt &expr,
std::vector<exprt> &collection)
{
if(is_min(expr))
{
INVARIANT(!is_positive(expr), "Min value cannot be >0.");
INVARIANT(
is_negative(expr) || is_zero(expr), "Non-negative MIN must be zero.");
}
if(is_zero(expr))
collection.push_back(expr);
else
{
collection.push_back(max_value_exprt(expr));
collection.push_back(min_value_exprt(expr));
}
}
/// Appends interval bounds that could arise from MIN * other. Accommodates for
/// overflows by over-approximating.
/// \param min the side known to be MIN for a given type
/// \param other the side of unknown value
/// \param collection reference to the vector of collected boundaries
void constant_interval_exprt::append_multiply_expression_min(
const exprt &min,
const exprt &other,
std::vector<exprt> &collection)
{
PRECONDITION(is_min(min));
INVARIANT(!is_positive(min), "Min value cannot be >0.");
INVARIANT(is_negative(min) || is_zero(min), "Non-negative MIN must be zero.");
if(is_zero(min))
collection.push_back(min);
else if(is_zero(other))
collection.push_back(other);
else
{
collection.push_back(min_value_exprt(min));
collection.push_back(max_value_exprt(min));
}
}
exprt constant_interval_exprt::generate_division_expression(
const exprt &lhs,
const exprt &rhs)
{
PRECONDITION(lhs.type().is_not_nil() && is_numeric(lhs.type()));
PRECONDITION(!is_zero(rhs));
if(rhs == 1)
{
return lhs;
}
if(is_max(lhs))
{
if(is_negative(rhs))
{
return min_value_exprt(lhs);
}
return lhs;
}
if(is_min(lhs))
{
if(is_negative(rhs))
{
return max_value_exprt(lhs);
}
return lhs;
}
INVARIANT(!is_extreme(lhs), "We ruled out extreme cases beforehand");
if(is_max(rhs))
{
return zero(rhs);
}
if(is_min(rhs))
{
INVARIANT(
is_signed(rhs), "We think this is a signed integer for some reason?");
return zero(rhs);
}
INVARIANT(
!is_extreme(lhs) && !is_extreme(rhs),
"We ruled out extreme cases beforehand");
auto div_expr = div_exprt(lhs, rhs);
return simplified_expr(div_expr);
}
exprt constant_interval_exprt::generate_modulo_expression(
const exprt &lhs,
const exprt &rhs)
{
PRECONDITION(lhs.type().is_not_nil() && is_numeric(lhs.type()));
PRECONDITION(!is_zero(rhs));
if(rhs == 1)
{
return lhs;
}
if(is_max(lhs))
{
if(is_negative(rhs))
{
return min_value_exprt(lhs);
}
return lhs;
}
if(is_min(lhs))
{
if(is_negative(rhs))
{
return max_value_exprt(lhs);
}
return lhs;
}
INVARIANT(!is_extreme(lhs), "We rule out this case beforehand");
if(is_max(rhs))
{
return zero(rhs);
}
if(is_min(rhs))
{
INVARIANT(is_signed(rhs), "We assume this is signed for some reason?");
return zero(rhs);
}
INVARIANT(
!is_extreme(lhs) && !is_extreme(rhs),
"We ruled out extreme values beforehand");
auto modulo_expr = mod_exprt(lhs, rhs);
return simplified_expr(modulo_expr);
}
constant_interval_exprt constant_interval_exprt::eval(const irep_idt &id) const
{
if(id == ID_unary_plus)
{
return unary_plus();
}
if(id == ID_unary_minus)
{
return unary_minus();
}
if(id == ID_bitnot)
{
return bitwise_not();
}
if(id == ID_not)
{
return tvt_to_interval(logical_not());
}
return top();
}
constant_interval_exprt constant_interval_exprt::eval(
const irep_idt &binary_operator,
const constant_interval_exprt &other) const
{
if(binary_operator == ID_plus)
{
return plus(other);
}
if(binary_operator == ID_minus)
{
return minus(other);
}
if(binary_operator == ID_mult)
{
return multiply(other);
}
if(binary_operator == ID_div)
{
return divide(other);
}
if(binary_operator == ID_mod)
{
return modulo(other);
}
if(binary_operator == ID_shl)
{
return left_shift(other);
}
if(binary_operator == ID_ashr)
{
return right_shift(other);
}
if(binary_operator == ID_bitor)
{
return bitwise_or(other);
}
if(binary_operator == ID_bitand)
{
return bitwise_and(other);
}
if(binary_operator == ID_bitxor)
{
return bitwise_xor(other);
}
if(binary_operator == ID_lt)
{
return tvt_to_interval(less_than(other));
}
if(binary_operator == ID_le)
{
return tvt_to_interval(less_than_or_equal(other));
}
if(binary_operator == ID_gt)
{
return tvt_to_interval(greater_than(other));
}
if(binary_operator == ID_ge)
{
return tvt_to_interval(greater_than_or_equal(other));
}
if(binary_operator == ID_equal)
{
return tvt_to_interval(equal(other));
}
if(binary_operator == ID_notequal)
{
return tvt_to_interval(not_equal(other));
}
if(binary_operator == ID_and)
{
return tvt_to_interval(logical_and(other));
}
if(binary_operator == ID_or)
{
return tvt_to_interval(logical_or(other));
}
if(binary_operator == ID_xor)
{
return tvt_to_interval(logical_xor(other));
}
return top();
}
exprt constant_interval_exprt::generate_shift_expression(
const exprt &lhs,
const exprt &rhs,
const irep_idt &operation)
{
PRECONDITION(operation == ID_shl || operation == ID_ashr);
if(is_zero(lhs) || is_zero(rhs))
{
// Shifting zero does nothing.
// Shifting BY zero also does nothing.
return lhs;
}
INVARIANT(!is_negative(rhs), "Should be caught at an earlier stage.");
if(is_max(lhs))
{
return lhs;
}
if(is_min(lhs))
{
return lhs;
}
if(is_max(rhs))
{
return min_value_exprt(rhs);
}
INVARIANT(
!is_extreme(lhs) && !is_extreme(rhs),
"We ruled out extreme cases beforehand");
auto shift_expr = shift_exprt(lhs, operation, rhs);
return simplified_expr(shift_expr);
}
constant_interval_exprt
constant_interval_exprt::handle_constant_unary_expression(
const irep_idt &op) const
{
if(is_single_value_interval())
{
auto expr = unary_exprt(op, get_lower());
return constant_interval_exprt::singleton(simplified_expr(expr));
}
return top();
}
constant_interval_exprt
constant_interval_exprt::handle_constant_binary_expression(
const constant_interval_exprt &other,
const irep_idt &op) const
{
PRECONDITION(is_single_value_interval() && other.is_single_value_interval());
auto expr = binary_exprt(get_lower(), op, other.get_lower());
return constant_interval_exprt::singleton(simplified_expr(expr));
}
exprt constant_interval_exprt::get_max(const exprt &a, const exprt &b)
{
return greater_than(a, b) ? a : b;
}
exprt constant_interval_exprt::get_min(const exprt &a, const exprt &b)
{
return less_than(a, b) ? a : b;
}
exprt constant_interval_exprt::get_min(std::vector<exprt> &values)
{
return get_extreme(values, true);
}
exprt constant_interval_exprt::get_max(std::vector<exprt> &values)
{
return get_extreme(values, false);
}
/* we don't simplify in the constructor otherwise */
constant_interval_exprt
constant_interval_exprt::simplified_interval(exprt &l, exprt &r)
{
return constant_interval_exprt(simplified_expr(l), simplified_expr(r));
}
exprt constant_interval_exprt::simplified_expr(exprt expr)
{
symbol_tablet symbol_table;
const namespacet ns(symbol_table);
PRECONDITION(!contains_extreme(expr));
return simplify_expr(expr, ns);
}
constant_exprt constant_interval_exprt::zero(const typet &type)
{
constant_exprt zero = from_integer(mp_integer(0), type);
INVARIANT(
zero == 0 || (type.id() == ID_bool && zero == false),
"The value created from 0 should be zero or false");
return zero;
}