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 "llvm/Instructions.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/CFG.h"
25 #include "llvm/ADT/Statistic.h"
28 STATISTIC(NumFPKill, "Number of FP_REG_KILL instructions added");
31 struct FPRegKiller : public MachineFunctionPass {
33 FPRegKiller() : MachineFunctionPass(&ID) {}
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
37 AU.addPreservedID(MachineLoopInfoID);
38 AU.addPreservedID(MachineDominatorsID);
39 MachineFunctionPass::getAnalysisUsage(AU);
42 virtual bool runOnMachineFunction(MachineFunction &MF);
44 virtual const char *getPassName() const {
45 return "X86 FP_REG_KILL inserter";
48 char FPRegKiller::ID = 0;
51 FunctionPass *llvm::createX87FPRegKillInserterPass() {
52 return new FPRegKiller();
55 /// isFPStackVReg - Return true if the specified vreg is from a fp stack
57 static bool isFPStackVReg(unsigned RegNo, const MachineRegisterInfo &MRI) {
58 if (!TargetRegisterInfo::isVirtualRegister(RegNo))
61 switch (MRI.getRegClass(RegNo)->getID()) {
62 default: return false;
63 case X86::RFP32RegClassID:
64 case X86::RFP64RegClassID:
65 case X86::RFP80RegClassID:
71 /// ContainsFPStackCode - Return true if the specific MBB has floating point
72 /// stack code, and thus needs an FP_REG_KILL.
73 static bool ContainsFPStackCode(MachineBasicBlock *MBB,
74 const MachineRegisterInfo &MRI) {
75 // Scan the block, looking for instructions that define fp stack vregs.
76 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
78 if (I->getNumOperands() == 0 || !I->getOperand(0).isReg())
81 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
82 if (!I->getOperand(op).isReg() || !I->getOperand(op).isDef())
85 if (isFPStackVReg(I->getOperand(op).getReg(), MRI))
90 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
91 // a copy of the input value in this block, which is a definition of the
93 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
94 E = MBB->succ_end(); SI != E; ++ SI) {
95 MachineBasicBlock *SuccBB = *SI;
96 for (MachineBasicBlock::iterator I = SuccBB->begin(), E = SuccBB->end();
98 // All PHI nodes are at the top of the block.
99 if (!I->isPHI()) break;
101 if (isFPStackVReg(I->getOperand(0).getReg(), MRI))
109 bool FPRegKiller::runOnMachineFunction(MachineFunction &MF) {
110 // If we are emitting FP stack code, scan the basic block to determine if this
111 // block defines any FP values. If so, put an FP_REG_KILL instruction before
112 // the terminator of the block.
114 // Note that FP stack instructions are used in all modes for long double,
115 // so we always need to do this check.
116 // Also note that it's possible for an FP stack register to be live across
117 // an instruction that produces multiple basic blocks (SSE CMOV) so we
118 // must check all the generated basic blocks.
120 // Scan all of the machine instructions in these MBBs, checking for FP
121 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
123 // Fast-path: If nothing is using the x87 registers, we don't need to do
125 const MachineRegisterInfo &MRI = MF.getRegInfo();
126 if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() &&
127 MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() &&
128 MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty())
131 bool Changed = false;
132 MachineFunction::iterator MBBI = MF.begin();
133 MachineFunction::iterator EndMBB = MF.end();
134 for (; MBBI != EndMBB; ++MBBI) {
135 MachineBasicBlock *MBB = MBBI;
137 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
138 // before the return.
140 MachineBasicBlock::iterator EndI = MBB->end();
142 if (EndI->getDesc().isReturn())
146 // If we find any FP stack code, emit the FP_REG_KILL instruction.
147 if (ContainsFPStackCode(MBB, MRI)) {
148 BuildMI(*MBB, MBBI->getFirstTerminator(), DebugLoc(),
149 MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL));