From 5db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afb Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Thu, 16 Oct 2008 03:51:50 +0000 Subject: [PATCH] Teach tryEvaluate that fabs, copysign, and unary +/- are constants for floats. - With testcase, which also has some other things GCC folds but we don't commented out in it. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57624 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/ExprConstant.cpp | 37 +++++++++++++++++++++++- test/Sema/constant-builtins-2.c | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 test/Sema/constant-builtins-2.c diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index cc2b4e6735..5bc4170e70 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -586,6 +586,7 @@ public: bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } bool VisitCallExpr(const CallExpr *E); + bool VisitUnaryOperator(const UnaryOperator *E); bool VisitBinaryOperator(const BinaryOperator *E); bool VisitFloatingLiteral(const FloatingLiteral *E); }; @@ -625,14 +626,48 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { } } return false; + + case Builtin::BI__builtin_fabs: + case Builtin::BI__builtin_fabsf: + case Builtin::BI__builtin_fabsl: + if (!EvaluateFloat(E->getArg(0), Result, Info)) + return false; + + if (Result.isNegative()) + Result.changeSign(); + return true; + + case Builtin::BI__builtin_copysign: + case Builtin::BI__builtin_copysignf: + case Builtin::BI__builtin_copysignl: { + APFloat RHS(0.); + if (!EvaluateFloat(E->getArg(0), Result, Info) || + !EvaluateFloat(E->getArg(1), RHS, Info)) + return false; + Result.copySign(RHS); + return true; + } } } +bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { + if (!EvaluateFloat(E->getSubExpr(), Result, Info)) + return false; + + switch (E->getOpcode()) { + default: return false; + case UnaryOperator::Plus: + return true; + case UnaryOperator::Minus: + Result.changeSign(); + return true; + } +} bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { // FIXME: Diagnostics? I really don't understand how the warnings // and errors are supposed to work. - APFloat LHS(0.0), RHS(0.0); + APFloat RHS(0.0); if (!EvaluateFloat(E->getLHS(), Result, Info)) return false; if (!EvaluateFloat(E->getRHS(), RHS, Info)) diff --git a/test/Sema/constant-builtins-2.c b/test/Sema/constant-builtins-2.c new file mode 100644 index 0000000000..d47cc3556b --- /dev/null +++ b/test/Sema/constant-builtins-2.c @@ -0,0 +1,50 @@ +// RUN: clang -fsyntax-only %s + +// Math stuff + +double g0 = __builtin_huge_val(); +float g1 = __builtin_huge_valf(); +long double g2 = __builtin_huge_vall(); + +double g3 = __builtin_inf(); +float g4 = __builtin_inff(); +long double g5 = __builtin_infl(); + +double g6 = __builtin_nan(""); +float g7 = __builtin_nanf(""); +long double g8 = __builtin_nanl(""); + +// GCC constant folds these too (via native strtol): +//double g6_1 = __builtin_nan("1"); +//float g7_1 = __builtin_nanf("1"); +//long double g8_1 = __builtin_nanl("1"); + +// APFloat doesn't have signalling NaN functions. +//double g9 = __builtin_nans(""); +//float g10 = __builtin_nansf(""); +//long double g11 = __builtin_nansl(""); + +//int g12 = __builtin_abs(-12); + +double g13 = __builtin_fabs(-12.); +double g13_0 = __builtin_fabs(-0.); +double g13_1 = __builtin_fabs(-__builtin_inf()); +float g14 = __builtin_fabsf(-12.f); +// GCC doesn't eat this one. +//long double g15 = __builtin_fabsfl(-12.0L); + +float g16 = __builtin_copysign(1.0, -1.0); +double g17 = __builtin_copysignf(1.0f, -1.0f); +long double g18 = __builtin_copysignl(1.0L, -1.0L); + +//double g19 = __builtin_powi(2.0, 4); +//float g20 = __builtin_powif(2.0f, 4); +//long double g21 = __builtin_powil(2.0L, 4); + +// GCC misc stuff + +extern int f(); + +int h0 = __builtin_types_compatible_p(int, float); +//int h1 = __builtin_choose_expr(1, 10, f()); +//int h2 = __builtin_expect(0, 0); -- 2.40.0