cprover
Loading...
Searching...
No Matches
interval_abstract_value.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3 Module: analyses variable-sensitivity intervals
4
5 Author: Diffblue Ltd.
6
7\*******************************************************************/
8
9#include <ostream>
10
11#include <util/arith_tools.h>
13#include <util/expr_util.h>
14#include <util/invariant.h>
15#include <util/make_unique.h>
16#include <util/simplify_expr.h>
17
21#include "widened_range.h"
22
24 const constant_interval_exprt &interval,
25 const namespacet &n);
26
28{
29public:
31 const constant_interval_exprt &interval_,
32 const namespacet &n)
33 : interval(interval_),
35 next(interval.get_lower()),
36 upper(interval.get_upper()),
37 ns(n)
38 {
39 }
40
41 const exprt &current() const override
42 {
43 return index;
44 }
45 bool advance_to_next() override
46 {
47 index = next;
50 .is_true();
51 }
52
57
58private:
59 static exprt next_element(const exprt &cur, const namespacet &ns)
60 {
61 return simplify_expr(plus_exprt(cur, from_integer(1, cur.type())), ns);
62 }
63
68 const namespacet &ns;
69};
70
77
78static inline bool
79bvint_value_is_max(const typet &type, const mp_integer &value)
80{
81 PRECONDITION(type.id() == ID_signedbv || type.id() == ID_unsignedbv);
82 if(type.id() == ID_signedbv)
83 {
84 return to_signedbv_type(type).largest() == value;
85 }
86 else
87 {
88 return to_unsignedbv_type(type).largest() == value;
89 }
90}
91
92static inline bool
93bvint_value_is_min(const typet &type, const mp_integer &value)
94{
95 PRECONDITION(type.id() == ID_signedbv || type.id() == ID_unsignedbv);
96 if(type.id() == ID_signedbv)
97 {
98 return to_signedbv_type(type).smallest() == value;
99 }
100 else
101 {
102 return to_unsignedbv_type(type).smallest() == value;
103 }
104}
105
106static inline constant_interval_exprt
108{
109 return constant_interval_exprt(min_value_exprt(value.type()), value);
110}
111
112static inline constant_interval_exprt
114{
115 return constant_interval_exprt(value, max_value_exprt(value.type()));
116}
117
118static inline mp_integer force_value_from_expr(const exprt &value)
119{
121 optionalt<mp_integer> maybe_integer_value = numeric_cast<mp_integer>(value);
122 INVARIANT(maybe_integer_value.has_value(), "Input has to have a value");
123 return maybe_integer_value.value();
124}
125
126static inline constant_interval_exprt
128{
129 mp_integer integer_value = force_value_from_expr(value);
130 if(!bvint_value_is_min(value.type(), integer_value))
132 min_value_exprt(value.type()),
133 from_integer(integer_value - 1, value.type()));
134 else
136}
137
138static inline constant_interval_exprt
140{
141 mp_integer integer_value = force_value_from_expr(value);
142 if(!bvint_value_is_max(value.type(), integer_value))
144 from_integer(integer_value + 1, value.type()),
145 max_value_exprt(value.type()));
146 else
148}
149
150static inline bool represents_interval(const exprt &expr)
151{
152 const exprt &e = skip_typecast(expr);
153 return (e.id() == ID_constant_interval || e.is_constant());
154}
155
157{
158 const exprt &e = skip_typecast(expr);
159
160 if(e.id() == ID_constant_interval)
161 {
163 }
164 else if(e.is_constant())
165 {
166 return constant_interval_exprt(e, e);
167 }
168 else
169 {
170 // not directly representable, so just return TOP
171 return constant_interval_exprt(e.type());
172 }
173}
174
175static inline irep_idt invert_relation(const irep_idt &relation)
176{
178 relation == ID_le || relation == ID_lt || relation == ID_ge ||
179 relation == ID_gt || relation == ID_equal);
180 if(relation == ID_le)
181 return ID_ge;
182 if(relation == ID_ge)
183 return ID_le;
184 if(relation == ID_lt)
185 return ID_gt;
186 if(relation == ID_gt)
187 return ID_lt;
188 return relation;
189}
190
197{
198 PRECONDITION(e.operands().size() == 2);
199 const auto &relation = e.id();
200 const auto &binary_e = to_binary_expr(e);
201 const auto &lhs = binary_e.lhs();
202 const auto &rhs = binary_e.rhs();
204 relation == ID_le || relation == ID_lt || relation == ID_ge ||
205 relation == ID_gt || relation == ID_equal);
206 PRECONDITION(lhs.is_constant() || lhs.id() == ID_symbol);
207 PRECONDITION(rhs.is_constant() || rhs.id() == ID_symbol);
208 PRECONDITION(lhs.id() != rhs.id());
209
210 const auto the_constant_part_of_the_relation =
211 (rhs.id() == ID_symbol ? lhs : rhs);
212 const auto maybe_inverted_relation =
213 (rhs.id() == ID_symbol ? invert_relation(relation) : relation);
214
215 if(maybe_inverted_relation == ID_le)
216 return interval_from_x_le_value(the_constant_part_of_the_relation);
217 if(maybe_inverted_relation == ID_lt)
218 return interval_from_x_lt_value(the_constant_part_of_the_relation);
219 if(maybe_inverted_relation == ID_ge)
220 return interval_from_x_ge_value(the_constant_part_of_the_relation);
221 if(maybe_inverted_relation == ID_gt)
222 return interval_from_x_gt_value(the_constant_part_of_the_relation);
223 INVARIANT(
224 maybe_inverted_relation == ID_equal, "We excluded other cases above");
226 the_constant_part_of_the_relation, the_constant_part_of_the_relation);
227}
228
230 const typet &t,
231 bool tp,
232 bool bttm)
233 : abstract_value_objectt(t, tp, bttm), interval(t)
234{
235}
236
238{
239 if(e.is_top())
240 return true;
241
242 if(e.get_lower().is_false() && e.get_upper().is_true())
243 return true;
244 if(
245 e.type().id() == ID_c_bool && e.get_lower().is_zero() &&
246 e.get_upper().is_one())
247 return true;
248
249 return false;
250}
251
254 : abstract_value_objectt(e.type(), new_interval_is_top(e), e.is_bottom()),
255 interval(e)
256{
257}
258
265
267 const exprt &e,
268 const abstract_environmentt &environment,
269 const namespacet &ns)
273 : (e.operands().size() == 2 ? interval_from_relation(e)
274 : constant_interval_exprt(e.type())))
275{
276}
277
279{
280 // Attempt to reduce this interval to a constant expression
282 {
283 // Interval is the equivalent of a constant, so reduce it to a constant
285 }
287#if 0
288 if(!is_top() && !is_bottom())
289 {
290 return this->interval;
291 }
292 else
293 {
295 }
296#endif
297}
298
303
305{
306 return std::hash<std::string>{}(interval.pretty());
307}
308
310 const abstract_object_pointert &other) const
311{
312 auto cast_other =
313 std::dynamic_pointer_cast<const interval_abstract_valuet>(other);
314 return cast_other && interval == cast_other->interval;
315}
316
318 std::ostream &out,
319 const ai_baset &ai,
320 const namespacet &ns) const
321{
322 if(!is_top() && !is_bottom())
323 {
324 std::string lower_string;
325 std::string upper_string;
326
327 if(interval.get_lower().id() == ID_min_value)
328 {
329 lower_string = "-INF";
330 }
331 else
332 {
333 INVARIANT(
334 interval.get_lower().is_constant(), "We only support constant limits");
335 lower_string =
337 }
338
339 if(interval.get_upper().id() == ID_max_value)
340 {
341 upper_string = "+INF";
342 }
343 else
344 {
345 INVARIANT(
346 interval.get_upper().is_constant(), "We only support constant limits");
347 upper_string =
349 }
350
351 out << "[" << lower_string << ", " << upper_string << "]";
352 }
353 else
354 {
355 abstract_objectt::output(out, ai, ns);
356 }
357}
358
360 const constant_interval_exprt &lhs,
361 const constant_interval_exprt &rhs)
362{
363 auto widened = widened_ranget(lhs, rhs);
364
365 // new interval ...
366 auto new_interval = constant_interval_exprt(
367 widened.widened_lower_bound, widened.widened_upper_bound);
368 return interval_abstract_valuet::make_interval(new_interval);
369}
370
372 const abstract_value_pointert &other,
373 const widen_modet &widen_mode) const
374{
375 if(other->is_bottom())
376 return shared_from_this();
377
378 auto other_interval = other->to_interval();
379
380 if(is_bottom())
381 return make_interval(other_interval);
382
383 if(interval.contains(other_interval))
384 return shared_from_this();
385
386 if(widen_mode == widen_modet::could_widen)
387 return widening_merge(interval, other_interval);
388
389 auto lower_bound = constant_interval_exprt::get_min(
390 interval.get_lower(), other_interval.get_lower());
391 auto upper_bound = constant_interval_exprt::get_max(
392 interval.get_upper(), other_interval.get_upper());
393
394 return make_interval(lower_bound, upper_bound);
395}
396
398 const abstract_value_pointert &other) const
399{
400 auto other_interval = other->to_interval();
401
402 if(other_interval.contains(interval))
403 return shared_from_this();
404
405 if(interval.contains(other_interval))
406 return make_interval(other_interval);
407
408 auto lower_bound = constant_interval_exprt::get_max(
409 interval.get_lower(), other_interval.get_lower());
410 auto upper_bound = constant_interval_exprt::get_min(
411 interval.get_upper(), other_interval.get_upper());
412
413 // if the interval is valid, we have a meet!
414 if(constant_interval_exprt::less_than_or_equal(lower_bound, upper_bound))
415 return make_interval(constant_interval_exprt(lower_bound, upper_bound));
416
417 return make_interval(interval.type(), false, true);
418}
419
422{
423 if(is_top() || is_bottom() || interval.is_top() || interval.is_bottom())
424 return make_empty_index_range();
425 if(
426 interval.get_lower().id() == ID_min_value ||
427 interval.get_upper().id() == ID_max_value)
428 {
429 return make_empty_index_range();
430 }
431
433}
434
440
442 const exprt &lower,
443 const exprt &upper) const
444{
445 auto lower_bound =
447 auto upper_bound =
449
450 if(constant_interval_exprt::greater_than(lower_bound, upper_bound))
451 return as_value(mutable_clone());
452
453 auto constrained_interval = constant_interval_exprt(lower_bound, upper_bound);
454 return as_value(
455 make_interval(constant_interval_exprt(lower_bound, upper_bound)));
456}
457
459{
461 return equal_exprt(name, interval.get_lower());
462
463 auto lower_bound = binary_relation_exprt(interval.get_lower(), ID_le, name);
464 auto upper_bound = binary_relation_exprt(name, ID_le, interval.get_upper());
465
466 return and_exprt(lower_bound, upper_bound);
467}
468
470 abstract_object_statisticst &statistics,
472 const abstract_environmentt &env,
473 const namespacet &ns) const
474{
475 abstract_objectt::get_statistics(statistics, visited, env, ns);
478 {
480 }
481 statistics.objects_memory_usage += memory_sizet::from_bytes(sizeof(*this));
482}
An abstract version of a program environment.
std::set< abstract_object_pointert > abstract_object_visitedt
sharing_ptrt< class abstract_objectt > abstract_object_pointert
Statistics gathering for the variable senstivity domain.
index_range_implementation_ptrt make_empty_index_range()
value_range_implementation_ptrt make_single_value_range(const abstract_object_pointert &value)
std::unique_ptr< index_range_implementationt > index_range_implementation_ptrt
std::unique_ptr< value_range_implementationt > value_range_implementation_ptrt
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
optionalt< Target > numeric_cast(const exprt &arg)
Converts an expression to any integral type.
Pre-defined bitvector types.
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
virtual exprt to_constant() const
Converts to a constant expression if possible.
virtual bool is_top() const
Find out if the abstract object is top.
virtual bool is_bottom() const
Find out if the abstract object is bottom.
virtual internal_abstract_object_pointert mutable_clone() const
virtual void output(std::ostream &out, const class ai_baset &ai, const namespacet &ns) const
Print the value of the abstract object.
virtual void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns) const
virtual const typet & type() const
Get the real type of the variable this abstract object is representing.
sharing_ptrt< const abstract_value_objectt > as_value(const abstract_object_pointert &obj) const
sharing_ptrt< const abstract_value_objectt > abstract_value_pointert
This is the basic interface of the abstract interpreter with default implementations of the core func...
Definition ai.h:118
Boolean AND.
Definition std_expr.h:2071
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly two argu...
Definition std_expr.h:676
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition std_expr.h:707
const irep_idt & get_value() const
Definition std_expr.h:2950
Represents an interval of values.
Definition interval.h:52
static bool is_bottom(const constant_interval_exprt &a)
constant_interval_exprt bottom() const
tvt greater_than(const constant_interval_exprt &o) const
Definition interval.cpp:397
tvt less_than_or_equal(const constant_interval_exprt &o) const
Definition interval.cpp:403
static bool is_top(const constant_interval_exprt &a)
bool is_single_value_interval() const
bool contains(const constant_interval_exprt &interval) const
static exprt get_max(const exprt &a, const exprt &b)
Definition interval.cpp:960
static exprt get_min(const exprt &a, const exprt &b)
Definition interval.cpp:965
const exprt & get_lower() const
Definition interval.cpp:27
const exprt & get_upper() const
Definition interval.cpp:32
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:39
Equality.
Definition std_expr.h:1306
Base class for all expressions.
Definition expr.h:56
bool is_one() const
Return whether the expression is a constant representing 1.
Definition expr.cpp:96
bool is_true() const
Return whether the expression is a constant representing true.
Definition expr.cpp:27
bool is_false() const
Return whether the expression is a constant representing false.
Definition expr.cpp:34
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition expr.cpp:47
bool is_constant() const
Return whether the expression is a constant.
Definition expr.h:204
typet & type()
Return the type of the expression.
Definition expr.h:84
operandst & operands()
Definition expr.h:94
mp_integer largest() const
Return the largest value that can be represented using this type.
mp_integer smallest() const
Return the smallest value that can be represented using this type.
CLONE abstract_object_pointert merge_with_value(const abstract_value_pointert &other, const widen_modet &widen_mode) const override
Merge another interval abstract object with this one.
static std::shared_ptr< interval_abstract_valuet > make_interval(Args &&... args)
interval_abstract_valuet(const typet &t, bool tp, bool bttm)
bool internal_equality(const abstract_object_pointert &other) const override
void output(std::ostream &out, const class ai_baset &ai, const class namespacet &ns) const override
void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns) const override
index_range_implementation_ptrt index_range_implementation(const namespacet &ns) const override
exprt to_constant() const override
Converts to a constant expression if possible.
abstract_value_pointert constrain(const exprt &lower, const exprt &upper) const override
exprt to_predicate_internal(const exprt &name) const override
to_predicate implementation - derived classes will override
abstract_object_pointert meet_with_value(const abstract_value_pointert &other) const override
Meet another interval abstract object with this one.
value_range_implementation_ptrt value_range_implementation() const override
size_t internal_hash() const override
constant_interval_exprt interval
const exprt & current() const override
index_range_implementation_ptrt reset() const override
static exprt next_element(const exprt &cur, const namespacet &ns)
interval_index_ranget(const constant_interval_exprt &interval_, const namespacet &n)
const constant_interval_exprt & interval
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition irep.cpp:490
const irep_idt & id() const
Definition irep.h:396
+∞ upper bound for intervals
Definition interval.h:18
static memory_sizet from_bytes(std::size_t bytes)
-∞ upper bound for intervals
Definition interval.h:33
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
The NIL expression.
Definition std_expr.h:3026
The plus expression Associativity is not specified.
Definition std_expr.h:947
The type of an expression, extends irept.
Definition type.h:29
const exprt & skip_typecast(const exprt &expr)
find the expression nested inside typecasts, if any
Deprecated expression utility functions.
const constant_interval_exprt & to_constant_interval_expr(const exprt &expr)
Definition interval.h:462
bool new_interval_is_top(const constant_interval_exprt &e)
static constant_interval_exprt interval_from_relation(const exprt &e)
Builds an interval representing all values satisfying the input expression.
static bool represents_interval(const exprt &expr)
static bool bvint_value_is_min(const typet &type, const mp_integer &value)
static mp_integer force_value_from_expr(const exprt &value)
abstract_object_pointert widening_merge(const constant_interval_exprt &lhs, const constant_interval_exprt &rhs)
static index_range_implementation_ptrt make_interval_index_range(const constant_interval_exprt &interval, const namespacet &n)
static constant_interval_exprt interval_from_x_gt_value(const exprt &value)
static constant_interval_exprt make_interval_expr(const exprt &expr)
static bool bvint_value_is_max(const typet &type, const mp_integer &value)
static irep_idt invert_relation(const irep_idt &relation)
static constant_interval_exprt interval_from_x_ge_value(const exprt &value)
static constant_interval_exprt interval_from_x_lt_value(const exprt &value)
static constant_interval_exprt interval_from_x_le_value(const exprt &value)
An interval to represent a set of possible values.
const std::string & id2string(const irep_idt &d)
Definition irep.h:47
std::unique_ptr< T > util_make_unique(Ts &&... ts)
Definition make_unique.h:19
nonstd::optional< T > optionalt
Definition optional.h:35
exprt simplify_expr(exprt src, const namespacet &ns)
BigInt mp_integer
Definition smt_terms.h:18
#define PRECONDITION(CONDITION)
Definition invariant.h:463
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition invariant.h:423
const binary_exprt & to_binary_expr(const exprt &expr)
Cast an exprt to a binary_exprt.
Definition std_expr.h:660
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition std_expr.h:2992
memory_sizet objects_memory_usage
An underestimation of the memory usage of the abstract objects.