1 //===-- PIC16ISelDAGToDAG.cpp - A dag to dag inst selector for PIC16 ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an instruction selector for the PIC16 target.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "pic16-isel"
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/Support/CFG.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Target/TargetMachine.h"
39 //===----------------------------------------------------------------------===//
40 // Instruction Selector Implementation
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
44 // PIC16DAGToDAGISel - PIC16 specific code to select PIC16 machine
45 // instructions for SelectionDAG operations.
46 //===----------------------------------------------------------------------===//
49 class VISIBILITY_HIDDEN PIC16DAGToDAGISel : public SelectionDAGISel {
51 /// TM - Keep a reference to PIC16TargetMachine.
52 PIC16TargetMachine &TM;
55 explicit PIC16DAGToDAGISel(PIC16TargetMachine &tm) :
56 SelectionDAGISel(*tm.getTargetLowering()),
59 virtual void InstructionSelect();
62 virtual const char *getPassName() const {
63 return "PIC16 DAG->DAG Pattern Instruction Selection";
67 // Include the pieces autogenerated from the target description.
68 #include "PIC16GenDAGISel.inc"
70 SDNode *Select(SDValue N);
72 // Select addressing mode. currently assume base + offset addr mode.
73 bool SelectAM(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset);
74 bool SelectDirectAM(SDValue Op, SDValue N, SDValue &Base,
76 bool StoreInDirectAM(SDValue Op, SDValue N, SDValue &fsr);
77 bool LoadFSR(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset);
78 bool LoadNothing(SDValue Op, SDValue N, SDValue &Base,
81 // getI8Imm - Return a target constant with the specified
83 inline SDValue getI8Imm(unsigned Imm) {
84 return CurDAG->getTargetConstant(Imm, MVT::i8);
95 /// InstructionSelect - This callback is invoked by
96 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
97 void PIC16DAGToDAGISel::InstructionSelect()
100 // Codegen the basic block.
102 DOUT << "===== Instruction selection begins:\n";
107 // Select target instructions for the DAG.
110 DOUT << "===== Instruction selection ends:\n";
112 CurDAG->RemoveDeadNodes();
116 bool PIC16DAGToDAGISel::
117 SelectDirectAM (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
119 GlobalAddressSDNode *GA;
122 // if Address is FI, get the TargetFrameIndex.
123 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
124 DOUT << "--------- its frame Index\n";
125 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
126 Offset = CurDAG->getTargetConstant(0, MVT::i32);
130 if (N.getOpcode() == ISD::GlobalAddress) {
131 GA = dyn_cast<GlobalAddressSDNode>(N);
132 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
133 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
138 if (N.getOpcode() == ISD::ADD) {
139 GC = dyn_cast<ConstantSDNode>(N.getOperand(1));
140 Offset = CurDAG->getTargetConstant((unsigned char)GC->getZExtValue(),
142 if ((GA = dyn_cast<GlobalAddressSDNode>(N.getOperand(0)))) {
143 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
147 else if (FrameIndexSDNode *FIN
148 = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
149 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
158 // FIXME: must also account for preinc/predec/postinc/postdec.
159 bool PIC16DAGToDAGISel::
160 StoreInDirectAM (SDValue Op, SDValue N, SDValue &fsr)
163 if (N.getOpcode() == ISD::LOAD) {
164 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
166 fsr = LD->getBasePtr();
168 else if (isa<RegisterSDNode>(N.getNode())) {
169 //FIXME an attempt to retrieve the register number
171 DOUT << "this is a register\n";
172 Reg = dyn_cast<RegisterSDNode>(N.getNode());
173 fsr = CurDAG->getRegister(Reg->getReg(),MVT::i16);
176 DOUT << "this is not a register\n";
177 // FIXME must use whatever load is using
178 fsr = CurDAG->getRegister(1,MVT::i16);
185 bool PIC16DAGToDAGISel::
186 LoadFSR (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
188 GlobalAddressSDNode *GA;
190 if (N.getOpcode() == ISD::GlobalAddress) {
191 GA = dyn_cast<GlobalAddressSDNode>(N);
192 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
193 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
197 else if (N.getOpcode() == PIC16ISD::Package) {
198 CurDAG->setGraphColor(Op.getNode(), "blue");
205 // LoadNothing - Don't thake this seriously, it will change.
206 bool PIC16DAGToDAGISel::
207 LoadNothing (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
209 GlobalAddressSDNode *GA;
210 if (N.getOpcode() == ISD::GlobalAddress) {
211 GA = dyn_cast<GlobalAddressSDNode>(N);
212 DOUT << "==========" << GA->getOffset() << "\n";
213 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
214 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
223 /// Select - Select instructions not customized! Used for
224 /// expanded, promoted and normal instructions.
225 SDNode* PIC16DAGToDAGISel::Select(SDValue N)
227 SDNode *Node = N.getNode();
228 unsigned Opcode = Node->getOpcode();
230 // Dump information about the Node being selected
232 DOUT << std::string(Indent, ' ') << "Selecting: ";
233 DEBUG(Node->dump(CurDAG));
238 // If we have a custom node, we already have selected!
239 if (Node->isMachineOpcode()) {
241 DOUT << std::string(Indent-2, ' ') << "== ";
242 DEBUG(Node->dump(CurDAG));
250 // FIXME: Instruction Selection not handled by custom or by the
251 // auto-generated tablegen selection should be handled here.
257 // Select the default instruction.
258 SDNode *ResNode = SelectCode(N);
261 DOUT << std::string(Indent-2, ' ') << "=> ";
262 if (ResNode == NULL || ResNode == N.getNode())
263 DEBUG(N.getNode()->dump(CurDAG));
265 DEBUG(ResNode->dump(CurDAG));
273 /// createPIC16ISelDag - This pass converts a legalized DAG into a
274 /// PIC16-specific DAG, ready for instruction scheduling.
275 FunctionPass *llvm::createPIC16ISelDag(PIC16TargetMachine &TM) {
276 return new PIC16DAGToDAGISel(TM);