blob: 512893b2e6a8513639e6727dc85ccf7b3d5e4399 [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
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
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
100/// InstructionSelectBasicBlock - This callback is invoked by
101/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
102void PIC16DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &SD)
103{
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();
118
119 // Emit machine code to BB.
120 ScheduleAndEmitDAG(SD);
121}
122
123
124bool PIC16DAGToDAGISel::
125SelectDirectAM (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)) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000132 DOUT << "--------- its frame Index\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000133 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,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000151 GC->getValue());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000152 return true;
153 }
154 else if (FrameIndexSDNode *FIN
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000155 = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000156 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
157 return true;
158 }
159 }
160
161 return false;
162}
163
164
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000165// FIXME: must also account for preinc/predec/postinc/postdec.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000166bool PIC16DAGToDAGISel::
167StoreInDirectAM (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
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000178 DOUT << "this is a register\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000179 Reg = dyn_cast<RegisterSDNode>(N.Val);
180 fsr = CurDAG->getRegister(Reg->getReg(),MVT::i16);
181 }
182 else {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000183 DOUT << "this is not a register\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000184 // FIXME must use whatever load is using
185 fsr = CurDAG->getRegister(1,MVT::i16);
186 }
187 return true;
188 }
189 return false;
190}
191
192bool PIC16DAGToDAGISel::
193LoadFSR (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,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000201 GA->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000202 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
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000212// LoadNothing - Don't thake this seriously, it will change.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000213bool PIC16DAGToDAGISel::
214LoadNothing (SDOperand Op, SDOperand N, SDOperand &Base, SDOperand &Offset)
215{
216 GlobalAddressSDNode *GA;
217 if (N.getOpcode() == ISD::GlobalAddress) {
218 GA = dyn_cast<GlobalAddressSDNode>(N);
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000219 DOUT << "==========" << GA->getOffset() << "\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000220 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
221 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000222 GA->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000223 return true;
224 }
225
226 return false;
227}
228
229
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000230/// Select - Select instructions not customized! Used for
231/// expanded, promoted and normal instructions.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000232SDNode* PIC16DAGToDAGISel::Select(SDOperand N)
233{
234 SDNode *Node = N.Val;
235 unsigned Opcode = Node->getOpcode();
236
237 // Dump information about the Node being selected
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000238#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000239 DOUT << std::string(Indent, ' ') << "Selecting: ";
240 DEBUG(Node->dump(CurDAG));
241 DOUT << "\n";
242 Indent += 2;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000243#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000244
245 // If we have a custom node, we already have selected!
246 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < PIC16ISD::FIRST_NUMBER) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000247#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000248 DOUT << std::string(Indent-2, ' ') << "== ";
249 DEBUG(Node->dump(CurDAG));
250 DOUT << "\n";
251 Indent -= 2;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000252#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000253 return NULL;
254 }
255
256 ///
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000257 // FIXME: Instruction Selection not handled by custom or by the
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000258 // 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
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000267#ifndef NDEBUG
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000268 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;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000275#endif
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000276
277 return ResNode;
278}
279
280/// createPIC16ISelDag - This pass converts a legalized DAG into a
281/// PIC16-specific DAG, ready for instruction scheduling.
282FunctionPass *llvm::createPIC16ISelDag(PIC16TargetMachine &TM) {
283 return new PIC16DAGToDAGISel(TM);
284}
285