LLVM API Documentation

Mips16ISelDAGToDAG.cpp
Go to the documentation of this file.
00001 //===-- Mips16ISelDAGToDAG.cpp - A Dag to Dag Inst Selector for Mips16 ----===//
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 mips16.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "Mips16ISelDAGToDAG.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 Mips16DAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
00040   Subtarget = &TM.getSubtarget<MipsSubtarget>();
00041   if (!Subtarget->inMips16Mode())
00042     return false;
00043   return MipsDAGToDAGISel::runOnMachineFunction(MF);
00044 }
00045 /// Select multiply instructions.
00046 std::pair<SDNode*, SDNode*>
00047 Mips16DAGToDAGISel::selectMULT(SDNode *N, unsigned Opc, SDLoc DL, EVT Ty,
00048                                bool HasLo, bool HasHi) {
00049   SDNode *Lo = nullptr, *Hi = nullptr;
00050   SDNode *Mul = CurDAG->getMachineNode(Opc, DL, MVT::Glue, N->getOperand(0),
00051                                        N->getOperand(1));
00052   SDValue InFlag = SDValue(Mul, 0);
00053 
00054   if (HasLo) {
00055     unsigned Opcode = Mips::Mflo16;
00056     Lo = CurDAG->getMachineNode(Opcode, DL, Ty, MVT::Glue, InFlag);
00057     InFlag = SDValue(Lo, 1);
00058   }
00059   if (HasHi) {
00060     unsigned Opcode = Mips::Mfhi16;
00061     Hi = CurDAG->getMachineNode(Opcode, DL, Ty, InFlag);
00062   }
00063   return std::make_pair(Lo, Hi);
00064 }
00065 
00066 void Mips16DAGToDAGISel::initGlobalBaseReg(MachineFunction &MF) {
00067   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
00068 
00069   if (!MipsFI->globalBaseRegSet())
00070     return;
00071 
00072   MachineBasicBlock &MBB = MF.front();
00073   MachineBasicBlock::iterator I = MBB.begin();
00074   MachineRegisterInfo &RegInfo = MF.getRegInfo();
00075   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
00076   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
00077   unsigned V0, V1, V2, GlobalBaseReg = MipsFI->getGlobalBaseReg();
00078   const TargetRegisterClass *RC =
00079     (const TargetRegisterClass*)&Mips::CPU16RegsRegClass;
00080 
00081   V0 = RegInfo.createVirtualRegister(RC);
00082   V1 = RegInfo.createVirtualRegister(RC);
00083   V2 = RegInfo.createVirtualRegister(RC);
00084 
00085   BuildMI(MBB, I, DL, TII.get(Mips::GotPrologue16), V0).
00086     addReg(V1, RegState::Define).
00087     addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI).
00088     addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
00089 
00090   BuildMI(MBB, I, DL, TII.get(Mips::SllX16), V2).addReg(V0).addImm(16);
00091   BuildMI(MBB, I, DL, TII.get(Mips::AdduRxRyRz16), GlobalBaseReg)
00092     .addReg(V1).addReg(V2);
00093 }
00094 
00095 // Insert instructions to initialize the Mips16 SP Alias register in the
00096 // first MBB of the function.
00097 //
00098 void Mips16DAGToDAGISel::initMips16SPAliasReg(MachineFunction &MF) {
00099   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
00100 
00101   if (!MipsFI->mips16SPAliasRegSet())
00102     return;
00103 
00104   MachineBasicBlock &MBB = MF.front();
00105   MachineBasicBlock::iterator I = MBB.begin();
00106   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
00107   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
00108   unsigned Mips16SPAliasReg = MipsFI->getMips16SPAliasReg();
00109 
00110   BuildMI(MBB, I, DL, TII.get(Mips::MoveR3216), Mips16SPAliasReg)
00111     .addReg(Mips::SP);
00112 }
00113 
00114 void Mips16DAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) {
00115   initGlobalBaseReg(MF);
00116   initMips16SPAliasReg(MF);
00117 }
00118 
00119 /// getMips16SPAliasReg - Output the instructions required to put the
00120 /// SP into a Mips16 accessible aliased register.
00121 SDValue Mips16DAGToDAGISel::getMips16SPAliasReg() {
00122   unsigned Mips16SPAliasReg =
00123     MF->getInfo<MipsFunctionInfo>()->getMips16SPAliasReg();
00124   return CurDAG->getRegister(Mips16SPAliasReg,
00125                              getTargetLowering()->getPointerTy());
00126 }
00127 
00128 void Mips16DAGToDAGISel::getMips16SPRefReg(SDNode *Parent, SDValue &AliasReg) {
00129   SDValue AliasFPReg = CurDAG->getRegister(Mips::S0,
00130                                            getTargetLowering()->getPointerTy());
00131   if (Parent) {
00132     switch (Parent->getOpcode()) {
00133       case ISD::LOAD: {
00134         LoadSDNode *SD = dyn_cast<LoadSDNode>(Parent);
00135         switch (SD->getMemoryVT().getSizeInBits()) {
00136         case 8:
00137         case 16:
00138           AliasReg = TM.getSubtargetImpl()->getFrameLowering()->hasFP(*MF)
00139                          ? AliasFPReg
00140                          : getMips16SPAliasReg();
00141           return;
00142         }
00143         break;
00144       }
00145       case ISD::STORE: {
00146         StoreSDNode *SD = dyn_cast<StoreSDNode>(Parent);
00147         switch (SD->getMemoryVT().getSizeInBits()) {
00148         case 8:
00149         case 16:
00150           AliasReg = TM.getSubtargetImpl()->getFrameLowering()->hasFP(*MF)
00151                          ? AliasFPReg
00152                          : getMips16SPAliasReg();
00153           return;
00154         }
00155         break;
00156       }
00157     }
00158   }
00159   AliasReg = CurDAG->getRegister(Mips::SP, getTargetLowering()->getPointerTy());
00160   return;
00161 
00162 }
00163 
00164 bool Mips16DAGToDAGISel::selectAddr16(
00165   SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset,
00166   SDValue &Alias) {
00167   EVT ValTy = Addr.getValueType();
00168 
00169   Alias = CurDAG->getTargetConstant(0, ValTy);
00170 
00171   // if Address is FI, get the TargetFrameIndex.
00172   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
00173     Base   = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
00174     Offset = CurDAG->getTargetConstant(0, ValTy);
00175     getMips16SPRefReg(Parent, Alias);
00176     return true;
00177   }
00178   // on PIC code Load GA
00179   if (Addr.getOpcode() == MipsISD::Wrapper) {
00180     Base   = Addr.getOperand(0);
00181     Offset = Addr.getOperand(1);
00182     return true;
00183   }
00184   if (TM.getRelocationModel() != Reloc::PIC_) {
00185     if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
00186         Addr.getOpcode() == ISD::TargetGlobalAddress))
00187       return false;
00188   }
00189   // Addresses of the form FI+const or FI|const
00190   if (CurDAG->isBaseWithConstantOffset(Addr)) {
00191     ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
00192     if (isInt<16>(CN->getSExtValue())) {
00193 
00194       // If the first operand is a FI, get the TargetFI Node
00195       if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
00196                                   (Addr.getOperand(0))) {
00197         Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
00198         getMips16SPRefReg(Parent, Alias);
00199       }
00200       else
00201         Base = Addr.getOperand(0);
00202 
00203       Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
00204       return true;
00205     }
00206   }
00207   // Operand is a result from an ADD.
00208   if (Addr.getOpcode() == ISD::ADD) {
00209     // When loading from constant pools, load the lower address part in
00210     // the instruction itself. Example, instead of:
00211     //  lui $2, %hi($CPI1_0)
00212     //  addiu $2, $2, %lo($CPI1_0)
00213     //  lwc1 $f0, 0($2)
00214     // Generate:
00215     //  lui $2, %hi($CPI1_0)
00216     //  lwc1 $f0, %lo($CPI1_0)($2)
00217     if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
00218         Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
00219       SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
00220       if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
00221           isa<JumpTableSDNode>(Opnd0)) {
00222         Base = Addr.getOperand(0);
00223         Offset = Opnd0;
00224         return true;
00225       }
00226     }
00227 
00228     // If an indexed floating point load/store can be emitted, return false.
00229     const LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(Parent);
00230 
00231     if (LS) {
00232       if (LS->getMemoryVT() == MVT::f32 && Subtarget->hasMips4_32r2())
00233         return false;
00234       if (LS->getMemoryVT() == MVT::f64 && Subtarget->hasMips4_32r2())
00235         return false;
00236     }
00237   }
00238   Base   = Addr;
00239   Offset = CurDAG->getTargetConstant(0, ValTy);
00240   return true;
00241 }
00242 
00243 /// Select instructions not customized! Used for
00244 /// expanded, promoted and normal instructions
00245 std::pair<bool, SDNode*> Mips16DAGToDAGISel::selectNode(SDNode *Node) {
00246   unsigned Opcode = Node->getOpcode();
00247   SDLoc DL(Node);
00248 
00249   ///
00250   // Instruction Selection not handled by the auto-generated
00251   // tablegen selection should be handled here.
00252   ///
00253   EVT NodeTy = Node->getValueType(0);
00254   unsigned MultOpc;
00255 
00256   switch(Opcode) {
00257   default: break;
00258 
00259   case ISD::SUBE:
00260   case ISD::ADDE: {
00261     SDValue InFlag = Node->getOperand(2), CmpLHS;
00262     unsigned Opc = InFlag.getOpcode(); (void)Opc;
00263     assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
00264             (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
00265            "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
00266 
00267     unsigned MOp;
00268     if (Opcode == ISD::ADDE) {
00269       CmpLHS = InFlag.getValue(0);
00270       MOp = Mips::AdduRxRyRz16;
00271     } else {
00272       CmpLHS = InFlag.getOperand(0);
00273       MOp = Mips::SubuRxRyRz16;
00274     }
00275 
00276     SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
00277 
00278     SDValue LHS = Node->getOperand(0);
00279     SDValue RHS = Node->getOperand(1);
00280 
00281     EVT VT = LHS.getValueType();
00282 
00283     unsigned Sltu_op = Mips::SltuRxRyRz16;
00284     SDNode *Carry = CurDAG->getMachineNode(Sltu_op, DL, VT, Ops);
00285     unsigned Addu_op = Mips::AdduRxRyRz16;
00286     SDNode *AddCarry = CurDAG->getMachineNode(Addu_op, DL, VT,
00287                                               SDValue(Carry,0), RHS);
00288 
00289     SDNode *Result = CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue, LHS,
00290                                           SDValue(AddCarry,0));
00291     return std::make_pair(true, Result);
00292   }
00293 
00294   /// Mul with two results
00295   case ISD::SMUL_LOHI:
00296   case ISD::UMUL_LOHI: {
00297     MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MultuRxRy16 : Mips::MultRxRy16);
00298     std::pair<SDNode*, SDNode*> LoHi = selectMULT(Node, MultOpc, DL, NodeTy,
00299                                                   true, true);
00300     if (!SDValue(Node, 0).use_empty())
00301       ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
00302 
00303     if (!SDValue(Node, 1).use_empty())
00304       ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
00305 
00306     return std::make_pair(true, nullptr);
00307   }
00308 
00309   case ISD::MULHS:
00310   case ISD::MULHU: {
00311     MultOpc = (Opcode == ISD::MULHU ? Mips::MultuRxRy16 : Mips::MultRxRy16);
00312     SDNode *Result = selectMULT(Node, MultOpc, DL, NodeTy, false, true).second;
00313     return std::make_pair(true, Result);
00314   }
00315   }
00316 
00317   return std::make_pair(false, nullptr);
00318 }
00319 
00320 FunctionPass *llvm::createMips16ISelDag(MipsTargetMachine &TM) {
00321   return new Mips16DAGToDAGISel(TM);
00322 }