mark some targets as experimental. Andrew, if you think that Alpha is
[oota-llvm.git] / lib / Target / PIC16 / PIC16ISelDAGToDAG.cpp
1 //===-- PIC16ISelDAGToDAG.cpp - A dag to dag inst selector for PIC16 ------===//
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 PIC16 target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "pic16-isel"
15
16 #include "PIC16.h"
17 #include "PIC16ISelLowering.h"
18 #include "PIC16RegisterInfo.h"
19 #include "PIC16Subtarget.h"
20 #include "PIC16TargetMachine.h"
21 #include "llvm/GlobalValue.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Intrinsics.h"
24 #include "llvm/Type.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/SelectionDAGISel.h"
30 #include "llvm/Support/CFG.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include <queue>
35 #include <set>
36
37 using namespace llvm;
38
39 //===----------------------------------------------------------------------===//
40 // Instruction Selector Implementation
41 //===----------------------------------------------------------------------===//
42
43 //===----------------------------------------------------------------------===//
44 // PIC16DAGToDAGISel - PIC16 specific code to select PIC16 machine
45 // instructions for SelectionDAG operations.
46 //===----------------------------------------------------------------------===//
47 namespace {
48
49 class VISIBILITY_HIDDEN PIC16DAGToDAGISel : public SelectionDAGISel {
50
51   /// TM - Keep a reference to PIC16TargetMachine.
52   PIC16TargetMachine &TM;
53
54 public:
55   explicit PIC16DAGToDAGISel(PIC16TargetMachine &tm) : 
56         SelectionDAGISel(*tm.getTargetLowering()),
57         TM(tm) {}
58   
59   virtual void InstructionSelect();
60
61   // Pass Name
62   virtual const char *getPassName() const {
63     return "PIC16 DAG->DAG Pattern Instruction Selection";
64   } 
65   
66 private:
67   // Include the pieces autogenerated from the target description.
68 #include "PIC16GenDAGISel.inc"
69
70   SDNode *Select(SDValue N);
71
72   // Select addressing mode. currently assume base + offset addr mode.
73   bool SelectAM(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset);
74   bool SelectDirectAM(SDValue Op, SDValue N, SDValue &Base, 
75                       SDValue &Offset);
76   bool StoreInDirectAM(SDValue Op, SDValue N, SDValue &fsr);
77   bool LoadFSR(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset);
78   bool LoadNothing(SDValue Op, SDValue N, SDValue &Base, 
79                    SDValue &Offset);
80
81   // getI8Imm - Return a target constant with the specified
82   // value, of type i8.
83   inline SDValue getI8Imm(unsigned Imm) {
84     return CurDAG->getTargetConstant(Imm, MVT::i8);
85   }
86
87
88 #ifndef NDEBUG
89   unsigned Indent;
90 #endif
91 };
92
93 }
94
95 /// InstructionSelect - This callback is invoked by
96 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
97 void PIC16DAGToDAGISel::InstructionSelect() 
98 {
99   DEBUG(BB->dump());
100   // Codegen the basic block.
101
102   DOUT << "===== Instruction selection begins:\n";
103 #ifndef NDEBUG
104   Indent = 0;
105 #endif
106
107   // Select target instructions for the DAG.
108   SelectRoot();
109
110   DOUT << "===== Instruction selection ends:\n";
111
112   CurDAG->RemoveDeadNodes();
113 }
114
115
116 bool PIC16DAGToDAGISel::
117 SelectDirectAM (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
118 {
119   GlobalAddressSDNode *GA;
120   ConstantSDNode      *GC;
121
122   // if Address is FI, get the TargetFrameIndex.
123   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
124     DOUT << "--------- its frame Index\n";
125     Base   = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
126     Offset = CurDAG->getTargetConstant(0, MVT::i32);
127     return true;
128   }
129
130   if (N.getOpcode() == ISD::GlobalAddress) {
131     GA = dyn_cast<GlobalAddressSDNode>(N);
132     Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
133     Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
134                                           GA->getOffset());
135     return true;
136   } 
137
138   if (N.getOpcode() == ISD::ADD) {
139     GC = dyn_cast<ConstantSDNode>(N.getOperand(1));
140     Offset = CurDAG->getTargetConstant((unsigned char)GC->getZExtValue(),
141                                        MVT::i8);
142     if ((GA = dyn_cast<GlobalAddressSDNode>(N.getOperand(0)))) {
143       Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16, 
144                                             GC->getZExtValue());
145       return true;
146     }
147     else if (FrameIndexSDNode *FIN 
148                 = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
149       Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
150       return true;
151     }
152   }
153
154   return false;  
155 }
156
157
158 // FIXME: must also account for preinc/predec/postinc/postdec.
159 bool PIC16DAGToDAGISel::
160 StoreInDirectAM (SDValue Op, SDValue N, SDValue &fsr)
161 {
162   RegisterSDNode *Reg;
163   if (N.getOpcode() == ISD::LOAD) {
164     LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
165     if (LD) {
166       fsr = LD->getBasePtr();
167     }
168     else if (isa<RegisterSDNode>(N.getNode())) { 
169       //FIXME an attempt to retrieve the register number
170       //but does not work
171       DOUT << "this is a register\n";
172       Reg = dyn_cast<RegisterSDNode>(N.getNode());
173       fsr = CurDAG->getRegister(Reg->getReg(),MVT::i16);  
174     }
175     else {
176       DOUT << "this is not a register\n";
177       // FIXME must use whatever load is using
178       fsr = CurDAG->getRegister(1,MVT::i16);
179     }
180     return true;
181   }
182   return false;  
183 }
184
185 bool PIC16DAGToDAGISel::
186 LoadFSR (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
187 {
188   GlobalAddressSDNode *GA;
189
190   if (N.getOpcode() == ISD::GlobalAddress) {
191     GA = dyn_cast<GlobalAddressSDNode>(N);
192     Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
193     Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
194                                           GA->getOffset());
195     return true;
196   }
197   else if (N.getOpcode() == PIC16ISD::Package) {
198     CurDAG->setGraphColor(Op.getNode(), "blue");
199     CurDAG->viewGraph();
200   }
201
202   return false;
203 }
204
205 // LoadNothing - Don't thake this seriously, it will change.
206 bool PIC16DAGToDAGISel::
207 LoadNothing (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
208 {
209   GlobalAddressSDNode *GA;
210   if (N.getOpcode() == ISD::GlobalAddress) {
211     GA = dyn_cast<GlobalAddressSDNode>(N);
212     DOUT << "==========" << GA->getOffset() << "\n";
213     Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
214     Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
215                                           GA->getOffset());
216     return true;
217   }  
218
219   return false;
220 }
221
222
223 /// Select - Select instructions not customized! Used for
224 /// expanded, promoted and normal instructions.
225 SDNode* PIC16DAGToDAGISel::Select(SDValue N) 
226 {
227   SDNode *Node = N.getNode();
228   unsigned Opcode = Node->getOpcode();
229
230   // Dump information about the Node being selected
231 #ifndef NDEBUG
232   DOUT << std::string(Indent, ' ') << "Selecting: ";
233   DEBUG(Node->dump(CurDAG));
234   DOUT << "\n";
235   Indent += 2;
236 #endif
237
238   // If we have a custom node, we already have selected!
239   if (Node->isMachineOpcode()) {
240 #ifndef NDEBUG
241     DOUT << std::string(Indent-2, ' ') << "== ";
242     DEBUG(Node->dump(CurDAG));
243     DOUT << "\n";
244     Indent -= 2;
245 #endif
246     return NULL;
247   }
248
249   ///
250   // FIXME: Instruction Selection not handled by custom or by the 
251   // auto-generated tablegen selection should be handled here.
252   /// 
253   switch(Opcode) {
254     default: break;
255   }
256
257   // Select the default instruction.
258   SDNode *ResNode = SelectCode(N);
259
260 #ifndef NDEBUG
261   DOUT << std::string(Indent-2, ' ') << "=> ";
262   if (ResNode == NULL || ResNode == N.getNode())
263     DEBUG(N.getNode()->dump(CurDAG));
264   else
265     DEBUG(ResNode->dump(CurDAG));
266   DOUT << "\n";
267   Indent -= 2;
268 #endif
269
270   return ResNode;
271 }
272
273 /// createPIC16ISelDag - This pass converts a legalized DAG into a 
274 /// PIC16-specific DAG, ready for instruction scheduling.
275 FunctionPass *llvm::createPIC16ISelDag(PIC16TargetMachine &TM) {
276   return new PIC16DAGToDAGISel(TM);
277 }
278