39 if(expr.
id()==ID_symbol ||
40 expr.
id()==ID_index ||
41 expr.
id()==ID_member ||
42 expr.
id()==ID_dereference)
48 for(
const auto &op : expr.
operands())
67 for(goto_programt::instructionst::const_iterator
68 it=instructions.begin();
69 it!=instructions.end();
78 for(
const auto &step : path)
86 for(
const auto &step : loop)
95 modified.insert(t->assign_lhs());
99 std::map<exprt, polynomialt> polynomials,
115 std::vector<exprt> polynomials_hold;
120 for(std::map<exprt, polynomialt>::iterator it=polynomials.begin();
121 it!=polynomials.end();
124 const equal_exprt holds(it->first, it->second.to_expr());
127 polynomials_hold.push_back(holds);
140 for(
const auto &p : polynomials_hold)
144 std::cout <<
"Checking following program for inductiveness:\n";
154 std::cout <<
"Not inductive!\n";
163 catch(
const std::string &s)
165 std::cout <<
"Error in inductiveness SAT check: " << s <<
'\n';
168 catch (
const char *s)
170 std::cout <<
"Error in inductiveness SAT check: " << s <<
'\n';
177 std::map<exprt, polynomialt> &polynomials,
186 for(std::map<exprt, polynomialt>::iterator it=polynomials.begin();
187 it!=polynomials.end();
190 it->second.substitute(substitution);
201 std::set<symbol_exprt> vars;
202 for(
const exprt &expr : modified)
205 vars.erase(loop_counter_sym);
227 for(patht::reverse_iterator r_it=path.rbegin();
236 const exprt &lhs = t->assign_lhs();
237 const exprt &rhs = t->assign_rhs();
239 if(lhs.
id()==ID_symbol ||
240 lhs.
id()==ID_index ||
241 lhs.
id()==ID_dereference)
250 else if(t->is_assume() || t->is_assert())
259 if(!r_it->guard.is_true() && !r_it->guard.is_nil())
279 if(expr.
id()==ID_index ||
280 expr.
id()==ID_dereference)
282 expr_mapt::iterator it=abstractions.find(expr);
284 if(it==abstractions.end())
311 if(expr.
id() == ID_not &&
to_not_expr(expr).op().
id() == ID_nondet)
315 else if(expr.
id()==ID_equal ||
322 if(rel_expr.lhs().id() == ID_nondet || rel_expr.rhs().id() == ID_nondet)
330 std::map<exprt, polynomialt> polynomials,
365 std::vector<exprt> polynomials_hold;
367 for(std::map<exprt, polynomialt>::iterator it=polynomials.begin();
368 it!=polynomials.end();
372 exprt rhs=it->second.to_expr();
379 for(std::vector<exprt>::iterator it=polynomials_hold.begin();
380 it!=polynomials_hold.end();
392 for(std::map<exprt, polynomialt>::iterator p_it=polynomials.begin();
393 p_it!=polynomials.end();
396 program.
assign(p_it->first, p_it->second.to_expr());
399 program.
assume(condition);
404 for(std::map<exprt, polynomialt>::iterator p_it=polynomials.begin();
405 p_it!=polynomials.end();
408 program.
assign(p_it->first, p_it->second.to_expr());
419 std::cout <<
"Checking following program for monotonicity:\n";
428 std::cout <<
"Path is not monotone\n";
434 catch(
const std::string &s)
436 std::cout <<
"Error in monotonicity SAT check: " << s <<
'\n';
441 std::cout <<
"Error in monotonicity SAT check: " << s <<
'\n';
446 std::cout <<
"Path is monotone\n";
460 checker_options.
set_option(
"signed-overflow-check",
true);
461 checker_options.
set_option(
"unsigned-overflow-check",
true);
462 checker_options.
set_option(
"assert-to-assume",
true);
463 checker_options.
set_option(
"assumptions",
true);
469 std::cout <<
"Adding overflow checks at " << now <<
"...\n";
482 std::cout <<
"Done at " << now <<
".\n";
492 for(goto_programt::instructionst::reverse_iterator r_it=loop_body.rbegin();
493 r_it!=loop_body.rend();
496 if(r_it->is_assign())
499 exprt assignment_lhs = r_it->assign_lhs();
500 exprt assignment_rhs = r_it->assign_rhs();
502 if(assignment_lhs.
id() == ID_index)
505 assignments.push_back(std::make_pair(assignment_lhs, assignment_rhs));
509 arrays_written.insert(index_expr.
array());
515 for(expr_pairst::iterator a_it=assignments.begin();
516 a_it!=assignments.end();
519 replace_expr(assignment_lhs, assignment_rhs, a_it->first);
520 replace_expr(assignment_lhs, assignment_rhs, a_it->second);
531 std::map<exprt, polynomialt> &polynomials,
536 std::cout <<
"Doing arrays...\n";
545 std::cout <<
"Found " << array_assignments.size()
546 <<
" array assignments\n";
549 if(array_assignments.empty())
559 array_assignments, polynomials, poly_assignments, nondet_indices))
564 std::cout <<
"Couldn't model an array assignment :-(\n";
570 for(expr_sett::iterator it=arrays_written.begin();
571 it!=arrays_written.end();
581 for(polynomial_array_assignmentst::iterator it=poly_assignments.begin();
582 it!=poly_assignments.end();
613 for(expr_sett::iterator a_it=arrays_written.begin();
614 a_it!=arrays_written.end();
618 exprt old_array=substitution[array];
619 std::vector<polynomialt> indices;
620 bool nonlinear_index=
false;
622 for(polynomial_array_assignmentst::iterator it=poly_assignments.begin();
623 it!=poly_assignments.end();
630 indices.push_back(index);
638 nonlinear_index=
true;
660 pos_eliminator=neg_eliminator;
661 pos_eliminator.
mult(-1);
665 for(std::vector<polynomialt>::iterator it=indices.begin();
670 exprt max_idx, min_idx;
678 index.
add(pos_eliminator);
687 index.
add(neg_eliminator);
698 unchanged_operands.push_back(unchanged_by_this_one);
711 for(std::vector<polynomialt>::iterator it=indices.begin();
715 idx_touched_operands.push_back(
733 implies_exprt idx_not_touched_bound(l_bound, idx_not_touched);
735 idx_never_touched=
exprt(ID_forall);
750 implies_exprt idx_unchanged(idx_never_touched, value_unchanged);
758 program.
assume(array_unchanged);
766 std::map<exprt, polynomialt> &polynomials,
770 for(expr_pairst::iterator it=array_assignments.begin();
771 it!=array_assignments.end();
783 std::cout <<
"Couldn't convert index: "
790 std::cout <<
"Converted index to: "
795 if(it->second.id()==ID_nondet)
797 nondet_indices.push_back(poly_assignment.
index);
799 else if(!
expr2poly(it->second, polynomials, poly_assignment.
value))
803 std::cout <<
"Couldn't convert RHS: " <<
expr2c(it->second,
ns)
811 std::cout <<
"Converted RHS to: "
816 array_polynomials.push_back(poly_assignment);
825 std::map<exprt, polynomialt> &polynomials,
828 exprt subbed_expr=expr;
830 for(std::map<exprt, polynomialt>::iterator it=polynomials.begin();
831 it!=polynomials.end();
834 replace_expr(it->first, it->second.to_expr(), subbed_expr);
838 std::cout <<
"expr2poly(" <<
expr2c(subbed_expr,
ns) <<
")\n";
855 std::map<exprt, polynomialt> &polynomials,
866 std::unordered_set<index_exprt, irep_hash> array_writes;
870 for(std::map<exprt, polynomialt>::iterator it=polynomials.begin();
871 it!=polynomials.end();
874 const exprt &var=it->first;
889 for(expr_sett::iterator it=nonrecursive.begin();
890 it!=nonrecursive.end();
897 for(goto_programt::instructionst::iterator it=body.begin();
903 exprt lhs = it->assign_lhs();
904 exprt rhs = it->assign_rhs();
906 if(lhs.
id()==ID_dereference)
910 std::cout <<
"Bailing out on write-through-pointer\n";
915 if(lhs.
id()==ID_index)
919 array_writes.insert(lhs_index_expr);
921 if(arrays_written.find(lhs_index_expr.array()) != arrays_written.end())
926 std::cout <<
"Bailing out on array written to twice in loop: "
927 <<
expr2c(lhs_index_expr.array(),
ns) <<
'\n';
932 arrays_written.insert(lhs_index_expr.array());
943 for(expr_sett::iterator it=arrays_written.begin();
944 it!=arrays_written.end();
947 if(arrays_read.find(*it)!=arrays_read.end())
950 std::cout <<
"Bailing out on array read and written on same path\n";
956 for(expr_sett::iterator it=nonrecursive.begin();
957 it!=nonrecursive.end();
960 if(it->id()==ID_symbol)
962 exprt &val=state[*it];
966 std::cout <<
"Fitted nonrecursive: " <<
expr2c(*it,
ns) <<
"=" <<
972 for(
const auto &
write : array_writes)
974 const auto &lhs =
write;
975 const auto &rhs = state[
write];
980 std::cout <<
"Failed to assign a nonrecursive array: " <<
996 std::cout <<
"Modelling array assignment " <<
expr2c(lhs,
ns) <<
"=" <<
1000 if(lhs.
id()==ID_dereference)
1004 std::cout <<
"Bailing out on write-through-pointer\n";
1022 const exprt &fresh_array =
1050 std::cout <<
"Trying to polynomialize " <<
expr2c(idx,
ns) <<
'\n';
1057 if(idx.
id()==ID_pointer_offset)
1070 std::cout <<
"Failed to polynomialize\n";
1084 std::cout <<
"Bailing out on nonlinear index: "
1092 exprt lower_bound=idx;
1093 exprt upper_bound=idx;
1114 std::cout <<
"Bailing out on write to constant array index: " <<
1119 else if(stride == 1 || stride == -1)
1157 exprt fresh_lhs=lhs;
1168 forall.
where() = implies;
1173 program.
assign(arr, fresh_array);
1182 if(e.
id() == ID_index)
1186 else if(e.
id() == ID_dereference)
1199 std::set<std::pair<expr_listt, exprt> > &coefficients,
1202 for(std::set<std::pair<expr_listt, exprt> >::iterator it=coefficients.begin();
1203 it!=coefficients.end();
1208 exprt coefficient=it->second;
1210 std::map<exprt, int> degrees;
1213 monomial.
coeff = numeric_cast_v<int>(mp);
1215 if(monomial.
coeff==0)
1220 for(
const auto &term : terms)
1222 if(degrees.find(term)!=degrees.end())
1232 for(std::map<exprt, int>::iterator it=degrees.begin();
1238 term.
exp=it->second;
1239 monomial.
terms.push_back(term);
1242 polynomial.
monomials.push_back(monomial);
1248 static int num_symbols=0;
1252 ret.module=
"scratch";
1254 ret.pretty_name=name;
std::unordered_map< exprt, exprt, irep_hash > expr_mapt
std::list< exprt > expr_listt
static exprt guard(const exprt::operandst &guards, exprt cond)
symbol_table_baset & symbol_table
void push_nondet(exprt &expr)
bool array_assignments2polys(expr_pairst &array_assignments, std::map< exprt, polynomialt > &polynomials, polynomial_array_assignmentst &array_polynomials, polynomialst &nondet_indices)
void find_modified(const patht &path, expr_sett &modified)
std::vector< polynomial_array_assignmentt > polynomial_array_assignmentst
bool do_nonrecursive(goto_programt::instructionst &loop_body, std::map< exprt, polynomialt > &polynomials, substitutiont &substitution, expr_sett &nonrecursive, scratch_programt &program)
bool do_arrays(goto_programt::instructionst &loop_body, std::map< exprt, polynomialt > &polynomials, substitutiont &substitution, scratch_programt &program)
void stash_polynomials(scratch_programt &program, std::map< exprt, polynomialt > &polynomials, std::map< exprt, exprt > &stashed, patht &path)
void extract_polynomial(scratch_programt &program, std::set< std::pair< expr_listt, exprt >> &coefficients, polynomialt &polynomial)
bool expr2poly(exprt &expr, std::map< exprt, polynomialt > &polynomials, polynomialt &poly)
message_handlert & message_handler
bool check_inductive(std::map< exprt, polynomialt > polynomials, patht &path, guard_managert &guard_manager)
bool assign_array(const index_exprt &lhs, const exprt &rhs, scratch_programt &program)
void gather_rvalues(const exprt &expr, expr_sett &rvalues)
symbolt fresh_symbol(std::string base, typet type)
void abstract_arrays(exprt &expr, expr_mapt &abstractions)
std::vector< expr_pairt > expr_pairst
bool do_assumptions(std::map< exprt, polynomialt > polynomials, patht &body, exprt &guard, guard_managert &guard_manager)
void ensure_no_overflows(scratch_programt &program)
exprt precondition(patht &path)
void gather_array_accesses(const exprt &expr, expr_sett &arrays)
void stash_variables(scratch_programt &program, expr_sett modified, substitutiont &substitution)
expr_pairst gather_array_assignments(goto_programt::instructionst &loop_body, expr_sett &arrays_written)
A base class for relations, i.e., binary predicates whose two operands have the same type.
A goto_instruction_codet representing an assignment in the program.
A constant literal expression.
const irep_idt & get_value() const
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
const char * c_str() const
Base class for all expressions.
std::vector< exprt > operandst
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
const source_locationt & source_location() const
typet & type()
Return the type of the expression.
The Boolean constant false.
A generic container class for the GOTO intermediate representation of one function.
static instructiont make_assumption(const exprt &g, const source_locationt &l=source_locationt::nil())
std::ostream & output(std::ostream &out) const
Output goto-program to given stream.
instructionst::const_iterator const_targett
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
std::list< instructiont > instructionst
targett add(instructiont &&instruction)
Adds a given instruction at the end.
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
const irep_idt & id() const
Modulo defined as lhs-(rhs * truncate(lhs/rhs)).
std::vector< termt > terms
parentt::loopt natural_loopt
void set_option(const std::string &option, const bool value)
void add_overflow_checks()
The plus expression Associativity is not specified.
void substitute(substitutiont &substitution)
void from_expr(const exprt &expr)
std::vector< monomialt > monomials
int coeff(const exprt &expr)
void add(polynomialt &other)
int max_degree(const exprt &var)
bool check_sat(bool do_slice, guard_managert &guard_manager)
targett assume(const exprt &guard)
targett assign(const exprt &lhs, const exprt &rhs)
exprt eval(const exprt &e)
void append_path(patht &path)
A side_effect_exprt that returns a non-deterministically chosen value.
Expression to hold a symbol (variable)
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
The type of an expression, extends irept.
std::unordered_set< exprt, irep_hash > expr_sett
std::string expr2c(const exprt &expr, const namespacet &ns, const expr2c_configurationt &configuration)
#define Forall_operands(it, expr)
static bool find_symbols(symbol_kindt, const typet &, std::function< bool(const symbol_exprt &)>, std::unordered_set< irep_idt > &bindings, const std::vector< irep_idt > &subs_to_find)
Find identifiers with id ID_symbol of the sub expressions and the subs with ID in subs_to_find consid...
#define forall_goto_program_instructions(it, program)
const mp_integer binary2integer(const std::string &n, bool is_signed)
convert binary string representation to mp_integer
std::list< path_nodet > patht
const pointer_offset_exprt & to_pointer_offset_expr(const exprt &expr)
Cast an exprt to a pointer_offset_exprt.
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
std::vector< polynomialt > polynomialst
std::map< exprt, exprt > substitutiont
bool replace_expr(const exprt &what, const exprt &by, exprt &dest)
std::unordered_map< exprt, exprt, irep_hash > replace_mapt
bool simplify(exprt &expr, const namespacet &ns)
exprt simplify_expr(exprt src, const namespacet &ns)
#define UNREACHABLE
This should be used to mark dead code.
exprt conjunction(const exprt::operandst &op)
1) generates a conjunction for two or more operands 2) for one operand, returns the operand 3) return...
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
const not_exprt & to_not_expr(const exprt &expr)
Cast an exprt to an not_exprt.
const binary_relation_exprt & to_binary_relation_expr(const exprt &expr)
Cast an exprt to a binary_relation_exprt.
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
This is unused by this implementation of guards, but can be used by other implementations of the same...
time_t time(time_t *tloc)
ssize_t write(int fildes, const void *buf, size_t nbyte)
unsignedbv_typet unsigned_poly_type()
signedbv_typet signed_poly_type()