Skip to content

C front-end: evaluate __builtin_clz over constants #5735

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

Merged
merged 2 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions regression/cbmc-library/__builtin_clz-02/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifdef _MSC_VER
# define __builtin_clz(x) __lzcnt(x)
# define _Static_assert static_assert
#endif

int main()
{
_Static_assert(
__builtin_clz(0xffU) == 8 * sizeof(unsigned) - 8, "compile-time constant");

return 0;
}
11 changes: 11 additions & 0 deletions regression/cbmc-library/__builtin_clz-02/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
main.c

^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
--
For this example, __builtin_clz is fully evaluated in the front-end. The
_Static_assert ensures this is the case, as type checking fails when
_Static_assert does not have a compile-time constant value.
34 changes: 34 additions & 0 deletions src/ansi-c/c_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2591,6 +2591,40 @@ exprt c_typecheck_baset::do_special_functions(

return std::move(popcount_expr);
}
else if(
identifier == "__builtin_clz" || identifier == "__builtin_clzl" ||
identifier == "__builtin_clzll" || identifier == "__lzcnt16" ||
identifier == "__lzcnt" || identifier == "__lzcnt64")
{
if(expr.arguments().size() != 1)
{
error().source_location = f_op.source_location();
error() << identifier << " expects one operand" << eom;
throw 0;
}

side_effect_expr_function_callt try_constant{expr};
typecheck_function_call_arguments(try_constant);
exprt argument = try_constant.arguments().front();
simplify(argument, *this);
const auto int_constant = numeric_cast<mp_integer>(argument);

if(
!int_constant.has_value() || *int_constant == 0 ||
argument.type().id() != ID_unsignedbv)
{
return nil_exprt{};
Copy link
Member

Choose a reason for hiding this comment

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

What happens here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That just means it will be treated as a regular function call, not a "special function." So we fall back to using the library implementation.

}

const std::string binary_value = integer2binary(
*int_constant, to_unsignedbv_type(argument.type()).get_width());
std::size_t n_leading_zeros = binary_value.find('1');
CHECK_RETURN(n_leading_zeros != std::string::npos);

return from_integer(
n_leading_zeros,
to_code_type(try_constant.function().type()).return_type());
}
else if(identifier==CPROVER_PREFIX "equal")
{
if(expr.arguments().size()!=2)
Expand Down
3 changes: 3 additions & 0 deletions src/ansi-c/windows_builtin_headers.h
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
int __assume(int);
unsigned short __lzcnt16(unsigned short value);
unsigned int __lzcnt(unsigned int value);
unsigned __int64 __lzcnt64(unsigned __int64 value);