LLVM API Documentation

MipsSEInstrInfo.cpp
Go to the documentation of this file.
00001 //===-- MipsSEInstrInfo.cpp - Mips32/64 Instruction Information -----------===//
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 Mips32/64 implementation of the TargetInstrInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "MipsSEInstrInfo.h"
00015 #include "InstPrinter/MipsInstPrinter.h"
00016 #include "MipsMachineFunction.h"
00017 #include "MipsTargetMachine.h"
00018 #include "llvm/ADT/STLExtras.h"
00019 #include "llvm/CodeGen/MachineInstrBuilder.h"
00020 #include "llvm/CodeGen/MachineRegisterInfo.h"
00021 #include "llvm/Support/CommandLine.h"
00022 #include "llvm/Support/ErrorHandling.h"
00023 #include "llvm/Support/TargetRegistry.h"
00024 
00025 using namespace llvm;
00026 
00027 MipsSEInstrInfo::MipsSEInstrInfo(const MipsSubtarget &STI)
00028     : MipsInstrInfo(STI, STI.getRelocationModel() == Reloc::PIC_ ? Mips::B
00029                                                                  : Mips::J),
00030       RI(STI), IsN64(STI.isABI_N64()) {}
00031 
00032 const MipsRegisterInfo &MipsSEInstrInfo::getRegisterInfo() const {
00033   return RI;
00034 }
00035 
00036 /// isLoadFromStackSlot - If the specified machine instruction is a direct
00037 /// load from a stack slot, return the virtual or physical register number of
00038 /// the destination along with the FrameIndex of the loaded stack slot.  If
00039 /// not, return 0.  This predicate must return 0 if the instruction has
00040 /// any side effects other than loading from the stack slot.
00041 unsigned MipsSEInstrInfo::
00042 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
00043 {
00044   unsigned Opc = MI->getOpcode();
00045 
00046   if ((Opc == Mips::LW)   || (Opc == Mips::LD)   ||
00047       (Opc == Mips::LWC1) || (Opc == Mips::LDC1) || (Opc == Mips::LDC164)) {
00048     if ((MI->getOperand(1).isFI()) && // is a stack slot
00049         (MI->getOperand(2).isImm()) &&  // the imm is zero
00050         (isZeroImm(MI->getOperand(2)))) {
00051       FrameIndex = MI->getOperand(1).getIndex();
00052       return MI->getOperand(0).getReg();
00053     }
00054   }
00055 
00056   return 0;
00057 }
00058 
00059 /// isStoreToStackSlot - If the specified machine instruction is a direct
00060 /// store to a stack slot, return the virtual or physical register number of
00061 /// the source reg along with the FrameIndex of the loaded stack slot.  If
00062 /// not, return 0.  This predicate must return 0 if the instruction has
00063 /// any side effects other than storing to the stack slot.
00064 unsigned MipsSEInstrInfo::
00065 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
00066 {
00067   unsigned Opc = MI->getOpcode();
00068 
00069   if ((Opc == Mips::SW)   || (Opc == Mips::SD)   ||
00070       (Opc == Mips::SWC1) || (Opc == Mips::SDC1) || (Opc == Mips::SDC164)) {
00071     if ((MI->getOperand(1).isFI()) && // is a stack slot
00072         (MI->getOperand(2).isImm()) &&  // the imm is zero
00073         (isZeroImm(MI->getOperand(2)))) {
00074       FrameIndex = MI->getOperand(1).getIndex();
00075       return MI->getOperand(0).getReg();
00076     }
00077   }
00078   return 0;
00079 }
00080 
00081 void MipsSEInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
00082                                   MachineBasicBlock::iterator I, DebugLoc DL,
00083                                   unsigned DestReg, unsigned SrcReg,
00084                                   bool KillSrc) const {
00085   unsigned Opc = 0, ZeroReg = 0;
00086   bool isMicroMips = Subtarget.inMicroMipsMode();
00087 
00088   if (Mips::GPR32RegClass.contains(DestReg)) { // Copy to CPU Reg.
00089     if (Mips::GPR32RegClass.contains(SrcReg)) {
00090       if (isMicroMips)
00091         Opc = Mips::MOVE16_MM;
00092       else
00093         Opc = Mips::ADDu, ZeroReg = Mips::ZERO;
00094     } else if (Mips::CCRRegClass.contains(SrcReg))
00095       Opc = Mips::CFC1;
00096     else if (Mips::FGR32RegClass.contains(SrcReg))
00097       Opc = Mips::MFC1;
00098     else if (Mips::HI32RegClass.contains(SrcReg)) {
00099       Opc = isMicroMips ? Mips::MFHI16_MM : Mips::MFHI;
00100       SrcReg = 0;
00101     } else if (Mips::LO32RegClass.contains(SrcReg)) {
00102       Opc = isMicroMips ? Mips::MFLO16_MM : Mips::MFLO;
00103       SrcReg = 0;
00104     } else if (Mips::HI32DSPRegClass.contains(SrcReg))
00105       Opc = Mips::MFHI_DSP;
00106     else if (Mips::LO32DSPRegClass.contains(SrcReg))
00107       Opc = Mips::MFLO_DSP;
00108     else if (Mips::DSPCCRegClass.contains(SrcReg)) {
00109       BuildMI(MBB, I, DL, get(Mips::RDDSP), DestReg).addImm(1 << 4)
00110         .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
00111       return;
00112     }
00113     else if (Mips::MSACtrlRegClass.contains(SrcReg))
00114       Opc = Mips::CFCMSA;
00115   }
00116   else if (Mips::GPR32RegClass.contains(SrcReg)) { // Copy from CPU Reg.
00117     if (Mips::CCRRegClass.contains(DestReg))
00118       Opc = Mips::CTC1;
00119     else if (Mips::FGR32RegClass.contains(DestReg))
00120       Opc = Mips::MTC1;
00121     else if (Mips::HI32RegClass.contains(DestReg))
00122       Opc = Mips::MTHI, DestReg = 0;
00123     else if (Mips::LO32RegClass.contains(DestReg))
00124       Opc = Mips::MTLO, DestReg = 0;
00125     else if (Mips::HI32DSPRegClass.contains(DestReg))
00126       Opc = Mips::MTHI_DSP;
00127     else if (Mips::LO32DSPRegClass.contains(DestReg))
00128       Opc = Mips::MTLO_DSP;
00129     else if (Mips::DSPCCRegClass.contains(DestReg)) {
00130       BuildMI(MBB, I, DL, get(Mips::WRDSP))
00131         .addReg(SrcReg, getKillRegState(KillSrc)).addImm(1 << 4)
00132         .addReg(DestReg, RegState::ImplicitDefine);
00133       return;
00134     }
00135     else if (Mips::MSACtrlRegClass.contains(DestReg))
00136       Opc = Mips::CTCMSA;
00137   }
00138   else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
00139     Opc = Mips::FMOV_S;
00140   else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
00141     Opc = Mips::FMOV_D32;
00142   else if (Mips::FGR64RegClass.contains(DestReg, SrcReg))
00143     Opc = Mips::FMOV_D64;
00144   else if (Mips::GPR64RegClass.contains(DestReg)) { // Copy to CPU64 Reg.
00145     if (Mips::GPR64RegClass.contains(SrcReg))
00146       Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64;
00147     else if (Mips::HI64RegClass.contains(SrcReg))
00148       Opc = Mips::MFHI64, SrcReg = 0;
00149     else if (Mips::LO64RegClass.contains(SrcReg))
00150       Opc = Mips::MFLO64, SrcReg = 0;
00151     else if (Mips::FGR64RegClass.contains(SrcReg))
00152       Opc = Mips::DMFC1;
00153   }
00154   else if (Mips::GPR64RegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
00155     if (Mips::HI64RegClass.contains(DestReg))
00156       Opc = Mips::MTHI64, DestReg = 0;
00157     else if (Mips::LO64RegClass.contains(DestReg))
00158       Opc = Mips::MTLO64, DestReg = 0;
00159     else if (Mips::FGR64RegClass.contains(DestReg))
00160       Opc = Mips::DMTC1;
00161   }
00162   else if (Mips::MSA128BRegClass.contains(DestReg)) { // Copy to MSA reg
00163     if (Mips::MSA128BRegClass.contains(SrcReg))
00164       Opc = Mips::MOVE_V;
00165   }
00166 
00167   assert(Opc && "Cannot copy registers");
00168 
00169   MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
00170 
00171   if (DestReg)
00172     MIB.addReg(DestReg, RegState::Define);
00173 
00174   if (SrcReg)
00175     MIB.addReg(SrcReg, getKillRegState(KillSrc));
00176 
00177   if (ZeroReg)
00178     MIB.addReg(ZeroReg);
00179 }
00180 
00181 void MipsSEInstrInfo::
00182 storeRegToStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00183                 unsigned SrcReg, bool isKill, int FI,
00184                 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI,
00185                 int64_t Offset) const {
00186   DebugLoc DL;
00187   if (I != MBB.end()) DL = I->getDebugLoc();
00188   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
00189 
00190   unsigned Opc = 0;
00191 
00192   if (Mips::GPR32RegClass.hasSubClassEq(RC))
00193     Opc = Mips::SW;
00194   else if (Mips::GPR64RegClass.hasSubClassEq(RC))
00195     Opc = Mips::SD;
00196   else if (Mips::ACC64RegClass.hasSubClassEq(RC))
00197     Opc = Mips::STORE_ACC64;
00198   else if (Mips::ACC64DSPRegClass.hasSubClassEq(RC))
00199     Opc = Mips::STORE_ACC64DSP;
00200   else if (Mips::ACC128RegClass.hasSubClassEq(RC))
00201     Opc = Mips::STORE_ACC128;
00202   else if (Mips::DSPCCRegClass.hasSubClassEq(RC))
00203     Opc = Mips::STORE_CCOND_DSP;
00204   else if (Mips::FGR32RegClass.hasSubClassEq(RC))
00205     Opc = Mips::SWC1;
00206   else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
00207     Opc = Mips::SDC1;
00208   else if (Mips::FGR64RegClass.hasSubClassEq(RC))
00209     Opc = Mips::SDC164;
00210   else if (RC->hasType(MVT::v16i8))
00211     Opc = Mips::ST_B;
00212   else if (RC->hasType(MVT::v8i16) || RC->hasType(MVT::v8f16))
00213     Opc = Mips::ST_H;
00214   else if (RC->hasType(MVT::v4i32) || RC->hasType(MVT::v4f32))
00215     Opc = Mips::ST_W;
00216   else if (RC->hasType(MVT::v2i64) || RC->hasType(MVT::v2f64))
00217     Opc = Mips::ST_D;
00218 
00219   assert(Opc && "Register class not handled!");
00220   BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
00221     .addFrameIndex(FI).addImm(Offset).addMemOperand(MMO);
00222 }
00223 
00224 void MipsSEInstrInfo::
00225 loadRegFromStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
00226                  unsigned DestReg, int FI, const TargetRegisterClass *RC,
00227                  const TargetRegisterInfo *TRI, int64_t Offset) const {
00228   DebugLoc DL;
00229   if (I != MBB.end()) DL = I->getDebugLoc();
00230   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
00231   unsigned Opc = 0;
00232 
00233   if (Mips::GPR32RegClass.hasSubClassEq(RC))
00234     Opc = Mips::LW;
00235   else if (Mips::GPR64RegClass.hasSubClassEq(RC))
00236     Opc = Mips::LD;
00237   else if (Mips::ACC64RegClass.hasSubClassEq(RC))
00238     Opc = Mips::LOAD_ACC64;
00239   else if (Mips::ACC64DSPRegClass.hasSubClassEq(RC))
00240     Opc = Mips::LOAD_ACC64DSP;
00241   else if (Mips::ACC128RegClass.hasSubClassEq(RC))
00242     Opc = Mips::LOAD_ACC128;
00243   else if (Mips::DSPCCRegClass.hasSubClassEq(RC))
00244     Opc = Mips::LOAD_CCOND_DSP;
00245   else if (Mips::FGR32RegClass.hasSubClassEq(RC))
00246     Opc = Mips::LWC1;
00247   else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
00248     Opc = Mips::LDC1;
00249   else if (Mips::FGR64RegClass.hasSubClassEq(RC))
00250     Opc = Mips::LDC164;
00251   else if (RC->hasType(MVT::v16i8))
00252     Opc = Mips::LD_B;
00253   else if (RC->hasType(MVT::v8i16) || RC->hasType(MVT::v8f16))
00254     Opc = Mips::LD_H;
00255   else if (RC->hasType(MVT::v4i32) || RC->hasType(MVT::v4f32))
00256     Opc = Mips::LD_W;
00257   else if (RC->hasType(MVT::v2i64) || RC->hasType(MVT::v2f64))
00258     Opc = Mips::LD_D;
00259 
00260   assert(Opc && "Register class not handled!");
00261   BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(Offset)
00262     .addMemOperand(MMO);
00263 }
00264 
00265 bool MipsSEInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
00266   MachineBasicBlock &MBB = *MI->getParent();
00267   bool isMicroMips = Subtarget.inMicroMipsMode();
00268   unsigned Opc;
00269 
00270   switch(MI->getDesc().getOpcode()) {
00271   default:
00272     return false;
00273   case Mips::RetRA:
00274     expandRetRA(MBB, MI);
00275     break;
00276   case Mips::PseudoMFHI:
00277     Opc = isMicroMips ? Mips::MFHI16_MM : Mips::MFHI;
00278     expandPseudoMFHiLo(MBB, MI, Opc);
00279     break;
00280   case Mips::PseudoMFLO:
00281     Opc = isMicroMips ? Mips::MFLO16_MM : Mips::MFLO;
00282     expandPseudoMFHiLo(MBB, MI, Opc);
00283     break;
00284   case Mips::PseudoMFHI64:
00285     expandPseudoMFHiLo(MBB, MI, Mips::MFHI64);
00286     break;
00287   case Mips::PseudoMFLO64:
00288     expandPseudoMFHiLo(MBB, MI, Mips::MFLO64);
00289     break;
00290   case Mips::PseudoMTLOHI:
00291     expandPseudoMTLoHi(MBB, MI, Mips::MTLO, Mips::MTHI, false);
00292     break;
00293   case Mips::PseudoMTLOHI64:
00294     expandPseudoMTLoHi(MBB, MI, Mips::MTLO64, Mips::MTHI64, false);
00295     break;
00296   case Mips::PseudoMTLOHI_DSP:
00297     expandPseudoMTLoHi(MBB, MI, Mips::MTLO_DSP, Mips::MTHI_DSP, true);
00298     break;
00299   case Mips::PseudoCVT_S_W:
00300     expandCvtFPInt(MBB, MI, Mips::CVT_S_W, Mips::MTC1, false);
00301     break;
00302   case Mips::PseudoCVT_D32_W:
00303     expandCvtFPInt(MBB, MI, Mips::CVT_D32_W, Mips::MTC1, false);
00304     break;
00305   case Mips::PseudoCVT_S_L:
00306     expandCvtFPInt(MBB, MI, Mips::CVT_S_L, Mips::DMTC1, true);
00307     break;
00308   case Mips::PseudoCVT_D64_W:
00309     expandCvtFPInt(MBB, MI, Mips::CVT_D64_W, Mips::MTC1, true);
00310     break;
00311   case Mips::PseudoCVT_D64_L:
00312     expandCvtFPInt(MBB, MI, Mips::CVT_D64_L, Mips::DMTC1, true);
00313     break;
00314   case Mips::BuildPairF64:
00315     expandBuildPairF64(MBB, MI, false);
00316     break;
00317   case Mips::BuildPairF64_64:
00318     expandBuildPairF64(MBB, MI, true);
00319     break;
00320   case Mips::ExtractElementF64:
00321     expandExtractElementF64(MBB, MI, false);
00322     break;
00323   case Mips::ExtractElementF64_64:
00324     expandExtractElementF64(MBB, MI, true);
00325     break;
00326   case Mips::MIPSeh_return32:
00327   case Mips::MIPSeh_return64:
00328     expandEhReturn(MBB, MI);
00329     break;
00330   }
00331 
00332   MBB.erase(MI);
00333   return true;
00334 }
00335 
00336 /// getOppositeBranchOpc - Return the inverse of the specified
00337 /// opcode, e.g. turning BEQ to BNE.
00338 unsigned MipsSEInstrInfo::getOppositeBranchOpc(unsigned Opc) const {
00339   switch (Opc) {
00340   default:           llvm_unreachable("Illegal opcode!");
00341   case Mips::BEQ:    return Mips::BNE;
00342   case Mips::BNE:    return Mips::BEQ;
00343   case Mips::BGTZ:   return Mips::BLEZ;
00344   case Mips::BGEZ:   return Mips::BLTZ;
00345   case Mips::BLTZ:   return Mips::BGEZ;
00346   case Mips::BLEZ:   return Mips::BGTZ;
00347   case Mips::BEQ64:  return Mips::BNE64;
00348   case Mips::BNE64:  return Mips::BEQ64;
00349   case Mips::BGTZ64: return Mips::BLEZ64;
00350   case Mips::BGEZ64: return Mips::BLTZ64;
00351   case Mips::BLTZ64: return Mips::BGEZ64;
00352   case Mips::BLEZ64: return Mips::BGTZ64;
00353   case Mips::BC1T:   return Mips::BC1F;
00354   case Mips::BC1F:   return Mips::BC1T;
00355   }
00356 }
00357 
00358 /// Adjust SP by Amount bytes.
00359 void MipsSEInstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
00360                                      MachineBasicBlock &MBB,
00361                                      MachineBasicBlock::iterator I) const {
00362   const MipsSubtarget &STI = Subtarget;
00363   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
00364   unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
00365   unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
00366 
00367   if (isInt<16>(Amount))// addi sp, sp, amount
00368     BuildMI(MBB, I, DL, get(ADDiu), SP).addReg(SP).addImm(Amount);
00369   else { // Expand immediate that doesn't fit in 16-bit.
00370     unsigned Reg = loadImmediate(Amount, MBB, I, DL, nullptr);
00371     BuildMI(MBB, I, DL, get(ADDu), SP).addReg(SP).addReg(Reg, RegState::Kill);
00372   }
00373 }
00374 
00375 /// This function generates the sequence of instructions needed to get the
00376 /// result of adding register REG and immediate IMM.
00377 unsigned
00378 MipsSEInstrInfo::loadImmediate(int64_t Imm, MachineBasicBlock &MBB,
00379                                MachineBasicBlock::iterator II, DebugLoc DL,
00380                                unsigned *NewImm) const {
00381   MipsAnalyzeImmediate AnalyzeImm;
00382   const MipsSubtarget &STI = Subtarget;
00383   MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
00384   unsigned Size = STI.isABI_N64() ? 64 : 32;
00385   unsigned LUi = STI.isABI_N64() ? Mips::LUi64 : Mips::LUi;
00386   unsigned ZEROReg = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
00387   const TargetRegisterClass *RC = STI.isABI_N64() ?
00388     &Mips::GPR64RegClass : &Mips::GPR32RegClass;
00389   bool LastInstrIsADDiu = NewImm;
00390 
00391   const MipsAnalyzeImmediate::InstSeq &Seq =
00392     AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu);
00393   MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
00394 
00395   assert(Seq.size() && (!LastInstrIsADDiu || (Seq.size() > 1)));
00396 
00397   // The first instruction can be a LUi, which is different from other
00398   // instructions (ADDiu, ORI and SLL) in that it does not have a register
00399   // operand.
00400   unsigned Reg = RegInfo.createVirtualRegister(RC);
00401 
00402   if (Inst->Opc == LUi)
00403     BuildMI(MBB, II, DL, get(LUi), Reg).addImm(SignExtend64<16>(Inst->ImmOpnd));
00404   else
00405     BuildMI(MBB, II, DL, get(Inst->Opc), Reg).addReg(ZEROReg)
00406       .addImm(SignExtend64<16>(Inst->ImmOpnd));
00407 
00408   // Build the remaining instructions in Seq.
00409   for (++Inst; Inst != Seq.end() - LastInstrIsADDiu; ++Inst)
00410     BuildMI(MBB, II, DL, get(Inst->Opc), Reg).addReg(Reg, RegState::Kill)
00411       .addImm(SignExtend64<16>(Inst->ImmOpnd));
00412 
00413   if (LastInstrIsADDiu)
00414     *NewImm = Inst->ImmOpnd;
00415 
00416   return Reg;
00417 }
00418 
00419 unsigned MipsSEInstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
00420   return (Opc == Mips::BEQ    || Opc == Mips::BNE    || Opc == Mips::BGTZ   ||
00421           Opc == Mips::BGEZ   || Opc == Mips::BLTZ   || Opc == Mips::BLEZ   ||
00422           Opc == Mips::BEQ64  || Opc == Mips::BNE64  || Opc == Mips::BGTZ64 ||
00423           Opc == Mips::BGEZ64 || Opc == Mips::BLTZ64 || Opc == Mips::BLEZ64 ||
00424           Opc == Mips::BC1T   || Opc == Mips::BC1F   || Opc == Mips::B      ||
00425           Opc == Mips::J) ?
00426          Opc : 0;
00427 }
00428 
00429 void MipsSEInstrInfo::expandRetRA(MachineBasicBlock &MBB,
00430                                   MachineBasicBlock::iterator I) const {
00431   if (Subtarget.isGP64bit())
00432     BuildMI(MBB, I, I->getDebugLoc(), get(Mips::PseudoReturn64))
00433         .addReg(Mips::RA_64);
00434   else
00435     BuildMI(MBB, I, I->getDebugLoc(), get(Mips::PseudoReturn)).addReg(Mips::RA);
00436 }
00437 
00438 std::pair<bool, bool>
00439 MipsSEInstrInfo::compareOpndSize(unsigned Opc,
00440                                  const MachineFunction &MF) const {
00441   const MCInstrDesc &Desc = get(Opc);
00442   assert(Desc.NumOperands == 2 && "Unary instruction expected.");
00443   const MipsRegisterInfo *RI = &getRegisterInfo();
00444   unsigned DstRegSize = getRegClass(Desc, 0, RI, MF)->getSize();
00445   unsigned SrcRegSize = getRegClass(Desc, 1, RI, MF)->getSize();
00446 
00447   return std::make_pair(DstRegSize > SrcRegSize, DstRegSize < SrcRegSize);
00448 }
00449 
00450 void MipsSEInstrInfo::expandPseudoMFHiLo(MachineBasicBlock &MBB,
00451                                          MachineBasicBlock::iterator I,
00452                                          unsigned NewOpc) const {
00453   BuildMI(MBB, I, I->getDebugLoc(), get(NewOpc), I->getOperand(0).getReg());
00454 }
00455 
00456 void MipsSEInstrInfo::expandPseudoMTLoHi(MachineBasicBlock &MBB,
00457                                          MachineBasicBlock::iterator I,
00458                                          unsigned LoOpc,
00459                                          unsigned HiOpc,
00460                                          bool HasExplicitDef) const {
00461   // Expand
00462   //  lo_hi pseudomtlohi $gpr0, $gpr1
00463   // to these two instructions:
00464   //  mtlo $gpr0
00465   //  mthi $gpr1
00466 
00467   DebugLoc DL = I->getDebugLoc();
00468   const MachineOperand &SrcLo = I->getOperand(1), &SrcHi = I->getOperand(2);
00469   MachineInstrBuilder LoInst = BuildMI(MBB, I, DL, get(LoOpc));
00470   MachineInstrBuilder HiInst = BuildMI(MBB, I, DL, get(HiOpc));
00471   LoInst.addReg(SrcLo.getReg(), getKillRegState(SrcLo.isKill()));
00472   HiInst.addReg(SrcHi.getReg(), getKillRegState(SrcHi.isKill()));
00473 
00474   // Add lo/hi registers if the mtlo/hi instructions created have explicit
00475   // def registers.
00476   if (HasExplicitDef) {
00477     unsigned DstReg = I->getOperand(0).getReg();
00478     unsigned DstLo = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
00479     unsigned DstHi = getRegisterInfo().getSubReg(DstReg, Mips::sub_hi);
00480     LoInst.addReg(DstLo, RegState::Define);
00481     HiInst.addReg(DstHi, RegState::Define);
00482   }
00483 }
00484 
00485 void MipsSEInstrInfo::expandCvtFPInt(MachineBasicBlock &MBB,
00486                                      MachineBasicBlock::iterator I,
00487                                      unsigned CvtOpc, unsigned MovOpc,
00488                                      bool IsI64) const {
00489   const MCInstrDesc &CvtDesc = get(CvtOpc), &MovDesc = get(MovOpc);
00490   const MachineOperand &Dst = I->getOperand(0), &Src = I->getOperand(1);
00491   unsigned DstReg = Dst.getReg(), SrcReg = Src.getReg(), TmpReg = DstReg;
00492   unsigned KillSrc =  getKillRegState(Src.isKill());
00493   DebugLoc DL = I->getDebugLoc();
00494   bool DstIsLarger, SrcIsLarger;
00495 
00496   std::tie(DstIsLarger, SrcIsLarger) =
00497       compareOpndSize(CvtOpc, *MBB.getParent());
00498 
00499   if (DstIsLarger)
00500     TmpReg = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
00501 
00502   if (SrcIsLarger)
00503     DstReg = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
00504 
00505   BuildMI(MBB, I, DL, MovDesc, TmpReg).addReg(SrcReg, KillSrc);
00506   BuildMI(MBB, I, DL, CvtDesc, DstReg).addReg(TmpReg, RegState::Kill);
00507 }
00508 
00509 void MipsSEInstrInfo::expandExtractElementF64(MachineBasicBlock &MBB,
00510                                               MachineBasicBlock::iterator I,
00511                                               bool FP64) const {
00512   unsigned DstReg = I->getOperand(0).getReg();
00513   unsigned SrcReg = I->getOperand(1).getReg();
00514   unsigned N = I->getOperand(2).getImm();
00515   DebugLoc dl = I->getDebugLoc();
00516 
00517   assert(N < 2 && "Invalid immediate");
00518   unsigned SubIdx = N ? Mips::sub_hi : Mips::sub_lo;
00519   unsigned SubReg = getRegisterInfo().getSubReg(SrcReg, SubIdx);
00520 
00521   // FPXX on MIPS-II or MIPS32r1 should have been handled with a spill/reload
00522   // in MipsSEFrameLowering.cpp.
00523   assert(!(Subtarget.isABI_FPXX() && !Subtarget.hasMips32r2()));
00524 
00525   // FP64A (FP64 with nooddspreg) should have been handled with a spill/reload
00526   // in MipsSEFrameLowering.cpp.
00527   assert(!(Subtarget.isFP64bit() && !Subtarget.useOddSPReg()));
00528 
00529   if (SubIdx == Mips::sub_hi && Subtarget.hasMTHC1()) {
00530     // FIXME: Strictly speaking MFHC1 only reads the top 32-bits however, we
00531     //        claim to read the whole 64-bits as part of a white lie used to
00532     //        temporarily work around a widespread bug in the -mfp64 support.
00533     //        The problem is that none of the 32-bit fpu ops mention the fact
00534     //        that they clobber the upper 32-bits of the 64-bit FPR. Fixing that
00535     //        requires a major overhaul of the FPU implementation which can't
00536     //        be done right now due to time constraints.
00537     //        MFHC1 is one of two instructions that are affected since they are
00538     //        the only instructions that don't read the lower 32-bits.
00539     //        We therefore pretend that it reads the bottom 32-bits to
00540     //        artificially create a dependency and prevent the scheduler
00541     //        changing the behaviour of the code.
00542     BuildMI(MBB, I, dl, get(FP64 ? Mips::MFHC1_D64 : Mips::MFHC1_D32), DstReg)
00543         .addReg(SrcReg);
00544   } else
00545     BuildMI(MBB, I, dl, get(Mips::MFC1), DstReg).addReg(SubReg);
00546 }
00547 
00548 void MipsSEInstrInfo::expandBuildPairF64(MachineBasicBlock &MBB,
00549                                          MachineBasicBlock::iterator I,
00550                                          bool FP64) const {
00551   unsigned DstReg = I->getOperand(0).getReg();
00552   unsigned LoReg = I->getOperand(1).getReg(), HiReg = I->getOperand(2).getReg();
00553   const MCInstrDesc& Mtc1Tdd = get(Mips::MTC1);
00554   DebugLoc dl = I->getDebugLoc();
00555   const TargetRegisterInfo &TRI = getRegisterInfo();
00556 
00557   // When mthc1 is available, use:
00558   //   mtc1 Lo, $fp
00559   //   mthc1 Hi, $fp
00560   //
00561   // Otherwise, for O32 FPXX ABI:
00562   //   spill + reload via ldc1
00563   // This case is handled by the frame lowering code.
00564   //
00565   // Otherwise, for FP32:
00566   //   mtc1 Lo, $fp
00567   //   mtc1 Hi, $fp + 1
00568   //
00569   // The case where dmtc1 is available doesn't need to be handled here
00570   // because it never creates a BuildPairF64 node.
00571 
00572   // FPXX on MIPS-II or MIPS32r1 should have been handled with a spill/reload
00573   // in MipsSEFrameLowering.cpp.
00574   assert(!(Subtarget.isABI_FPXX() && !Subtarget.hasMips32r2()));
00575 
00576   // FP64A (FP64 with nooddspreg) should have been handled with a spill/reload
00577   // in MipsSEFrameLowering.cpp.
00578   assert(!(Subtarget.isFP64bit() && !Subtarget.useOddSPReg()));
00579 
00580   BuildMI(MBB, I, dl, Mtc1Tdd, TRI.getSubReg(DstReg, Mips::sub_lo))
00581     .addReg(LoReg);
00582 
00583   if (Subtarget.hasMTHC1()) {
00584     // FIXME: The .addReg(DstReg) is a white lie used to temporarily work
00585     //        around a widespread bug in the -mfp64 support.
00586     //        The problem is that none of the 32-bit fpu ops mention the fact
00587     //        that they clobber the upper 32-bits of the 64-bit FPR. Fixing that
00588     //        requires a major overhaul of the FPU implementation which can't
00589     //        be done right now due to time constraints.
00590     //        MTHC1 is one of two instructions that are affected since they are
00591     //        the only instructions that don't read the lower 32-bits.
00592     //        We therefore pretend that it reads the bottom 32-bits to
00593     //        artificially create a dependency and prevent the scheduler
00594     //        changing the behaviour of the code.
00595     BuildMI(MBB, I, dl, get(FP64 ? Mips::MTHC1_D64 : Mips::MTHC1_D32), DstReg)
00596         .addReg(DstReg)
00597         .addReg(HiReg);
00598   } else if (Subtarget.isABI_FPXX())
00599     llvm_unreachable("BuildPairF64 not expanded in frame lowering code!");
00600   else
00601     BuildMI(MBB, I, dl, Mtc1Tdd, TRI.getSubReg(DstReg, Mips::sub_hi))
00602       .addReg(HiReg);
00603 }
00604 
00605 void MipsSEInstrInfo::expandEhReturn(MachineBasicBlock &MBB,
00606                                      MachineBasicBlock::iterator I) const {
00607   // This pseudo instruction is generated as part of the lowering of
00608   // ISD::EH_RETURN. We convert it to a stack increment by OffsetReg, and
00609   // indirect jump to TargetReg
00610   unsigned ADDU = Subtarget.isABI_N64() ? Mips::DADDu : Mips::ADDu;
00611   unsigned SP = Subtarget.isGP64bit() ? Mips::SP_64 : Mips::SP;
00612   unsigned RA = Subtarget.isGP64bit() ? Mips::RA_64 : Mips::RA;
00613   unsigned T9 = Subtarget.isGP64bit() ? Mips::T9_64 : Mips::T9;
00614   unsigned ZERO = Subtarget.isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;
00615   unsigned OffsetReg = I->getOperand(0).getReg();
00616   unsigned TargetReg = I->getOperand(1).getReg();
00617 
00618   // addu $ra, $v0, $zero
00619   // addu $sp, $sp, $v1
00620   // jr   $ra (via RetRA)
00621   const TargetMachine &TM = MBB.getParent()->getTarget();
00622   if (TM.getRelocationModel() == Reloc::PIC_)
00623     BuildMI(MBB, I, I->getDebugLoc(),
00624             TM.getSubtargetImpl()->getInstrInfo()->get(ADDU), T9)
00625         .addReg(TargetReg)
00626         .addReg(ZERO);
00627   BuildMI(MBB, I, I->getDebugLoc(),
00628           TM.getSubtargetImpl()->getInstrInfo()->get(ADDU), RA)
00629       .addReg(TargetReg)
00630       .addReg(ZERO);
00631   BuildMI(MBB, I, I->getDebugLoc(),
00632           TM.getSubtargetImpl()->getInstrInfo()->get(ADDU), SP)
00633       .addReg(SP)
00634       .addReg(OffsetReg);
00635   expandRetRA(MBB, I);
00636 }
00637 
00638 const MipsInstrInfo *llvm::createMipsSEInstrInfo(const MipsSubtarget &STI) {
00639   return new MipsSEInstrInfo(STI);
00640 }