diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 651539f0196e..90fcb698285e 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -362,6 +362,19 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()), "tmp")); } + case Builtin::BI__builtin_isfinite: { + // isfinite(x) --> x == x && fabs(x) != infinity; } + Value *V = EmitScalarExpr(E->getArg(0)); + Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq"); + + Value *Abs = EmitFAbs(*this, V, E->getArg(0)->getType()); + Value *IsNotInf = + Builder.CreateFCmpUNE(Abs, ConstantFP::getInfinity(V->getType()),"isinf"); + + V = Builder.CreateAnd(Eq, IsNotInf, "and"); + return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType()))); + } + case Builtin::BIalloca: case Builtin::BI__builtin_alloca: { // FIXME: LLVM IR Should allow alloca with an i64 size! diff --git a/clang/test/CodeGen/builtins.c b/clang/test/CodeGen/builtins.c index e604fbe31893..b6a4a68ea4fc 100644 --- a/clang/test/CodeGen/builtins.c +++ b/clang/test/CodeGen/builtins.c @@ -165,8 +165,8 @@ void bar() { // CHECK: } -// CHECK: define void @test_inff -void test_inff(float F, double D, long double LD) { +// CHECK: define void @test_float_builtins +void test_float_builtins(float F, double D, long double LD) { volatile int res; res = __builtin_isinf(F); // CHECK: call float @fabsf(float @@ -179,5 +179,11 @@ void test_inff(float F, double D, long double LD) { res = __builtin_isinf(LD); // CHECK: call x86_fp80 @fabsl(x86_fp80 // CHECK: fcmp oeq x86_fp80 {{.*}}, 0xK7FFF8000000000000000 + + res = __builtin_isfinite(F); + // CHECK: fcmp oeq float + // CHECK: call float @fabsf + // CHECK: fcmp une float {{.*}}, 0x7FF0000000000000 + // CHECK: and i1 }