Skip to content

Commit 50080d8

Browse files
committed
Construct true_exprt, false_exprt exactly once
Should save 300 seconds.
1 parent e09a575 commit 50080d8

File tree

12 files changed

+61
-33
lines changed

12 files changed

+61
-33
lines changed

jbmc/src/java_bytecode/java_types.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <util/std_expr.h>
1616
#include <util/ieee_float.h>
1717
#include <util/invariant.h>
18+
#include <util/singleton_factory.h>
1819

1920
#include "java_types.h"
2021
#include "java_utils.h"
@@ -37,8 +38,7 @@ signedbv_typet java_int_type()
3738

3839
empty_typet java_void_type()
3940
{
40-
static const auto result = empty_typet();
41-
return result;
41+
return singleton_factory<empty_typet>();
4242
}
4343

4444
signedbv_typet java_long_type()

src/analyses/goto_check.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Author: Daniel Kroening, [email protected]
2929
#include <util/pointer_offset_size.h>
3030
#include <util/pointer_predicates.h>
3131
#include <util/simplify_expr.h>
32+
#include <util/singleton_factory.h>
3233
#include <util/std_expr.h>
3334
#include <util/std_types.h>
3435

@@ -1275,7 +1276,7 @@ void goto_checkt::bounds_check(
12751276
}
12761277
}
12771278

1278-
exprt type_matches_size=true_exprt();
1279+
exprt type_matches_size = singleton_factory<true_exprt>();
12791280

12801281
if(ode.root_object().id()==ID_dereference)
12811282
{
@@ -1615,7 +1616,7 @@ void goto_checkt::check_rec(const exprt &expr, guardt &guard, bool address)
16151616

16161617
void goto_checkt::check(const exprt &expr)
16171618
{
1618-
guardt guard{true_exprt{}};
1619+
guardt guard{singleton_factory<true_exprt>()};
16191620
check_rec(expr, guard, false);
16201621
}
16211622

@@ -1747,7 +1748,7 @@ void goto_checkt::goto_check(
17471748
"pointer dereference",
17481749
i.source_location,
17491750
pointer,
1750-
guardt(true_exprt()));
1751+
guardt(singleton_factory<true_exprt>()));
17511752
}
17521753
}
17531754

@@ -1785,7 +1786,7 @@ void goto_checkt::goto_check(
17851786
"pointer dereference",
17861787
i.source_location,
17871788
pointer,
1788-
guardt(true_exprt()));
1789+
guardt(singleton_factory<true_exprt>()));
17891790
}
17901791

17911792
// this has no successor
@@ -1863,7 +1864,7 @@ void goto_checkt::goto_check(
18631864
"memory-leak",
18641865
source_location,
18651866
eq,
1866-
guardt(true_exprt()));
1867+
guardt(singleton_factory<true_exprt>()));
18671868
}
18681869
}
18691870

src/goto-programs/goto_program.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Author: Daniel Kroening, [email protected]
2020

2121
#include <util/invariant.h>
2222
#include <util/namespace.h>
23+
#include <util/singleton_factory.h>
2324
#include <util/source_location.h>
2425
#include <util/std_code.h>
2526
#include <util/std_expr.h>
@@ -342,7 +343,7 @@ class goto_programt
342343
{
343344
type=_type;
344345
targets.clear();
345-
guard=true_exprt();
346+
guard = singleton_factory<true_exprt>();
346347
code.make_nil();
347348
}
348349

@@ -478,7 +479,7 @@ class goto_programt
478479
: code(static_cast<const codet &>(get_nil_irep())),
479480
source_location(static_cast<const source_locationt &>(get_nil_irep())),
480481
type(_type),
481-
guard(true_exprt())
482+
guard(singleton_factory<true_exprt>())
482483
{
483484
}
484485

@@ -965,7 +966,7 @@ class goto_programt
965966
const code_gotot &_code,
966967
const source_locationt &l = source_locationt::nil())
967968
{
968-
return instructiont(_code, l, INCOMPLETE_GOTO, true_exprt(), {});
969+
return instructiont(_code, l, INCOMPLETE_GOTO, singleton_factory<true_exprt>(), {});
969970
}
970971

971972
static instructiont make_goto(
@@ -976,7 +977,7 @@ class goto_programt
976977
static_cast<const codet &>(get_nil_irep()),
977978
l,
978979
GOTO,
979-
true_exprt(),
980+
singleton_factory<true_exprt>(),
980981
{_target});
981982
}
982983

src/goto-symex/goto_symex_state.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Author: Daniel Kroening, [email protected]
1919
#include <util/invariant.h>
2020
#include <util/make_unique.h>
2121
#include <util/sharing_map.h>
22+
#include <util/singleton_factory.h>
2223
#include <util/ssa_expr.h>
2324
#include <util/std_expr.h>
2425

