| 1 | // X86InsertX87Wait.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 | |
| 21 | #include "X86.h" |
| 22 | #include "X86InstrInfo.h" |
| 23 | #include "X86Subtarget.h" |
| 24 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 25 | #include "llvm/CodeGen/MachineFunction.h" |
| 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | #include "llvm/CodeGen/MachineInstr.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 29 | #include "llvm/CodeGen/MachineOperand.h" |
| 30 | #include "llvm/IR/DebugLoc.h" |
| 31 | #include "llvm/Support/Debug.h" |
| 32 | |
| 33 | using namespace llvm; |
| 34 | |
| 35 | #define DEBUG_TYPE "x86-insert-x87-wait" |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | class X86InsertX87WaitLegacy : public MachineFunctionPass { |
| 40 | public: |
| 41 | static char ID; |
| 42 | |
| 43 | X86InsertX87WaitLegacy() : MachineFunctionPass(ID) {} |
| 44 | |
| 45 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 46 | |
| 47 | StringRef getPassName() const override { |
| 48 | return "X86 insert wait instruction" ; |
| 49 | } |
| 50 | }; |
| 51 | } // end anonymous namespace |
| 52 | |
| 53 | char X86InsertX87WaitLegacy::ID = 0; |
| 54 | |
| 55 | FunctionPass *llvm::createX86InsertX87WaitLegacyPass() { |
| 56 | return new X86InsertX87WaitLegacy(); |
| 57 | } |
| 58 | |
| 59 | // Classifies an x87 control instruction by whether it performs the implicit |
| 60 | // wait (FP exception sync); non-waiting FN-prefixed forms do not. |
| 61 | enum class X87ControlKind { NotControl, Waiting, NonWaiting }; |
| 62 | |
| 63 | static X87ControlKind classifyX87ControlInstruction(unsigned Opcode) { |
| 64 | switch (Opcode) { |
| 65 | default: |
| 66 | return X87ControlKind::NotControl; |
| 67 | case X86::FNINIT: |
| 68 | case X86::FNSTCW16m: |
| 69 | case X86::FNSTSW16r: |
| 70 | case X86::FNSTSWm: |
| 71 | case X86::FNCLEX: |
| 72 | case X86::FSTENVm: |
| 73 | case X86::FSAVEm: |
| 74 | return X87ControlKind::NonWaiting; |
| 75 | case X86::FLDCW16m: |
| 76 | case X86::FLDENVm: |
| 77 | case X86::FRSTORm: |
| 78 | case X86::FINCSTP: |
| 79 | case X86::FDECSTP: |
| 80 | case X86::FFREE: |
| 81 | case X86::FFREEP: |
| 82 | case X86::FNOP: |
| 83 | case X86::WAIT: |
| 84 | return X87ControlKind::Waiting; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static bool insertWaitInstruction(MachineFunction &MF) { |
| 89 | if (!MF.getFunction().hasFnAttribute(Kind: Attribute::StrictFP)) |
| 90 | return false; |
| 91 | |
| 92 | const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>(); |
| 93 | const X86InstrInfo *TII = ST.getInstrInfo(); |
| 94 | bool Changed = false; |
| 95 | |
| 96 | for (MachineBasicBlock &MBB : MF) { |
| 97 | for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) { |
| 98 | // Jump non X87 instruction. |
| 99 | if (!X86::isX87Instruction(MI&: *MI)) |
| 100 | continue; |
| 101 | // If the instruction instruction neither has float exception nor is |
| 102 | // a load/store instruction, or the instruction is x87 control |
| 103 | // instruction, do not insert wait. |
| 104 | if (!(MI->mayRaiseFPException() || MI->mayLoadOrStore()) || |
| 105 | classifyX87ControlInstruction(Opcode: MI->getOpcode()) != |
| 106 | X87ControlKind::NotControl) |
| 107 | continue; |
| 108 | // If the following instruction is an X87 instruction that performs the |
| 109 | // wait operation itself, we can omit inserting wait. Skip |
| 110 | // meta-instructions so the decision is independent of debug info, and |
| 111 | // keep the wait for non-waiting (FN-prefixed) successors. |
| 112 | MachineBasicBlock::iterator AfterMI = std::next(x: MI); |
| 113 | MachineBasicBlock::iterator NextMI = AfterMI; |
| 114 | while (NextMI != MBB.end() && NextMI->isMetaInstruction()) |
| 115 | ++NextMI; |
| 116 | if (NextMI != MBB.end() && X86::isX87Instruction(MI&: *NextMI) && |
| 117 | classifyX87ControlInstruction(Opcode: NextMI->getOpcode()) != |
| 118 | X87ControlKind::NonWaiting) |
| 119 | continue; |
| 120 | |
| 121 | BuildMI(BB&: MBB, I: AfterMI, MIMD: MI->getDebugLoc(), MCID: TII->get(Opcode: X86::WAIT)); |
| 122 | LLVM_DEBUG(dbgs() << "\nInsert wait after:\t" << *MI); |
| 123 | // Jump the newly inserting wait |
| 124 | ++MI; |
| 125 | Changed = true; |
| 126 | } |
| 127 | } |
| 128 | return Changed; |
| 129 | } |
| 130 | |
| 131 | bool X86InsertX87WaitLegacy::runOnMachineFunction(MachineFunction &MF) { |
| 132 | return insertWaitInstruction(MF); |
| 133 | } |
| 134 | |
| 135 | PreservedAnalyses X86InsertX87WaitPass::run(MachineFunction &MF, |
| 136 | MachineFunctionAnalysisManager &) { |
| 137 | return insertWaitInstruction(MF) ? getMachineFunctionPassPreservedAnalyses() |
| 138 | .preserveSet<CFGAnalyses>() |
| 139 | : PreservedAnalyses::all(); |
| 140 | } |
| 141 | |