MSP430ISelDAGToDAG.cpp revision 193323
1//===-- MSP430ISelDAGToDAG.cpp - A dag to dag inst selector for MSP430 ----===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the MSP430 target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MSP430.h"
15#include "MSP430ISelLowering.h"
16#include "MSP430TargetMachine.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Intrinsics.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/Target/TargetLowering.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/Debug.h"
31using namespace llvm;
32
33/// MSP430DAGToDAGISel - MSP430 specific code to select MSP430 machine
34/// instructions for SelectionDAG operations.
35///
36namespace {
37  class MSP430DAGToDAGISel : public SelectionDAGISel {
38    MSP430TargetLowering &Lowering;
39    const MSP430Subtarget &Subtarget;
40
41  public:
42    MSP430DAGToDAGISel(MSP430TargetMachine &TM, CodeGenOpt::Level OptLevel)
43      : SelectionDAGISel(TM, OptLevel),
44        Lowering(*TM.getTargetLowering()),
45        Subtarget(*TM.getSubtargetImpl()) { }
46
47    virtual void InstructionSelect();
48
49    virtual const char *getPassName() const {
50      return "MSP430 DAG->DAG Pattern Instruction Selection";
51    }
52
53    // Include the pieces autogenerated from the target description.
54  #include "MSP430GenDAGISel.inc"
55
56  private:
57    SDNode *Select(SDValue Op);
58    bool SelectAddr(SDValue Op, SDValue Addr, SDValue &Base, SDValue &Disp);
59
60  #ifndef NDEBUG
61    unsigned Indent;
62  #endif
63  };
64}  // end anonymous namespace
65
66/// createMSP430ISelDag - This pass converts a legalized DAG into a
67/// MSP430-specific DAG, ready for instruction scheduling.
68///
69FunctionPass *llvm::createMSP430ISelDag(MSP430TargetMachine &TM,
70                                        CodeGenOpt::Level OptLevel) {
71  return new MSP430DAGToDAGISel(TM, OptLevel);
72}
73
74// FIXME: This is pretty dummy routine and needs to be rewritten in the future.
75bool MSP430DAGToDAGISel::SelectAddr(SDValue Op, SDValue Addr,
76                                    SDValue &Base, SDValue &Disp) {
77  // Try to match frame address first.
78  if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
79    Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i16);
80    Disp = CurDAG->getTargetConstant(0, MVT::i16);
81    return true;
82  }
83
84  switch (Addr.getOpcode()) {
85  case ISD::ADD:
86   // Operand is a result from ADD with constant operand which fits into i16.
87   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
88      uint64_t CVal = CN->getZExtValue();
89      // Offset should fit into 16 bits.
90      if (((CVal << 48) >> 48) == CVal) {
91        SDValue N0 = Addr.getOperand(0);
92        if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N0))
93          Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i16);
94        else
95          Base = N0;
96
97        Disp = CurDAG->getTargetConstant(CVal, MVT::i16);
98        return true;
99      }
100    }
101    break;
102  case MSP430ISD::Wrapper:
103    SDValue N0 = Addr.getOperand(0);
104    if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
105      Base = CurDAG->getTargetGlobalAddress(G->getGlobal(),
106                                            MVT::i16, G->getOffset());
107      Disp = CurDAG->getTargetConstant(0, MVT::i16);
108      return true;
109    } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(N0)) {
110      Base = CurDAG->getTargetExternalSymbol(E->getSymbol(), MVT::i16);
111      Disp = CurDAG->getTargetConstant(0, MVT::i16);
112    }
113    break;
114  };
115
116  Base = Addr;
117  Disp = CurDAG->getTargetConstant(0, MVT::i16);
118
119  return true;
120}
121
122
123
124/// InstructionSelect - This callback is invoked by
125/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
126void MSP430DAGToDAGISel::InstructionSelect() {
127  DEBUG(BB->dump());
128
129  // Codegen the basic block.
130#ifndef NDEBUG
131  DOUT << "===== Instruction selection begins:\n";
132  Indent = 0;
133#endif
134  SelectRoot(*CurDAG);
135#ifndef NDEBUG
136  DOUT << "===== Instruction selection ends:\n";
137#endif
138
139  CurDAG->RemoveDeadNodes();
140}
141
142SDNode *MSP430DAGToDAGISel::Select(SDValue Op) {
143  SDNode *Node = Op.getNode();
144  DebugLoc dl = Op.getDebugLoc();
145
146  // Dump information about the Node being selected
147  #ifndef NDEBUG
148  DOUT << std::string(Indent, ' ') << "Selecting: ";
149  DEBUG(Node->dump(CurDAG));
150  DOUT << "\n";
151  Indent += 2;
152  #endif
153
154  // If we have a custom node, we already have selected!
155  if (Node->isMachineOpcode()) {
156    #ifndef NDEBUG
157    DOUT << std::string(Indent-2, ' ') << "== ";
158    DEBUG(Node->dump(CurDAG));
159    DOUT << "\n";
160    Indent -= 2;
161    #endif
162    return NULL;
163  }
164
165  // Few custom selection stuff.
166  switch (Node->getOpcode()) {
167  default: break;
168  case ISD::FrameIndex: {
169    assert(Op.getValueType() == MVT::i16);
170    int FI = cast<FrameIndexSDNode>(Node)->getIndex();
171    SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i16);
172    if (Node->hasOneUse())
173      return CurDAG->SelectNodeTo(Node, MSP430::ADD16ri, MVT::i16,
174                                  TFI, CurDAG->getTargetConstant(0, MVT::i16));
175    return CurDAG->getTargetNode(MSP430::ADD16ri, dl, MVT::i16,
176                                 TFI, CurDAG->getTargetConstant(0, MVT::i16));
177  }
178  }
179
180  // Select the default instruction
181  SDNode *ResNode = SelectCode(Op);
182
183  #ifndef NDEBUG
184  DOUT << std::string(Indent-2, ' ') << "=> ";
185  if (ResNode == NULL || ResNode == Op.getNode())
186    DEBUG(Op.getNode()->dump(CurDAG));
187  else
188    DEBUG(ResNode->dump(CurDAG));
189  DOUT << "\n";
190  Indent -= 2;
191  #endif
192
193  return ResNode;
194}
195