Joerg Sonnenberg wrote:
> On Tue, Jun 11, 2013 at 12:18:55AM -0600, Justin Bogner wrote:
>> The clz, ctz, and popcount builtins are trivial to add constant folded
>> implementations of, and gcc accepts them in const contexts.
> 
> Can you test the constant folding with
> char foo[f(x) == val ? 1 : -1];
> 
> or so?

Do you mean something like this?

diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 8c65029..9287800 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -5042,6 +5042,36 @@ bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
     return Success(Val.byteSwap(), E);
   }
 
+  case Builtin::BI__builtin_clz:
+  case Builtin::BI__builtin_clzl:
+  case Builtin::BI__builtin_clzll: {
+    APSInt Val;
+    if (!EvaluateInteger(E->getArg(0), Val, Info))
+      return false;
+
+    return Success(Val.countLeadingZeros(), E);
+  }
+
+  case Builtin::BI__builtin_ctz:
+  case Builtin::BI__builtin_ctzl:
+  case Builtin::BI__builtin_ctzll: {
+    APSInt Val;
+    if (!EvaluateInteger(E->getArg(0), Val, Info))
+      return false;
+
+    return Success(Val.countTrailingZeros(), E);
+  }
+
+  case Builtin::BI__builtin_popcount:
+  case Builtin::BI__builtin_popcountl:
+  case Builtin::BI__builtin_popcountll: {
+    APSInt Val;
+    if (!EvaluateInteger(E->getArg(0), Val, Info))
+      return false;
+
+    return Success(Val.countPopulation(), E);
+  }
+
   case Builtin::BI__builtin_classify_type:
     return Success(EvaluateBuiltinClassifyType(E), E);
 
diff --git a/test/Sema/constant-builtins-2.c b/test/Sema/constant-builtins-2.c
index 13d81fe..85af2ed 100644
--- a/test/Sema/constant-builtins-2.c
+++ b/test/Sema/constant-builtins-2.c
@@ -41,6 +41,16 @@ long double  g18 = __builtin_copysignl(1.0L, -1.0L);
 //float        g20 = __builtin_powif(2.0f, 4);
 //long double  g21 = __builtin_powil(2.0L, 4);
 
+char g22[__builtin_clz(1) == sizeof(int) * 8 - 1 ? 1 : -1];
+char g23[__builtin_clzl(1L) == sizeof(long) * 8 - 1 ? 1 : -1];
+char g24[__builtin_clzll(1LL) == sizeof(long long) * 8 - 1 ? 1 : -1];
+char g25[__builtin_ctz(2) == 1 ? 1 : -1];
+char g26[__builtin_ctzl(2L) == 1 ? 1 : -1];
+char g27[__builtin_ctzll(2LL) == 1 ? 1 : -1];
+char g28[__builtin_popcount(1) == 1 ? 1 : -1];
+char g29[__builtin_popcountl(1L) == 1 ? 1 : -1];
+char g30[__builtin_popcountll(1LL) == 1 ? 1 : -1];
+
 // GCC misc stuff
 
 extern int f();
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to