Skip to content

Commit 3566ce6

Browse files
committed
New expression: count_leading_zeros_exprt
Rather than ad-hoc handling __builtin_clz and __lzcnt (and their variants) in the C front-end, make counting-leading-zeros available across the code base.
1 parent 631e7b0 commit 3566ce6

File tree

21 files changed

+264
-142
lines changed

21 files changed

+264
-142
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int main()
2+
{
3+
#ifdef __GNUC__
4+
_Static_assert(
5+
__builtin_clz(0xffU) == 8 * sizeof(unsigned) - 8,
6+
"GCC/Clang compile-time constant");
7+
#endif
8+
9+
return 0;
10+
}

regression/cbmc-library/__builtin_clz-02/main.c

Lines changed: 0 additions & 12 deletions
This file was deleted.

regression/cbmc-library/__builtin_clz-01/main.c renamed to regression/cbmc/__builtin_clz-01/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,9 @@ int main()
5656
__CPROVER_assume(u != 0);
5757
assert(nlz2a(u) == __builtin_clz(u));
5858

59+
#undef __builtin_clz
60+
// a failing assertion should be generated as __builtin_clz is undefined for 0
61+
__builtin_clz(0U);
62+
5963
return 0;
6064
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--bounds-check
4+
^\[main.bit_count.\d+\] line 61 count leading zeros argument in __builtin_clz\(0u\): FAILURE$
5+
^\*\* 1 of \d+ failed
6+
^VERIFICATION FAILED$
7+
^EXIT=10$
8+
^SIGNAL=0$
9+
--
10+
^warning: ignoring

scripts/expected_doxygen_warnings.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,20 @@
8282
warning: Include graph for 'goto_instrument_parse_options.cpp' not generated, too many nodes (97), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8383
warning: Included by graph for 'goto_functions.h' not generated, too many nodes (66), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8484
warning: Included by graph for 'goto_model.h' not generated, too many nodes (110), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
85-
warning: Included by graph for 'arith_tools.h' not generated, too many nodes (181), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
85+
warning: Included by graph for 'arith_tools.h' not generated, too many nodes (182), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8686
warning: Included by graph for 'c_types.h' not generated, too many nodes (110), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8787
warning: Included by graph for 'config.h' not generated, too many nodes (87), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8888
warning: Included by graph for 'exception_utils.h' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
8989
warning: Included by graph for 'expr.h' not generated, too many nodes (87), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9090
warning: Included by graph for 'expr_util.h' not generated, too many nodes (61), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
91-
warning: Included by graph for 'invariant.h' not generated, too many nodes (187), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
91+
warning: Included by graph for 'invariant.h' not generated, too many nodes (188), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9292
warning: Included by graph for 'irep.h' not generated, too many nodes (62), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9393
warning: Included by graph for 'message.h' not generated, too many nodes (117), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9494
warning: Included by graph for 'namespace.h' not generated, too many nodes (109), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9595
warning: Included by graph for 'pointer_expr.h' not generated, too many nodes (101), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9696
warning: Included by graph for 'prefix.h' not generated, too many nodes (86), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
97-
warning: Included by graph for 'simplify_expr.h' not generated, too many nodes (77), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
97+
warning: Included by graph for 'simplify_expr.h' not generated, too many nodes (78), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9898
warning: Included by graph for 'std_code.h' not generated, too many nodes (78), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
99-
warning: Included by graph for 'std_expr.h' not generated, too many nodes (244), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
99+
warning: Included by graph for 'std_expr.h' not generated, too many nodes (245), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
100100
warning: Included by graph for 'std_types.h' not generated, too many nodes (121), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
101101
warning: Included by graph for 'symbol_table.h' not generated, too many nodes (95), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.

src/analyses/goto_check.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ class goto_checkt
172172

173173
using conditionst = std::list<conditiont>;
174174

