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