[clang] [clang] Implement constexpr support for __builtin_{clzg, ctzg} (PR #86577)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 26 13:32:05 PDT 2024


================
@@ -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:
   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) {
+      if (BuiltinOp == Builtin::BI__builtin_clzg && E->getNumArgs() > 1) {
+        if (!EvaluateInteger(E->getArg(1), Val, Info))
----------------
efriedma-quic wrote:

This EvaluateInteger call can't be guarded by the `if (!Val)` check: even if we don't actually use the value of the second argument, we have to evaluate it for side-effects.  We don't want short-circuit the evaluation like for `&&`/`||`.

https://github.com/llvm/llvm-project/pull/86577


More information about the cfe-commits mailing list