LLVM API Documentation

FPMover.cpp

Go to the documentation of this file.
00001 //===-- FPMover.cpp - Sparc double-precision floating point move fixer ----===//
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 // Expand FpMOVD/FpABSD/FpNEGD instructions into their single-precision pieces.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "fpmover"
00015 #include "Sparc.h"
00016 #include "SparcSubtarget.h"
00017 #include "llvm/CodeGen/MachineFunctionPass.h"
00018 #include "llvm/CodeGen/MachineInstrBuilder.h"
00019 #include "llvm/Target/TargetMachine.h"
00020 #include "llvm/Target/TargetInstrInfo.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/Support/Debug.h"
00023 #include "llvm/Support/ErrorHandling.h"
00024 using namespace llvm;
00025 
00026 STATISTIC(NumFpDs , "Number of instructions translated");
00027 STATISTIC(NoopFpDs, "Number of noop instructions removed");
00028 
00029 namespace {
00030   struct FPMover : public MachineFunctionPass {
00031     /// Target machine description which we query for reg. names, data
00032     /// layout, etc.
00033     ///
00034     TargetMachine &TM;
00035     
00036     static char ID;
00037     explicit FPMover(TargetMachine &tm) 
00038       : MachineFunctionPass(&ID), TM(tm) { }
00039 
00040     virtual const char *getPassName() const {
00041       return "Sparc Double-FP Move Fixer";
00042     }
00043 
00044     bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
00045     bool runOnMachineFunction(MachineFunction &F);
00046   };
00047   char FPMover::ID = 0;
00048 } // end of anonymous namespace
00049 
00050 /// createSparcFPMoverPass - Returns a pass that turns FpMOVD
00051 /// instructions into FMOVS instructions
00052 ///
00053 FunctionPass *llvm::createSparcFPMoverPass(TargetMachine &tm) {
00054   return new FPMover(tm);
00055 }
00056 
00057 /// getDoubleRegPair - Given a DFP register, return the even and odd FP
00058 /// registers that correspond to it.
00059 static void getDoubleRegPair(unsigned DoubleReg, unsigned &EvenReg,
00060                              unsigned &OddReg) {
00061   static const unsigned EvenHalvesOfPairs[] = {
00062     SP::F0, SP::F2, SP::F4, SP::F6, SP::F8, SP::F10, SP::F12, SP::F14,
00063     SP::F16, SP::F18, SP::F20, SP::F22, SP::F24, SP::F26, SP::F28, SP::F30
00064   };
00065   static const unsigned OddHalvesOfPairs[] = {
00066     SP::F1, SP::F3, SP::F5, SP::F7, SP::F9, SP::F11, SP::F13, SP::F15,
00067     SP::F17, SP::F19, SP::F21, SP::F23, SP::F25, SP::F27, SP::F29, SP::F31
00068   };
00069   static const unsigned DoubleRegsInOrder[] = {
00070     SP::D0, SP::D1, SP::D2, SP::D3, SP::D4, SP::D5, SP::D6, SP::D7, SP::D8,
00071     SP::D9, SP::D10, SP::D11, SP::D12, SP::D13, SP::D14, SP::D15
00072   };
00073   for (unsigned i = 0; i < sizeof(DoubleRegsInOrder)/sizeof(unsigned); ++i)
00074     if (DoubleRegsInOrder[i] == DoubleReg) {
00075       EvenReg = EvenHalvesOfPairs[i];
00076       OddReg = OddHalvesOfPairs[i];
00077       return;
00078     }
00079   llvm_unreachable("Can't find reg");
00080 }
00081 
00082 /// runOnMachineBasicBlock - Fixup FpMOVD instructions in this MBB.
00083 ///
00084 bool FPMover::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
00085   bool Changed = false;
00086   for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
00087     MachineInstr *MI = I++;
00088     DebugLoc dl = MI->getDebugLoc();
00089     if (MI->getOpcode() == SP::FpMOVD || MI->getOpcode() == SP::FpABSD ||
00090         MI->getOpcode() == SP::FpNEGD) {
00091       Changed = true;
00092       unsigned DestDReg = MI->getOperand(0).getReg();
00093       unsigned SrcDReg  = MI->getOperand(1).getReg();
00094       if (DestDReg == SrcDReg && MI->getOpcode() == SP::FpMOVD) {
00095         MBB.erase(MI);   // Eliminate the noop copy.
00096         ++NoopFpDs;
00097         continue;
00098       }
00099       
00100       unsigned EvenSrcReg = 0, OddSrcReg = 0, EvenDestReg = 0, OddDestReg = 0;
00101       getDoubleRegPair(DestDReg, EvenDestReg, OddDestReg);
00102       getDoubleRegPair(SrcDReg, EvenSrcReg, OddSrcReg);
00103 
00104       const TargetInstrInfo *TII = TM.getInstrInfo();
00105       if (MI->getOpcode() == SP::FpMOVD)
00106         MI->setDesc(TII->get(SP::FMOVS));
00107       else if (MI->getOpcode() == SP::FpNEGD)
00108         MI->setDesc(TII->get(SP::FNEGS));
00109       else if (MI->getOpcode() == SP::FpABSD)
00110         MI->setDesc(TII->get(SP::FABSS));
00111       else
00112         llvm_unreachable("Unknown opcode!");
00113         
00114       MI->getOperand(0).setReg(EvenDestReg);
00115       MI->getOperand(1).setReg(EvenSrcReg);
00116       DOUT << "FPMover: the modified instr is: " << *MI;
00117       // Insert copy for the other half of the double.
00118       if (DestDReg != SrcDReg) {
00119         MI = BuildMI(MBB, I, dl, TM.getInstrInfo()->get(SP::FMOVS), OddDestReg)
00120           .addReg(OddSrcReg);
00121         DOUT << "FPMover: the inserted instr is: " << *MI;
00122       }
00123       ++NumFpDs;
00124     }
00125   }
00126   return Changed;
00127 }
00128 
00129 bool FPMover::runOnMachineFunction(MachineFunction &F) {
00130   // If the target has V9 instructions, the fp-mover pseudos will never be
00131   // emitted.  Avoid a scan of the instructions to improve compile time.
00132   if (TM.getSubtarget<SparcSubtarget>().isV9())
00133     return false;
00134   
00135   bool Changed = false;
00136   for (MachineFunction::iterator FI = F.begin(), FE = F.end();
00137        FI != FE; ++FI)
00138     Changed |= runOnMachineBasicBlock(*FI);
00139   return Changed;
00140 }