Skip to content

Commit 9364cde

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 cf88f82 commit 9364cde

File tree

19 files changed

+249
-130
lines changed

19 files changed

+249
-130
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.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

scripts/expected_doxygen_warnings.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
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.

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,27 +2689,22 @@ 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);
2692+
typecheck_function_call_arguments(expr);
2693+
2694+
exprt clz =
2695+
count_leading_zeros_exprt{expr.arguments().front(),
2696+
has_prefix(id2string(identifier), "__lzcnt"),
2697+
expr.type()};
2698+
clz.add_source_location() = source_location;
26972699

26982700
if(
2699-
!int_constant.has_value() || *int_constant == 0 ||
2700-
argument.type().id() != ID_unsignedbv)
2701+
config.ansi_c.mode == configt::ansi_ct::flavourt::GCC ||
2702+
config.ansi_c.mode == configt::ansi_ct::flavourt::CLANG)
27012703
{
2702-
return nil_exprt{};
2704+
simplify(clz, *this);
27032705
}
27042706

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);
2709-
2710-
return from_integer(
2711-
n_leading_zeros,
2712-
to_code_type(try_constant.function().type()).return_type());
2707+
return clz;
27132708
}
27142709
else if(identifier==CPROVER_PREFIX "equal")
27152710
{

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/goto-programs/goto_clean_expr.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Author: Daniel Kroening, kroening@kroening.com
1111

1212
#include "goto_convert_class.h"
1313

14+
#include <util/arith_tools.h>
15+
#include <util/bitvector_expr.h>
1416
#include <util/cprover_prefix.h>
1517
#include <util/exception_utils.h>
1618
#include <util/expr_util.h>
@@ -100,6 +102,13 @@ bool goto_convertt::needs_cleaning(const exprt &expr)
100102
if(expr.id()==ID_forall || expr.id()==ID_exists)
101103
return false;
102104

105+
if(
106+
expr.id() == ID_count_leading_zeros &&
107+
!to_count_leading_zeros_expr(expr).zero_permitted())
108+
{
109+
return true;
110+
}
111+
103112
forall_operands(it, expr)
104113
if(needs_cleaning(*it))
105114
return true;
@@ -429,6 +438,22 @@ void goto_convertt::clean_expr(
429438
expr.operands().size() == 1, "ID_compound_literal has a single operand");
430439
expr = to_unary_expr(expr).op();
431440
}
441+
else if(expr.id() == ID_count_leading_zeros)
442+
{
443+
count_leading_zeros_exprt &clz = to_count_leading_zeros_expr(expr);
444+
if(!clz.zero_permitted())
445+
{
446+
clz.zero_permitted(true);
447+
const source_locationt source_location = clz.source_location();
448+
449+
exprt nondet = side_effect_expr_nondett{expr.type(), source_location};
450+
remove_side_effect(to_side_effect_expr(nondet), dest, mode, true, false);
451+
452+
expr = if_exprt{
453+
equal_exprt{clz.op(), from_integer(0, clz.op().type())}, nondet, clz};
454+
expr.add_source_location() = source_location;
455+
}
456+
}
432457
}
433458

434459
void goto_convertt::clean_expr_address_of(

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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
PRECONDITION(expr.zero_permitted());
20+
21+
// x = x | (x >> 1);
22+
// x = x | (x >> 2);
23+
// x = x | (x >> 4);
24+
// x = x | (x >> 8);
25+
// etc.
26+
// return popcount(~x);
27+
28+
// make sure the operand width is a power of two
29+
exprt x = expr.op();
30+
const auto x_width = pointer_offset_bits(x.type(), ns);
31+
CHECK_RETURN(x_width.has_value() && *x_width >= 1);
32+
const std::size_t bits = address_bits(*x_width);
33+
const std::size_t new_width = numeric_cast_v<std::size_t>(power(2, bits));
34+
35+
const bool need_typecast =
36+
new_width > *x_width || x.type().id() != ID_unsignedbv;
37+
38+
if(need_typecast)
39+
x = typecast_exprt(x, unsignedbv_typet(new_width));
40+
41+
// repeatedly compute x = x | (x >> shift)
42+
for(std::size_t shift = 1; shift < new_width; shift <<= 1)
43+
{
44+
// x >> shift
45+
lshr_exprt shifted_x(
46+
x, from_integer(shift, unsignedbv_typet(address_bits(shift) + 1)));
47+
// build the expression
48+
x = bitor_exprt{x, shifted_x};
49+
}
50+
51+
// the result is restricted to the result type
52+
return lower_popcount(
53+
popcount_exprt{
54+
bitnot_exprt{typecast_exprt::conditional_cast(x, expr.op().type())},
55+
expr.type()},
56+
ns);
57+
}

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 */

src/solvers/smt2/smt2_conv.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,11 @@ void smt2_convt::convert_expr(const exprt &expr)
19871987
exprt lowered = lower_popcount(to_popcount_expr(expr), ns);
19881988
convert_expr(lowered);
19891989
}
1990+
else if(expr.id() == ID_count_leading_zeros)
1991+
{
1992+
exprt lowered = lower_clz(to_count_leading_zeros_expr(expr), ns);
1993+
convert_expr(lowered);
1994+
}
19901995
else
19911996
INVARIANT_WITH_DIAGNOSTICS(
19921997
false,

0 commit comments

Comments
 (0)