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/CFG.h"
26 #include "llvm/ADT/Statistic.h"
29 STATISTIC(NumFPKill, "Number of FP_REG_KILL instructions added");
32 struct FPRegKiller : public MachineFunctionPass {
34 FPRegKiller() : MachineFunctionPass(&ID) {}
36 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.addPreservedID(MachineLoopInfoID);
39 AU.addPreservedID(MachineDominatorsID);
40 MachineFunctionPass::getAnalysisUsage(AU);
43 virtual bool runOnMachineFunction(MachineFunction &MF);
45 virtual const char *getPassName() const {
46 return "X86 FP_REG_KILL inserter";
49 char FPRegKiller::ID = 0;
52 FunctionPass *llvm::createX87FPRegKillInserterPass() {
53 return new FPRegKiller();
56 /// ContainsFPStackCode - Return true if the specific MBB has floating point
57 /// stack code, and thus needs an FP_REG_KILL.
58 static bool ContainsFPStackCode(MachineBasicBlock *MBB, unsigned SSELevel,
59 MachineRegisterInfo &MRI) {
60 // Scan the block, looking for instructions that define fp stack vregs.
61 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
63 if (I->getNumOperands() == 0 || !I->getOperand(0).isReg())
66 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
67 if (!I->getOperand(op).isReg() || !I->getOperand(op).isDef() ||
68 !TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()))
71 const TargetRegisterClass *RegClass =
72 MRI.getRegClass(I->getOperand(op).getReg());
74 switch (RegClass->getID())
75 case X86::RFP32RegClassID:
76 case X86::RFP64RegClassID:
77 case X86::RFP80RegClassID:
82 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
83 // a copy of the input value in this block. In SSE mode, we only care about
86 // Final check, check LLVM BB's that are successors to the LLVM BB
87 // corresponding to BB for FP PHI nodes.
88 const BasicBlock *LLVMBB = MBB->getBasicBlock();
89 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
92 for (BasicBlock::const_iterator II = SI->begin();
93 (PN = dyn_cast<PHINode>(II)); ++II) {
94 if (PN->getType()->isX86_FP80Ty() ||
95 (SSELevel == 0 && PN->getType()->isFloatingPointTy()) ||
96 (SSELevel < 2 && PN->getType()->isDoubleTy())) {
105 bool FPRegKiller::runOnMachineFunction(MachineFunction &MF) {
106 // If we are emitting FP stack code, scan the basic block to determine if this
107 // block defines any FP values. If so, put an FP_REG_KILL instruction before
108 // the terminator of the block.
110 // Note that FP stack instructions are used in all modes for long double,
111 // so we always need to do this check.
112 // Also note that it's possible for an FP stack register to be live across
113 // an instruction that produces multiple basic blocks (SSE CMOV) so we
114 // must check all the generated basic blocks.
116 // Scan all of the machine instructions in these MBBs, checking for FP
117 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
119 // Fast-path: If nothing is using the x87 registers, we don't need to do
121 MachineRegisterInfo &MRI = MF.getRegInfo();
122 if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() &&
123 MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() &&
124 MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty())
127 const X86Subtarget &Subtarget = MF.getTarget().getSubtarget<X86Subtarget>();
128 unsigned SSELevel = 0;
129 if (Subtarget.hasSSE2())
131 else if (Subtarget.hasSSE1())
134 bool Changed = false;
135 MachineFunction::iterator MBBI = MF.begin();
136 MachineFunction::iterator EndMBB = MF.end();
137 for (; MBBI != EndMBB; ++MBBI) {
138 MachineBasicBlock *MBB = MBBI;
140 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
141 // before the return.
143 MachineBasicBlock::iterator EndI = MBB->end();
145 if (EndI->getDesc().isReturn())
149 // If we find any FP stack code, emit the FP_REG_KILL instruction.
150 if (ContainsFPStackCode(MBB, SSELevel, MRI)) {
151 BuildMI(*MBB, MBBI->getFirstTerminator(), DebugLoc(),
152 MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL));