Add target hook CodeGen queries when generating builtin pow*.

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
diff --git a/lib/CodeGen/CGBuiltin.cpp b/lib/CodeGen/CGBuiltin.cpp
index 7e224f9..2fd873e 100644
--- a/lib/CodeGen/CGBuiltin.cpp
+++ b/lib/CodeGen/CGBuiltin.cpp
@@ -1293,13 +1293,18 @@
   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:
diff --git a/lib/CodeGen/TargetInfo.cpp b/lib/CodeGen/TargetInfo.cpp
index bef0d05..fa97357 100644
--- a/lib/CodeGen/TargetInfo.cpp
+++ b/lib/CodeGen/TargetInfo.cpp
@@ -443,6 +443,10 @@
  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 {
diff --git a/lib/CodeGen/TargetInfo.h b/lib/CodeGen/TargetInfo.h
index a7fb886..1be7f4d 100644
--- a/lib/CodeGen/TargetInfo.h
+++ b/lib/CodeGen/TargetInfo.h
@@ -74,6 +74,10 @@
     ///     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.
     ///