Skip to content

Commit 07378c7

Browse files
committed
Cache expensive circuits for given input bvts
This makes solving trivial in presence of temporary intermediate variables.
1 parent ec0e36c commit 07378c7

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/solvers/flattening/bv_utils.cpp

+25-3
Original file line numberDiff line numberDiff line change
@@ -976,13 +976,23 @@ bvt bv_utilst::multiplier(
976976
const bvt &op1,
977977
representationt rep)
978978
{
979+
auto cache_entry =
980+
circuit_cache[{ID_mult, rep}].insert({{op0, op1}, {bvt{}}});
981+
if(!cache_entry.second)
982+
return cache_entry.first->second[0];
983+
979984
switch(rep)
980985
{
981-
case representationt::SIGNED: return signed_multiplier(op0, op1);
982-
case representationt::UNSIGNED: return unsigned_multiplier(op0, op1);
986+
case representationt::SIGNED:
987+
cache_entry.first->second[0] = signed_multiplier(op0, op1);
988+
case representationt::UNSIGNED:
989+
cache_entry.first->second[0] = unsigned_multiplier(op0, op1);
983990
}
984991

985-
UNREACHABLE;
992+
// multiplication is commutative
993+
circuit_cache[{ID_mult, rep}][{op1, op0}] = {cache_entry.first->second};
994+
995+
return cache_entry.first->second[0];
986996
}
987997

988998
bvt bv_utilst::multiplier_no_overflow(
@@ -1045,13 +1055,25 @@ void bv_utilst::divider(
10451055
{
10461056
PRECONDITION(prop.has_set_to());
10471057

1058+
auto cache_entry =
1059+
circuit_cache[{ID_div, rep}].insert({{op0, op1}, {bvt{}, bvt{}}});
1060+
if(!cache_entry.second)
1061+
{
1062+
result = cache_entry.first->second[0];
1063+
remainer = cache_entry.first->second[1];
1064+
return;
1065+
}
1066+
10481067
switch(rep)
10491068
{
10501069
case representationt::SIGNED:
10511070
signed_divider(op0, op1, result, remainer); break;
10521071
case representationt::UNSIGNED:
10531072
unsigned_divider(op0, op1, result, remainer); break;
10541073
}
1074+
1075+
cache_entry.first->second[0] = result;
1076+
cache_entry.first->second[1] = remainer;
10551077
}
10561078

10571079
void bv_utilst::unsigned_divider(

src/solvers/flattening/bv_utils.h

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include <solvers/prop/prop.h>
1616

17+
#include <map>
18+
1719
// Shares variables between var == const tests for registered variables.
1820
// Gives ~15% memory savings on some programs using constant arrays
1921
// but seems to give a run-time penalty.
@@ -245,6 +247,11 @@ class bv_utilst
245247
bvt cond_negate_no_overflow(const bvt &bv, const literalt cond);
246248

247249
bvt wallace_tree(const std::vector<bvt> &pps);
250+
251+
using circuit_cachet = std::map<
252+
std::pair<irep_idt, representationt>,
253+
std::map<std::vector<bvt>, std::vector<bvt>>>;
254+
circuit_cachet circuit_cache;
248255
};
249256

250257
#endif // CPROVER_SOLVERS_FLATTENING_BV_UTILS_H

0 commit comments

Comments
 (0)