LLVM API Documentation

BlackfinISelDAGToDAG.cpp

Go to the documentation of this file.
00001 //===- BlackfinISelDAGToDAG.cpp - A dag to dag inst selector for Blackfin -===//
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 Blackfin target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "Blackfin.h"
00015 #include "BlackfinISelLowering.h"
00016 #include "BlackfinTargetMachine.h"
00017 #include "BlackfinRegisterInfo.h"
00018 #include "llvm/Intrinsics.h"
00019 #include "llvm/CodeGen/SelectionDAGISel.h"
00020 #include "llvm/CodeGen/MachineRegisterInfo.h"
00021 #include "llvm/Support/Compiler.h"
00022 #include "llvm/Support/Debug.h"
00023 #include "llvm/Support/ErrorHandling.h"
00024 #include "llvm/Support/raw_ostream.h"
00025 
00026 using namespace llvm;
00027 
00028 //===----------------------------------------------------------------------===//
00029 // Instruction Selector Implementation
00030 //===----------------------------------------------------------------------===//
00031 
00032 //===----------------------------------------------------------------------===//
00033 /// BlackfinDAGToDAGISel - Blackfin specific code to select blackfin machine
00034 /// instructions for SelectionDAG operations.
00035 namespace {
00036   class BlackfinDAGToDAGISel : public SelectionDAGISel {
00037     /// Subtarget - Keep a pointer to the Blackfin Subtarget around so that we
00038     /// can make the right decision when generating code for different targets.
00039     //const BlackfinSubtarget &Subtarget;
00040   public:
00041     BlackfinDAGToDAGISel(BlackfinTargetMachine &TM, CodeGenOpt::Level OptLevel)
00042       : SelectionDAGISel(TM, OptLevel) {}
00043 
00044     virtual void InstructionSelect();
00045 
00046     virtual const char *getPassName() const {
00047       return "Blackfin DAG->DAG Pattern Instruction Selection";
00048     }
00049 
00050     // Include the pieces autogenerated from the target description.
00051 #include "BlackfinGenDAGISel.inc"
00052 
00053   private:
00054     SDNode *Select(SDValue Op);
00055     bool SelectADDRspii(SDValue Op, SDValue Addr,
00056                         SDValue &Base, SDValue &Offset);
00057 
00058     // Walk the DAG after instruction selection, fixing register class issues.
00059     void FixRegisterClasses(SelectionDAG &DAG);
00060 
00061     const BlackfinInstrInfo &getInstrInfo() {
00062       return *static_cast<const BlackfinTargetMachine&>(TM).getInstrInfo();
00063     }
00064     const BlackfinRegisterInfo *getRegisterInfo() {
00065       return static_cast<const BlackfinTargetMachine&>(TM).getRegisterInfo();
00066     }
00067   };
00068 }  // end anonymous namespace
00069 
00070 FunctionPass *llvm::createBlackfinISelDag(BlackfinTargetMachine &TM,
00071                                           CodeGenOpt::Level OptLevel) {
00072   return new BlackfinDAGToDAGISel(TM, OptLevel);
00073 }
00074 
00075 /// InstructionSelect - This callback is invoked by
00076 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
00077 void BlackfinDAGToDAGISel::InstructionSelect() {
00078   // Select target instructions for the DAG.
00079   SelectRoot(*CurDAG);
00080   DOUT << "Selected selection DAG before regclass fixup:\n";
00081   DEBUG(CurDAG->dump());
00082   FixRegisterClasses(*CurDAG);
00083 }
00084 
00085 SDNode *BlackfinDAGToDAGISel::Select(SDValue Op) {
00086   SDNode *N = Op.getNode();
00087   DebugLoc dl = N->getDebugLoc();
00088   if (N->isMachineOpcode())
00089     return NULL;   // Already selected.
00090 
00091   switch (N->getOpcode()) {
00092   default: break;
00093   case ISD::FrameIndex: {
00094     // Selects to ADDpp FI, 0 which in turn will become ADDimm7 SP, imm or ADDpp
00095     // SP, Px
00096     int FI = cast<FrameIndexSDNode>(N)->getIndex();
00097     SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i32);
00098     return CurDAG->SelectNodeTo(N, BF::ADDpp, MVT::i32, TFI,
00099                                 CurDAG->getTargetConstant(0, MVT::i32));
00100   }
00101   }
00102 
00103   return SelectCode(Op);
00104 }
00105 
00106 bool BlackfinDAGToDAGISel::SelectADDRspii(SDValue Op,
00107                                           SDValue Addr,
00108                                           SDValue &Base,
00109                                           SDValue &Offset) {
00110   FrameIndexSDNode *FIN = 0;
00111   if ((FIN = dyn_cast<FrameIndexSDNode>(Addr))) {
00112     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
00113     Offset = CurDAG->getTargetConstant(0, MVT::i32);
00114     return true;
00115   }
00116   if (Addr.getOpcode() == ISD::ADD) {
00117     ConstantSDNode *CN = 0;
00118     if ((FIN = dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) &&
00119         (CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) &&
00120         (CN->getSExtValue() % 4 == 0 && CN->getSExtValue() >= 0)) {
00121       // Constant positive word offset from frame index
00122       Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
00123       Offset = CurDAG->getTargetConstant(CN->getSExtValue(), MVT::i32);
00124       return true;
00125     }
00126   }
00127   return false;
00128 }
00129 
00130 static inline bool isCC(const TargetRegisterClass *RC) {
00131   return RC == &BF::AnyCCRegClass || BF::AnyCCRegClass.hasSubClass(RC);
00132 }
00133 
00134 static inline bool isDCC(const TargetRegisterClass *RC) {
00135   return RC == &BF::DRegClass || BF::DRegClass.hasSubClass(RC) || isCC(RC);
00136 }
00137 
00138 static void UpdateNodeOperand(SelectionDAG &DAG,
00139                               SDNode *N,
00140                               unsigned Num,
00141                               SDValue Val) {
00142   SmallVector<SDValue, 8> ops(N->op_begin(), N->op_end());
00143   ops[Num] = Val;
00144   SDValue New = DAG.UpdateNodeOperands(SDValue(N, 0), ops.data(), ops.size());
00145   DAG.ReplaceAllUsesWith(N, New.getNode());
00146 }
00147 
00148 // After instruction selection, insert COPY_TO_REGCLASS nodes to help in
00149 // choosing the proper register classes.
00150 void BlackfinDAGToDAGISel::FixRegisterClasses(SelectionDAG &DAG) {
00151   const BlackfinInstrInfo &TII = getInstrInfo();
00152   const BlackfinRegisterInfo *TRI = getRegisterInfo();
00153   DAG.AssignTopologicalOrder();
00154   HandleSDNode Dummy(DAG.getRoot());
00155 
00156   for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin();
00157        NI != DAG.allnodes_end(); ++NI) {
00158     if (NI->use_empty() || !NI->isMachineOpcode())
00159       continue;
00160     const TargetInstrDesc &DefTID = TII.get(NI->getMachineOpcode());
00161     for (SDNode::use_iterator UI = NI->use_begin(); !UI.atEnd(); ++UI) {
00162       if (!UI->isMachineOpcode())
00163         continue;
00164 
00165       if (UI.getUse().getResNo() >= DefTID.getNumDefs())
00166         continue;
00167       const TargetRegisterClass *DefRC =
00168         DefTID.OpInfo[UI.getUse().getResNo()].getRegClass(TRI);
00169 
00170       const TargetInstrDesc &UseTID = TII.get(UI->getMachineOpcode());
00171       if (UseTID.getNumDefs()+UI.getOperandNo() >= UseTID.getNumOperands())
00172         continue;
00173       const TargetRegisterClass *UseRC =
00174         UseTID.OpInfo[UseTID.getNumDefs()+UI.getOperandNo()].getRegClass(TRI);
00175       if (!DefRC || !UseRC)
00176         continue;
00177       // We cannot copy CC <-> !(CC/D)
00178       if ((isCC(DefRC) && !isDCC(UseRC)) || (isCC(UseRC) && !isDCC(DefRC))) {
00179         SDNode *Copy =
00180           DAG.getTargetNode(TargetInstrInfo::COPY_TO_REGCLASS,
00181                             NI->getDebugLoc(),
00182                             MVT::i32,
00183                             UI.getUse().get(),
00184                             DAG.getTargetConstant(BF::DRegClassID, MVT::i32));
00185         UpdateNodeOperand(DAG, *UI, UI.getOperandNo(), SDValue(Copy, 0));
00186       }
00187     }
00188   }
00189   DAG.setRoot(Dummy.getValue());
00190 }
00191