CBMC
equality.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "equality.h"
10 
11 #ifdef DEBUG
12 #include <iostream>
13 #endif
14 
15 #include "bv_utils.h"
16 
17 literalt equalityt::equality(const exprt &e1, const exprt &e2)
18 {
19  if(e1<e2)
20  return equality2(e1, e2);
21  else
22  return equality2(e2, e1);
23 }
24 
26 {
27  PRECONDITION(e1.type() == e2.type());
28 
29  const typet &type = e1.type();
30 
31  // check for syntactical equality
32 
33  if(e1==e2)
34  return const_literal(true);
35 
36  // check for boolean equality
37 
38  INVARIANT(
39  type.id() != ID_bool, "method shall not be used to compare Boolean types");
40 
41  // look it up
42 
43  typestructt &typestruct=typemap[type];
44  elementst &elements=typestruct.elements;
45  elements_revt &elements_rev=typestruct.elements_rev;
46  equalitiest &equalities=typestruct.equalities;
47 
48  std::pair<unsigned, unsigned> u;
49 
50  {
51  std::pair<elementst::iterator, bool> result=
52  elements.insert(std::pair<exprt, unsigned>(e1, elements.size()));
53 
54  u.first=result.first->second;
55 
56  if(result.second)
57  elements_rev[u.first]=e1;
58  }
59 
60  {
61  std::pair<elementst::iterator, bool> result=
62  elements.insert(elementst::value_type(e2, elements.size()));
63 
64  u.second=result.first->second;
65 
66  if(result.second)
67  elements_rev[u.second]=e2;
68  }
69 
70  literalt l;
71 
72  {
73  equalitiest::const_iterator result=equalities.find(u);
74 
75  if(result==equalities.end())
76  {
77  l=prop.new_variable();
78  if(freeze_all && !l.is_constant())
79  prop.set_frozen(l);
80  equalities.insert(equalitiest::value_type(u, l));
81  }
82  else
83  l=result->second;
84  }
85 
86  return l;
87 }
88 
90 {
91  for(typemapt::const_iterator it=typemap.begin();
92  it!=typemap.end(); it++)
93  add_equality_constraints(it->second);
94 }
95 
97 {
98  std::size_t no_elements=typestruct.elements.size();
99  std::size_t bits=0;
100 
101  // get number of necessary bits
102 
103  for(std::size_t i=no_elements; i!=0; bits++)
104  i=(i>>1);
105 
106  // generate bit vectors
107 
108  std::vector<bvt> eq_bvs;
109 
110  eq_bvs.resize(no_elements);
111 
112  for(std::size_t i=0; i<no_elements; i++)
113  eq_bvs[i] = prop.new_variables(bits);
114 
115  // generate equality constraints
116 
117  bv_utilst bv_utils(prop);
118 
119  for(equalitiest::const_iterator
120  it=typestruct.equalities.begin();
121  it!=typestruct.equalities.end();
122  it++)
123  {
124  const bvt &bv1=eq_bvs[it->first.first];
125  const bvt &bv2=eq_bvs[it->first.second];
126 
127  prop.set_equal(bv_utils.equal(bv1, bv2), it->second);
128  }
129 }
literalt equal(const bvt &op0, const bvt &op1)
Bit-blasting ID_equal and use in other encodings.
Definition: bv_utils.cpp:1369
std::map< std::pair< unsigned, unsigned >, literalt > equalitiest
Definition: equality.h:40
std::unordered_map< const exprt, unsigned, irep_hash > elementst
Definition: equality.h:39
virtual void add_equality_constraints()
Definition: equality.cpp:89
typemapt typemap
Definition: equality.h:52
std::map< unsigned, exprt > elements_revt
Definition: equality.h:41
virtual literalt equality2(const exprt &e1, const exprt &e2)
Definition: equality.cpp:25
virtual literalt equality(const exprt &e1, const exprt &e2)
Definition: equality.cpp:17
Base class for all expressions.
Definition: expr.h:56
typet & type()
Return the type of the expression.
Definition: expr.h:84
const irep_idt & id() const
Definition: irep.h:388
bool is_constant() const
Definition: literal.h:166
virtual void set_frozen(literalt)
Definition: prop.h:117
virtual void set_equal(literalt a, literalt b)
asserts a==b in the propositional formula
Definition: prop.cpp:12
virtual bvt new_variables(std::size_t width)
generates a bitvector of given width with new variables
Definition: prop.cpp:30
virtual literalt new_variable()=0
The type of an expression, extends irept.
Definition: type.h:29
std::vector< literalt > bvt
Definition: literal.h:201
literalt const_literal(bool value)
Definition: literal.h:188
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
elements_revt elements_rev
Definition: equality.h:46
elementst elements
Definition: equality.h:45
equalitiest equalities
Definition: equality.h:47