blob: 325e71e7bcf68e5a0fd4d5bf82049bc3abb4adf6 [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001//===-- 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
38using 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//===----------------------------------------------------------------------===//
48namespace {
49
50class 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
Sanjiv Gupta0e687712008-05-13 09:02:57 +000059public:
60 PIC16DAGToDAGISel(PIC16TargetMachine &tm) :
61 SelectionDAGISel(PIC16Lowering),
62 TM(tm), PIC16Lowering(*TM.getTargetLowering()) {}
63
Evan Chengdb8d56b2008-06-30 20:45:06 +000064 virtual void InstructionSelect(SelectionDAG &SD);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000065
66 // Pass Name
67 virtual const char *getPassName() const {
68 return "PIC16 DAG->DAG Pattern Instruction Selection";
69 }
70
71private:
72 // Include the pieces autogenerated from the target description.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000073#include "PIC16GenDAGISel.inc"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000074
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,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000080 SDOperand &Offset);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000081 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,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000084 SDOperand &Offset);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000085
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
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000093#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +000094 unsigned Indent;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000095#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +000096};
97
98}
99
Evan Chengdb8d56b2008-06-30 20:45:06 +0000100/// InstructionSelect - This callback is invoked by
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000101/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Evan Chengdb8d56b2008-06-30 20:45:06 +0000102void PIC16DAGToDAGISel::InstructionSelect(SelectionDAG &SD)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000103{
104 DEBUG(BB->dump());
105 // Codegen the basic block.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000106
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000107 DOUT << "===== Instruction selection begins:\n";
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000108#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000109 Indent = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000110#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000111
112 // Select target instructions for the DAG.
113 SD.setRoot(SelectRoot(SD.getRoot()));
114
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000115 DOUT << "===== Instruction selection ends:\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000116
117 SD.RemoveDeadNodes();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000118}
119
120
121bool PIC16DAGToDAGISel::
122SelectDirectAM (SDOperand Op, SDOperand N, SDOperand &Base, SDOperand &Offset)
123{
124 GlobalAddressSDNode *GA;
125 ConstantSDNode *GC;
126
127 // if Address is FI, get the TargetFrameIndex.
128 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000129 DOUT << "--------- its frame Index\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000130 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
131 Offset = CurDAG->getTargetConstant(0, MVT::i32);
132 return true;
133 }
134
135 if (N.getOpcode() == ISD::GlobalAddress) {
136 GA = dyn_cast<GlobalAddressSDNode>(N);
137 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
138 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
139 GA->getOffset());
140 return true;
141 }
142
143 if (N.getOpcode() == ISD::ADD) {
144 GC = dyn_cast<ConstantSDNode>(N.getOperand(1));
145 Offset = CurDAG->getTargetConstant((unsigned char)GC->getValue(), MVT::i8);
146 if ((GA = dyn_cast<GlobalAddressSDNode>(N.getOperand(0)))) {
147 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000148 GC->getValue());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000149 return true;
150 }
151 else if (FrameIndexSDNode *FIN
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000152 = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000153 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
154 return true;
155 }
156 }
157
158 return false;
159}
160
161
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000162// FIXME: must also account for preinc/predec/postinc/postdec.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000163bool PIC16DAGToDAGISel::
164StoreInDirectAM (SDOperand Op, SDOperand N, SDOperand &fsr)
165{
166 RegisterSDNode *Reg;
167 if (N.getOpcode() == ISD::LOAD) {
168 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
169 if (LD) {
170 fsr = LD->getBasePtr();
171 }
172 else if (isa<RegisterSDNode>(N.Val)) {
173 //FIXME an attempt to retrieve the register number
174 //but does not work
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000175 DOUT << "this is a register\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000176 Reg = dyn_cast<RegisterSDNode>(N.Val);
177 fsr = CurDAG->getRegister(Reg->getReg(),MVT::i16);
178 }
179 else {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000180 DOUT << "this is not a register\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000181 // FIXME must use whatever load is using
182 fsr = CurDAG->getRegister(1,MVT::i16);
183 }
184 return true;
185 }
186 return false;
187}
188
189bool PIC16DAGToDAGISel::
190LoadFSR (SDOperand Op, SDOperand N, SDOperand &Base, SDOperand &Offset)
191{
192 GlobalAddressSDNode *GA;
193
194 if (N.getOpcode() == ISD::GlobalAddress) {
195 GA = dyn_cast<GlobalAddressSDNode>(N);
196 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
197 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000198 GA->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000199 return true;
200 }
201 else if (N.getOpcode() == PIC16ISD::Package) {
202 CurDAG->setGraphColor(Op.Val, "blue");
203 CurDAG->viewGraph();
204 }
205
206 return false;
207}
208
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000209// LoadNothing - Don't thake this seriously, it will change.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000210bool PIC16DAGToDAGISel::
211LoadNothing (SDOperand Op, SDOperand N, SDOperand &Base, SDOperand &Offset)
212{
213 GlobalAddressSDNode *GA;
214 if (N.getOpcode() == ISD::GlobalAddress) {
215 GA = dyn_cast<GlobalAddressSDNode>(N);
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000216 DOUT << "==========" << GA->getOffset() << "\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000217 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
218 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000219 GA->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000220 return true;
221 }
222
223 return false;
224}
225
226
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000227/// Select - Select instructions not customized! Used for
228/// expanded, promoted and normal instructions.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000229SDNode* PIC16DAGToDAGISel::Select(SDOperand N)
230{
231 SDNode *Node = N.Val;
232 unsigned Opcode = Node->getOpcode();
233
234 // Dump information about the Node being selected
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000235#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000236 DOUT << std::string(Indent, ' ') << "Selecting: ";
237 DEBUG(Node->dump(CurDAG));
238 DOUT << "\n";
239 Indent += 2;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000240#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000241
242 // If we have a custom node, we already have selected!
243 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < PIC16ISD::FIRST_NUMBER) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000244#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000245 DOUT << std::string(Indent-2, ' ') << "== ";
246 DEBUG(Node->dump(CurDAG));
247 DOUT << "\n";
248 Indent -= 2;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000249#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000250 return NULL;
251 }
252
253 ///
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000254 // FIXME: Instruction Selection not handled by custom or by the
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000255 // auto-generated tablegen selection should be handled here.
256 ///
257 switch(Opcode) {
258 default: break;
259 }
260
261 // Select the default instruction.
262 SDNode *ResNode = SelectCode(N);
263
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000264#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000265 DOUT << std::string(Indent-2, ' ') << "=> ";
266 if (ResNode == NULL || ResNode == N.Val)
267 DEBUG(N.Val->dump(CurDAG));
268 else
269 DEBUG(ResNode->dump(CurDAG));
270 DOUT << "\n";
271 Indent -= 2;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000272#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000273
274 return ResNode;
275}
276
277/// createPIC16ISelDag - This pass converts a legalized DAG into a
278/// PIC16-specific DAG, ready for instruction scheduling.
279FunctionPass *llvm::createPIC16ISelDag(PIC16TargetMachine &TM) {
280 return new PIC16DAGToDAGISel(TM);
281}
282