[cfe-commits] r97383 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/CodeGen/builtins.c

John McCall rjmccall at apple.com
Sun Feb 28 05:00:20 PST 2010


Author: rjmccall
Date: Sun Feb 28 07:00:19 2010
New Revision: 97383

URL: http://llvm.org/viewvc/llvm-project?rev=97383&view=rev
Log:
Support constant-evaluation of __builtin_nans* as well as the correct constant
evaluation of __builtin_nan*.  Most of the work to make this work is in LLVM.

Fixes <rdar://problem/7696712> and part of PR 5255.


Modified:
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/test/CodeGen/builtins.c

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=97383&r1=97382&r2=97383&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Sun Feb 28 07:00:19 2010
@@ -1560,6 +1560,31 @@
   return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
 }
 
+static bool TryEvaluateBuiltinNaN(ASTContext &Context,
+                                  QualType ResultTy,
+                                  const Expr *Arg,
+                                  bool SNaN,
+                                  llvm::APFloat &Result) {
+  const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
+  if (!S) return false;
+
+  const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
+
+  llvm::APInt fill;
+
+  // Treat empty strings as if they were zero.
+  if (S->getString().empty())
+    fill = llvm::APInt(32, 0);
+  else if (S->getString().getAsInteger(0, fill))
+    return false;
+
+  if (SNaN)
+    Result = llvm::APFloat::getSNaN(Sem, false, &fill);
+  else
+    Result = llvm::APFloat::getQNaN(Sem, false, &fill);
+  return true;
+}
+
 bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
   switch (E->isBuiltinCall(Info.Ctx)) {
   default: return false;
@@ -1575,24 +1600,19 @@
     return true;
   }
 
+  case Builtin::BI__builtin_nans:
+  case Builtin::BI__builtin_nansf:
+  case Builtin::BI__builtin_nansl:
+    return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
+                                 true, Result);
+
   case Builtin::BI__builtin_nan:
   case Builtin::BI__builtin_nanf:
   case Builtin::BI__builtin_nanl:
     // If this is __builtin_nan() turn this into a nan, otherwise we
     // can't constant fold it.
-    if (const StringLiteral *S =
-        dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
-      if (!S->isWide()) {
-        const llvm::fltSemantics &Sem =
-          Info.Ctx.getFloatTypeSemantics(E->getType());
-        unsigned Type = 0;
-        if (!S->getString().empty() && S->getString().getAsInteger(0, Type))
-          return false;
-        Result = llvm::APFloat::getNaN(Sem, false, Type);
-        return true;
-      }
-    }
-    return false;
+    return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
+                                 false, Result);
 
   case Builtin::BI__builtin_fabs:
   case Builtin::BI__builtin_fabsf:

Modified: cfe/trunk/test/CodeGen/builtins.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/builtins.c?rev=97383&r1=97382&r2=97383&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/builtins.c (original)
+++ cfe/trunk/test/CodeGen/builtins.c Sun Feb 28 07:00:19 2010
@@ -1,5 +1,6 @@
 // RUN: %clang_cc1 -emit-llvm -o %t %s
 // RUN: not grep __builtin %t
+// RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple | FileCheck %s
 
 int printf(const char *, ...);
 
@@ -9,11 +10,17 @@
 void q(char *str, double x) {
   printf("%s: %f\n", str, x);
 }
+void r(char *str, void *ptr) {
+  printf("%s: %p\n", str, ptr);
+}
+
+int random(void);
 
 int main() {
   int N = random();
 #define P(n,args) p(#n #args, __builtin_##n args)
 #define Q(n,args) q(#n #args, __builtin_##n args)
+#define R(n,args) r(#n #args, __builtin_##n args)
 #define V(n,args) p(#n #args, (__builtin_##n args, 0))
   P(types_compatible_p, (int, float));
   P(choose_expr, (0, 10, 20));
@@ -110,16 +117,48 @@
   // FIXME
   // V(clear_cache, (&N, &N+1));
   V(trap, ());
-  P(extract_return_addr, (&N));
+  R(extract_return_addr, (&N));
 
   return 0;
 }
 
 
 
-void strcat() {}
-
 void foo() {
  __builtin_strcat(0, 0);
 }
 
+// CHECK: define void @bar(
+void bar() {
+  float f;
+  double d;
+  long double ld;
+
+  // LLVM's hex representation of float constants is really unfortunate;
+  // basically it does a float-to-double "conversion" and then prints the
+  // hex form of that.  That gives us wierd artifacts like exponents
+  // that aren't numerically similar to the original exponent and
+  // significand bit-patterns that are offset by three bits (because
+  // the exponent was expanded from 8 bits to 11).
+  //
+  // 0xAE98 == 1010111010011000
+  // 0x15D3 == 1010111010011
+
+  f = __builtin_huge_valf();     // CHECK: float    0x7FF0000000000000
+  d = __builtin_huge_val();      // CHECK: double   0x7FF0000000000000
+  ld = __builtin_huge_vall();    // CHECK: x86_fp80 0xK7FFF8000000000000000
+  f = __builtin_nanf("");        // CHECK: float    0x7FF8000000000000
+  d = __builtin_nan("");         // CHECK: double   0x7FF8000000000000
+  ld = __builtin_nanl("");       // CHECK: x86_fp80 0xK7FFFC000000000000000
+  f = __builtin_nanf("0xAE98");  // CHECK: float    0x7FF815D300000000
+  d = __builtin_nan("0xAE98");   // CHECK: double   0x7FF800000000AE98
+  ld = __builtin_nanl("0xAE98"); // CHECK: x86_fp80 0xK7FFFC00000000000AE98
+  f = __builtin_nansf("");       // CHECK: float    0x7FF4000000000000
+  d = __builtin_nans("");        // CHECK: double   0x7FF4000000000000
+  ld = __builtin_nansl("");      // CHECK: x86_fp80 0xK7FFFA000000000000000
+  f = __builtin_nansf("0xAE98"); // CHECK: float    0x7FF015D300000000
+  d = __builtin_nans("0xAE98");  // CHECK: double   0x7FF000000000AE98
+  ld = __builtin_nansl("0xAE98");// CHECK: x86_fp80 0xK7FFF800000000000AE98
+
+}
+// CHECK: }





More information about the cfe-commits mailing list