cprover
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 "nodiscard.h"
23 #include "type.h"
24 // #define USE_LOCAL_REPLACE_MAP
25 #ifdef USE_LOCAL_REPLACE_MAP
26 #include "replace_expr.h"
27 #endif
28 
29 class abs_exprt;
30 class address_of_exprt;
31 class array_exprt;
32 class binary_exprt;
35 class bitnot_exprt;
36 class bitreverse_exprt;
37 class bswap_exprt;
38 class byte_extract_exprt;
39 class byte_update_exprt;
43 class dereference_exprt;
44 class div_exprt;
45 class exprt;
46 class extractbit_exprt;
47 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 plus_exprt;
62 class popcount_exprt;
64 class shift_exprt;
65 class sign_exprt;
66 class typecast_exprt;
67 class unary_exprt;
68 class unary_minus_exprt;
70 class unary_plus_exprt;
71 class update_exprt;
72 class with_exprt;
73 
75 {
76 public:
77  explicit simplify_exprt(const namespacet &_ns):
78  do_simplify_if(true),
79  ns(_ns)
80 #ifdef DEBUG_ON_DEMAND
81  , debug_on(false)
82 #endif
83  {
84 #ifdef DEBUG_ON_DEMAND
85  struct stat f;
86  debug_on=stat("SIMP_DEBUG", &f)==0;
87 #endif
88  }
89 
90  virtual ~simplify_exprt()
91  {
92  }
93 
95 
96  template <typename T = exprt>
97  struct resultt
98  {
99  bool has_changed() const
100  {
101  return expr_changed == CHANGED;
102  }
103 
105  {
107  UNCHANGED
109 
110  T expr;
111 
113  operator T() const
114  {
115  return expr;
116  }
117 
120  // NOLINTNEXTLINE(runtime/explicit)
121  resultt(T _expr) : expr_changed(CHANGED), expr(std::move(_expr))
122  {
123  }
124 
125  resultt(expr_changedt _expr_changed, T _expr)
126  : expr_changed(_expr_changed), expr(std::move(_expr))
127  {
128  }
129  };
130 
132  {
133  return resultt<>(resultt<>::UNCHANGED, std::move(expr));
134  }
135 
137  {
139  return result;
140  }
141 
142  // These below all return 'true' if the simplification wasn't applicable.
143  // If false is returned, the expression has changed.
158  bool simplify_if_preorder(if_exprt &expr);
192 
197 
202 
207 
210 
213 
216 
217  // auxiliary
218  bool simplify_if_implies(
219  exprt &expr, const exprt &cond, bool truth, bool &new_truth);
220  bool simplify_if_recursive(exprt &expr, const exprt &cond, bool truth);
221  bool simplify_if_conj(exprt &expr, const exprt &cond);
222  bool simplify_if_disj(exprt &expr, const exprt &cond);
223  bool simplify_if_branch(exprt &trueexpr, exprt &falseexpr, const exprt &cond);
224  bool simplify_if_cond(exprt &expr);
236 
237  // main recursion
239  bool simplify_node_preorder(exprt &expr);
241 
242  virtual bool simplify(exprt &expr);
243 
244  static bool is_bitvector_type(const typet &type)
245  {
246  return type.id()==ID_unsignedbv ||
247  type.id()==ID_signedbv ||
248  type.id()==ID_bv;
249  }
250 
251 protected:
252  const namespacet &ns;
253 #ifdef DEBUG_ON_DEMAND
254  bool debug_on;
255 #endif
256 #ifdef USE_LOCAL_REPLACE_MAP
257  replace_mapt local_replace_map;
258 #endif
259 
260 };
261 
262 #endif // CPROVER_UTIL_SIMPLIFY_EXPR_CLASS_H
Absolute value.
Definition: std_expr.h:346
Operator to return the address of an object.
Definition: pointer_expr.h:361
Array constructor from list of elements.
Definition: std_expr.h:1476
A base class for binary expressions.
Definition: std_expr.h:550
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:674
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:648
Division.
Definition: std_expr.h:1064
Base class for all expressions.
Definition: expr.h:54
Extracts a single bit of a bit-vector operand.
Extracts a sub-range of a bit-vector operand.
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:2226
Array index operator.
Definition: std_expr.h:1328
const irep_idt & id() const
Definition: irep.h:396
A (mathematical) lambda expression.
Extract member of struct or union.
Definition: std_expr.h:2667
Binary minus.
Definition: std_expr.h:973
Modulo defined as lhs-(rhs * truncate(lhs/rhs)).
Definition: std_expr.h:1135
Binary multiplication Associativity is not specified.
Definition: std_expr.h:1019
A base class for multi-ary expressions Associativity is not specified.
Definition: std_expr.h:824
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
Boolean negation.
Definition: std_expr.h:2181
The plus expression Associativity is not specified.
Definition: std_expr.h:914
The popcount (counting the number of bits set to 1) expression.
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:506
static bool is_bitvector_type(const typet &type)
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_good_pointer(const unary_exprt &)
bool simplify_if_disj(exprt &expr, const exprt &cond)
resultt simplify_popcount(const popcount_exprt &)
static resultt changed(resultt<> result)
bool simplify_if_preorder(if_exprt &expr)
resultt simplify_address_of(const address_of_exprt &)
resultt simplify_if(const if_exprt &)
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_pointer_offset(const unary_exprt &)
resultt simplify_minus(const minus_exprt &)
resultt simplify_extractbit(const extractbit_exprt &)
resultt simplify_rec(const exprt &)
resultt simplify_shifts(const shift_exprt &)
bool simplify_if_cond(exprt &expr)
resultt simplify_typecast(const typecast_exprt &)
simplify_exprt(const namespacet &_ns)
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_object_size(const unary_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 &)
bool simplify_node_preorder(exprt &expr)
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_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_is_dynamic_object(const unary_exprt &)
resultt simplify_node(exprt)
bool simplify_if_implies(exprt &expr, const exprt &cond, bool truth, bool &new_truth)
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_pointer_object(const unary_exprt &)
resultt simplify_sign(const sign_exprt &)
resultt simplify_unary_minus(const unary_minus_exprt &)
Semantic type conversion.
Definition: std_expr.h:1920
The type of an expression, extends irept.
Definition: type.h:29
Generic base class for unary expressions.
Definition: std_expr.h:281
The unary minus expression.
Definition: std_expr.h:390
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:439
Operator to update elements in structs and arrays.
Definition: std_expr.h:2496
Operator to update elements in structs and arrays.
Definition: std_expr.h:2312
#define NODISCARD
Definition: nodiscard.h:22
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.