]> granicus.if.org Git - clang/commitdiff
CodeGen: Cast llvm.flt.rounds result to match __builtin_flt_rounds
authorAnton Korobeynikov <anton@korobeynikov.info>
Thu, 17 Jan 2019 15:21:55 +0000 (15:21 +0000)
committerAnton Korobeynikov <anton@korobeynikov.info>
Thu, 17 Jan 2019 15:21:55 +0000 (15:21 +0000)
llvm.flt.rounds returns an i32, but the builtin expects an integer.
On targets where integers are not 32-bits clang tries to bitcast the result, causing an assertion failure.

The patch enables newlib build for msp430.

Patch by Edward Jones!

Differential Revision: https://reviews.llvm.org/D24461

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@351449 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGBuiltin.cpp
test/CodeGen/builtins-msp430.c [new file with mode: 0644]
test/CodeGen/builtins.c

index 34bf194be2fd8d270f0d1d87e9a26517a0c6c115..6954d32549aedd8982b585b7c625d48d61cd668f 100644 (file)
@@ -2130,6 +2130,17 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
     return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
   }
 
+  case Builtin::BI__builtin_flt_rounds: {
+    Value *F = CGM.getIntrinsic(Intrinsic::flt_rounds);
+
+    llvm::Type *ResultType = ConvertType(E->getType());
+    Value *Result = Builder.CreateCall(F);
+    if (Result->getType() != ResultType)
+      Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
+                                     "cast");
+    return RValue::get(Result);
+  }
+
   case Builtin::BI__builtin_fpclassify: {
     Value *V = EmitScalarExpr(E->getArg(5));
     llvm::Type *Ty = ConvertType(E->getArg(5)->getType());
diff --git a/test/CodeGen/builtins-msp430.c b/test/CodeGen/builtins-msp430.c
new file mode 100644 (file)
index 0000000..8fb409b
--- /dev/null
@@ -0,0 +1,10 @@
+// REQUIRES: msp430-registered-target
+// RUN: %clang_cc1 -triple msp430-unknown-unknown -emit-llvm %s -o - | FileCheck %s
+
+int test_builtin_flt_rounds() {
+  // CHECK:     [[V0:[%A-Za-z0-9.]+]] = call i32 @llvm.flt.rounds()
+  // CHECK-DAG: [[V1:[%A-Za-z0-9.]+]] = trunc i32 [[V0]] to i16
+  // CHECK-DAG: ret i16 [[V1]]
+  return __builtin_flt_rounds();
+}
+
index 8e0542a1a8393fb089cb2dd6701c9aac6d68fd94..452c1408149eebe29ea8437745313afb0e7d2a06 100644 (file)
@@ -246,6 +246,9 @@ void test_float_builtins(float F, double D, long double LD) {
   // CHECK: fcmp uge float {{.*}}, 0x3810000000000000
   // CHECK: and i1
   // CHECK: and i1
+
+  res = __builtin_flt_rounds();
+  // CHECK: call i32 @llvm.flt.rounds(
 }
 
 // CHECK-LABEL: define void @test_float_builtin_ops