blob: a9c1d096137b7898f41fdee8e5c0c6d1672a7cd7 [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"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000030#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
37using 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//===----------------------------------------------------------------------===//
47namespace {
48
49class 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
Sanjiv Gupta0e687712008-05-13 09:02:57 +000058public:
Dan Gohman1002c022008-07-07 18:00:37 +000059 explicit PIC16DAGToDAGISel(PIC16TargetMachine &tm) :
Sanjiv Gupta0e687712008-05-13 09:02:57 +000060 SelectionDAGISel(PIC16Lowering),
61 TM(tm), PIC16Lowering(*TM.getTargetLowering()) {}
62
Dan Gohmanf350b272008-08-23 02:25:05 +000063 virtual void InstructionSelect();
Sanjiv Gupta0e687712008-05-13 09:02:57 +000064
65 // Pass Name
66 virtual const char *getPassName() const {
67 return "PIC16 DAG->DAG Pattern Instruction Selection";
68 }
69
70private:
71 // Include the pieces autogenerated from the target description.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000072#include "PIC16GenDAGISel.inc"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000073
Dan Gohman475871a2008-07-27 21:46:04 +000074 SDNode *Select(SDValue N);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000075
76 // Select addressing mode. currently assume base + offset addr mode.
Dan Gohman475871a2008-07-27 21:46:04 +000077 bool SelectAM(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset);
78 bool SelectDirectAM(SDValue Op, SDValue N, SDValue &Base,
79 SDValue &Offset);
80 bool StoreInDirectAM(SDValue Op, SDValue N, SDValue &fsr);
81 bool LoadFSR(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset);
82 bool LoadNothing(SDValue Op, SDValue N, SDValue &Base,
83 SDValue &Offset);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000084
85 // getI8Imm - Return a target constant with the specified
86 // value, of type i8.
Dan Gohman475871a2008-07-27 21:46:04 +000087 inline SDValue getI8Imm(unsigned Imm) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000088 return CurDAG->getTargetConstant(Imm, MVT::i8);
89 }
90
91
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000092#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +000093 unsigned Indent;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000094#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +000095};
96
97}
98
Evan Chengdb8d56b2008-06-30 20:45:06 +000099/// InstructionSelect - This callback is invoked by
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000100/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000101void PIC16DAGToDAGISel::InstructionSelect()
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000102{
103 DEBUG(BB->dump());
104 // Codegen the basic block.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000105
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000106 DOUT << "===== Instruction selection begins:\n";
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000107#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000108 Indent = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000109#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000110
111 // Select target instructions for the DAG.
Dan Gohmanad3460c2008-08-21 16:36:34 +0000112 SelectRoot();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000113
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000114 DOUT << "===== Instruction selection ends:\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000115
Dan Gohmanf350b272008-08-23 02:25:05 +0000116 CurDAG->RemoveDeadNodes();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000117}
118
119
120bool PIC16DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +0000121SelectDirectAM (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000122{
123 GlobalAddressSDNode *GA;
124 ConstantSDNode *GC;
125
126 // if Address is FI, get the TargetFrameIndex.
127 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000128 DOUT << "--------- its frame Index\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000129 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,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000147 GC->getValue());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000148 return true;
149 }
150 else if (FrameIndexSDNode *FIN
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000151 = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000152 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
153 return true;
154 }
155 }
156
157 return false;
158}
159
160
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000161// FIXME: must also account for preinc/predec/postinc/postdec.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000162bool PIC16DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +0000163StoreInDirectAM (SDValue Op, SDValue N, SDValue &fsr)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000164{
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
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000174 DOUT << "this is a register\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000175 Reg = dyn_cast<RegisterSDNode>(N.Val);
176 fsr = CurDAG->getRegister(Reg->getReg(),MVT::i16);
177 }
178 else {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000179 DOUT << "this is not a register\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000180 // FIXME must use whatever load is using
181 fsr = CurDAG->getRegister(1,MVT::i16);
182 }
183 return true;
184 }
185 return false;
186}
187
188bool PIC16DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +0000189LoadFSR (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000190{
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,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000197 GA->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000198 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
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000208// LoadNothing - Don't thake this seriously, it will change.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000209bool PIC16DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +0000210LoadNothing (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000211{
212 GlobalAddressSDNode *GA;
213 if (N.getOpcode() == ISD::GlobalAddress) {
214 GA = dyn_cast<GlobalAddressSDNode>(N);
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000215 DOUT << "==========" << GA->getOffset() << "\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000216 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
217 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000218 GA->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000219 return true;
220 }
221
222 return false;
223}
224
225
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000226/// Select - Select instructions not customized! Used for
227/// expanded, promoted and normal instructions.
Dan Gohman475871a2008-07-27 21:46:04 +0000228SDNode* PIC16DAGToDAGISel::Select(SDValue N)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000229{
230 SDNode *Node = N.Val;
231 unsigned Opcode = Node->getOpcode();
232
233 // Dump information about the Node being selected
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000234#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000235 DOUT << std::string(Indent, ' ') << "Selecting: ";
236 DEBUG(Node->dump(CurDAG));
237 DOUT << "\n";
238 Indent += 2;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000239#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000240
241 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000242 if (Node->isMachineOpcode()) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000243#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000244 DOUT << std::string(Indent-2, ' ') << "== ";
245 DEBUG(Node->dump(CurDAG));
246 DOUT << "\n";
247 Indent -= 2;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000248#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000249 return NULL;
250 }
251
252 ///
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000253 // FIXME: Instruction Selection not handled by custom or by the
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000254 // 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
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000263#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000264 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;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000271#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000272
273 return ResNode;
274}
275
276/// createPIC16ISelDag - This pass converts a legalized DAG into a
277/// PIC16-specific DAG, ready for instruction scheduling.
278FunctionPass *llvm::createPIC16ISelDag(PIC16TargetMachine &TM) {
279 return new PIC16DAGToDAGISel(TM);
280}
281