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