CBMC
Loading...
Searching...
No Matches
simplify_expr.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9#include "simplify_expr.h"
10
11#include <algorithm>
12
13#include "bitvector_expr.h"
14#include "byte_operators.h"
15#include "c_types.h"
16#include "config.h"
17#include "expr_util.h"
18#include "fixedbv.h"
19#include "floatbv_expr.h"
20#include "invariant.h"
21#include "mathematical_expr.h"
22#include "namespace.h"
23#include "pointer_expr.h"
24#include "pointer_offset_size.h"
25#include "pointer_offset_sum.h"
26#include "rational.h"
27#include "rational_tools.h"
28#include "simplify_utils.h"
29#include "std_expr.h"
30#include "string_expr.h"
31
32// #define DEBUGX
33
34#ifdef DEBUGX
35#include "format_expr.h"
36#include <iostream>
37#endif
38
39#include "simplify_expr_class.h"
40
41// #define USE_CACHE
42
43#ifdef USE_CACHE
45{
46public:
47 #if 1
48 typedef std::unordered_map<
50 #else
51 typedef std::unordered_map<exprt, exprt, irep_hash> containert;
52 #endif
53
55
56 containert &container()
57 {
58 return container_normal;
59 }
60};
61
63#endif
64
66{
67 if(expr.op().is_constant())
68 {
69 const typet &type = to_unary_expr(expr).op().type();
70
71 if(type.id()==ID_floatbv)
72 {
74 value.set_sign(false);
75 return value.to_expr();
76 }
77 else if(type.id()==ID_signedbv ||
78 type.id()==ID_unsignedbv)
79 {
80 auto value = numeric_cast<mp_integer>(to_unary_expr(expr).op());
81 if(value.has_value())
82 {
83 if(*value >= 0)
84 {
85 return to_unary_expr(expr).op();
86 }
87 else
88 {
89 value->negate();
90 return from_integer(*value, type);
91 }
92 }
93 }
94 }
95
96 return unchanged(expr);
97}
98
100{
101 if(expr.op().is_constant())
102 {
103 const typet &type = expr.op().type();
104
105 if(type.id()==ID_floatbv)
106 {
108 return make_boolean_expr(value.get_sign());
109 }
110 else if(type.id()==ID_signedbv ||
111 type.id()==ID_unsignedbv)
112 {
113 const auto value = numeric_cast<mp_integer>(expr.op());
114 if(value.has_value())
115 {
116 return make_boolean_expr(*value >= 0);
117 }
118 }
119 }
120
121 return unchanged(expr);
122}
123
126{
127 const exprt &op = expr.op();
128
129 if(op.is_constant())
130 {
131 const typet &op_type = op.type();
132
133 if(op_type.id() == ID_signedbv || op_type.id() == ID_unsignedbv)
134 {
135 const auto width = to_bitvector_type(op_type).get_width();
136 const auto &value = to_constant_expr(op).get_value();
137 std::size_t result = 0;
138
139 for(std::size_t i = 0; i < width; i++)
140 if(get_bvrep_bit(value, width, i))
141 result++;
142
143 return from_integer(result, expr.type());
144 }
145 }
146
147 return unchanged(expr);
148}
149
152{
153 const bool is_little_endian =
155
156 const auto const_bits_opt = expr2bits(expr.op(), is_little_endian, ns);
157
158 if(!const_bits_opt.has_value())
159 return unchanged(expr);
160
161 std::size_t n_leading_zeros =
162 is_little_endian ? const_bits_opt->rfind('1') : const_bits_opt->find('1');
163 if(n_leading_zeros == std::string::npos)
164 {
165 if(!expr.zero_permitted())
166 return unchanged(expr);
167
169 }
170 else if(is_little_endian)
172
173 return from_integer(n_leading_zeros, expr.type());
174}
175
178{
179 const bool is_little_endian =
181
182 const auto const_bits_opt = expr2bits(expr.op(), is_little_endian, ns);
183
184 if(!const_bits_opt.has_value())
185 return unchanged(expr);
186
187 std::size_t n_trailing_zeros =
188 is_little_endian ? const_bits_opt->find('1') : const_bits_opt->rfind('1');
189 if(n_trailing_zeros == std::string::npos)
190 {
191 if(!expr.zero_permitted())
192 return unchanged(expr);
193
195 }
196 else if(!is_little_endian)
198
199 return from_integer(n_trailing_zeros, expr.type());
200}
201
204{
205 const bool is_little_endian =
207
208 const auto const_bits_opt = expr2bits(expr.op(), is_little_endian, ns);
209
210 if(!const_bits_opt.has_value())
211 return unchanged(expr);
212
213 std::size_t first_one_bit =
214 is_little_endian ? const_bits_opt->find('1') : const_bits_opt->rfind('1');
215 if(first_one_bit == std::string::npos)
216 first_one_bit = 0;
217 else if(is_little_endian)
219 else
221
222 return from_integer(first_one_bit, expr.type());
223}
224
230 const function_application_exprt &expr,
231 const namespacet &ns)
232{
233 const refined_string_exprt &s1 = to_string_expr(expr.arguments().at(0));
234 const auto s1_data_opt = try_get_string_data_array(s1.content(), ns);
235
236 if(!s1_data_opt)
237 return simplify_exprt::unchanged(expr);
238
239 const array_exprt &s1_data = s1_data_opt->get();
240 const refined_string_exprt &s2 = to_string_expr(expr.arguments().at(1));
241 const auto s2_data_opt = try_get_string_data_array(s2.content(), ns);
242
243 if(!s2_data_opt)
244 return simplify_exprt::unchanged(expr);
245
246 const array_exprt &s2_data = s2_data_opt->get();
247 const bool res = s2_data.operands().size() <= s1_data.operands().size() &&
248 std::equal(
249 s2_data.operands().rbegin(),
250 s2_data.operands().rend(),
251 s1_data.operands().rbegin());
252
253 return from_integer(res ? 1 : 0, expr.type());
254}
255
258 const function_application_exprt &expr,
259 const namespacet &ns)
260{
261 // We want to get both arguments of any starts-with comparison, and
262 // trace them back to the actual string instance. All variables on the
263 // way must be constant for us to be sure this will work.
264 auto &first_argument = to_string_expr(expr.arguments().at(0));
265 auto &second_argument = to_string_expr(expr.arguments().at(1));
266
267 const auto first_value_opt =
269
270 if(!first_value_opt)
271 {
272 return simplify_exprt::unchanged(expr);
273 }
274
276
277 const auto second_value_opt =
279
281 {
282 return simplify_exprt::unchanged(expr);
283 }
284
286
287 // Is our 'contains' array directly contained in our target.
288 const bool includes =
289 std::search(
290 first_value.operands().begin(),
291 first_value.operands().end(),
292 second_value.operands().begin(),
293 second_value.operands().end()) != first_value.operands().end();
294
295 return from_integer(includes ? 1 : 0, expr.type());
296}
297
303 const function_application_exprt &expr,
304 const namespacet &ns)
305{
308 const refined_string_exprt &s =
309 to_string_expr(function_app.arguments().at(0));
310
311 if(!s.length().is_constant())
312 return simplify_exprt::unchanged(expr);
313
314 const auto numeric_length =
316
317 return from_integer(numeric_length == 0 ? 1 : 0, expr.type());
318}
319
328 const function_application_exprt &expr,
329 const namespacet &ns)
330{
331 const refined_string_exprt &s1 = to_string_expr(expr.arguments().at(0));
332 const auto s1_data_opt = try_get_string_data_array(s1.content(), ns);
333
334 if(!s1_data_opt)
335 return simplify_exprt::unchanged(expr);
336
337 const refined_string_exprt &s2 = to_string_expr(expr.arguments().at(1));
338 const auto s2_data_opt = try_get_string_data_array(s2.content(), ns);
339
340 if(!s2_data_opt)
341 return simplify_exprt::unchanged(expr);
342
343 const array_exprt &s1_data = s1_data_opt->get();
344 const array_exprt &s2_data = s2_data_opt->get();
345
346 if(s1_data.operands() == s2_data.operands())
347 return from_integer(0, expr.type());
348
349 const mp_integer s1_size = s1_data.operands().size();
350 const mp_integer s2_size = s2_data.operands().size();
351 const bool first_shorter = s1_size < s2_size;
352 const exprt::operandst &ops1 =
353 first_shorter ? s1_data.operands() : s2_data.operands();
354 const exprt::operandst &ops2 =
355 first_shorter ? s2_data.operands() : s1_data.operands();
356 auto it_pair = std::mismatch(ops1.begin(), ops1.end(), ops2.begin());
357
358 if(it_pair.first == ops1.end())
359 return from_integer(s1_size - s2_size, expr.type());
360
361 const mp_integer char1 =
363 const mp_integer char2 =
365
366 return from_integer(
367 first_shorter ? char1 - char2 : char2 - char1, expr.type());
368}
369
377 const function_application_exprt &expr,
378 const namespacet &ns,
379 const bool search_from_end)
380{
381 std::size_t starting_index = 0;
382
383 // Determine starting index for the comparison (if given)
384 if(expr.arguments().size() == 3)
385 {
386 auto &starting_index_expr = expr.arguments().at(2);
387
388 if(!starting_index_expr.is_constant())
389 {
390 return simplify_exprt::unchanged(expr);
391 }
392
393 const mp_integer idx =
395
396 // Negative indices are treated like 0
397 if(idx > 0)
398 {
400 }
401 }
402
403 const refined_string_exprt &s1 = to_string_expr(expr.arguments().at(0));
404
405 const auto s1_data_opt = try_get_string_data_array(s1.content(), ns);
406
407 if(!s1_data_opt)
408 {
409 return simplify_exprt::unchanged(expr);
410 }
411
412 const array_exprt &s1_data = s1_data_opt->get();
413
414 const auto search_string_size = s1_data.operands().size();
416 {
417 return from_integer(-1, expr.type());
418 }
419
420 unsigned long starting_offset =
423 {
424 // Second argument is a string
425
426 const refined_string_exprt &s2 = to_string_expr(expr.arguments().at(1));
427
428 const auto s2_data_opt = try_get_string_data_array(s2.content(), ns);
429
430 if(!s2_data_opt)
431 {
432 return simplify_exprt::unchanged(expr);
433 }
434
435 const array_exprt &s2_data = s2_data_opt->get();
436
437 // Searching for empty string is a special case and is simply the
438 // "always found at the first searched position. This needs to take into
439 // account starting position and if you're starting from the beginning or
440 // end.
441 if(s2_data.operands().empty())
442 return from_integer(
445 : 0,
446 expr.type());
447
449 {
450 auto end = std::prev(s1_data.operands().end(), starting_offset);
451 auto it = std::find_end(
452 s1_data.operands().begin(),
453 end,
454 s2_data.operands().begin(),
455 s2_data.operands().end());
456 if(it != end)
457 return from_integer(
458 std::distance(s1_data.operands().begin(), it), expr.type());
459 }
460 else
461 {
462 auto it = std::search(
463 std::next(s1_data.operands().begin(), starting_index),
464 s1_data.operands().end(),
465 s2_data.operands().begin(),
466 s2_data.operands().end());
467
468 if(it != s1_data.operands().end())
469 return from_integer(
470 std::distance(s1_data.operands().begin(), it), expr.type());
471 }
472 }
473 else if(expr.arguments().at(1).is_constant())
474 {
475 // Second argument is a constant character
476
477 const constant_exprt &c1 = to_constant_expr(expr.arguments().at(1));
479
480 auto pred = [&](const exprt &c2) {
482
483 return c1_val == c2_val;
484 };
485
487 {
488 auto it = std::find_if(
489 std::next(s1_data.operands().rbegin(), starting_offset),
490 s1_data.operands().rend(),
491 pred);
492 if(it != s1_data.operands().rend())
493 return from_integer(
494 std::distance(s1_data.operands().begin(), it.base() - 1),
495 expr.type());
496 }
497 else
498 {
499 auto it = std::find_if(
500 std::next(s1_data.operands().begin(), starting_index),
501 s1_data.operands().end(),
502 pred);
503 if(it != s1_data.operands().end())
504 return from_integer(
505 std::distance(s1_data.operands().begin(), it), expr.type());
506 }
507 }
508 else
509 {
510 return simplify_exprt::unchanged(expr);
511 }
512
513 return from_integer(-1, expr.type());
514}
515
522 const function_application_exprt &expr,
523 const namespacet &ns)
524{
525 if(!expr.arguments().at(1).is_constant())
526 {
527 return simplify_exprt::unchanged(expr);
528 }
529
530 const auto &index = to_constant_expr(expr.arguments().at(1));
531
532 const refined_string_exprt &s = to_string_expr(expr.arguments().at(0));
533
534 const auto char_seq_opt = try_get_string_data_array(s.content(), ns);
535
536 if(!char_seq_opt)
537 {
538 return simplify_exprt::unchanged(expr);
539 }
540
541 const array_exprt &char_seq = char_seq_opt->get();
542
543 const auto i_opt = numeric_cast<std::size_t>(index);
544
545 if(!i_opt || *i_opt >= char_seq.operands().size())
546 {
547 return simplify_exprt::unchanged(expr);
548 }
549
550 const auto &c = to_constant_expr(char_seq.operands().at(*i_opt));
551
552 if(c.type() != expr.type())
553 {
554 return simplify_exprt::unchanged(expr);
555 }
556
557 return c;
558}
559
562{
563 auto &operands = string_data.operands();
564 for(auto &operand : operands)
565 {
568
569 // Can't guarantee matches against non-ASCII characters.
570 if(character >= 128)
571 return false;
572
573 if(isalpha(character))
574 {
575 if(isupper(character))
577 from_integer(tolower(character), constant_value.type());
578 }
579 }
580
581 return true;
582}
583
590 const function_application_exprt &expr,
591 const namespacet &ns)
592{
593 // We want to get both arguments of any starts-with comparison, and
594 // trace them back to the actual string instance. All variables on the
595 // way must be constant for us to be sure this will work.
596 auto &first_argument = to_string_expr(expr.arguments().at(0));
597 auto &second_argument = to_string_expr(expr.arguments().at(1));
598
599 const auto first_value_opt =
601
602 if(!first_value_opt)
603 {
604 return simplify_exprt::unchanged(expr);
605 }
606
608
609 const auto second_value_opt =
611
613 {
614 return simplify_exprt::unchanged(expr);
615 }
616
618
619 // Just lower-case both expressions.
620 if(
623 return simplify_exprt::unchanged(expr);
624
625 bool is_equal = first_value == second_value;
626 return from_integer(is_equal ? 1 : 0, expr.type());
627}
628
635 const function_application_exprt &expr,
636 const namespacet &ns)
637{
638 // We want to get both arguments of any starts-with comparison, and
639 // trace them back to the actual string instance. All variables on the
640 // way must be constant for us to be sure this will work.
641 auto &first_argument = to_string_expr(expr.arguments().at(0));
642 auto &second_argument = to_string_expr(expr.arguments().at(1));
643
644 const auto first_value_opt =
646
647 if(!first_value_opt)
648 {
649 return simplify_exprt::unchanged(expr);
650 }
651
653
654 const auto second_value_opt =
656
658 {
659 return simplify_exprt::unchanged(expr);
660 }
661
663
665 if(expr.arguments().size() == 3)
666 {
667 auto &offset = expr.arguments()[2];
668 if(!offset.is_constant())
669 return simplify_exprt::unchanged(expr);
671 }
672
673 // test whether second_value is a prefix of first_value
674 bool is_prefix =
675 offset_int >= 0 && mp_integer(first_value.operands().size()) >=
676 offset_int + second_value.operands().size();
677 if(is_prefix)
678 {
679 exprt::operandst::const_iterator second_it =
680 second_value.operands().begin();
681 for(const auto &first_op : first_value.operands())
682 {
683 if(offset_int > 0)
684 --offset_int;
685 else if(second_it == second_value.operands().end())
686 break;
687 else if(first_op != *second_it)
688 {
689 is_prefix = false;
690 break;
691 }
692 else
693 ++second_it;
694 }
695 }
696
697 return from_integer(is_prefix ? 1 : 0, expr.type());
698}
699
701 const function_application_exprt &expr)
702{
703 if(expr.function().id() == ID_lambda)
704 {
705 // expand the function application
706 return to_lambda_expr(expr.function()).application(expr.arguments());
707 }
708
709 if(expr.function().id() != ID_symbol)
710 return unchanged(expr);
711
712 const irep_idt &func_id = to_symbol_expr(expr.function()).get_identifier();
713
714 // String.startsWith() is used to implement String.equals() in the models
715 // library
717 {
718 return simplify_string_startswith(expr, ns);
719 }
721 {
722 return simplify_string_endswith(expr, ns);
723 }
725 {
726 return simplify_string_is_empty(expr, ns);
727 }
729 {
730 return simplify_string_compare_to(expr, ns);
731 }
733 {
734 return simplify_string_index_of(expr, ns, false);
735 }
737 {
738 return simplify_string_char_at(expr, ns);
739 }
741 {
742 return simplify_string_contains(expr, ns);
743 }
745 {
746 return simplify_string_index_of(expr, ns, true);
747 }
749 {
751 }
752
753 return unchanged(expr);
754}
755
758{
759 const typet &expr_type = expr.type();
760 const typet &op_type = expr.op().type();
761
762 // eliminate casts of infinity
763 if(expr.op().id() == ID_infinity)
764 {
765 typet new_type=expr.type();
766 exprt tmp = expr.op();
767 tmp.type()=new_type;
768 return std::move(tmp);
769 }
770
771 // casts from NULL to any integer
772 if(
773 op_type.id() == ID_pointer && expr.op().is_constant() &&
774 to_constant_expr(expr.op()).get_value() == ID_NULL &&
775 (expr_type.id() == ID_unsignedbv || expr_type.id() == ID_signedbv) &&
776 config.ansi_c.NULL_is_zero)
777 {
778 return from_integer(0, expr_type);
779 }
780
781 // casts from pointer to integer
782 // where width of integer >= width of pointer
783 // (void*)(intX)expr -> (void*)expr
784 if(
785 expr_type.id() == ID_pointer && expr.op().id() == ID_typecast &&
786 (op_type.id() == ID_signedbv || op_type.id() == ID_unsignedbv ||
787 op_type.id() == ID_bv) &&
788 to_bitvector_type(op_type).get_width() >=
789 to_bitvector_type(expr_type).get_width())
790 {
791 auto new_expr = expr;
792 new_expr.op() = to_typecast_expr(expr.op()).op();
793 return changed(simplify_typecast(new_expr)); // rec. call
794 }
795
796 // eliminate redundant typecasts
797 if(expr.type() == expr.op().type())
798 {
799 return expr.op();
800 }
801
802 // eliminate casts to proper bool
803 if(expr_type.id()==ID_bool)
804 {
805 // rewrite (bool)x to x!=0
807 expr.op(),
810 inequality.add_source_location()=expr.source_location();
812 }
813
814 // eliminate casts from proper bool
815 if(
816 op_type.id() == ID_bool &&
817 (expr_type.id() == ID_signedbv || expr_type.id() == ID_unsignedbv ||
818 expr_type.id() == ID_c_bool || expr_type.id() == ID_c_bit_field))
819 {
820 // rewrite (T)(bool) to bool?1:0
821 auto one = from_integer(1, expr_type);
822 auto zero = from_integer(0, expr_type);
824 if_exprt{expr.op(), std::move(one), std::move(zero)}));
825 }
826
827 // circular casts through types shorter than `int`
828 // we use fixed bit widths as this is specifically for the Java bytecode
829 // front-end
830 if(op_type == signedbv_typet(32) && expr.op().id() == ID_typecast)
831 {
832 if(expr_type==c_bool_typet(8) ||
836 {
837 // We checked that the id was ID_typecast in the enclosing `if`
838 const auto &typecast = expr_checked_cast<typecast_exprt>(expr.op());
839 if(typecast.op().type()==expr_type)
840 {
841 return typecast.op();
842 }
843 }
844 }
845
846 // eliminate casts to _Bool
847 if(expr_type.id()==ID_c_bool &&
848 op_type.id()!=ID_bool)
849 {
850 // rewrite (_Bool)x to (_Bool)(x!=0)
851 exprt inequality = is_not_zero(expr.op(), ns);
852 auto new_expr = expr;
853 new_expr.op() = simplify_node(std::move(inequality));
854 return changed(simplify_typecast(new_expr)); // recursive call
855 }
856
857 // eliminate typecasts from NULL
858 if(
859 expr_type.id() == ID_pointer && expr.op().is_constant() &&
860 (to_constant_expr(expr.op()).get_value() == ID_NULL ||
861 (expr.op().is_zero() && config.ansi_c.NULL_is_zero)))
862 {
863 exprt tmp = expr.op();
864 tmp.type()=expr.type();
865 to_constant_expr(tmp).set_value(ID_NULL);
866 return std::move(tmp);
867 }
868
869 // eliminate duplicate pointer typecasts
870 // (T1 *)(T2 *)x -> (T1 *)x
871 if(
872 expr_type.id() == ID_pointer && expr.op().id() == ID_typecast &&
873 op_type.id() == ID_pointer)
874 {
875 auto new_expr = expr;
876 new_expr.op() = to_typecast_expr(expr.op()).op();
877 return changed(simplify_typecast(new_expr)); // recursive call
878 }
879
880 // casts from integer to pointer and back:
881 // (int)(void *)int -> (int)(size_t)int
882 if(
883 (expr_type.id() == ID_signedbv || expr_type.id() == ID_unsignedbv) &&
884 expr.op().id() == ID_typecast && expr.op().operands().size() == 1 &&
885 op_type.id() == ID_pointer)
886 {
887 auto inner_cast = to_typecast_expr(expr.op());
888 inner_cast.type() = size_type();
889
890 auto outer_cast = expr;
891 outer_cast.op() = simplify_typecast(inner_cast); // rec. call
892 return changed(simplify_typecast(outer_cast)); // rec. call
893 }
894
895 // mildly more elaborate version of the above:
896 // (int)((T*)0 + int) -> (int)(sizeof(T)*(size_t)int) if NULL is zero
897 if(
898 config.ansi_c.NULL_is_zero &&
899 (expr_type.id() == ID_signedbv || expr_type.id() == ID_unsignedbv) &&
900 op_type.id() == ID_pointer && expr.op().id() == ID_plus &&
901 expr.op().operands().size() == 2)
902 {
903 const auto &op_plus_expr = to_plus_expr(expr.op());
904
905 if(
906 (op_plus_expr.op0().id() == ID_typecast &&
907 to_typecast_expr(op_plus_expr.op0()).op().is_zero()) ||
908 (op_plus_expr.op0().is_constant() &&
909 to_constant_expr(op_plus_expr.op0()).is_null_pointer()))
910 {
911 auto sub_size =
913 if(sub_size.has_value())
914 {
915 auto new_expr = expr;
918
919 // void*
920 if(*sub_size == 0 || *sub_size == 1)
921 new_expr.op() = offset_expr;
922 else
923 {
924 new_expr.op() = simplify_mult(
926 }
927
928 return changed(simplify_typecast(new_expr)); // rec. call
929 }
930 }
931 }
932
933 // Push a numerical typecast into various integer operations, i.e.,
934 // (T)(x OP y) ---> (T)x OP (T)y
935 //
936 // Doesn't work for many, e.g., pointer difference, floating-point,
937 // division, modulo.
938 // Many operations fail if the width of T
939 // is bigger than that of (x OP y). This includes ID_bitnot and
940 // anything that might overflow, e.g., ID_plus.
941 //
942 if((expr_type.id()==ID_signedbv || expr_type.id()==ID_unsignedbv) &&
943 (op_type.id()==ID_signedbv || op_type.id()==ID_unsignedbv))
944 {
945 bool enlarge=
946 to_bitvector_type(expr_type).get_width()>
947 to_bitvector_type(op_type).get_width();
948
949 if(!enlarge)
950 {
951 irep_idt op_id = expr.op().id();
952
953 if(
954 op_id == ID_plus || op_id == ID_minus || op_id == ID_mult ||
955 op_id == ID_unary_minus || op_id == ID_bitxor || op_id == ID_bitxnor ||
956 op_id == ID_bitor || op_id == ID_bitand)
957 {
958 exprt result = expr.op();
959
960 if(
961 result.operands().size() >= 1 &&
962 to_multi_ary_expr(result).op0().type() == result.type())
963 {
964 result.type()=expr.type();
965
966 Forall_operands(it, result)
967 {
968 auto new_operand = typecast_exprt(*it, expr.type());
969 *it = simplify_typecast(new_operand); // recursive call
970 }
971
972 return changed(simplify_node(result)); // possibly recursive call
973 }
974 }
975 else if(op_id==ID_ashr || op_id==ID_lshr || op_id==ID_shl)
976 {
977 }
978 }
979 }
980
981 // Push a numerical typecast into pointer arithmetic
982 // (T)(ptr + int) ---> (T)((size_t)ptr + sizeof(subtype)*(size_t)int)
983 //
984 if(
985 (expr_type.id() == ID_signedbv || expr_type.id() == ID_unsignedbv) &&
986 op_type.id() == ID_pointer && expr.op().id() == ID_plus)
987 {
988 const auto step =
990
991 if(step.has_value() && *step != 0)
992 {
993 const typet size_t_type(size_type());
994 auto new_expr = expr;
995
996 new_expr.op().type() = size_t_type;
997
998 for(auto &op : new_expr.op().operands())
999 {
1001 if(op.type().id() != ID_pointer && *step > 1)
1002 {
1003 new_op =
1005 }
1006 op = std::move(new_op);
1007 }
1008
1010
1011 return changed(simplify_typecast(new_expr)); // recursive call
1012 }
1013 }
1014
1015 const irep_idt &expr_type_id=expr_type.id();
1016 const exprt &operand = expr.op();
1017 const irep_idt &op_type_id=op_type.id();
1018
1019 if(operand.is_constant())
1020 {
1021 const irep_idt &value=to_constant_expr(operand).get_value();
1022
1023 // preserve the sizeof type annotation
1025 static_cast<const typet &>(operand.find(ID_C_c_sizeof_type));
1026
1027 if(op_type_id==ID_integer ||
1029 {
1030 // from integer to ...
1031
1033
1035 {
1036 return make_boolean_expr(int_value != 0);
1037 }
1038
1044 {
1046 }
1047 else if(expr_type_id == ID_c_enum_tag)
1048 {
1049 const auto &c_enum_type = ns.follow_tag(to_c_enum_tag_type(expr_type));
1050 if(!c_enum_type.is_incomplete()) // possibly incomplete
1051 {
1053 tmp.type() = expr_type; // we maintain the tag type
1054 return std::move(tmp);
1055 }
1056 }
1057 }
1058 else if(op_type_id==ID_rational)
1059 {
1060 }
1061 else if(op_type_id==ID_real)
1062 {
1063 }
1064 else if(op_type_id==ID_bool)
1065 {
1074 {
1075 if(operand.is_true())
1076 {
1077 return from_integer(1, expr_type);
1078 }
1079 else if(operand.is_false())
1080 {
1081 return from_integer(0, expr_type);
1082 }
1083 }
1084 else if(expr_type_id==ID_c_enum_tag)
1085 {
1086 const auto &c_enum_type = ns.follow_tag(to_c_enum_tag_type(expr_type));
1087 if(!c_enum_type.is_incomplete()) // possibly incomplete
1088 {
1089 unsigned int_value = operand.is_true() ? 1u : 0u;
1091 tmp.type()=expr_type; // we maintain the tag type
1092 return std::move(tmp);
1093 }
1094 }
1095 else if(expr_type_id==ID_pointer &&
1096 operand.is_false() &&
1097 config.ansi_c.NULL_is_zero)
1098 {
1100 }
1101 }
1102 else if(op_type_id==ID_unsignedbv ||
1106 {
1108
1110 return unchanged(expr);
1111
1113 {
1114 return make_boolean_expr(int_value != 0);
1115 }
1116
1118 {
1119 return from_integer(int_value != 0, expr_type);
1120 }
1121
1123 {
1125 }
1126
1128 {
1129 if(int_value>=0)
1130 {
1132 }
1133 }
1134
1139 {
1140 auto result = from_integer(int_value, expr_type);
1141
1142 if(c_sizeof_type.is_not_nil())
1143 result.set(ID_C_c_sizeof_type, c_sizeof_type);
1144
1145 return std::move(result);
1146 }
1147
1149 {
1150 const auto &c_enum_type = ns.follow_tag(to_c_enum_tag_type(expr_type));
1151 if(!c_enum_type.is_incomplete()) // possibly incomplete
1152 {
1154 tmp.type()=expr_type; // we maintain the tag type
1155 return std::move(tmp);
1156 }
1157 }
1158
1160 {
1162 }
1163
1165 {
1166 // int to float
1169
1170 fixedbvt f;
1173 return f.to_expr();
1174 }
1175
1177 {
1178 // int to float
1181
1185
1186 return f.to_expr();
1187 }
1188
1190 {
1192 return from_rational(r);
1193 }
1194 }
1195 else if(op_type_id==ID_fixedbv)
1196 {
1199 {
1200 // cast from fixedbv to int
1201 fixedbvt f(to_constant_expr(expr.op()));
1202 return from_integer(f.to_integer(), expr_type);
1203 }
1204 else if(expr_type_id==ID_fixedbv)
1205 {
1206 // fixedbv to fixedbv
1207 fixedbvt f(to_constant_expr(expr.op()));
1209 return f.to_expr();
1210 }
1211 else if(expr_type_id == ID_bv)
1212 {
1213 fixedbvt f{to_constant_expr(expr.op())};
1214 return from_integer(f.get_value(), expr_type);
1215 }
1216 }
1217 else if(op_type_id==ID_floatbv)
1218 {
1219 ieee_floatt f(
1220 to_constant_expr(expr.op()),
1222
1225 {
1226 // cast from float to int
1227 return from_integer(f.to_integer(), expr_type);
1228 }
1229 else if(expr_type_id==ID_floatbv)
1230 {
1231 // float to double or double to float
1233 return f.to_expr();
1234 }
1235 else if(expr_type_id==ID_fixedbv)
1236 {
1240 factor.from_integer(power(2, fixedbv.spec.get_fraction_bits()));
1241 f*=factor;
1242 fixedbv.set_value(f.to_integer());
1243 return fixedbv.to_expr();
1244 }
1245 else if(expr_type_id == ID_bv)
1246 {
1247 return from_integer(f.pack(), expr_type);
1248 }
1249 }
1250 else if(op_type_id==ID_bv)
1251 {
1252 if(
1256 {
1257 const auto width = to_bv_type(op_type).get_width();
1258 const auto int_value = bvrep2integer(value, width, false);
1261 else
1262 {
1264 auto result = from_integer(int_value, ns.follow_tag(tag_type));
1265 result.type() = tag_type;
1266 return std::move(result);
1267 }
1268 }
1269 else if(expr_type_id == ID_floatbv)
1270 {
1271 const auto width = to_bv_type(op_type).get_width();
1272 const auto int_value = bvrep2integer(value, width, false);
1274 ieee_float.unpack(int_value);
1275 return ieee_float.to_expr();
1276 }
1277 else if(expr_type_id == ID_fixedbv)
1278 {
1279 const auto width = to_bv_type(op_type).get_width();
1280 const auto int_value = bvrep2integer(value, width, false);
1282 fixedbv.set_value(int_value);
1283 return fixedbv.to_expr();
1284 }
1285 }
1286 else if(op_type_id==ID_c_enum_tag) // enum to int
1287 {
1288 const typet &base_type =
1289 ns.follow_tag(to_c_enum_tag_type(op_type)).underlying_type();
1290 if(base_type.id()==ID_signedbv || base_type.id()==ID_unsignedbv)
1291 {
1292 // enum constants use the representation of their base type
1293 auto new_expr = expr;
1294 new_expr.op().type() = base_type;
1295 return changed(simplify_typecast(new_expr)); // recursive call
1296 }
1297 }
1298 else if(op_type_id==ID_c_enum) // enum to int
1299 {
1300 const typet &base_type = to_c_enum_type(op_type).underlying_type();
1301 if(base_type.id()==ID_signedbv || base_type.id()==ID_unsignedbv)
1302 {
1303 // enum constants use the representation of their base type
1304 auto new_expr = expr;
1305 new_expr.op().type() = base_type;
1306 return changed(simplify_typecast(new_expr)); // recursive call
1307 }
1308 }
1309 }
1310 else if(operand.id()==ID_typecast) // typecast of typecast
1311 {
1312 // (T1)(T2)x ---> (T1)
1313 // where T1 has fewer bits than T2
1314 if(
1317 expr_type_id == ID_bv) &&
1318 to_bitvector_type(expr_type).get_width() <=
1319 to_bitvector_type(operand.type()).get_width())
1320 {
1321 auto new_expr = expr;
1322 new_expr.op() = to_typecast_expr(operand).op();
1323 // might enable further simplification
1324 return changed(simplify_typecast(new_expr)); // recursive call
1325 }
1326 }
1327 else if(operand.id()==ID_address_of)
1328 {
1329 const exprt &o=to_address_of_expr(operand).object();
1330
1331 // turn &array into &array[0] when casting to pointer-to-element-type
1332 if(
1333 o.type().id() == ID_array &&
1334 expr_type == pointer_type(to_array_type(o.type()).element_type()))
1335 {
1336 auto result =
1338
1339 return changed(simplify_address_of(result)); // recursive call
1340 }
1341 }
1342
1343 return unchanged(expr);
1344}
1345
1348{
1349 const typet &expr_type = expr.type();
1350 const typet &op_type = expr.op().type();
1351
1352 // (T)(a?b:c) --> a?(T)b:(T)c; don't do this for floating-point type casts as
1353 // the type cast itself may be costly
1354 if(
1355 expr.op().id() == ID_if && expr_type.id() != ID_floatbv &&
1356 op_type.id() != ID_floatbv)
1357 {
1358 if_exprt if_expr = lift_if(expr, 0);
1360 }
1361 else
1362 {
1363 auto r_it = simplify_rec(expr.op()); // recursive call
1364 if(r_it.has_changed())
1365 {
1366 auto tmp = expr;
1367 tmp.op() = r_it.expr;
1368 return tmp;
1369 }
1370 }
1371
1372 return unchanged(expr);
1373}
1374
1377{
1378 const exprt &pointer = expr.pointer();
1379
1380 if(pointer.type().id()!=ID_pointer)
1381 return unchanged(expr);
1382
1383 if(pointer.id()==ID_address_of)
1384 {
1385 exprt tmp=to_address_of_expr(pointer).object();
1386 // one address_of is gone, try again
1387 return changed(simplify_rec(tmp));
1388 }
1389 // rewrite *(&a[0] + x) to a[x]
1390 else if(
1391 pointer.id() == ID_plus && pointer.operands().size() == 2 &&
1392 to_plus_expr(pointer).op0().id() == ID_address_of)
1393 {
1394 const auto &pointer_plus_expr = to_plus_expr(pointer);
1395
1398
1399 if(address_of.object().id()==ID_index)
1400 {
1401 const index_exprt &old=to_index_expr(address_of.object());
1402 if(old.array().type().id() == ID_array)
1403 {
1404 index_exprt idx(
1405 old.array(),
1407 to_array_type(old.array().type()).element_type());
1408 return changed(simplify_rec(idx));
1409 }
1410 }
1411 }
1412
1413 return unchanged(expr);
1414}
1415
1418{
1419 const exprt &pointer = expr.pointer();
1420
1421 if(pointer.id() == ID_if)
1422 {
1423 if_exprt if_expr = lift_if(expr, 0);
1425 }
1426 else
1427 {
1428 auto r_it = simplify_rec(pointer); // recursive call
1429 if(r_it.has_changed())
1430 {
1431 auto tmp = expr;
1432 tmp.pointer() = r_it.expr;
1433 return tmp;
1434 }
1435 }
1436
1437 return unchanged(expr);
1438}
1439
1442{
1443 return unchanged(expr);
1444}
1445
1447{
1448 bool no_change = true;
1449
1450 if((expr.operands().size()%2)!=1)
1451 return unchanged(expr);
1452
1453 // copy
1454 auto with_expr = expr;
1455
1456 // now look at first operand
1457
1458 if(
1459 with_expr.old().type().id() == ID_struct ||
1460 with_expr.old().type().id() == ID_struct_tag)
1461 {
1462 if(with_expr.old().id() == ID_struct || with_expr.old().is_constant())
1463 {
1464 while(with_expr.operands().size() > 1)
1465 {
1466 const irep_idt &component_name =
1467 with_expr.where().get(ID_component_name);
1468
1470 with_expr.old().type().id() == ID_struct_tag
1471 ? ns.follow_tag(to_struct_tag_type(with_expr.old().type()))
1472 : to_struct_type(with_expr.old().type());
1473 if(!old_type_followed.has_component(component_name))
1474 return unchanged(expr);
1475
1476 std::size_t number = old_type_followed.component_number(component_name);
1477
1478 if(number >= with_expr.old().operands().size())
1479 return unchanged(expr);
1480
1481 with_expr.old().operands()[number].swap(with_expr.new_value());
1482
1483 with_expr.operands().erase(++with_expr.operands().begin());
1484 with_expr.operands().erase(++with_expr.operands().begin());
1485
1486 no_change = false;
1487 }
1488 }
1489 }
1490 else if(
1491 with_expr.old().type().id() == ID_array ||
1492 with_expr.old().type().id() == ID_vector)
1493 {
1494 if(
1495 with_expr.old().id() == ID_array || with_expr.old().is_constant() ||
1496 with_expr.old().id() == ID_vector)
1497 {
1498 while(with_expr.operands().size() > 1)
1499 {
1500 const auto i = numeric_cast<mp_integer>(with_expr.where());
1501
1502 if(!i.has_value())
1503 break;
1504
1505 if(*i < 0 || *i >= with_expr.old().operands().size())
1506 break;
1507
1508 with_expr.old().operands()[numeric_cast_v<std::size_t>(*i)].swap(
1509 with_expr.new_value());
1510
1511 with_expr.operands().erase(++with_expr.operands().begin());
1512 with_expr.operands().erase(++with_expr.operands().begin());
1513
1514 no_change = false;
1515 }
1516 }
1517 }
1518
1519 if(with_expr.operands().size() == 1)
1520 return with_expr.old();
1521
1522 if(no_change)
1523 return unchanged(expr);
1524 else
1525 return std::move(with_expr);
1526}
1527
1530{
1531 // this is to push updates into (possibly nested) constants
1532
1533 const exprt::operandst &designator = expr.designator();
1534
1535 exprt updated_value = expr.old();
1537
1538 for(const auto &e : designator)
1539 {
1540 if(e.id()==ID_index_designator &&
1541 value_ptr->id()==ID_array)
1542 {
1543 const auto i = numeric_cast<mp_integer>(to_index_designator(e).index());
1544
1545 if(!i.has_value())
1546 return unchanged(expr);
1547
1548 if(*i < 0 || *i >= value_ptr->operands().size())
1549 return unchanged(expr);
1550
1552 }
1553 else if(e.id()==ID_member_designator &&
1554 value_ptr->id()==ID_struct)
1555 {
1556 const irep_idt &component_name=
1557 e.get(ID_component_name);
1559 value_ptr->type().id() == ID_struct_tag
1560 ? ns.follow_tag(to_struct_tag_type(value_ptr->type()))
1561 : to_struct_type(value_ptr->type());
1562 if(!value_ptr_struct_type.has_component(component_name))
1563 return unchanged(expr);
1565 value_ptr = &designator_as_struct_expr.component(component_name, ns);
1566 CHECK_RETURN(value_ptr->is_not_nil());
1567 }
1568 else
1569 return unchanged(expr); // give up, unknown designator
1570 }
1571
1572 // found, done
1573 *value_ptr = expr.new_value();
1574 return updated_value;
1575}
1576
1578{
1579 if(expr.id()==ID_plus)
1580 {
1581 if(expr.type().id()==ID_pointer)
1582 {
1583 // kill integers from sum
1584 for(auto &op : expr.operands())
1585 if(op.type().id() == ID_pointer)
1586 return changed(simplify_object(op)); // recursive call
1587 }
1588 }
1589 else if(expr.id()==ID_typecast)
1590 {
1591 auto const &typecast_expr = to_typecast_expr(expr);
1592 const typet &op_type = typecast_expr.op().type();
1593
1594 if(op_type.id()==ID_pointer)
1595 {
1596 // cast from pointer to pointer
1597 return changed(simplify_object(typecast_expr.op())); // recursive call
1598 }
1599 else if(op_type.id()==ID_signedbv || op_type.id()==ID_unsignedbv)
1600 {
1601 // cast from integer to pointer
1602
1603 // We do a bit of special treatment for (TYPE *)(a+(int)&o) and
1604 // (TYPE *)(a+(int)((T*)&o+x)), which are re-written to '&o'.
1605
1606 const exprt &casted_expr = typecast_expr.op();
1607 if(casted_expr.id() == ID_plus && casted_expr.operands().size() == 2)
1608 {
1609 const auto &plus_expr = to_plus_expr(casted_expr);
1610
1611 const exprt &cand = plus_expr.op0().id() == ID_typecast
1612 ? plus_expr.op0()
1613 : plus_expr.op1();
1614
1615 if(cand.id() == ID_typecast)
1616 {
1617 const auto &typecast_op = to_typecast_expr(cand).op();
1618
1619 if(typecast_op.id() == ID_address_of)
1620 {
1621 return typecast_op;
1622 }
1623 else if(
1624 typecast_op.id() == ID_plus && typecast_op.operands().size() == 2 &&
1625 to_plus_expr(typecast_op).op0().id() == ID_typecast &&
1626 to_typecast_expr(to_plus_expr(typecast_op).op0()).op().id() ==
1628 {
1629 return to_typecast_expr(to_plus_expr(typecast_op).op0()).op();
1630 }
1631 }
1632 }
1633 }
1634 }
1635 else if(expr.id()==ID_address_of)
1636 {
1637 const auto &object = to_address_of_expr(expr).object();
1638
1639 if(object.id() == ID_index)
1640 {
1641 // &some[i] -> &some
1642 address_of_exprt new_expr(to_index_expr(object).array());
1643 return changed(simplify_object(new_expr)); // recursion
1644 }
1645 else if(object.id() == ID_member)
1646 {
1647 // &some.f -> &some
1648 address_of_exprt new_expr(to_member_expr(object).compound());
1649 return changed(simplify_object(new_expr)); // recursion
1650 }
1651 }
1652
1653 return unchanged(expr);
1654}
1655
1658{
1659 // lift up any ID_if on the object
1660 if(expr.op().id() == ID_if)
1661 {
1662 if_exprt if_expr = lift_if(expr, 0);
1663 if_expr.true_case() =
1665 if_expr.false_case() =
1667 return changed(simplify_if(if_expr));
1668 }
1669
1670 const auto el_size = pointer_offset_bits(expr.type(), ns);
1671 if(el_size.has_value() && *el_size < 0)
1672 return unchanged(expr);
1673
1674 // byte_extract(byte_extract(root, offset1), offset2) =>
1675 // byte_extract(root, offset1+offset2)
1676 if(expr.op().id()==expr.id())
1677 {
1678 auto tmp = expr;
1679
1680 tmp.offset() = simplify_rec(plus_exprt(
1682 to_byte_extract_expr(expr.op()).offset(), expr.offset().type()),
1683 expr.offset()));
1684 tmp.op() = to_byte_extract_expr(expr.op()).op();
1685
1686 return changed(simplify_byte_extract(tmp)); // recursive call
1687 }
1688
1689 // byte_extract(byte_update(root, offset, value), offset) =>
1690 // value
1691 if(
1692 ((expr.id() == ID_byte_extract_big_endian &&
1693 expr.op().id() == ID_byte_update_big_endian) ||
1694 (expr.id() == ID_byte_extract_little_endian &&
1695 expr.op().id() == ID_byte_update_little_endian)) &&
1696 expr.offset() == to_byte_update_expr(as_const(expr).op()).offset())
1697 {
1698 const auto &op_byte_update = to_byte_update_expr(expr.op());
1699
1700 if(expr.type() == op_byte_update.value().type())
1701 {
1702 return op_byte_update.value();
1703 }
1704 else if(el_size.has_value())
1705 {
1706 const auto update_bits_opt =
1707 pointer_offset_bits(op_byte_update.value().type(), ns);
1708
1709 if(update_bits_opt.has_value() && *el_size <= *update_bits_opt)
1710 {
1711 auto tmp = expr;
1712 tmp.op() = op_byte_update.value();
1713 tmp.offset() = from_integer(0, expr.offset().type());
1714
1715 return changed(simplify_byte_extract(tmp)); // recursive call
1716 }
1717 }
1718 }
1719
1720 // the following require a constant offset
1721 auto offset = numeric_cast<mp_integer>(expr.offset());
1722 if(!offset.has_value() || *offset < 0)
1723 return unchanged(expr);
1724
1725 // try to simplify byte_extract(byte_update(...))
1726 auto const bu = expr_try_dynamic_cast<byte_update_exprt>(expr.op());
1727 std::optional<mp_integer> update_offset;
1728 if(bu)
1730 if(bu && el_size.has_value() && update_offset.has_value())
1731 {
1732 // byte_extract(byte_update(root, offset_u, value), offset_e) so that the
1733 // update does not affect what is being extracted simplifies to
1734 // byte_extract(root, offset_e)
1735 //
1736 // byte_extract(byte_update(root, offset_u, value), offset_e) so that the
1737 // extracted range fully lies within the update value simplifies to
1738 // byte_extract(value, offset_e - offset_u)
1739 if(
1740 *offset * expr.get_bits_per_byte() + *el_size <=
1741 *update_offset * bu->get_bits_per_byte())
1742 {
1743 // extracting before the update
1744 auto tmp = expr;
1745 tmp.op() = bu->op();
1746 return changed(simplify_byte_extract(tmp)); // recursive call
1747 }
1748 else if(
1749 const auto update_size = pointer_offset_bits(bu->value().type(), ns))
1750 {
1751 if(
1752 *offset * expr.get_bits_per_byte() >=
1753 *update_offset * bu->get_bits_per_byte() + *update_size)
1754 {
1755 // extracting after the update
1756 auto tmp = expr;
1757 tmp.op() = bu->op();
1758 return changed(simplify_byte_extract(tmp)); // recursive call
1759 }
1760 else if(
1761 *offset >= *update_offset &&
1762 *offset * expr.get_bits_per_byte() + *el_size <=
1763 *update_offset * bu->get_bits_per_byte() + *update_size)
1764 {
1765 // extracting from the update
1766 auto tmp = expr;
1767 tmp.op() = bu->value();
1768 tmp.offset() =
1769 from_integer(*offset - *update_offset, expr.offset().type());
1770 return changed(simplify_byte_extract(tmp)); // recursive call
1771 }
1772 }
1773 }
1774
1775 // don't do any of the following if endianness doesn't match, as
1776 // bytes need to be swapped
1777 if(
1778 *offset == 0 && ((expr.id() == ID_byte_extract_little_endian &&
1779 config.ansi_c.endianness ==
1781 (expr.id() == ID_byte_extract_big_endian &&
1782 config.ansi_c.endianness ==
1784 {
1785 // byte extract of full object is object
1786 if(expr.type() == expr.op().type())
1787 {
1788 return expr.op();
1789 }
1790 else if(
1791 expr.type().id() == ID_pointer && expr.op().type().id() == ID_pointer)
1792 {
1793 return typecast_exprt(expr.op(), expr.type());
1794 }
1795 }
1796
1797 if(
1798 (expr.type().id() == ID_union &&
1799 to_union_type(expr.type()).components().empty()) ||
1800 (expr.type().id() == ID_union_tag &&
1801 ns.follow_tag(to_union_tag_type(expr.type())).components().empty()))
1802 {
1803 return empty_union_exprt{expr.type()};
1804 }
1805 else if(
1806 (expr.type().id() == ID_struct &&
1807 to_struct_type(expr.type()).components().empty()) ||
1808 (expr.type().id() == ID_struct_tag &&
1809 ns.follow_tag(to_struct_tag_type(expr.type())).components().empty()))
1810 {
1811 return struct_exprt{{}, expr.type()};
1812 }
1813
1814 // no proper simplification for expr.type()==void
1815 // or types of unknown size
1816 if(!el_size.has_value() || *el_size == 0)
1817 return unchanged(expr);
1818
1819 if(
1820 expr.op().id() == ID_array_of &&
1821 to_array_of_expr(expr.op()).op().is_constant())
1822 {
1823 const auto const_bits_opt = expr2bits(
1824 to_array_of_expr(expr.op()).op(),
1825 config.ansi_c.endianness ==
1827 ns);
1828
1829 if(!const_bits_opt.has_value())
1830 return unchanged(expr);
1831
1832 std::string const_bits=const_bits_opt.value();
1833
1834 DATA_INVARIANT(!const_bits.empty(), "bit representation must be non-empty");
1835
1836 // double the string until we have sufficiently many bits
1837 while(mp_integer(const_bits.size()) <
1838 *offset * expr.get_bits_per_byte() + *el_size)
1839 {
1841 }
1842
1843 std::string el_bits = std::string(
1844 const_bits,
1847
1848 auto tmp = bits2expr(
1849 el_bits, expr.type(), expr.id() == ID_byte_extract_little_endian, ns);
1850
1851 if(tmp.has_value())
1852 return std::move(*tmp);
1853 }
1854
1855 // in some cases we even handle non-const array_of
1856 if(
1857 expr.op().id() == ID_array_of &&
1858 (*offset * expr.get_bits_per_byte()) % (*el_size) == 0 &&
1859 *el_size <=
1860 pointer_offset_bits(to_array_of_expr(expr.op()).what().type(), ns))
1861 {
1862 auto tmp = expr;
1863 tmp.op() = simplify_index(index_exprt(expr.op(), expr.offset()));
1864 tmp.offset() = from_integer(0, expr.offset().type());
1866 }
1867
1868 // extract bits of a constant
1869 const auto bits =
1870 expr2bits(expr.op(), expr.id() == ID_byte_extract_little_endian, ns);
1871
1872 if(
1873 bits.has_value() &&
1874 mp_integer(bits->size()) >= *el_size + *offset * expr.get_bits_per_byte())
1875 {
1876 // make sure we don't lose bits with structs containing flexible array
1877 // members
1879 expr.type(),
1880 [&](const typet &type) {
1881 if(type.id() != ID_struct && type.id() != ID_struct_tag)
1882 return false;
1883
1884 const struct_typet &st = type.id() == ID_struct_tag
1885 ? ns.follow_tag(to_struct_tag_type(type))
1886 : to_struct_type(type);
1887 const auto &comps = st.components();
1888 if(comps.empty() || comps.back().type().id() != ID_array)
1889 return false;
1890
1891 if(comps.back().type().get_bool(ID_C_flexible_array_member))
1892 return true;
1893
1894 const auto size =
1895 numeric_cast<mp_integer>(to_array_type(comps.back().type()).size());
1896 return !size.has_value() || *size <= 1;
1897 },
1898 ns);
1900 {
1901 std::string bits_cut = std::string(
1902 bits.value(),
1905
1906 auto tmp = bits2expr(
1907 bits_cut, expr.type(), expr.id() == ID_byte_extract_little_endian, ns);
1908
1909 if(tmp.has_value())
1910 return std::move(*tmp);
1911 }
1912 }
1913
1914 // push byte extracts into struct or union expressions, just like
1915 // lower_byte_extract does (this is the same code, except recursive calls use
1916 // simplify rather than lower_byte_extract)
1917 if(expr.op().id() == ID_struct || expr.op().id() == ID_union)
1918 {
1919 if(expr.type().id() == ID_struct || expr.type().id() == ID_struct_tag)
1920 {
1921 const struct_typet &struct_type =
1922 expr.type().id() == ID_struct_tag
1923 ? ns.follow_tag(to_struct_tag_type(expr.type()))
1924 : to_struct_type(expr.type());
1925 const struct_typet::componentst &components = struct_type.components();
1926
1927 bool failed = false;
1928 struct_exprt s({}, expr.type());
1929
1930 for(const auto &comp : components)
1931 {
1932 auto component_bits = pointer_offset_bits(comp.type(), ns);
1933
1934 // the next member would be misaligned, abort
1935 if(
1936 !component_bits.has_value() || *component_bits == 0 ||
1937 *component_bits % expr.get_bits_per_byte() != 0)
1938 {
1939 failed = true;
1940 break;
1941 }
1942
1943 auto member_offset_opt =
1944 member_offset_expr(struct_type, comp.get_name(), ns);
1945
1946 if(!member_offset_opt.has_value())
1947 {
1948 failed = true;
1949 break;
1950 }
1951
1953 plus_exprt{expr.offset(),
1955 member_offset_opt.value(), expr.offset().type())});
1956
1957 byte_extract_exprt tmp = expr;
1958 tmp.type() = comp.type();
1959 tmp.offset() = new_offset;
1960
1962 }
1963
1964 if(!failed)
1965 return s;
1966 }
1967 else if(expr.type().id() == ID_union || expr.type().id() == ID_union_tag)
1968 {
1969 const union_typet &union_type =
1970 expr.type().id() == ID_union_tag
1971 ? ns.follow_tag(to_union_tag_type(expr.type()))
1972 : to_union_type(expr.type());
1973 auto widest_member_opt = union_type.find_widest_union_component(ns);
1974 if(widest_member_opt.has_value())
1975 {
1976 byte_extract_exprt be = expr;
1977 be.type() = widest_member_opt->first.type();
1978 return union_exprt{widest_member_opt->first.get_name(),
1980 expr.type()};
1981 }
1982 }
1983 }
1984 else if(expr.op().id() == ID_array)
1985 {
1986 const array_typet &array_type = to_array_type(expr.op().type());
1987 const auto &element_bit_width =
1988 pointer_offset_bits(array_type.element_type(), ns);
1989 if(element_bit_width.has_value() && *element_bit_width > 0)
1990 {
1991 if(
1992 *offset > 0 &&
1993 *offset * expr.get_bits_per_byte() % *element_bit_width == 0)
1994 {
1996 (*offset * expr.get_bits_per_byte()) / *element_bit_width);
1998 slice.operands().erase(
1999 slice.operands().begin(),
2000 slice.operands().begin() +
2001 std::min(elements_to_erase, slice.operands().size()));
2002 slice.type().size() =
2003 from_integer(slice.operands().size(), slice.type().size().type());
2004 byte_extract_exprt be = expr;
2005 be.op() = slice;
2006 be.offset() = from_integer(0, expr.offset().type());
2008 }
2009 else if(*offset == 0 && *el_size % *element_bit_width == 0)
2010 {
2011 const auto elements_to_keep =
2014 if(slice.operands().size() > elements_to_keep)
2015 {
2016 slice.operands().resize(elements_to_keep);
2017 slice.type().size() =
2018 from_integer(slice.operands().size(), slice.type().size().type());
2019 byte_extract_exprt be = expr;
2020 be.op() = slice;
2022 }
2023 }
2024 }
2025 }
2026
2027 // try to refine it down to extracting from a member or an index in an array
2028 auto subexpr =
2029 get_subexpression_at_offset(expr.op(), *offset, expr.type(), ns);
2030 if(subexpr.has_value() && subexpr.value() != expr)
2031 return changed(simplify_rec(subexpr.value())); // recursive call
2032
2033 if(can_forward_propagatet(ns)(expr))
2034 return changed(simplify_rec(lower_byte_extract(expr, ns)));
2035
2036 return unchanged(expr);
2037}
2038
2041{
2042 // lift up any ID_if on the object
2043 if(expr.op().id() == ID_if)
2044 {
2045 if_exprt if_expr = lift_if(expr, 0);
2047 }
2048 else
2049 {
2050 std::optional<exprt::operandst> new_operands;
2051
2052 for(std::size_t i = 0; i < expr.operands().size(); ++i)
2053 {
2054 auto r_it = simplify_rec(expr.operands()[i]); // recursive call
2055 if(r_it.has_changed())
2056 {
2057 if(!new_operands.has_value())
2058 new_operands = expr.operands();
2059 (*new_operands)[i] = std::move(r_it.expr);
2060 }
2061 }
2062
2063 if(new_operands.has_value())
2064 {
2065 exprt result = expr;
2066 std::swap(result.operands(), *new_operands);
2067 return result;
2068 }
2069 }
2070
2071 return unchanged(expr);
2072}
2073
2076{
2077 // byte_update(byte_update(root, offset, value), offset, value2) =>
2078 // byte_update(root, offset, value2)
2079 if(
2080 expr.id() == expr.op().id() &&
2081 expr.offset() == to_byte_update_expr(expr.op()).offset() &&
2082 expr.value().type() == to_byte_update_expr(expr.op()).value().type())
2083 {
2084 auto tmp = expr;
2085 tmp.set_op(to_byte_update_expr(expr.op()).op());
2086 return std::move(tmp);
2087 }
2088
2089 const exprt &root = expr.op();
2090 const exprt &offset = expr.offset();
2091 const exprt &value = expr.value();
2092 const auto val_size = pointer_offset_bits(value.type(), ns);
2093 const auto root_size = pointer_offset_bits(root.type(), ns);
2094
2095 const auto matching_byte_extract_id =
2098
2099 // byte update of full object is byte_extract(new value)
2100 if(
2101 offset.is_zero() && val_size.has_value() && *val_size > 0 &&
2102 root_size.has_value() && *root_size > 0 && *val_size >= *root_size)
2103 {
2106 value,
2107 offset,
2108 expr.get_bits_per_byte(),
2109 expr.type());
2110
2112 }
2113
2114 // update bits in a constant
2115 const auto offset_int = numeric_cast<mp_integer>(offset);
2116 if(
2117 root_size.has_value() && *root_size >= 0 && val_size.has_value() &&
2118 *val_size >= 0 && offset_int.has_value() && *offset_int >= 0 &&
2120 {
2121 auto root_bits =
2122 expr2bits(root, expr.id() == ID_byte_update_little_endian, ns);
2123
2124 if(root_bits.has_value())
2125 {
2126 const auto val_bits =
2127 expr2bits(value, expr.id() == ID_byte_update_little_endian, ns);
2128
2129 if(val_bits.has_value())
2130 {
2131 root_bits->replace(
2134 *val_bits);
2135
2136 auto tmp = bits2expr(
2137 *root_bits,
2138 expr.type(),
2140 ns);
2141
2142 if(tmp.has_value())
2143 return std::move(*tmp);
2144 }
2145 }
2146 }
2147
2148 /*
2149 * byte_update(root, offset,
2150 * extract(root, offset) WITH component:=value)
2151 * =>
2152 * byte_update(root, offset + component offset,
2153 * value)
2154 */
2155
2156 if(value.id()==ID_with)
2157 {
2158 const with_exprt &with=to_with_expr(value);
2159
2160 if(with.old().id() == matching_byte_extract_id)
2161 {
2162 const byte_extract_exprt &extract=to_byte_extract_expr(with.old());
2163
2164 /* the simplification can be used only if
2165 root and offset of update and extract
2166 are the same */
2167 if(!(root==extract.op()))
2168 return unchanged(expr);
2169 if(!(offset==extract.offset()))
2170 return unchanged(expr);
2171
2172 if(with.type().id() == ID_struct || with.type().id() == ID_struct_tag)
2173 {
2174 const struct_typet &struct_type =
2175 with.type().id() == ID_struct_tag
2176 ? ns.follow_tag(to_struct_tag_type(with.type()))
2177 : to_struct_type(with.type());
2178 const irep_idt &component_name=with.where().get(ID_component_name);
2179 const typet &c_type = struct_type.get_component(component_name).type();
2180
2181 // is this a bit field?
2182 if(c_type.id() == ID_c_bit_field || c_type.id() == ID_bool)
2183 {
2184 // don't touch -- might not be byte-aligned
2185 }
2186 else
2187 {
2188 // new offset = offset + component offset
2189 auto i = member_offset(struct_type, component_name, ns);
2190 if(i.has_value())
2191 {
2192 exprt compo_offset = from_integer(*i, offset.type());
2194 exprt new_value(with.new_value());
2195 auto tmp = expr;
2196 tmp.set_offset(simplify_node(std::move(new_offset)));
2197 tmp.set_value(std::move(new_value));
2198 return changed(simplify_byte_update(tmp)); // recursive call
2199 }
2200 }
2201 }
2202 else if(with.type().id() == ID_array)
2203 {
2204 auto i =
2205 pointer_offset_size(to_array_type(with.type()).element_type(), ns);
2206 if(i.has_value())
2207 {
2208 const exprt &index=with.where();
2210 simplify_mult(mult_exprt(index, from_integer(*i, index.type())));
2211
2212 // index_offset may need a typecast
2213 if(offset.type() != index.type())
2214 {
2215 index_offset =
2217 }
2218
2220 exprt new_value(with.new_value());
2221 auto tmp = expr;
2222 tmp.set_offset(simplify_plus(std::move(new_offset)));
2223 tmp.set_value(std::move(new_value));
2224 return changed(simplify_byte_update(tmp)); // recursive call
2225 }
2226 }
2227 }
2228 }
2229
2230 // the following require a constant offset
2231 if(!offset_int.has_value() || *offset_int < 0)
2232 return unchanged(expr);
2233
2234 // size must be known
2235 if(!val_size.has_value() || *val_size == 0)
2236 return unchanged(expr);
2237
2238 // Are we updating (parts of) a struct? Do individual member updates
2239 // instead, unless there are non-byte-sized bit fields
2240 if(root.type().id() == ID_struct || root.type().id() == ID_struct_tag)
2241 {
2243 result_expr.make_nil();
2244
2245 auto update_size = pointer_offset_size(value.type(), ns);
2246
2247 const struct_typet &struct_type =
2248 root.type().id() == ID_struct_tag
2249 ? ns.follow_tag(to_struct_tag_type(root.type()))
2250 : to_struct_type(root.type());
2251 const struct_typet::componentst &components=
2252 struct_type.components();
2253
2254 for(const auto &component : components)
2255 {
2256 auto m_offset = member_offset(struct_type, component.get_name(), ns);
2257
2259
2260 // can we determine the current offset?
2261 if(!m_offset.has_value())
2262 {
2263 result_expr.make_nil();
2264 break;
2265 }
2266
2267 // is it a byte-sized member?
2268 if(
2269 !m_size_bits.has_value() || *m_size_bits == 0 ||
2270 (*m_size_bits) % expr.get_bits_per_byte() != 0)
2271 {
2272 result_expr.make_nil();
2273 break;
2274 }
2275
2276 mp_integer m_size_bytes = (*m_size_bits) / expr.get_bits_per_byte();
2277
2278 // is that member part of the update?
2280 continue;
2281 // are we done updating?
2282 else if(
2283 update_size.has_value() && *update_size > 0 &&
2285 {
2286 break;
2287 }
2288
2289 if(result_expr.is_nil())
2290 result_expr = as_const(expr).op();
2291
2293 member_name.set(ID_component_name, component.get_name());
2295
2296 // are we updating on member boundaries?
2297 if(
2298 *m_offset < *offset_int ||
2299 (*m_offset == *offset_int && update_size.has_value() &&
2301 {
2303 expr.id(),
2304 member_exprt(root, component.get_name(), component.type()),
2305 from_integer(*offset_int - *m_offset, offset.type()),
2306 value,
2307 expr.get_bits_per_byte());
2308
2309 to_with_expr(result_expr).new_value().swap(v);
2310 }
2311 else if(
2312 update_size.has_value() && *update_size > 0 &&
2314 {
2315 // we don't handle this for the moment
2316 result_expr.make_nil();
2317 break;
2318 }
2319 else
2320 {
2323 value,
2324 from_integer(*m_offset - *offset_int, offset.type()),
2325 expr.get_bits_per_byte(),
2326 component.type());
2327
2328 to_with_expr(result_expr).new_value().swap(v);
2329 }
2330 }
2331
2332 if(result_expr.is_not_nil())
2334 }
2335
2336 // replace elements of array or struct expressions, possibly using
2337 // byte_extract
2338 if(root.id()==ID_array)
2339 {
2340 auto el_size =
2341 pointer_offset_bits(to_type_with_subtype(root.type()).subtype(), ns);
2342
2343 if(
2344 !el_size.has_value() || *el_size == 0 ||
2345 (*el_size) % expr.get_bits_per_byte() != 0 ||
2346 (*val_size) % expr.get_bits_per_byte() != 0)
2347 {
2348 return unchanged(expr);
2349 }
2350
2351 exprt result=root;
2352
2354 Forall_operands(it, result)
2355 {
2357 break;
2358
2360 {
2366
2369 value,
2370 from_integer(val_offset, offset.type()),
2371 expr.get_bits_per_byte(),
2374 from_integer(bytes_req, offset.type())));
2375
2376 *it = byte_update_exprt(
2377 expr.id(),
2378 *it,
2381 offset.type()),
2382 new_val,
2383 expr.get_bits_per_byte());
2384
2385 *it = simplify_rec(*it); // recursive call
2386
2388 }
2389
2391 }
2392
2393 return std::move(result);
2394 }
2395
2396 return unchanged(expr);
2397}
2398
2401{
2402 if(expr.id() == ID_complex_real)
2403 {
2405
2406 if(complex_real_expr.op().id() == ID_complex)
2407 return to_complex_expr(complex_real_expr.op()).real();
2408 }
2409 else if(expr.id() == ID_complex_imag)
2410 {
2412
2413 if(complex_imag_expr.op().id() == ID_complex)
2414 return to_complex_expr(complex_imag_expr.op()).imag();
2415 }
2416
2417 return unchanged(expr);
2418}
2419
2422{
2423 // When one operand is zero, an overflow can only occur for a subtraction from
2424 // zero.
2425 if(
2426 expr.op1().is_zero() ||
2427 (expr.op0().is_zero() && !can_cast_expr<minus_overflow_exprt>(expr)))
2428 {
2429 return false_exprt{};
2430 }
2431
2432 // One is neutral element for multiplication
2433 if(
2435 (expr.op0().is_one() || expr.op1().is_one()))
2436 {
2437 return false_exprt{};
2438 }
2439
2440 // we only handle the case of same operand types
2441 if(expr.op0().type() != expr.op1().type())
2442 return unchanged(expr);
2443
2444 // catch some cases over mathematical types
2445 const irep_idt &op_type_id = expr.op0().type().id();
2446 if(
2449 {
2450 return false_exprt{};
2451 }
2452
2454 return false_exprt{};
2455
2456 // we only handle constants over signedbv/unsignedbv for the remaining cases
2458 return unchanged(expr);
2459
2460 if(!expr.op0().is_constant() || !expr.op1().is_constant())
2461 return unchanged(expr);
2462
2463 const auto op0_value = numeric_cast<mp_integer>(expr.op0());
2464 const auto op1_value = numeric_cast<mp_integer>(expr.op1());
2465 if(!op0_value.has_value() || !op1_value.has_value())
2466 return unchanged(expr);
2467
2470 no_overflow_result = *op0_value + *op1_value;
2472 no_overflow_result = *op0_value - *op1_value;
2474 no_overflow_result = *op0_value * *op1_value;
2476 no_overflow_result = *op0_value << *op1_value;
2477 else
2479
2480 const std::size_t width = to_bitvector_type(expr.op0().type()).get_width();
2482 if(
2483 no_overflow_result < bv_type.smallest() ||
2484 no_overflow_result > bv_type.largest())
2485 {
2486 return true_exprt{};
2487 }
2488 else
2489 return false_exprt{};
2490}
2491
2494{
2495 // zero is a neutral element for all operations supported here
2496 if(expr.op().is_zero())
2497 return false_exprt{};
2498
2499 // catch some cases over mathematical types
2500 const irep_idt &op_type_id = expr.op().type().id();
2501 if(
2504 {
2505 return false_exprt{};
2506 }
2507
2508 if(op_type_id == ID_natural)
2509 return true_exprt{};
2510
2511 // we only handle constants over signedbv/unsignedbv for the remaining cases
2513 return unchanged(expr);
2514
2515 if(!expr.op().is_constant())
2516 return unchanged(expr);
2517
2518 const auto op_value = numeric_cast<mp_integer>(expr.op());
2519 if(!op_value.has_value())
2520 return unchanged(expr);
2521
2525 else
2527
2528 const std::size_t width = to_bitvector_type(expr.op().type()).get_width();
2530 if(
2531 no_overflow_result < bv_type.smallest() ||
2532 no_overflow_result > bv_type.largest())
2533 {
2534 return true_exprt{};
2535 }
2536 else
2537 return false_exprt{};
2538}
2539
2542{
2544 {
2545 // zero is a neutral element
2546 if(expr.op0().is_zero())
2547 return struct_exprt{{expr.op0(), false_exprt{}}, expr.type()};
2548
2549 // catch some cases over mathematical types
2550 const irep_idt &op_type_id = expr.op0().type().id();
2551 if(
2554 {
2555 return struct_exprt{{expr.op0(), false_exprt{}}, expr.type()};
2556 }
2557
2558 // always an overflow for natural numbers, but the result is not
2559 // representable
2560 if(op_type_id == ID_natural)
2561 return unchanged(expr);
2562
2563 // we only handle constants over signedbv/unsignedbv for the remaining cases
2565 return unchanged(expr);
2566
2567 if(!expr.op0().is_constant())
2568 return unchanged(expr);
2569
2570 const auto op_value = numeric_cast<mp_integer>(expr.op0());
2571 if(!op_value.has_value())
2572 return unchanged(expr);
2573
2575
2576 const std::size_t width = to_bitvector_type(expr.op0().type()).get_width();
2578 if(
2579 no_overflow_result < bv_type.smallest() ||
2580 no_overflow_result > bv_type.largest())
2581 {
2582 return struct_exprt{
2584 expr.type()};
2585 }
2586 else
2587 {
2588 return struct_exprt{
2590 expr.type()};
2591 }
2592 }
2593 else
2594 {
2595 // When one operand is zero, an overflow can only occur for a subtraction
2596 // from zero.
2597 if(expr.op0().is_zero())
2598 {
2599 if(
2600 expr.id() == ID_overflow_result_plus ||
2601 expr.id() == ID_overflow_result_shl)
2602 {
2603 return struct_exprt{{expr.op1(), false_exprt{}}, expr.type()};
2604 }
2605 else if(expr.id() == ID_overflow_result_mult)
2606 {
2607 return struct_exprt{
2608 {from_integer(0, expr.op0().type()), false_exprt{}}, expr.type()};
2609 }
2610 }
2611 else if(expr.op1().is_zero())
2612 {
2613 if(
2614 expr.id() == ID_overflow_result_plus ||
2615 expr.id() == ID_overflow_result_minus ||
2616 expr.id() == ID_overflow_result_shl)
2617 {
2618 return struct_exprt{{expr.op0(), false_exprt{}}, expr.type()};
2619 }
2620 else
2621 {
2622 return struct_exprt{
2623 {from_integer(0, expr.op0().type()), false_exprt{}}, expr.type()};
2624 }
2625 }
2626
2627 // One is neutral element for multiplication
2628 if(
2629 expr.id() == ID_overflow_result_mult &&
2630 (expr.op0().is_one() || expr.op1().is_one()))
2631 {
2632 return struct_exprt{
2633 {expr.op0().is_one() ? expr.op1() : expr.op0(), false_exprt{}},
2634 expr.type()};
2635 }
2636
2637 // we only handle the case of same operand types
2638 if(
2639 expr.id() != ID_overflow_result_shl &&
2640 expr.op0().type() != expr.op1().type())
2641 {
2642 return unchanged(expr);
2643 }
2644
2645 // catch some cases over mathematical types
2646 const irep_idt &op_type_id = expr.op0().type().id();
2647 if(
2648 expr.id() != ID_overflow_result_shl &&
2650 op_type_id == ID_real))
2651 {
2652 irep_idt id =
2653 expr.id() == ID_overflow_result_plus
2654 ? ID_plus
2656 return struct_exprt{
2657 {simplify_node(binary_exprt{expr.op0(), id, expr.op1()}),
2658 false_exprt{}},
2659 expr.type()};
2660 }
2661
2662 if(
2663 (expr.id() == ID_overflow_result_plus ||
2664 expr.id() == ID_overflow_result_mult) &&
2666 {
2667 return struct_exprt{
2669 expr.op0(),
2671 expr.op1()}),
2672 false_exprt{}},
2673 expr.type()};
2674 }
2675
2676 // we only handle constants over signedbv/unsignedbv for the remaining cases
2678 return unchanged(expr);
2679
2680 // a special case of overflow-minus checking with operands (X + n) and X
2681 if(expr.id() == ID_overflow_result_minus)
2682 {
2683 const exprt &tc_op0 = skip_typecast(expr.op0());
2684 const exprt &tc_op1 = skip_typecast(expr.op1());
2685
2687 {
2688 if(skip_typecast(sum->op0()) == tc_op1 && sum->operands().size() == 2)
2689 {
2690 std::optional<exprt> offset;
2691 if(sum->type().id() == ID_pointer)
2692 {
2693 offset = std::move(simplify_pointer_offset(
2694 pointer_offset_exprt{*sum, expr.op0().type()})
2695 .expr);
2696 if(offset->id() == ID_pointer_offset)
2697 return unchanged(expr);
2698 }
2699 else
2700 offset = std::move(
2701 simplify_typecast(typecast_exprt{sum->op1(), expr.op0().type()})
2702 .expr);
2703
2704 exprt offset_op = skip_typecast(*offset);
2705 if(
2706 offset_op.type().id() != ID_signedbv &&
2707 offset_op.type().id() != ID_unsignedbv)
2708 {
2709 return unchanged(expr);
2710 }
2711
2712 const std::size_t width =
2713 to_bitvector_type(expr.op0().type()).get_width();
2715
2718 offset_op,
2719 ID_lt,
2720 from_integer(bv_type.smallest(), offset_op.type())},
2722 offset_op,
2723 ID_gt,
2724 from_integer(bv_type.largest(), offset_op.type())}};
2725
2726 return struct_exprt{
2727 {*offset, simplify_rec(not_representable)}, expr.type()};
2728 }
2729 }
2730 }
2731
2732 if(!expr.op0().is_constant() || !expr.op1().is_constant())
2733 return unchanged(expr);
2734
2735 // preserve the sizeof type annotation
2736 std::optional<typet> c_sizeof_type;
2737 for(const auto &op : expr.operands())
2738 {
2739 const typet &sizeof_type =
2740 static_cast<const typet &>(op.find(ID_C_c_sizeof_type));
2741 if(sizeof_type.is_not_nil())
2742 {
2744 break;
2745 }
2746 }
2747
2748 const auto op0_value = numeric_cast<mp_integer>(expr.op0());
2749 const auto op1_value = numeric_cast<mp_integer>(expr.op1());
2750 if(!op0_value.has_value() || !op1_value.has_value())
2751 return unchanged(expr);
2752
2754 if(expr.id() == ID_overflow_result_plus)
2755 no_overflow_result = *op0_value + *op1_value;
2756 else if(expr.id() == ID_overflow_result_minus)
2757 no_overflow_result = *op0_value - *op1_value;
2758 else if(expr.id() == ID_overflow_result_mult)
2759 no_overflow_result = *op0_value * *op1_value;
2760 else if(expr.id() == ID_overflow_result_shl)
2761 no_overflow_result = *op0_value << *op1_value;
2762 else
2764
2767 if(c_sizeof_type.has_value())
2769
2770 const std::size_t width = to_bitvector_type(expr.op0().type()).get_width();
2772 if(
2773 no_overflow_result < bv_type.smallest() ||
2774 no_overflow_result > bv_type.largest())
2775 {
2776 return struct_exprt{
2777 {std::move(no_overflow_result_expr), true_exprt{}}, expr.type()};
2778 }
2779 else
2780 {
2781 return struct_exprt{
2782 {std::move(no_overflow_result_expr), false_exprt{}}, expr.type()};
2783 }
2784 }
2785}
2786
2789{
2790 auto result = unchanged(expr);
2791
2792 // The ifs below could one day be replaced by a switch()
2793
2794 if(expr.id()==ID_address_of)
2795 {
2796 // the argument of this expression needs special treatment
2797 }
2798 else if(expr.id()==ID_if)
2799 {
2800 result = simplify_if_preorder(to_if_expr(expr));
2801 }
2802 else if(expr.id() == ID_typecast)
2803 {
2805 }
2806 else if(
2809 {
2811 }
2812 else if(expr.id() == ID_dereference)
2813 {
2815 }
2816 else if(expr.id() == ID_index)
2817 {
2818 result = simplify_index_preorder(to_index_expr(expr));
2819 }
2820 else if(expr.id() == ID_member)
2821 {
2823 }
2824 else if(
2825 expr.id() == ID_is_dynamic_object || expr.id() == ID_is_invalid_pointer ||
2826 expr.id() == ID_object_size || expr.id() == ID_pointer_object ||
2827 expr.id() == ID_pointer_offset)
2828 {
2830 }
2831 else if(expr.has_operands())
2832 {
2833 std::optional<exprt::operandst> new_operands;
2834
2835 for(std::size_t i = 0; i < expr.operands().size(); ++i)
2836 {
2837 auto r_it = simplify_rec(expr.operands()[i]); // recursive call
2838 if(r_it.has_changed())
2839 {
2840 if(!new_operands.has_value())
2841 new_operands = expr.operands();
2842 (*new_operands)[i] = std::move(r_it.expr);
2843 }
2844 }
2845
2846 if(new_operands.has_value())
2847 {
2848 std::swap(result.expr.operands(), *new_operands);
2849 result.expr_changed = resultt<>::CHANGED;
2850 }
2851 }
2852
2853 if(as_const(result.expr).type().id() == ID_array)
2854 {
2855 const array_typet &array_type = to_array_type(as_const(result.expr).type());
2857 if(simp_size.has_changed())
2858 {
2859 to_array_type(result.expr.type()).size() = simp_size.expr;
2860 result.expr_changed = resultt<>::CHANGED;
2861 }
2862 }
2863
2864 return result;
2865}
2866
2868{
2869 if(!node.has_operands())
2870 return unchanged(node); // no change
2871
2872 // #define DEBUGX
2873
2874#ifdef DEBUGX
2875 exprt old(node);
2876#endif
2877
2878 exprt expr = node;
2880
2881 resultt<> r = unchanged(expr);
2882
2883 if(expr.id()==ID_typecast)
2884 {
2886 }
2887 else if(expr.id()==ID_equal || expr.id()==ID_notequal ||
2888 expr.id()==ID_gt || expr.id()==ID_lt ||
2889 expr.id()==ID_ge || expr.id()==ID_le)
2890 {
2892 }
2893 else if(expr.id()==ID_if)
2894 {
2895 r = simplify_if(to_if_expr(expr));
2896 }
2897 else if(expr.id()==ID_lambda)
2898 {
2900 }
2901 else if(expr.id()==ID_with)
2902 {
2903 r = simplify_with(to_with_expr(expr));
2904 }
2905 else if(expr.id()==ID_update)
2906 {
2908 }
2909 else if(expr.id()==ID_index)
2910 {
2912 }
2913 else if(expr.id()==ID_member)
2914 {
2916 }
2917 else if(expr.id()==ID_byte_update_little_endian ||
2919 {
2921 }
2922 else if(expr.id()==ID_byte_extract_little_endian ||
2924 {
2926 }
2927 else if(expr.id()==ID_pointer_object)
2928 {
2930 }
2931 else if(expr.id() == ID_is_dynamic_object)
2932 {
2934 }
2935 else if(expr.id() == ID_is_invalid_pointer)
2936 {
2938 }
2939 else if(
2941 {
2943 }
2944 else if(expr.id()==ID_div)
2945 {
2946 r = simplify_div(to_div_expr(expr));
2947 }
2948 else if(expr.id()==ID_mod)
2949 {
2950 r = simplify_mod(to_mod_expr(expr));
2951 }
2952 else if(expr.id()==ID_bitnot)
2953 {
2955 }
2956 else if(
2957 expr.id() == ID_bitand || expr.id() == ID_bitor || expr.id() == ID_bitxor ||
2958 expr.id() == ID_bitxnor)
2959 {
2961 }
2962 else if(expr.id()==ID_ashr || expr.id()==ID_lshr || expr.id()==ID_shl)
2963 {
2965 }
2966 else if(expr.id()==ID_power)
2967 {
2969 }
2970 else if(expr.id()==ID_plus)
2971 {
2972 r = simplify_plus(to_plus_expr(expr));
2973 }
2974 else if(expr.id()==ID_minus)
2975 {
2977 }
2978 else if(expr.id()==ID_mult)
2979 {
2980 r = simplify_mult(to_mult_expr(expr));
2981 }
2982 else if(expr.id()==ID_floatbv_plus ||
2983 expr.id()==ID_floatbv_minus ||
2984 expr.id()==ID_floatbv_mult ||
2985 expr.id()==ID_floatbv_div)
2986 {
2988 }
2989 else if(expr.id()==ID_floatbv_typecast)
2990 {
2992 }
2993 else if(expr.id()==ID_unary_minus)
2994 {
2996 }
2997 else if(expr.id()==ID_unary_plus)
2998 {
3000 }
3001 else if(expr.id()==ID_not)
3002 {
3003 r = simplify_not(to_not_expr(expr));
3004 }
3005 else if(expr.id()==ID_implies ||
3006 expr.id()==ID_or || expr.id()==ID_xor ||
3007 expr.id()==ID_and)
3008 {
3009 r = simplify_boolean(expr);
3010 }
3011 else if(expr.id()==ID_dereference)
3012 {
3014 }
3015 else if(expr.id()==ID_address_of)
3016 {
3018 }
3019 else if(expr.id()==ID_pointer_offset)
3020 {
3022 }
3023 else if(expr.id()==ID_extractbit)
3024 {
3026 }
3027 else if(expr.id()==ID_concatenation)
3028 {
3030 }
3031 else if(expr.id()==ID_extractbits)
3032 {
3034 }
3035 else if(expr.id() == ID_zero_extend)
3036 {
3038 }
3039 else if(expr.id()==ID_ieee_float_equal ||
3040 expr.id()==ID_ieee_float_notequal)
3041 {
3043 }
3044 else if(expr.id() == ID_bswap)
3045 {
3047 }
3048 else if(expr.id()==ID_isinf)
3049 {
3051 }
3052 else if(expr.id()==ID_isnan)
3053 {
3055 }
3056 else if(expr.id()==ID_isnormal)
3057 {
3059 }
3060 else if(expr.id()==ID_abs)
3061 {
3062 r = simplify_abs(to_abs_expr(expr));
3063 }
3064 else if(expr.id()==ID_sign)
3065 {
3066 r = simplify_sign(to_sign_expr(expr));
3067 }
3068 else if(expr.id() == ID_popcount)
3069 {
3071 }
3072 else if(expr.id() == ID_count_leading_zeros)
3073 {
3075 }
3076 else if(expr.id() == ID_count_trailing_zeros)
3077 {
3079 }
3080 else if(expr.id() == ID_find_first_set)
3081 {
3083 }
3084 else if(expr.id() == ID_function_application)
3085 {
3087 }
3088 else if(expr.id() == ID_complex_real || expr.id() == ID_complex_imag)
3089 {
3091 }
3092 else if(
3093 const auto binary_overflow =
3095 {
3097 }
3098 else if(
3099 const auto unary_overflow =
3101 {
3103 }
3104 else if(
3105 const auto overflow_result =
3107 {
3109 }
3110 else if(expr.id() == ID_bitreverse)
3111 {
3113 }
3114 else if(
3115 const auto prophecy_r_or_w_ok =
3117 {
3119 }
3120 else if(
3121 const auto prophecy_pointer_in_range =
3123 {
3125 }
3126
3128 r = changed(r);
3129
3130#ifdef DEBUGX
3131 if(
3132 r.has_changed()
3134 && debug_on
3135# endif
3136 )
3137 {
3138 std::cout << "===== " << node.id() << ": " << format(node) << '\n'
3139 << " ---> " << format(r.expr) << '\n';
3140 }
3141#endif
3142
3143 return r;
3144}
3145
3147{
3148 // look up in cache
3149
3150 #ifdef USE_CACHE
3151 std::pair<simplify_expr_cachet::containert::iterator, bool>
3153 insert(std::pair<exprt, exprt>(expr, exprt()));
3154
3155 if(!cache_result.second) // found!
3156 {
3157 const exprt &new_expr=cache_result.first->second;
3158
3159 if(new_expr.id().empty())
3160 return true; // no change
3161
3162 expr=new_expr;
3163 return false;
3164 }
3165 #endif
3166
3167 // We work on a copy to prevent unnecessary destruction of sharing.
3169
3171
3172 if(
3173 !simplify_node_result.has_changed() &&
3174 simplify_node_preorder_result.has_changed())
3175 {
3176 simplify_node_result.expr_changed =
3177 simplify_node_preorder_result.expr_changed;
3178 }
3179
3180#ifdef USE_LOCAL_REPLACE_MAP
3182# if 1
3183 replace_mapt::const_iterator it =
3185 if(it!=local_replace_map.end())
3186 simplify_node_result = changed(it->second);
3187# else
3188 if(
3189 !local_replace_map.empty() &&
3191 {
3193 }
3194# endif
3195#endif
3196
3197 if(!simplify_node_result.has_changed())
3198 {
3199 return unchanged(expr);
3200 }
3201 else
3202 {
3204 (as_const(simplify_node_result.expr).type().id() == ID_array &&
3205 expr.type().id() == ID_array) ||
3206 as_const(simplify_node_result.expr).type() == expr.type(),
3207 simplify_node_result.expr.pretty(),
3208 expr.pretty());
3209
3210#ifdef USE_CACHE
3211 // save in cache
3212 cache_result.first->second = simplify_node_result.expr;
3213#endif
3214
3215 return simplify_node_result;
3216 }
3217}
3218
3221{
3222#ifdef DEBUG_ON_DEMAND
3223 if(debug_on)
3224 std::cout << "TO-SIMP " << format(expr) << "\n";
3225#endif
3226 auto result = simplify_rec(expr);
3227#ifdef DEBUG_ON_DEMAND
3228 if(debug_on)
3229 std::cout << "FULLSIMP " << format(result.expr) << "\n";
3230#endif
3231 if(result.has_changed())
3232 {
3233 expr = result.expr;
3234 return false; // change
3235 }
3236 else
3237 return true; // no change
3238}
3239
3241bool simplify(exprt &expr, const namespacet &ns)
3242{
3243 return simplify_exprt(ns).simplify(expr);
3244}
3245
3247{
3248 simplify_exprt(ns).simplify(src);
3249 return src;
3250}
configt config
Definition config.cpp:25
mp_integer bvrep2integer(const irep_idt &src, std::size_t width, bool is_signed)
convert a bit-vector representation (possibly signed) to integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
bool to_integer(const constant_exprt &expr, mp_integer &int_value)
Convert a constant expression expr to an arbitrary-precision integer.
bool get_bvrep_bit(const irep_idt &src, std::size_t width, std::size_t bit_index)
Get a bit with given index from bit-vector representation.
mp_integer power(const mp_integer &base, const mp_integer &exponent)
A multi-precision implementation of the power operator.
const T & as_const(T &value)
Return a reference to the same object but ensures the type is const.
Definition as_const.h:14
API to expression classes for bitvectors.
const shift_exprt & to_shift_expr(const exprt &expr)
Cast an exprt to a shift_exprt.
const popcount_exprt & to_popcount_expr(const exprt &expr)
Cast an exprt to a popcount_exprt.
const extractbits_exprt & to_extractbits_expr(const exprt &expr)
Cast an exprt to an extractbits_exprt.
const find_first_set_exprt & to_find_first_set_expr(const exprt &expr)
Cast an exprt to a find_first_set_exprt.
const bitnot_exprt & to_bitnot_expr(const exprt &expr)
Cast an exprt to a bitnot_exprt.
const bswap_exprt & to_bswap_expr(const exprt &expr)
Cast an exprt to a bswap_exprt.
const count_leading_zeros_exprt & to_count_leading_zeros_expr(const exprt &expr)
Cast an exprt to a count_leading_zeros_exprt.
const bitreverse_exprt & to_bitreverse_expr(const exprt &expr)
Cast an exprt to a bitreverse_exprt.
const extractbit_exprt & to_extractbit_expr(const exprt &expr)
Cast an exprt to an extractbit_exprt.
const concatenation_exprt & to_concatenation_expr(const exprt &expr)
Cast an exprt to a concatenation_exprt.
const zero_extend_exprt & to_zero_extend_expr(const exprt &expr)
Cast an exprt to a zero_extend_exprt.
const count_trailing_zeros_exprt & to_count_trailing_zeros_expr(const exprt &expr)
Cast an exprt to a count_trailing_zeros_exprt.
const bv_typet & to_bv_type(const typet &type)
Cast a typet to a bv_typet.
const fixedbv_typet & to_fixedbv_type(const typet &type)
Cast a typet to a fixedbv_typet.
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
void slice(symex_bmct &symex, symex_target_equationt &symex_target_equation, const namespacet &ns, const optionst &options, ui_message_handlert &ui_message_handler)
Definition bmc_util.cpp:198
Expression classes for byte-level operators.
const byte_update_exprt & to_byte_update_expr(const exprt &expr)
exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
Rewrite a byte extract expression to more fundamental operations.
const byte_extract_exprt & to_byte_extract_expr(const exprt &expr)
pointer_typet pointer_type(const typet &subtype)
Definition c_types.cpp:235
const c_enum_typet & to_c_enum_type(const typet &type)
Cast a typet to a c_enum_typet.
Definition c_types.h:335
const c_enum_tag_typet & to_c_enum_tag_type(const typet &type)
Cast a typet to a c_enum_tag_typet.
Definition c_types.h:377
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
Definition c_types.h:184
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Definition c_types.h:224
Absolute value.
Definition std_expr.h:442
Operator to return the address of an object.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
ait()
Definition ai.h:565
Array constructor from list of elements.
Definition std_expr.h:1621
Arrays with given size.
Definition std_types.h:807
A base class for binary expressions.
Definition std_expr.h:638
exprt & op0()
Definition expr.h:133
exprt & op1()
Definition expr.h:136
A Boolean expression returning true, iff operation kind would result in an overflow when applied to o...
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition std_expr.h:762
Expression of type type extracted from some object op starting at position offset (given in number of...
std::size_t get_bits_per_byte() const
Expression corresponding to op() where the bytes starting at position offset (given in number of byte...
const exprt & offset() const
const exprt & op() const
std::size_t get_bits_per_byte() const
const exprt & value() const
The C/C++ Booleans.
Definition c_types.h:97
C enum tag type, i.e., c_enum_typet with an identifier.
Definition c_types.h:352
Determine whether an expression is constant.
Definition expr_util.h:91
struct configt::ansi_ct ansi_c
A constant literal expression.
Definition std_expr.h:3117
The count leading zeros (counting the number of zero bits starting from the most-significant bit) exp...
The count trailing zeros (counting the number of zero bits starting from the least-significant bit) e...
Operator to dereference a pointer.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Union constructor to support unions without any member (a GCC/Clang feature).
Definition std_expr.h:1834
Base class for all expressions.
Definition expr.h:56
std::vector< exprt > operandst
Definition expr.h:58
bool is_one() const
Return whether the expression is a constant representing 1.
Definition expr.cpp:96
bool has_operands() const
Return true if there is at least one operand.
Definition expr.h:91
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition expr.cpp:47
bool is_constant() const
Return whether the expression is a constant.
Definition expr.h:212
typet & type()
Return the type of the expression.
Definition expr.h:84
operandst & operands()
Definition expr.h:94
const source_locationt & source_location() const
Definition expr.h:231
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
Definition expr.h:170
The Boolean constant false.
Definition std_expr.h:3199
Returns one plus the index of the least-significant one bit, or zero if the operand is zero.
Fixed-width bit-vector with signed fixed-point interpretation.
fixedbv_spect spec
Definition fixedbv.h:44
void from_integer(const mp_integer &i)
Definition fixedbv.cpp:32
mp_integer to_integer() const
Definition fixedbv.cpp:37
void round(const fixedbv_spect &dest_spec)
Definition fixedbv.cpp:52
constant_exprt to_expr() const
Definition fixedbv.cpp:43
Fixed-width bit-vector with IEEE floating-point interpretation.
Application of (mathematical) function.
An IEEE 754 floating-point value, including specificiation.
Definition ieee_float.h:117
void set_sign(bool _sign)
Definition ieee_float.h:160
ieee_float_spect spec
Definition ieee_float.h:119
constant_exprt to_expr() const
mp_integer pack() const
bool get_sign() const
Definition ieee_float.h:254
An IEEE 754 value plus a rounding mode, enabling operations with rounding on values.
Definition ieee_float.h:330
mp_integer to_integer() const
void from_integer(const mp_integer &i)
void change_spec(const ieee_float_spect &dest_spec)
The trinary if-then-else operator.
Definition std_expr.h:2497
Array index operator.
Definition std_expr.h:1470
exprt & index()
Definition std_expr.h:1510
exprt & array()
Definition std_expr.h:1500
Fixed-width bit-vector representing a signed or unsigned integer.
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition irep.cpp:482
const irep_idt & id() const
Definition irep.h:388
A (mathematical) lambda expression.
Extract member of struct or union.
Definition std_expr.h:2971
Binary multiplication Associativity is not specified.
Definition std_expr.h:1107
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
The NIL expression.
Definition std_expr.h:3208
The null pointer constant.
Boolean OR.
Definition std_expr.h:2270
An expression returning both the result of the arithmetic operation under wrap-around semantics as we...
exprt & op0()
Definition expr.h:133
exprt & op1()
Definition expr.h:136
The plus expression Associativity is not specified.
Definition std_expr.h:1002
The offset (in bytes) of a pointer relative to the object.
The popcount (counting the number of bits set to 1) expression.
const exprt & length() const
const exprt & content() const
Sign of an expression Predicate is true if _op is negative, false otherwise.
Definition std_expr.h:596
Fixed-width bit-vector with two's complement interpretation.
resultt simplify_isnan(const unary_exprt &)
resultt simplify_bitwise(const multi_ary_exprt &)
const namespacet & ns
resultt simplify_div(const div_exprt &)
resultt simplify_byte_extract(const byte_extract_exprt &)
resultt simplify_bitreverse(const bitreverse_exprt &)
Try to simplify bit-reversing to a constant expression.
resultt simplify_abs(const abs_exprt &)
resultt simplify_isnormal(const unary_exprt &)
resultt simplify_dereference(const dereference_exprt &)
resultt simplify_bitnot(const bitnot_exprt &)
resultt simplify_zero_extend(const zero_extend_exprt &)
resultt simplify_prophecy_r_or_w_ok(const prophecy_r_or_w_ok_exprt &)
Try to simplify prophecy_{r,w,rw}_ok to a constant expression.
resultt simplify_member_preorder(const member_exprt &)
resultt simplify_popcount(const popcount_exprt &)
static resultt changed(resultt<> result)
resultt simplify_dereference_preorder(const dereference_exprt &)
resultt simplify_unary_pointer_predicate_preorder(const unary_exprt &)
resultt simplify_address_of(const address_of_exprt &)
resultt simplify_if(const if_exprt &)
resultt simplify_node(const exprt &)
resultt simplify_node_preorder(const exprt &)
resultt simplify_prophecy_pointer_in_range(const prophecy_pointer_in_range_exprt &)
Try to simplify prophecy_pointer_in_range to a constant expression.
resultt simplify_overflow_unary(const unary_overflow_exprt &)
Try to simplify overflow-unary-.
resultt simplify_minus(const minus_exprt &)
resultt simplify_extractbit(const extractbit_exprt &)
resultt simplify_rec(const exprt &)
resultt simplify_shifts(const shift_exprt &)
resultt simplify_index_preorder(const index_exprt &)
resultt simplify_typecast(const typecast_exprt &)
resultt simplify_pointer_object(const pointer_object_exprt &)
resultt simplify_boolean(const exprt &)
resultt simplify_object(const exprt &)
resultt simplify_mult(const mult_exprt &)
resultt simplify_floatbv_typecast(const floatbv_typecast_exprt &)
resultt simplify_with(const with_exprt &)
resultt simplify_inequality(const binary_relation_exprt &)
simplifies inequalities !=, <=, <, >=, >, and also ==
resultt simplify_not(const not_exprt &)
resultt simplify_isinf(const unary_exprt &)
resultt simplify_overflow_binary(const binary_overflow_exprt &)
Try to simplify overflow-+, overflow-*, overflow–, overflow-shl.
resultt simplify_function_application(const function_application_exprt &)
Attempt to simplify mathematical function applications if we have enough information to do so.
resultt simplify_index(const index_exprt &)
resultt simplify_bswap(const bswap_exprt &)
resultt simplify_member(const member_exprt &)
static resultt unchanged(exprt expr)
resultt simplify_byte_update(const byte_update_exprt &)
resultt simplify_extractbits(const extractbits_exprt &)
Simplifies extracting of bits from a constant.
resultt simplify_update(const update_exprt &)
resultt simplify_is_invalid_pointer(const unary_exprt &)
resultt simplify_mod(const mod_exprt &)
resultt simplify_complex(const unary_exprt &)
resultt simplify_pointer_offset(const pointer_offset_exprt &)
resultt simplify_plus(const plus_exprt &)
virtual bool simplify(exprt &expr)
resultt simplify_unary_plus(const unary_plus_exprt &)
resultt simplify_overflow_result(const overflow_result_exprt &)
Try to simplify overflow_result-+, overflow_result-*, overflow_result–, overflow_result-shl,...
resultt simplify_ffs(const find_first_set_exprt &)
Try to simplify find-first-set to a constant expression.
resultt simplify_if_preorder(const if_exprt &expr)
resultt simplify_byte_extract_preorder(const byte_extract_exprt &)
resultt simplify_is_dynamic_object(const unary_exprt &)
resultt simplify_power(const power_exprt &)
resultt simplify_object_size(const object_size_exprt &)
resultt simplify_lambda(const lambda_exprt &)
resultt simplify_concatenation(const concatenation_exprt &)
resultt simplify_floatbv_op(const ieee_float_op_exprt &)
resultt simplify_ctz(const count_trailing_zeros_exprt &)
Try to simplify count-trailing-zeros to a constant expression.
resultt simplify_clz(const count_leading_zeros_exprt &)
Try to simplify count-leading-zeros to a constant expression.
resultt simplify_ieee_float_relation(const binary_relation_exprt &)
resultt simplify_typecast_preorder(const typecast_exprt &)
resultt simplify_sign(const sign_exprt &)
resultt simplify_unary_minus(const unary_minus_exprt &)
Struct constructor from list of elements.
Definition std_expr.h:1877
Structure type, corresponds to C style structs.
Definition std_types.h:231
std::vector< componentt > componentst
Definition std_types.h:140
The Boolean constant true.
Definition std_expr.h:3190
Semantic type conversion.
Definition std_expr.h:2073
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition std_expr.h:2081
The type of an expression, extends irept.
Definition type.h:29
Generic base class for unary expressions.
Definition std_expr.h:361
const exprt & op() const
Definition std_expr.h:391
A Boolean expression returning true, iff operation kind would result in an overflow when applied to t...
Union constructor from single element.
Definition std_expr.h:1770
The union type.
Definition c_types.h:147
Fixed-width bit-vector with unsigned binary interpretation.
Operator to update elements in structs and arrays.
Definition std_expr.h:2782
exprt & old()
Definition std_expr.h:2794
exprt::operandst & designator()
Definition std_expr.h:2808
exprt & new_value()
Definition std_expr.h:2818
Operator to update elements in structs and arrays.
Definition std_expr.h:2598
int isalpha(int c)
Definition ctype.c:9
int tolower(int c)
Definition ctype.c:109
int isupper(int c)
Definition ctype.c:90
#define Forall_operands(it, expr)
Definition expr.h:27
constant_exprt make_boolean_expr(bool value)
returns true_exprt if given true and false_exprt otherwise
exprt is_not_zero(const exprt &src, const namespacet &ns)
converts a scalar/float expression to C/C++ Booleans
Definition expr_util.cpp:74
const exprt & skip_typecast(const exprt &expr)
find the expression nested inside typecasts, if any
if_exprt lift_if(const exprt &src, std::size_t operand_number)
lift up an if_exprt one level
bool has_subtype(const typet &type, const std::function< bool(const typet &)> &pred, const namespacet &ns)
returns true if any of the contained types satisfies pred
Deprecated expression utility functions.
API to expression classes for floating-point arithmetic.
const ieee_float_op_exprt & to_ieee_float_op_expr(const exprt &expr)
Cast an exprt to an ieee_float_op_exprt.
const floatbv_typecast_exprt & to_floatbv_typecast_expr(const exprt &expr)
Cast an exprt to a floatbv_typecast_exprt.
static format_containert< T > format(const T &o)
Definition format.h:37
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
static int8_t r
Definition irep_hash.h:60
API to expression classes for 'mathematical' expressions.
const power_exprt & to_power_expr(const exprt &expr)
Cast an exprt to a power_exprt.
const function_application_exprt & to_function_application_expr(const exprt &expr)
Cast an exprt to a function_application_exprt.
const lambda_exprt & to_lambda_expr(const exprt &expr)
Cast an exprt to a lambda_exprt.
const mp_integer string2integer(const std::string &n, unsigned base)
Definition mp_arith.cpp:54
API to expression classes for Pointers.
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
const pointer_offset_exprt & to_pointer_offset_expr(const exprt &expr)
Cast an exprt to a pointer_offset_exprt.
const pointer_object_exprt & to_pointer_object_expr(const exprt &expr)
Cast an exprt to a pointer_object_exprt.
std::optional< mp_integer > pointer_offset_size(const typet &type, const namespacet &ns)
Compute the size of a type in bytes, rounding up to full bytes.
std::optional< exprt > get_subexpression_at_offset(const exprt &expr, const mp_integer &offset_bytes, const typet &target_type_raw, const namespacet &ns)
std::optional< mp_integer > pointer_offset_bits(const typet &type, const namespacet &ns)
std::optional< mp_integer > member_offset(const struct_typet &type, const irep_idt &member, const namespacet &ns)
std::optional< exprt > member_offset_expr(const member_exprt &member_expr, const namespacet &ns)
Pointer Logic.
exprt pointer_offset_sum(const exprt &a, const exprt &b)
Pointer Dereferencing.
exprt object_size(const exprt &pointer)
constant_exprt from_rational(const rationalt &a)
bool replace_expr(const exprt &what, const exprt &by, exprt &dest)
bool simplify(exprt &expr, const namespacet &ns)
static simplify_exprt::resultt simplify_string_compare_to(const function_application_exprt &expr, const namespacet &ns)
Simplify String.compareTo function when arguments are constant.
static simplify_exprt::resultt simplify_string_contains(const function_application_exprt &expr, const namespacet &ns)
Simplify String.contains function when arguments are constant.
static simplify_exprt::resultt simplify_string_endswith(const function_application_exprt &expr, const namespacet &ns)
Simplify String.endsWith function when arguments are constant.
static simplify_exprt::resultt simplify_string_char_at(const function_application_exprt &expr, const namespacet &ns)
Simplify String.charAt function when arguments are constant.
static simplify_exprt::resultt simplify_string_startswith(const function_application_exprt &expr, const namespacet &ns)
Simplify String.startsWith function when arguments are constant.
static simplify_exprt::resultt simplify_string_is_empty(const function_application_exprt &expr, const namespacet &ns)
Simplify String.isEmpty function when arguments are constant.
static bool lower_case_string_expression(array_exprt &string_data)
Take the passed-in constant string array and lower-case every character.
static simplify_exprt::resultt simplify_string_index_of(const function_application_exprt &expr, const namespacet &ns, const bool search_from_end)
Simplify String.indexOf function when arguments are constant.
static simplify_exprt::resultt simplify_string_equals_ignore_case(const function_application_exprt &expr, const namespacet &ns)
Simplify String.equalsIgnorecase function when arguments are constant.
exprt simplify_expr(exprt src, const namespacet &ns)
std::optional< exprt > bits2expr(const std::string &bits, const typet &type, bool little_endian, const namespacet &ns)
std::optional< std::string > expr2bits(const exprt &expr, bool little_endian, const namespacet &ns)
std::optional< std::reference_wrapper< const array_exprt > > try_get_string_data_array(const exprt &content, const namespacet &ns)
Get char sequence from content field of a refined string expression.
bool join_operands(exprt &expr)
BigInt mp_integer
Definition smt_terms.h:17
#define CHECK_RETURN(CONDITION)
Definition invariant.h:495
#define UNREACHABLE
This should be used to mark dead code.
Definition invariant.h:525
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition invariant.h:534
#define POSTCONDITION_WITH_DIAGNOSTICS(CONDITION,...)
Definition invariant.h:480
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns) -> decltype(struct_expr.op0())
Definition std_expr.cpp:97
API to expression classes.
const struct_exprt & to_struct_expr(const exprt &expr)
Cast an exprt to a struct_exprt.
Definition std_expr.h:1900
const array_of_exprt & to_array_of_expr(const exprt &expr)
Cast an exprt to an array_of_exprt.
Definition std_expr.h:1603
const binary_relation_exprt & to_binary_relation_expr(const exprt &expr)
Cast an exprt to a binary_relation_exprt.
Definition std_expr.h:895
const unary_plus_exprt & to_unary_plus_expr(const exprt &expr)
Cast an exprt to a unary_plus_exprt.
Definition std_expr.h:556
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition std_expr.h:1538
const mod_exprt & to_mod_expr(const exprt &expr)
Cast an exprt to a mod_exprt.
Definition std_expr.h:1277
const mult_exprt & to_mult_expr(const exprt &expr)
Cast an exprt to a mult_exprt.
Definition std_expr.h:1137
const array_exprt & to_array_expr(const exprt &expr)
Cast an exprt to an array_exprt.
Definition std_expr.h:1665
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast an exprt to a typecast_exprt.
Definition std_expr.h:2107
const div_exprt & to_div_expr(const exprt &expr)
Cast an exprt to a div_exprt.
Definition std_expr.h:1206
const plus_exprt & to_plus_expr(const exprt &expr)
Cast an exprt to a plus_exprt.
Definition std_expr.h:1041
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
Definition std_expr.h:426
const multi_ary_exprt & to_multi_ary_expr(const exprt &expr)
Cast an exprt to a multi_ary_exprt.
Definition std_expr.h:987
const abs_exprt & to_abs_expr(const exprt &expr)
Cast an exprt to a abs_exprt.
Definition std_expr.h:466
const if_exprt & to_if_expr(const exprt &expr)
Cast an exprt to an if_exprt.
Definition std_expr.h:2577
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition std_expr.h:3063
const minus_exprt & to_minus_expr(const exprt &expr)
Cast an exprt to a minus_exprt.
Definition std_expr.h:1086
const complex_imag_exprt & to_complex_imag_expr(const exprt &expr)
Cast an exprt to a complex_imag_exprt.
Definition std_expr.h:2053
const index_designatort & to_index_designator(const exprt &expr)
Cast an exprt to an index_designatort.
Definition std_expr.h:2713
const complex_real_exprt & to_complex_real_expr(const exprt &expr)
Cast an exprt to a complex_real_exprt.
Definition std_expr.h:2010
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition std_expr.h:3172
const not_exprt & to_not_expr(const exprt &expr)
Cast an exprt to an not_exprt.
Definition std_expr.h:2479
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:272
const with_exprt & to_with_expr(const exprt &expr)
Cast an exprt to a with_exprt.
Definition std_expr.h:2660
const complex_exprt & to_complex_expr(const exprt &expr)
Cast an exprt to a complex_exprt.
Definition std_expr.h:1965
const update_exprt & to_update_expr(const exprt &expr)
Cast an exprt to an update_exprt.
Definition std_expr.h:2865
const unary_minus_exprt & to_unary_minus_expr(const exprt &expr)
Cast an exprt to a unary_minus_exprt.
Definition std_expr.h:514
const sign_exprt & to_sign_expr(const exprt &expr)
Cast an exprt to a sign_exprt.
Definition std_expr.h:621
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition std_types.h:308
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition std_types.h:518
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition std_types.h:888
String expressions for the string solver.
refined_string_exprt & to_string_expr(exprt &expr)
static bool failed(bool error_indicator)
const type_with_subtypet & to_type_with_subtype(const typet &type)
Definition type.h:208
#define size_type
Definition unistd.c:186