1 //===-- X86FloatingPoint.cpp - FP_REG_KILL inserter -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the pass which inserts FP_REG_KILL instructions.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "x86-codegen"
16 #include "X86InstrInfo.h"
17 #include "X86Subtarget.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/ADT/Statistic.h"
30 STATISTIC(NumFPKill
, "Number of FP_REG_KILL instructions added");
33 struct VISIBILITY_HIDDEN FPRegKiller
: public MachineFunctionPass
{
35 FPRegKiller() : MachineFunctionPass(&ID
) {}
37 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
39 AU
.addPreservedID(MachineLoopInfoID
);
40 AU
.addPreservedID(MachineDominatorsID
);
41 MachineFunctionPass::getAnalysisUsage(AU
);
44 virtual bool runOnMachineFunction(MachineFunction
&MF
);
46 virtual const char *getPassName() const { return "X86 FP_REG_KILL inserter"; }
48 char FPRegKiller::ID
= 0;
51 FunctionPass
*llvm::createX87FPRegKillInserterPass() { return new FPRegKiller(); }
53 bool FPRegKiller::runOnMachineFunction(MachineFunction
&MF
) {
54 // If we are emitting FP stack code, scan the basic block to determine if this
55 // block defines any FP values. If so, put an FP_REG_KILL instruction before
56 // the terminator of the block.
58 // Note that FP stack instructions are used in all modes for long double,
59 // so we always need to do this check.
60 // Also note that it's possible for an FP stack register to be live across
61 // an instruction that produces multiple basic blocks (SSE CMOV) so we
62 // must check all the generated basic blocks.
64 // Scan all of the machine instructions in these MBBs, checking for FP
65 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
67 // Fast-path: If nothing is using the x87 registers, we don't need to do
69 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
70 if (MRI
.getRegClassVirtRegs(X86::RFP80RegisterClass
).empty() &&
71 MRI
.getRegClassVirtRegs(X86::RFP64RegisterClass
).empty() &&
72 MRI
.getRegClassVirtRegs(X86::RFP32RegisterClass
).empty())
76 const X86Subtarget
&Subtarget
= MF
.getTarget().getSubtarget
<X86Subtarget
>();
77 MachineFunction::iterator MBBI
= MF
.begin();
78 MachineFunction::iterator EndMBB
= MF
.end();
79 for (; MBBI
!= EndMBB
; ++MBBI
) {
80 MachineBasicBlock
*MBB
= MBBI
;
82 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
85 MachineBasicBlock::iterator EndI
= MBB
->end();
87 if (EndI
->getDesc().isReturn())
91 bool ContainsFPCode
= false;
92 for (MachineBasicBlock::iterator I
= MBB
->begin(), E
= MBB
->end();
93 !ContainsFPCode
&& I
!= E
; ++I
) {
94 if (I
->getNumOperands() != 0 && I
->getOperand(0).isReg()) {
95 const TargetRegisterClass
*clas
;
96 for (unsigned op
= 0, e
= I
->getNumOperands(); op
!= e
; ++op
) {
97 if (I
->getOperand(op
).isReg() && I
->getOperand(op
).isDef() &&
98 TargetRegisterInfo::isVirtualRegister(I
->getOperand(op
).getReg()) &&
99 ((clas
= MRI
.getRegClass(I
->getOperand(op
).getReg())) ==
100 X86::RFP32RegisterClass
||
101 clas
== X86::RFP64RegisterClass
||
102 clas
== X86::RFP80RegisterClass
)) {
103 ContainsFPCode
= true;
109 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
110 // a copy of the input value in this block. In SSE mode, we only care about
112 if (!ContainsFPCode
) {
113 // Final check, check LLVM BB's that are successors to the LLVM BB
114 // corresponding to BB for FP PHI nodes.
115 const BasicBlock
*LLVMBB
= MBB
->getBasicBlock();
117 for (succ_const_iterator SI
= succ_begin(LLVMBB
), E
= succ_end(LLVMBB
);
118 !ContainsFPCode
&& SI
!= E
; ++SI
) {
119 for (BasicBlock::const_iterator II
= SI
->begin();
120 (PN
= dyn_cast
<PHINode
>(II
)); ++II
) {
121 if (PN
->getType()==Type::getX86_FP80Ty(LLVMBB
->getContext()) ||
122 (!Subtarget
.hasSSE1() && PN
->getType()->isFloatingPoint()) ||
123 (!Subtarget
.hasSSE2() &&
124 PN
->getType()==Type::getDoubleTy(LLVMBB
->getContext()))) {
125 ContainsFPCode
= true;
131 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
132 if (ContainsFPCode
) {
133 BuildMI(*MBB
, MBBI
->getFirstTerminator(), DebugLoc::getUnknownLoc(),
134 MF
.getTarget().getInstrInfo()->get(X86::FP_REG_KILL
));