summaryrefslogtreecommitdiff
path: root/src/theory/bv/slicer.h
blob: 6bbe2f467d7a19be435411f8b95997380ffda213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*********************                                                        */
/*! \file slicer.h
 ** \verbatim
 ** Original author: lianah
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010, 2011  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief Bitvector theory.
 **
 ** Bitvector theory.
 **/

#include "cvc4_private.h"


#include <vector>
#include <list>
#include <ext/hash_map>
#include <math.h>

#include "util/bitvector.h"
#include "util/statistics_registry.h"
#include "util/index.h"
#include "expr/node.h"
#include "theory/bv/theory_bv_utils.h"
#include "context/context.h"
#include "context/cdhashset.h"
#include "context/cdo.h"
#include "context/cdqueue.h"
#ifndef __CVC4__THEORY__BV__SLICER_BV_H
#define __CVC4__THEORY__BV__SLICER_BV_H


namespace CVC4 {

namespace theory {
namespace bv {



typedef Index TermId;
typedef TermId ExplanationId; 
extern const TermId UndefinedId;

class CDBase; 

/** 
 * Base
 * 
 */
class Base {
  Index d_size;
  std::vector<uint32_t> d_repr;
  void undoSliceAt(Index index); 
public:
  Base (Index size);
  void sliceAt(Index index);
  
  void sliceWith(const Base& other);
  bool isCutPoint(Index index) const;
  void diffCutPoints(const Base& other, Base& res) const;
  bool isEmpty() const;
  std::string debugPrint() const;
  Index getBitwidth() const { return d_size; }
  void clear() {
    for (unsigned i = 0; i < d_repr.size(); ++i) {
      d_repr[i] = 0; 
    }
  }
  bool operator==(const Base& other) const {
    if (other.getBitwidth() != getBitwidth())
      return false;
    for (unsigned i = 0; i < d_repr.size(); ++i) {
      if (d_repr[i] != other.d_repr[i])
        return false;
    }
    return true; 
  }
}; 


/**
 * UnionFind
 * 
 */
typedef context::CDHashSet<uint32_t, std::hash<uint32_t> > CDTermSet;
typedef std::vector<TermId> Decomposition; 

struct ExtractTerm {
  TermId id;
  Index high;
  Index low;
  ExtractTerm()
    : id (UndefinedId),
      high(UndefinedId),
      low(UndefinedId)
  {}
  ExtractTerm(TermId i, Index h, Index l)
    : id (i),
      high(h),
      low(l)
  {
    Assert (h >= l && id != UndefinedId); 
  }
  bool operator== (const ExtractTerm& other) const {
    return id == other.id && high == other.high && low == other.low; 
  }
  Index getBitwidth() const { return high - low + 1; }
  std::string debugPrint() const; 
  friend class ExtractTermHashFunction; 
};

struct ExtractTermHashFunction {
  ::std::size_t operator() (const ExtractTerm& t) const {
    __gnu_cxx::hash<unsigned> h;
    unsigned id = t.id;
    unsigned high = t.high;
    unsigned low = t.low; 
    return (h(id) * 7919 + h(high))* 4391 + h(low); 
  }
}; 

class UnionFind; 

struct NormalForm {
  Base base;
  Decomposition decomp;

  NormalForm(Index bitwidth)
    : base(bitwidth),
      decomp()
  {}
  /** 
   * Returns the term in the decomposition on which the index i
   * falls in
   * @param i 
   * 
   * @return 
   */
  std::pair<TermId, Index> getTerm(Index i, const UnionFind& uf) const;
  std::string debugPrint(const UnionFind& uf) const;
  void clear() { base.clear(); decomp.clear(); }
};

class Slicer; 

class UnionFind : public context::ContextNotifyObj {

  struct ReprEdge {
    TermId repr;
    ExplanationId reason;
    ReprEdge()
      : repr(UndefinedId),
        reason(UndefinedId)
    {}
  }; 
    
