blob: d98be99f86c5b8cbd846ab8496fc7fbde58001bc [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"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000034using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// Instruction Selector Implementation
38//===----------------------------------------------------------------------===//
39
40//===----------------------------------------------------------------------===//
41// PIC16DAGToDAGISel - PIC16 specific code to select PIC16 machine
42// instructions for SelectionDAG operations.
43//===----------------------------------------------------------------------===//
44namespace {
45
46class VISIBILITY_HIDDEN PIC16DAGToDAGISel : public SelectionDAGISel {
47
48 /// TM - Keep a reference to PIC16TargetMachine.
49 PIC16TargetMachine &TM;
50
Sanjiv Gupta0e687712008-05-13 09:02:57 +000051public:
Dan Gohman1002c022008-07-07 18:00:37 +000052 explicit PIC16DAGToDAGISel(PIC16TargetMachine &tm) :
Dan Gohmanda8ac5f2008-10-03 16:55:19 +000053 SelectionDAGISel(*tm.getTargetLowering()),
54 TM(tm) {}
Sanjiv Gupta0e687712008-05-13 09:02:57 +000055
Dan Gohmanf350b272008-08-23 02:25:05 +000056 virtual void InstructionSelect();
Sanjiv Gupta0e687712008-05-13 09:02:57 +000057
58 // Pass Name
59 virtual const char *getPassName() const {
60 return "PIC16 DAG->DAG Pattern Instruction Selection";
61 }
62
63private:
64 // Include the pieces autogenerated from the target description.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000065#include "PIC16GenDAGISel.inc"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000066
Dan Gohman475871a2008-07-27 21:46:04 +000067 SDNode *Select(SDValue N);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000068
69 // Select addressing mode. currently assume base + offset addr mode.
Dan Gohman475871a2008-07-27 21:46:04 +000070 bool SelectAM(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset);
71 bool SelectDirectAM(SDValue Op, SDValue N, SDValue &Base,
72 SDValue &Offset);
73 bool StoreInDirectAM(SDValue Op, SDValue N, SDValue &fsr);
74 bool LoadFSR(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset);
75 bool LoadNothing(SDValue Op, SDValue N, SDValue &Base,
76 SDValue &Offset);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000077
78 // getI8Imm - Return a target constant with the specified
79 // value, of type i8.
Dan Gohman475871a2008-07-27 21:46:04 +000080 inline SDValue getI8Imm(unsigned Imm) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000081 return CurDAG->getTargetConstant(Imm, MVT::i8);
82 }
83
84
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000085#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +000086 unsigned Indent;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000087#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +000088};
89
90}
91
Evan Chengdb8d56b2008-06-30 20:45:06 +000092/// InstructionSelect - This callback is invoked by
Sanjiv Gupta0e687712008-05-13 09:02:57 +000093/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +000094void PIC16DAGToDAGISel::InstructionSelect()
Sanjiv Gupta0e687712008-05-13 09:02:57 +000095{
96 DEBUG(BB->dump());
97 // Codegen the basic block.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000098
Sanjiv Gupta0e687712008-05-13 09:02:57 +000099 DOUT << "===== Instruction selection begins:\n";
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000100#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000101 Indent = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000102#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000103
104 // Select target instructions for the DAG.
David Greene8ad4c002008-10-27 21:56:29 +0000105 SelectRoot(*CurDAG);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000106
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000107 DOUT << "===== Instruction selection ends:\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000108
Dan Gohmanf350b272008-08-23 02:25:05 +0000109 CurDAG->RemoveDeadNodes();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000110}
111
112
113bool PIC16DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +0000114SelectDirectAM (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000115{
116 GlobalAddressSDNode *GA;
117 ConstantSDNode *GC;
118
119 // if Address is FI, get the TargetFrameIndex.
120 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000121 DOUT << "--------- its frame Index\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000122 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
123 Offset = CurDAG->getTargetConstant(0, MVT::i32);
124 return true;
125 }
126
127 if (N.getOpcode() == ISD::GlobalAddress) {
128 GA = dyn_cast<GlobalAddressSDNode>(N);
129 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
130 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
131 GA->getOffset());
132 return true;
133 }
134
135 if (N.getOpcode() == ISD::ADD) {
136 GC = dyn_cast<ConstantSDNode>(N.getOperand(1));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000137 Offset = CurDAG->getTargetConstant((unsigned char)GC->getZExtValue(),
138 MVT::i8);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000139 if ((GA = dyn_cast<GlobalAddressSDNode>(N.getOperand(0)))) {
140 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000141 GC->getZExtValue());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000142 return true;
143 }
144 else if (FrameIndexSDNode *FIN
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000145 = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000146 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
147 return true;
148 }
149 }
150
151 return false;
152}
153
154
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000155// FIXME: must also account for preinc/predec/postinc/postdec.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000156bool PIC16DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +0000157StoreInDirectAM (SDValue Op, SDValue N, SDValue &fsr)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000158{
159 RegisterSDNode *Reg;
160 if (N.getOpcode() == ISD::LOAD) {
161 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
162 if (LD) {
163 fsr = LD->getBasePtr();
164 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000165 else if (isa<RegisterSDNode>(N.getNode())) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000166 //FIXME an attempt to retrieve the register number
167 //but does not work
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000168 DOUT << "this is a register\n";
Gabor Greifba36cb52008-08-28 21:40:38 +0000169 Reg = dyn_cast<RegisterSDNode>(N.getNode());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000170 fsr = CurDAG->getRegister(Reg->getReg(),MVT::i16);
171 }
172 else {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000173 DOUT << "this is not a register\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000174 // FIXME must use whatever load is using
175 fsr = CurDAG->getRegister(1,MVT::i16);
176 }
177 return true;
178 }
179 return false;
180}
181
182bool PIC16DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +0000183LoadFSR (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000184{
185 GlobalAddressSDNode *GA;
186
187 if (N.getOpcode() == ISD::GlobalAddress) {
188 GA = dyn_cast<GlobalAddressSDNode>(N);
189 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
190 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000191 GA->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000192 return true;
193 }
194 else if (N.getOpcode() == PIC16ISD::Package) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000195 CurDAG->setGraphColor(Op.getNode(), "blue");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000196 CurDAG->viewGraph();
197 }
198
199 return false;
200}
201
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000202// LoadNothing - Don't thake this seriously, it will change.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000203bool PIC16DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +0000204LoadNothing (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000205{
206 GlobalAddressSDNode *GA;
207 if (N.getOpcode() == ISD::GlobalAddress) {
208 GA = dyn_cast<GlobalAddressSDNode>(N);
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000209 DOUT << "==========" << GA->getOffset() << "\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000210 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
211 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000212 GA->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000213 return true;
214 }
215
216 return false;
217}
218
219
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000220/// Select - Select instructions not customized! Used for
221/// expanded, promoted and normal instructions.
Dan Gohman475871a2008-07-27 21:46:04 +0000222SDNode* PIC16DAGToDAGISel::Select(SDValue N)
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000223{
Gabor Greifba36cb52008-08-28 21:40:38 +0000224 SDNode *Node = N.getNode();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000225 unsigned Opcode = Node->getOpcode();
226
227 // Dump information about the Node being selected
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000228#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000229 DOUT << std::string(Indent, ' ') << "Selecting: ";
230 DEBUG(Node->dump(CurDAG));
231 DOUT << "\n";
232 Indent += 2;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000233#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000234
235 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000236 if (Node->isMachineOpcode()) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000237#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000238 DOUT << std::string(Indent-2, ' ') << "== ";
239 DEBUG(Node->dump(CurDAG));
240 DOUT << "\n";
241 Indent -= 2;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000242#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000243 return NULL;
244 }
245
246 ///
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000247 // FIXME: Instruction Selection not handled by custom or by the
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000248 // auto-generated tablegen selection should be handled here.
249 ///
250 switch(Opcode) {
251 default: break;
252 }
253
254 // Select the default instruction.
255 SDNode *ResNode = SelectCode(N);
256
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000257#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000258 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +0000259 if (ResNode == NULL || ResNode == N.getNode())
260 DEBUG(N.getNode()->dump(CurDAG));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000261 else
262 DEBUG(ResNode->dump(CurDAG));
263 DOUT << "\n";
264 Indent -= 2;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000265#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000266
267 return ResNode;
268}
269
270/// createPIC16ISelDag - This pass converts a legalized DAG into a
271/// PIC16-specific DAG, ready for instruction scheduling.
272FunctionPass *llvm::createPIC16ISelDag(PIC16TargetMachine &TM) {
273 return new PIC16DAGToDAGISel(TM);
274}
275