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