LLVM API Documentation

MipsSEISelDAGToDAG.cpp
Go to the documentation of this file.
00001 //===-- MipsSEISelDAGToDAG.cpp - A Dag to Dag Inst Selector for MipsSE ----===//
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 // Subclass of MipsDAGToDAGISel specialized for mips32/64.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "MipsSEISelDAGToDAG.h"
00015 #include "MCTargetDesc/MipsBaseInfo.h"
00016 #include "Mips.h"
00017 #include "MipsAnalyzeImmediate.h"
00018 #include "MipsMachineFunction.h"
00019 #include "MipsRegisterInfo.h"
00020 #include "llvm/CodeGen/MachineConstantPool.h"
00021 #include "llvm/CodeGen/MachineFrameInfo.h"
00022 #include "llvm/CodeGen/MachineFunction.h"
00023 #include "llvm/CodeGen/MachineInstrBuilder.h"
00024 #include "llvm/CodeGen/MachineRegisterInfo.h"
00025 #include "llvm/CodeGen/SelectionDAGNodes.h"
00026 #include "llvm/IR/CFG.h"
00027 #include "llvm/IR/GlobalValue.h"
00028 #include "llvm/IR/Instructions.h"
00029 #include "llvm/IR/Intrinsics.h"
00030 #include "llvm/IR/Type.h"
00031 #include "llvm/Support/Debug.h"
00032 #include "llvm/Support/ErrorHandling.h"
00033 #include "llvm/Support/raw_ostream.h"
00034 #include "llvm/Target/TargetMachine.h"
00035 using namespace llvm;
00036 
00037 #define DEBUG_TYPE "mips-isel"
00038 
00039 bool MipsSEDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
00040   Subtarget = &TM.getSubtarget<MipsSubtarget>();
00041   if (Subtarget->inMips16Mode())
00042     return false;
00043   return MipsDAGToDAGISel::runOnMachineFunction(MF);
00044 }
00045 
00046 void MipsSEDAGToDAGISel::addDSPCtrlRegOperands(bool IsDef, MachineInstr &MI,
00047                                                MachineFunction &MF) {
00048   MachineInstrBuilder MIB(MF, &MI);
00049   unsigned Mask = MI.getOperand(1).getImm();
00050   unsigned Flag = IsDef ? RegState::ImplicitDefine : RegState::Implicit;
00051 
00052   if (Mask & 1)
00053     MIB.addReg(Mips::DSPPos, Flag);
00054 
00055   if (Mask & 2)
00056     MIB.addReg(Mips::DSPSCount, Flag);
00057 
00058   if (Mask & 4)
00059     MIB.addReg(Mips::DSPCarry, Flag);
00060 
00061   if (Mask & 8)
00062     MIB.addReg(Mips::DSPOutFlag, Flag);
00063 
00064   if (Mask & 16)
00065     MIB.addReg(Mips::DSPCCond, Flag);
00066 
00067   if (Mask & 32)
00068     MIB.addReg(Mips::DSPEFI, Flag);
00069 }
00070 
00071 unsigned MipsSEDAGToDAGISel::getMSACtrlReg(const SDValue RegIdx) const {
00072   switch (cast<ConstantSDNode>(RegIdx)->getZExtValue()) {
00073   default:
00074     llvm_unreachable("Could not map int to register");
00075   case 0: return Mips::MSAIR;
00076   case 1: return Mips::MSACSR;
00077   case 2: return Mips::MSAAccess;
00078   case 3: return Mips::MSASave;
00079   case 4: return Mips::MSAModify;
00080   case 5: return Mips::MSARequest;
00081   case 6: return Mips::MSAMap;
00082   case 7: return Mips::MSAUnmap;
00083   }
00084 }
00085 
00086 bool MipsSEDAGToDAGISel::replaceUsesWithZeroReg(MachineRegisterInfo *MRI,
00087                                                 const MachineInstr& MI) {
00088   unsigned DstReg = 0, ZeroReg = 0;
00089 
00090   // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
00091   if ((MI.getOpcode() == Mips::ADDiu) &&
00092       (MI.getOperand(1).getReg() == Mips::ZERO) &&
00093       (MI.getOperand(2).getImm() == 0)) {
00094     DstReg = MI.getOperand(0).getReg();
00095     ZeroReg = Mips::ZERO;
00096   } else if ((MI.getOpcode() == Mips::DADDiu) &&
00097              (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
00098              (MI.getOperand(2).getImm() == 0)) {
00099     DstReg = MI.getOperand(0).getReg();
00100     ZeroReg = Mips::ZERO_64;
00101   }
00102 
00103   if (!DstReg)
00104     return false;
00105 
00106   // Replace uses with ZeroReg.
00107   for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
00108        E = MRI->use_end(); U != E;) {
00109     MachineOperand &MO = *U;
00110     unsigned OpNo = U.getOperandNo();
00111     MachineInstr *MI = MO.getParent();
00112     ++U;
00113 
00114     // Do not replace if it is a phi's operand or is tied to def operand.
00115     if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo())
00116       continue;
00117 
00118     MO.setReg(ZeroReg);
00119   }
00120 
00121   return true;
00122 }
00123 
00124 void MipsSEDAGToDAGISel::initGlobalBaseReg(MachineFunction &MF) {
00125   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
00126 
00127   if (!MipsFI->globalBaseRegSet())
00128     return;
00129 
00130   MachineBasicBlock &MBB = MF.front();
00131   MachineBasicBlock::iterator I = MBB.begin();
00132   MachineRegisterInfo &RegInfo = MF.getRegInfo();
00133   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
00134   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
00135   unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg();
00136   const TargetRegisterClass *RC;
00137 
00138   if (Subtarget->isABI_N64())
00139     RC = (const TargetRegisterClass*)&Mips::GPR64RegClass;
00140   else
00141     RC = (const TargetRegisterClass*)&Mips::GPR32RegClass;
00142 
00143   V0 = RegInfo.createVirtualRegister(RC);
00144   V1 = RegInfo.createVirtualRegister(RC);
00145 
00146   if (Subtarget->isABI_N64()) {
00147     MF.getRegInfo().addLiveIn(Mips::T9_64);
00148     MBB.addLiveIn(Mips::T9_64);
00149 
00150     // lui $v0, %hi(%neg(%gp_rel(fname)))
00151     // daddu $v1, $v0, $t9
00152     // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
00153     const GlobalValue *FName = MF.getFunction();
00154     BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
00155       .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
00156     BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
00157       .addReg(Mips::T9_64);
00158     BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
00159       .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
00160     return;
00161   }
00162 
00163   if (MF.getTarget().getRelocationModel() == Reloc::Static) {
00164     // Set global register to __gnu_local_gp.
00165     //
00166     // lui   $v0, %hi(__gnu_local_gp)
00167     // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
00168     BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
00169       .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
00170     BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
00171       .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
00172     return;
00173   }
00174 
00175   MF.getRegInfo().addLiveIn(Mips::T9);
00176   MBB.addLiveIn(Mips::T9);
00177 
00178   if (Subtarget->isABI_N32()) {
00179     // lui $v0, %hi(%neg(%gp_rel(fname)))
00180     // addu $v1, $v0, $t9
00181     // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
00182     const GlobalValue *FName = MF.getFunction();
00183     BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
00184       .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
00185     BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
00186     BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
00187       .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
00188     return;
00189   }
00190 
00191   assert(Subtarget->isABI_O32());
00192 
00193   // For O32 ABI, the following instruction sequence is emitted to initialize
00194   // the global base register:
00195   //
00196   //  0. lui   $2, %hi(_gp_disp)
00197   //  1. addiu $2, $2, %lo(_gp_disp)
00198   //  2. addu  $globalbasereg, $2, $t9
00199   //
00200   // We emit only the last instruction here.
00201   //
00202   // GNU linker requires that the first two instructions appear at the beginning
00203   // of a function and no instructions be inserted before or between them.
00204   // The two instructions are emitted during lowering to MC layer in order to
00205   // avoid any reordering.
00206   //
00207   // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
00208   // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
00209   // reads it.
00210   MF.getRegInfo().addLiveIn(Mips::V0);
00211   MBB.addLiveIn(Mips::V0);
00212   BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
00213     .addReg(Mips::V0).addReg(Mips::T9);
00214 }
00215 
00216 void MipsSEDAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) {
00217   initGlobalBaseReg(MF);
00218 
00219   MachineRegisterInfo *MRI = &MF.getRegInfo();
00220 
00221   for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
00222        ++MFI)
00223     for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) {
00224       if (I->getOpcode() == Mips::RDDSP)
00225         addDSPCtrlRegOperands(false, *I, MF);
00226       else if (I->getOpcode() == Mips::WRDSP)
00227         addDSPCtrlRegOperands(true, *I, MF);
00228       else
00229         replaceUsesWithZeroReg(MRI, *I);
00230     }
00231 }
00232 
00233 SDNode *MipsSEDAGToDAGISel::selectAddESubE(unsigned MOp, SDValue InFlag,
00234                                            SDValue CmpLHS, SDLoc DL,
00235                                            SDNode *Node) const {
00236   unsigned Opc = InFlag.getOpcode(); (void)Opc;
00237 
00238   assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
00239           (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
00240          "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
00241 
00242   SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
00243   SDValue LHS = Node->getOperand(0), RHS = Node->getOperand(1);
00244   EVT VT = LHS.getValueType();
00245 
00246   SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, DL, VT, Ops);
00247   SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, DL, VT,
00248                                             SDValue(Carry, 0), RHS);
00249   return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue, LHS,
00250                               SDValue(AddCarry, 0));
00251 }
00252 
00253 /// Match frameindex
00254 bool MipsSEDAGToDAGISel::selectAddrFrameIndex(SDValue Addr, SDValue &Base,
00255                                               SDValue &Offset) const {
00256   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
00257     EVT ValTy = Addr.getValueType();
00258 
00259     Base   = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
00260     Offset = CurDAG->getTargetConstant(0, ValTy);
00261     return true;
00262   }
00263   return false;
00264 }
00265 
00266 /// Match frameindex+offset and frameindex|offset
00267 bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset(SDValue Addr, SDValue &Base,
00268                                                     SDValue &Offset,
00269                                                     unsigned OffsetBits) const {
00270   if (CurDAG->isBaseWithConstantOffset(Addr)) {
00271     ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
00272     if (isIntN(OffsetBits, CN->getSExtValue())) {
00273       EVT ValTy = Addr.getValueType();
00274 
00275       // If the first operand is a FI, get the TargetFI Node
00276       if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
00277                                   (Addr.getOperand(0)))
00278         Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
00279       else
00280         Base = Addr.getOperand(0);
00281 
00282       Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
00283       return true;
00284     }
00285   }
00286   return false;
00287 }
00288 
00289 /// ComplexPattern used on MipsInstrInfo
00290 /// Used on Mips Load/Store instructions
00291 bool MipsSEDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
00292                                           SDValue &Offset) const {
00293   // if Address is FI, get the TargetFrameIndex.
00294   if (selectAddrFrameIndex(Addr, Base, Offset))
00295     return true;
00296 
00297   // on PIC code Load GA
00298   if (Addr.getOpcode() == MipsISD::Wrapper) {
00299     Base   = Addr.getOperand(0);
00300     Offset = Addr.getOperand(1);
00301     return true;
00302   }
00303 
00304   if (TM.getRelocationModel() != Reloc::PIC_) {
00305     if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
00306         Addr.getOpcode() == ISD::TargetGlobalAddress))
00307       return false;
00308   }
00309 
00310   // Addresses of the form FI+const or FI|const
00311   if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16))
00312     return true;
00313 
00314   // Operand is a result from an ADD.
00315   if (Addr.getOpcode() == ISD::ADD) {
00316     // When loading from constant pools, load the lower address part in
00317     // the instruction itself. Example, instead of:
00318     //  lui $2, %hi($CPI1_0)
00319     //  addiu $2, $2, %lo($CPI1_0)
00320     //  lwc1 $f0, 0($2)
00321     // Generate:
00322     //  lui $2, %hi($CPI1_0)
00323     //  lwc1 $f0, %lo($CPI1_0)($2)
00324     if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
00325         Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
00326       SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
00327       if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
00328           isa<JumpTableSDNode>(Opnd0)) {
00329         Base = Addr.getOperand(0);
00330         Offset = Opnd0;
00331         return true;
00332       }
00333     }
00334   }
00335 
00336   return false;
00337 }
00338 
00339 /// ComplexPattern used on MipsInstrInfo
00340 /// Used on Mips Load/Store instructions
00341 bool MipsSEDAGToDAGISel::selectAddrRegReg(SDValue Addr, SDValue &Base,
00342                                           SDValue &Offset) const {
00343   // Operand is a result from an ADD.
00344   if (Addr.getOpcode() == ISD::ADD) {
00345     Base = Addr.getOperand(0);
00346     Offset = Addr.getOperand(1);
00347     return true;
00348   }
00349 
00350   return false;
00351 }
00352 
00353 bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
00354                                            SDValue &Offset) const {
00355   Base = Addr;
00356   Offset = CurDAG->getTargetConstant(0, Addr.getValueType());
00357   return true;
00358 }
00359 
00360 bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
00361                                        SDValue &Offset) const {
00362   return selectAddrRegImm(Addr, Base, Offset) ||
00363     selectAddrDefault(Addr, Base, Offset);
00364 }
00365 
00366 bool MipsSEDAGToDAGISel::selectAddrRegImm10(SDValue Addr, SDValue &Base,
00367                                             SDValue &Offset) const {
00368   if (selectAddrFrameIndex(Addr, Base, Offset))
00369     return true;
00370 
00371   if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10))
00372     return true;
00373 
00374   return false;
00375 }
00376 
00377 /// Used on microMIPS Load/Store unaligned instructions (12-bit offset)
00378 bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr, SDValue &Base,
00379                                             SDValue &Offset) const {
00380   if (selectAddrFrameIndex(Addr, Base, Offset))
00381     return true;
00382 
00383   if (selectAddrFrameIndexOffset(Addr, Base, Offset, 12))
00384     return true;
00385 
00386   return false;
00387 }
00388 
00389 bool MipsSEDAGToDAGISel::selectIntAddrMM(SDValue Addr, SDValue &Base,
00390                                          SDValue &Offset) const {
00391   return selectAddrRegImm12(Addr, Base, Offset) ||
00392     selectAddrDefault(Addr, Base, Offset);
00393 }
00394 
00395 bool MipsSEDAGToDAGISel::selectIntAddrMSA(SDValue Addr, SDValue &Base,
00396                                           SDValue &Offset) const {
00397   if (selectAddrRegImm10(Addr, Base, Offset))
00398     return true;
00399 
00400   if (selectAddrDefault(Addr, Base, Offset))
00401     return true;
00402 
00403   return false;
00404 }
00405 
00406 // Select constant vector splats.
00407 //
00408 // Returns true and sets Imm if:
00409 // * MSA is enabled
00410 // * N is a ISD::BUILD_VECTOR representing a constant splat
00411 bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm) const {
00412   if (!Subtarget->hasMSA())
00413     return false;
00414 
00415   BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N);
00416 
00417   if (!Node)
00418     return false;
00419 
00420   APInt SplatValue, SplatUndef;
00421   unsigned SplatBitSize;
00422   bool HasAnyUndefs;
00423 
00424   if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
00425                              HasAnyUndefs, 8,
00426                              !Subtarget->isLittle()))
00427     return false;
00428 
00429   Imm = SplatValue;
00430 
00431   return true;
00432 }
00433 
00434 // Select constant vector splats.
00435 //
00436 // In addition to the requirements of selectVSplat(), this function returns
00437 // true and sets Imm if:
00438 // * The splat value is the same width as the elements of the vector
00439 // * The splat value fits in an integer with the specified signed-ness and
00440 //   width.
00441 //
00442 // This function looks through ISD::BITCAST nodes.
00443 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
00444 //       sometimes a shuffle in big-endian mode.
00445 //
00446 // It's worth noting that this function is not used as part of the selection
00447 // of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd]
00448 // instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in
00449 // MipsSEDAGToDAGISel::selectNode.
00450 bool MipsSEDAGToDAGISel::
00451 selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed,
00452                    unsigned ImmBitSize) const {
00453   APInt ImmValue;
00454   EVT EltTy = N->getValueType(0).getVectorElementType();
00455 
00456   if (N->getOpcode() == ISD::BITCAST)
00457     N = N->getOperand(0);
00458 
00459   if (selectVSplat (N.getNode(), ImmValue) &&
00460       ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
00461     if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) ||
00462         (!Signed && ImmValue.isIntN(ImmBitSize))) {
00463       Imm = CurDAG->getTargetConstant(ImmValue, EltTy);
00464       return true;
00465     }
00466   }
00467 
00468   return false;
00469 }
00470 
00471 // Select constant vector splats.
00472 bool MipsSEDAGToDAGISel::
00473 selectVSplatUimm1(SDValue N, SDValue &Imm) const {
00474   return selectVSplatCommon(N, Imm, false, 1);
00475 }
00476 
00477 bool MipsSEDAGToDAGISel::
00478 selectVSplatUimm2(SDValue N, SDValue &Imm) const {
00479   return selectVSplatCommon(N, Imm, false, 2);
00480 }
00481 
00482 bool MipsSEDAGToDAGISel::
00483 selectVSplatUimm3(SDValue N, SDValue &Imm) const {
00484   return selectVSplatCommon(N, Imm, false, 3);
00485 }
00486 
00487 // Select constant vector splats.
00488 bool MipsSEDAGToDAGISel::
00489 selectVSplatUimm4(SDValue N, SDValue &Imm) const {
00490   return selectVSplatCommon(N, Imm, false, 4);
00491 }
00492 
00493 // Select constant vector splats.
00494 bool MipsSEDAGToDAGISel::
00495 selectVSplatUimm5(SDValue N, SDValue &Imm) const {
00496   return selectVSplatCommon(N, Imm, false, 5);
00497 }
00498 
00499 // Select constant vector splats.
00500 bool MipsSEDAGToDAGISel::
00501 selectVSplatUimm6(SDValue N, SDValue &Imm) const {
00502   return selectVSplatCommon(N, Imm, false, 6);
00503 }
00504 
00505 // Select constant vector splats.
00506 bool MipsSEDAGToDAGISel::
00507 selectVSplatUimm8(SDValue N, SDValue &Imm) const {
00508   return selectVSplatCommon(N, Imm, false, 8);
00509 }
00510 
00511 // Select constant vector splats.
00512 bool MipsSEDAGToDAGISel::
00513 selectVSplatSimm5(SDValue N, SDValue &Imm) const {
00514   return selectVSplatCommon(N, Imm, true, 5);
00515 }
00516 
00517 // Select constant vector splats whose value is a power of 2.
00518 //
00519 // In addition to the requirements of selectVSplat(), this function returns
00520 // true and sets Imm if:
00521 // * The splat value is the same width as the elements of the vector
00522 // * The splat value is a power of two.
00523 //
00524 // This function looks through ISD::BITCAST nodes.
00525 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
00526 //       sometimes a shuffle in big-endian mode.
00527 bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const {
00528   APInt ImmValue;
00529   EVT EltTy = N->getValueType(0).getVectorElementType();
00530 
00531   if (N->getOpcode() == ISD::BITCAST)
00532     N = N->getOperand(0);
00533 
00534   if (selectVSplat (N.getNode(), ImmValue) &&
00535       ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
00536     int32_t Log2 = ImmValue.exactLogBase2();
00537 
00538     if (Log2 != -1) {
00539       Imm = CurDAG->getTargetConstant(Log2, EltTy);
00540       return true;
00541     }
00542   }
00543 
00544   return false;
00545 }
00546 
00547 // Select constant vector splats whose value only has a consecutive sequence
00548 // of left-most bits set (e.g. 0b11...1100...00).
00549 //
00550 // In addition to the requirements of selectVSplat(), this function returns
00551 // true and sets Imm if:
00552 // * The splat value is the same width as the elements of the vector
00553 // * The splat value is a consecutive sequence of left-most bits.
00554 //
00555 // This function looks through ISD::BITCAST nodes.
00556 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
00557 //       sometimes a shuffle in big-endian mode.
00558 bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const {
00559   APInt ImmValue;
00560   EVT EltTy = N->getValueType(0).getVectorElementType();
00561 
00562   if (N->getOpcode() == ISD::BITCAST)
00563     N = N->getOperand(0);
00564 
00565   if (selectVSplat(N.getNode(), ImmValue) &&
00566       ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
00567     // Extract the run of set bits starting with bit zero from the bitwise
00568     // inverse of ImmValue, and test that the inverse of this is the same
00569     // as the original value.
00570     if (ImmValue == ~(~ImmValue & ~(~ImmValue + 1))) {
00571 
00572       Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), EltTy);
00573       return true;
00574     }
00575   }
00576 
00577   return false;
00578 }
00579 
00580 // Select constant vector splats whose value only has a consecutive sequence
00581 // of right-most bits set (e.g. 0b00...0011...11).
00582 //
00583 // In addition to the requirements of selectVSplat(), this function returns
00584 // true and sets Imm if:
00585 // * The splat value is the same width as the elements of the vector
00586 // * The splat value is a consecutive sequence of right-most bits.
00587 //
00588 // This function looks through ISD::BITCAST nodes.
00589 // TODO: This might not be appropriate for big-endian MSA since BITCAST is
00590 //       sometimes a shuffle in big-endian mode.
00591 bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const {
00592   APInt ImmValue;
00593   EVT EltTy = N->getValueType(0).getVectorElementType();
00594 
00595   if (N->getOpcode() == ISD::BITCAST)
00596     N = N->getOperand(0);
00597 
00598   if (selectVSplat(N.getNode(), ImmValue) &&
00599       ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
00600     // Extract the run of set bits starting with bit zero, and test that the
00601     // result is the same as the original value
00602     if (ImmValue == (ImmValue & ~(ImmValue + 1))) {
00603       Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), EltTy);
00604       return true;
00605     }
00606   }
00607 
00608   return false;
00609 }
00610 
00611 bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N,
00612                                                  SDValue &Imm) const {
00613   APInt ImmValue;
00614   EVT EltTy = N->getValueType(0).getVectorElementType();
00615 
00616   if (N->getOpcode() == ISD::BITCAST)
00617     N = N->getOperand(0);
00618 
00619   if (selectVSplat(N.getNode(), ImmValue) &&
00620       ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
00621     int32_t Log2 = (~ImmValue).exactLogBase2();
00622 
00623     if (Log2 != -1) {
00624       Imm = CurDAG->getTargetConstant(Log2, EltTy);
00625       return true;
00626     }
00627   }
00628 
00629   return false;
00630 }
00631 
00632 std::pair<bool, SDNode*> MipsSEDAGToDAGISel::selectNode(SDNode *Node) {
00633   unsigned Opcode = Node->getOpcode();
00634   SDLoc DL(Node);
00635 
00636   ///
00637   // Instruction Selection not handled by the auto-generated
00638   // tablegen selection should be handled here.
00639   ///
00640   SDNode *Result;
00641 
00642   switch(Opcode) {
00643   default: break;
00644 
00645   case ISD::SUBE: {
00646     SDValue InFlag = Node->getOperand(2);
00647     Result = selectAddESubE(Mips::SUBu, InFlag, InFlag.getOperand(0), DL, Node);
00648     return std::make_pair(true, Result);
00649   }
00650 
00651   case ISD::ADDE: {
00652     if (Subtarget->hasDSP()) // Select DSP instructions, ADDSC and ADDWC.
00653       break;
00654     SDValue InFlag = Node->getOperand(2);
00655     Result = selectAddESubE(Mips::ADDu, InFlag, InFlag.getValue(0), DL, Node);
00656     return std::make_pair(true, Result);
00657   }
00658 
00659   case ISD::ConstantFP: {
00660     ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
00661     if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
00662       if (Subtarget->isGP64bit()) {
00663         SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
00664                                               Mips::ZERO_64, MVT::i64);
00665         Result = CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero);
00666       } else if (Subtarget->isFP64bit()) {
00667         SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
00668                                               Mips::ZERO, MVT::i32);
00669         Result = CurDAG->getMachineNode(Mips::BuildPairF64_64, DL, MVT::f64,
00670                                         Zero, Zero);
00671       } else {
00672         SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
00673                                               Mips::ZERO, MVT::i32);
00674         Result = CurDAG->getMachineNode(Mips::BuildPairF64, DL, MVT::f64, Zero,
00675                                         Zero);
00676       }
00677 
00678       return std::make_pair(true, Result);
00679     }
00680     break;
00681   }
00682 
00683   case ISD::Constant: {
00684     const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
00685     unsigned Size = CN->getValueSizeInBits(0);
00686 
00687     if (Size == 32)
00688       break;
00689 
00690     MipsAnalyzeImmediate AnalyzeImm;
00691     int64_t Imm = CN->getSExtValue();
00692 
00693     const MipsAnalyzeImmediate::InstSeq &Seq =
00694       AnalyzeImm.Analyze(Imm, Size, false);
00695 
00696     MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
00697     SDLoc DL(CN);
00698     SDNode *RegOpnd;
00699     SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
00700                                                 MVT::i64);
00701 
00702     // The first instruction can be a LUi which is different from other
00703     // instructions (ADDiu, ORI and SLL) in that it does not have a register
00704     // operand.
00705     if (Inst->Opc == Mips::LUi64)
00706       RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
00707     else
00708       RegOpnd =
00709         CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
00710                                CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
00711                                ImmOpnd);
00712 
00713     // The remaining instructions in the sequence are handled here.
00714     for (++Inst; Inst != Seq.end(); ++Inst) {
00715       ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
00716                                           MVT::i64);
00717       RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
00718                                        SDValue(RegOpnd, 0), ImmOpnd);
00719     }
00720 
00721     return std::make_pair(true, RegOpnd);
00722   }
00723 
00724   case ISD::INTRINSIC_W_CHAIN: {
00725     switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
00726     default:
00727       break;
00728 
00729     case Intrinsic::mips_cfcmsa: {
00730       SDValue ChainIn = Node->getOperand(0);
00731       SDValue RegIdx = Node->getOperand(2);
00732       SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL,
00733                                            getMSACtrlReg(RegIdx), MVT::i32);
00734       return std::make_pair(true, Reg.getNode());
00735     }
00736     }
00737     break;
00738   }
00739 
00740   case ISD::INTRINSIC_WO_CHAIN: {
00741     switch (cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue()) {
00742     default:
00743       break;
00744 
00745     case Intrinsic::mips_move_v:
00746       // Like an assignment but will always produce a move.v even if
00747       // unnecessary.
00748       return std::make_pair(true,
00749                             CurDAG->getMachineNode(Mips::MOVE_V, DL,
00750                                                    Node->getValueType(0),
00751                                                    Node->getOperand(1)));
00752     }
00753     break;
00754   }
00755 
00756   case ISD::INTRINSIC_VOID: {
00757     switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
00758     default:
00759       break;
00760 
00761     case Intrinsic::mips_ctcmsa: {
00762       SDValue ChainIn = Node->getOperand(0);
00763       SDValue RegIdx  = Node->getOperand(2);
00764       SDValue Value   = Node->getOperand(3);
00765       SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL,
00766                                               getMSACtrlReg(RegIdx), Value);
00767       return std::make_pair(true, ChainOut.getNode());
00768     }
00769     }
00770     break;
00771   }
00772 
00773   case MipsISD::ThreadPointer: {
00774     EVT PtrVT = getTargetLowering()->getPointerTy();
00775     unsigned RdhwrOpc, DestReg;
00776 
00777     if (PtrVT == MVT::i32) {
00778       RdhwrOpc = Mips::RDHWR;
00779       DestReg = Mips::V1;
00780     } else {
00781       RdhwrOpc = Mips::RDHWR64;
00782       DestReg = Mips::V1_64;
00783     }
00784 
00785     SDNode *Rdhwr =
00786       CurDAG->getMachineNode(RdhwrOpc, SDLoc(Node),
00787                              Node->getValueType(0),
00788                              CurDAG->getRegister(Mips::HWR29, MVT::i32));
00789     SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg,
00790                                          SDValue(Rdhwr, 0));
00791     SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT);
00792     ReplaceUses(SDValue(Node, 0), ResNode);
00793     return std::make_pair(true, ResNode.getNode());
00794   }
00795 
00796   case ISD::BUILD_VECTOR: {
00797     // Select appropriate ldi.[bhwd] instructions for constant splats of
00798     // 128-bit when MSA is enabled. Fixup any register class mismatches that
00799     // occur as a result.
00800     //
00801     // This allows the compiler to use a wider range of immediates than would
00802     // otherwise be allowed. If, for example, v4i32 could only use ldi.h then
00803     // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101,
00804     // 0x01010101 } without using a constant pool. This would be sub-optimal
00805     // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the
00806     // same set/ of registers. Similarly, ldi.h isn't capable of producing {
00807     // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can.
00808 
00809     BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node);
00810     APInt SplatValue, SplatUndef;
00811     unsigned SplatBitSize;
00812     bool HasAnyUndefs;
00813     unsigned LdiOp;
00814     EVT ResVecTy = BVN->getValueType(0);
00815     EVT ViaVecTy;
00816 
00817     if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector())
00818       return std::make_pair(false, nullptr);
00819 
00820     if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
00821                               HasAnyUndefs, 8,
00822                               !Subtarget->isLittle()))
00823       return std::make_pair(false, nullptr);
00824 
00825     switch (SplatBitSize) {
00826     default:
00827       return std::make_pair(false, nullptr);
00828     case 8:
00829       LdiOp = Mips::LDI_B;
00830       ViaVecTy = MVT::v16i8;
00831       break;
00832     case 16:
00833       LdiOp = Mips::LDI_H;
00834       ViaVecTy = MVT::v8i16;
00835       break;
00836     case 32:
00837       LdiOp = Mips::LDI_W;
00838       ViaVecTy = MVT::v4i32;
00839       break;
00840     case 64:
00841       LdiOp = Mips::LDI_D;
00842       ViaVecTy = MVT::v2i64;
00843       break;
00844     }
00845 
00846     if (!SplatValue.isSignedIntN(10))
00847       return std::make_pair(false, nullptr);
00848 
00849     SDValue Imm = CurDAG->getTargetConstant(SplatValue,
00850                                             ViaVecTy.getVectorElementType());
00851 
00852     SDNode *Res = CurDAG->getMachineNode(LdiOp, SDLoc(Node), ViaVecTy, Imm);
00853 
00854     if (ResVecTy != ViaVecTy) {
00855       // If LdiOp is writing to a different register class to ResVecTy, then
00856       // fix it up here. This COPY_TO_REGCLASS should never cause a move.v
00857       // since the source and destination register sets contain the same
00858       // registers.
00859       const TargetLowering *TLI = getTargetLowering();
00860       MVT ResVecTySimple = ResVecTy.getSimpleVT();
00861       const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple);
00862       Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, SDLoc(Node),
00863                                    ResVecTy, SDValue(Res, 0),
00864                                    CurDAG->getTargetConstant(RC->getID(),
00865                                                              MVT::i32));
00866     }
00867 
00868     return std::make_pair(true, Res);
00869   }
00870 
00871   }
00872 
00873   return std::make_pair(false, nullptr);
00874 }
00875 
00876 FunctionPass *llvm::createMipsSEISelDag(MipsTargetMachine &TM) {
00877   return new MipsSEDAGToDAGISel(TM);
00878 }