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;
54 /// PIC16Lowering - This object fully describes how to lower LLVM code to an
55 /// PIC16-specific SelectionDAG.
56 PIC16TargetLowering PIC16Lowering;
59 explicit PIC16DAGToDAGISel(PIC16TargetMachine &tm) :
60 SelectionDAGISel(PIC16Lowering),
61 TM(tm), PIC16Lowering(*TM.getTargetLowering()) {}
63 virtual void InstructionSelect(SelectionDAG &SD);
66 virtual const char *getPassName() const {
67 return "PIC16 DAG->DAG Pattern Instruction Selection";
71 // Include the pieces autogenerated from the target description.
72 #include "PIC16GenDAGISel.inc"
74 SDNode *Select(SDValue N);
76 // Select addressing mode. currently assume base + offset addr mode.
77 bool SelectAM(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset);
78 bool SelectDirectAM(SDValue Op, SDValue N, SDValue &Base,
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,
85 // getI8Imm - Return a target constant with the specified
87 inline SDValue getI8Imm(unsigned Imm) {
88 return CurDAG->getTargetConstant(Imm, MVT::i8);
99 /// InstructionSelect - This callback is invoked by
100 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
101 void PIC16DAGToDAGISel::InstructionSelect(SelectionDAG &SD)
104 // Codegen the basic block.
106 DOUT << "===== Instruction selection begins:\n";
111 // Select target instructions for the DAG.
112 SD.setRoot(SelectRoot(SD.getRoot()));
114 DOUT << "===== Instruction selection ends:\n";
116 SD.RemoveDeadNodes();
120 bool PIC16DAGToDAGISel::
121 SelectDirectAM (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
123 GlobalAddressSDNode *GA;
126 // if Address is FI, get the TargetFrameIndex.
127 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
128 DOUT << "--------- its frame Index\n";
129 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
130 Offset = CurDAG->getTargetConstant(0, MVT::i32);
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,
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,
150 else if (FrameIndexSDNode *FIN
151 = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
152 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
161 // FIXME: must also account for preinc/predec/postinc/postdec.
162 bool PIC16DAGToDAGISel::
163 StoreInDirectAM (SDValue Op, SDValue N, SDValue &fsr)
166 if (N.getOpcode() == ISD::LOAD) {
167 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
169 fsr = LD->getBasePtr();
171 else if (isa<RegisterSDNode>(N.Val)) {
172 //FIXME an attempt to retrieve the register number
174 DOUT << "this is a register\n";
175 Reg = dyn_cast<RegisterSDNode>(N.Val);
176 fsr = CurDAG->getRegister(Reg->getReg(),MVT::i16);
179 DOUT << "this is not a register\n";
180 // FIXME must use whatever load is using
181 fsr = CurDAG->getRegister(1,MVT::i16);
188 bool PIC16DAGToDAGISel::
189 LoadFSR (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
191 GlobalAddressSDNode *GA;
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,
200 else if (N.getOpcode() == PIC16ISD::Package) {
201 CurDAG->setGraphColor(Op.Val, "blue");
208 // LoadNothing - Don't thake this seriously, it will change.
209 bool PIC16DAGToDAGISel::
210 LoadNothing (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset)
212 GlobalAddressSDNode *GA;
213 if (N.getOpcode() == ISD::GlobalAddress) {
214 GA = dyn_cast<GlobalAddressSDNode>(N);
215 DOUT << "==========" << GA->getOffset() << "\n";
216 Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8);
217 Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16,
226 /// Select - Select instructions not customized! Used for
227 /// expanded, promoted and normal instructions.
228 SDNode* PIC16DAGToDAGISel::Select(SDValue N)
230 SDNode *Node = N.Val;
231 unsigned Opcode = Node->getOpcode();
233 // Dump information about the Node being selected
235 DOUT << std::string(Indent, ' ') << "Selecting: ";
236 DEBUG(Node->dump(CurDAG));
241 // If we have a custom node, we already have selected!
242 if (Node->isMachineOpcode()) {
244 DOUT << std::string(Indent-2, ' ') << "== ";
245 DEBUG(Node->dump(CurDAG));
253 // FIXME: Instruction Selection not handled by custom or by the
254 // auto-generated tablegen selection should be handled here.
260 // Select the default instruction.
261 SDNode *ResNode = SelectCode(N);
264 DOUT << std::string(Indent-2, ' ') << "=> ";
265 if (ResNode == NULL || ResNode == N.Val)
266 DEBUG(N.Val->dump(CurDAG));
268 DEBUG(ResNode->dump(CurDAG));
276 /// createPIC16ISelDag - This pass converts a legalized DAG into a
277 /// PIC16-specific DAG, ready for instruction scheduling.
278 FunctionPass *llvm::createPIC16ISelDag(PIC16TargetMachine &TM) {
279 return new PIC16DAGToDAGISel(TM);