CBMC
simplify_expr_class.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_UTIL_SIMPLIFY_EXPR_CLASS_H
11 #define CPROVER_UTIL_SIMPLIFY_EXPR_CLASS_H
12 
13 // #define DEBUG_ON_DEMAND
14 #ifdef DEBUG_ON_DEMAND
15 #include <sys/stat.h>
16 #endif
17 
18 #include <set>
19 
20 #include "expr.h"
21 #include "mp_arith.h"
22 #include "type.h"
23 // #define USE_LOCAL_REPLACE_MAP
24 #ifdef USE_LOCAL_REPLACE_MAP
25 #include "replace_expr.h"
26 #endif
27 
28 class abs_exprt;
29 class address_of_exprt;
30 class array_exprt;
31 class binary_exprt;
34 class bitnot_exprt;
35 class bitreverse_exprt;
36 class bswap_exprt;
37 class byte_extract_exprt;
38 class byte_update_exprt;
42 class dereference_exprt;
43 class div_exprt;
44 class exprt;
45 class extractbit_exprt;
46 class extractbits_exprt;
51 class if_exprt;
52 class index_exprt;
53 class lambda_exprt;
54 class member_exprt;
55 class minus_exprt;
56 class mod_exprt;
57 class multi_ary_exprt;
58 class mult_exprt;
59 class namespacet;
60 class not_exprt;
61 class object_size_exprt;
63 class plus_exprt;
66 class popcount_exprt;
70 class shift_exprt;
71 class sign_exprt;
72 class typecast_exprt;
73 class unary_exprt;
74 class unary_minus_exprt;
76 class unary_plus_exprt;
77 class update_exprt;
78 class with_exprt;
79 
81 {
82 public:
83  explicit simplify_exprt(const namespacet &_ns):
84  do_simplify_if(true),
85  ns(_ns)
86 #ifdef DEBUG_ON_DEMAND
87  , debug_on(false)
88 #endif
89  {
90 #ifdef DEBUG_ON_DEMAND
91  struct stat f;
92  debug_on=stat("SIMP_DEBUG", &f)==0;
93 #endif
94  }
95 
96  virtual ~simplify_exprt()
97  {
98  }
99 
101 
102  template <typename T = exprt>
103  struct resultt
104  {
105  bool has_changed() const
106  {
107  return expr_changed == CHANGED;
108  }
109 
111  {
113  UNCHANGED
115 
116  T expr;
117 
119  operator T() const
120  {
121  return expr;
122  }
123 
126  // NOLINTNEXTLINE(runtime/explicit)
127  resultt(T _expr) : expr_changed(CHANGED), expr(std::move(_expr))
128  {
129  }
130 
131  resultt(expr_changedt _expr_changed, T _expr)
132  : expr_changed(_expr_changed), expr(std::move(_expr))
133  {
134  }
135  };
136 
137  [[nodiscard]] static resultt<> unchanged(exprt expr)
138  {
139  return resultt<>(resultt<>::UNCHANGED, std::move(expr));
140  }
141 
142  [[nodiscard]] static resultt<> changed(resultt<> result)
143  {
145  return result;
146  }
147 
148  // These below all return 'true' if the simplification wasn't applicable.
149  // If false is returned, the expression has changed.
150  [[nodiscard]] resultt<> simplify_typecast(const typecast_exprt &);
151  [[nodiscard]] resultt<> simplify_typecast_preorder(const typecast_exprt &);
152  [[nodiscard]] resultt<> simplify_extractbit(const extractbit_exprt &);
153  [[nodiscard]] resultt<> simplify_extractbits(const extractbits_exprt &);
155  [[nodiscard]] resultt<> simplify_mult(const mult_exprt &);
156  [[nodiscard]] resultt<> simplify_div(const div_exprt &);
157  [[nodiscard]] resultt<> simplify_mod(const mod_exprt &);
158  [[nodiscard]] resultt<> simplify_plus(const plus_exprt &);
159  [[nodiscard]] resultt<> simplify_minus(const minus_exprt &);
160  [[nodiscard]] resultt<> simplify_floatbv_op(const ieee_float_op_exprt &);
161  [[nodiscard]] resultt<>
163  [[nodiscard]] resultt<> simplify_shifts(const shift_exprt &);
164  [[nodiscard]] resultt<> simplify_power(const binary_exprt &);
165  [[nodiscard]] resultt<> simplify_bitwise(const multi_ary_exprt &);
166  [[nodiscard]] resultt<> simplify_if_preorder(const if_exprt &expr);
167  [[nodiscard]] resultt<> simplify_if(const if_exprt &);
168  [[nodiscard]] resultt<> simplify_bitnot(const bitnot_exprt &);
169  [[nodiscard]] resultt<> simplify_not(const not_exprt &);
170  [[nodiscard]] resultt<> simplify_boolean(const exprt &);
171  [[nodiscard]] resultt<> simplify_inequality(const binary_relation_exprt &);
172  [[nodiscard]] resultt<>
174  [[nodiscard]] resultt<> simplify_lambda(const lambda_exprt &);
175  [[nodiscard]] resultt<> simplify_with(const with_exprt &);
176  [[nodiscard]] resultt<> simplify_update(const update_exprt &);
177  [[nodiscard]] resultt<> simplify_index(const index_exprt &);
178  [[nodiscard]] resultt<> simplify_index_preorder(const index_exprt &);
179  [[nodiscard]] resultt<> simplify_member(const member_exprt &);
180  [[nodiscard]] resultt<> simplify_member_preorder(const member_exprt &);
181  [[nodiscard]] resultt<> simplify_byte_update(const byte_update_exprt &);
182  [[nodiscard]] resultt<> simplify_byte_extract(const byte_extract_exprt &);
183  [[nodiscard]] resultt<>
186  [[nodiscard]] resultt<>
188  [[nodiscard]] resultt<> simplify_object_size(const object_size_exprt &);
189  [[nodiscard]] resultt<> simplify_is_dynamic_object(const unary_exprt &);
190  [[nodiscard]] resultt<> simplify_is_invalid_pointer(const unary_exprt &);
191  [[nodiscard]] resultt<> simplify_object(const exprt &);
192  [[nodiscard]] resultt<> simplify_unary_minus(const unary_minus_exprt &);
193  [[nodiscard]] resultt<> simplify_unary_plus(const unary_plus_exprt &);
194  [[nodiscard]] resultt<> simplify_dereference(const dereference_exprt &);
195  [[nodiscard]] resultt<>
197  [[nodiscard]] resultt<> simplify_address_of(const address_of_exprt &);
199  [[nodiscard]] resultt<> simplify_bswap(const bswap_exprt &);
200  [[nodiscard]] resultt<> simplify_isinf(const unary_exprt &);
201  [[nodiscard]] resultt<> simplify_isnan(const unary_exprt &);
202  [[nodiscard]] resultt<> simplify_isnormal(const unary_exprt &);
203  [[nodiscard]] resultt<> simplify_abs(const abs_exprt &);
204  [[nodiscard]] resultt<> simplify_sign(const sign_exprt &);
205  [[nodiscard]] resultt<> simplify_popcount(const popcount_exprt &);
206  [[nodiscard]] resultt<> simplify_complex(const unary_exprt &);
207 
211  [[nodiscard]] resultt<>
213 
218 
223  [[nodiscard]] resultt<>
225 
228  [[nodiscard]] resultt<>
230 
232  [[nodiscard]] resultt<> simplify_clz(const count_leading_zeros_exprt &);
233 
235  [[nodiscard]] resultt<> simplify_ctz(const count_trailing_zeros_exprt &);
236 
238  [[nodiscard]] resultt<> simplify_bitreverse(const bitreverse_exprt &);
239 
241  [[nodiscard]] resultt<> simplify_ffs(const find_first_set_exprt &);
242 
244  [[nodiscard]] resultt<>
246 
248  [[nodiscard]] resultt<>
250 
251  // auxiliary
252  bool simplify_if_implies(
253  exprt &expr, const exprt &cond, bool truth, bool &new_truth);
254  bool simplify_if_recursive(exprt &expr, const exprt &cond, bool truth);
255  bool simplify_if_conj(exprt &expr, const exprt &cond);
256  bool simplify_if_disj(exprt &expr, const exprt &cond);
257  bool simplify_if_branch(exprt &trueexpr, exprt &falseexpr, const exprt &cond);
258  bool simplify_if_cond(exprt &expr);
259  [[nodiscard]] resultt<> simplify_address_of_arg(const exprt &);
260  [[nodiscard]] resultt<>
262  [[nodiscard]] resultt<>
264  [[nodiscard]] resultt<>
266  [[nodiscard]] resultt<>
268  [[nodiscard]] resultt<>
270 
271  // main recursion
272  [[nodiscard]] resultt<> simplify_node(const exprt &);
273  [[nodiscard]] resultt<> simplify_node_preorder(const exprt &);
274  [[nodiscard]] resultt<> simplify_rec(const exprt &);
275 
276  virtual bool simplify(exprt &expr);
277 
278 protected:
279  const namespacet &ns;
280 #ifdef DEBUG_ON_DEMAND
281  bool debug_on;
282 #endif
283 #ifdef USE_LOCAL_REPLACE_MAP
284  replace_mapt local_replace_map;
285 #endif
286 
287 };
288 
289 #endif // CPROVER_UTIL_SIMPLIFY_EXPR_CLASS_H
Absolute value.
Definition: std_expr.h:442
Operator to return the address of an object.
Definition: pointer_expr.h:540
Array constructor from list of elements.
Definition: std_expr.h:1616
A base class for binary expressions.
Definition: std_expr.h:638
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
Bit-wise negation of bit-vectors.
Reverse the order of bits in a bit-vector.
The byte swap expression.
Expression of type type extracted from some object op starting at position offset (given in number of...
Expression corresponding to op() where the bytes starting at position offset (given in number of byte...
Concatenation of bit-vector operands.
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.
Definition: pointer_expr.h:834
Division.
Definition: std_expr.h:1152
Base class for all expressions.
Definition: expr.h:56
Extracts a single bit of a bit-vector operand.
Extracts a sub-range of a bit-vector operand.
Returns one plus the index of the least-significant one bit, or zero if the operand is zero.
Semantic type conversion from/to floating-point formats.
Definition: floatbv_expr.h:19
Application of (mathematical) function.
IEEE floating-point operations These have two data operands (op0 and op1) and one rounding mode (op2)...
Definition: floatbv_expr.h:364
The trinary if-then-else operator.
Definition: std_expr.h:2370
Array index operator.
Definition: std_expr.h:1465
A (mathematical) lambda expression.
Extract member of struct or union.
Definition: std_expr.h:2844
Binary minus.
Definition: std_expr.h:1061
Modulo defined as lhs-(rhs * truncate(lhs/rhs)).
Definition: std_expr.h:1223
Binary multiplication Associativity is not specified.
Definition: std_expr.h:1107
A base class for multi-ary expressions Associativity is not specified.
Definition: std_expr.h:912
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
Boolean negation.
Definition: std_expr.h:2327
Expression for finding the size (in bytes) of the object a pointer points to.
An expression returning both the result of the arithmetic operation under wrap-around semantics as we...
The plus expression Associativity is not specified.
Definition: std_expr.h:1002
A numerical identifier for the object a pointer points to.
The offset (in bytes) of a pointer relative to the object.
The popcount (counting the number of bits set to 1) expression.
pointer_in_range (see pointer_in_range_exprt) with prophecy expressions to encode whether a pointer r...
Definition: pointer_expr.h:453
A base class for a predicate that indicates that an address range is ok to read or write or both.
A base class for shift and rotate operators.
Sign of an expression Predicate is true if _op is negative, false otherwise.
Definition: std_expr.h:596
resultt simplify_isnan(const unary_exprt &)
resultt simplify_bitwise(const multi_ary_exprt &)
resultt simplify_power(const binary_exprt &)
const namespacet & ns
resultt simplify_inequality_address_of(const binary_relation_exprt &)
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 &)
bool simplify_if_conj(exprt &expr, const exprt &cond)
resultt simplify_isnormal(const unary_exprt &)
resultt simplify_dereference(const dereference_exprt &)
resultt simplify_bitnot(const bitnot_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.
bool simplify_if_disj(exprt &expr, const exprt &cond)
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-.
bool simplify_if_recursive(exprt &expr, const exprt &cond, bool truth)
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 &)
bool simplify_if_cond(exprt &expr)
resultt simplify_typecast(const typecast_exprt &)
simplify_exprt(const namespacet &_ns)
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 &)
bool simplify_if_branch(exprt &trueexpr, exprt &falseexpr, const exprt &cond)
resultt simplify_inequality_rhs_is_constant(const binary_relation_exprt &)
resultt simplify_address_of_arg(const exprt &)
virtual ~simplify_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 &)
resultt simplify_inequality_pointer_object(const binary_relation_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_inequality_no_constant(const binary_relation_exprt &)
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 &)
bool simplify_if_implies(exprt &expr, const exprt &cond, bool truth, bool &new_truth)
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_inequality_both_constant(const binary_relation_exprt &)
simplifies inequalities for the case in which both sides of the inequality are constants
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 &)
Semantic type conversion.
Definition: std_expr.h:2068
Generic base class for unary expressions.
Definition: std_expr.h:361
The unary minus expression.
Definition: std_expr.h:484
A Boolean expression returning true, iff operation kind would result in an overflow when applied to t...
The unary plus expression.
Definition: std_expr.h:531
Operator to update elements in structs and arrays.
Definition: std_expr.h:2655
Operator to update elements in structs and arrays.
Definition: std_expr.h:2471
resultt
The result of goto verifying.
Definition: properties.h:45
std::unordered_map< exprt, exprt, irep_hash > replace_mapt
Definition: replace_expr.h:22
resultt(expr_changedt _expr_changed, T _expr)
resultt(T _expr)
conversion from expression, thus not 'explicit' marks the expression as "CHANGED"
enum simplify_exprt::resultt::expr_changedt expr_changed
Defines typet, type_with_subtypet and type_with_subtypest.