blob: 1f7b0614589fb98db0853cb410e6004dd1ffb694 [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)
Sanjiv Guptacae1b622009-04-06 10:54:50 +000034 : TargetLowering(TM), TmpSize(0) {
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
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000140 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Custom);
141
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000142 // Now deduce the information based on the above mentioned
143 // actions
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000144 computeRegisterProperties();
145}
146
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000147// getOutFlag - Extract the flag result if the Op has it.
148static SDValue getOutFlag(SDValue &Op) {
149 // Flag is the last value of the node.
150 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
151
152 assert (Flag.getValueType() == MVT::Flag
153 && "Node does not have an out Flag");
154
155 return Flag;
156}
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000157// Get the TmpOffset for FrameIndex
158unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI) {
159 std::map<unsigned, unsigned>::iterator
160 MapIt = FiTmpOffsetMap.find(FI);
161 if (MapIt != FiTmpOffsetMap.end())
162 return MapIt->second;
163
164 // This FI (FrameIndex) is not yet mapped, so map it
165 FiTmpOffsetMap[FI] = TmpSize;
166 return TmpSize++;
167}
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000168
169// To extract chain value from the SDValue Nodes
170// This function will help to maintain the chain extracting
171// code at one place. In case of any change in future it will
172// help maintain the code.
173static SDValue getChain(SDValue &Op) {
174 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
175
176 // If the last value returned in Flag then the chain is
177 // second last value returned.
178 if (Chain.getValueType() == MVT::Flag)
179 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
180
181 // All nodes may not produce a chain. Therefore following assert
182 // verifies that the node is returning a chain only.
183 assert (Chain.getValueType() == MVT::Other
184 && "Node does not have a chain");
185
186 return Chain;
187}
188
189/// PopulateResults - Helper function to LowerOperation.
190/// If a node wants to return multiple results after lowering,
191/// it stuffs them into an array of SDValue called Results.
192
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000193static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
194 if (N.getOpcode() == ISD::MERGE_VALUES) {
195 int NumResults = N.getNumOperands();
196 for( int i = 0; i < NumResults; i++)
197 Results.push_back(N.getOperand(i));
198 }
199 else
200 Results.push_back(N);
201}
202
203MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
204 return MVT::i8;
205}
206
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000207/// The type legalizer framework of generating legalizer can generate libcalls
208/// only when the operand/result types are illegal.
209/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
210/// For example an arithmetic right shift. These functions are used to lower
211/// such operations that generate libcall for legal types.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000212
213void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000214PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000215 const char *Name) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000216 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000217}
218
219const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000220PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000221 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000222}
223
224SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000225PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000226 MVT RetVT, const SDValue *Ops,
227 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000228 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000229
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000230 TargetLowering::ArgListTy Args;
231 Args.reserve(NumOps);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000232
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000233 TargetLowering::ArgListEntry Entry;
234 for (unsigned i = 0; i != NumOps; ++i) {
235 Entry.Node = Ops[i];
236 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
237 Entry.isSExt = isSigned;
238 Entry.isZExt = !isSigned;
239 Args.push_back(Entry);
240 }
241 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000242
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000243 const Type *RetTy = RetVT.getTypeForMVT();
244 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000245 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000246 false, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000247
248 return CallInfo.first;
249}
250
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000251const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
252 switch (Opcode) {
253 default: return NULL;
254 case PIC16ISD::Lo: return "PIC16ISD::Lo";
255 case PIC16ISD::Hi: return "PIC16ISD::Hi";
256 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
257 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000258 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
259 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000260 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
261 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000262 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000263 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000264 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000265 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000266 case PIC16ISD::BCF: return "PIC16ISD::BCF";
267 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
268 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
269 case PIC16ISD::RLF: return "PIC16ISD::RLF";
270 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000271 case PIC16ISD::CALL: return "PIC16ISD::CALL";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000272 case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000273 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
274 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
275 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000276 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000277 case PIC16ISD::PIC16StopPoint: return "PIC16ISD::PIC16StopPoint";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000278 }
279}
280
Duncan Sands1607f052008-12-01 11:39:25 +0000281void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
282 SmallVectorImpl<SDValue>&Results,
283 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000284
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000285 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000286 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000287 Results.push_back(ExpandGlobalAddress(N, DAG));
288 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000289 case ISD::ExternalSymbol:
290 Results.push_back(ExpandExternalSymbol(N, DAG));
291 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000292 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000293 Results.push_back(ExpandStore(N, DAG));
294 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000295 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000296 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000297 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000298 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000299 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000300 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000301 case ISD::FrameIndex:
302 Results.push_back(ExpandFrameIndex(N, DAG));
303 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000304 default:
305 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000306 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000307 }
308}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000309
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000310SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
311
312 // Currently handling FrameIndex of size MVT::i16 only
313 // One example of this scenario is when return value is written on
314 // FrameIndex#0
315
316 if (N->getValueType(0) != MVT::i16)
317 return SDValue();
318
319 // Expand the FrameIndex into ExternalSymbol and a Constant node
320 // The constant will represent the frame index number
321 // Get the current function frame
322 MachineFunction &MF = DAG.getMachineFunction();
323 const Function *Func = MF.getFunction();
324 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000325
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000326 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000327 // FIXME there isn't really debug info here
328 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000329 int Index = FR->getIndex();
330
331 SDValue FI[2];
332 FI[0] = DAG.getTargetFrameIndex(Index, MVT::i8);
333 FI[1] = DAG.getTargetFrameIndex(Index + 1, MVT::i8);
Dale Johannesende064702009-02-06 21:50:26 +0000334 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), FI[0], FI[1]);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000335}
336
337
338SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000339 StoreSDNode *St = cast<StoreSDNode>(N);
340 SDValue Chain = St->getChain();
341 SDValue Src = St->getValue();
342 SDValue Ptr = St->getBasePtr();
343 MVT ValueType = Src.getValueType();
344 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000345 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000346
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000347 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000348 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000349
350 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000351 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000352 PtrLo, PtrHi,
353 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000354 }
355 else if (ValueType == MVT::i16) {
356 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
357 SDValue SrcLo, SrcHi;
358 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
359 SDValue ChainLo = Chain, ChainHi = Chain;
360 if (Chain.getOpcode() == ISD::TokenFactor) {
361 ChainLo = Chain.getOperand(0);
362 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000363 }
Dale Johannesende064702009-02-06 21:50:26 +0000364 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000365 ChainLo,
366 SrcLo, PtrLo, PtrHi,
367 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000368
Dale Johannesende064702009-02-06 21:50:26 +0000369 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000370 SrcHi, PtrLo, PtrHi,
371 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000372
Dale Johannesende064702009-02-06 21:50:26 +0000373 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000374 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000375 }
376 else if (ValueType == MVT::i32) {
377 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
378 SDValue SrcLo, SrcHi;
379 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000380
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000381 // Get the expanded parts of each of SrcLo and SrcHi.
382 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
383 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
384 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000385
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000386 SDValue ChainLo = Chain, ChainHi = Chain;
387 if (Chain.getOpcode() == ISD::TokenFactor) {
388 ChainLo = Chain.getOperand(0);
389 ChainHi = Chain.getOperand(1);
390 }
391 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
392 ChainHi2 = ChainHi;
393 if (ChainLo.getOpcode() == ISD::TokenFactor) {
394 ChainLo1 = ChainLo.getOperand(0);
395 ChainLo2 = ChainLo.getOperand(1);
396 }
397 if (ChainHi.getOpcode() == ISD::TokenFactor) {
398 ChainHi1 = ChainHi.getOperand(0);
399 ChainHi2 = ChainHi.getOperand(1);
400 }
Dale Johannesende064702009-02-06 21:50:26 +0000401 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000402 ChainLo1,
403 SrcLo1, PtrLo, PtrHi,
404 DAG.getConstant (0 + StoreOffset, MVT::i8));
405
Dale Johannesende064702009-02-06 21:50:26 +0000406 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000407 SrcLo2, PtrLo, PtrHi,
408 DAG.getConstant (1 + StoreOffset, MVT::i8));
409
Dale Johannesende064702009-02-06 21:50:26 +0000410 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000411 SrcHi1, PtrLo, PtrHi,
412 DAG.getConstant (2 + StoreOffset, MVT::i8));
413
Dale Johannesende064702009-02-06 21:50:26 +0000414 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000415 SrcHi2, PtrLo, PtrHi,
416 DAG.getConstant (3 + StoreOffset, MVT::i8));
417
Dale Johannesende064702009-02-06 21:50:26 +0000418 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
419 getChain(Store1), getChain(Store2));
420 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
421 getChain(Store3), getChain(Store4));
422 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000423
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000424 }
425 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000426 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000427 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000428 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000429}
430
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000431SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
432{
433 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000434 // FIXME there isn't really debug info here
435 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000436
437 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
438
Dale Johannesende064702009-02-06 21:50:26 +0000439 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES);
440 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000441
Dale Johannesende064702009-02-06 21:50:26 +0000442 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000443}
444
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000445// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000446SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000447 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000448 // FIXME there isn't really debug info here
449 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000450
451 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
452 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000453
Dale Johannesende064702009-02-06 21:50:26 +0000454 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA);
455 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000456
Dale Johannesende064702009-02-06 21:50:26 +0000457 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000458}
459
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000460bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
461 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000462
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000463 if (Op.getOpcode() == ISD::BUILD_PAIR) {
464 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
465 return true;
466 }
467 return false;
468}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000469
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000470// Return true if DirectAddress is in ROM_SPACE
471bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000472
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000473 // RomAddress is a GlobalAddress in ROM_SPACE_
474 // If the Op is not a GlobalAddress return NULL without checking
475 // anything further.
476 if (!isDirectAddress(Op))
477 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000478
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000479 // Its a GlobalAddress.
480 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
481 SDValue TGA = Op.getOperand(0).getOperand(0);
482 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
483 const Type *ValueType = GSDN->getGlobal()->getType();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000484
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000485 if (!isa<PointerType>(ValueType)) {
486 assert(0 && "TGA must be of a PointerType");
487 }
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000488
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000489 int AddrSpace = dyn_cast<PointerType>(ValueType)->getAddressSpace();
490 if (AddrSpace == PIC16ISD::ROM_SPACE)
491 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000492
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000493 // Any other address space return it false
494 return false;
495}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000496
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000497
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000498// GetExpandedParts - This function is on the similiar lines as
499// the GetExpandedInteger in type legalizer is. This returns expanded
500// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000501
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000502void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
503 SDValue &Lo, SDValue &Hi) {
504 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000505 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000506 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000507
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000508 // Extract the lo component.
509 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
510 DAG.getConstant(0, MVT::i8));
511
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000512 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000513 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
514 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000515}
516
517// Legalize FrameIndex into ExternalSymbol and offset.
518void
519PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
520 SDValue &ES, int &Offset) {
521
522 MachineFunction &MF = DAG.getMachineFunction();
523 const Function *Func = MF.getFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000524 MachineFrameInfo *MFI = MF.getFrameInfo();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000525 const std::string Name = Func->getName();
526
527 char *tmpName = new char [strlen(Name.c_str()) + 8];
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000528 sprintf(tmpName, "%s.frame", Name.c_str());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000529 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
530 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000531
532 // FrameIndices are not stack offsets. But they represent the request
533 // for space on stack. That space requested may be more than one byte.
534 // Therefore, to calculate the stack offset that a FrameIndex aligns
535 // with, we need to traverse all the FrameIndices available earlier in
536 // the list and add their requested size.
537 unsigned FIndex = FR->getIndex();
538 Offset = 0;
539 for (unsigned i=0; i<FIndex ; ++i) {
540 Offset += MFI->getObjectSize(i);
541 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000542
543 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000544}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000545
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000546// This function legalizes the PIC16 Addresses. If the Pointer is
547// -- Direct address variable residing
548// --> then a Banksel for that variable will be created.
549// -- Rom variable
550// --> then it will be treated as an indirect address.
551// -- Indirect address
552// --> then the address will be loaded into FSR
553// -- ADD with constant operand
554// --> then constant operand of ADD will be returned as Offset
555// and non-constant operand of ADD will be treated as pointer.
556// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000557
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000558void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
559 SDValue &Lo, SDValue &Hi,
560 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000561
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000562 // Offset, by default, should be 0
563 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000564
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000565 // If the pointer is ADD with constant,
566 // return the constant value as the offset
567 if (Ptr.getOpcode() == ISD::ADD) {
568 SDValue OperLeft = Ptr.getOperand(0);
569 SDValue OperRight = Ptr.getOperand(1);
570 if (OperLeft.getOpcode() == ISD::Constant) {
571 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
572 Ptr = OperRight;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000573 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000574 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000575 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000576 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000577 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000578
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000579 // If the pointer is Type i8 and an external symbol
580 // then treat it as direct address.
581 // One example for such case is storing and loading
582 // from function frame during a call
583 if (Ptr.getValueType() == MVT::i8) {
584 switch (Ptr.getOpcode()) {
585 case ISD::TargetExternalSymbol:
586 Lo = Ptr;
587 Hi = DAG.getConstant(1, MVT::i8);
588 return;
589 }
590 }
591
592 if (Ptr.getOpcode() == ISD::BUILD_PAIR &&
593 Ptr.getOperand(0).getOpcode() == ISD::TargetFrameIndex) {
594
595 int FrameOffset;
596 LegalizeFrameIndex(Ptr.getOperand(0), DAG, Lo, FrameOffset);
597 Hi = DAG.getConstant(1, MVT::i8);
598 Offset += FrameOffset;
599 return;
600 }
601
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000602 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
603 // Direct addressing case for RAM variables. The Hi part is constant
604 // and the Lo part is the TGA itself.
605 Lo = Ptr.getOperand(0).getOperand(0);
606
607 // For direct addresses Hi is a constant. Value 1 for the constant
608 // signifies that banksel needs to generated for it. Value 0 for
609 // the constant signifies that banksel does not need to be generated
610 // for it. Mark it as 1 now and optimize later.
611 Hi = DAG.getConstant(1, MVT::i8);
612 return;
613 }
614
615 // Indirect addresses. Get the hi and lo parts of ptr.
616 GetExpandedParts(Ptr, DAG, Lo, Hi);
617
618 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000619 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
620 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000621
622 return;
623}
624
Duncan Sands1607f052008-12-01 11:39:25 +0000625SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000626 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
627 SDValue Chain = LD->getChain();
628 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000629 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000630
631 SDValue Load, Offset;
632 SDVTList Tys;
633 MVT VT, NewVT;
634 SDValue PtrLo, PtrHi;
635 unsigned LoadOffset;
636
637 // Legalize direct/indirect addresses. This will give the lo and hi parts
638 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000639 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000640
641 // Load from the pointer (direct address or FSR)
642 VT = N->getValueType(0);
643 unsigned NumLoads = VT.getSizeInBits() / 8;
644 std::vector<SDValue> PICLoads;
645 unsigned iter;
646 MVT MemVT = LD->getMemoryVT();
647 if(ISD::isNON_EXTLoad(N)) {
648 for (iter=0; iter<NumLoads ; ++iter) {
649 // Add the pointer offset if any
650 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
651 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000652 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000653 Offset);
654 PICLoads.push_back(Load);
655 }
656 } else {
657 // If it is extended load then use PIC16Load for Memory Bytes
658 // and for all extended bytes perform action based on type of
659 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
660
661
662 // For extended loads this is the memory value type
663 // i.e. without any extension
664 MVT MemVT = LD->getMemoryVT();
665 unsigned MemBytes = MemVT.getSizeInBits() / 8;
666 unsigned ExtdBytes = VT.getSizeInBits() / 8;
667 Offset = DAG.getConstant(LoadOffset, MVT::i8);
668
669 Tys = DAG.getVTList(MVT::i8, MVT::Other);
670 // For MemBytes generate PIC16Load with proper offset
671 for (iter=0; iter<MemBytes; ++iter) {
672 // Add the pointer offset if any
673 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000674 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000675 Offset);
676 PICLoads.push_back(Load);
677 }
678
679 // For SignExtendedLoad
680 if (ISD::isSEXTLoad(N)) {
681 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
682 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000683 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000684 DAG.getConstant(7, MVT::i8));
685 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
686 PICLoads.push_back(SRA);
687 }
688 } else if (ISD::isZEXTLoad(N)) {
689 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
690 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
691 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
692 PICLoads.push_back(ConstZero);
693 }
694 }
695 }
696 SDValue BP;
697
698 if (VT == MVT::i8) {
699 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000700 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000701 }
702 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000703 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000704 if (MemVT == MVT::i8)
705 Chain = getChain(PICLoads[0]);
706 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000707 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
708 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000709 } else if (VT == MVT::i32) {
710 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000711 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
712 PICLoads[0], PICLoads[1]);
713 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
714 PICLoads[2], PICLoads[3]);
715 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000716 if (MemVT == MVT::i8)
717 Chain = getChain(PICLoads[0]);
718 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000719 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
720 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000721 else {
722 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000723 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000724 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000725 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000726 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000727 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
728 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000729 }
730 }
731 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000732 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000733}
734
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000735SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
736 // We should have handled larger operands in type legalizer itself.
737 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
738
739 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000740 SDValue Value = N->getOperand(0);
741 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000742 PIC16ISD::PIC16Libcall CallCode;
743 switch (N->getOpcode()) {
744 case ISD::SRA:
745 CallCode = PIC16ISD::SRA_I8;
746 break;
747 case ISD::SHL:
748 CallCode = PIC16ISD::SLL_I8;
749 break;
750 case ISD::SRL:
751 CallCode = PIC16ISD::SRL_I8;
752 break;
753 default:
754 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000755 return SDValue();
756 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000757 SmallVector<SDValue, 2> Ops(2);
758 Ops[0] = Value;
759 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000760 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
761 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000762 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000763}
764
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000765void
766PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
767 SmallVectorImpl<SDValue>&Results,
768 SelectionDAG &DAG) {
769 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000770 SDValue Res;
771 unsigned i;
772 switch (Op.getOpcode()) {
773 case ISD::FORMAL_ARGUMENTS:
774 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
775 case ISD::LOAD:
776 Res = ExpandLoad(Op.getNode(), DAG); break;
777 case ISD::CALL:
778 Res = LowerCALL(Op, DAG); break;
779 default: {
780 // All other operations are handled in LowerOperation.
781 Res = LowerOperation(Op, DAG);
782 if (Res.getNode())
783 Results.push_back(Res);
784
785 return;
786 }
787 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000788
789 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000790 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000791 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000792 Results.push_back(SDValue(N, i));
793 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000794}
795
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000796SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
797 switch (Op.getOpcode()) {
798 case ISD::FORMAL_ARGUMENTS:
799 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000800 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000801 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000802 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000803 return LowerADD(Op, DAG);
804 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000805 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000806 case ISD::SUBE:
807 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000808 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000809 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000810 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000811 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000812 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000813 case ISD::SRA:
814 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000815 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000816 case ISD::OR:
817 case ISD::AND:
818 case ISD::XOR:
819 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000820 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000821 return LowerCALL(Op, DAG);
822 case ISD::RET:
823 return LowerRET(Op, DAG);
824 case ISD::BR_CC:
825 return LowerBR_CC(Op, DAG);
826 case ISD::SELECT_CC:
827 return LowerSELECT_CC(Op, DAG);
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000828 case ISD::DBG_STOPPOINT:
829 return LowerStopPoint(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000830 }
Dan Gohman475871a2008-07-27 21:46:04 +0000831 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000832}
833
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000834SDValue PIC16TargetLowering::LowerStopPoint(SDValue Op, SelectionDAG &DAG) {
835 DbgStopPointSDNode *SP = dyn_cast<DbgStopPointSDNode>(Op);
836 unsigned line = SP->getLine();
837 SDValue LineNode = DAG.getConstant(line, MVT::i8);
838 DebugLoc dl = Op.getDebugLoc();
839 return DAG.getNode(PIC16ISD::PIC16StopPoint, dl, MVT::Other,
840 Op.getOperand(0), LineNode);
841}
842
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000843SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000844 SelectionDAG &DAG,
845 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000846 assert (Op.getValueType() == MVT::i8
847 && "illegal value type to store on stack.");
848
849 MachineFunction &MF = DAG.getMachineFunction();
850 const Function *Func = MF.getFunction();
851 const std::string FuncName = Func->getName();
852
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000853 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000854
855 // Put the value on stack.
856 // Get a stack slot index and convert to es.
857 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
858 sprintf(tmpName, "%s.tmp", FuncName.c_str());
859 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
860
861 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000862 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000863 DAG.getEntryNode(),
864 Op, ES,
865 DAG.getConstant (1, MVT::i8), // Banksel.
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000866 DAG.getConstant (GetTmpOffsetForFI(FI),
867 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000868
869 // Load the value from ES.
870 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000871 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000872 ES, DAG.getConstant (1, MVT::i8),
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000873 DAG.getConstant (GetTmpOffsetForFI(FI),
874 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000875
876 return Load.getValue(0);
877}
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000878
879SDValue PIC16TargetLowering::
880LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
881 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
882 SelectionDAG &DAG) {
883 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
884 unsigned NumOps = TheCall->getNumArgs();
885 DebugLoc dl = TheCall->getDebugLoc();
886
887 // If call has no arguments then do nothing and return.
888 if (NumOps == 0)
889 return Chain;
890
891 std::vector<SDValue> Ops;
892 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
893 SDValue Arg, StoreRet;
894 for (unsigned i=0; i<NumOps; i++) {
895 // Get the arguments
896 Arg = TheCall->getArg(i);
897 Ops.clear();
898 Ops.push_back(Chain);
899 Ops.push_back(Arg);
900 Ops.push_back(DataAddr_Lo);
901 Ops.push_back(DataAddr_Hi);
902 Ops.push_back(DAG.getConstant(i, MVT::i8));
903 Ops.push_back(InFlag);
904
905 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
906
907 Chain = getChain(StoreRet);
908 InFlag = getOutFlag(StoreRet);
909 }
910 return Chain;
911}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000912
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000913SDValue PIC16TargetLowering::
914LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress,
915 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000916 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
917 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +0000918 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000919 std::string Name;
920 SDValue Arg, StoreAt;
921 MVT ArgVT;
922 unsigned Size=0;
923 unsigned ArgCount=0;
924
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000925 // If call has no arguments then do nothing and return.
926 if (NumOps == 0)
927 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000928
929 // FIXME: This portion of code currently assumes only
930 // primitive types being passed as arguments.
931
932 // Legalize the address before use
933 SDValue PtrLo, PtrHi;
934 unsigned AddressOffset;
935 int StoreOffset = 0;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000936 LegalizeAddress(FrameAddress, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000937 SDValue StoreRet;
938
939 std::vector<SDValue> Ops;
940 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
941 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
942 // Get the argument
943 Arg = TheCall->getArg(i);
944
945 StoreOffset = (Offset + AddressOffset);
946
947 // Store the argument on frame
948
949 Ops.clear();
950 Ops.push_back(Chain);
951 Ops.push_back(Arg.getValue(0));
952 Ops.push_back(PtrLo);
953 Ops.push_back(PtrHi);
954 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
955 Ops.push_back(InFlag);
956
Dale Johannesene8d72302009-02-06 23:05:02 +0000957 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000958
959 Chain = getChain(StoreRet);
960 InFlag = getOutFlag(StoreRet);
961
962 // Update the frame offset to be used for next argument
963 ArgVT = Arg.getValueType();
964 Size = ArgVT.getSizeInBits();
965 Size = Size/8; // Calculate size in bytes
966 Offset += Size; // Increase the frame offset
967 }
968 return Chain;
969}
970
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000971SDValue PIC16TargetLowering::
972LowerIndirectCallReturn (SDValue Op, SDValue Chain, SDValue InFlag,
973 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
974 SelectionDAG &DAG) {
975 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
976 DebugLoc dl = TheCall->getDebugLoc();
977 unsigned RetVals = TheCall->getNumRetVals();
978
979 // If call does not have anything to return
980 // then do nothing and go back.
981 if (RetVals == 0)
982 return Chain;
983
984 // Call has something to return
985 std::vector<SDValue> ResultVals;
986 SDValue LoadRet;
987
988 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
989 for(unsigned i=0;i<RetVals;i++) {
990 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
991 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
992 InFlag);
993 InFlag = getOutFlag(LoadRet);
994 Chain = getChain(LoadRet);
995 ResultVals.push_back(LoadRet);
996 }
997 ResultVals.push_back(Chain);
998 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
999 return Res;
1000}
1001
1002SDValue PIC16TargetLowering::
1003LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress,
1004 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001005 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001006 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001007 // Currently handling primitive types only. They will come in
1008 // i8 parts
1009 unsigned RetVals = TheCall->getNumRetVals();
1010
1011 std::vector<SDValue> ResultVals;
1012
1013 // Return immediately if the return type is void
1014 if (RetVals == 0)
1015 return Chain;
1016
1017 // Call has something to return
1018
1019 // Legalize the address before use
1020 SDValue LdLo, LdHi;
1021 unsigned LdOffset;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001022 LegalizeAddress(FrameAddress, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001023
1024 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1025 SDValue LoadRet;
1026
1027 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1028
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001029 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001030 DAG.getConstant(LdOffset + Offset, MVT::i8),
1031 InFlag);
1032
1033 InFlag = getOutFlag(LoadRet);
1034
1035 Chain = getChain(LoadRet);
1036 Offset++;
1037 ResultVals.push_back(LoadRet);
1038 }
1039
1040 // To return use MERGE_VALUES
1041 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001042 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001043 return Res;
1044}
1045
1046SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001047 SDValue Chain = Op.getOperand(0);
1048 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001049
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001050 if (Op.getNumOperands() == 1) // return void
1051 return Op;
1052
1053 // return should have odd number of operands
1054 if ((Op.getNumOperands() % 2) == 0 ) {
1055 assert(0 && "Do not know how to return this many arguments!");
1056 abort();
1057 }
1058
1059 // Number of values to return
1060 unsigned NumRet = (Op.getNumOperands() / 2);
1061
1062 // Function returns value always on stack with the offset starting
1063 // from 0
1064 MachineFunction &MF = DAG.getMachineFunction();
1065 const Function *F = MF.getFunction();
1066 std::string FuncName = F->getName();
1067
1068 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
1069 sprintf(tmpName, "%s.frame", FuncName.c_str());
1070 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1071 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1072 SDValue BS = DAG.getConstant(1, MVT::i8);
1073 SDValue RetVal;
1074 for(unsigned i=0;i<NumRet; ++i) {
1075 RetVal = Op.getNode()->getOperand(2*i + 1);
1076 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1077 ES, BS,
1078 DAG.getConstant (i, MVT::i8));
1079
1080 }
1081 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001082}
1083
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001084// CALL node may have some operands non-legal to PIC16. Generate new CALL
1085// node with all the operands legal.
1086// Currently only Callee operand of the CALL node is non-legal. This function
1087// legalizes the Callee operand and uses all other operands as are to generate
1088// new CALL node.
1089
1090SDValue PIC16TargetLowering::LegalizeCALL(SDValue Op, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001091 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1092 SDValue Chain = TheCall->getChain();
1093 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001094 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001095 unsigned i =0;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001096
1097 assert(Callee.getValueType() == MVT::i16 &&
1098 "Don't know how to legalize this call node!!!");
1099 assert(Callee.getOpcode() == ISD::BUILD_PAIR &&
1100 "Don't know how to legalize this call node!!!");
1101
1102 if (isDirectAddress(Callee)) {
1103 // Come here for direct calls
1104 Callee = Callee.getOperand(0).getOperand(0);
1105 } else {
1106 // Come here for indirect calls
1107 SDValue Lo, Hi;
1108 // Indirect addresses. Get the hi and lo parts of ptr.
1109 GetExpandedParts(Callee, DAG, Lo, Hi);
1110 // Connect Lo and Hi parts of the callee with the PIC16Connect
1111 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1112 }
1113 std::vector<SDValue> Ops;
1114 Ops.push_back(Chain);
1115 Ops.push_back(Callee);
1116
1117 // Add the call arguments and their flags
1118 unsigned NumArgs = TheCall->getNumArgs();
1119 for(i=0;i<NumArgs;i++) {
1120 Ops.push_back(TheCall->getArg(i));
1121 Ops.push_back(TheCall->getArgFlagsVal(i));
1122 }
1123 std::vector<MVT> NodeTys;
1124 unsigned NumRets = TheCall->getNumRetVals();
1125 for(i=0;i<NumRets;i++)
1126 NodeTys.push_back(TheCall->getRetValType(i));
1127
1128 // Return a Chain as well
1129 NodeTys.push_back(MVT::Other);
1130
1131 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1132 // Generate new call with all the operands legal
1133 return DAG.getCall(TheCall->getCallingConv(), dl,
1134 TheCall->isVarArg(), TheCall->isTailCall(),
1135 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
1136}
1137
1138void PIC16TargetLowering::
1139GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1140 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1141 SelectionDAG &DAG) {
1142 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1143 && "Don't know what to do of such callee!!");
1144 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1145 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1146 Chain = getChain(SeqStart);
1147 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1148
1149 // Get the Lo and Hi part of code address
1150 SDValue Lo = Callee.getOperand(0);
1151 SDValue Hi = Callee.getOperand(1);
1152
1153 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1154 Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Hi);
1155 // Use the Lo part as is and make CALLW
1156 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1157 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1158 OperFlag);
1159 Chain = getChain(Call);
1160 OperFlag = getOutFlag(Call);
1161 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1162 OperFlag);
1163 Chain = getChain(SeqEnd);
1164 OperFlag = getOutFlag(SeqEnd);
1165
1166 // Low part of Data Address
1167 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1168
1169 // Make the second call.
1170 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1171 Chain = getChain(SeqStart);
1172 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1173
1174 // Add 1 to Lo part for the second code word.
1175 Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, Lo, DAG.getConstant(1, MVT::i8));
1176 // Use new Lo to make another CALLW
1177 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1178 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1179 Chain = getChain(Call);
1180 OperFlag = getOutFlag(Call);
1181 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1182 OperFlag);
1183 Chain = getChain(SeqEnd);
1184 OperFlag = getOutFlag(SeqEnd);
1185 // Hi part of Data Address
1186 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1187}
1188
1189
1190SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
1191 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1192 SDValue Chain = TheCall->getChain();
1193 SDValue Callee = TheCall->getCallee();
1194 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001195 if (Callee.getValueType() == MVT::i16 &&
1196 Callee.getOpcode() == ISD::BUILD_PAIR) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001197 // Control should come here only from TypeLegalizer for lowering
1198
1199 // Legalize the non-legal arguments of call and return the
1200 // new call with legal arguments.
1201 return LegalizeCALL(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001202 }
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001203 // Control should come here from Legalize DAG.
1204 // Here all the operands of CALL node should be legal.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001205
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001206 // If this is an indirect call then to pass the arguments
1207 // and read the return value back, we need the data address
1208 // of the function being called.
1209 // To get the data address two more calls need to be made.
1210
1211 // The flag to track if this is a direct or indirect call.
1212 bool IsDirectCall = true;
1213 unsigned RetVals = TheCall->getNumRetVals();
1214 unsigned NumArgs = TheCall->getNumArgs();
1215
1216 SDValue DataAddr_Lo, DataAddr_Hi;
1217 if (Callee.getOpcode() == PIC16ISD::PIC16Connect) {
1218 IsDirectCall = false; // This is indirect call
1219 // Read DataAddress only if we have to pass arguments or
1220 // read return value.
1221 if ((RetVals > 0) || (NumArgs > 0))
1222 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1223 }
1224
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001225 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1226
1227 // Start the call sequence.
1228 // Carring the Constant 0 along the CALLSEQSTART
1229 // because there is nothing else to carry.
1230 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1231 Chain = getChain(SeqStart);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001232 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1233 std::string Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001234
1235 // For any direct call - callee will be GlobalAddressNode or
1236 // ExternalSymbol
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001237 SDValue ArgLabel, RetLabel;
1238 if (IsDirectCall) {
1239 // Considering the GlobalAddressNode case here.
1240 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1241 GlobalValue *GV = G->getGlobal();
1242 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1243 Name = G->getGlobal()->getName();
1244 } else {// Considering the ExternalSymbol case here
1245 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1246 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1247 Name = ES->getSymbol();
1248 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001249
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001250 // Label for argument passing
1251 char *argFrame = new char [strlen(Name.c_str()) + 8];
1252 sprintf(argFrame, "%s.args", Name.c_str());
1253 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001254
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001255 // Label for reading return value
1256 char *retName = new char [strlen(Name.c_str()) + 8];
1257 sprintf(retName, "%s.retval", Name.c_str());
1258 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1259 } else {
1260 // if indirect call
1261 SDValue CodeAddr_Lo = Callee.getOperand(0);
1262 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001263
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001264 CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1265 DAG.getConstant(2, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001266
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001267 // move Hi part in PCLATH
1268 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1269 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1270 CodeAddr_Hi);
1271 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001272
1273 // Pass the argument to function before making the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001274 SDValue CallArgs;
1275 if (IsDirectCall) {
1276 CallArgs = LowerDirectCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1277 Chain = getChain(CallArgs);
1278 OperFlag = getOutFlag(CallArgs);
1279 } else {
1280 CallArgs = LowerIndirectCallArguments(Op, Chain, OperFlag, DataAddr_Lo,
1281 DataAddr_Hi, DAG);
1282 Chain = getChain(CallArgs);
1283 OperFlag = getOutFlag(CallArgs);
1284 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001285
1286 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001287 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001288 OperFlag);
1289 Chain = getChain(PICCall);
1290 OperFlag = getOutFlag(PICCall);
1291
1292
1293 // Carrying the Constant 0 along the CALLSEQSTART
1294 // because there is nothing else to carry.
1295 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1296 OperFlag);
1297 Chain = getChain(SeqEnd);
1298 OperFlag = getOutFlag(SeqEnd);
1299
1300 // Lower the return value reading after the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001301 if (IsDirectCall)
1302 return LowerDirectCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1303 else
1304 return LowerIndirectCallReturn(Op, Chain, OperFlag, DataAddr_Lo,
1305 DataAddr_Hi, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001306}
1307
1308bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1309 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1310 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1311 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1312 return true;
1313 return false;
1314}
1315
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001316// NeedToConvertToMemOp - Returns true if one of the operands of the
1317// operation 'Op' needs to be put into memory. Also returns the
1318// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1319// no instruction that can operation on two registers. Most insns take
1320// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001321bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001322 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001323 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1324 Op.getOperand(1).getOpcode() == ISD::Constant)
1325 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001326
1327 // Return false if one of the operands is already a direct
1328 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001329 if (isDirectLoad(Op.getOperand(0))) {
1330 if (Op.getOperand(0).hasOneUse())
1331 return false;
1332 else
1333 MemOp = 0;
1334 }
1335 if (isDirectLoad(Op.getOperand(1))) {
1336 if (Op.getOperand(1).hasOneUse())
1337 return false;
1338 else
1339 MemOp = 1;
1340 }
1341 return true;
1342}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001343
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001344// LowerBinOp - Lower a commutative binary operation that does not
1345// affect status flag carry.
1346SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001347 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001348
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001349 // We should have handled larger operands in type legalizer itself.
1350 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001351
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001352 unsigned MemOp = 1;
1353 if (NeedToConvertToMemOp(Op, MemOp)) {
1354 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001355 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001356
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001357 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001358 NewVal);
1359 }
1360 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001361 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001362 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001363}
1364
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001365// LowerADD - Lower all types of ADD operations including the ones
1366// that affects carry.
1367SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001368 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001369 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001370 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001371 unsigned MemOp = 1;
1372 if (NeedToConvertToMemOp(Op, MemOp)) {
1373 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001374 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001375
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001376 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001377
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001378 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001379 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1380 NewVal, Op.getOperand(2));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001381 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001382 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1383 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001384 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001385 else if (Op.getOpcode() == ISD::ADD) {
1386 return Op;
1387 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001388 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001389 return SDValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001390 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001391}
1392
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001393SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001394 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001395 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001396 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001397
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001398 // Nothing to do if the first operand is already a direct load and it has
1399 // only one use.
1400 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001401 return SDValue();
1402
1403 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001404 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001405
1406 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001407 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001408 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001409 Op.getOperand(2));
1410 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001411 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001412}
1413
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001414// LowerFORMAL_ARGUMENTS - Argument values are loaded from the
1415// <fname>.args + offset. All arguments are already broken to leaglized
1416// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001417
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001418SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001419 SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001420 SmallVector<SDValue, 8> ArgValues;
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001421 unsigned NumArgVals = Op.getNode()->getNumValues() - 1;
Dale Johannesene8d72302009-02-06 23:05:02 +00001422 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001423 SDValue Chain = Op.getOperand(0); // Formal arguments' chain
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001424
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001425
1426 // Reset the map of FI and TmpOffset
1427 ResetTmpOffsetMap();
1428 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001429 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001430 const Function *F = MF.getFunction();
1431 std::string FuncName = F->getName();
1432
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001433 // Create the <fname>.args external symbol.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001434 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
1435 sprintf(tmpName, "%s.args", FuncName.c_str());
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001436 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001437
1438 // Load arg values from the label + offset.
1439 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001440 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001441 for (unsigned i = 0; i < NumArgVals ; ++i) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001442 SDValue Offset = DAG.getConstant(i, MVT::i8);
1443 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001444 Offset);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001445 Chain = getChain(PICLoad);
1446 ArgValues.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001447 }
1448
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001449 // Return a MERGE_VALUE node.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001450 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001451 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001452 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001453}
1454
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001455// Perform DAGCombine of PIC16Load.
1456// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001457SDValue PIC16TargetLowering::
1458PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1459 SelectionDAG &DAG = DCI.DAG;
1460 SDValue Chain = N->getOperand(0);
1461 if (N->hasNUsesOfValue(0, 0)) {
1462 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1463 }
1464 return SDValue();
1465}
1466
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001467// For all the functions with arguments some STORE nodes are generated
1468// that store the argument on the frameindex. However in PIC16 the arguments
1469// are passed on stack only. Therefore these STORE nodes are redundant.
1470// To remove these STORE nodes will be removed in PerformStoreCombine
1471//
1472// Currently this function is doint nothing and will be updated for removing
1473// unwanted store operations
1474SDValue PIC16TargetLowering::
1475PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001476 return SDValue(N, 0);
1477 /*
1478 // Storing an undef value is of no use, so remove it
1479 if (isStoringUndef(N, Chain, DAG)) {
1480 return Chain; // remove the store and return the chain
1481 }
1482 //else everything is ok.
1483 return SDValue(N, 0);
1484 */
1485}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001486
1487SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1488 DAGCombinerInfo &DCI) const {
1489 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001490 case ISD::STORE:
1491 return PerformStoreCombine(N, DCI);
1492 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001493 return PerformPIC16LoadCombine(N, DCI);
1494 }
1495 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001496}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001497
1498static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1499 switch (CC) {
1500 default: assert(0 && "Unknown condition code!");
1501 case ISD::SETNE: return PIC16CC::NE;
1502 case ISD::SETEQ: return PIC16CC::EQ;
1503 case ISD::SETGT: return PIC16CC::GT;
1504 case ISD::SETGE: return PIC16CC::GE;
1505 case ISD::SETLT: return PIC16CC::LT;
1506 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001507 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001508 case ISD::SETULE: return PIC16CC::LE;
1509 case ISD::SETUGE: return PIC16CC::GE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001510 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001511 }
1512}
1513
1514// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1515// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1516static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1517 ISD::CondCode CC, unsigned &SPCC) {
1518 if (isa<ConstantSDNode>(RHS) &&
1519 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1520 CC == ISD::SETNE &&
1521 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1522 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1523 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1524 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1525 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1526 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1527 SDValue CMPCC = LHS.getOperand(3);
1528 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1529 LHS = CMPCC.getOperand(0);
1530 RHS = CMPCC.getOperand(1);
1531 }
1532}
1533
1534// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1535SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1536 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001537 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001538 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1539
1540 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1541 // i.e. a < 12 can be rewritten as 12 > a.
1542 if (RHS.getOpcode() == ISD::Constant) {
1543
1544 SDValue Tmp = LHS;
1545 LHS = RHS;
1546 RHS = Tmp;
1547
1548 switch (CondCode) {
1549 default: break;
1550 case PIC16CC::LT:
1551 CondCode = PIC16CC::GT;
1552 break;
1553 case PIC16CC::GT:
1554 CondCode = PIC16CC::LT;
1555 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001556 case PIC16CC::ULT:
1557 CondCode = PIC16CC::UGT;
1558 break;
1559 case PIC16CC::UGT:
1560 CondCode = PIC16CC::ULT;
1561 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001562 case PIC16CC::GE:
1563 CondCode = PIC16CC::LE;
1564 break;
1565 case PIC16CC::LE:
1566 CondCode = PIC16CC::GE;
1567 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001568 case PIC16CC::ULE:
1569 CondCode = PIC16CC::UGE;
1570 break;
1571 case PIC16CC::UGE:
1572 CondCode = PIC16CC::ULE;
1573 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001574 }
1575 }
1576
1577 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001578
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001579 // These are signed comparisons.
1580 SDValue Mask = DAG.getConstant(128, MVT::i8);
1581 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001582 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1583 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001584 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001585
1586 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001587 // We can use a subtract operation to set the condition codes. But
1588 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001589 // Nothing to do if the first operand is already a valid type (direct load
1590 // for subwf and literal for sublw) and it is used by this operation only.
1591 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1592 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001593 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001594
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001595 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001596 LHS = ConvertToMemOperand (LHS, DAG, dl);
1597 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001598}
1599
1600
1601SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1602 SDValue LHS = Op.getOperand(0);
1603 SDValue RHS = Op.getOperand(1);
1604 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1605 SDValue TrueVal = Op.getOperand(2);
1606 SDValue FalseVal = Op.getOperand(3);
1607 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001608 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001609
1610 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1611 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1612 // i.e.
1613 // A setcc: lhs, rhs, cc is expanded by llvm to
1614 // select_cc: result of setcc, 0, 1, 0, setne
1615 // We can think of it as:
1616 // select_cc: lhs, rhs, 1, 0, cc
1617 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1618 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1619
1620 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001621 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001622
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001623 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001624 FalseVal, PIC16CC, Cmp.getValue(1));
1625}
1626
1627MachineBasicBlock *
1628PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001629 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001630 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1631 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001632 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001633
1634 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1635 // control-flow pattern. The incoming instruction knows the destination vreg
1636 // to set, the condition code register to branch on, the true/false values to
1637 // select between, and a branch opcode to use.
1638 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1639 MachineFunction::iterator It = BB;
1640 ++It;
1641
1642 // thisMBB:
1643 // ...
1644 // TrueVal = ...
1645 // [f]bCC copy1MBB
1646 // fallthrough --> copy0MBB
1647 MachineBasicBlock *thisMBB = BB;
1648 MachineFunction *F = BB->getParent();
1649 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1650 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001651 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001652 F->insert(It, copy0MBB);
1653 F->insert(It, sinkMBB);
1654
1655 // Update machine-CFG edges by transferring all successors of the current
1656 // block to the new block which will contain the Phi node for the select.
1657 sinkMBB->transferSuccessors(BB);
1658 // Next, add the true and fallthrough blocks as its successors.
1659 BB->addSuccessor(copy0MBB);
1660 BB->addSuccessor(sinkMBB);
1661
1662 // copy0MBB:
1663 // %FalseValue = ...
1664 // # fallthrough to sinkMBB
1665 BB = copy0MBB;
1666
1667 // Update machine-CFG edges
1668 BB->addSuccessor(sinkMBB);
1669
1670 // sinkMBB:
1671 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1672 // ...
1673 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001674 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001675 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1676 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1677
1678 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1679 return BB;
1680}
1681
1682
1683SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1684 SDValue Chain = Op.getOperand(0);
1685 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1686 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1687 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1688 SDValue Dest = Op.getOperand(4); // BB to jump to
1689 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001690 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001691
1692 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1693 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1694 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1695 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1696
1697 // Get the Compare insn and condition code.
1698 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001699 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001700
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001701 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001702 Cmp.getValue(1));
1703}
1704
1705