diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h b/llvm/include/llvm/CodeGen/MachineModuleInfo.h index 97b439c726b0a..dfa0e993ec06a 100644 --- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h +++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h @@ -116,10 +116,6 @@ class MachineModuleInfo { /// True if debugging information is available in this module. bool DbgInfoAvailable = false; - /// True if this module is being built for windows/msvc, and uses floating - /// point. This is used to emit an undefined reference to _fltused. - bool UsesMSVCFloatingPoint = false; - /// Maps IR Functions to their corresponding MachineFunctions. DenseMap> MachineFunctions; /// Next unique number available for a MachineFunction. @@ -189,10 +185,6 @@ class MachineModuleInfo { /// Returns true if valid debug info is present. bool hasDebugInfo() const { return DbgInfoAvailable; } - bool usesMSVCFloatingPoint() const { return UsesMSVCFloatingPoint; } - - void setUsesMSVCFloatingPoint(bool b) { UsesMSVCFloatingPoint = b; } - /// \name Exception Handling /// \{ diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp b/llvm/lib/CodeGen/MachineModuleInfo.cpp index 088e76029f1a3..150ab363c8fcd 100644 --- a/llvm/lib/CodeGen/MachineModuleInfo.cpp +++ b/llvm/lib/CodeGen/MachineModuleInfo.cpp @@ -28,7 +28,6 @@ void MachineModuleInfo::initialize() { ObjFileMMI = nullptr; CurCallSite = 0; NextFnNum = 0; - UsesMSVCFloatingPoint = false; DbgInfoAvailable = false; } diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index 401d23b22adcd..84331d257a3d0 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -417,30 +417,6 @@ void SelectionDAGISelLegacy::getAnalysisUsage(AnalysisUsage &AU) const { MachineFunctionPass::getAnalysisUsage(AU); } -static void computeUsesMSVCFloatingPoint(const Triple &TT, const Function &F, - MachineModuleInfo &MMI) { - // Only needed for MSVC - if (!TT.isWindowsMSVCEnvironment()) - return; - - // If it's already set, nothing to do. - if (MMI.usesMSVCFloatingPoint()) - return; - - for (const Instruction &I : instructions(F)) { - if (I.getType()->isFPOrFPVectorTy()) { - MMI.setUsesMSVCFloatingPoint(true); - return; - } - for (const auto &Op : I.operands()) { - if (Op->getType()->isFPOrFPVectorTy()) { - MMI.setUsesMSVCFloatingPoint(true); - return; - } - } - } -} - PreservedAnalyses SelectionDAGISelPass::run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM) { @@ -802,9 +778,6 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) { } } - // Determine if floating point is used for msvc - computeUsesMSVCFloatingPoint(TM.getTargetTriple(), Fn, *CurDAG->getMMI()); - // Release function-specific state. SDB and CurDAG are already cleared // at this point. FuncInfo->clear(); diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp b/llvm/lib/Target/X86/X86AsmPrinter.cpp index 0c2c6bf7f8b70..957eb21cf4f8c 100644 --- a/llvm/lib/Target/X86/X86AsmPrinter.cpp +++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp @@ -28,6 +28,7 @@ #include "llvm/CodeGenTypes/MachineValueType.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/InlineAsm.h" +#include "llvm/IR/InstIterator.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" @@ -975,6 +976,33 @@ static void emitNonLazyStubs(MachineModuleInfo *MMI, MCStreamer &OutStreamer) { } } +/// True if this module is being built for windows/msvc, and uses floating +/// point. This is used to emit an undefined reference to _fltused. This is +/// needed in Windows kernel or driver contexts to find and prevent code from +/// modifying non-GPR registers. +/// +/// TODO: It would be better if this was computed from MIR by looking for +/// selected floating-point instructions. +static bool usesMSVCFloatingPoint(const Triple &TT, const Module &M) { + // Only needed for MSVC + if (!TT.isWindowsMSVCEnvironment()) + return false; + + for (const Function &F : M) { + for (const Instruction &I : instructions(F)) { + if (I.getType()->isFPOrFPVectorTy()) + return true; + + for (const auto &Op : I.operands()) { + if (Op->getType()->isFPOrFPVectorTy()) + return true; + } + } + } + + return false; +} + void X86AsmPrinter::emitEndOfAsmFile(Module &M) { const Triple &TT = TM.getTargetTriple(); @@ -993,7 +1021,7 @@ void X86AsmPrinter::emitEndOfAsmFile(Module &M) { // safe to set. OutStreamer->emitAssemblerFlag(MCAF_SubsectionsViaSymbols); } else if (TT.isOSBinFormatCOFF()) { - if (MMI->usesMSVCFloatingPoint()) { + if (usesMSVCFloatingPoint(TT, M)) { // In Windows' libcmt.lib, there is a file which is linked in only if the // symbol _fltused is referenced. Linking this in causes some // side-effects: