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