forked from OSchip/llvm-project
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. llvm-svn: 97383
This commit is contained in:
parent
c12b133d00
commit
1629149103
|
@ -1560,6 +1560,31 @@ static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
|
|||
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 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
|
|||
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:
|
||||
|
|
|
@ -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 p(char *str, int x) {
|
|||
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 @@ int main() {
|
|||
// 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: }
|
||||
|
|
Loading…
Reference in New Issue