Skip to content

Value-set dereference: use cond_exprt to avoid quadratic guards #4555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

67 changes: 46 additions & 21 deletions src/pointer-analysis/value_set_dereference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ exprt value_set_dereferencet::dereference(const exprt &pointer)
may_fail=true;
}

exprt failure_value;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it too non-trivial to check that failure_value is never used uninitialised. See comment below.


if(may_fail)
{
// first see if we have a "failed object" for this pointer

exprt failure_value;

if(
const symbolt *failed_symbol =
dereference_callback.get_or_create_failed_symbol(pointer))
Expand All @@ -138,36 +138,61 @@ exprt value_set_dereferencet::dereference(const exprt &pointer)
failure_value=symbol.symbol_expr();
failure_value.set(ID_C_invalid_object, true);
}

valuet value;
value.value=failure_value;
value.pointer_guard=true_exprt();
values.push_front(value);
}

// now build big case split, but we only do "good" objects

exprt value=nil_exprt();

for(std::list<valuet>::const_iterator
it=values.begin();
it!=values.end();
it++)
optionalt<exprt> value_without_condition;
cond_exprt cond({}, type, true);
for(const auto &alias_value : values)
{
if(it->value.is_not_nil())
if(alias_value.value.is_not_nil())
{
if(value.is_nil()) // first?
value=it->value;
if(alias_value.pointer_guard.is_false())
{
INVARIANT(
!value_without_condition.has_value(),
"can't discriminate between two different catch-all aliases");
value_without_condition = alias_value.value;
}
else
value=if_exprt(it->pointer_guard, it->value, value);
{
cond.add_case(alias_value.pointer_guard, alias_value.value);
INVARIANT(
alias_value.value.type() == type,
"deref value types should match the pointer being derefd");
}
}
}

#if 0
std::cout << "R: " << format(value) << "\n\n";
#endif
// I'd like to put an invariant here that values without a pointer guard, such
// as integer_address, cannot co-occur with failed objects, but this isn't the
// case. There's no way to write a GOTO condition to discriminate between the
// two however, so purely by historical accident, the failed object takes
// precedence:

if(may_fail || value_without_condition.has_value())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just combine value_without_condition and failure_value into a single expression?

{
// The cases must be disjoint, so add
// "!(p == &o1 || p == &o2 || p == &o3 || ...) => failure-value"
exprt::operandst other_case_conditions;
for(std::size_t i = 0; i < cond.get_n_cases(); ++i)
other_case_conditions.push_back(cond.condition(i));
cond.add_case(
not_exprt(disjunction(other_case_conditions)),
may_fail ? failure_value : *value_without_condition);
}

return value;
#if 0
std::cout << "R: " << format(cond) << "\n\n";
#endif

if(cond.get_n_cases() == 0)
return nil_exprt();
else if(cond.get_n_cases() == 1)
return cond.value(0);
else
return std::move(cond);
}

/// Check if the two types have matching number of ID_pointer levels, with
Expand Down
1 change: 1 addition & 0 deletions src/util/irep_ids.def
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ IREP_ID_ONE(to_member)
IREP_ID_ONE(pointer_to_member)
IREP_ID_ONE(tuple)
IREP_ID_ONE(function_body)
IREP_ID_ONE(exclusive)

// Projects depending on this code base that wish to extend the list of
// available ids should provide a file local_irep_ids.def in their source tree
Expand Down
36 changes: 35 additions & 1 deletion src/util/std_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -4394,6 +4394,8 @@ inline popcount_exprt &to_popcount_expr(exprt &expr)
/// this is a parametric version of an if-expression: it returns
/// the value of the first case (using the ordering of the operands)
/// whose condition evaluates to true.
/// It can be exclusive, in which case the creator asserts that the conditions
/// are mutually exclusive (i.e. they may be processed in any order).
class cond_exprt : public multi_ary_exprt
{
public:
Expand All @@ -4402,9 +4404,11 @@ class cond_exprt : public multi_ary_exprt
{
}

cond_exprt(operandst _operands, typet _type)
cond_exprt(operandst _operands, typet _type, bool exclusive = false)
: multi_ary_exprt(ID_cond, std::move(_operands), std::move(_type))
{
if(exclusive)
set(ID_exclusive, true);
}

/// adds a case to a cond expression
Expand All @@ -4417,6 +4421,36 @@ class cond_exprt : public multi_ary_exprt
operands().push_back(condition);
operands().push_back(value);
}

std::size_t get_n_cases() const
{
return operands().size() / 2;
}

const exprt &condition(std::size_t case_index) const
{
return operands().at(case_index * 2);
}

const exprt &value(std::size_t case_index) const
{
return operands().at((case_index * 2) + 1);
}

exprt &condition(std::size_t case_index)
{
return operands().at(case_index * 2);
}

exprt &value(std::size_t case_index)
{
return operands().at((case_index * 2) + 1);
}

bool is_exclusive() const
{
return get_bool(ID_exclusive);
}
};

template <>
Expand Down