LLVM API Documentation

SparcInstrInfo.cpp

Go to the documentation of this file.
00001 //===- SparcInstrInfo.cpp - Sparc Instruction Information -------*- C++ -*-===//
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 contains the Sparc implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "SparcInstrInfo.h"
00015 #include "SparcSubtarget.h"
00016 #include "Sparc.h"
00017 #include "llvm/ADT/STLExtras.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/CodeGen/MachineInstrBuilder.h"
00020 #include "llvm/Support/ErrorHandling.h"
00021 #include "SparcGenInstrInfo.inc"
00022 using namespace llvm;
00023 
00024 SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
00025   : TargetInstrInfoImpl(SparcInsts, array_lengthof(SparcInsts)),
00026     RI(ST, *this), Subtarget(ST) {
00027 }
00028 
00029 static bool isZeroImm(const MachineOperand &op) {
00030   return op.isImm() && op.getImm() == 0;
00031 }
00032 
00033 /// Return true if the instruction is a register to register move and
00034 /// leave the source and dest operands in the passed parameters.
00035 ///
00036 bool SparcInstrInfo::isMoveInstr(const MachineInstr &MI,
00037                                  unsigned &SrcReg, unsigned &DstReg,
00038                                  unsigned &SrcSR, unsigned &DstSR) const {
00039   SrcSR = DstSR = 0; // No sub-registers.
00040 
00041   // We look for 3 kinds of patterns here:
00042   // or with G0 or 0
00043   // add with G0 or 0
00044   // fmovs or FpMOVD (pseudo double move).
00045   if (MI.getOpcode() == SP::ORrr || MI.getOpcode() == SP::ADDrr) {
00046     if (MI.getOperand(1).getReg() == SP::G0) {
00047       DstReg = MI.getOperand(0).getReg();
00048       SrcReg = MI.getOperand(2).getReg();
00049       return true;
00050     } else if (MI.getOperand(2).getReg() == SP::G0) {
00051       DstReg = MI.getOperand(0).getReg();
00052       SrcReg = MI.getOperand(1).getReg();
00053       return true;
00054     }
00055   } else if ((MI.getOpcode() == SP::ORri || MI.getOpcode() == SP::ADDri) &&
00056              isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isReg()) {
00057     DstReg = MI.getOperand(0).getReg();
00058     SrcReg = MI.getOperand(1).getReg();
00059     return true;
00060   } else if (MI.getOpcode() == SP::FMOVS || MI.getOpcode() == SP::FpMOVD ||
00061              MI.getOpcode() == SP::FMOVD) {
00062     SrcReg = MI.getOperand(1).getReg();
00063     DstReg = MI.getOperand(0).getReg();
00064     return true;
00065   }
00066   return false;
00067 }
00068 
00069 /// isLoadFromStackSlot - If the specified machine instruction is a direct
00070 /// load from a stack slot, return the virtual or physical register number of
00071 /// the destination along with the FrameIndex of the loaded stack slot.  If
00072 /// not, return 0.  This predicate must return 0 if the instruction has
00073 /// any side effects other than loading from the stack slot.
00074 unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
00075                                              int &FrameIndex) const {
00076   if (MI->getOpcode() == SP::LDri ||
00077       MI->getOpcode() == SP::LDFri ||
00078       MI->getOpcode() == SP::LDDFri) {
00079     if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
00080         MI->getOperand(2).getImm() == 0) {
00081       FrameIndex = MI->getOperand(1).getIndex();
00082       return MI->getOperand(0).getReg();
00083     }
00084   }
00085   return 0;
00086 }
00087 
00088 /// isStoreToStackSlot - If the specified machine instruction is a direct
00089 /// store to a stack slot, return the virtual or physical register number of
00090 /// the source reg along with the FrameIndex of the loaded stack slot.  If
00091 /// not, return 0.  This predicate must return 0 if the instruction has
00092 /// any side effects other than storing to the stack slot.
00093 unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
00094                                             int &FrameIndex) const {
00095   if (MI->getOpcode() == SP::STri ||
00096       MI->getOpcode() == SP::STFri ||
00097       MI->getOpcode() == SP::STDFri) {
00098     if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
00099         MI->getOperand(1).getImm() == 0) {
00100       FrameIndex = MI->getOperand(0).getIndex();
00101       return MI->getOperand(2).getReg();
00102     }
00103   }
00104   return 0;
00105 }
00106 
00107 unsigned
00108 SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
00109                              MachineBasicBlock *FBB,
00110                              const SmallVectorImpl<MachineOperand> &Cond)const{
00111   // FIXME this should probably take a DebugLoc argument
00112   DebugLoc dl = DebugLoc::getUnknownLoc();
00113   // Can only insert uncond branches so far.
00114   assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
00115   BuildMI(&MBB, dl, get(SP::BA)).addMBB(TBB);
00116   return 1;
00117 }
00118 
00119 bool SparcInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
00120                                   MachineBasicBlock::iterator I,
00121                                   unsigned DestReg, unsigned SrcReg,
00122                                   const TargetRegisterClass *DestRC,
00123                                   const TargetRegisterClass *SrcRC) const {
00124   if (DestRC != SrcRC) {
00125     // Not yet supported!
00126     return false;
00127   }
00128 
00129   DebugLoc DL = DebugLoc::getUnknownLoc();
00130   if (I != MBB.end()) DL = I->getDebugLoc();
00131 
00132   if (DestRC == SP::IntRegsRegisterClass)
00133     BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0).addReg(SrcReg);
00134   else if (DestRC == SP::FPRegsRegisterClass)
00135     BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg).addReg(SrcReg);
00136   else if (DestRC == SP::DFPRegsRegisterClass)
00137     BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD),DestReg)
00138       .addReg(SrcReg);
00139   else
00140     // Can't copy this register
00141     return false;
00142 
00143   return true;
00144 }
00145 
00146 void SparcInstrInfo::
00147 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00148                     unsigned SrcReg, bool isKill, int FI,
00149                     const TargetRegisterClass *RC) const {
00150   DebugLoc DL = DebugLoc::getUnknownLoc();
00151   if (I != MBB.end()) DL = I->getDebugLoc();
00152 
00153   // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
00154   if (RC == SP::IntRegsRegisterClass)
00155     BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
00156       .addReg(SrcReg, getKillRegState(isKill));
00157   else if (RC == SP::FPRegsRegisterClass)
00158     BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
00159       .addReg(SrcReg,  getKillRegState(isKill));
00160   else if (RC == SP::DFPRegsRegisterClass)
00161     BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
00162       .addReg(SrcReg,  getKillRegState(isKill));
00163   else
00164     llvm_unreachable("Can't store this register to stack slot");
00165 }
00166 
00167 void SparcInstrInfo::
00168 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00169                      unsigned DestReg, int FI,
00170                      const TargetRegisterClass *RC) const {
00171   DebugLoc DL = DebugLoc::getUnknownLoc();
00172   if (I != MBB.end()) DL = I->getDebugLoc();
00173 
00174   if (RC == SP::IntRegsRegisterClass)
00175     BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
00176   else if (RC == SP::FPRegsRegisterClass)
00177     BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
00178   else if (RC == SP::DFPRegsRegisterClass)
00179     BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
00180   else
00181     llvm_unreachable("Can't load this register from stack slot");
00182 }
00183 
00184 MachineInstr *SparcInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
00185                                                     MachineInstr* MI,
00186                                           const SmallVectorImpl<unsigned> &Ops,
00187                                                     int FI) const {
00188   if (Ops.size() != 1) return NULL;
00189 
00190   unsigned OpNum = Ops[0];
00191   bool isFloat = false;
00192   MachineInstr *NewMI = NULL;
00193   switch (MI->getOpcode()) {
00194   case SP::ORrr:
00195     if (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == SP::G0&&
00196         MI->getOperand(0).isReg() && MI->getOperand(2).isReg()) {
00197       if (OpNum == 0)    // COPY -> STORE
00198         NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::STri))
00199           .addFrameIndex(FI)
00200           .addImm(0)
00201           .addReg(MI->getOperand(2).getReg());
00202       else               // COPY -> LOAD
00203         NewMI = BuildMI(MF, MI->getDebugLoc(), get(SP::LDri),
00204                         MI->getOperand(0).getReg())
00205           .addFrameIndex(FI)
00206           .addImm(0);
00207     }
00208     break;
00209   case SP::FMOVS:
00210     isFloat = true;
00211     // FALLTHROUGH
00212   case SP::FMOVD:
00213     if (OpNum == 0) { // COPY -> STORE
00214       unsigned SrcReg = MI->getOperand(1).getReg();
00215       bool isKill = MI->getOperand(1).isKill();
00216       bool isUndef = MI->getOperand(1).isUndef();
00217       NewMI = BuildMI(MF, MI->getDebugLoc(),
00218                       get(isFloat ? SP::STFri : SP::STDFri))
00219         .addFrameIndex(FI)
00220         .addImm(0)
00221         .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef));
00222     } else {             // COPY -> LOAD
00223       unsigned DstReg = MI->getOperand(0).getReg();
00224       bool isDead = MI->getOperand(0).isDead();
00225       bool isUndef = MI->getOperand(0).isUndef();
00226       NewMI = BuildMI(MF, MI->getDebugLoc(),
00227                       get(isFloat ? SP::LDFri : SP::LDDFri))
00228         .addReg(DstReg, RegState::Define |
00229                 getDeadRegState(isDead) | getUndefRegState(isUndef))
00230         .addFrameIndex(FI)
00231         .addImm(0);
00232     }
00233     break;
00234   }
00235 
00236   return NewMI;
00237 }