LLVM API Documentation

MSP430ISelDAGToDAG.cpp

Go to the documentation of this file.
00001 //===-- MSP430ISelDAGToDAG.cpp - A dag to dag inst selector for MSP430 ----===//
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 MSP430 target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "MSP430.h"
00015 #include "MSP430ISelLowering.h"
00016 #include "MSP430TargetMachine.h"
00017 #include "llvm/DerivedTypes.h"
00018 #include "llvm/Function.h"
00019 #include "llvm/Intrinsics.h"
00020 #include "llvm/CallingConv.h"
00021 #include "llvm/Constants.h"
00022 #include "llvm/CodeGen/MachineFrameInfo.h"
00023 #include "llvm/CodeGen/MachineFunction.h"
00024 #include "llvm/CodeGen/MachineInstrBuilder.h"
00025 #include "llvm/CodeGen/MachineRegisterInfo.h"
00026 #include "llvm/CodeGen/SelectionDAG.h"
00027 #include "llvm/CodeGen/SelectionDAGISel.h"
00028 #include "llvm/Target/TargetLowering.h"
00029 #include "llvm/Support/Compiler.h"
00030 #include "llvm/Support/Debug.h"
00031 #include "llvm/Support/ErrorHandling.h"
00032 #include "llvm/Support/raw_ostream.h"
00033 using namespace llvm;
00034 
00035 /// MSP430DAGToDAGISel - MSP430 specific code to select MSP430 machine
00036 /// instructions for SelectionDAG operations.
00037 ///
00038 namespace {
00039   class MSP430DAGToDAGISel : public SelectionDAGISel {
00040     MSP430TargetLowering &Lowering;
00041     const MSP430Subtarget &Subtarget;
00042 
00043   public:
00044     MSP430DAGToDAGISel(MSP430TargetMachine &TM, CodeGenOpt::Level OptLevel)
00045       : SelectionDAGISel(TM, OptLevel),
00046         Lowering(*TM.getTargetLowering()),
00047         Subtarget(*TM.getSubtargetImpl()) { }
00048 
00049     virtual void InstructionSelect();
00050 
00051     virtual const char *getPassName() const {
00052       return "MSP430 DAG->DAG Pattern Instruction Selection";
00053     }
00054 
00055     // Include the pieces autogenerated from the target description.
00056   #include "MSP430GenDAGISel.inc"
00057 
00058   private:
00059     SDNode *Select(SDValue Op);
00060     bool SelectAddr(SDValue Op, SDValue Addr, SDValue &Base, SDValue &Disp);
00061 
00062   #ifndef NDEBUG
00063     unsigned Indent;
00064   #endif
00065   };
00066 }  // end anonymous namespace
00067 
00068 /// createMSP430ISelDag - This pass converts a legalized DAG into a
00069 /// MSP430-specific DAG, ready for instruction scheduling.
00070 ///
00071 FunctionPass *llvm::createMSP430ISelDag(MSP430TargetMachine &TM,
00072                                         CodeGenOpt::Level OptLevel) {
00073   return new MSP430DAGToDAGISel(TM, OptLevel);
00074 }
00075 
00076 // FIXME: This is pretty dummy routine and needs to be rewritten in the future.
00077 bool MSP430DAGToDAGISel::SelectAddr(SDValue Op, SDValue Addr,
00078                                     SDValue &Base, SDValue &Disp) {
00079   // Try to match frame address first.
00080   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
00081     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i16);
00082     Disp = CurDAG->getTargetConstant(0, MVT::i16);
00083     return true;
00084   }
00085 
00086   switch (Addr.getOpcode()) {
00087   case ISD::ADD:
00088    // Operand is a result from ADD with constant operand which fits into i16.
00089    if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
00090       uint64_t CVal = CN->getZExtValue();
00091       // Offset should fit into 16 bits.
00092       if (((CVal << 48) >> 48) == CVal) {
00093         SDValue N0 = Addr.getOperand(0);
00094         if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N0))
00095           Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i16);
00096         else
00097           Base = N0;
00098 
00099         Disp = CurDAG->getTargetConstant(CVal, MVT::i16);
00100         return true;
00101       }
00102     }
00103     break;
00104   case MSP430ISD::Wrapper:
00105     SDValue N0 = Addr.getOperand(0);
00106     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
00107       Base = CurDAG->getTargetGlobalAddress(G->getGlobal(),
00108                                             MVT::i16, G->getOffset());
00109       Disp = CurDAG->getTargetConstant(0, MVT::i16);
00110       return true;
00111     } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(N0)) {
00112       Base = CurDAG->getTargetExternalSymbol(E->getSymbol(), MVT::i16);
00113       Disp = CurDAG->getTargetConstant(0, MVT::i16);
00114     }
00115     break;
00116   };
00117 
00118   Base = Addr;
00119   Disp = CurDAG->getTargetConstant(0, MVT::i16);
00120 
00121   return true;
00122 }
00123 
00124 
00125 
00126 /// InstructionSelect - This callback is invoked by
00127 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
00128 void MSP430DAGToDAGISel::InstructionSelect() {
00129   DEBUG(BB->dump());
00130 
00131   // Codegen the basic block.
00132 #ifndef NDEBUG
00133   DOUT << "===== Instruction selection begins:\n";
00134   Indent = 0;
00135 #endif
00136   SelectRoot(*CurDAG);
00137 #ifndef NDEBUG
00138   DOUT << "===== Instruction selection ends:\n";
00139 #endif
00140 
00141   CurDAG->RemoveDeadNodes();
00142 }
00143 
00144 SDNode *MSP430DAGToDAGISel::Select(SDValue Op) {
00145   SDNode *Node = Op.getNode();
00146   DebugLoc dl = Op.getDebugLoc();
00147 
00148   // Dump information about the Node being selected
00149   #ifndef NDEBUG
00150   DOUT << std::string(Indent, ' ') << "Selecting: ";
00151   DEBUG(Node->dump(CurDAG));
00152   DOUT << "\n";
00153   Indent += 2;
00154   #endif
00155 
00156   // If we have a custom node, we already have selected!
00157   if (Node->isMachineOpcode()) {
00158     #ifndef NDEBUG
00159     DOUT << std::string(Indent-2, ' ') << "== ";
00160     DEBUG(Node->dump(CurDAG));
00161     DOUT << "\n";
00162     Indent -= 2;
00163     #endif
00164     return NULL;
00165   }
00166 
00167   // Few custom selection stuff.
00168   switch (Node->getOpcode()) {
00169   default: break;
00170   case ISD::FrameIndex: {
00171     assert(Op.getValueType() == MVT::i16);
00172     int FI = cast<FrameIndexSDNode>(Node)->getIndex();
00173     SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i16);
00174     if (Node->hasOneUse())
00175       return CurDAG->SelectNodeTo(Node, MSP430::ADD16ri, MVT::i16,
00176                                   TFI, CurDAG->getTargetConstant(0, MVT::i16));
00177     return CurDAG->getTargetNode(MSP430::ADD16ri, dl, MVT::i16,
00178                                  TFI, CurDAG->getTargetConstant(0, MVT::i16));
00179   }
00180   }
00181 
00182   // Select the default instruction
00183   SDNode *ResNode = SelectCode(Op);
00184 
00185   #ifndef NDEBUG
00186   DOUT << std::string(Indent-2, ' ') << "=> ";
00187   if (ResNode == NULL || ResNode == Op.getNode())
00188     DEBUG(Op.getNode()->dump(CurDAG));
00189   else
00190     DEBUG(ResNode->dump(CurDAG));
00191   DOUT << "\n";
00192   Indent -= 2;
00193   #endif
00194 
00195   return ResNode;
00196 }