Skip to content

Commit 84baff9

Browse files
committed
Do not sort operands as part of simplification
Data for ReachSafety-BitVectors, the smallest of SV-COMP's categories, running for at most 600s per benchmark: 163M sort_operands calls, contributing to 1B irept::operator< calls, resulting in 4.5B irept::compare calls. None of these calls are actually necessary. As part of this work, also clean up naming of "sort_and_join" functions (as most of them don't actually sort or join, but instead just prepare for doing so).
1 parent 076b72e commit 84baff9

File tree

7 files changed

+40
-23
lines changed

7 files changed

+40
-23
lines changed

jbmc/regression/jbmc/throwing-function-return-value/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CORE
22
Test
33
--function Test.main --show-vcc
44
java::Test\.main:\(Z\)V::14::t1!0@1#\d+ = address_of\(symex_dynamic::dynamic_object\d+\)
5-
java::Test\.main:\(Z\)V::9::x!0@1#\d+ = 5 \+ java::Test\.main:\(Z\)V::9::x!0@1#\d+
5+
java::Test\.main:\(Z\)V::9::x!0@1#\d+ = java::Test\.main:\(Z\)V::9::x!0@1#\d+ \+ 5
66
java::Test\.g:\(\)I#return_value!0#[0-9]+ = 5
77
^EXIT=0$
88
^SIGNAL=0$

regression/goto-analyzer/constant_propagation_07/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ main.c
33
--constants --simplify out.gb
44
^EXIT=0$
55
^SIGNAL=0$
6-
^Simplified: assert: 1, assume: 0, goto: 3, assigns: 13, function calls: 0$
6+
^Simplified: assert: 1, assume: 0, goto: 3, assigns: 9, function calls: 0$
77
^Unmodified: assert: 0, assume: 0, goto: 1, assigns: 14, function calls: 2$
88
--
99
^warning: ignoring

src/util/expr.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ Author: Daniel Kroening, [email protected]
99
#ifndef CPROVER_UTIL_EXPR_H
1010
#define CPROVER_UTIL_EXPR_H
1111

12+
#include "as_const.h"
1213
#include "deprecate.h"
1314
#include "type.h"
1415
#include "validate_expressions.h"
1516
#include "validate_types.h"
1617
#include "validation_mode.h"
1718

18-
#define forall_operands(it, expr) \
19-
if((expr).has_operands()) /* NOLINT(readability/braces) */ \
20-
for(exprt::operandst::const_iterator it=(expr).operands().begin(), \
21-
it##_end=(expr).operands().end(); \
22-
it!=it##_end; ++it)
19+
#define forall_operands(it, expr) \
20+
if(as_const(expr).has_operands()) \
21+
for(exprt::operandst::const_iterator \
22+
it = as_const(expr).operands().begin(), \
23+
it##_end = as_const(expr).operands().end(); \
24+
it != it##_end; \
25+
++it)
2326

2427
#define Forall_operands(it, expr) \
2528
if((expr).has_operands()) /* NOLINT(readability/braces) */ \

src/util/simplify_expr.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,7 @@ simplify_exprt::resultt<> simplify_exprt::simplify_node(exprt node)
23802380
#endif
23812381

23822382
exprt expr = node;
2383-
bool no_change_sort_and_join = sort_and_join(expr);
2383+
bool no_change_join_operands = join_operands(expr);
23842384

23852385
resultt<> r = unchanged(expr);
23862386

@@ -2581,7 +2581,7 @@ simplify_exprt::resultt<> simplify_exprt::simplify_node(exprt node)
25812581
r = simplify_complex(to_unary_expr(expr));
25822582
}
25832583

2584-
if(!no_change_sort_and_join)
2584+
if(!no_change_join_operands)
25852585
r = changed(r);
25862586

25872587
#ifdef DEBUGX

src/util/simplify_utils.cpp

+26-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Author: Daniel Kroening, [email protected]
77
\*******************************************************************/
88

99
#include "simplify_utils.h"
10-
#include "as_const.h"
1110

1211
#include <algorithm>
1312

@@ -95,7 +94,7 @@ struct saj_tablet
9594
{ irep_idt(), { irep_idt() }}
9695
};
9796

98-
static bool sort_and_join(
97+
static bool is_associative_and_commutative_for_type(
9998
const struct saj_tablet &saj_entry,
10099
const irep_idt &type_id)
101100
{
@@ -106,42 +105,44 @@ static bool sort_and_join(
106105
return false;
107106
}
108107

109-
static const struct saj_tablet &sort_and_join(
110-
const irep_idt &id,
111-
const irep_idt &type_id)
108+
static const struct saj_tablet &
109+
get_sort_and_join_table_entry(const irep_idt &id, const irep_idt &type_id)
112110
{
113111
unsigned i=0;
114112

115113
for( ; !saj_table[i].id.empty(); i++)
116-
if(id==saj_table[i].id &&
117-
sort_and_join(saj_table[i], type_id))
114+
{
115+
if(
116+
id == saj_table[i].id &&
117+
is_associative_and_commutative_for_type(saj_table[i], type_id))
118118
return saj_table[i];
119+
}
119120

120121
return saj_table[i];
121122
}
122123

123-
bool sort_and_join(exprt &expr)
124+
static bool sort_and_join(exprt &expr, bool do_sort)
124125
{
125126
bool no_change = true;
126127

127128
if(!expr.has_operands())
128129
return true;
129130

130131
const struct saj_tablet &saj_entry =
131-
sort_and_join(expr.id(), as_const(expr).type().id());
132+
get_sort_and_join_table_entry(expr.id(), as_const(expr).type().id());
132133
if(saj_entry.id.empty())
133134
return true;
134135

135136
// check operand types
136137

137138
forall_operands(it, expr)
138-
if(!sort_and_join(saj_entry, it->type().id()))
139+
if(!is_associative_and_commutative_for_type(saj_entry, it->type().id()))
139140
return true;
140141

141142
// join expressions
142143

143144
exprt::operandst new_ops;
144-
new_ops.reserve(expr.operands().size());
145+
new_ops.reserve(as_const(expr).operands().size());
145146

146147
forall_operands(it, expr)
147148
{
@@ -159,9 +160,21 @@ bool sort_and_join(exprt &expr)
159160
}
160161

161162
// sort it
163+
if(do_sort)
164+
no_change = sort_operands(new_ops) && no_change;
162165

163-
no_change = sort_operands(new_ops) && no_change;
164-
expr.operands().swap(new_ops);
166+
if(!no_change)
167+
expr.operands().swap(new_ops);
165168

166169
return no_change;
167170
}
171+
172+
bool sort_and_join(exprt &expr)
173+
{
174+
return sort_and_join(expr, true);
175+
}
176+
177+
bool join_operands(exprt &expr)
178+
{
179+
return sort_and_join(expr, false);
180+
}

src/util/simplify_utils.h

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

1515
bool sort_operands(exprt::operandst &operands);
1616

17+
bool join_operands(exprt &expr);
18+
1719
bool sort_and_join(exprt &expr);
1820

1921
#endif // CPROVER_UTIL_SIMPLIFY_UTILS_H

src/util/std_expr.h

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Author: Daniel Kroening, [email protected]
1313
/// \file util/std_expr.h
1414
/// API to expression classes
1515

16-
#include "as_const.h"
1716
#include "expr_cast.h"
1817
#include "invariant.h"
1918
#include "narrow.h"

0 commit comments

Comments
 (0)