blob: c057d69aadcb620db1e29229a077785dd661eb0b [file] [log] [blame]
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +00001//===-- 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>
33using namespace llvm;
34
35/// MSP430DAGToDAGISel - MSP430 specific code to select MSP430 machine
36/// instructions for SelectionDAG operations.
37///
38namespace {
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);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000060 bool SelectAddr(SDValue Op, SDValue Addr, SDValue &Disp, SDValue &Base);
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +000061
62 #ifndef NDEBUG
63 unsigned Indent;
64 #endif
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000065 };
66} // end anonymous namespace
67
68/// createMSP430ISelDag - This pass converts a legalized DAG into a
69/// MSP430-specific DAG, ready for instruction scheduling.
70///
71FunctionPass *llvm::createMSP430ISelDag(MSP430TargetMachine &TM) {
72 return new MSP430DAGToDAGISel(TM);
73}
74
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +000075// FIXME: This is pretty dummy routine and needs to be rewritten in the future.
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000076bool MSP430DAGToDAGISel::SelectAddr(SDValue Op, SDValue Addr,
77 SDValue &Disp, SDValue &Base) {
78 // We don't support frame index stuff yet.
79 if (isa<FrameIndexSDNode>(Addr))
80 return false;
81
82 // Operand is a result from ADD with constant operand which fits into i16.
Anton Korobeynikov0eb6af42009-05-03 13:08:51 +000083 switch (Addr.getOpcode()) {
84 case ISD::ADD:
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000085 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
86 uint64_t CVal = CN->getZExtValue();
87 // Offset should fit into 16 bits.
88 if (((CVal << 48) >> 48) == CVal) {
89 // We don't support frame index stuff yet.
90 if (isa<FrameIndexSDNode>(Addr.getOperand(0)))
91 return false;
92
93 Base = Addr.getOperand(0);
94 Disp = CurDAG->getTargetConstant(CVal, MVT::i16);
95
96 return true;
97 }
98 }
Anton Korobeynikov0eb6af42009-05-03 13:08:51 +000099 break;
100 case MSP430ISD::Wrapper:
101 SDValue N0 = Addr.getOperand(0);
102 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000103 Base = CurDAG->getRegister(0, MVT::i16);
104 Disp = CurDAG->getTargetGlobalAddress(G->getGlobal(),
105 MVT::i16, G->getOffset());
Anton Korobeynikov0eb6af42009-05-03 13:08:51 +0000106
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000107 return true;
Anton Korobeynikov0eb6af42009-05-03 13:08:51 +0000108 }
109 break;
110 };
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000111
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000112 Base = CurDAG->getRegister(0, MVT::i16);
113 Disp = Addr;
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000114
115 return true;
116}
117
118
119
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000120/// InstructionSelect - This callback is invoked by
121/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Anton Korobeynikov9e123392009-05-03 12:58:40 +0000122void MSP430DAGToDAGISel::InstructionSelect() {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000123 DEBUG(BB->dump());
124
125 // Select target instructions for the DAG.
126 SelectRoot(*CurDAG);
127
128 CurDAG->RemoveDeadNodes();
129}
130
131SDNode *MSP430DAGToDAGISel::Select(SDValue Op) {
Anton Korobeynikov43ed64a2009-05-03 12:58:58 +0000132 SDNode *Node = Op.getNode();
133
134 // Dump information about the Node being selected
135 #ifndef NDEBUG
136 DOUT << std::string(Indent, ' ') << "Selecting: ";
137 DEBUG(Node->dump(CurDAG));
138 DOUT << "\n";
139 Indent += 2;
140 #endif
141
142 // If we have a custom node, we already have selected!
143 if (Node->isMachineOpcode()) {
144 #ifndef NDEBUG
145 DOUT << std::string(Indent-2, ' ') << "== ";
146 DEBUG(Node->dump(CurDAG));
147 DOUT << "\n";
148 Indent -= 2;
149 #endif
150 return NULL;
151 }
152
153 // Instruction Selection not handled by the auto-generated tablegen selection
154 // should be handled here.
155 // Something like this:
156 // unsigned Opcode = Node->getOpcode();
157 // switch (Opcode) {
158 // default: break;
159 // case ISD::Foo:
160 // return SelectFoo(Node)
161 // }
162
163 // Select the default instruction
164 SDNode *ResNode = SelectCode(Op);
165
166 #ifndef NDEBUG
167 DOUT << std::string(Indent-2, ' ') << "=> ";
168 if (ResNode == NULL || ResNode == Op.getNode())
169 DEBUG(Op.getNode()->dump(CurDAG));
170 else
171 DEBUG(ResNode->dump(CurDAG));
172 DOUT << "\n";
173 Indent -= 2;
174 #endif
175
176 return ResNode;
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000177}