CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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/simplify_expr.h>
16
20#include "widened_range.h"
21
24 const namespacet &n);
25
27{
28public:
31 const namespacet &n)
34 next(interval.get_lower()),
35 upper(interval.get_upper()),
36 ns(n)
37 {
38 }
39
40 const exprt &current() const override
41 {
42 return index;
43 }
44 bool advance_to_next() override
45 {
46 index = next;
49 .is_true();
50 }
51
56
57private:
58 static exprt next_element(const exprt &cur, const namespacet &ns)
59 {
60 return simplify_expr(plus_exprt(cur, from_integer(1, cur.type())), ns);
61 }
62
67 const namespacet &ns;
68};
69
72 const namespacet &n)
73{
74 return std::make_unique<interval_index_ranget>(interval, n);
75}
76
77static inline bool
78bvint_value_is_max(const typet &type, const mp_integer &value)
79{
80 PRECONDITION(type.id() == ID_signedbv || type.id() == ID_unsignedbv);
81 if(type.id() == ID_signedbv)
82 {
83 return to_signedbv_type(type).largest() == value;
84 }
85 else
86 {
87 return to_unsignedbv_type(type).largest() == value;
88 }
89}
90
91static inline bool
92bvint_value_is_min(const typet &type, const mp_integer &value)
93{
94 PRECONDITION(type.id() == ID_signedbv || type.id() == ID_unsignedbv);
95 if(type.id() == ID_signedbv)
96 {
97 return to_signedbv_type(type).smallest() == value;
98 }
99 else
100 {
101 return to_unsignedbv_type(type).smallest() == value;
102 }
103}
104
105static inline constant_interval_exprt
107{
108 return constant_interval_exprt(min_value_exprt(value.type()), value);
109}
110
111static inline constant_interval_exprt
113{
114 return constant_interval_exprt(value, max_value_exprt(value.type()));
115}
116
117static inline mp_integer force_value_from_expr(const exprt &value)
118{
120 std::optional<mp_integer> maybe_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{
132 min_value_exprt(value.type()),
133 from_integer(integer_value - 1, value.type()));
134 else
136}
137
138static inline constant_interval_exprt
140{
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
211 (rhs.id() == ID_symbol ? lhs : rhs);
212 const auto maybe_inverted_relation =
213 (rhs.id() == ID_symbol ? invert_relation(relation) : relation);
214
223 INVARIANT(
224 maybe_inverted_relation == ID_equal, "We excluded other cases above");
227}
228
230 const typet &t,
231 bool tp,
232 bool bttm)
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
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
281 if(!is_top() && !is_bottom() && interval.is_single_value_interval())
282 {
283 // Interval is the equivalent of a constant, so reduce it to a constant
284 return to_constant_expr(interval.get_lower());
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");
336 id2string(to_constant_expr(interval.get_lower()).get_value());
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");
348 id2string(to_constant_expr(interval.get_upper()).get_value());
349 }
350
351 out << "[" << lower_string << ", " << upper_string << "]";
352 }
353 else
354 {
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 ...
367 widened.widened_lower_bound, widened.widened_upper_bound);
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())
382
383 if(interval.contains(other_interval))
384 return shared_from_this();
385
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))
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 =
446 constant_interval_exprt::get_max(lower, interval.get_lower());
447 auto upper_bound =
448 constant_interval_exprt::get_min(upper, interval.get_upper());
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{
460 if(interval.is_single_value_interval())
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,
473 const namespacet &ns) const
474{
475 abstract_objectt::get_statistics(statistics, visited, env, ns);
477 if(interval.is_single_value_interval())
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)
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 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:117
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
Boolean AND.
Definition std_expr.h:2125
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly two argu...
Definition std_expr.h:731
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition std_expr.h:762
Represents an interval of values.
Definition interval.h:52
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)
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:38
Equality.
Definition std_expr.h:1366
Base class for all expressions.
Definition expr.h:56
bool is_true() const
Return whether the expression is a constant representing true.
Definition expr.cpp:27
bool is_constant() const
Return whether the expression is a constant.
Definition expr.h:212
typet & type()
Return the type of the expression.
Definition expr.h:84
operandst & operands()
Definition expr.h:94
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)
internal_abstract_object_pointert mutable_clone() const override
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
const irep_idt & id() const
Definition irep.h:388
+∞ 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:3208
The plus expression Associativity is not specified.
Definition std_expr.h:1002
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:44
exprt simplify_expr(exprt src, const namespacet &ns)
BigInt mp_integer
Definition smt_terms.h:17
#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:715
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition std_expr.h:3172
memory_sizet objects_memory_usage
An underestimation of the memory usage of the abstract objects.