Skip to content

Commit 14f6721

Browse files
authored
Merge pull request #1629 from owen-jones-diffblue/owen-jones-diffblue/refactor-vsa-objectt
Replace objectt with optional<mp_integer>
2 parents cd86eb8 + c543892 commit 14f6721

8 files changed

+246
-325
lines changed

src/pointer-analysis/value_set.cpp

+26-28
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,23 @@ value_sett::entryt &value_sett::get_entry(
7575
bool value_sett::insert(
7676
object_mapt &dest,
7777
object_numberingt::number_type n,
78-
const objectt &object) const
78+
const offsett &offset) const
7979
{
8080
auto entry=dest.read().find(n);
8181

8282
if(entry==dest.read().end())
8383
{
8484
// new
85-
dest.write()[n]=object;
85+
dest.write()[n] = offset;
8686
return true;
8787
}
88-
else if(!entry->second.offset_is_set)
88+
else if(!entry->second)
8989
return false; // no change
90-
else if(object.offset_is_set &&
91-
entry->second.offset==object.offset)
90+
else if(offset && *entry->second == *offset)
9291
return false; // no change
9392
else
9493
{
95-
dest.write()[n].offset_is_set=false;
94+
dest.write()[n].reset();
9695
return true;
9796
}
9897
}
@@ -155,8 +154,8 @@ void value_sett::output(
155154
{
156155
result="<"+from_expr(ns, identifier, o)+", ";
157156

158-
if(o_it->second.offset_is_set)
159-
result+=integer2string(o_it->second.offset)+"";
157+
if(o_it->second)
158+
result += integer2string(*o_it->second) + "";
160159
else
161160
result+='*';
162161

@@ -199,8 +198,8 @@ exprt value_sett::to_expr(const object_map_dt::value_type &it) const
199198

200199
od.object()=object;
201200

202-
if(it.second.offset_is_set)
203-
od.offset()=from_integer(it.second.offset, index_type());
201+
if(it.second)
202+
od.offset() = from_integer(*it.second, index_type());
204203

205204
od.type()=od.object().type();
206205

@@ -282,7 +281,7 @@ bool value_sett::eval_pointer_offset(
282281
it=object_map.begin();
283282
it!=object_map.end();
284283
it++)
285-
if(!it->second.offset_is_set)
284+
if(!it->second)
286285
return false;
287286
else
288287
{
@@ -292,7 +291,7 @@ bool value_sett::eval_pointer_offset(
292291
if(ptr_offset<0)
293292
return false;
294293

295-
ptr_offset+=it->second.offset;
294+
ptr_offset += *it->second;
296295

297296
if(mod && ptr_offset!=previous_offset)
298297
return false;
@@ -635,15 +634,15 @@ void value_sett::get_value_set_rec(
635634
it!=pointer_expr_set.read().end();
636635
it++)
637636
{
638-
objectt object=it->second;
637+
offsett offset = it->second;
639638

640639
// adjust by offset
641-
if(object.offset_is_set && i_is_set)
642-
object.offset+=i;
640+
if(offset && i_is_set)
641+
*offset += i;
643642
else
644-
object.offset_is_set=false;
643+
offset.reset();
645644

646-
insert(dest, it->first, object);
645+
insert(dest, it->first, offset);
647646
}
648647
}
649648
else if(expr.id()==ID_mult)
@@ -668,12 +667,12 @@ void value_sett::get_value_set_rec(
668667
it!=pointer_expr_set.read().end();
669668
it++)
670669
{
671-
objectt object=it->second;
670+
offsett offset = it->second;
672671

673672
// kill any offset
674-
object.offset_is_set=false;
673+
offset.reset();
675674

676-
insert(dest, it->first, object);
675+
insert(dest, it->first, offset);
677676
}
678677
}
679678
else if(expr.id()==ID_side_effect)
@@ -1002,24 +1001,23 @@ void value_sett::get_reference_set_rec(
10021001
if(ns.follow(object.type())!=array_type)
10031002
index_expr.make_typecast(array.type());
10041003

1005-
objectt o=a_it->second;
1004+
offsett o = a_it->second;
10061005
mp_integer i;
10071006

10081007
if(offset.is_zero())
10091008
{
10101009
}
1011-
else if(!to_integer(offset, i) &&
1012-
o.offset_is_set)
1010+
else if(!to_integer(offset, i) && o)
10131011
{
10141012
mp_integer size=pointer_offset_size(array_type.subtype(), ns);
10151013

10161014
if(size<=0)
1017-
o.offset_is_set=false;
1015+
o.reset();
10181016
else
1019-
o.offset=i*size;
1017+
*o = i * size;
10201018
}
10211019
else
1022-
o.offset_is_set=false;
1020+
o.reset();
10231021

10241022
insert(dest, index_expr, o);
10251023
}
@@ -1052,7 +1050,7 @@ void value_sett::get_reference_set_rec(
10521050
insert(dest, exprt(ID_unknown, expr.type()));
10531051
else
10541052
{
1055-
objectt o=it->second;
1053+
offsett o = it->second;
10561054

10571055
member_exprt member_expr(object, component_name, expr.type());
10581056

@@ -1282,7 +1280,7 @@ void value_sett::do_free(
12821280
else
12831281
{
12841282
// adjust
1285-
objectt o=o_it->second;
1283+
offsett o = o_it->second;
12861284
exprt tmp(object);
12871285
to_dynamic_object_expr(tmp).valid()=exprt(ID_unknown);
12881286
insert(new_object_map, tmp, o);

src/pointer-analysis/value_set.h

+27-49
Original file line numberDiff line numberDiff line change
@@ -61,54 +61,23 @@ class value_sett
6161
typedef irep_idt idt;
6262

6363
/// Represents the offset into an object: either a unique integer offset,
64-
/// or an unknown value, represented by `!offset_is_set`.
65-
class objectt
64+
/// or an unknown value, represented by `!offset`.
65+
typedef optionalt<mp_integer> offsett;
66+
bool offset_is_zero(const offsett &offset) const
6667
{
67-
public:
68-
/// Constructs an unknown offset
69-
objectt():offset_is_set(false)
70-
{
71-
}
72-
73-
/// Constructs a known offset
74-
explicit objectt(const mp_integer &_offset):
75-
offset(_offset),
76-
offset_is_set(true)
77-
{
78-
}
79-
80-
/// Offset into the target object. Ignored if `offset_is_set` is false.
81-
mp_integer offset;
82-
83-
/// If true, `offset` gives a unique integer offset; if false, represents
84-
/// an unknown offset.
85-
bool offset_is_set;
86-
87-
bool offset_is_zero() const
88-
{ return offset_is_set && offset.is_zero(); }
89-
90-
bool operator==(const objectt &other) const
91-
{
92-
return
93-
offset_is_set==other.offset_is_set &&
94-
(!offset_is_set || offset==other.offset);
95-
}
96-
bool operator!=(const objectt &other) const
97-
{
98-
return !(*this==other);
99-
}
100-
};
68+
return offset && offset->is_zero();
69+
}
10170

10271
/// Represents a set of expressions (`exprt` instances) with corresponding
103-
/// offsets (`objectt` instances). This is the RHS set of a single row of
72+
/// offsets (`offsett` instances). This is the RHS set of a single row of
10473
/// the enclosing `value_sett`, such as `{ null, dynamic_object1 }`.
105-
/// The set is represented as a map from numbered `exprt`s to `objectt`
74+
/// The set is represented as a map from numbered `exprt`s to `offsett`
10675
/// instead of a set of pairs to make lookup by `exprt` easier. All
10776
/// methods matching the interface of `std::map` forward those methods
10877
/// to the internal map.
10978
class object_map_dt
11079
{
111-
typedef std::map<object_numberingt::number_type, objectt> data_typet;
80+
typedef std::map<object_numberingt::number_type, offsett> data_typet;
11281
data_typet data;
11382

11483
public:
@@ -135,9 +104,18 @@ class value_sett
135104
void erase(key_type i) { data.erase(i); }
136105
void erase(const_iterator it) { data.erase(it); }
137106

138-
objectt &operator[](key_type i) { return data[i]; }
139-
objectt &at(key_type i) { return data.at(i); }
140-
const objectt &at(key_type i) const { return data.at(i); }
107+
offsett &operator[](key_type i)
108+
{
109+
return data[i];
110+
}
111+
offsett &at(key_type i)
112+
{
113+
return data.at(i);
114+
}
115+
const offsett &at(key_type i) const
116+
{
117+
return data.at(i);
118+
}
141119

142120
template <typename It>
143121
void insert(It b, It e) { data.insert(b, e); }
@@ -210,21 +188,21 @@ class value_sett
210188
/// \param src: expression to add
211189
bool insert(object_mapt &dest, const exprt &src) const
212190
{
213-
return insert(dest, object_numbering.number(src), objectt());
191+
return insert(dest, object_numbering.number(src), offsett());
214192
}
215193

216194
/// Adds an expression to an object map, with known offset. If the
217195
/// destination map has an existing entry for the same expression
218196
/// with a differing offset its offset is marked unknown.
219197
/// \param dest: object map to update
220198
/// \param src: expression to add
221-
/// \param offset: offset into `src`
199+
/// \param offset_value: offset into `src`
222200
bool insert(
223201
object_mapt &dest,
224202
const exprt &src,
225-
const mp_integer &offset) const
203+
const mp_integer &offset_value) const
226204
{
227-
return insert(dest, object_numbering.number(src), objectt(offset));
205+
return insert(dest, object_numbering.number(src), offsett(offset_value));
228206
}
229207

230208
/// Adds a numbered expression and offset to an object map. If the
@@ -237,17 +215,17 @@ class value_sett
237215
bool insert(
238216
object_mapt &dest,
239217
object_numberingt::number_type n,
240-
const objectt &object) const;
218+
const offsett &offset) const;
241219

242220
/// Adds an expression and offset to an object map. If the
243221
/// destination map has an existing entry for the same expression
244222
/// with a differing offset its offset is marked unknown.
245223
/// \param dest: object map to update
246224
/// \param expr: expression to add
247225
/// \param object: offset into `expr` (may be unknown).
248-
bool insert(object_mapt &dest, const exprt &expr, const objectt &object) const
226+
bool insert(object_mapt &dest, const exprt &expr, const offsett &offset) const
249227
{
250-
return insert(dest, object_numbering.number(expr), object);
228+
return insert(dest, object_numbering.number(expr), offset);
251229
}
252230

253231
/// Represents a row of a `value_sett`. For example, this might represent

0 commit comments

Comments
 (0)