X86-Windows: Emit an undefined global __fltused symbol when targeting Windows
if any floating point arguments are passed to an external function.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@116665 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/MachineModuleInfo.cpp b/lib/CodeGen/MachineModuleInfo.cpp
index 347d8c7..25dda0a 100644
--- a/lib/CodeGen/MachineModuleInfo.cpp
+++ b/lib/CodeGen/MachineModuleInfo.cpp
@@ -256,7 +256,8 @@
 MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI)
 : ImmutablePass(ID), Context(MAI),
   ObjFileMMI(0),
-  CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0), DbgInfoAvailable(false){
+  CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0), DbgInfoAvailable(false),
+  CallsExternalFunctionWithFloatingPointArguments(false) {
   // Always emit some info, by default "no personality" info.
   Personalities.push_back(NULL);
   AddrLabelSymbols = 0;
diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index ebc6d38..04c424f 100644
--- a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -15,6 +15,7 @@
 #include "SDNodeDbgValue.h"
 #include "SelectionDAGBuilder.h"
 #include "llvm/ADT/BitVector.h"
+#include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/Analysis/AliasAnalysis.h"
 #include "llvm/Analysis/ConstantFolding.h"
@@ -5019,6 +5020,25 @@
       }
     }
 
+    // See if any floating point values are being passed to this external
+    // function. This is used to emit an undefined reference to fltused on
+    // Windows.
+    if (!F->hasLocalLinkage() && F->hasName()) {
+      MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
+      for (unsigned i = 0, e = I.getNumArgOperands(); i != e &&
+                  !MMI.callsExternalFunctionWithFloatingPointArguments(); ++i) {
+        const Type* T = I.getArgOperand(i)->getType();
+        for (po_iterator<const Type*> i = po_begin(T),
+                                      e = po_end(T);
+                                      i != e; ++i) {
+          if (i->isFloatingPointTy()) {
+            MMI.setCallsExternalFunctionWithFloatingPointArguments(true);
+            break;
+          }
+        }
+      }
+    }
+
     // Check for well-known libc/libm calls.  If the function is internal, it
     // can't be a library call.
     if (!F->hasLocalLinkage() && F->hasName()) {
diff --git a/lib/Target/X86/X86AsmPrinter.cpp b/lib/Target/X86/X86AsmPrinter.cpp
index 85441b1..c4fd729 100644
--- a/lib/Target/X86/X86AsmPrinter.cpp
+++ b/lib/Target/X86/X86AsmPrinter.cpp
@@ -580,6 +580,13 @@
     OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
   }
 
+  if (Subtarget->isTargetWindows()
+   && !Subtarget->isTargetCygMing()
+   && MMI->callsExternalFunctionWithFloatingPointArguments()) {
+    MCSymbol *S = MMI->getContext().GetOrCreateSymbol(StringRef("__fltused"));
+    OutStreamer.EmitSymbolAttribute(S, MCSA_Global);
+  }
+
   if (Subtarget->isTargetCOFF()) {
     X86COFFMachineModuleInfo &COFFMMI =
       MMI->getObjFileInfo<X86COFFMachineModuleInfo>();