X86InsertWait.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //- X86Insertwait.cpp - Strict-Fp:Insert wait instruction X87 instructions --//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the pass which insert x86 wait instructions after each
  10. // X87 instructions when strict float is enabled.
  11. //
  12. // The logic to insert a wait instruction after an X87 instruction is as below:
  13. // 1. If the X87 instruction don't raise float exception nor is a load/store
  14. // instruction, or is a x87 control instruction, don't insert wait.
  15. // 2. If the X87 instruction is an instruction which the following instruction
  16. // is an X87 exception synchronizing X87 instruction, don't insert wait.
  17. // 3. For other situations, insert wait instruction.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "X86.h"
  21. #include "X86InstrInfo.h"
  22. #include "X86Subtarget.h"
  23. #include "llvm/CodeGen/MachineBasicBlock.h"
  24. #include "llvm/CodeGen/MachineFunction.h"
  25. #include "llvm/CodeGen/MachineFunctionPass.h"
  26. #include "llvm/CodeGen/MachineInstr.h"
  27. #include "llvm/CodeGen/MachineInstrBuilder.h"
  28. #include "llvm/CodeGen/MachineOperand.h"
  29. #include "llvm/IR/DebugLoc.h"
  30. #include "llvm/Support/Debug.h"
  31. using namespace llvm;
  32. #define DEBUG_TYPE "x86-insert-wait"
  33. namespace {
  34. class WaitInsert : public MachineFunctionPass {
  35. public:
  36. static char ID;
  37. WaitInsert() : MachineFunctionPass(ID) {}
  38. bool runOnMachineFunction(MachineFunction &MF) override;
  39. StringRef getPassName() const override {
  40. return "X86 insert wait instruction";
  41. }
  42. };
  43. } // namespace
  44. char WaitInsert::ID = 0;
  45. FunctionPass *llvm::createX86InsertX87waitPass() { return new WaitInsert(); }
  46. static bool isX87ControlInstruction(MachineInstr &MI) {
  47. switch (MI.getOpcode()) {
  48. case X86::FNINIT:
  49. case X86::FLDCW16m:
  50. case X86::FNSTCW16m:
  51. case X86::FNSTSW16r:
  52. case X86::FNSTSWm:
  53. case X86::FNCLEX:
  54. case X86::FLDENVm:
  55. case X86::FSTENVm:
  56. case X86::FRSTORm:
  57. case X86::FSAVEm:
  58. case X86::FINCSTP:
  59. case X86::FDECSTP:
  60. case X86::FFREE:
  61. case X86::FFREEP:
  62. case X86::FNOP:
  63. case X86::WAIT:
  64. return true;
  65. default:
  66. return false;
  67. }
  68. }
  69. static bool isX87NonWaitingControlInstruction(MachineInstr &MI) {
  70. // a few special control instructions don't perform a wait operation
  71. switch (MI.getOpcode()) {
  72. case X86::FNINIT:
  73. case X86::FNSTSW16r:
  74. case X86::FNSTSWm:
  75. case X86::FNSTCW16m:
  76. case X86::FNCLEX:
  77. return true;
  78. default:
  79. return false;
  80. }
  81. }
  82. bool WaitInsert::runOnMachineFunction(MachineFunction &MF) {
  83. if (!MF.getFunction().hasFnAttribute(Attribute::StrictFP))
  84. return false;
  85. const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
  86. const X86InstrInfo *TII = ST.getInstrInfo();
  87. bool Changed = false;
  88. for (MachineBasicBlock &MBB : MF) {
  89. for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) {
  90. // Jump non X87 instruction.
  91. if (!X86::isX87Instruction(*MI))
  92. continue;
  93. // If the instruction instruction neither has float exception nor is
  94. // a load/store instruction, or the instruction is x87 control
  95. // instruction, do not insert wait.
  96. if (!(MI->mayRaiseFPException() || MI->mayLoadOrStore()) ||
  97. isX87ControlInstruction(*MI))
  98. continue;
  99. // If the following instruction is an X87 instruction and isn't an X87
  100. // non-waiting control instruction, we can omit insert wait instruction.
  101. MachineBasicBlock::iterator AfterMI = std::next(MI);
  102. if (AfterMI != MBB.end() && X86::isX87Instruction(*AfterMI) &&
  103. !isX87NonWaitingControlInstruction(*AfterMI))
  104. continue;
  105. BuildMI(MBB, AfterMI, MI->getDebugLoc(), TII->get(X86::WAIT));
  106. LLVM_DEBUG(dbgs() << "\nInsert wait after:\t" << *MI);
  107. // Jump the newly inserting wait
  108. ++MI;
  109. Changed = true;
  110. }
  111. }
  112. return Changed;
  113. }