@@ -51,7 +52,7 @@ class goto_statet
5152
// the if branch will be guarded by the condition of the if (and if there
5253
// is an else branch then instructions on it will be guarded by the negation
5354
// of the condition of the if).
54-
guardt guard{true_exprt{}};
55+
guardt guard{singleton_factory<true_exprt>()};
5556

5657
symex_targett::sourcet source;
5758

@@ -270,7 +271,7 @@ class goto_symex_statet final : public goto_statet
270271
struct threadt
271272
{
272273
goto_programt::const_targett pc;
273-
guardt guard{true_exprt{}};
274+
guardt guard{singleton_factory<true_exprt>()};
274275
call_stackt call_stack;
275276
std::map<irep_idt, unsigned> function_frame;
276277
unsigned atomic_section_id = 0;

src/goto-symex/symex_assign.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Author: Daniel Kroening, [email protected]
1616
#include <util/cprover_prefix.h>
1717
#include <util/exception_utils.h>
1818
#include <util/pointer_offset_size.h>
19+
#include <util/singleton_factory.h>
1920

2021
#include "goto_symex_state.h"
2122

@@ -74,7 +75,7 @@ void goto_symext::symex_assign(
7475
if(state.source.pc->source_location.get_hide())
7576
assignment_type=symex_targett::assignment_typet::HIDDEN;
7677

77-
guardt guard{true_exprt{}}; // NOT the state guard!
78+
guardt guard{singleton_factory<true_exprt>()}; // NOT the state guard!
7879
symex_assign_rec(state, lhs, nil_exprt(), rhs, guard, assignment_type);
7980
}
8081
}

src/goto-symex/symex_function_call.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Author: Daniel Kroening, [email protected]
1717
#include <util/c_types.h>
1818
#include <util/exception_utils.h>
1919
#include <util/invariant.h>
20+
#include <util/singleton_factory.h>
2021

