-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Implement constexpr support for __builtin_{clzg,ctzg} #86577
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12354,21 +12354,31 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, | |
case Builtin::BI__builtin_clzl: | ||
case Builtin::BI__builtin_clzll: | ||
case Builtin::BI__builtin_clzs: | ||
case Builtin::BI__builtin_clzg: | ||
nickdesaulniers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes | ||
case Builtin::BI__lzcnt: | ||
case Builtin::BI__lzcnt64: { | ||
APSInt Val; | ||
if (!EvaluateInteger(E->getArg(0), Val, Info)) | ||
return false; | ||
|
||
// When the argument is 0, the result of GCC builtins is undefined, whereas | ||
// for Microsoft intrinsics, the result is the bit-width of the argument. | ||
bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 && | ||
BuiltinOp != Builtin::BI__lzcnt && | ||
BuiltinOp != Builtin::BI__lzcnt64; | ||
if (!Val) { | ||
nickdesaulniers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (BuiltinOp == Builtin::BI__builtin_clzg && E->getNumArgs() > 1) { | ||
if (!EvaluateInteger(E->getArg(1), Val, Info)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This EvaluateInteger call can't be guarded by the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. I will fix this now. |
||
return false; | ||
return Success(Val, E); | ||
} | ||
|
||
if (ZeroIsUndefined && !Val) | ||
return Error(E); | ||
// When the argument is 0, the result of GCC builtins is undefined, | ||
// whereas for Microsoft intrinsics, the result is the bit-width of the | ||
// argument. | ||
bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 && | ||
BuiltinOp != Builtin::BI__lzcnt && | ||
BuiltinOp != Builtin::BI__lzcnt64; | ||
|
||
if (ZeroIsUndefined) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sink the definition of |
||
return Error(E); | ||
} | ||
|
||
return Success(Val.countl_zero(), E); | ||
} | ||
|
@@ -12410,12 +12420,21 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, | |
case Builtin::BI__builtin_ctz: | ||
case Builtin::BI__builtin_ctzl: | ||
case Builtin::BI__builtin_ctzll: | ||
case Builtin::BI__builtin_ctzs: { | ||
case Builtin::BI__builtin_ctzs: | ||
case Builtin::BI__builtin_ctzg: { | ||
APSInt Val; | ||
if (!EvaluateInteger(E->getArg(0), Val, Info)) | ||
return false; | ||
if (!Val) | ||
|
||
if (!Val) { | ||
if (BuiltinOp == Builtin::BI__builtin_ctzg && E->getNumArgs() > 1) { | ||
if (!EvaluateInteger(E->getArg(1), Val, Info)) | ||
return false; | ||
return Success(Val, E); | ||
} | ||
nickdesaulniers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return Error(E); | ||
} | ||
|
||
return Success(Val.countr_zero(), E); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had forgotten to add a release note for the addition of these builtins in #83431.