LLVM API Documentation

SparcISelDAGToDAG.cpp
Go to the documentation of this file.
00001 //===-- SparcISelDAGToDAG.cpp - A dag to dag inst selector for Sparc ------===//
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 defines an instruction selector for the SPARC target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "SparcTargetMachine.h"
00015 #include "llvm/CodeGen/SelectionDAGISel.h"
00016 #include "llvm/IR/Intrinsics.h"
00017 #include "llvm/Support/Compiler.h"
00018 #include "llvm/Support/Debug.h"
00019 #include "llvm/Support/ErrorHandling.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 using namespace llvm;
00022 
00023 //===----------------------------------------------------------------------===//
00024 // Instruction Selector Implementation
00025 //===----------------------------------------------------------------------===//
00026 
00027 //===--------------------------------------------------------------------===//
00028 /// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
00029 /// instructions for SelectionDAG operations.
00030 ///
00031 namespace {
00032 class SparcDAGToDAGISel : public SelectionDAGISel {
00033   /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
00034   /// make the right decision when generating code for different targets.
00035   const SparcSubtarget &Subtarget;
00036   SparcTargetMachine &TM;
00037 public:
00038   explicit SparcDAGToDAGISel(SparcTargetMachine &tm)
00039     : SelectionDAGISel(tm),
00040       Subtarget(tm.getSubtarget<SparcSubtarget>()),
00041       TM(tm) {
00042   }
00043 
00044   SDNode *Select(SDNode *N) override;
00045 
00046   // Complex Pattern Selectors.
00047   bool SelectADDRrr(SDValue N, SDValue &R1, SDValue &R2);
00048   bool SelectADDRri(SDValue N, SDValue &Base, SDValue &Offset);
00049 
00050   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
00051   /// inline asm expressions.
00052   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
00053                                     char ConstraintCode,
00054                                     std::vector<SDValue> &OutOps) override;
00055 
00056   const char *getPassName() const override {
00057     return "SPARC DAG->DAG Pattern Instruction Selection";
00058   }
00059 
00060   // Include the pieces autogenerated from the target description.
00061 #include "SparcGenDAGISel.inc"
00062 
00063 private:
00064   SDNode* getGlobalBaseReg();
00065 };
00066 }  // end anonymous namespace
00067 
00068 SDNode* SparcDAGToDAGISel::getGlobalBaseReg() {
00069   unsigned GlobalBaseReg =
00070       TM.getSubtargetImpl()->getInstrInfo()->getGlobalBaseReg(MF);
00071   return CurDAG->getRegister(GlobalBaseReg,
00072                              getTargetLowering()->getPointerTy()).getNode();
00073 }
00074 
00075 bool SparcDAGToDAGISel::SelectADDRri(SDValue Addr,
00076                                      SDValue &Base, SDValue &Offset) {
00077   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
00078     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
00079                                        getTargetLowering()->getPointerTy());
00080     Offset = CurDAG->getTargetConstant(0, MVT::i32);
00081     return true;
00082   }
00083   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
00084       Addr.getOpcode() == ISD::TargetGlobalAddress ||
00085       Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
00086     return false;  // direct calls.
00087 
00088   if (Addr.getOpcode() == ISD::ADD) {
00089     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
00090       if (isInt<13>(CN->getSExtValue())) {
00091         if (FrameIndexSDNode *FIN =
00092                 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
00093           // Constant offset from frame ref.
00094           Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
00095                                            getTargetLowering()->getPointerTy());
00096         } else {
00097           Base = Addr.getOperand(0);
00098         }
00099         Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
00100         return true;
00101       }
00102     }
00103     if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
00104       Base = Addr.getOperand(1);
00105       Offset = Addr.getOperand(0).getOperand(0);
00106       return true;
00107     }
00108     if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
00109       Base = Addr.getOperand(0);
00110       Offset = Addr.getOperand(1).getOperand(0);
00111       return true;
00112     }
00113   }
00114   Base = Addr;
00115   Offset = CurDAG->getTargetConstant(0, MVT::i32);
00116   return true;
00117 }
00118 
00119 bool SparcDAGToDAGISel::SelectADDRrr(SDValue Addr, SDValue &R1, SDValue &R2) {
00120   if (Addr.getOpcode() == ISD::FrameIndex) return false;
00121   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
00122       Addr.getOpcode() == ISD::TargetGlobalAddress ||
00123       Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
00124     return false;  // direct calls.
00125 
00126   if (Addr.getOpcode() == ISD::ADD) {
00127     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
00128       if (isInt<13>(CN->getSExtValue()))
00129         return false;  // Let the reg+imm pattern catch this!
00130     if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
00131         Addr.getOperand(1).getOpcode() == SPISD::Lo)
00132       return false;  // Let the reg+imm pattern catch this!
00133     R1 = Addr.getOperand(0);
00134     R2 = Addr.getOperand(1);
00135     return true;
00136   }
00137 
00138   R1 = Addr;
00139   R2 = CurDAG->getRegister(SP::G0, getTargetLowering()->getPointerTy());
00140   return true;
00141 }
00142 
00143 SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
00144   SDLoc dl(N);
00145   if (N->isMachineOpcode()) {
00146     N->setNodeId(-1);
00147     return nullptr;   // Already selected.
00148   }
00149 
00150   switch (N->getOpcode()) {
00151   default: break;
00152   case SPISD::GLOBAL_BASE_REG:
00153     return getGlobalBaseReg();
00154 
00155   case ISD::SDIV:
00156   case ISD::UDIV: {
00157     // sdivx / udivx handle 64-bit divides.
00158     if (N->getValueType(0) == MVT::i64)
00159       break;
00160     // FIXME: should use a custom expander to expose the SRA to the dag.
00161     SDValue DivLHS = N->getOperand(0);
00162     SDValue DivRHS = N->getOperand(1);
00163 
00164     // Set the Y register to the high-part.
00165     SDValue TopPart;
00166     if (N->getOpcode() == ISD::SDIV) {
00167       TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
00168                                    CurDAG->getTargetConstant(31, MVT::i32)), 0);
00169     } else {
00170       TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
00171     }
00172     TopPart = SDValue(CurDAG->getMachineNode(SP::WRYrr, dl, MVT::Glue, TopPart,
00173                                      CurDAG->getRegister(SP::G0, MVT::i32)), 0);
00174 
00175     // FIXME: Handle div by immediate.
00176     unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
00177     return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
00178                                 TopPart);
00179   }
00180   case ISD::MULHU:
00181   case ISD::MULHS: {
00182     // FIXME: Handle mul by immediate.
00183     SDValue MulLHS = N->getOperand(0);
00184     SDValue MulRHS = N->getOperand(1);
00185     unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
00186     SDNode *Mul = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Glue,
00187                                          MulLHS, MulRHS);
00188     // The high part is in the Y register.
00189     return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
00190   }
00191   }
00192 
00193   return SelectCode(N);
00194 }
00195 
00196 
00197 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
00198 /// inline asm expressions.
00199 bool
00200 SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
00201                                                 char ConstraintCode,
00202                                                 std::vector<SDValue> &OutOps) {
00203   SDValue Op0, Op1;
00204   switch (ConstraintCode) {
00205   default: return true;
00206   case 'm':   // memory
00207    if (!SelectADDRrr(Op, Op0, Op1))
00208      SelectADDRri(Op, Op0, Op1);
00209    break;
00210   }
00211 
00212   OutOps.push_back(Op0);
00213   OutOps.push_back(Op1);
00214   return false;
00215 }
00216 
00217 /// createSparcISelDag - This pass converts a legalized DAG into a
00218 /// SPARC-specific DAG, ready for instruction scheduling.
00219 ///
00220 FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
00221   return new SparcDAGToDAGISel(TM);
00222 }