LLVM API Documentation

XCoreISelDAGToDAG.cpp

Go to the documentation of this file.
00001 //===-- XCoreISelDAGToDAG.cpp - A dag to dag inst selector for XCore ------===//
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 XCore target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "XCore.h"
00015 #include "XCoreISelLowering.h"
00016 #include "XCoreTargetMachine.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/LLVMContext.h"
00023 #include "llvm/CodeGen/MachineFrameInfo.h"
00024 #include "llvm/CodeGen/MachineFunction.h"
00025 #include "llvm/CodeGen/MachineInstrBuilder.h"
00026 #include "llvm/CodeGen/MachineRegisterInfo.h"
00027 #include "llvm/CodeGen/SelectionDAG.h"
00028 #include "llvm/CodeGen/SelectionDAGISel.h"
00029 #include "llvm/Target/TargetLowering.h"
00030 #include "llvm/Support/Compiler.h"
00031 #include "llvm/Support/Debug.h"
00032 #include "llvm/Support/ErrorHandling.h"
00033 #include "llvm/Support/raw_ostream.h"
00034 #include <queue>
00035 #include <set>
00036 using namespace llvm;
00037 
00038 /// XCoreDAGToDAGISel - XCore specific code to select XCore machine
00039 /// instructions for SelectionDAG operations.
00040 ///
00041 namespace {
00042   class XCoreDAGToDAGISel : public SelectionDAGISel {
00043     XCoreTargetLowering &Lowering;
00044     const XCoreSubtarget &Subtarget;
00045 
00046   public:
00047     XCoreDAGToDAGISel(XCoreTargetMachine &TM)
00048       : SelectionDAGISel(TM),
00049         Lowering(*TM.getTargetLowering()), 
00050         Subtarget(*TM.getSubtargetImpl()) { }
00051 
00052     SDNode *Select(SDValue Op);
00053     
00054     /// getI32Imm - Return a target constant with the specified value, of type
00055     /// i32.
00056     inline SDValue getI32Imm(unsigned Imm) {
00057       return CurDAG->getTargetConstant(Imm, MVT::i32);
00058     }
00059 
00060     // Complex Pattern Selectors.
00061     bool SelectADDRspii(SDValue Op, SDValue Addr, SDValue &Base,
00062                         SDValue &Offset);
00063     bool SelectADDRdpii(SDValue Op, SDValue Addr, SDValue &Base,
00064                         SDValue &Offset);
00065     bool SelectADDRcpii(SDValue Op, SDValue Addr, SDValue &Base,
00066                         SDValue &Offset);
00067     
00068     virtual void InstructionSelect();
00069 
00070     virtual const char *getPassName() const {
00071       return "XCore DAG->DAG Pattern Instruction Selection";
00072     } 
00073     
00074     // Include the pieces autogenerated from the target description.
00075   #include "XCoreGenDAGISel.inc"
00076   };
00077 }  // end anonymous namespace
00078 
00079 /// createXCoreISelDag - This pass converts a legalized DAG into a 
00080 /// XCore-specific DAG, ready for instruction scheduling.
00081 ///
00082 FunctionPass *llvm::createXCoreISelDag(XCoreTargetMachine &TM) {
00083   return new XCoreDAGToDAGISel(TM);
00084 }
00085 
00086 bool XCoreDAGToDAGISel::SelectADDRspii(SDValue Op, SDValue Addr,
00087                                   SDValue &Base, SDValue &Offset) {
00088   FrameIndexSDNode *FIN = 0;
00089   if ((FIN = dyn_cast<FrameIndexSDNode>(Addr))) {
00090     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
00091     Offset = CurDAG->getTargetConstant(0, MVT::i32);
00092     return true;
00093   }
00094   if (Addr.getOpcode() == ISD::ADD) {
00095     ConstantSDNode *CN = 0;
00096     if ((FIN = dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))
00097       && (CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
00098       && (CN->getSExtValue() % 4 == 0 && CN->getSExtValue() >= 0)) {
00099       // Constant positive word offset from frame index
00100       Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
00101       Offset = CurDAG->getTargetConstant(CN->getSExtValue(), MVT::i32);
00102       return true;
00103     }
00104   }
00105   return false;
00106 }
00107 
00108 bool XCoreDAGToDAGISel::SelectADDRdpii(SDValue Op, SDValue Addr,
00109                                   SDValue &Base, SDValue &Offset) {
00110   if (Addr.getOpcode() == XCoreISD::DPRelativeWrapper) {
00111     Base = Addr.getOperand(0);
00112     Offset = CurDAG->getTargetConstant(0, MVT::i32);
00113     return true;
00114   }
00115   if (Addr.getOpcode() == ISD::ADD) {
00116     ConstantSDNode *CN = 0;
00117     if ((Addr.getOperand(0).getOpcode() == XCoreISD::DPRelativeWrapper)
00118       && (CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
00119       && (CN->getSExtValue() % 4 == 0)) {
00120       // Constant word offset from a object in the data region
00121       Base = Addr.getOperand(0).getOperand(0);
00122       Offset = CurDAG->getTargetConstant(CN->getSExtValue(), MVT::i32);
00123       return true;
00124     }
00125   }
00126   return false;
00127 }
00128 
00129 bool XCoreDAGToDAGISel::SelectADDRcpii(SDValue Op, SDValue Addr,
00130                                   SDValue &Base, SDValue &Offset) {
00131   if (Addr.getOpcode() == XCoreISD::CPRelativeWrapper) {
00132     Base = Addr.getOperand(0);
00133     Offset = CurDAG->getTargetConstant(0, MVT::i32);
00134     return true;
00135   }
00136   if (Addr.getOpcode() == ISD::ADD) {
00137     ConstantSDNode *CN = 0;
00138     if ((Addr.getOperand(0).getOpcode() == XCoreISD::CPRelativeWrapper)
00139       && (CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
00140       && (CN->getSExtValue() % 4 == 0)) {
00141       // Constant word offset from a object in the data region
00142       Base = Addr.getOperand(0).getOperand(0);
00143       Offset = CurDAG->getTargetConstant(CN->getSExtValue(), MVT::i32);
00144       return true;
00145     }
00146   }
00147   return false;
00148 }
00149 
00150 /// InstructionSelect - This callback is invoked by
00151 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
00152 void XCoreDAGToDAGISel::
00153 InstructionSelect() {
00154   DEBUG(BB->dump());
00155 
00156   // Select target instructions for the DAG.
00157   SelectRoot(*CurDAG);
00158   
00159   CurDAG->RemoveDeadNodes();
00160 }
00161 
00162 SDNode *XCoreDAGToDAGISel::Select(SDValue Op) {
00163   SDNode *N = Op.getNode();
00164   DebugLoc dl = N->getDebugLoc();
00165   EVT NVT = N->getValueType(0);
00166   if (NVT == MVT::i32) {
00167     switch (N->getOpcode()) {
00168       default: break;
00169       case ISD::Constant: {
00170         if (Predicate_immMskBitp(N)) {
00171           SDValue MskSize = Transform_msksize_xform(N);
00172           return CurDAG->getTargetNode(XCore::MKMSK_rus, dl, MVT::i32, MskSize);
00173         }
00174         else if (! Predicate_immU16(N)) {
00175           unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
00176           SDValue CPIdx =
00177             CurDAG->getTargetConstantPool(ConstantInt::get(
00178                                   Type::getInt32Ty(*CurDAG->getContext()), Val),
00179                                           TLI.getPointerTy());
00180           return CurDAG->getTargetNode(XCore::LDWCP_lru6, dl, MVT::i32, 
00181                                        MVT::Other, CPIdx, 
00182                                        CurDAG->getEntryNode());
00183         }
00184         break;
00185       }
00186       case ISD::SMUL_LOHI: {
00187         // FIXME fold addition into the macc instruction
00188         if (!Subtarget.isXS1A()) {
00189           SDValue Zero(CurDAG->getTargetNode(XCore::LDC_ru6, dl, MVT::i32,
00190                                   CurDAG->getTargetConstant(0, MVT::i32)), 0);
00191           SDValue Ops[] = { Zero, Zero, Op.getOperand(0), Op.getOperand(1) };
00192           SDNode *ResNode = CurDAG->getTargetNode(XCore::MACCS_l4r, dl,
00193                                                   MVT::i32, MVT::i32, Ops, 4);
00194           ReplaceUses(SDValue(N, 0), SDValue(ResNode, 1));
00195           ReplaceUses(SDValue(N, 1), SDValue(ResNode, 0));
00196           return NULL;
00197         }
00198         break;
00199       }
00200       case ISD::UMUL_LOHI: {
00201         // FIXME fold addition into the macc / lmul instruction
00202         SDValue Zero(CurDAG->getTargetNode(XCore::LDC_ru6, dl, MVT::i32,
00203                                   CurDAG->getTargetConstant(0, MVT::i32)), 0);
00204         SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
00205                             Zero, Zero };
00206         SDNode *ResNode = CurDAG->getTargetNode(XCore::LMUL_l6r, dl, MVT::i32,
00207                                                 MVT::i32, Ops, 4);
00208         ReplaceUses(SDValue(N, 0), SDValue(ResNode, 1));
00209         ReplaceUses(SDValue(N, 1), SDValue(ResNode, 0));
00210         return NULL;
00211       }
00212       case XCoreISD::LADD: {
00213         if (!Subtarget.isXS1A()) {
00214           SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
00215                               Op.getOperand(2) };
00216           return CurDAG->getTargetNode(XCore::LADD_l5r, dl, MVT::i32, MVT::i32,
00217                                        Ops, 3);
00218         }
00219         break;
00220       }
00221       case XCoreISD::LSUB: {
00222         if (!Subtarget.isXS1A()) {
00223           SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
00224                               Op.getOperand(2) };
00225           return CurDAG->getTargetNode(XCore::LSUB_l5r, dl, MVT::i32, MVT::i32,
00226                                        Ops, 3);
00227         }
00228         break;
00229       }
00230       // Other cases are autogenerated.
00231     }
00232   }
00233   return SelectCode(Op);
00234 }