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