2122
static void locality(
2223
const irep_idt &function_identifier,
@@ -145,7 +146,7 @@ void goto_symext::parameter_assignments(
145146
clean_expr(lhs, state, true);
146147
clean_expr(rhs, state, false);
147148

148-
guardt guard{true_exprt{}};
149+
guardt guard{singleton_factory<true_exprt>()};
149150
symex_assign_rec(state, lhs, nil_exprt(), rhs, guard, assignment_type);
150151
}
151152

src/goto-symex/symex_goto.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Author: Daniel Kroening, [email protected]
1717
#include <util/expr_util.h>
1818
#include <util/invariant.h>
1919
#include <util/pointer_offset_size.h>
20+
#include <util/singleton_factory.h>
2021
#include <util/std_expr.h>
2122

2223
#include <util/simplify_expr.h>
@@ -75,7 +76,7 @@ void goto_symext::symex_goto(statet &state)
7576
// generate assume(false) or a suitable negation if this
7677
// instruction is a conditional goto
7778
if(new_guard.is_true())
78-
symex_assume(state, false_exprt());
79+
symex_assume(state, singleton_factory<false_exprt>());
7980
else
8081
symex_assume(state, not_exprt(new_guard));
8182

@@ -227,7 +228,7 @@ void goto_symext::symex_goto(statet &state)
227228
// adjust guards
228229
if(new_guard.is_true())
229230
{
230-
state.guard = guardt(false_exprt());
231+
state.guard = guardt(singleton_factory<false_exprt>());
231232
}
232233
else
233234
{
@@ -251,8 +252,6 @@ void goto_symext::symex_goto(statet &state)
251252
state.rename(new_lhs, ns, goto_symex_statet::L1);
252253
state.assignment(new_lhs, new_rhs, ns, true, false);
253254

254-
guardt guard{true_exprt{}};
255-
256255
log.conditional_output(
257256
log.debug(),
258257
[this, &new_lhs](messaget::mstreamt &mstream) {
@@ -262,7 +261,7 @@ void goto_symext::symex_goto(statet &state)
262261
});
263262

264263
target.assignment(
265-
guard.as_expr(),
264+
singleton_factory<true_exprt>(),
266265
new_lhs, new_lhs, guard_symbol_expr,
267266
new_rhs,
268267
original_source,
@@ -501,7 +500,7 @@ static void merge_names(
501500
});
502501

503502
target.assignment(
504-
true_exprt(),
503+
singleton_factory<true_exprt>(),
505504
new_lhs,
506505
new_lhs,
507506
new_lhs.get_original_expr(),
@@ -551,7 +550,7 @@ void goto_symext::loop_bound_exceeded(
551550
exprt negated_cond;
552551

553552
if(guard.is_true())
554-
negated_cond=false_exprt();
553+
negated_cond = singleton_factory<false_exprt>();
555554
else
556555
negated_cond=not_exprt(guard);
557556

src/util/c_types.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected]
1010
#include "std_types.h"
1111
#include "config.h"
1212
#include "invariant.h"
13+
#include "singleton_factory.h"
1314

1415
#include "c_types.h"
1516

@@ -252,8 +253,7 @@ reference_typet reference_type(const typet &subtype)
252253

253254
empty_typet void_type()
254255
{
255-
static const auto result = empty_typet();
256-
return result;
256+
return singleton_factory<empty_typet>();
257257
}
258258

259259
std::string c_type_as_string(const irep_idt &c_type)

src/util/guard.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Author: Daniel Kroening, [email protected]
1616
#include "expr_util.h"
1717
#include "invariant.h"
1818
#include "simplify_utils.h"
19+
#include "singleton_factory.h"
1920
#include "std_expr.h"
2021

2122
void guardt::guard_expr(exprt &dest) const
@@ -110,7 +111,7 @@ guardt &operator |= (guardt &g1, const guardt &g2)
110111
exprt tmp(boolean_negate(g2.as_expr()));
111112

112113
if(tmp == g1.as_expr())
113-
g1.expr = true_exprt();
114+
g1.expr = singleton_factory<true_exprt>();
114115
else
115116
g1.expr = or_exprt(g1.as_expr(), g2.as_expr());
116117

src/util/simplify_expr_boolean.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include "invariant.h"
1616
#include "mathematical_expr.h"
1717
#include "namespace.h"
18+
#include "singleton_factory.h"
1819
#include "std_expr.h"
1920

2021
bool simplify_exprt::simplify_boolean(exprt &expr)
@@ -106,12 +107,12 @@ bool simplify_exprt::simplify_boolean(exprt &expr)
106107

107108
if(expr.id()==ID_and && is_false)
108109
{
109-
expr=false_exprt();
110+
expr = singleton_factory<false_exprt>();
110111
return false;
111112
}
112113
else if(expr.id()==ID_or && is_true)
113114
{
114-
expr=true_exprt();
115+
expr = singleton_factory<true_exprt>();
115116
return false;
116117
}
117118

@@ -182,12 +183,12 @@ bool simplify_exprt::simplify_not(exprt &expr)
182183
}
183184
else if(op.is_false())
184185
{
185-
expr=true_exprt();
186+
expr = singleton_factory<true_exprt>();
186187
return false;
187188
}
188189
else if(op.is_true())
189190
{
190-
expr=false_exprt();
191+
expr = singleton_factory<false_exprt>();
191192
return false;
192193
}
193194
else if(op.id()==ID_and ||
@@ -263,12 +264,12 @@ bool simplify_exprt::simplify_not_preorder(exprt &expr)
263264
}
264265
else if(op.is_false())
265266
{
266-
expr = true_exprt();
267+
expr = singleton_factory<true_exprt>();
267268
return false;
268269
}
269270
else if(op.is_true())
270271
{
271-
expr = false_exprt();
272+
expr = singleton_factory<false_exprt>();
272273
return false;
273274
}
274275
else if(op.id() == ID_and || op.id() == ID_or)

src/util/singleton_factory.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*******************************************************************\
2+
3+
Module: Factory to ensure sharing between default-constructible objects
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_SINGLETON_FACTORY_H
10+
#define CPROVER_UTIL_SINGLETON_FACTORY_H
11+
12+
/// Return a reference to an object of type T constructed just once. This avoids
13+
/// repeated construction, e.g., of expressions such as \ref true_exprt.
14+
template<typename T>
15+
inline const T &singleton_factory()
16+
{
17+
static T object;
18+
return object;
19+
}
20+
21+
#endif // CPROVER_UTIL_SINGLETON_FACTORY_H

src/util/std_expr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Author: Daniel Kroening, [email protected]
1717
#include "mathematical_types.h"
1818
#include "pointer_offset_size.h"
1919
#include "simplify_expr.h"
20+
#include "singleton_factory.h"
2021

2122
bool constant_exprt::value_is_zero_string() const
2223
{
@@ -27,7 +28,7 @@ bool constant_exprt::value_is_zero_string() const
2728
exprt disjunction(const exprt::operandst &op)
2829
{
2930
if(op.empty())
30-
return false_exprt();
31+
return singleton_factory<false_exprt>();
3132
else if(op.size()==1)
3233
return op.front();
3334
else
@@ -49,7 +50,7 @@ unsigned int dynamic_object_exprt::get_instance() const
4950
exprt conjunction(const exprt::operandst &op)
5051
{
5152
if(op.empty())
52-
return true_exprt();
53+
return singleton_factory<true_exprt>();
5354
else if(op.size()==1)
5455
return op.front();
5556
else

0 commit comments

Comments
 (0)