175-
void bounds_check(const index_exprt &, const guardt &);
175+
void bounds_check(const exprt &, const guardt &);
176+
void bounds_check_index(const index_exprt &, const guardt &);
177+
void bounds_check_clz(const count_leading_zeros_exprt &, const guardt &);
176178
void div_by_zero_check(const div_exprt &, const guardt &);
177179
void mod_by_zero_check(const mod_exprt &, const guardt &);
178180
void mod_overflow_check(const mod_exprt &, const guardt &);
@@ -1310,9 +1312,7 @@ std::string goto_checkt::array_name(const exprt &expr)
13101312
return ::array_name(ns, expr);
13111313
}
13121314

1313-
void goto_checkt::bounds_check(
1314-
const index_exprt &expr,
1315-
const guardt &guard)
1315+
void goto_checkt::bounds_check(const exprt &expr, const guardt &guard)
13161316
{
13171317
if(!enable_bounds_check)
13181318
return;
@@ -1324,6 +1324,16 @@ void goto_checkt::bounds_check(
13241324
return;
13251325
}
13261326

1327+
if(expr.id() == ID_index)
1328+
bounds_check_index(to_index_expr(expr), guard);
1329+
else if(expr.id() == ID_count_leading_zeros)
1330+
bounds_check_clz(to_count_leading_zeros_expr(expr), guard);
1331+
}
1332+
1333+
void goto_checkt::bounds_check_index(
1334+
const index_exprt &expr,
1335+
const guardt &guard)
1336+
{
13271337
typet array_type = expr.array().type();
13281338

13291339
if(array_type.id()==ID_pointer)
@@ -1499,6 +1509,19 @@ void goto_checkt::bounds_check(
14991509
}
15001510
}
15011511

1512+
void goto_checkt::bounds_check_clz(
1513+
const count_leading_zeros_exprt &expr,
1514+
const guardt &guard)
1515+
{
1516+
add_guarded_property(
1517+
notequal_exprt{expr.op(), from_integer(0, expr.op().type())},
1518+
"count leading zeros argument",
1519+
"bit count",
1520+
expr.find_source_location(),
1521+
expr,
1522+
guard);
1523+
}
1524+
15021525
void goto_checkt::add_guarded_property(
15031526
const exprt &asserted_expr,
15041527
const std::string &comment,
@@ -1718,7 +1741,7 @@ void goto_checkt::check_rec(const exprt &expr, guardt &guard)
17181741

17191742
if(expr.id()==ID_index)
17201743
{
1721-
bounds_check(to_index_expr(expr), guard);
1744+
bounds_check(expr, guard);
17221745
}
17231746
else if(expr.id()==ID_div)
17241747
{
@@ -1755,6 +1778,10 @@ void goto_checkt::check_rec(const exprt &expr, guardt &guard)
17551778
{
17561779
pointer_primitive_check(expr, guard);
17571780
}
1781+
else if(expr.id() == ID_count_leading_zeros)
1782+
{
1783+
bounds_check(expr, guard);
1784+
}
17581785
}
17591786

17601787
void goto_checkt::check(const exprt &expr)

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,27 +2689,14 @@ exprt c_typecheck_baset::do_special_functions(
26892689
throw 0;
26902690
}
26912691

2692-
side_effect_expr_function_callt try_constant{expr};
2693-
typecheck_function_call_arguments(try_constant);
2694-
exprt argument = try_constant.arguments().front();
2695-
simplify(argument, *this);
2696-
const auto int_constant = numeric_cast<mp_integer>(argument);
2697-
2698-
if(
2699-
!int_constant.has_value() || *int_constant == 0 ||
2700-
argument.type().id() != ID_unsignedbv)
2701-
{
2702-
return nil_exprt{};
2703-
}
2692+
typecheck_function_call_arguments(expr);
27042693

2705-
const std::string binary_value = integer2binary(
2706-
*int_constant, to_unsignedbv_type(argument.type()).get_width());
2707-
std::size_t n_leading_zeros = binary_value.find('1');
2708-
CHECK_RETURN(n_leading_zeros != std::string::npos);
2694+
count_leading_zeros_exprt clz{expr.arguments().front(),
2695+
has_prefix(id2string(identifier), "__lzcnt"),
2696+
expr.type()};
2697+
clz.add_source_location() = source_location;
27092698

2710-
return from_integer(
2711-
n_leading_zeros,
2712-
to_code_type(try_constant.function().type()).return_type());
2699+
return std::move(clz);
27132700
}
27142701
else if(identifier==CPROVER_PREFIX "equal")
27152702
{

src/ansi-c/expr2c.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3896,6 +3896,9 @@ std::string expr2ct::convert_with_precedence(
38963896
else if(src.id()==ID_type)
38973897
return convert(src.type());
38983898

3899+
else if(src.id() == ID_count_leading_zeros)
3900+
return convert_function(src, "__builtin_clz");
3901+
38993902
// no C language expression for internal representation
39003903
return convert_norep(src, precedence);
39013904
}

src/ansi-c/library/gcc.c

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,64 +33,6 @@ inline void __sync_synchronize(void)
3333
#endif
3434
}
3535

