LLVM API Documentation

X86FloatingPointRegKill.cpp

Go to the documentation of this file.
00001 //===-- X86FloatingPoint.cpp - FP_REG_KILL inserter -----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the pass which inserts FP_REG_KILL instructions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "x86-codegen"
00015 #include "X86.h"
00016 #include "X86InstrInfo.h"
00017 #include "X86Subtarget.h"
00018 #include "llvm/Instructions.h"
00019 #include "llvm/CodeGen/MachineFunctionPass.h"
00020 #include "llvm/CodeGen/MachineInstrBuilder.h"
00021 #include "llvm/CodeGen/MachineRegisterInfo.h"
00022 #include "llvm/CodeGen/Passes.h"
00023 #include "llvm/Target/TargetMachine.h"
00024 #include "llvm/Support/Debug.h"
00025 #include "llvm/Support/Compiler.h"
00026 #include "llvm/Support/CFG.h"
00027 #include "llvm/ADT/Statistic.h"
00028 using namespace llvm;
00029 
00030 STATISTIC(NumFPKill, "Number of FP_REG_KILL instructions added");
00031 
00032 namespace {
00033   struct VISIBILITY_HIDDEN FPRegKiller : public MachineFunctionPass {
00034     static char ID;
00035     FPRegKiller() : MachineFunctionPass(&ID) {}
00036 
00037     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00038       AU.addPreservedID(MachineLoopInfoID);
00039       AU.addPreservedID(MachineDominatorsID);
00040       MachineFunctionPass::getAnalysisUsage(AU);
00041     }
00042 
00043     virtual bool runOnMachineFunction(MachineFunction &MF);
00044 
00045     virtual const char *getPassName() const { return "X86 FP_REG_KILL inserter"; }
00046   };
00047   char FPRegKiller::ID = 0;
00048 }
00049 
00050 FunctionPass *llvm::createX87FPRegKillInserterPass() { return new FPRegKiller(); }
00051 
00052 bool FPRegKiller::runOnMachineFunction(MachineFunction &MF) {
00053   // If we are emitting FP stack code, scan the basic block to determine if this
00054   // block defines any FP values.  If so, put an FP_REG_KILL instruction before
00055   // the terminator of the block.
00056 
00057   // Note that FP stack instructions are used in all modes for long double,
00058   // so we always need to do this check.
00059   // Also note that it's possible for an FP stack register to be live across
00060   // an instruction that produces multiple basic blocks (SSE CMOV) so we
00061   // must check all the generated basic blocks.
00062 
00063   // Scan all of the machine instructions in these MBBs, checking for FP
00064   // stores.  (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
00065 
00066   // Fast-path: If nothing is using the x87 registers, we don't need to do
00067   // any scanning.
00068   MachineRegisterInfo &MRI = MF.getRegInfo();
00069   if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() &&
00070       MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() &&
00071       MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty())
00072     return false;
00073 
00074   bool Changed = false;
00075   const X86Subtarget &Subtarget = MF.getTarget().getSubtarget<X86Subtarget>();
00076   MachineFunction::iterator MBBI = MF.begin();
00077   MachineFunction::iterator EndMBB = MF.end();
00078   for (; MBBI != EndMBB; ++MBBI) {
00079     MachineBasicBlock *MBB = MBBI;
00080     
00081     // If this block returns, ignore it.  We don't want to insert an FP_REG_KILL
00082     // before the return.
00083     if (!MBB->empty()) {
00084       MachineBasicBlock::iterator EndI = MBB->end();
00085       --EndI;
00086       if (EndI->getDesc().isReturn())
00087         continue;
00088     }
00089     
00090     bool ContainsFPCode = false;
00091     for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
00092          !ContainsFPCode && I != E; ++I) {
00093       if (I->getNumOperands() != 0 && I->getOperand(0).isReg()) {
00094         const TargetRegisterClass *clas;
00095         for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
00096           if (I->getOperand(op).isReg() && I->getOperand(op).isDef() &&
00097             TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
00098               ((clas = MRI.getRegClass(I->getOperand(op).getReg())) == 
00099                  X86::RFP32RegisterClass ||
00100                clas == X86::RFP64RegisterClass ||
00101                clas == X86::RFP80RegisterClass)) {
00102             ContainsFPCode = true;
00103             break;
00104           }
00105         }
00106       }
00107     }
00108     // Check PHI nodes in successor blocks.  These PHI's will be lowered to have
00109     // a copy of the input value in this block.  In SSE mode, we only care about
00110     // 80-bit values.
00111     if (!ContainsFPCode) {
00112       // Final check, check LLVM BB's that are successors to the LLVM BB
00113       // corresponding to BB for FP PHI nodes.
00114       const BasicBlock *LLVMBB = MBB->getBasicBlock();
00115       const PHINode *PN;
00116       for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
00117            !ContainsFPCode && SI != E; ++SI) {
00118         for (BasicBlock::const_iterator II = SI->begin();
00119              (PN = dyn_cast<PHINode>(II)); ++II) {
00120           if (PN->getType()==Type::X86_FP80Ty ||
00121               (!Subtarget.hasSSE1() && PN->getType()->isFloatingPoint()) ||
00122               (!Subtarget.hasSSE2() && PN->getType()==Type::DoubleTy)) {
00123             ContainsFPCode = true;
00124             break;
00125           }
00126         }
00127       }
00128     }
00129     // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
00130     if (ContainsFPCode) {
00131       BuildMI(*MBB, MBBI->getFirstTerminator(),
00132               MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL));
00133       ++NumFPKill;
00134       Changed = true;
00135     }
00136   }
00137 
00138   return Changed;
00139 }