blob: bb59194115742f9800d37484f68f02bb4ecd9aa3 [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001//
2// The LLVM Compiler Infrastructure
3//
4// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the interfaces that PIC16 uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "pic16-lower"
15
16#include "PIC16ISelLowering.h"
17#include "PIC16TargetMachine.h"
18#include "llvm/DerivedTypes.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000019#include "llvm/GlobalValue.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000020#include "llvm/Function.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000021#include "llvm/CallingConv.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
Duncan Sandsc6dbe7f2008-11-28 10:20:03 +000026#include <cstdio>
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000027
28
Sanjiv Gupta0e687712008-05-13 09:02:57 +000029using namespace llvm;
30
Sanjiv Gupta0e687712008-05-13 09:02:57 +000031
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000032// PIC16TargetLowering Constructor.
33PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
34 : TargetLowering(TM) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000035
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000036 Subtarget = &TM.getSubtarget<PIC16Subtarget>();
Sanjiv Gupta0e687712008-05-13 09:02:57 +000037
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000038 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000039
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000040 setShiftAmountType(MVT::i8);
41 setShiftAmountFlavor(Extend);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000042
Sanjiv Gupta1b046942009-01-13 19:18:47 +000043 // SRA library call names
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000044 setPIC16LibcallName(PIC16ISD::SRA_I8, "__intrinsics.sra.i8");
45 setLibcallName(RTLIB::SRA_I16, "__intrinsics.sra.i16");
46 setLibcallName(RTLIB::SRA_I32, "__intrinsics.sra.i32");
Sanjiv Gupta1b046942009-01-13 19:18:47 +000047
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000048 // SHL library call names
49 setPIC16LibcallName(PIC16ISD::SLL_I8, "__intrinsics.sll.i8");
50 setLibcallName(RTLIB::SHL_I16, "__intrinsics.sll.i16");
51 setLibcallName(RTLIB::SHL_I32, "__intrinsics.sll.i32");
Sanjiv Gupta1b046942009-01-13 19:18:47 +000052
53 // SRL library call names
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000054 setPIC16LibcallName(PIC16ISD::SRL_I8, "__intrinsics.srl.i8");
55 setLibcallName(RTLIB::SRL_I16, "__intrinsics.srl.i16");
56 setLibcallName(RTLIB::SRL_I32, "__intrinsics.srl.i32");
57
58 // MUL Library call names
59 setPIC16LibcallName(PIC16ISD::MUL_I8, "__intrinsics.mul.i8");
60 setLibcallName(RTLIB::MUL_I16, "__intrinsics.mul.i16");
61 setLibcallName(RTLIB::MUL_I32, "__intrinsics.mul.i32");
Sanjiv Gupta0e687712008-05-13 09:02:57 +000062
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000063 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000064 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000065
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000066 setOperationAction(ISD::LOAD, MVT::i8, Legal);
67 setOperationAction(ISD::LOAD, MVT::i16, Custom);
68 setOperationAction(ISD::LOAD, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000069
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000070 setOperationAction(ISD::STORE, MVT::i8, Legal);
71 setOperationAction(ISD::STORE, MVT::i16, Custom);
72 setOperationAction(ISD::STORE, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000073
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000074 setOperationAction(ISD::ADDE, MVT::i8, Custom);
75 setOperationAction(ISD::ADDC, MVT::i8, Custom);
76 setOperationAction(ISD::SUBE, MVT::i8, Custom);
77 setOperationAction(ISD::SUBC, MVT::i8, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000078 setOperationAction(ISD::ADD, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000079 setOperationAction(ISD::ADD, MVT::i16, Custom);
80
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +000081 setOperationAction(ISD::OR, MVT::i8, Custom);
82 setOperationAction(ISD::AND, MVT::i8, Custom);
83 setOperationAction(ISD::XOR, MVT::i8, Custom);
84
Sanjiv Gupta1b046942009-01-13 19:18:47 +000085 setOperationAction(ISD::FrameIndex, MVT::i16, Custom);
86 setOperationAction(ISD::CALL, MVT::i16, Custom);
87 setOperationAction(ISD::RET, MVT::Other, Custom);
88
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000089 setOperationAction(ISD::MUL, MVT::i8, Custom);
90 setOperationAction(ISD::MUL, MVT::i16, Expand);
91 setOperationAction(ISD::MUL, MVT::i32, Expand);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000092
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000093 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
94 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
95 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
96 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
97 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
98 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
99 setOperationAction(ISD::MULHU, MVT::i8, Expand);
100 setOperationAction(ISD::MULHU, MVT::i16, Expand);
101 setOperationAction(ISD::MULHU, MVT::i32, Expand);
102 setOperationAction(ISD::MULHS, MVT::i8, Expand);
103 setOperationAction(ISD::MULHS, MVT::i16, Expand);
104 setOperationAction(ISD::MULHS, MVT::i32, Expand);
105
106 setOperationAction(ISD::SRA, MVT::i8, Custom);
107 setOperationAction(ISD::SRA, MVT::i16, Expand);
108 setOperationAction(ISD::SRA, MVT::i32, Expand);
109 setOperationAction(ISD::SHL, MVT::i8, Custom);
110 setOperationAction(ISD::SHL, MVT::i16, Expand);
111 setOperationAction(ISD::SHL, MVT::i32, Expand);
112 setOperationAction(ISD::SRL, MVT::i8, Custom);
113 setOperationAction(ISD::SRL, MVT::i16, Expand);
114 setOperationAction(ISD::SRL, MVT::i32, Expand);
115
116 // PIC16 does not support shift parts
117 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
118 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
119 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
120 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
121 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
122 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
123 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
124 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
125 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
126
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000127
128 // PIC16 does not have a SETCC, expand it to SELECT_CC.
129 setOperationAction(ISD::SETCC, MVT::i8, Expand);
130 setOperationAction(ISD::SELECT, MVT::i8, Expand);
131 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
132 setOperationAction(ISD::BRIND, MVT::Other, Expand);
133
134 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
135 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000136
137 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
138 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
139
140 // Now deduce the information based on the above mentioned
141 // actions
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000142 computeRegisterProperties();
143}
144
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000145// getOutFlag - Extract the flag result if the Op has it.
146static SDValue getOutFlag(SDValue &Op) {
147 // Flag is the last value of the node.
148 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
149
150 assert (Flag.getValueType() == MVT::Flag
151 && "Node does not have an out Flag");
152
153 return Flag;
154}
155
156// To extract chain value from the SDValue Nodes
157// This function will help to maintain the chain extracting
158// code at one place. In case of any change in future it will
159// help maintain the code.
160static SDValue getChain(SDValue &Op) {
161 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
162
163 // If the last value returned in Flag then the chain is
164 // second last value returned.
165 if (Chain.getValueType() == MVT::Flag)
166 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
167
168 // All nodes may not produce a chain. Therefore following assert
169 // verifies that the node is returning a chain only.
170 assert (Chain.getValueType() == MVT::Other
171 && "Node does not have a chain");
172
173 return Chain;
174}
175
176/// PopulateResults - Helper function to LowerOperation.
177/// If a node wants to return multiple results after lowering,
178/// it stuffs them into an array of SDValue called Results.
179
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000180static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
181 if (N.getOpcode() == ISD::MERGE_VALUES) {
182 int NumResults = N.getNumOperands();
183 for( int i = 0; i < NumResults; i++)
184 Results.push_back(N.getOperand(i));
185 }
186 else
187 Results.push_back(N);
188}
189
190MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
191 return MVT::i8;
192}
193
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000194/// FIXME: These three functions below should not be here if we change
195/// the generic code to allow generting libcalls for I8 types as well.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000196
197void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000198PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000199 const char *Name) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000200 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000201}
202
203const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000204PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
205 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000206}
207
208SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000209PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000210 MVT RetVT, const SDValue *Ops,
211 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000212 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000213
214 TargetLowering::ArgListTy Args;
215 Args.reserve(NumOps);
216
217 TargetLowering::ArgListEntry Entry;
218 for (unsigned i = 0; i != NumOps; ++i) {
219 Entry.Node = Ops[i];
220 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
221 Entry.isSExt = isSigned;
222 Entry.isZExt = !isSigned;
223 Args.push_back(Entry);
224 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000225 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000226
227 const Type *RetTy = RetVT.getTypeForMVT();
228 std::pair<SDValue,SDValue> CallInfo =
229 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000230 false, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000231
232 return CallInfo.first;
233}
234
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000235const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
236 switch (Opcode) {
237 default: return NULL;
238 case PIC16ISD::Lo: return "PIC16ISD::Lo";
239 case PIC16ISD::Hi: return "PIC16ISD::Hi";
240 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
241 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
242 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
243 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000244 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000245 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000246 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000247 case PIC16ISD::BCF: return "PIC16ISD::BCF";
248 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
249 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
250 case PIC16ISD::RLF: return "PIC16ISD::RLF";
251 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000252 case PIC16ISD::CALL: return "PIC16ISD::CALL";
253 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
254 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
255 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000256 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000257 }
258}
259
Duncan Sands1607f052008-12-01 11:39:25 +0000260void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
261 SmallVectorImpl<SDValue>&Results,
262 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000263
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000264 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000265 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000266 Results.push_back(ExpandGlobalAddress(N, DAG));
267 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000268 case ISD::ExternalSymbol:
269 Results.push_back(ExpandExternalSymbol(N, DAG));
270 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000271 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000272 Results.push_back(ExpandStore(N, DAG));
273 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000274 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000275 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000276 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000277 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000278 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000279 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000280 case ISD::FrameIndex:
281 Results.push_back(ExpandFrameIndex(N, DAG));
282 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000283 default:
284 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000285 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000286 }
287}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000288
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000289SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
290
291 // Currently handling FrameIndex of size MVT::i16 only
292 // One example of this scenario is when return value is written on
293 // FrameIndex#0
294
295 if (N->getValueType(0) != MVT::i16)
296 return SDValue();
297
298 // Expand the FrameIndex into ExternalSymbol and a Constant node
299 // The constant will represent the frame index number
300 // Get the current function frame
301 MachineFunction &MF = DAG.getMachineFunction();
302 const Function *Func = MF.getFunction();
303 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000304
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000305 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000306 // FIXME there isn't really debug info here
307 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000308 int Index = FR->getIndex();
309
310 SDValue FI[2];
311 FI[0] = DAG.getTargetFrameIndex(Index, MVT::i8);
312 FI[1] = DAG.getTargetFrameIndex(Index + 1, MVT::i8);
Dale Johannesende064702009-02-06 21:50:26 +0000313 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), FI[0], FI[1]);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000314}
315
316
317SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000318 StoreSDNode *St = cast<StoreSDNode>(N);
319 SDValue Chain = St->getChain();
320 SDValue Src = St->getValue();
321 SDValue Ptr = St->getBasePtr();
322 MVT ValueType = Src.getValueType();
323 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000324 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000325
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000326 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000327 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000328
329 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000330 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000331 PtrLo, PtrHi,
332 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000333 }
334 else if (ValueType == MVT::i16) {
335 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
336 SDValue SrcLo, SrcHi;
337 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
338 SDValue ChainLo = Chain, ChainHi = Chain;
339 if (Chain.getOpcode() == ISD::TokenFactor) {
340 ChainLo = Chain.getOperand(0);
341 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000342 }
Dale Johannesende064702009-02-06 21:50:26 +0000343 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000344 ChainLo,
345 SrcLo, PtrLo, PtrHi,
346 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000347
Dale Johannesende064702009-02-06 21:50:26 +0000348 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000349 SrcHi, PtrLo, PtrHi,
350 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000351
Dale Johannesende064702009-02-06 21:50:26 +0000352 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000353 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000354 }
355 else if (ValueType == MVT::i32) {
356 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
357 SDValue SrcLo, SrcHi;
358 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000359
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000360 // Get the expanded parts of each of SrcLo and SrcHi.
361 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
362 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
363 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000364
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000365 SDValue ChainLo = Chain, ChainHi = Chain;
366 if (Chain.getOpcode() == ISD::TokenFactor) {
367 ChainLo = Chain.getOperand(0);
368 ChainHi = Chain.getOperand(1);
369 }
370 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
371 ChainHi2 = ChainHi;
372 if (ChainLo.getOpcode() == ISD::TokenFactor) {
373 ChainLo1 = ChainLo.getOperand(0);
374 ChainLo2 = ChainLo.getOperand(1);
375 }
376 if (ChainHi.getOpcode() == ISD::TokenFactor) {
377 ChainHi1 = ChainHi.getOperand(0);
378 ChainHi2 = ChainHi.getOperand(1);
379 }
Dale Johannesende064702009-02-06 21:50:26 +0000380 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000381 ChainLo1,
382 SrcLo1, PtrLo, PtrHi,
383 DAG.getConstant (0 + StoreOffset, MVT::i8));
384
Dale Johannesende064702009-02-06 21:50:26 +0000385 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000386 SrcLo2, PtrLo, PtrHi,
387 DAG.getConstant (1 + StoreOffset, MVT::i8));
388
Dale Johannesende064702009-02-06 21:50:26 +0000389 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000390 SrcHi1, PtrLo, PtrHi,
391 DAG.getConstant (2 + StoreOffset, MVT::i8));
392
Dale Johannesende064702009-02-06 21:50:26 +0000393 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000394 SrcHi2, PtrLo, PtrHi,
395 DAG.getConstant (3 + StoreOffset, MVT::i8));
396
Dale Johannesende064702009-02-06 21:50:26 +0000397 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
398 getChain(Store1), getChain(Store2));
399 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
400 getChain(Store3), getChain(Store4));
401 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000402
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000403 }
404 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000405 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000406 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000407 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000408}
409
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000410SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
411{
412 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000413 // FIXME there isn't really debug info here
414 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000415
416 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
417
Dale Johannesende064702009-02-06 21:50:26 +0000418 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES);
419 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000420
Dale Johannesende064702009-02-06 21:50:26 +0000421 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000422}
423
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000424// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000425SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000426 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000427 // FIXME there isn't really debug info here
428 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000429
430 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
431 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000432
Dale Johannesende064702009-02-06 21:50:26 +0000433 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA);
434 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000435
Dale Johannesende064702009-02-06 21:50:26 +0000436 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000437}
438
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000439bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
440 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000441
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000442 if (Op.getOpcode() == ISD::BUILD_PAIR) {
443 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
444 return true;
445 }
446 return false;
447}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000448
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000449// Return true if DirectAddress is in ROM_SPACE
450bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000451
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000452 // RomAddress is a GlobalAddress in ROM_SPACE_
453 // If the Op is not a GlobalAddress return NULL without checking
454 // anything further.
455 if (!isDirectAddress(Op))
456 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000457
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000458 // Its a GlobalAddress.
459 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
460 SDValue TGA = Op.getOperand(0).getOperand(0);
461 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
462 const Type *ValueType = GSDN->getGlobal()->getType();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000463
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000464 if (!isa<PointerType>(ValueType)) {
465 assert(0 && "TGA must be of a PointerType");
466 }
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000467
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000468 int AddrSpace = dyn_cast<PointerType>(ValueType)->getAddressSpace();
469 if (AddrSpace == PIC16ISD::ROM_SPACE)
470 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000471
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000472 // Any other address space return it false
473 return false;
474}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000475
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000476
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000477// GetExpandedParts - This function is on the similiar lines as
478// the GetExpandedInteger in type legalizer is. This returns expanded
479// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000480
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000481void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
482 SDValue &Lo, SDValue &Hi) {
483 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000484 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000485 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000486
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000487 // Extract the lo component.
488 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
489 DAG.getConstant(0, MVT::i8));
490
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000491 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000492 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
493 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000494}
495
496// Legalize FrameIndex into ExternalSymbol and offset.
497void
498PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
499 SDValue &ES, int &Offset) {
500
501 MachineFunction &MF = DAG.getMachineFunction();
502 const Function *Func = MF.getFunction();
503 const std::string Name = Func->getName();
504
505 char *tmpName = new char [strlen(Name.c_str()) + 8];
506 sprintf(tmpName, "%s.args", Name.c_str());
507 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
508 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
509 Offset = FR->getIndex();
510
511 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000512}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000513
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000514// This function legalizes the PIC16 Addresses. If the Pointer is
515// -- Direct address variable residing
516// --> then a Banksel for that variable will be created.
517// -- Rom variable
518// --> then it will be treated as an indirect address.
519// -- Indirect address
520// --> then the address will be loaded into FSR
521// -- ADD with constant operand
522// --> then constant operand of ADD will be returned as Offset
523// and non-constant operand of ADD will be treated as pointer.
524// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000525
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000526void PIC16TargetLowering:: LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
527 SDValue &Lo, SDValue &Hi,
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000528 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000529
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000530 // Offset, by default, should be 0
531 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000532
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000533 // If the pointer is ADD with constant,
534 // return the constant value as the offset
535 if (Ptr.getOpcode() == ISD::ADD) {
536 SDValue OperLeft = Ptr.getOperand(0);
537 SDValue OperRight = Ptr.getOperand(1);
538 if (OperLeft.getOpcode() == ISD::Constant) {
539 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
540 Ptr = OperRight;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000541 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000542 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000543 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000544 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000545 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000546
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000547 // If the pointer is Type i8 and an external symbol
548 // then treat it as direct address.
549 // One example for such case is storing and loading
550 // from function frame during a call
551 if (Ptr.getValueType() == MVT::i8) {
552 switch (Ptr.getOpcode()) {
553 case ISD::TargetExternalSymbol:
554 Lo = Ptr;
555 Hi = DAG.getConstant(1, MVT::i8);
556 return;
557 }
558 }
559
560 if (Ptr.getOpcode() == ISD::BUILD_PAIR &&
561 Ptr.getOperand(0).getOpcode() == ISD::TargetFrameIndex) {
562
563 int FrameOffset;
564 LegalizeFrameIndex(Ptr.getOperand(0), DAG, Lo, FrameOffset);
565 Hi = DAG.getConstant(1, MVT::i8);
566 Offset += FrameOffset;
567 return;
568 }
569
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000570 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
571 // Direct addressing case for RAM variables. The Hi part is constant
572 // and the Lo part is the TGA itself.
573 Lo = Ptr.getOperand(0).getOperand(0);
574
575 // For direct addresses Hi is a constant. Value 1 for the constant
576 // signifies that banksel needs to generated for it. Value 0 for
577 // the constant signifies that banksel does not need to be generated
578 // for it. Mark it as 1 now and optimize later.
579 Hi = DAG.getConstant(1, MVT::i8);
580 return;
581 }
582
583 // Indirect addresses. Get the hi and lo parts of ptr.
584 GetExpandedParts(Ptr, DAG, Lo, Hi);
585
586 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000587 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
588 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000589
590 return;
591}
592
Duncan Sands1607f052008-12-01 11:39:25 +0000593SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000594 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
595 SDValue Chain = LD->getChain();
596 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000597 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000598
599 SDValue Load, Offset;
600 SDVTList Tys;
601 MVT VT, NewVT;
602 SDValue PtrLo, PtrHi;
603 unsigned LoadOffset;
604
605 // Legalize direct/indirect addresses. This will give the lo and hi parts
606 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000607 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000608
609 // Load from the pointer (direct address or FSR)
610 VT = N->getValueType(0);
611 unsigned NumLoads = VT.getSizeInBits() / 8;
612 std::vector<SDValue> PICLoads;
613 unsigned iter;
614 MVT MemVT = LD->getMemoryVT();
615 if(ISD::isNON_EXTLoad(N)) {
616 for (iter=0; iter<NumLoads ; ++iter) {
617 // Add the pointer offset if any
618 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
619 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000620 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000621 Offset);
622 PICLoads.push_back(Load);
623 }
624 } else {
625 // If it is extended load then use PIC16Load for Memory Bytes
626 // and for all extended bytes perform action based on type of
627 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
628
629
630 // For extended loads this is the memory value type
631 // i.e. without any extension
632 MVT MemVT = LD->getMemoryVT();
633 unsigned MemBytes = MemVT.getSizeInBits() / 8;
634 unsigned ExtdBytes = VT.getSizeInBits() / 8;
635 Offset = DAG.getConstant(LoadOffset, MVT::i8);
636
637 Tys = DAG.getVTList(MVT::i8, MVT::Other);
638 // For MemBytes generate PIC16Load with proper offset
639 for (iter=0; iter<MemBytes; ++iter) {
640 // Add the pointer offset if any
641 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000642 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000643 Offset);
644 PICLoads.push_back(Load);
645 }
646
647 // For SignExtendedLoad
648 if (ISD::isSEXTLoad(N)) {
649 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
650 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000651 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000652 DAG.getConstant(7, MVT::i8));
653 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
654 PICLoads.push_back(SRA);
655 }
656 } else if (ISD::isZEXTLoad(N)) {
657 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
658 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
659 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
660 PICLoads.push_back(ConstZero);
661 }
662 }
663 }
664 SDValue BP;
665
666 if (VT == MVT::i8) {
667 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000668 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000669 }
670 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000671 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000672 if (MemVT == MVT::i8)
673 Chain = getChain(PICLoads[0]);
674 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000675 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
676 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000677 } else if (VT == MVT::i32) {
678 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000679 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
680 PICLoads[0], PICLoads[1]);
681 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
682 PICLoads[2], PICLoads[3]);
683 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000684 if (MemVT == MVT::i8)
685 Chain = getChain(PICLoads[0]);
686 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000687 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
688 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000689 else {
690 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000691 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000692 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000693 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000694 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000695 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
696 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000697 }
698 }
699 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000700 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000701}
702
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000703SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
704 // We should have handled larger operands in type legalizer itself.
705 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
706
707 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000708 SDValue Value = N->getOperand(0);
709 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000710 PIC16ISD::PIC16Libcall CallCode;
711 switch (N->getOpcode()) {
712 case ISD::SRA:
713 CallCode = PIC16ISD::SRA_I8;
714 break;
715 case ISD::SHL:
716 CallCode = PIC16ISD::SLL_I8;
717 break;
718 case ISD::SRL:
719 CallCode = PIC16ISD::SRL_I8;
720 break;
721 default:
722 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000723 return SDValue();
724 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000725 SmallVector<SDValue, 2> Ops(2);
726 Ops[0] = Value;
727 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000728 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
729 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000730 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000731}
732
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000733void
734PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
735 SmallVectorImpl<SDValue>&Results,
736 SelectionDAG &DAG) {
737 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000738 SDValue Res;
739 unsigned i;
740 switch (Op.getOpcode()) {
741 case ISD::FORMAL_ARGUMENTS:
742 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
743 case ISD::LOAD:
744 Res = ExpandLoad(Op.getNode(), DAG); break;
745 case ISD::CALL:
746 Res = LowerCALL(Op, DAG); break;
747 default: {
748 // All other operations are handled in LowerOperation.
749 Res = LowerOperation(Op, DAG);
750 if (Res.getNode())
751 Results.push_back(Res);
752
753 return;
754 }
755 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000756
757 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000758 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000759 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000760 Results.push_back(SDValue(N, i));
761 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000762}
763
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000764SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
765 switch (Op.getOpcode()) {
766 case ISD::FORMAL_ARGUMENTS:
767 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000768 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000769 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000770 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000771 return LowerADD(Op, DAG);
772 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000773 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000774 case ISD::SUBE:
775 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000776 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000777 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000778 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000779 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000780 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000781 case ISD::SRA:
782 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000783 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000784 case ISD::OR:
785 case ISD::AND:
786 case ISD::XOR:
787 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000788 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000789 return LowerCALL(Op, DAG);
790 case ISD::RET:
791 return LowerRET(Op, DAG);
792 case ISD::BR_CC:
793 return LowerBR_CC(Op, DAG);
794 case ISD::SELECT_CC:
795 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000796 }
Dan Gohman475871a2008-07-27 21:46:04 +0000797 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000798}
799
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000800SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000801 SelectionDAG &DAG,
802 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000803 assert (Op.getValueType() == MVT::i8
804 && "illegal value type to store on stack.");
805
806 MachineFunction &MF = DAG.getMachineFunction();
807 const Function *Func = MF.getFunction();
808 const std::string FuncName = Func->getName();
809
810 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
811
812 // Put the value on stack.
813 // Get a stack slot index and convert to es.
814 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
815 sprintf(tmpName, "%s.tmp", FuncName.c_str());
816 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
817
818 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000819 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000820 DAG.getEntryNode(),
821 Op, ES,
822 DAG.getConstant (1, MVT::i8), // Banksel.
823 DAG.getConstant (FI, MVT::i8));
824
825 // Load the value from ES.
826 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000827 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000828 ES, DAG.getConstant (1, MVT::i8),
829 DAG.getConstant (FI, MVT::i8));
830
831 return Load.getValue(0);
832}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000833
834SDValue
835PIC16TargetLowering::LowerCallArguments(SDValue Op, SDValue Chain,
836 SDValue FrameAddress,
837 SDValue InFlag,
838 SelectionDAG &DAG) {
839 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
840 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +0000841 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000842 std::string Name;
843 SDValue Arg, StoreAt;
844 MVT ArgVT;
845 unsigned Size=0;
846 unsigned ArgCount=0;
847
848
849 // FIXME: This portion of code currently assumes only
850 // primitive types being passed as arguments.
851
852 // Legalize the address before use
853 SDValue PtrLo, PtrHi;
854 unsigned AddressOffset;
855 int StoreOffset = 0;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000856 LegalizeAddress(FrameAddress, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000857 SDValue StoreRet;
858
859 std::vector<SDValue> Ops;
860 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
861 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
862 // Get the argument
863 Arg = TheCall->getArg(i);
864
865 StoreOffset = (Offset + AddressOffset);
866
867 // Store the argument on frame
868
869 Ops.clear();
870 Ops.push_back(Chain);
871 Ops.push_back(Arg.getValue(0));
872 Ops.push_back(PtrLo);
873 Ops.push_back(PtrHi);
874 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
875 Ops.push_back(InFlag);
876
Dale Johannesene8d72302009-02-06 23:05:02 +0000877 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000878
879 Chain = getChain(StoreRet);
880 InFlag = getOutFlag(StoreRet);
881
882 // Update the frame offset to be used for next argument
883 ArgVT = Arg.getValueType();
884 Size = ArgVT.getSizeInBits();
885 Size = Size/8; // Calculate size in bytes
886 Offset += Size; // Increase the frame offset
887 }
888 return Chain;
889}
890
891SDValue
892PIC16TargetLowering::LowerCallReturn(SDValue Op, SDValue Chain,
893 SDValue FrameAddress,
894 SDValue InFlag,
895 SelectionDAG &DAG) {
896 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000897 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000898 // Currently handling primitive types only. They will come in
899 // i8 parts
900 unsigned RetVals = TheCall->getNumRetVals();
901
902 std::vector<SDValue> ResultVals;
903
904 // Return immediately if the return type is void
905 if (RetVals == 0)
906 return Chain;
907
908 // Call has something to return
909
910 // Legalize the address before use
911 SDValue LdLo, LdHi;
912 unsigned LdOffset;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000913 LegalizeAddress(FrameAddress, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000914
915 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
916 SDValue LoadRet;
917
918 for(unsigned i=0, Offset=0;i<RetVals;i++) {
919
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000920 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000921 DAG.getConstant(LdOffset + Offset, MVT::i8),
922 InFlag);
923
924 InFlag = getOutFlag(LoadRet);
925
926 Chain = getChain(LoadRet);
927 Offset++;
928 ResultVals.push_back(LoadRet);
929 }
930
931 // To return use MERGE_VALUES
932 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000933 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000934 return Res;
935}
936
937SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
938 //int NumOps = Op.getNode()->getNumOperands();
939
940 // For default cases LLVM returns the value on the function frame
941 // So let LLVM do this for all the cases other than character
942 return Op;
943}
944
945SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
946 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
947 SDValue Chain = TheCall->getChain();
948 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000949 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000950 unsigned i =0;
951 if (Callee.getValueType() == MVT::i16 &&
952 Callee.getOpcode() == ISD::BUILD_PAIR) {
953 // It has come from TypeLegalizer for lowering
954
955 Callee = Callee.getOperand(0).getOperand(0);
956
957 std::vector<SDValue> Ops;
958 Ops.push_back(Chain);
959 Ops.push_back(Callee);
960
961 // Add the call arguments and their flags
962 unsigned NumArgs = TheCall->getNumArgs();
963 for(i=0;i<NumArgs;i++) {
964 Ops.push_back(TheCall->getArg(i));
965 Ops.push_back(TheCall->getArgFlagsVal(i));
966 }
967
968 std::vector<MVT> NodeTys;
969 unsigned NumRets = TheCall->getNumRetVals();
970 for(i=0;i<NumRets;i++)
971 NodeTys.push_back(TheCall->getRetValType(i));
972
973 // Return a Chain as well
974 NodeTys.push_back(MVT::Other);
975
976 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
977 SDValue NewCall =
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000978 DAG.getCall(TheCall->getCallingConv(), dl,
Dale Johannesen8ad9b432009-02-04 01:17:06 +0000979 TheCall->isVarArg(), TheCall->isTailCall(),
980 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000981
982 return NewCall;
983 }
984
985 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
986
987 // Start the call sequence.
988 // Carring the Constant 0 along the CALLSEQSTART
989 // because there is nothing else to carry.
990 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
991 Chain = getChain(SeqStart);
992
993 // For any direct call - callee will be GlobalAddressNode or
994 // ExternalSymbol
995
996 // Considering the GlobalAddressNode case here.
997 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
998 GlobalValue *GV = G->getGlobal();
999 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1000 }
1001
1002 // Considering the ExternalSymbol case here
1003 if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1004 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1005 }
1006
1007 SDValue OperFlag = getOutFlag(Chain); // To manage the data dependency
1008
1009 std::string Name;
1010
1011 // Considering GlobalAddress here
1012 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1013 Name = G->getGlobal()->getName();
1014
1015 // Considering ExternalSymbol here
1016 if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee))
1017 Name = ES->getSymbol();
1018
1019 char *argFrame = new char [strlen(Name.c_str()) + 8];
1020 sprintf(argFrame, "%s.args", Name.c_str());
1021 SDValue ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
1022
1023 char *retName = new char [strlen(Name.c_str()) + 8];
1024 sprintf(retName, "%s.retval", Name.c_str());
1025 SDValue RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1026
1027 // Pass the argument to function before making the call.
1028 SDValue CallArgs = LowerCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1029 Chain = getChain(CallArgs);
1030 OperFlag = getOutFlag(CallArgs);
1031
1032 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001033 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001034 OperFlag);
1035 Chain = getChain(PICCall);
1036 OperFlag = getOutFlag(PICCall);
1037
1038
1039 // Carrying the Constant 0 along the CALLSEQSTART
1040 // because there is nothing else to carry.
1041 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1042 OperFlag);
1043 Chain = getChain(SeqEnd);
1044 OperFlag = getOutFlag(SeqEnd);
1045
1046 // Lower the return value reading after the call.
1047 return LowerCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1048}
1049
1050bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1051 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1052 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1053 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1054 return true;
1055 return false;
1056}
1057
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001058// NeedToConvertToMemOp - Returns true if one of the operands of the
1059// operation 'Op' needs to be put into memory. Also returns the
1060// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1061// no instruction that can operation on two registers. Most insns take
1062// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001063bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001064 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001065 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1066 Op.getOperand(1).getOpcode() == ISD::Constant)
1067 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001068
1069 // Return false if one of the operands is already a direct
1070 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001071 if (isDirectLoad(Op.getOperand(0))) {
1072 if (Op.getOperand(0).hasOneUse())
1073 return false;
1074 else
1075 MemOp = 0;
1076 }
1077 if (isDirectLoad(Op.getOperand(1))) {
1078 if (Op.getOperand(1).hasOneUse())
1079 return false;
1080 else
1081 MemOp = 1;
1082 }
1083 return true;
1084}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001085
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001086// LowerBinOp - Lower a commutative binary operation that does not
1087// affect status flag carry.
1088SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001089 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001090
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001091 // We should have handled larger operands in type legalizer itself.
1092 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001093
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001094 unsigned MemOp = 1;
1095 if (NeedToConvertToMemOp(Op, MemOp)) {
1096 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001097 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001098
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001099 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001100 NewVal);
1101 }
1102 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001103 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001104 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001105}
1106
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001107// LowerADD - Lower all types of ADD operations including the ones
1108// that affects carry.
1109SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001110 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001111 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001112 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001113 unsigned MemOp = 1;
1114 if (NeedToConvertToMemOp(Op, MemOp)) {
1115 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001116 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001117
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001118 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001119
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001120 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001121 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1122 NewVal, Op.getOperand(2));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001123 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001124 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1125 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001126 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001127 else if (Op.getOpcode() == ISD::ADD) {
1128 return Op;
1129 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001130 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001131 return SDValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001132 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001133}
1134
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001135SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001136 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001137 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001138 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001139
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001140 // Nothing to do if the first operand is already a direct load and it has
1141 // only one use.
1142 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001143 return SDValue();
1144
1145 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001146 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001147
1148 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001149 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001150 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001151 Op.getOperand(2));
1152 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001153 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001154}
1155
1156// LowerFORMAL_ARGUMENTS - In Lowering FORMAL ARGUMENTS - MERGE_VALUES nodes
1157// is returned. MERGE_VALUES nodes number of operands and number of values are
1158// equal. Therefore to construct MERGE_VALUE node, UNDEF nodes equal to the
1159// number of arguments of function have been created.
1160
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001161SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001162 SelectionDAG &DAG) {
1163 SmallVector<SDValue, 8> ArgValues;
1164 unsigned NumArgs = Op.getNumOperands() - 3;
Dale Johannesene8d72302009-02-06 23:05:02 +00001165 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001166
1167 // Creating UNDEF nodes to meet the requirement of MERGE_VALUES node.
1168 for(unsigned i = 0 ; i<NumArgs ; i++) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001169 SDValue TempNode = DAG.getUNDEF(Op.getNode()->getValueType(i));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001170 ArgValues.push_back(TempNode);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001171 }
1172
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001173 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001174 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001175 &ArgValues[0],
1176 ArgValues.size()).getValue(Op.getResNo());
1177}
1178
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001179// Perform DAGCombine of PIC16Load.
1180// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001181SDValue PIC16TargetLowering::
1182PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1183 SelectionDAG &DAG = DCI.DAG;
1184 SDValue Chain = N->getOperand(0);
1185 if (N->hasNUsesOfValue(0, 0)) {
1186 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1187 }
1188 return SDValue();
1189}
1190
1191
1192SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1193 DAGCombinerInfo &DCI) const {
1194 switch (N->getOpcode()) {
1195 case PIC16ISD::PIC16Load:
1196 return PerformPIC16LoadCombine(N, DCI);
1197 }
1198 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001199}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001200
1201static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1202 switch (CC) {
1203 default: assert(0 && "Unknown condition code!");
1204 case ISD::SETNE: return PIC16CC::NE;
1205 case ISD::SETEQ: return PIC16CC::EQ;
1206 case ISD::SETGT: return PIC16CC::GT;
1207 case ISD::SETGE: return PIC16CC::GE;
1208 case ISD::SETLT: return PIC16CC::LT;
1209 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001210 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001211 case ISD::SETULE: return PIC16CC::LE;
1212 case ISD::SETUGE: return PIC16CC::GE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001213 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001214 }
1215}
1216
1217// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1218// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1219static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1220 ISD::CondCode CC, unsigned &SPCC) {
1221 if (isa<ConstantSDNode>(RHS) &&
1222 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1223 CC == ISD::SETNE &&
1224 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1225 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1226 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1227 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1228 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1229 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1230 SDValue CMPCC = LHS.getOperand(3);
1231 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1232 LHS = CMPCC.getOperand(0);
1233 RHS = CMPCC.getOperand(1);
1234 }
1235}
1236
1237// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1238SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1239 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001240 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001241 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1242
1243 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1244 // i.e. a < 12 can be rewritten as 12 > a.
1245 if (RHS.getOpcode() == ISD::Constant) {
1246
1247 SDValue Tmp = LHS;
1248 LHS = RHS;
1249 RHS = Tmp;
1250
1251 switch (CondCode) {
1252 default: break;
1253 case PIC16CC::LT:
1254 CondCode = PIC16CC::GT;
1255 break;
1256 case PIC16CC::GT:
1257 CondCode = PIC16CC::LT;
1258 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001259 case PIC16CC::ULT:
1260 CondCode = PIC16CC::UGT;
1261 break;
1262 case PIC16CC::UGT:
1263 CondCode = PIC16CC::ULT;
1264 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001265 case PIC16CC::GE:
1266 CondCode = PIC16CC::LE;
1267 break;
1268 case PIC16CC::LE:
1269 CondCode = PIC16CC::GE;
1270 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001271 case PIC16CC::ULE:
1272 CondCode = PIC16CC::UGE;
1273 break;
1274 case PIC16CC::UGE:
1275 CondCode = PIC16CC::ULE;
1276 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001277 }
1278 }
1279
1280 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001281
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001282 // These are signed comparisons.
1283 SDValue Mask = DAG.getConstant(128, MVT::i8);
1284 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001285 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1286 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001287 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001288
1289 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001290 // We can use a subtract operation to set the condition codes. But
1291 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001292 // Nothing to do if the first operand is already a valid type (direct load
1293 // for subwf and literal for sublw) and it is used by this operation only.
1294 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1295 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001296 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001297
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001298 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001299 LHS = ConvertToMemOperand (LHS, DAG, dl);
1300 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001301}
1302
1303
1304SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1305 SDValue LHS = Op.getOperand(0);
1306 SDValue RHS = Op.getOperand(1);
1307 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1308 SDValue TrueVal = Op.getOperand(2);
1309 SDValue FalseVal = Op.getOperand(3);
1310 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001311 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001312
1313 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1314 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1315 // i.e.
1316 // A setcc: lhs, rhs, cc is expanded by llvm to
1317 // select_cc: result of setcc, 0, 1, 0, setne
1318 // We can think of it as:
1319 // select_cc: lhs, rhs, 1, 0, cc
1320 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1321 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1322
1323 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001324 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001325
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001326 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001327 FalseVal, PIC16CC, Cmp.getValue(1));
1328}
1329
1330MachineBasicBlock *
1331PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001332 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001333 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1334 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001335 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001336
1337 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1338 // control-flow pattern. The incoming instruction knows the destination vreg
1339 // to set, the condition code register to branch on, the true/false values to
1340 // select between, and a branch opcode to use.
1341 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1342 MachineFunction::iterator It = BB;
1343 ++It;
1344
1345 // thisMBB:
1346 // ...
1347 // TrueVal = ...
1348 // [f]bCC copy1MBB
1349 // fallthrough --> copy0MBB
1350 MachineBasicBlock *thisMBB = BB;
1351 MachineFunction *F = BB->getParent();
1352 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1353 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001354 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001355 F->insert(It, copy0MBB);
1356 F->insert(It, sinkMBB);
1357
1358 // Update machine-CFG edges by transferring all successors of the current
1359 // block to the new block which will contain the Phi node for the select.
1360 sinkMBB->transferSuccessors(BB);
1361 // Next, add the true and fallthrough blocks as its successors.
1362 BB->addSuccessor(copy0MBB);
1363 BB->addSuccessor(sinkMBB);
1364
1365 // copy0MBB:
1366 // %FalseValue = ...
1367 // # fallthrough to sinkMBB
1368 BB = copy0MBB;
1369
1370 // Update machine-CFG edges
1371 BB->addSuccessor(sinkMBB);
1372
1373 // sinkMBB:
1374 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1375 // ...
1376 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001377 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001378 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1379 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1380
1381 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1382 return BB;
1383}
1384
1385
1386SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1387 SDValue Chain = Op.getOperand(0);
1388 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1389 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1390 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1391 SDValue Dest = Op.getOperand(4); // BB to jump to
1392 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001393 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001394
1395 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1396 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1397 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1398 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1399
1400 // Get the Compare insn and condition code.
1401 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001402 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001403
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001404 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001405 Cmp.getValue(1));
1406}
1407
1408