]> granicus.if.org Git - clang/commitdiff
Fix part of PR12457. Patch by Justin Bogner!
authorRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 13 Jun 2013 05:04:16 +0000 (05:04 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 13 Jun 2013 05:04:16 +0000 (05:04 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183886 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/ExprConstant.cpp
test/Sema/constant-builtins-2.c

index 21fb4599664dc3f62655062ba02c6ae939fca5a6..84f7bfdf82b78035b83ae159f22b871b93cb1e94 100644 (file)
@@ -5691,6 +5691,40 @@ 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;
+    if (!Val)
+      return Error(E);
+
+    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;
+    if (!Val)
+      return Error(E);
+
+    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);
 
index 13d81fe13cbf2e30dfcbcddd5f5c9f3c6fcd71d7..14afe3e31a1a19e52cd15ccb173e861b1a7c6bb8 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only %s
+// RUN: %clang_cc1 -fsyntax-only -verify %s
 
 // Math stuff
 
@@ -41,6 +41,33 @@ long double  g18 = __builtin_copysignl(1.0L, -1.0L);
 //float        g20 = __builtin_powif(2.0f, 4);
 //long double  g21 = __builtin_powil(2.0L, 4);
 
+#define BITSIZE(x) (sizeof(x) * 8)
+char g22[__builtin_clz(1) == BITSIZE(int) - 1 ? 1 : -1];
+char g23[__builtin_clz(7) == BITSIZE(int) - 3 ? 1 : -1];
+char g24[__builtin_clz(1 << (BITSIZE(int) - 1)) == 0 ? 1 : -1];
+int g25 = __builtin_clz(0); // expected-error {{not a compile-time constant}}
+char g26[__builtin_clzl(0xFL) == BITSIZE(long) - 4 ? 1 : -1];
+char g27[__builtin_clzll(0xFFLL) == BITSIZE(long long) - 8 ? 1 : -1];
+
+char g28[__builtin_ctz(1) == 0 ? 1 : -1];
+char g29[__builtin_ctz(8) == 3 ? 1 : -1];
+char g30[__builtin_ctz(1 << (BITSIZE(int) - 1)) == BITSIZE(int) - 1 ? 1 : -1];
+int g31 = __builtin_ctz(0); // expected-error {{not a compile-time constant}}
+char g32[__builtin_ctzl(0x10L) == 4 ? 1 : -1];
+char g33[__builtin_ctzll(0x100LL) == 8 ? 1 : -1];
+
+char g34[__builtin_popcount(0) == 0 ? 1 : -1];
+char g35[__builtin_popcount(0xF0F0) == 8 ? 1 : -1];
+char g36[__builtin_popcount(~0) == BITSIZE(int) ? 1 : -1];
+char g37[__builtin_popcount(~0L) == BITSIZE(int) ? 1 : -1];
+char g38[__builtin_popcountl(0L) == 0 ? 1 : -1];
+char g39[__builtin_popcountl(0xF0F0L) == 8 ? 1 : -1];
+char g40[__builtin_popcountl(~0L) == BITSIZE(long) ? 1 : -1];
+char g41[__builtin_popcountll(0LL) == 0 ? 1 : -1];
+char g42[__builtin_popcountll(0xF0F0LL) == 8 ? 1 : -1];
+char g43[__builtin_popcountll(~0LL) == BITSIZE(long long) ? 1 : -1];
+#undef BITSIZE
+
 // GCC misc stuff
 
 extern int f();