2 // The LLVM Compiler Infrastructure
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===//
9 // This file defines the interfaces that PIC16 uses to lower LLVM code into a
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "pic16-lower"
15 #include "PIC16ABINames.h"
16 #include "PIC16ISelLowering.h"
17 #include "PIC16TargetObjectFile.h"
18 #include "PIC16TargetMachine.h"
19 #include "PIC16MachineFunctionInfo.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/GlobalValue.h"
22 #include "llvm/Function.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/SelectionDAGISel.h"
29 #include "llvm/Support/ErrorHandling.h"
34 static const char *getIntrinsicName(unsigned opcode) {
37 default: llvm_unreachable("do not know intrinsic name");
38 // Arithmetic Right shift for integer types.
39 case PIC16ISD::SRA_I8: Basename = "sra.i8"; break;
40 case RTLIB::SRA_I16: Basename = "sra.i16"; break;
41 case RTLIB::SRA_I32: Basename = "sra.i32"; break;
43 // Left shift for integer types.
44 case PIC16ISD::SLL_I8: Basename = "sll.i8"; break;
45 case RTLIB::SHL_I16: Basename = "sll.i16"; break;
46 case RTLIB::SHL_I32: Basename = "sll.i32"; break;
48 // Logical Right Shift for integer types.
49 case PIC16ISD::SRL_I8: Basename = "srl.i8"; break;
50 case RTLIB::SRL_I16: Basename = "srl.i16"; break;
51 case RTLIB::SRL_I32: Basename = "srl.i32"; break;
53 // Multiply for integer types.
54 case PIC16ISD::MUL_I8: Basename = "mul.i8"; break;
55 case RTLIB::MUL_I16: Basename = "mul.i16"; break;
56 case RTLIB::MUL_I32: Basename = "mul.i32"; break;
58 // Signed division for integers.
59 case RTLIB::SDIV_I16: Basename = "sdiv.i16"; break;
60 case RTLIB::SDIV_I32: Basename = "sdiv.i32"; break;
62 // Unsigned division for integers.
63 case RTLIB::UDIV_I16: Basename = "udiv.i16"; break;
64 case RTLIB::UDIV_I32: Basename = "udiv.i32"; break;
66 // Signed Modulas for integers.
67 case RTLIB::SREM_I16: Basename = "srem.i16"; break;
68 case RTLIB::SREM_I32: Basename = "srem.i32"; break;
70 // Unsigned Modulas for integers.
71 case RTLIB::UREM_I16: Basename = "urem.i16"; break;
72 case RTLIB::UREM_I32: Basename = "urem.i32"; break;
74 //////////////////////
75 // LIBCALLS FOR FLOATS
76 //////////////////////
78 // Float to signed integrals
79 case RTLIB::FPTOSINT_F32_I8: Basename = "f32_to_si32"; break;
80 case RTLIB::FPTOSINT_F32_I16: Basename = "f32_to_si32"; break;
81 case RTLIB::FPTOSINT_F32_I32: Basename = "f32_to_si32"; break;
83 // Signed integrals to float. char and int are first sign extended to i32
84 // before being converted to float, so an I8_F32 or I16_F32 isn't required.
85 case RTLIB::SINTTOFP_I32_F32: Basename = "si32_to_f32"; break;
87 // Float to Unsigned conversions.
88 // Signed conversion can be used for unsigned conversion as well.
89 // In signed and unsigned versions only the interpretation of the
90 // MSB is different. Bit representation remains the same.
91 case RTLIB::FPTOUINT_F32_I8: Basename = "f32_to_si32"; break;
92 case RTLIB::FPTOUINT_F32_I16: Basename = "f32_to_si32"; break;
93 case RTLIB::FPTOUINT_F32_I32: Basename = "f32_to_si32"; break;
95 // Unsigned to Float conversions. char and int are first zero extended
96 // before being converted to float.
97 case RTLIB::UINTTOFP_I32_F32: Basename = "ui32_to_f32"; break;
99 // Floating point add, sub, mul, div.
100 case RTLIB::ADD_F32: Basename = "add.f32"; break;
101 case RTLIB::SUB_F32: Basename = "sub.f32"; break;
102 case RTLIB::MUL_F32: Basename = "mul.f32"; break;
103 case RTLIB::DIV_F32: Basename = "div.f32"; break;
105 // Floating point comparison
106 case RTLIB::O_F32: Basename = "unordered.f32"; break;
107 case RTLIB::UO_F32: Basename = "unordered.f32"; break;
108 case RTLIB::OLE_F32: Basename = "le.f32"; break;
109 case RTLIB::OGE_F32: Basename = "ge.f32"; break;
110 case RTLIB::OLT_F32: Basename = "lt.f32"; break;
111 case RTLIB::OGT_F32: Basename = "gt.f32"; break;
112 case RTLIB::OEQ_F32: Basename = "eq.f32"; break;
113 case RTLIB::UNE_F32: Basename = "neq.f32"; break;
116 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
117 std::string tagname = PAN::getTagName(PAN::LIBCALL);
118 std::string Fullname = prefix + tagname + Basename;
120 // The name has to live through program life.
121 return ESNames::createESName(Fullname);
124 // getStdLibCallName - Get the name for the standard library function.
125 static const char *getStdLibCallName(unsigned opcode) {
126 std::string BaseName;
128 case RTLIB::COS_F32: BaseName = "cos";
130 case RTLIB::SIN_F32: BaseName = "sin";
132 case RTLIB::MEMCPY: BaseName = "memcpy";
134 case RTLIB::MEMSET: BaseName = "memset";
136 case RTLIB::MEMMOVE: BaseName = "memmove";
138 default: llvm_unreachable("do not know std lib call name");
140 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
141 std::string LibCallName = prefix + BaseName;
143 // The name has to live through program life.
144 return ESNames::createESName(LibCallName);
147 // PIC16TargetLowering Constructor.
148 PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
149 : TargetLowering(TM, new PIC16TargetObjectFile()) {
151 Subtarget = &TM.getSubtarget<PIC16Subtarget>();
153 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
155 setShiftAmountType(MVT::i8);
157 // Std lib call names
158 setLibcallName(RTLIB::COS_F32, getStdLibCallName(RTLIB::COS_F32));
159 setLibcallName(RTLIB::SIN_F32, getStdLibCallName(RTLIB::SIN_F32));
160 setLibcallName(RTLIB::MEMCPY, getStdLibCallName(RTLIB::MEMCPY));
161 setLibcallName(RTLIB::MEMSET, getStdLibCallName(RTLIB::MEMSET));
162 setLibcallName(RTLIB::MEMMOVE, getStdLibCallName(RTLIB::MEMMOVE));
164 // SRA library call names
165 setPIC16LibcallName(PIC16ISD::SRA_I8, getIntrinsicName(PIC16ISD::SRA_I8));
166 setLibcallName(RTLIB::SRA_I16, getIntrinsicName(RTLIB::SRA_I16));
167 setLibcallName(RTLIB::SRA_I32, getIntrinsicName(RTLIB::SRA_I32));
169 // SHL library call names
170 setPIC16LibcallName(PIC16ISD::SLL_I8, getIntrinsicName(PIC16ISD::SLL_I8));
171 setLibcallName(RTLIB::SHL_I16, getIntrinsicName(RTLIB::SHL_I16));
172 setLibcallName(RTLIB::SHL_I32, getIntrinsicName(RTLIB::SHL_I32));
174 // SRL library call names
175 setPIC16LibcallName(PIC16ISD::SRL_I8, getIntrinsicName(PIC16ISD::SRL_I8));
176 setLibcallName(RTLIB::SRL_I16, getIntrinsicName(RTLIB::SRL_I16));
177 setLibcallName(RTLIB::SRL_I32, getIntrinsicName(RTLIB::SRL_I32));
179 // MUL Library call names
180 setPIC16LibcallName(PIC16ISD::MUL_I8, getIntrinsicName(PIC16ISD::MUL_I8));
181 setLibcallName(RTLIB::MUL_I16, getIntrinsicName(RTLIB::MUL_I16));
182 setLibcallName(RTLIB::MUL_I32, getIntrinsicName(RTLIB::MUL_I32));
184 // Signed division lib call names
185 setLibcallName(RTLIB::SDIV_I16, getIntrinsicName(RTLIB::SDIV_I16));
186 setLibcallName(RTLIB::SDIV_I32, getIntrinsicName(RTLIB::SDIV_I32));
188 // Unsigned division lib call names
189 setLibcallName(RTLIB::UDIV_I16, getIntrinsicName(RTLIB::UDIV_I16));
190 setLibcallName(RTLIB::UDIV_I32, getIntrinsicName(RTLIB::UDIV_I32));
192 // Signed remainder lib call names
193 setLibcallName(RTLIB::SREM_I16, getIntrinsicName(RTLIB::SREM_I16));
194 setLibcallName(RTLIB::SREM_I32, getIntrinsicName(RTLIB::SREM_I32));
196 // Unsigned remainder lib call names
197 setLibcallName(RTLIB::UREM_I16, getIntrinsicName(RTLIB::UREM_I16));
198 setLibcallName(RTLIB::UREM_I32, getIntrinsicName(RTLIB::UREM_I32));
200 // Floating point to signed int conversions.
201 setLibcallName(RTLIB::FPTOSINT_F32_I8,
202 getIntrinsicName(RTLIB::FPTOSINT_F32_I8));
203 setLibcallName(RTLIB::FPTOSINT_F32_I16,
204 getIntrinsicName(RTLIB::FPTOSINT_F32_I16));
205 setLibcallName(RTLIB::FPTOSINT_F32_I32,
206 getIntrinsicName(RTLIB::FPTOSINT_F32_I32));
208 // Signed int to floats.
209 setLibcallName(RTLIB::SINTTOFP_I32_F32,
210 getIntrinsicName(RTLIB::SINTTOFP_I32_F32));
212 // Floating points to unsigned ints.
213 setLibcallName(RTLIB::FPTOUINT_F32_I8,
214 getIntrinsicName(RTLIB::FPTOUINT_F32_I8));
215 setLibcallName(RTLIB::FPTOUINT_F32_I16,
216 getIntrinsicName(RTLIB::FPTOUINT_F32_I16));
217 setLibcallName(RTLIB::FPTOUINT_F32_I32,
218 getIntrinsicName(RTLIB::FPTOUINT_F32_I32));
220 // Unsigned int to floats.
221 setLibcallName(RTLIB::UINTTOFP_I32_F32,
222 getIntrinsicName(RTLIB::UINTTOFP_I32_F32));
224 // Floating point add, sub, mul ,div.
225 setLibcallName(RTLIB::ADD_F32, getIntrinsicName(RTLIB::ADD_F32));
226 setLibcallName(RTLIB::SUB_F32, getIntrinsicName(RTLIB::SUB_F32));
227 setLibcallName(RTLIB::MUL_F32, getIntrinsicName(RTLIB::MUL_F32));
228 setLibcallName(RTLIB::DIV_F32, getIntrinsicName(RTLIB::DIV_F32));
230 // Floationg point comparison
231 setLibcallName(RTLIB::O_F32, getIntrinsicName(RTLIB::O_F32));
232 setLibcallName(RTLIB::UO_F32, getIntrinsicName(RTLIB::UO_F32));
233 setLibcallName(RTLIB::OLE_F32, getIntrinsicName(RTLIB::OLE_F32));
234 setLibcallName(RTLIB::OGE_F32, getIntrinsicName(RTLIB::OGE_F32));
235 setLibcallName(RTLIB::OLT_F32, getIntrinsicName(RTLIB::OLT_F32));
236 setLibcallName(RTLIB::OGT_F32, getIntrinsicName(RTLIB::OGT_F32));
237 setLibcallName(RTLIB::OEQ_F32, getIntrinsicName(RTLIB::OEQ_F32));
238 setLibcallName(RTLIB::UNE_F32, getIntrinsicName(RTLIB::UNE_F32));
240 // Return value comparisons of floating point calls.
241 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
242 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
243 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
244 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
245 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
246 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
247 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
248 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
250 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
251 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
253 setOperationAction(ISD::LOAD, MVT::i8, Legal);
254 setOperationAction(ISD::LOAD, MVT::i16, Custom);
255 setOperationAction(ISD::LOAD, MVT::i32, Custom);
257 setOperationAction(ISD::STORE, MVT::i8, Legal);
258 setOperationAction(ISD::STORE, MVT::i16, Custom);
259 setOperationAction(ISD::STORE, MVT::i32, Custom);
260 setOperationAction(ISD::STORE, MVT::i64, Custom);
262 setOperationAction(ISD::ADDE, MVT::i8, Custom);
263 setOperationAction(ISD::ADDC, MVT::i8, Custom);
264 setOperationAction(ISD::SUBE, MVT::i8, Custom);
265 setOperationAction(ISD::SUBC, MVT::i8, Custom);
266 setOperationAction(ISD::SUB, MVT::i8, Custom);
267 setOperationAction(ISD::ADD, MVT::i8, Custom);
268 setOperationAction(ISD::ADD, MVT::i16, Custom);
270 setOperationAction(ISD::OR, MVT::i8, Custom);
271 setOperationAction(ISD::AND, MVT::i8, Custom);
272 setOperationAction(ISD::XOR, MVT::i8, Custom);
274 setOperationAction(ISD::FrameIndex, MVT::i16, Custom);
276 setOperationAction(ISD::MUL, MVT::i8, Custom);
278 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
279 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
280 setOperationAction(ISD::MULHU, MVT::i8, Expand);
281 setOperationAction(ISD::MULHS, MVT::i8, Expand);
283 setOperationAction(ISD::SRA, MVT::i8, Custom);
284 setOperationAction(ISD::SHL, MVT::i8, Custom);
285 setOperationAction(ISD::SRL, MVT::i8, Custom);
287 setOperationAction(ISD::ROTL, MVT::i8, Expand);
288 setOperationAction(ISD::ROTR, MVT::i8, Expand);
290 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
292 // PIC16 does not support shift parts
293 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
294 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
295 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
298 // PIC16 does not have a SETCC, expand it to SELECT_CC.
299 setOperationAction(ISD::SETCC, MVT::i8, Expand);
300 setOperationAction(ISD::SELECT, MVT::i8, Expand);
301 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
302 setOperationAction(ISD::BRIND, MVT::Other, Expand);
304 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
305 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
307 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
308 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
310 // Now deduce the information based on the above mentioned
312 computeRegisterProperties();
315 // getOutFlag - Extract the flag result if the Op has it.
316 static SDValue getOutFlag(SDValue &Op) {
317 // Flag is the last value of the node.
318 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
320 assert (Flag.getValueType() == MVT::Flag
321 && "Node does not have an out Flag");
325 // Get the TmpOffset for FrameIndex
326 unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI, unsigned size,
327 MachineFunction &MF) const {
328 PIC16MachineFunctionInfo *FuncInfo = MF.getInfo<PIC16MachineFunctionInfo>();
329 std::map<unsigned, unsigned> &FiTmpOffsetMap = FuncInfo->getFiTmpOffsetMap();
331 std::map<unsigned, unsigned>::iterator
332 MapIt = FiTmpOffsetMap.find(FI);
333 if (MapIt != FiTmpOffsetMap.end())
334 return MapIt->second;
336 // This FI (FrameIndex) is not yet mapped, so map it
337 FiTmpOffsetMap[FI] = FuncInfo->getTmpSize();
338 FuncInfo->setTmpSize(FuncInfo->getTmpSize() + size);
339 return FiTmpOffsetMap[FI];
342 void PIC16TargetLowering::ResetTmpOffsetMap(SelectionDAG &DAG) const {
343 MachineFunction &MF = DAG.getMachineFunction();
344 PIC16MachineFunctionInfo *FuncInfo = MF.getInfo<PIC16MachineFunctionInfo>();
345 FuncInfo->getFiTmpOffsetMap().clear();
346 FuncInfo->setTmpSize(0);
349 // To extract chain value from the SDValue Nodes
350 // This function will help to maintain the chain extracting
351 // code at one place. In case of any change in future it will
352 // help maintain the code.
353 static SDValue getChain(SDValue &Op) {
354 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
356 // If the last value returned in Flag then the chain is
357 // second last value returned.
358 if (Chain.getValueType() == MVT::Flag)
359 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
361 // All nodes may not produce a chain. Therefore following assert
362 // verifies that the node is returning a chain only.
363 assert (Chain.getValueType() == MVT::Other
364 && "Node does not have a chain");
369 /// PopulateResults - Helper function to LowerOperation.
370 /// If a node wants to return multiple results after lowering,
371 /// it stuffs them into an array of SDValue called Results.
373 static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
374 if (N.getOpcode() == ISD::MERGE_VALUES) {
375 int NumResults = N.getNumOperands();
376 for( int i = 0; i < NumResults; i++)
377 Results.push_back(N.getOperand(i));
380 Results.push_back(N);
384 PIC16TargetLowering::getSetCCResultType(EVT ValType) const {
389 PIC16TargetLowering::getCmpLibcallReturnType() const {
393 /// The type legalizer framework of generating legalizer can generate libcalls
394 /// only when the operand/result types are illegal.
395 /// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
396 /// For example an arithmetic right shift. These functions are used to lower
397 /// such operations that generate libcall for legal types.
400 PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
402 PIC16LibcallNames[Call] = Name;
406 PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) const {
407 return PIC16LibcallNames[Call];
411 PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
412 EVT RetVT, const SDValue *Ops,
413 unsigned NumOps, bool isSigned,
414 SelectionDAG &DAG, DebugLoc dl) const {
416 TargetLowering::ArgListTy Args;
417 Args.reserve(NumOps);
419 TargetLowering::ArgListEntry Entry;
420 for (unsigned i = 0; i != NumOps; ++i) {
422 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
423 Entry.isSExt = isSigned;
424 Entry.isZExt = !isSigned;
425 Args.push_back(Entry);
428 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i16);
430 const Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
431 std::pair<SDValue,SDValue> CallInfo =
432 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
433 false, 0, CallingConv::C, false,
434 /*isReturnValueUsed=*/true,
435 Callee, Args, DAG, dl);
437 return CallInfo.first;
440 const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
442 default: return NULL;
443 case PIC16ISD::Lo: return "PIC16ISD::Lo";
444 case PIC16ISD::Hi: return "PIC16ISD::Hi";
445 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
446 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
447 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
448 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
449 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
450 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
451 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
452 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
453 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
454 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
455 case PIC16ISD::BCF: return "PIC16ISD::BCF";
456 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
457 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
458 case PIC16ISD::RLF: return "PIC16ISD::RLF";
459 case PIC16ISD::RRF: return "PIC16ISD::RRF";
460 case PIC16ISD::CALL: return "PIC16ISD::CALL";
461 case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
462 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
463 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
464 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
465 case PIC16ISD::RET: return "PIC16ISD::RET";
466 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
470 void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
471 SmallVectorImpl<SDValue>&Results,
472 SelectionDAG &DAG) const {
474 switch (N->getOpcode()) {
475 case ISD::GlobalAddress:
476 Results.push_back(ExpandGlobalAddress(N, DAG));
478 case ISD::ExternalSymbol:
479 Results.push_back(ExpandExternalSymbol(N, DAG));
482 Results.push_back(ExpandStore(N, DAG));
485 PopulateResults(ExpandLoad(N, DAG), Results);
488 // Results.push_back(ExpandAdd(N, DAG));
490 case ISD::FrameIndex:
491 Results.push_back(ExpandFrameIndex(N, DAG));
494 assert (0 && "not implemented");
499 SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N,
500 SelectionDAG &DAG) const {
502 // Currently handling FrameIndex of size MVT::i16 only
503 // One example of this scenario is when return value is written on
506 if (N->getValueType(0) != MVT::i16)
509 // Expand the FrameIndex into ExternalSymbol and a Constant node
510 // The constant will represent the frame index number
511 // Get the current function frame
512 MachineFunction &MF = DAG.getMachineFunction();
513 const Function *Func = MF.getFunction();
514 const std::string Name = Func->getName();
516 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
517 // FIXME there isn't really debug info here
518 DebugLoc dl = FR->getDebugLoc();
520 // Expand FrameIndex like GlobalAddress and ExternalSymbol
521 // Also use Offset field for lo and hi parts. The default
526 SDValue FI = SDValue(N,0);
527 LegalizeFrameIndex(FI, DAG, ES, FrameOffset);
528 SDValue Offset = DAG.getConstant(FrameOffset, MVT::i8);
529 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, ES, Offset);
530 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, ES, Offset);
531 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), Lo, Hi);
535 SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) const {
536 StoreSDNode *St = cast<StoreSDNode>(N);
537 SDValue Chain = St->getChain();
538 SDValue Src = St->getValue();
539 SDValue Ptr = St->getBasePtr();
540 EVT ValueType = Src.getValueType();
541 unsigned StoreOffset = 0;
542 DebugLoc dl = N->getDebugLoc();
544 SDValue PtrLo, PtrHi;
545 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
547 if (ValueType == MVT::i8) {
548 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
550 DAG.getConstant (0 + StoreOffset, MVT::i8));
552 else if (ValueType == MVT::i16) {
553 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
554 SDValue SrcLo, SrcHi;
555 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
556 SDValue ChainLo = Chain, ChainHi = Chain;
557 // FIXME: This makes unsafe assumptions. The Chain may be a TokenFactor
558 // created for an unrelated purpose, in which case it may not have
559 // exactly two operands. Also, even if it does have two operands, they
560 // may not be the low and high parts of an aligned load that was split.
561 if (Chain.getOpcode() == ISD::TokenFactor) {
562 ChainLo = Chain.getOperand(0);
563 ChainHi = Chain.getOperand(1);
565 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
568 DAG.getConstant (0 + StoreOffset, MVT::i8));
570 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
572 DAG.getConstant (1 + StoreOffset, MVT::i8));
574 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
577 else if (ValueType == MVT::i32) {
578 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
579 SDValue SrcLo, SrcHi;
580 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
582 // Get the expanded parts of each of SrcLo and SrcHi.
583 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
584 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
585 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
587 SDValue ChainLo = Chain, ChainHi = Chain;
588 // FIXME: This makes unsafe assumptions; see the FIXME above.
589 if (Chain.getOpcode() == ISD::TokenFactor) {
590 ChainLo = Chain.getOperand(0);
591 ChainHi = Chain.getOperand(1);
593 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
595 // FIXME: This makes unsafe assumptions; see the FIXME above.
596 if (ChainLo.getOpcode() == ISD::TokenFactor) {
597 ChainLo1 = ChainLo.getOperand(0);
598 ChainLo2 = ChainLo.getOperand(1);
600 // FIXME: This makes unsafe assumptions; see the FIXME above.
601 if (ChainHi.getOpcode() == ISD::TokenFactor) {
602 ChainHi1 = ChainHi.getOperand(0);
603 ChainHi2 = ChainHi.getOperand(1);
605 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
607 SrcLo1, PtrLo, PtrHi,
608 DAG.getConstant (0 + StoreOffset, MVT::i8));
610 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
611 SrcLo2, PtrLo, PtrHi,
612 DAG.getConstant (1 + StoreOffset, MVT::i8));
614 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
615 SrcHi1, PtrLo, PtrHi,
616 DAG.getConstant (2 + StoreOffset, MVT::i8));
618 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
619 SrcHi2, PtrLo, PtrHi,
620 DAG.getConstant (3 + StoreOffset, MVT::i8));
622 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
623 getChain(Store1), getChain(Store2));
624 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
625 getChain(Store3), getChain(Store4));
626 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
628 } else if (ValueType == MVT::i64) {
629 SDValue SrcLo, SrcHi;
630 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
631 SDValue ChainLo = Chain, ChainHi = Chain;
632 // FIXME: This makes unsafe assumptions; see the FIXME above.
633 if (Chain.getOpcode() == ISD::TokenFactor) {
634 ChainLo = Chain.getOperand(0);
635 ChainHi = Chain.getOperand(1);
637 SDValue Store1 = DAG.getStore(ChainLo, dl, SrcLo, Ptr, NULL,
638 0 + StoreOffset, false, false, 0);
640 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
641 DAG.getConstant(4, Ptr.getValueType()));
642 SDValue Store2 = DAG.getStore(ChainHi, dl, SrcHi, Ptr, NULL,
643 1 + StoreOffset, false, false, 0);
645 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1,
648 assert (0 && "value type not supported");
653 SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N,
656 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
657 // FIXME there isn't really debug info here
658 DebugLoc dl = ES->getDebugLoc();
660 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
661 SDValue Offset = DAG.getConstant(0, MVT::i8);
662 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES, Offset);
663 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES, Offset);
665 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
668 // ExpandGlobalAddress -
669 SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N,
670 SelectionDAG &DAG) const {
671 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
672 // FIXME there isn't really debug info here
673 DebugLoc dl = G->getDebugLoc();
675 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
678 SDValue Offset = DAG.getConstant(0, MVT::i8);
679 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA, Offset);
680 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA, Offset);
682 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
685 bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) const {
686 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
688 if (Op.getOpcode() == ISD::BUILD_PAIR) {
689 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
695 // Return true if DirectAddress is in ROM_SPACE
696 bool PIC16TargetLowering::isRomAddress(const SDValue &Op) const {
698 // RomAddress is a GlobalAddress in ROM_SPACE_
699 // If the Op is not a GlobalAddress return NULL without checking
701 if (!isDirectAddress(Op))
704 // Its a GlobalAddress.
705 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
706 SDValue TGA = Op.getOperand(0).getOperand(0);
707 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
709 if (GSDN->getAddressSpace() == PIC16ISD::ROM_SPACE)
712 // Any other address space return it false
717 // GetExpandedParts - This function is on the similiar lines as
718 // the GetExpandedInteger in type legalizer is. This returns expanded
719 // parts of Op in Lo and Hi.
721 void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
722 SDValue &Lo, SDValue &Hi) const {
723 SDNode *N = Op.getNode();
724 DebugLoc dl = N->getDebugLoc();
725 EVT NewVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
727 // Extract the lo component.
728 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
729 DAG.getConstant(0, MVT::i8));
731 // extract the hi component
732 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
733 DAG.getConstant(1, MVT::i8));
736 // Legalize FrameIndex into ExternalSymbol and offset.
738 PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
739 SDValue &ES, int &Offset) const {
741 MachineFunction &MF = DAG.getMachineFunction();
742 const Function *Func = MF.getFunction();
743 MachineFrameInfo *MFI = MF.getFrameInfo();
744 PIC16MachineFunctionInfo *FuncInfo = MF.getInfo<PIC16MachineFunctionInfo>();
745 const std::string Name = Func->getName();
747 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
749 // FrameIndices are not stack offsets. But they represent the request
750 // for space on stack. That space requested may be more than one byte.
751 // Therefore, to calculate the stack offset that a FrameIndex aligns
752 // with, we need to traverse all the FrameIndices available earlier in
753 // the list and add their requested size.
754 unsigned FIndex = FR->getIndex();
756 if (FIndex < FuncInfo->getReservedFrameCount()) {
757 tmpName = ESNames::createESName(PAN::getFrameLabel(Name));
758 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
760 for (unsigned i=0; i<FIndex ; ++i) {
761 Offset += MFI->getObjectSize(i);
764 // FrameIndex has been made for some temporary storage
765 tmpName = ESNames::createESName(PAN::getTempdataLabel(Name));
766 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
767 Offset = GetTmpOffsetForFI(FIndex, MFI->getObjectSize(FIndex), MF);
773 // This function legalizes the PIC16 Addresses. If the Pointer is
774 // -- Direct address variable residing
775 // --> then a Banksel for that variable will be created.
777 // --> then it will be treated as an indirect address.
778 // -- Indirect address
779 // --> then the address will be loaded into FSR
780 // -- ADD with constant operand
781 // --> then constant operand of ADD will be returned as Offset
782 // and non-constant operand of ADD will be treated as pointer.
783 // Returns the high and lo part of the address, and the offset(in case of ADD).
785 void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
786 SDValue &Lo, SDValue &Hi,
787 unsigned &Offset, DebugLoc dl) const {
789 // Offset, by default, should be 0
792 // If the pointer is ADD with constant,
793 // return the constant value as the offset
794 if (Ptr.getOpcode() == ISD::ADD) {
795 SDValue OperLeft = Ptr.getOperand(0);
796 SDValue OperRight = Ptr.getOperand(1);
797 if ((OperLeft.getOpcode() == ISD::Constant) &&
798 (dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue() < 32 )) {
799 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
801 } else if ((OperRight.getOpcode() == ISD::Constant) &&
802 (dyn_cast<ConstantSDNode>(OperRight)->getZExtValue() < 32 )){
803 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
808 // If the pointer is Type i8 and an external symbol
809 // then treat it as direct address.
810 // One example for such case is storing and loading
811 // from function frame during a call
812 if (Ptr.getValueType() == MVT::i8) {
813 switch (Ptr.getOpcode()) {
814 case ISD::TargetExternalSymbol:
816 Hi = DAG.getConstant(1, MVT::i8);
821 // Expansion of FrameIndex has Lo/Hi parts
822 if (isDirectAddress(Ptr)) {
823 SDValue TFI = Ptr.getOperand(0).getOperand(0);
825 if (TFI.getOpcode() == ISD::TargetFrameIndex) {
826 LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset);
827 Hi = DAG.getConstant(1, MVT::i8);
828 Offset += FrameOffset;
830 } else if (TFI.getOpcode() == ISD::TargetExternalSymbol) {
831 // FrameIndex has already been expanded.
832 // Now just make use of its expansion
834 Hi = DAG.getConstant(1, MVT::i8);
835 SDValue FOffset = Ptr.getOperand(0).getOperand(1);
836 assert (FOffset.getOpcode() == ISD::Constant &&
837 "Invalid operand of PIC16ISD::Lo");
838 Offset += dyn_cast<ConstantSDNode>(FOffset)->getZExtValue();
843 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
844 // Direct addressing case for RAM variables. The Hi part is constant
845 // and the Lo part is the TGA itself.
846 Lo = Ptr.getOperand(0).getOperand(0);
848 // For direct addresses Hi is a constant. Value 1 for the constant
849 // signifies that banksel needs to generated for it. Value 0 for
850 // the constant signifies that banksel does not need to be generated
851 // for it. Mark it as 1 now and optimize later.
852 Hi = DAG.getConstant(1, MVT::i8);
856 // Indirect addresses. Get the hi and lo parts of ptr.
857 GetExpandedParts(Ptr, DAG, Lo, Hi);
859 // Put the hi and lo parts into FSR.
860 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
861 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
866 SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) const {
867 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
868 SDValue Chain = LD->getChain();
869 SDValue Ptr = LD->getBasePtr();
870 DebugLoc dl = LD->getDebugLoc();
872 SDValue Load, Offset;
875 SDValue PtrLo, PtrHi;
878 // Legalize direct/indirect addresses. This will give the lo and hi parts
879 // of the address and the offset.
880 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
882 // Load from the pointer (direct address or FSR)
883 VT = N->getValueType(0);
884 unsigned NumLoads = VT.getSizeInBits() / 8;
885 std::vector<SDValue> PICLoads;
887 EVT MemVT = LD->getMemoryVT();
888 if(ISD::isNON_EXTLoad(N)) {
889 for (iter=0; iter<NumLoads ; ++iter) {
890 // Add the pointer offset if any
891 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
892 Tys = DAG.getVTList(MVT::i8, MVT::Other);
893 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
895 PICLoads.push_back(Load);
898 // If it is extended load then use PIC16Load for Memory Bytes
899 // and for all extended bytes perform action based on type of
900 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
903 // For extended loads this is the memory value type
904 // i.e. without any extension
905 EVT MemVT = LD->getMemoryVT();
906 unsigned MemBytes = MemVT.getSizeInBits() / 8;
907 // if MVT::i1 is extended to MVT::i8 then MemBytes will be zero
909 if (MemBytes == 0) MemBytes = 1;
911 unsigned ExtdBytes = VT.getSizeInBits() / 8;
912 Offset = DAG.getConstant(LoadOffset, MVT::i8);
914 Tys = DAG.getVTList(MVT::i8, MVT::Other);
915 // For MemBytes generate PIC16Load with proper offset
916 for (iter=0; iter < MemBytes; ++iter) {
917 // Add the pointer offset if any
918 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
919 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
921 PICLoads.push_back(Load);
924 // For SignExtendedLoad
925 if (ISD::isSEXTLoad(N)) {
926 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
928 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
929 DAG.getConstant(7, MVT::i8));
930 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
931 PICLoads.push_back(SRA);
933 } else if (ISD::isZEXTLoad(N) || ISD::isEXTLoad(N)) {
934 //} else if (ISD::isZEXTLoad(N)) {
935 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
936 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
937 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
938 PICLoads.push_back(ConstZero);
945 // Operand of Load is illegal -- Load itself is legal
948 else if (VT == MVT::i16) {
949 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
950 if ((MemVT == MVT::i8) || (MemVT == MVT::i1))
951 Chain = getChain(PICLoads[0]);
953 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
954 getChain(PICLoads[0]), getChain(PICLoads[1]));
955 } else if (VT == MVT::i32) {
957 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
958 PICLoads[0], PICLoads[1]);
959 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
960 PICLoads[2], PICLoads[3]);
961 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
962 if ((MemVT == MVT::i8) || (MemVT == MVT::i1))
963 Chain = getChain(PICLoads[0]);
964 else if (MemVT == MVT::i16)
965 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
966 getChain(PICLoads[0]), getChain(PICLoads[1]));
969 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
970 getChain(PICLoads[0]), getChain(PICLoads[1]));
971 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
972 getChain(PICLoads[2]), getChain(PICLoads[3]));
973 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
974 Chains[0], Chains[1]);
977 Tys = DAG.getVTList(VT, MVT::Other);
978 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
981 SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
982 // We should have handled larger operands in type legalizer itself.
983 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
985 SDNode *N = Op.getNode();
986 SDValue Value = N->getOperand(0);
987 SDValue Amt = N->getOperand(1);
988 PIC16ISD::PIC16Libcall CallCode;
989 switch (N->getOpcode()) {
991 CallCode = PIC16ISD::SRA_I8;
994 CallCode = PIC16ISD::SLL_I8;
997 CallCode = PIC16ISD::SRL_I8;
1000 assert ( 0 && "This shift is not implemented yet.");
1003 SmallVector<SDValue, 2> Ops(2);
1006 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
1007 true, DAG, N->getDebugLoc());
1011 SDValue PIC16TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
1012 // We should have handled larger operands in type legalizer itself.
1013 assert (Op.getValueType() == MVT::i8 && "illegal multiply to lower");
1015 SDNode *N = Op.getNode();
1016 SmallVector<SDValue, 2> Ops(2);
1017 Ops[0] = N->getOperand(0);
1018 Ops[1] = N->getOperand(1);
1019 SDValue Call = MakePIC16Libcall(PIC16ISD::MUL_I8, N->getValueType(0),
1020 &Ops[0], 2, true, DAG, N->getDebugLoc());
1025 PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
1026 SmallVectorImpl<SDValue>&Results,
1027 SelectionDAG &DAG) const {
1028 SDValue Op = SDValue(N, 0);
1031 switch (Op.getOpcode()) {
1033 Res = ExpandLoad(Op.getNode(), DAG); break;
1035 // All other operations are handled in LowerOperation.
1036 Res = LowerOperation(Op, DAG);
1038 Results.push_back(Res);
1045 unsigned NumValues = N->getNumValues();
1046 for (i = 0; i < NumValues ; i++) {
1047 Results.push_back(SDValue(N, i));
1051 SDValue PIC16TargetLowering::LowerOperation(SDValue Op,
1052 SelectionDAG &DAG) const {
1053 switch (Op.getOpcode()) {
1057 return LowerADD(Op, DAG);
1061 return LowerSUB(Op, DAG);
1063 return ExpandLoad(Op.getNode(), DAG);
1065 return ExpandStore(Op.getNode(), DAG);
1067 return LowerMUL(Op, DAG);
1071 return LowerShift(Op, DAG);
1075 return LowerBinOp(Op, DAG);
1077 return LowerBR_CC(Op, DAG);
1078 case ISD::SELECT_CC:
1079 return LowerSELECT_CC(Op, DAG);
1084 SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
1086 DebugLoc dl) const {
1087 assert (Op.getValueType() == MVT::i8
1088 && "illegal value type to store on stack.");
1090 MachineFunction &MF = DAG.getMachineFunction();
1091 const Function *Func = MF.getFunction();
1092 const std::string FuncName = Func->getName();
1095 // Put the value on stack.
1096 // Get a stack slot index and convert to es.
1097 int FI = MF.getFrameInfo()->CreateStackObject(1, 1, false);
1098 const char *tmpName = ESNames::createESName(PAN::getTempdataLabel(FuncName));
1099 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1101 // Store the value to ES.
1102 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
1105 DAG.getConstant (1, MVT::i8), // Banksel.
1106 DAG.getConstant (GetTmpOffsetForFI(FI, 1, MF),
1109 // Load the value from ES.
1110 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
1111 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
1112 ES, DAG.getConstant (1, MVT::i8),
1113 DAG.getConstant (GetTmpOffsetForFI(FI, 1, MF),
1116 return Load.getValue(0);
1119 SDValue PIC16TargetLowering::
1120 LowerIndirectCallArguments(SDValue Chain, SDValue InFlag,
1121 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1122 const SmallVectorImpl<ISD::OutputArg> &Outs,
1123 const SmallVectorImpl<ISD::InputArg> &Ins,
1124 DebugLoc dl, SelectionDAG &DAG) const {
1125 unsigned NumOps = Outs.size();
1127 // If call has no arguments then do nothing and return.
1131 std::vector<SDValue> Ops;
1132 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1133 SDValue Arg, StoreRet;
1135 // For PIC16 ABI the arguments come after the return value.
1136 unsigned RetVals = Ins.size();
1137 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
1138 // Get the arguments
1142 Ops.push_back(Chain);
1144 Ops.push_back(DataAddr_Lo);
1145 Ops.push_back(DataAddr_Hi);
1146 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
1147 Ops.push_back(InFlag);
1149 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
1151 Chain = getChain(StoreRet);
1152 InFlag = getOutFlag(StoreRet);
1158 SDValue PIC16TargetLowering::
1159 LowerDirectCallArguments(SDValue ArgLabel, SDValue Chain, SDValue InFlag,
1160 const SmallVectorImpl<ISD::OutputArg> &Outs,
1161 DebugLoc dl, SelectionDAG &DAG) const {
1162 unsigned NumOps = Outs.size();
1164 SDValue Arg, StoreAt;
1168 // If call has no arguments then do nothing and return.
1172 // FIXME: This portion of code currently assumes only
1173 // primitive types being passed as arguments.
1175 // Legalize the address before use
1176 SDValue PtrLo, PtrHi;
1177 unsigned AddressOffset;
1178 int StoreOffset = 0;
1179 LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl);
1182 std::vector<SDValue> Ops;
1183 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1184 for (unsigned i=0, Offset = 0; i<NumOps; i++) {
1187 StoreOffset = (Offset + AddressOffset);
1189 // Store the argument on frame
1192 Ops.push_back(Chain);
1194 Ops.push_back(PtrLo);
1195 Ops.push_back(PtrHi);
1196 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
1197 Ops.push_back(InFlag);
1199 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
1201 Chain = getChain(StoreRet);
1202 InFlag = getOutFlag(StoreRet);
1204 // Update the frame offset to be used for next argument
1205 ArgVT = Arg.getValueType();
1206 Size = ArgVT.getSizeInBits();
1207 Size = Size/8; // Calculate size in bytes
1208 Offset += Size; // Increase the frame offset
1213 SDValue PIC16TargetLowering::
1214 LowerIndirectCallReturn(SDValue Chain, SDValue InFlag,
1215 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1216 const SmallVectorImpl<ISD::InputArg> &Ins,
1217 DebugLoc dl, SelectionDAG &DAG,
1218 SmallVectorImpl<SDValue> &InVals) const {
1219 unsigned RetVals = Ins.size();
1221 // If call does not have anything to return
1222 // then do nothing and go back.
1226 // Call has something to return
1229 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1230 for(unsigned i=0;i<RetVals;i++) {
1231 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
1232 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
1234 InFlag = getOutFlag(LoadRet);
1235 Chain = getChain(LoadRet);
1236 InVals.push_back(LoadRet);
1241 SDValue PIC16TargetLowering::
1242 LowerDirectCallReturn(SDValue RetLabel, SDValue Chain, SDValue InFlag,
1243 const SmallVectorImpl<ISD::InputArg> &Ins,
1244 DebugLoc dl, SelectionDAG &DAG,
1245 SmallVectorImpl<SDValue> &InVals) const {
1247 // Currently handling primitive types only. They will come in
1249 unsigned RetVals = Ins.size();
1251 // Return immediately if the return type is void
1255 // Call has something to return
1257 // Legalize the address before use
1260 LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl);
1262 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1265 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1267 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
1268 DAG.getConstant(LdOffset + Offset, MVT::i8),
1271 InFlag = getOutFlag(LoadRet);
1273 Chain = getChain(LoadRet);
1275 InVals.push_back(LoadRet);
1282 PIC16TargetLowering::LowerReturn(SDValue Chain,
1283 CallingConv::ID CallConv, bool isVarArg,
1284 const SmallVectorImpl<ISD::OutputArg> &Outs,
1285 DebugLoc dl, SelectionDAG &DAG) const {
1287 // Number of values to return
1288 unsigned NumRet = Outs.size();
1290 // Function returns value always on stack with the offset starting
1292 MachineFunction &MF = DAG.getMachineFunction();
1293 const Function *F = MF.getFunction();
1294 std::string FuncName = F->getName();
1296 const char *tmpName = ESNames::createESName(PAN::getFrameLabel(FuncName));
1297 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1298 SDValue BS = DAG.getConstant(1, MVT::i8);
1300 for(unsigned i=0;i<NumRet; ++i) {
1301 RetVal = Outs[i].Val;
1302 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1304 DAG.getConstant (i, MVT::i8));
1307 return DAG.getNode(PIC16ISD::RET, dl, MVT::Other, Chain);
1310 void PIC16TargetLowering::
1311 GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1312 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1313 SelectionDAG &DAG) const {
1314 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1315 && "Don't know what to do of such callee!!");
1316 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1317 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1318 Chain = getChain(SeqStart);
1319 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1321 // Get the Lo and Hi part of code address
1322 SDValue Lo = Callee.getOperand(0);
1323 SDValue Hi = Callee.getOperand(1);
1325 SDValue Data_Lo, Data_Hi;
1326 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1327 // Subtract 2 from Address to get the Lower part of DataAddress.
1328 SDVTList VTList = DAG.getVTList(MVT::i8, MVT::Flag);
1329 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1330 DAG.getConstant(2, MVT::i8));
1331 SDValue Ops[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1332 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, Ops, 3);
1333 SDValue PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1334 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
1335 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1337 Chain = getChain(Call);
1338 OperFlag = getOutFlag(Call);
1339 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1341 Chain = getChain(SeqEnd);
1342 OperFlag = getOutFlag(SeqEnd);
1344 // Low part of Data Address
1345 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1347 // Make the second call.
1348 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1349 Chain = getChain(SeqStart);
1350 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1352 // Subtract 1 from Address to get high part of data address.
1353 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1354 DAG.getConstant(1, MVT::i8));
1355 SDValue HiOps[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1356 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1357 PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1359 // Use new Lo to make another CALLW
1360 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
1361 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1362 Chain = getChain(Call);
1363 OperFlag = getOutFlag(Call);
1364 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1366 Chain = getChain(SeqEnd);
1367 OperFlag = getOutFlag(SeqEnd);
1368 // Hi part of Data Address
1369 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1373 PIC16TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
1374 CallingConv::ID CallConv, bool isVarArg,
1376 const SmallVectorImpl<ISD::OutputArg> &Outs,
1377 const SmallVectorImpl<ISD::InputArg> &Ins,
1378 DebugLoc dl, SelectionDAG &DAG,
1379 SmallVectorImpl<SDValue> &InVals) const {
1380 // PIC16 target does not yet support tail call optimization.
1383 assert(Callee.getValueType() == MVT::i16 &&
1384 "Don't know how to legalize this call node!!!");
1386 // The flag to track if this is a direct or indirect call.
1387 bool IsDirectCall = true;
1388 unsigned RetVals = Ins.size();
1389 unsigned NumArgs = Outs.size();
1391 SDValue DataAddr_Lo, DataAddr_Hi;
1392 if (!isa<GlobalAddressSDNode>(Callee) &&
1393 !isa<ExternalSymbolSDNode>(Callee)) {
1394 IsDirectCall = false; // This is indirect call
1396 // If this is an indirect call then to pass the arguments
1397 // and read the return value back, we need the data address
1398 // of the function being called.
1399 // To get the data address two more calls need to be made.
1401 // Come here for indirect calls
1403 // Indirect addresses. Get the hi and lo parts of ptr.
1404 GetExpandedParts(Callee, DAG, Lo, Hi);
1405 // Connect Lo and Hi parts of the callee with the PIC16Connect
1406 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1408 // Read DataAddress only if we have to pass arguments or
1409 // read return value.
1410 if ((RetVals > 0) || (NumArgs > 0))
1411 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1414 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1416 // Start the call sequence.
1417 // Carring the Constant 0 along the CALLSEQSTART
1418 // because there is nothing else to carry.
1419 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1420 Chain = getChain(SeqStart);
1421 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1424 // For any direct call - callee will be GlobalAddressNode or
1426 SDValue ArgLabel, RetLabel;
1428 // Considering the GlobalAddressNode case here.
1429 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1430 const GlobalValue *GV = G->getGlobal();
1431 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1432 Name = G->getGlobal()->getName();
1433 } else {// Considering the ExternalSymbol case here
1434 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1435 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1436 Name = ES->getSymbol();
1439 // Label for argument passing
1440 const char *argFrame = ESNames::createESName(PAN::getArgsLabel(Name));
1441 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
1443 // Label for reading return value
1444 const char *retName = ESNames::createESName(PAN::getRetvalLabel(Name));
1445 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1448 SDValue CodeAddr_Lo = Callee.getOperand(0);
1449 SDValue CodeAddr_Hi = Callee.getOperand(1);
1451 /*CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1452 DAG.getConstant(2, MVT::i8));*/
1454 // move Hi part in PCLATH
1455 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1456 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1460 // Pass the argument to function before making the call.
1463 CallArgs = LowerDirectCallArguments(ArgLabel, Chain, OperFlag,
1465 Chain = getChain(CallArgs);
1466 OperFlag = getOutFlag(CallArgs);
1468 CallArgs = LowerIndirectCallArguments(Chain, OperFlag, DataAddr_Lo,
1469 DataAddr_Hi, Outs, Ins, dl, DAG);
1470 Chain = getChain(CallArgs);
1471 OperFlag = getOutFlag(CallArgs);
1474 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1475 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
1477 Chain = getChain(PICCall);
1478 OperFlag = getOutFlag(PICCall);
1481 // Carrying the Constant 0 along the CALLSEQSTART
1482 // because there is nothing else to carry.
1483 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1485 Chain = getChain(SeqEnd);
1486 OperFlag = getOutFlag(SeqEnd);
1488 // Lower the return value reading after the call.
1490 return LowerDirectCallReturn(RetLabel, Chain, OperFlag,
1491 Ins, dl, DAG, InVals);
1493 return LowerIndirectCallReturn(Chain, OperFlag, DataAddr_Lo,
1494 DataAddr_Hi, Ins, dl, DAG, InVals);
1497 bool PIC16TargetLowering::isDirectLoad(const SDValue Op) const {
1498 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1499 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1500 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1505 // NeedToConvertToMemOp - Returns true if one of the operands of the
1506 // operation 'Op' needs to be put into memory. Also returns the
1507 // operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1508 // no instruction that can operation on two registers. Most insns take
1509 // one register and one memory operand (addwf) / Constant (addlw).
1510 bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp,
1511 SelectionDAG &DAG) const {
1512 // If one of the operand is a constant, return false.
1513 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1514 Op.getOperand(1).getOpcode() == ISD::Constant)
1517 // Return false if one of the operands is already a direct
1518 // load and that operand has only one use.
1519 if (isDirectLoad(Op.getOperand(0))) {
1520 if (Op.getOperand(0).hasOneUse()) {
1521 // Legal and profitable folding check uses the NodeId of DAG nodes.
1522 // This NodeId is assigned by topological order. Therefore first
1523 // assign topological order then perform legal and profitable check.
1524 // Note:- Though this ordering is done before begining with legalization,
1525 // newly added node during legalization process have NodeId=-1 (NewNode)
1526 // therefore before performing any check proper ordering of the node is
1528 DAG.AssignTopologicalOrder();
1530 // Direct load operands are folded in binary operations. But before folding
1531 // verify if this folding is legal. Fold only if it is legal otherwise
1532 // convert this direct load to a separate memory operation.
1533 if (SelectionDAGISel::IsLegalToFold(Op.getOperand(0),
1534 Op.getNode(), Op.getNode(),
1535 CodeGenOpt::Default))
1542 // For operations that are non-cummutative there is no need to check
1543 // for right operand because folding right operand may result in
1544 // incorrect operation.
1545 if (! SelectionDAG::isCommutativeBinOp(Op.getOpcode()))
1548 if (isDirectLoad(Op.getOperand(1))) {
1549 if (Op.getOperand(1).hasOneUse()) {
1550 // Legal and profitable folding check uses the NodeId of DAG nodes.
1551 // This NodeId is assigned by topological order. Therefore first
1552 // assign topological order then perform legal and profitable check.
1553 // Note:- Though this ordering is done before begining with legalization,
1554 // newly added node during legalization process have NodeId=-1 (NewNode)
1555 // therefore before performing any check proper ordering of the node is
1557 DAG.AssignTopologicalOrder();
1559 // Direct load operands are folded in binary operations. But before folding
1560 // verify if this folding is legal. Fold only if it is legal otherwise
1561 // convert this direct load to a separate memory operation.
1562 if (SelectionDAGISel::IsLegalToFold(Op.getOperand(1),
1563 Op.getNode(), Op.getNode(),
1564 CodeGenOpt::Default))
1573 // LowerBinOp - Lower a commutative binary operation that does not
1574 // affect status flag carry.
1575 SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) const {
1576 DebugLoc dl = Op.getDebugLoc();
1578 // We should have handled larger operands in type legalizer itself.
1579 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
1582 if (NeedToConvertToMemOp(Op, MemOp, DAG)) {
1583 // Put one value on stack.
1584 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
1586 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1594 // LowerADD - Lower all types of ADD operations including the ones
1595 // that affects carry.
1596 SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
1597 // We should have handled larger operands in type legalizer itself.
1598 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
1599 DebugLoc dl = Op.getDebugLoc();
1601 if (NeedToConvertToMemOp(Op, MemOp, DAG)) {
1602 // Put one value on stack.
1603 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
1605 // ADDC and ADDE produce two results.
1606 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
1608 // ADDE has three operands, the last one is the carry bit.
1609 if (Op.getOpcode() == ISD::ADDE)
1610 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1611 NewVal, Op.getOperand(2));
1612 // ADDC has two operands.
1613 else if (Op.getOpcode() == ISD::ADDC)
1614 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1616 // ADD it is. It produces only one result.
1618 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1625 SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
1626 DebugLoc dl = Op.getDebugLoc();
1627 // We should have handled larger operands in type legalizer itself.
1628 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
1630 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
1632 // Since we don't have an instruction for X - c ,
1633 // we can change it to X + (-c)
1634 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
1635 if (C && (Op.getOpcode() == ISD::SUB))
1637 return DAG.getNode(ISD::ADD,
1638 dl, MVT::i8, Op.getOperand(0),
1639 DAG.getConstant(0-(C->getZExtValue()), MVT::i8));
1642 if (NeedToConvertToMemOp(Op, MemOp, DAG) ||
1643 (isDirectLoad(Op.getOperand(1)) &&
1644 (!isDirectLoad(Op.getOperand(0))) &&
1645 (Op.getOperand(0).getOpcode() != ISD::Constant)))
1647 // Put first operand on stack.
1648 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
1650 switch (Op.getOpcode()) {
1652 assert (0 && "Opcode unknown.");
1654 return DAG.getNode(Op.getOpcode(),
1655 dl, Tys, NewVal, Op.getOperand(1),
1659 return DAG.getNode(Op.getOpcode(),
1660 dl, Tys, NewVal, Op.getOperand(1));
1663 return DAG.getNode(Op.getOpcode(),
1664 dl, MVT::i8, NewVal, Op.getOperand(1));
1672 void PIC16TargetLowering::InitReservedFrameCount(const Function *F,
1673 SelectionDAG &DAG) const {
1674 MachineFunction &MF = DAG.getMachineFunction();
1675 PIC16MachineFunctionInfo *FuncInfo = MF.getInfo<PIC16MachineFunctionInfo>();
1677 unsigned NumArgs = F->arg_size();
1679 bool isVoidFunc = (F->getReturnType()->getTypeID() == Type::VoidTyID);
1682 FuncInfo->setReservedFrameCount(NumArgs);
1684 FuncInfo->setReservedFrameCount(NumArgs + 1);
1687 // LowerFormalArguments - Argument values are loaded from the
1688 // <fname>.args + offset. All arguments are already broken to leaglized
1689 // types, so the offset just runs from 0 to NumArgVals - 1.
1692 PIC16TargetLowering::LowerFormalArguments(SDValue Chain,
1693 CallingConv::ID CallConv,
1695 const SmallVectorImpl<ISD::InputArg> &Ins,
1698 SmallVectorImpl<SDValue> &InVals)
1700 unsigned NumArgVals = Ins.size();
1702 // Get the callee's name to create the <fname>.args label to pass args.
1703 MachineFunction &MF = DAG.getMachineFunction();
1704 const Function *F = MF.getFunction();
1705 std::string FuncName = F->getName();
1707 // Reset the map of FI and TmpOffset
1708 ResetTmpOffsetMap(DAG);
1709 // Initialize the ReserveFrameCount
1710 InitReservedFrameCount(F, DAG);
1712 // Create the <fname>.args external symbol.
1713 const char *tmpName = ESNames::createESName(PAN::getArgsLabel(FuncName));
1714 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1716 // Load arg values from the label + offset.
1717 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1718 SDValue BS = DAG.getConstant(1, MVT::i8);
1719 for (unsigned i = 0; i < NumArgVals ; ++i) {
1720 SDValue Offset = DAG.getConstant(i, MVT::i8);
1721 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
1723 Chain = getChain(PICLoad);
1724 InVals.push_back(PICLoad);
1730 // Perform DAGCombine of PIC16Load.
1731 // FIXME - Need a more elaborate comment here.
1732 SDValue PIC16TargetLowering::
1733 PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1734 SelectionDAG &DAG = DCI.DAG;
1735 SDValue Chain = N->getOperand(0);
1736 if (N->hasNUsesOfValue(0, 0)) {
1737 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1742 // For all the functions with arguments some STORE nodes are generated
1743 // that store the argument on the frameindex. However in PIC16 the arguments
1744 // are passed on stack only. Therefore these STORE nodes are redundant.
1745 // To remove these STORE nodes will be removed in PerformStoreCombine
1747 // Currently this function is doint nothing and will be updated for removing
1748 // unwanted store operations
1749 SDValue PIC16TargetLowering::
1750 PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1751 return SDValue(N, 0);
1753 // Storing an undef value is of no use, so remove it
1754 if (isStoringUndef(N, Chain, DAG)) {
1755 return Chain; // remove the store and return the chain
1757 //else everything is ok.
1758 return SDValue(N, 0);
1762 SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1763 DAGCombinerInfo &DCI) const {
1764 switch (N->getOpcode()) {
1766 return PerformStoreCombine(N, DCI);
1767 case PIC16ISD::PIC16Load:
1768 return PerformPIC16LoadCombine(N, DCI);
1773 static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1775 default: llvm_unreachable("Unknown condition code!");
1776 case ISD::SETNE: return PIC16CC::NE;
1777 case ISD::SETEQ: return PIC16CC::EQ;
1778 case ISD::SETGT: return PIC16CC::GT;
1779 case ISD::SETGE: return PIC16CC::GE;
1780 case ISD::SETLT: return PIC16CC::LT;
1781 case ISD::SETLE: return PIC16CC::LE;
1782 case ISD::SETULT: return PIC16CC::ULT;
1783 case ISD::SETULE: return PIC16CC::ULE;
1784 case ISD::SETUGE: return PIC16CC::UGE;
1785 case ISD::SETUGT: return PIC16CC::UGT;
1789 // Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1790 // set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1791 static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1792 ISD::CondCode CC, unsigned &SPCC) {
1793 if (isa<ConstantSDNode>(RHS) &&
1794 cast<ConstantSDNode>(RHS)->isNullValue() &&
1796 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1797 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1798 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1799 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1800 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
1801 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
1802 SDValue CMPCC = LHS.getOperand(3);
1803 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1804 LHS = CMPCC.getOperand(0);
1805 RHS = CMPCC.getOperand(1);
1809 // Returns appropriate CMP insn and corresponding condition code in PIC16CC
1810 SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1811 unsigned CC, SDValue &PIC16CC,
1812 SelectionDAG &DAG, DebugLoc dl) const {
1813 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1815 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1816 // i.e. a < 12 can be rewritten as 12 > a.
1817 if (RHS.getOpcode() == ISD::Constant) {
1826 CondCode = PIC16CC::GT;
1829 CondCode = PIC16CC::LT;
1832 CondCode = PIC16CC::UGT;
1835 CondCode = PIC16CC::ULT;
1838 CondCode = PIC16CC::LE;
1841 CondCode = PIC16CC::GE;
1844 CondCode = PIC16CC::UGE;
1847 CondCode = PIC16CC::ULE;
1852 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
1854 // These are signed comparisons.
1855 SDValue Mask = DAG.getConstant(128, MVT::i8);
1856 if (isSignedComparison(CondCode)) {
1857 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1858 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
1861 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
1862 // We can use a subtract operation to set the condition codes. But
1863 // we need to put one operand in memory if required.
1864 // Nothing to do if the first operand is already a valid type (direct load
1865 // for subwf and literal for sublw) and it is used by this operation only.
1866 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1868 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
1870 // else convert the first operand to mem.
1871 LHS = ConvertToMemOperand (LHS, DAG, dl);
1872 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
1876 SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op,
1877 SelectionDAG &DAG) const {
1878 SDValue LHS = Op.getOperand(0);
1879 SDValue RHS = Op.getOperand(1);
1880 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1881 SDValue TrueVal = Op.getOperand(2);
1882 SDValue FalseVal = Op.getOperand(3);
1883 unsigned ORIGCC = ~0;
1884 DebugLoc dl = Op.getDebugLoc();
1886 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1887 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1889 // A setcc: lhs, rhs, cc is expanded by llvm to
1890 // select_cc: result of setcc, 0, 1, 0, setne
1891 // We can think of it as:
1892 // select_cc: lhs, rhs, 1, 0, cc
1893 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1894 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1897 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
1899 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
1900 FalseVal, PIC16CC, Cmp.getValue(1));
1904 PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1905 MachineBasicBlock *BB) const {
1906 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1907 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
1908 DebugLoc dl = MI->getDebugLoc();
1910 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1911 // control-flow pattern. The incoming instruction knows the destination vreg
1912 // to set, the condition code register to branch on, the true/false values to
1913 // select between, and a branch opcode to use.
1914 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1915 MachineFunction::iterator It = BB;
1922 // fallthrough --> copy0MBB
1923 MachineBasicBlock *thisMBB = BB;
1924 MachineFunction *F = BB->getParent();
1925 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1926 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1927 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
1928 F->insert(It, copy0MBB);
1929 F->insert(It, sinkMBB);
1931 // Transfer the remainder of BB and its successor edges to sinkMBB.
1932 sinkMBB->splice(sinkMBB->begin(), BB,
1933 llvm::next(MachineBasicBlock::iterator(MI)),
1935 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1937 // Next, add the true and fallthrough blocks as its successors.
1938 BB->addSuccessor(copy0MBB);
1939 BB->addSuccessor(sinkMBB);
1942 // %FalseValue = ...
1943 // # fallthrough to sinkMBB
1946 // Update machine-CFG edges
1947 BB->addSuccessor(sinkMBB);
1950 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1953 BuildMI(*BB, BB->begin(), dl,
1954 TII.get(PIC16::PHI), MI->getOperand(0).getReg())
1955 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1956 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1958 MI->eraseFromParent(); // The pseudo instruction is gone now.
1963 SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
1964 SDValue Chain = Op.getOperand(0);
1965 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1966 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1967 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1968 SDValue Dest = Op.getOperand(4); // BB to jump to
1969 unsigned ORIGCC = ~0;
1970 DebugLoc dl = Op.getDebugLoc();
1972 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1973 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1974 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1975 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1977 // Get the Compare insn and condition code.
1979 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
1981 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,