Skip to content

Commit ef00e0a

Browse files
committed
Keep track of whether an irept has been subject to simplification
This should avoid redundant simplification.
1 parent bd26a59 commit ef00e0a

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

src/util/irep.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Author: Daniel Kroening, [email protected]
2020
#define SHARING
2121
#define HASH_CODE
2222
#define NAMED_SUB_IS_FORWARD_LIST
23+
#define IS_CANONICAL_BIT
2324

2425
#ifdef NAMED_SUB_IS_FORWARD_LIST
2526
#include <forward_list>
@@ -143,13 +144,20 @@ class tree_nodet : public ref_count_ift<sharing>
143144
mutable std::size_t hash_code = 0;
144145
#endif
145146

147+
#ifdef IS_CANONICAL_BIT
148+
bool is_canonical = true;
149+
#endif
150+
146151
void clear()
147152
{
148153
data.clear();
149154
sub.clear();
150155
named_sub.clear();
151156
#ifdef HASH_CODE
152157
hash_code = 0;
158+
#endif
159+
#ifdef IS_CANONICAL_BIT
160+
is_canonical = true;
153161
#endif
154162
}
155163

@@ -160,6 +168,9 @@ class tree_nodet : public ref_count_ift<sharing>
160168
d.named_sub.swap(named_sub);
161169
#ifdef HASH_CODE
162170
std::swap(d.hash_code, hash_code);
171+
#endif
172+
#ifdef IS_CANONICAL_BIT
173+
std::swap(d.is_canonical, is_canonical);
163174
#endif
164175
}
165176

@@ -173,6 +184,10 @@ class tree_nodet : public ref_count_ift<sharing>
173184
: data(std::move(_data)),
174185
named_sub(std::move(_named_sub)),
175186
sub(std::move(_sub))
187+
#ifdef IS_CANONICAL_BIT
188+
,
189+
is_canonical(false)
190+
#endif
176191
{
177192
}
178193
};
@@ -282,6 +297,9 @@ class sharing_treet
282297
detach();
283298
#ifdef HASH_CODE
284299
data->hash_code = 0;
300+
#endif
301+
#ifdef IS_CANONICAL_BIT
302+
data->is_canonical = false;
285303
#endif
286304
return *data;
287305
}
@@ -323,6 +341,9 @@ class non_sharing_treet
323341
{
324342
#ifdef HASH_CODE
325343
data.hash_code = 0;
344+
#endif
345+
#ifdef IS_CANONICAL_BIT
346+
data.is_canonical = false;
326347
#endif
327348
return data;
328349
}
@@ -494,6 +515,22 @@ class irept
494515

495516
/// count the number of named_sub elements that are not comments
496517
static std::size_t number_of_non_comments(const named_subt &);
518+
519+
#ifdef IS_CANONICAL_BIT
520+
bool get_is_canonical() const
521+
{
522+
return read().is_canonical;
523+
}
524+
525+
void set_is_canonical()
526+
{
527+
#ifdef SHARING
528+
data->is_canonical = true;
529+
#else
530+
data.is_canonical = true;
531+
#endif
532+
}
533+
#endif
497534
};
498535

499536
// NOLINTNEXTLINE(readability/identifiers)

src/util/simplify_expr.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,9 +2464,14 @@ bool simplify_exprt::simplify_node(exprt &expr)
24642464
/// \return returns true if expression unchanged; returns false if changed
24652465
bool simplify_exprt::simplify_rec(exprt &expr)
24662466
{
2467-
// look up in cache
2467+
#ifdef IS_CANONICAL_BIT
2468+
if(expr.get_is_canonical())
2469+
return true;
2470+
#endif
2471+
2472+
// look up in cache
24682473

2469-
#ifdef USE_CACHE
2474+
#ifdef USE_CACHE
24702475
std::pair<simplify_expr_cachet::containert::iterator, bool>
24712476
cache_result=simplify_expr_cache.container().
24722477
insert(std::pair<exprt, exprt>(expr, exprt()));
@@ -2514,11 +2519,19 @@ bool simplify_exprt::simplify_rec(exprt &expr)
25142519
{
25152520
expr.swap(tmp);
25162521

2517-
#ifdef USE_CACHE
2522+
#ifdef IS_CANONICAL_BIT
2523+
expr.set_is_canonical();
2524+
#endif
2525+
2526+
#ifdef USE_CACHE
25182527
// save in cache
25192528
cache_result.first->second=expr;
25202529
#endif
25212530
}
2531+
#ifdef IS_CANONICAL_BIT
2532+
else
2533+
expr.set_is_canonical();
2534+
#endif
25222535

25232536
return result;
25242537
}

unit/util/irep.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,17 @@ SCENARIO("irept_memory", "[core][utils][irept]")
5959
const std::size_t hash_code_size = 0;
6060
#endif
6161

62+
#ifdef HASH_CODE
63+
const std::size_t is_canonical_size = sizeof(bool);
64+
#else
65+
const std::size_t is_canonical_size = 0;
66+
#endif
67+
6268
REQUIRE(
6369
sizeof(irept::dt) ==
64-
ref_count_size + data_size + sub_size + named_size + hash_code_size);
70+
((ref_count_size + data_size + sub_size + named_size + hash_code_size +
71+
is_canonical_size + (alignof(irept::dt) - 1)) &
72+
~(alignof(irept::dt) - 1)));
6573
}
6674

6775
THEN("get_nil_irep yields ID_nil")

0 commit comments

Comments
 (0)