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 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;
50 class if_exprt;
51 class index_exprt;
52 class lambda_exprt;
53 class member_exprt;
54 class minus_exprt;
55 class mod_exprt;
56 class multi_ary_exprt;
57 class mult_exprt;
58 class namespacet;
59 class not_exprt;
60 class plus_exprt;
61 class popcount_exprt;
63 class shift_exprt;
64 class sign_exprt;
65 class typecast_exprt;
66 class unary_exprt;
67 class unary_minus_exprt;
69 class unary_plus_exprt;
70 class update_exprt;
71 class with_exprt;
72 
74 {
75 public:
76  explicit simplify_exprt(const namespacet &_ns):
77  do_simplify_if(true),
78  ns(_ns)
79 #ifdef DEBUG_ON_DEMAND
80  , debug_on(false)
81 #endif
82  {
83 #ifdef DEBUG_ON_DEMAND
84  struct stat f;
85  debug_on=stat("SIMP_DEBUG", &f)==0;
86 #endif
87  }
88 
89  virtual ~simplify_exprt()
90  {
91  }
92 
94 
95  template <typename T = exprt>
96  struct resultt
97  {
98  bool has_changed() const
99  {
100  return expr_changed == CHANGED;
101  }
102 
104  {
106  UNCHANGED
108 
109  T expr;
110 
112  operator T() const
113  {
114  return expr;
115  }
116 
119  // NOLINTNEXTLINE(runtime/explicit)
120  resultt(T _expr) : expr_changed(CHANGED), expr(std::move(_expr))
121  {
122  }
123 
124  resultt(expr_changedt _expr_changed, T _expr)
125  : expr_changed(_expr_changed), expr(std::move(_expr))
126  {
127  }
128  };
129 
131  {
132  return resultt<>(resultt<>::UNCHANGED, std::move(expr));
133  }
134 
136  {
138  return result;
139  }
140 
141  // These below all return 'true' if the simplification wasn't applicable.
142  // If false is returned, the expression has changed.
157  bool simplify_if_preorder(if_exprt &expr);
191 
196 
201 
206 
209 
212 
213  // auxiliary
214  bool simplify_if_implies(
215  exprt &expr, const exprt &cond, bool truth, bool &new_truth);
216  bool simplify_if_recursive(exprt &expr, const exprt &cond, bool truth);
217  bool simplify_if_conj(exprt &expr, const exprt &cond);
218  bool simplify_if_disj(exprt &expr, const exprt &cond);
219  bool simplify_if_branch(exprt &trueexpr, exprt &falseexpr, const exprt &cond);
220  bool simplify_if_cond(exprt &expr);
232 
233  // main recursion
235  bool simplify_node_preorder(exprt &expr);
237 
238  virtual bool simplify(exprt &expr);
239 
240  static bool is_bitvector_type(const typet &type)
241  {
242  return type.id()==ID_unsignedbv ||
243  type.id()==ID_signedbv ||
244  type.id()==ID_bv;
245  }
246 
247 protected:
248  const namespacet &ns;
249 #ifdef DEBUG_ON_DEMAND
250  bool debug_on;
251 #endif
252 #ifdef USE_LOCAL_REPLACE_MAP
253  replace_mapt local_replace_map;
254 #endif
255 
256 };
257 
258 #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:341
Array constructor from list of elements.
Definition: std_expr.h:1467
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.
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:628
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:2172
Array index operator.
Definition: std_expr.h:1328
const irep_idt & id() const
Definition: irep.h:407
A (mathematical) lambda expression.
Extract member of struct or union.
Definition: std_expr.h:2613
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:2127
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_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:1866
The type of an expression, extends irept.
Definition: type.h:28
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:2442
Operator to update elements in structs and arrays.
Definition: std_expr.h:2258
#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.