Skip to content

Commit ddde9dc

Browse files
committed
Introduce popcount_exprt
ID_popcount was previously used without a dedicated class.
1 parent 3e793f5 commit ddde9dc

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,8 +2355,7 @@ exprt c_typecheck_baset::do_special_functions(
23552355
throw 0;
23562356
}
23572357

2358-
exprt popcount_expr(ID_popcount, expr.type());
2359-
popcount_expr.operands()=expr.arguments();
2358+
popcount_exprt popcount_expr(expr.arguments().front(), expr.type());
23602359
popcount_expr.add_source_location()=source_location;
23612360

23622361
return popcount_expr;

src/util/simplify_expr.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,28 +136,28 @@ bool simplify_exprt::simplify_sign(exprt &expr)
136136
return true;
137137
}
138138

139-
bool simplify_exprt::simplify_popcount(exprt &expr)
139+
bool simplify_exprt::simplify_popcount(popcount_exprt &expr)
140140
{
141-
if(expr.operands().size()!=1)
142-
return true;
141+
const exprt &op = expr.op();
143142

144-
if(expr.op0().is_constant())
143+
if(op.is_constant())
145144
{
146-
const typet &type=ns.follow(expr.op0().type());
145+
const typet &type=ns.follow(op.type());
147146

148147
if(type.id()==ID_signedbv ||
149148
type.id()==ID_unsignedbv)
150149
{
151150
mp_integer value;
152-
if(!to_integer(expr.op0(), value))
151+
if(!to_integer(op, value))
153152
{
154153
std::size_t result;
155154

156155
for(result=0; value!=0; value=value>>1)
157156
if(value.is_odd())
158157
result++;
159158

160-
expr=from_integer(result, expr.type());
159+
exprt simp_result = from_integer(result, expr.type());
160+
expr.swap(simp_result);
161161

162162
return false;
163163
}
@@ -2334,8 +2334,8 @@ bool simplify_exprt::simplify_node(exprt &expr)
23342334
result=simplify_abs(expr) && result;
23352335
else if(expr.id()==ID_sign)
23362336
result=simplify_sign(expr) && result;
2337-
else if(expr.id()==ID_popcount)
2338-
result=simplify_popcount(expr) && result;
2337+
else if(expr.id() == ID_popcount)
2338+
result = simplify_popcount(to_popcount_expr(expr)) && result;
23392339

23402340
#ifdef DEBUGX
23412341
if(!result

src/util/simplify_expr_class.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class if_exprt;
2929
class index_exprt;
3030
class member_exprt;
3131
class namespacet;
32+
class popcount_exprt;
3233
class tvt;
3334

3435
#define forall_value_list(it, value_list) \
@@ -107,7 +108,7 @@ class simplify_exprt
107108
bool simplify_isnormal(exprt &expr);
108109
bool simplify_abs(exprt &expr);
109110
bool simplify_sign(exprt &expr);
110-
bool simplify_popcount(exprt &expr);
111+
bool simplify_popcount(popcount_exprt &expr);
111112

112113
// auxiliary
113114
bool simplify_if_implies(

src/util/std_expr.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4807,4 +4807,61 @@ class exists_exprt:public binary_exprt
48074807
}
48084808
};
48094809

4810+
/*! \brief The popcount (counting the number of bits set to 1) expression
4811+
*/
4812+
class popcount_exprt: public unary_exprt
4813+
{
4814+
public:
4815+
popcount_exprt(): unary_exprt(ID_popcount)
4816+
{
4817+
}
4818+
4819+
popcount_exprt(const exprt &_op, const typet &_type)
4820+
: unary_exprt(ID_popcount, _op, _type)
4821+
{
4822+
}
4823+
4824+
explicit popcount_exprt(const exprt &_op)
4825+
: unary_exprt(ID_popcount, _op, _op.type())
4826+
{
4827+
}
4828+
};
4829+
4830+
/*! \brief Cast a generic exprt to a \ref popcount_exprt
4831+
*
4832+
* This is an unchecked conversion. \a expr must be known to be \ref
4833+
* popcount_exprt.
4834+
*
4835+
* \param expr Source expression
4836+
* \return Object of type \ref popcount_exprt
4837+
*
4838+
* \ingroup gr_std_expr
4839+
*/
4840+
inline const popcount_exprt &to_popcount_expr(const exprt &expr)
4841+
{
4842+
PRECONDITION(expr.id() == ID_popcount);
4843+
DATA_INVARIANT(expr.operands().size() == 1, "popcount must have one operand");
4844+
return static_cast<const popcount_exprt &>(expr);
4845+
}
4846+
4847+
/*! \copydoc to_popcount_expr(const exprt &)
4848+
* \ingroup gr_std_expr
4849+
*/
4850+
inline popcount_exprt &to_popcount_expr(exprt &expr)
4851+
{
4852+
PRECONDITION(expr.id() == ID_popcount);
4853+
DATA_INVARIANT(expr.operands().size() == 1, "popcount must have one operand");
4854+
return static_cast<popcount_exprt &>(expr);
4855+
}
4856+
4857+
template <>
4858+
inline bool can_cast_expr<popcount_exprt>(const exprt &base)
4859+
{
4860+
return base.id() == ID_popcount;
4861+
}
4862+
inline void validate_expr(const popcount_exprt &value)
4863+
{
4864+
validate_operands(value, 1, "popcount must have one operand");
4865+
}
4866+
48104867
#endif // CPROVER_UTIL_STD_EXPR_H

0 commit comments

Comments
 (0)