]> granicus.if.org Git - clang/commitdiff
Add target hook CodeGen queries when generating builtin pow*.
authorEli Bendersky <eliben@google.com>
Wed, 3 Jul 2013 19:19:12 +0000 (19:19 +0000)
committerEli Bendersky <eliben@google.com>
Wed, 3 Jul 2013 19:19:12 +0000 (19:19 +0000)
Without fmath-errno, Clang currently generates calls to @llvm.pow.* intrinsics
when it sees pow*(). This may not be suitable for all targets (for
example le32/PNaCl), so the attached patch adds a target hook that CodeGen
queries. The target can state its preference for having or not having the
intrinsic generated. Non-PNaCl behavior remains unchanged;
PNaCl-specific test added.

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

lib/CodeGen/CGBuiltin.cpp
lib/CodeGen/TargetInfo.cpp
lib/CodeGen/TargetInfo.h
test/CodeGen/le32-libcall-pow.c [new file with mode: 0644]

index 7e224f9300434beba8b1a3fc22a71caa9549937b..2fd873ef568f8eb5ad579dd7494a55967b20c514 100644 (file)
@@ -1293,13 +1293,18 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
   case Builtin::BIpow:
   case Builtin::BIpowf:
   case Builtin::BIpowl: {
-    if (!FD->hasAttr<ConstAttr>())
-      break;
-    Value *Base = EmitScalarExpr(E->getArg(0));
-    Value *Exponent = EmitScalarExpr(E->getArg(1));
-    llvm::Type *ArgType = Base->getType();
-    Value *F = CGM.getIntrinsic(Intrinsic::pow, ArgType);
-    return RValue::get(Builder.CreateCall2(F, Base, Exponent));
+    // Transform a call to pow* into a @llvm.pow.* intrinsic call, but only
+    // if the target agrees.
+    if (getTargetHooks().emitIntrinsicForPow()) {
+      if (!FD->hasAttr<ConstAttr>())
+        break;
+      Value *Base = EmitScalarExpr(E->getArg(0));
+      Value *Exponent = EmitScalarExpr(E->getArg(1));
+      llvm::Type *ArgType = Base->getType();
+      Value *F = CGM.getIntrinsic(Intrinsic::pow, ArgType);
+      return RValue::get(Builder.CreateCall2(F, Base, Exponent));
+    }
+    break;
   }
 
   case Builtin::BIfma:
index bef0d058d6f3ff02fd45e5bc779ef3f285d32a72..fa9735705eba73ebe09a1ae01a15ea8d27992947 100644 (file)
@@ -443,6 +443,10 @@ class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
  public:
   PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
     : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
+
+  /// For PNaCl we don't want llvm.pow.* intrinsics to be emitted instead
+  /// of library function calls.
+  bool emitIntrinsicForPow() const { return false; }
 };
 
 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
index a7fb88649a3ea2a9b62d92e4151f4c250a1f1321..1be7f4dfa45920a6df0d9702976731b8c8bf70f6 100644 (file)
@@ -74,6 +74,10 @@ namespace clang {
     ///     through such registers.
     virtual bool extendPointerWithSExt() const { return false; }
 
+    /// Controls whether BIpow* emit an intrinsic call instead of a library
+    /// function call.
+    virtual bool emitIntrinsicForPow() const { return true; }
+
     /// Determines the DWARF register number for the stack pointer, for
     /// exception-handling purposes.  Implements __builtin_dwarf_sp_column.
     ///
diff --git a/test/CodeGen/le32-libcall-pow.c b/test/CodeGen/le32-libcall-pow.c
new file mode 100644 (file)
index 0000000..c2f892c
--- /dev/null
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -fmath-errno -emit-llvm -o - %s -triple le32-unknown-nacl | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -o - %s -triple le32-unknown-nacl | FileCheck %s
+
+// le32 (PNaCl) never generates intrinsics for pow calls, with or without errno
+
+// CHECK: define void @test_pow
+void test_pow(float a0, double a1, long double a2) {
+  // CHECK: call float @powf
+  float l0 = powf(a0, a0);
+
+  // CHECK: call double @pow
+  double l1 = pow(a1, a1);
+
+  // CHECK: call double @powl
+  long double l2 = powl(a2, a2);
+}
+
+// CHECK: declare float @powf(float, float)
+// CHECK: declare double @pow(double, double)
+// CHECK: declare double @powl(double, double)
+