ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
goto_loop_invariant.h
Go to the documentation of this file.
1#ifndef GOTO_PROGRAMS_GOTO_LOOP_INVARIANT_H_
2#define GOTO_PROGRAMS_GOTO_LOOP_INVARIANT_H_
3
6#include <irep2/irep2_guard.h>
8#include <irep2/irep2_expr.h>
9#include <set>
10#include <unordered_set>
11#include <vector>
12
20extern const char *const kSynthesisedInvariantProperty;
21
22namespace loop_invariant
23{
30} // namespace loop_invariant
31
32// Forward declaration: full definition is in frame_enforcer.h (included in
33// .cpp)
34class frame_enforcert;
35
40 goto_functionst &goto_functions,
41 contextt &context,
42 bool use_frame_rule);
43
58
60{
61public:
71 static constexpr size_t kMaxInvariantSearchBack = 10;
72
74 const irep_idt &_function_name,
75 goto_functionst &_goto_functions,
76 goto_functiont &_goto_function,
77 contextt &_context,
78 bool _use_frame_rule)
79 : goto_loopst(_function_name, _goto_functions, _goto_function),
80 context(_context),
81 use_frame_rule(_use_frame_rule)
82 {
83 if (function_loops.size())
85 }
86
87protected:
90
99 std::vector<expr2tc> active_loop_assigns;
103 static constexpr unsigned kMaxHavocPointeeBits = 1024;
104
105 void goto_loop_invariant();
106
108
109 // Extract loop invariants from LOOP_INVARIANT instructions
110 std::vector<expr2tc> extract_loop_invariants(const loopst &loop);
111
112 // Extract loop assigns targets from LOOP_INVARIANT instructions
113 std::vector<expr2tc> extract_loop_assigns(const loopst &loop);
114
116 static void collect_symbols(const expr2tc &expr, std::set<irep_idt> &symbols);
117
125 goto_programt::targett loop_head,
126 const loopst &loop,
127 const std::vector<expr2tc> &invariants,
128 goto_programt &side_effects_out);
129
130 // Insert ASSERT invariant before loop (prepends side_effects if non-empty)
132 goto_programt::targett &loop_head,
133 const std::vector<expr2tc> &invariants,
134 const goto_programt &side_effects);
135
136 static type2tc havoc_pointee_type(const expr2tc &ptr, const namespacet &ns);
137
140 bool pointees_havocable(const loopst &loop) const;
141
142 // Insert HAVOC and ASSUME at \p loop_head, which must be the loop head's own
143 // instruction, so that everything the guard evaluates runs after the havoc
144 // (inserts side_effects between the HAVOC block and the ASSUME).
145 // side_effects is non-const: if frame rule is active, old_snapshot assigns
146 // are patched in-place so insert_inductive_step_and_termination (called
147 // after this) sees the patched version too.
149 void havoc_pointees(
150 const loopst &loop,
151 const locationt &loc,
152 goto_programt &dest) const;
153
155 goto_programt::targett loop_head,
156 const loopst &loop,
157 const std::vector<expr2tc> &invariants,
158 const std::vector<expr2tc> &loop_assigns,
159 goto_programt &side_effects);
160
161 // Insert inductive step verification and loop termination
162 // (prepends side_effects before the ASSERT)
164 const loopst &loop,
165 const std::vector<expr2tc> &invariants,
166 const goto_programt &side_effects);
167};
168
169// ---------------------------------------------------------------------------
170// Combined mode: Branch 1 invariant verification pass
171// ---------------------------------------------------------------------------
172
196{
197public:
199 const irep_idt &_function_name,
200 goto_functionst &_goto_functions,
201 goto_functiont &_goto_function)
202 : goto_loopst(_function_name, _goto_functions, _goto_function)
203 {
204 if (!function_loops.empty())
205 process_loops_combined();
206 }
207
208private:
209 void process_loops_combined();
210
212 void insert_invariant_verification_branch(loopst &loop);
213
222 void copy_loop_body(
223 goto_programt::targett body_begin,
224 goto_programt::targett loop_exit,
225 goto_programt &out) const;
226};
227
228#endif /* GOTO_PROGRAMS_GOTO_LOOP_INVARIANT_H_ */
Definition context.h:57
Definition frame_enforcer.h:39
Definition goto_functions.h:56
Definition goto_functions.h:23
goto_loop_invariant_combinedt(const irep_idt &_function_name, goto_functionst &_goto_functions, goto_functiont &_goto_function)
Definition goto_loop_invariant.h:198
std::vector< expr2tc > extract_loop_assigns(const loopst &loop)
Definition goto_loop_invariant.cpp:303
void insert_assert_before_loop(goto_programt::targett &loop_head, const std::vector< expr2tc > &invariants, const goto_programt &side_effects)
Definition goto_loop_invariant.cpp:609
void extract_and_remove_side_effects(goto_programt::targett loop_head, const loopst &loop, const std::vector< expr2tc > &invariants, goto_programt &side_effects_out)
Collect DECL/FUNCTION_CALL instructions immediately before LOOP_INVARIANT that define temporaries use...
Definition goto_loop_invariant.cpp:599
contextt & context
Definition goto_loop_invariant.h:88
void convert_loop_with_invariant(loopst &loop)
Definition goto_loop_invariant.cpp:229
static void collect_symbols(const expr2tc &expr, std::set< irep_idt > &symbols)
Collect all symbol names reachable from an expression tree.
Definition goto_loop_invariant.cpp:336
std::vector< expr2tc > extract_loop_invariants(const loopst &loop)
Definition goto_loop_invariant.cpp:296
static type2tc havoc_pointee_type(const expr2tc &ptr, const namespacet &ns)
The type of *ptr when the havoc can cover it, nil otherwise: a pointee with no static width (a VLA,...
Definition goto_loop_invariant.cpp:638
goto_loop_invariantt(const irep_idt &_function_name, goto_functionst &_goto_functions, goto_functiont &_goto_function, contextt &_context, bool _use_frame_rule)
Definition goto_loop_invariant.h:73
void insert_havoc_and_assume_before_condition(goto_programt::targett loop_head, const loopst &loop, const std::vector< expr2tc > &invariants, const std::vector< expr2tc > &loop_assigns, goto_programt &side_effects)
Definition goto_loop_invariant.cpp:692
bool use_frame_rule
Definition goto_loop_invariant.h:89
void insert_inductive_step_and_termination(const loopst &loop, const std::vector< expr2tc > &invariants, const goto_programt &side_effects)
Definition goto_loop_invariant.cpp:856
frame_enforcert * active_frame_enforcer
Frame enforcer shared between the havoc step (ASSUME) and the inductive step (ASSERT compliance check...
Definition goto_loop_invariant.h:95
static constexpr size_t kMaxInvariantSearchBack
Maximum number of instructions to search backwards from the loop head when locating the LOOP_INVARIAN...
Definition goto_loop_invariant.h:71
void goto_loop_invariant()
Definition goto_loop_invariant.cpp:217
void havoc_pointees(const loopst &loop, const locationt &loc, goto_programt &dest) const
Havoc the objects the loop writes through a pointer.
Definition goto_loop_invariant.cpp:675
bool pointees_havocable(const loopst &loop) const
False when a pointee the loop writes is out of the havoc's reach, which leaves the loop to the unwind...
Definition goto_loop_invariant.cpp:661
std::vector< expr2tc > active_loop_assigns
Assigns targets for the current loop (mirrors loop_assigns passed to insert_havoc_and_assume_before_c...
Definition goto_loop_invariant.h:99
static constexpr unsigned kMaxHavocPointeeBits
Largest pointee, in bits, the havoc will cover.
Definition goto_loop_invariant.h:103
goto_loopst(const irep_idt &_function_name, goto_functionst &_goto_functions, goto_functiont &_goto_function)
Definition goto_loops.h:121
function_loopst function_loops
Definition goto_loops.h:23
A generic container class for a control flow graph for one function, in the form of a goto-program.
Definition goto_program.h:63
instructionst::iterator targett
Definition goto_program.h:590
instructionst::const_iterator const_targett
Definition goto_program.h:591
Definition irep_idt.h:12
Definition location.h:7
Definition loopst.h:8
Definition namespace.h:10
const char *const kSynthesisedInvariantProperty
Property tag that goto_synthesise_loop_invariants stamps on the LOOP_INVARIANT markers it emits.
Definition goto_loop_invariant.cpp:49
void goto_loop_invariant(goto_functionst &goto_functions, contextt &context, bool use_frame_rule)
Entry point: process loop invariants for all functions.
Definition goto_loop_invariant.cpp:204
void goto_loop_invariant_combined(goto_functionst &goto_functions)
Combined loop-invariant + k-induction mode.
Definition goto_loop_invariant.cpp:927
irep_container< type2t > type2tc
Definition irep2.h:430
irep_container< expr2t > expr2tc
Definition irep2.h:431
Definition goto_loop_invariant.cpp:52
bool is_inert_scan_instruction(goto_programt::const_targett t)
Instructions with no effect a caller needs to account for: no write to a tracked variable,...
Definition goto_loop_invariant.cpp:53