  class Node {
    Index d_bitwidth;  
    TermId d_ch1, d_ch0; // the ids of the two children if they exist
    ReprEdge d_edge;     // points to the representative and stores the explanation
    
  public:
    Node(Index b)
  : d_bitwidth(b),
    d_ch1(UndefinedId),
    d_ch0(UndefinedId), 
    d_edge()
    {}

    TermId getRepr() const { return d_edge.repr; }
    ExplanationId getReason() const { return d_edge.reason; }
    Index getBitwidth() const { return d_bitwidth; }
    bool hasChildren() const { return d_ch1 != UndefinedId && d_ch0 != UndefinedId; }

    TermId getChild(Index i) const {
      Assert (i < 2);
      return i == 0? d_ch0 : d_ch1;
    }
    void setRepr(TermId repr, ExplanationId reason) {
      Assert (! hasChildren());
      d_edge.repr = repr;
      d_edge.reason = reason; 
    }
    void setChildren(TermId ch1, TermId ch0) {
      // Assert (d_repr == UndefinedId && !hasChildren());
      d_ch1 = ch1;
      d_ch0 = ch0; 
    }
    std::string debugPrint() const;
  };
  
  /// map from TermId to the nodes that represent them 
  std::vector<Node> d_nodes;
  __gnu_cxx::hash_map<TermId, ExtractTerm, __gnu_cxx::hash<TermId> > d_idToExtract;
  __gnu_cxx::hash_map<ExtractTerm, TermId, ExtractTermHashFunction > d_extractToId;
  
  void getDecomposition(const ExtractTerm& term, Decomposition& decomp);
  void getDecompositionWithExplanation(const ExtractTerm& term, Decomposition& decomp, std::vector<ExplanationId>& explanation);
  void handleCommonSlice(const Decomposition& d1, const Decomposition& d2, TermId common);
  /// getter methods for the internal nodes
  TermId getRepr(TermId id)  const {
    Assert (id < d_nodes.size());
    return d_nodes[id].getRepr(); 
  }
  ExplanationId getReason(TermId id) const {
    Assert (id < d_nodes.size());
    return d_nodes[id].getReason(); 
  }
  TermId getChild(TermId id, Index i) const {
    Assert (id < d_nodes.size());
    return d_nodes[id].getChild(i); 
  }
  Index getCutPoint(TermId id) const {
    return getBitwidth(getChild(id, 0)); 
  }
  bool hasChildren(TermId id) const {
    Assert (id < d_nodes.size());
    return d_nodes[id].hasChildren(); 
  }
  TermId getTopLevel(TermId id) const;
  
  /// setter methods for the internal nodes
  void setRepr(TermId id, TermId new_repr, ExplanationId reason) {
    Assert (id < d_nodes.size());
    d_nodes[id].setRepr(new_repr, reason); 
  }
  void setChildren(TermId id, TermId ch1, TermId ch0) {
    Assert ((ch1 == UndefinedId && ch0 == UndefinedId) ||
            (id < d_nodes.size() && getBitwidth(id) == getBitwidth(ch1) + getBitwidth(ch0)));
    d_nodes[id].setChildren(ch1, ch0); 
  }

  /* Backtracking mechanisms */

  enum OperationKind {
    MERGE,
    SPLIT
  }; 

  struct Operation {
    OperationKind op;
    TermId id;
    Operation(OperationKind o, TermId i)
      : op(o), id(i) {}
  };

  std::vector<Operation> d_undoStack;
  context::CDO<unsigned> d_undoStackIndex;