36-
/* FUNCTION: __builtin_clz */
37-
38-
int __builtin_popcount(unsigned int);
39-
40-
inline int __builtin_clz(unsigned int x)
41-
{
42-
__CPROVER_precondition(x != 0, "__builtin_clz(0) is undefined");
43-
44-
x = x | (x >> 1);
45-
x = x | (x >> 2);
46-
x = x | (x >> 4);
47-
x = x | (x >> 8);
48-
if(sizeof(x) >= 4)
49-
x = x | (x >> 16);
50-
51-
return __builtin_popcount(~x);
52-
}
53-
54-
/* FUNCTION: __builtin_clzl */
55-
56-
int __builtin_popcountl(unsigned long int);
57-
58-
inline int __builtin_clzl(unsigned long int x)
59-
{
60-
__CPROVER_precondition(x != 0, "__builtin_clzl(0) is undefined");
61-
62-
x = x | (x >> 1);
63-
x = x | (x >> 2);
64-
x = x | (x >> 4);
65-
x = x | (x >> 8);
66-
if(sizeof(x) >= 4)
67-
x = x | (x >> 16);
68-
if(sizeof(x) >= 8)
69-
x = x | (x >> 32);
70-
71-
return __builtin_popcountl(~x);
72-
}
73-
74-
/* FUNCTION: __builtin_clzll */
75-
76-
int __builtin_popcountll(unsigned long long int);
77-
78-
inline int __builtin_clzll(unsigned long long int x)
79-
{
80-
__CPROVER_precondition(x != 0, "__builtin_clzll(0) is undefined");
81-
82-
x = x | (x >> 1);
83-
x = x | (x >> 2);
84-
x = x | (x >> 4);
85-
x = x | (x >> 8);
86-
if(sizeof(x) >= 4)
87-
x = x | (x >> 16);
88-
if(sizeof(x) >= 8)
89-
x = x | (x >> 32);
90-
91-
return __builtin_popcountll(~x);
92-
}
93-
9436
/* FUNCTION: __builtin_ffs */
9537

9638
int __builtin_clz(unsigned int x);

src/ansi-c/library/windows.c

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,3 @@ inline HANDLE CreateThread(
5151
return handle;
5252
}
5353
#endif
54-
55-
/* FUNCTION: __lzcnt16 */
56-
57-
#ifdef _MSC_VER
58-
int __builtin_clz(unsigned int x);
59-
60-
unsigned short __lzcnt16(unsigned short value)
61-
{
62-
if(value == 0)
63-
return 16;
64-
65-
return __builtin_clz(value) -
66-
(sizeof(unsigned int) - sizeof(unsigned short)) * 8;
67-
}
68-
#endif
69-
70-
/* FUNCTION: __lzcnt */
71-
72-
#ifdef _MSC_VER
73-
int __builtin_clz(unsigned int x);
74-
75-
unsigned int __lzcnt(unsigned int value)
76-
{
77-
if(value == 0)
78-
return 32;
79-
80-
return __builtin_clz(value);
81-
}
82-
#endif
83-
84-
/* FUNCTION: __lzcnt64 */
85-
86-
#ifdef _MSC_VER
87-
int __builtin_clzll(unsigned long long x);
88-
89-
unsigned __int64 __lzcnt64(unsigned __int64 value)
90-
{
91-
if(value == 0)
92-
return 64;
93-
94-
return __builtin_clzll(value);
95-
}
96-
#endif

