1 //- X86Insertwait.cpp - Strict-Fp:Insert wait instruction X87 instructions --//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the pass which insert x86 wait instructions after each
10 // X87 instructions when strict float is enabled.
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.
19 //===----------------------------------------------------------------------===//
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"
35 #define DEBUG_TYPE "x86-insert-wait"
39 class WaitInsert
: public MachineFunctionPass
{
43 WaitInsert() : MachineFunctionPass(ID
) {}
45 bool runOnMachineFunction(MachineFunction
&MF
) override
;
47 StringRef
getPassName() const override
{
48 return "X86 insert wait instruction";
54 char WaitInsert::ID
= 0;
56 FunctionPass
*llvm::createX86InsertX87waitPass() { return new WaitInsert(); }
58 static bool isX87ControlInstruction(MachineInstr
&MI
) {
59 switch (MI
.getOpcode()) {
82 static bool isX87NonWaitingControlInstruction(MachineInstr
&MI
) {
83 // a few special control instructions don't perform a wait operation
84 switch (MI
.getOpcode()) {
96 bool WaitInsert::runOnMachineFunction(MachineFunction
&MF
) {
97 if (!MF
.getFunction().hasFnAttribute(Attribute::StrictFP
))
100 const X86Subtarget
&ST
= MF
.getSubtarget
<X86Subtarget
>();
101 const X86InstrInfo
*TII
= ST
.getInstrInfo();
102 bool Changed
= false;
104 for (MachineBasicBlock
&MBB
: MF
) {
105 for (MachineBasicBlock::iterator MI
= MBB
.begin(); MI
!= MBB
.end(); ++MI
) {
106 // Jump non X87 instruction.
107 if (!X86::isX87Instruction(*MI
))
109 // If the instruction instruction neither has float exception nor is
110 // a load/store instruction, or the instruction is x87 control
111 // instruction, do not insert wait.
112 if (!(MI
->mayRaiseFPException() || MI
->mayLoadOrStore()) ||
113 isX87ControlInstruction(*MI
))
115 // If the following instruction is an X87 instruction and isn't an X87
116 // non-waiting control instruction, we can omit insert wait instruction.
117 MachineBasicBlock::iterator AfterMI
= std::next(MI
);
118 if (AfterMI
!= MBB
.end() && X86::isX87Instruction(*AfterMI
) &&
119 !isX87NonWaitingControlInstruction(*AfterMI
))
122 BuildMI(MBB
, AfterMI
, MI
->getDebugLoc(), TII
->get(X86::WAIT
));
123 LLVM_DEBUG(dbgs() << "\nInsert wait after:\t" << *MI
);
124 // Jump the newly inserting wait