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.setPreservesCFG();
00039       AU.addPreservedID(MachineLoopInfoID);
00040       AU.addPreservedID(MachineDominatorsID);
00041       MachineFunctionPass::getAnalysisUsage(AU);
00042     }
00043 
00044     virtual bool runOnMachineFunction(MachineFunction &MF);
00045 
00046     virtual const char *getPassName() const { return "X86 FP_REG_KILL inserter"; }
00047   };
00048   char FPRegKiller::ID = 0;
00049 }
00050 
00051 FunctionPass *llvm::createX87FPRegKillInserterPass() { return new FPRegKiller(); }
00052 
00053 bool FPRegKiller::runOnMachineFunction(MachineFunction &MF) {
00054   // If we are emitting FP stack code, scan the basic block to determine if this
00055   // block defines any FP values.  If so, put an FP_REG_KILL instruction before
00056   // the terminator of the block.
00057 
00058   // Note that FP stack instructions are used in all modes for long double,
00059   // so we always need to do this check.
00060   // Also note that it's possible for an FP stack register to be live across
00061   // an instruction that produces multiple basic blocks (SSE CMOV) so we
00062   // must check all the generated basic blocks.
00063 
00064   // Scan all of the machine instructions in these MBBs, checking for FP
00065   // stores.  (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
00066 
00067   // Fast-path: If nothing is using the x87 registers, we don't need to do
00068   // any scanning.
00069   MachineRegisterInfo &MRI = MF.getRegInfo();
00070   if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() &&
00071       MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() &&
00072       MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty())
00073     return false;
00074 
00075   bool Changed = false;
00076   const X86Subtarget &Subtarget = MF.getTarget().getSubtarget<X86Subtarget>();
00077   MachineFunction::iterator MBBI = MF.begin();
00078   MachineFunction::iterator EndMBB = MF.end();
00079   for (; MBBI != EndMBB; ++MBBI) {
00080     MachineBasicBlock *MBB = MBBI;
00081     
00082     // If this block returns, ignore it.  We don't want to insert an FP_REG_KILL
00083     // before the return.
00084     if (!MBB->empty()) {
00085       MachineBasicBlock::iterator EndI = MBB->end();
00086       --EndI;
00087       if (EndI->getDesc().isReturn())
00088         continue;
00089     }
00090     
00091     bool ContainsFPCode = false;
00092     for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
00093          !ContainsFPCode && I != E; ++I) {
00094       if (I->getNumOperands() != 0 && I->getOperand(0).isReg()) {
00095         const TargetRegisterClass *clas;
00096         for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
00097           if (I->getOperand(op).isReg() && I->getOperand(op).isDef() &&
00098             TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
00099               ((clas = MRI.getRegClass(I->getOperand(op).getReg())) == 
00100                  X86::RFP32RegisterClass ||
00101                clas == X86::RFP64RegisterClass ||
00102                clas == X86::RFP80RegisterClass)) {
00103             ContainsFPCode = true;
00104             break;
00105           }
00106         }
00107       }
00108     }
00109     // Check PHI nodes in successor blocks.  These PHI's will be lowered to have
00110     // a copy of the input value in this block.  In SSE mode, we only care about
00111     // 80-bit values.
00112     if (!ContainsFPCode) {
00113       // Final check, check LLVM BB's that are successors to the LLVM BB
00114       // corresponding to BB for FP PHI nodes.
00115       const BasicBlock *LLVMBB = MBB->getBasicBlock();
00116       const PHINode *PN;
00117       for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
00118            !ContainsFPCode && SI != E; ++SI) {
00119         for (BasicBlock::const_iterator II = SI->begin();
00120              (PN = dyn_cast<PHINode>(II)); ++II) {
00121           if (PN->getType()==Type::getX86_FP80Ty(LLVMBB->getContext()) ||
00122               (!Subtarget.hasSSE1() && PN->getType()->isFloatingPoint()) ||
00123               (!Subtarget.hasSSE2() &&
00124                 PN->getType()==Type::getDoubleTy(LLVMBB->getContext()))) {
00125             ContainsFPCode = true;
00126             break;
00127           }
00128         }
00129       }
00130     }
00131     // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
00132     if (ContainsFPCode) {
00133       BuildMI(*MBB, MBBI->getFirstTerminator(), DebugLoc::getUnknownLoc(),
00134               MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL));
00135       ++NumFPKill;
00136       Changed = true;
00137     }
00138   }
00139 
00140   return Changed;
00141 }