src/solvers/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ SRC = $(BOOLEFORCE_SRC) \
140140
floatbv/float_utils.cpp \
141141
floatbv/float_approximation.cpp \
142142
lowering/byte_operators.cpp \
143+
lowering/count_leading_zeros.cpp \
143144
lowering/functions.cpp \
144145
lowering/popcount.cpp \
145146
bdd/miniBDD/miniBDD.cpp \

src/solvers/flattening/boolbv.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
228228
return convert_power(to_binary_expr(expr));
229229
else if(expr.id() == ID_popcount)
230230
return convert_bv(lower_popcount(to_popcount_expr(expr), ns));
231+
else if(expr.id() == ID_count_leading_zeros)
232+
return convert_bv(lower_clz(to_count_leading_zeros_expr(expr), ns));
231233

232234
return conversion_failed(expr);
233235
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*******************************************************************\
2+
3+
Module: Lowering of count_leading_zeros
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
#include "expr_lowering.h"
10+
11+
#include <util/arith_tools.h>
12+
#include <util/bitvector_expr.h>
13+
#include <util/invariant.h>
14+
#include <util/pointer_offset_size.h>
15+
#include <util/std_expr.h>
16+
17+
exprt lower_clz(const count_leading_zeros_exprt &expr, const namespacet &ns)
18+
{
19+
// x = x | (x >> 1);
20+
// x = x | (x >> 2);
21+
// x = x | (x >> 4);
22+
// x = x | (x >> 8);
23+
// etc.
24+
// return popcount(~x);
25+
26+
// make sure the operand width is a power of two
27+
exprt x = expr.op();
28+
const auto x_width = pointer_offset_bits(x.type(), ns);
29+
CHECK_RETURN(x_width.has_value() && *x_width >= 1);
30+
const std::size_t bits = address_bits(*x_width);
31+
const std::size_t new_width = numeric_cast_v<std::size_t>(power(2, bits));
32+
33+
const bool need_typecast =
34+
new_width > *x_width || x.type().id() != ID_unsignedbv;
35+
36+
if(need_typecast)
37+
x = typecast_exprt(x, unsignedbv_typet(new_width));
38+
39+
// repeatedly compute x = x | (x >> shift)
40+
for(std::size_t shift = 1; shift < new_width; shift <<= 1)
41+
{
42+
// x >> shift
43+
lshr_exprt shifted_x(
44+
x, from_integer(shift, unsignedbv_typet(address_bits(shift) + 1)));
45+
// build the expression
46+
x = bitor_exprt{x, shifted_x};
47+
}
48+
49+
// the result is restricted to the result type
50+
return lower_popcount(
51+
popcount_exprt{
52+
bitnot_exprt{typecast_exprt::conditional_cast(x, expr.op().type())},
53+
expr.type()},
54+
ns);
55+
}

src/solvers/lowering/expr_lowering.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Michael Tautschnig
1313

1414
class byte_extract_exprt;
1515
class byte_update_exprt;
16+
class count_leading_zeros_exprt;
1617
class namespacet;
1718
class popcount_exprt;
1819

@@ -50,4 +51,10 @@ bool has_byte_operator(const exprt &src);
5051
/// \return Semantically equivalent expression
5152
exprt lower_popcount(const popcount_exprt &expr, const namespacet &ns);
5253

54+
/// Lower a count_leading_zeros_exprt to arithmetic and logic expressions.
55+
/// \param expr: Input expression to be translated
56+
/// \param ns: Namespace for type lookups
57+
/// \return Semantically equivalent expression
58+
exprt lower_clz(const count_leading_zeros_exprt &expr, const namespacet &ns);
59+
5360
#endif /* CPROVER_SOLVERS_LOWERING_EXPR_LOWERING_H */

0 commit comments

Comments
 (0)