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