Small tweaking
[oota-llvm.git] / lib / Target / MSP430 / MSP430ISelDAGToDAG.cpp
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"
31 #include <queue>
32 #include <set>
33 using namespace llvm;
34
35 /// MSP430DAGToDAGISel - MSP430 specific code to select MSP430 machine
36 /// instructions for SelectionDAG operations.
37 ///
38 namespace {
39   class MSP430DAGToDAGISel : public SelectionDAGISel {
40     MSP430TargetLowering &Lowering;
41     const MSP430Subtarget &Subtarget;
42
43   public:
44     MSP430DAGToDAGISel(MSP430TargetMachine &TM)
45       : SelectionDAGISel(TM),
46         Lowering(*TM.getTargetLowering()),
47         Subtarget(*TM.getSubtargetImpl()) { }
48
49     virtual void InstructionSelect();
50
51     virtual const char *getPassName() const {
52       return "MSP430 DAG->DAG Pattern Instruction Selection";
53     }
54
55     // Include the pieces autogenerated from the target description.
56   #include "MSP430GenDAGISel.inc"
57
58   private:
59     SDNode *Select(SDValue Op);
60     bool SelectAddr(SDValue Op, SDValue Addr, SDValue &Base, SDValue &Disp);
61
62   #ifndef NDEBUG
63     unsigned Indent;
64   #endif
65   };
66 }  // end anonymous namespace
67
68 /// createMSP430ISelDag - This pass converts a legalized DAG into a
69 /// MSP430-specific DAG, ready for instruction scheduling.
70 ///
71 FunctionPass *llvm::createMSP430ISelDag(MSP430TargetMachine &TM) {
72   return new MSP430DAGToDAGISel(TM);
73 }
74
75 // FIXME: This is pretty dummy routine and needs to be rewritten in the future.
76 bool MSP430DAGToDAGISel::SelectAddr(SDValue Op, SDValue Addr,
77                                     SDValue &Base, SDValue &Disp) {
78   // Try to match frame address first.
79   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
80     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i16);
81     Disp = CurDAG->getTargetConstant(0, MVT::i16);
82     return true;
83   }
84
85   switch (Addr.getOpcode()) {
86   case ISD::ADD:
87    // Operand is a result from ADD with constant operand which fits into i16.
88    if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
89       uint64_t CVal = CN->getZExtValue();
90       // Offset should fit into 16 bits.
91       if (((CVal << 48) >> 48) == CVal) {
92         SDValue N0 = Addr.getOperand(0);
93         if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N0))
94           Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i16);
95         else
96           Base = N0;
97
98         Disp = CurDAG->getTargetConstant(CVal, MVT::i16);
99         return true;
100       }
101     }
102     break;
103   case MSP430ISD::Wrapper:
104     SDValue N0 = Addr.getOperand(0);
105     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
106       Base = CurDAG->getTargetGlobalAddress(G->getGlobal(),
107                                             MVT::i16, G->getOffset());
108       Disp = CurDAG->getTargetConstant(0, MVT::i16);
109       return true;
110     }
111     break;
112   };
113
114   Base = Addr;
115   Disp = CurDAG->getTargetConstant(0, MVT::i16);
116
117   return true;
118 }
119
120
121
122 /// InstructionSelect - This callback is invoked by
123 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
124 void MSP430DAGToDAGISel::InstructionSelect() {
125   DEBUG(BB->dump());
126
127   // Select target instructions for the DAG.
128   SelectRoot(*CurDAG);
129
130   CurDAG->RemoveDeadNodes();
131 }
132
133 SDNode *MSP430DAGToDAGISel::Select(SDValue Op) {
134   SDNode *Node = Op.getNode();
135   DebugLoc dl = Op.getDebugLoc();
136
137   // Dump information about the Node being selected
138   #ifndef NDEBUG
139   DOUT << std::string(Indent, ' ') << "Selecting: ";
140   DEBUG(Node->dump(CurDAG));
141   DOUT << "\n";
142   Indent += 2;
143   #endif
144
145   // If we have a custom node, we already have selected!
146   if (Node->isMachineOpcode()) {
147     #ifndef NDEBUG
148     DOUT << std::string(Indent-2, ' ') << "== ";
149     DEBUG(Node->dump(CurDAG));
150     DOUT << "\n";
151     Indent -= 2;
152     #endif
153     return NULL;
154   }
155
156   // Few custom selection stuff.
157   switch (Node->getOpcode()) {
158   default: break;
159   case ISD::FrameIndex: {
160     assert(Op.getValueType() == MVT::i16);
161     int FI = cast<FrameIndexSDNode>(Node)->getIndex();
162     SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i16);
163     if (Node->hasOneUse())
164       return CurDAG->SelectNodeTo(Node, MSP430::ADD16ri, MVT::i16,
165                                   TFI, CurDAG->getTargetConstant(0, MVT::i16));
166     return CurDAG->getTargetNode(MSP430::ADD16ri, dl, MVT::i16,
167                                  TFI, CurDAG->getTargetConstant(0, MVT::i16));
168   }
169   }
170
171   // Select the default instruction
172   SDNode *ResNode = SelectCode(Op);
173
174   #ifndef NDEBUG
175   DOUT << std::string(Indent-2, ' ') << "=> ";
176   if (ResNode == NULL || ResNode == Op.getNode())
177     DEBUG(Op.getNode()->dump(CurDAG));
178   else
179     DEBUG(ResNode->dump(CurDAG));
180   DOUT << "\n";
181   Indent -= 2;
182   #endif
183
184   return ResNode;
185 }