  void backtrack();
  void undoMerge(TermId id);
  void undoSplit(TermId id); 
  void recordOperation(OperationKind op, TermId term);
  virtual ~UnionFind() throw(AssertionException) {}
  class Statistics {
  public:
    IntStat d_numNodes; 
    IntStat d_numRepresentatives;
    IntStat d_numSplits;
    IntStat d_numMerges;
    AverageStat d_avgFindDepth;
    ReferenceStat<unsigned> d_numAddedEqualities; 
    Statistics();
    ~Statistics();
  };
  Statistics d_statistics;
  Slicer* d_slicer; 
public:
  UnionFind(context::Context* ctx, Slicer* slicer)
    : ContextNotifyObj(ctx), 
      d_nodes(),
      d_idToExtract(),
      d_extractToId(), 
      d_undoStack(),
      d_undoStackIndex(ctx),
      d_statistics(),
      d_slicer(slicer)
  {}

  TermId addNode(Index bitwidth);
  TermId addExtract(Index topLevel, Index high, Index low);
  ExtractTerm getExtractTerm(TermId id) const;
  bool isExtractTerm(TermId id) const; 
  
  void unionTerms(const ExtractTerm& t1, const ExtractTerm& t2, TermId reason); 
  void merge(TermId t1, TermId t2, TermId reason);
  TermId find(TermId t1);
  TermId findWithExplanation(TermId id, std::vector<ExplanationId>& explanation); 
  void split(TermId term, Index i);
  void getNormalForm(const ExtractTerm& term, NormalForm& nf);
  void getNormalFormWithExplanation(const ExtractTerm& term, NormalForm& nf, std::vector<ExplanationId>& explanation); 
  void alignSlicings(const ExtractTerm& term1, const ExtractTerm& term2);
  void ensureSlicing(const ExtractTerm& term);
  Index getBitwidth(TermId id) const {
    Assert (id < d_nodes.size());
    return d_nodes[id].getBitwidth(); 
  }
  void getBase(TermId id, Base& base, Index offset); 
  std::string debugPrint(TermId id);

  void contextNotifyPop() {
    backtrack();
  }
  friend class Slicer; 
};

class CoreSolver; 

class Slicer {
  __gnu_cxx::hash_map<TermId, TNode, __gnu_cxx::hash<TermId> > d_idToNode;
  __gnu_cxx::hash_map<TNode, TermId, TNodeHashFunction> d_nodeToId;
  __gnu_cxx::hash_map<TNode, bool, TNodeHashFunction> d_coreTermCache;
  __gnu_cxx::hash_map<TNode, ExplanationId, TNodeHashFunction> d_explanationToId;
  std::vector<TNode> d_explanations; 
  UnionFind d_unionFind;

  context::CDQueue<Node> d_newSplits;
  context::CDO<unsigned>  d_newSplitsIndex;
  CoreSolver* d_coreSolver;
  TermId d_termIdCount; 
public:
  Slicer(context::Context* ctx, CoreSolver* coreSolver)
    : d_idToNode(),
      d_nodeToId(),
      d_coreTermCache(),
      d_explanationToId(),
      d_explanations(), 
      d_unionFind(ctx, this),
      d_newSplits(ctx),
      d_newSplitsIndex(ctx, 0),
      d_coreSolver(coreSolver)
  {}
  
  void getBaseDecomposition(TNode node, std::vector<Node>& decomp, std::vector<TNode>& explanation);
  void registerEquality(TNode eq);
  ExtractTerm registerTerm(TNode node);
  void processEquality(TNode eq);
  void assertEquality(TNode eq);
  bool isCoreTerm (TNode node);

  bool hasNode(TermId id) const; 
  Node  getNode(TermId id) const;
  TermId getId(TNode node) const;

  bool hasExplanation(ExplanationId id) const;
  TNode getExplanation(ExplanationId id) const;
  ExplanationId getExplanationId(TNode reason) const;
  
  bool termInEqualityEngine(TermId id); 
  void enqueueSplit(TermId id, Index i);
  void getNewSplits(std::vector<Node>& splits);
  static void splitEqualities(TNode node, std::vector<Node>& equalities);
  static unsigned d_numAddedEqualities;
}; 


}/* CVC4::theory::bv namespace */
}/* CVC4::theory namespace */
}/* CVC4 namespace */

#endif /* __CVC4__THEORY__BV__SLICER_BV_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback