blob: dec92ad84df270c7b6f06d408529ecc75bbce823 [file] [log] [blame]
Sanjiv Gupta09bb4202008-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 Gupta085ae4f2008-11-19 11:00:54 +000019#include "llvm/GlobalValue.h"
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000020#include "llvm/Function.h"
Sanjiv Gupta4affaea2009-01-13 19:18:47 +000021#include "llvm/CallingConv.h"
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Sanjiv Gupta4affaea2009-01-13 19:18:47 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
Duncan Sandsd46f6b82008-11-28 10:20:03 +000026#include <cstdio>
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +000027
28
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000029using namespace llvm;
30
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000031
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +000032// PIC16TargetLowering Constructor.
33PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +000034 : TargetLowering(TM), TmpSize(0) {
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000035
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +000036 Subtarget = &TM.getSubtarget<PIC16Subtarget>();
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000037
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +000038 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000039
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +000040 setShiftAmountType(MVT::i8);
41 setShiftAmountFlavor(Extend);
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000042
Sanjiv Gupta4affaea2009-01-13 19:18:47 +000043 // SRA library call names
Sanjiv Gupta691e8132009-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 Gupta4affaea2009-01-13 19:18:47 +000047
Sanjiv Gupta691e8132009-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 Gupta4affaea2009-01-13 19:18:47 +000052
53 // SRL library call names
Sanjiv Gupta691e8132009-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 Gupta09bb4202008-05-13 09:02:57 +000062
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +000063 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
Sanjiv Gupta691e8132009-01-21 05:44:05 +000064 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Sanjiv Gupta09bb4202008-05-13 09:02:57 +000065
Sanjiv Gupta085ae4f2008-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 Gupta09bb4202008-05-13 09:02:57 +000069
Sanjiv Gupta085ae4f2008-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 Gupta09bb4202008-05-13 09:02:57 +000073
Sanjiv Gupta085ae4f2008-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 Gupta691e8132009-01-21 05:44:05 +000078 setOperationAction(ISD::ADD, MVT::i8, Custom);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +000079 setOperationAction(ISD::ADD, MVT::i16, Custom);
80
Sanjiv Guptaecfffdb2008-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 Gupta4affaea2009-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 Gupta691e8132009-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 Gupta4affaea2009-01-13 19:18:47 +000092
Sanjiv Gupta691e8132009-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 Gupta4affaea2009-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 Gupta085ae4f2008-11-19 11:00:54 +0000136
137 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
138 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
139
140 // Now deduce the information based on the above mentioned
141 // actions
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000142 computeRegisterProperties();
143}
144
Sanjiv Gupta04c76e02009-03-20 13:42:20 +0000145// getOutFlag - Extract the flag result if the Op has it.
146static SDValue getOutFlag(SDValue &Op) {
147 // Flag is the last value of the node.
148 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
149
150 assert (Flag.getValueType() == MVT::Flag
151 && "Node does not have an out Flag");
152
153 return Flag;
154}
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +0000155// Get the TmpOffset for FrameIndex
Sanjiv Guptaab5e8d92009-04-10 15:10:14 +0000156unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI, unsigned size) {
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +0000157 std::map<unsigned, unsigned>::iterator
158 MapIt = FiTmpOffsetMap.find(FI);
159 if (MapIt != FiTmpOffsetMap.end())
160 return MapIt->second;
161
162 // This FI (FrameIndex) is not yet mapped, so map it
163 FiTmpOffsetMap[FI] = TmpSize;
Sanjiv Guptaab5e8d92009-04-10 15:10:14 +0000164 TmpSize += size;
165 return FiTmpOffsetMap[FI];
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +0000166}
Sanjiv Gupta04c76e02009-03-20 13:42:20 +0000167
168// To extract chain value from the SDValue Nodes
169// This function will help to maintain the chain extracting
170// code at one place. In case of any change in future it will
171// help maintain the code.
172static SDValue getChain(SDValue &Op) {
173 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
174
175 // If the last value returned in Flag then the chain is
176 // second last value returned.
177 if (Chain.getValueType() == MVT::Flag)
178 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
179
180 // All nodes may not produce a chain. Therefore following assert
181 // verifies that the node is returning a chain only.
182 assert (Chain.getValueType() == MVT::Other
183 && "Node does not have a chain");
184
185 return Chain;
186}
187
188/// PopulateResults - Helper function to LowerOperation.
189/// If a node wants to return multiple results after lowering,
190/// it stuffs them into an array of SDValue called Results.
191
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000192static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
193 if (N.getOpcode() == ISD::MERGE_VALUES) {
194 int NumResults = N.getNumOperands();
195 for( int i = 0; i < NumResults; i++)
196 Results.push_back(N.getOperand(i));
197 }
198 else
199 Results.push_back(N);
200}
201
202MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
203 return MVT::i8;
204}
205
Sanjiv Guptae87847a2009-03-20 14:10:20 +0000206/// The type legalizer framework of generating legalizer can generate libcalls
207/// only when the operand/result types are illegal.
208/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
209/// For example an arithmetic right shift. These functions are used to lower
210/// such operations that generate libcall for legal types.
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000211
212void
Sanjiv Gupta691e8132009-01-21 05:44:05 +0000213PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000214 const char *Name) {
Sanjiv Guptae87847a2009-03-20 14:10:20 +0000215 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000216}
217
218const char *
Sanjiv Gupta691e8132009-01-21 05:44:05 +0000219PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae87847a2009-03-20 14:10:20 +0000220 return PIC16LibcallNames[Call];
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000221}
222
223SDValue
Sanjiv Gupta691e8132009-01-21 05:44:05 +0000224PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000225 MVT RetVT, const SDValue *Ops,
226 unsigned NumOps, bool isSigned,
Dale Johannesenca6237b2009-01-30 23:10:59 +0000227 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000228
Sanjiv Guptae87847a2009-03-20 14:10:20 +0000229 TargetLowering::ArgListTy Args;
230 Args.reserve(NumOps);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000231
Sanjiv Guptae87847a2009-03-20 14:10:20 +0000232 TargetLowering::ArgListEntry Entry;
233 for (unsigned i = 0; i != NumOps; ++i) {
234 Entry.Node = Ops[i];
235 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
236 Entry.isSExt = isSigned;
237 Entry.isZExt = !isSigned;
238 Args.push_back(Entry);
239 }
240 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000241
Sanjiv Guptae87847a2009-03-20 14:10:20 +0000242 const Type *RetTy = RetVT.getTypeForMVT();
243 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000244 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Sanjiv Guptae87847a2009-03-20 14:10:20 +0000245 false, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000246
247 return CallInfo.first;
248}
249
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000250const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
251 switch (Opcode) {
252 default: return NULL;
253 case PIC16ISD::Lo: return "PIC16ISD::Lo";
254 case PIC16ISD::Hi: return "PIC16ISD::Hi";
255 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
256 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000257 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
258 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000259 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
260 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +0000261 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000262 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000263 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000264 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000265 case PIC16ISD::BCF: return "PIC16ISD::BCF";
266 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
267 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
268 case PIC16ISD::RLF: return "PIC16ISD::RLF";
269 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000270 case PIC16ISD::CALL: return "PIC16ISD::CALL";
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000271 case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000272 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
273 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
274 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000275 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000276 }
277}
278
Duncan Sands7d9834b2008-12-01 11:39:25 +0000279void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
280 SmallVectorImpl<SDValue>&Results,
281 SelectionDAG &DAG) {
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000282
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000283 switch (N->getOpcode()) {
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000284 case ISD::GlobalAddress:
Duncan Sands7d9834b2008-12-01 11:39:25 +0000285 Results.push_back(ExpandGlobalAddress(N, DAG));
286 return;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000287 case ISD::ExternalSymbol:
288 Results.push_back(ExpandExternalSymbol(N, DAG));
289 return;
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000290 case ISD::STORE:
Duncan Sands7d9834b2008-12-01 11:39:25 +0000291 Results.push_back(ExpandStore(N, DAG));
292 return;
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000293 case ISD::LOAD:
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000294 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands7d9834b2008-12-01 11:39:25 +0000295 return;
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000296 case ISD::ADD:
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000297 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands7d9834b2008-12-01 11:39:25 +0000298 return;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000299 case ISD::FrameIndex:
300 Results.push_back(ExpandFrameIndex(N, DAG));
301 return;
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000302 default:
303 assert (0 && "not implemented");
Duncan Sands7d9834b2008-12-01 11:39:25 +0000304 return;
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000305 }
306}
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000307
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000308SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
309
310 // Currently handling FrameIndex of size MVT::i16 only
311 // One example of this scenario is when return value is written on
312 // FrameIndex#0
313
314 if (N->getValueType(0) != MVT::i16)
315 return SDValue();
316
317 // Expand the FrameIndex into ExternalSymbol and a Constant node
318 // The constant will represent the frame index number
319 // Get the current function frame
320 MachineFunction &MF = DAG.getMachineFunction();
321 const Function *Func = MF.getFunction();
322 const std::string Name = Func->getName();
Dale Johannesen175fdef2009-02-06 21:50:26 +0000323
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000324 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesen175fdef2009-02-06 21:50:26 +0000325 // FIXME there isn't really debug info here
326 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000327 int Index = FR->getIndex();
328
Sanjiv Guptaa8792002009-04-14 02:49:52 +0000329 // Expand FrameIndex like GlobalAddress and ExternalSymbol
330 // Also use Offset field for lo and hi parts. The default
331 // offset is zero.
332 SDValue Offset = DAG.getConstant(0, MVT::i8);
333 SDValue FI = DAG.getTargetFrameIndex(Index, MVT::i8);
334 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, FI, Offset);
335 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, FI, Offset);
336 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), Lo, Hi);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000337}
338
339
340SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000341 StoreSDNode *St = cast<StoreSDNode>(N);
342 SDValue Chain = St->getChain();
343 SDValue Src = St->getValue();
344 SDValue Ptr = St->getBasePtr();
345 MVT ValueType = Src.getValueType();
346 unsigned StoreOffset = 0;
Dale Johannesen175fdef2009-02-06 21:50:26 +0000347 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000348
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000349 SDValue PtrLo, PtrHi;
Dale Johannesen24dd9a52009-02-07 00:55:49 +0000350 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000351
352 if (ValueType == MVT::i8) {
Dale Johannesen175fdef2009-02-06 21:50:26 +0000353 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000354 PtrLo, PtrHi,
355 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000356 }
357 else if (ValueType == MVT::i16) {
358 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
359 SDValue SrcLo, SrcHi;
360 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
361 SDValue ChainLo = Chain, ChainHi = Chain;
362 if (Chain.getOpcode() == ISD::TokenFactor) {
363 ChainLo = Chain.getOperand(0);
364 ChainHi = Chain.getOperand(1);
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000365 }
Dale Johannesen175fdef2009-02-06 21:50:26 +0000366 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000367 ChainLo,
368 SrcLo, PtrLo, PtrHi,
369 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000370
Dale Johannesen175fdef2009-02-06 21:50:26 +0000371 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000372 SrcHi, PtrLo, PtrHi,
373 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000374
Dale Johannesen175fdef2009-02-06 21:50:26 +0000375 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands7d9834b2008-12-01 11:39:25 +0000376 getChain(Store2));
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000377 }
378 else if (ValueType == MVT::i32) {
379 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
380 SDValue SrcLo, SrcHi;
381 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000382
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000383 // Get the expanded parts of each of SrcLo and SrcHi.
384 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
385 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
386 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000387
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000388 SDValue ChainLo = Chain, ChainHi = Chain;
389 if (Chain.getOpcode() == ISD::TokenFactor) {
390 ChainLo = Chain.getOperand(0);
391 ChainHi = Chain.getOperand(1);
392 }
393 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
394 ChainHi2 = ChainHi;
395 if (ChainLo.getOpcode() == ISD::TokenFactor) {
396 ChainLo1 = ChainLo.getOperand(0);
397 ChainLo2 = ChainLo.getOperand(1);
398 }
399 if (ChainHi.getOpcode() == ISD::TokenFactor) {
400 ChainHi1 = ChainHi.getOperand(0);
401 ChainHi2 = ChainHi.getOperand(1);
402 }
Dale Johannesen175fdef2009-02-06 21:50:26 +0000403 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000404 ChainLo1,
405 SrcLo1, PtrLo, PtrHi,
406 DAG.getConstant (0 + StoreOffset, MVT::i8));
407
Dale Johannesen175fdef2009-02-06 21:50:26 +0000408 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000409 SrcLo2, PtrLo, PtrHi,
410 DAG.getConstant (1 + StoreOffset, MVT::i8));
411
Dale Johannesen175fdef2009-02-06 21:50:26 +0000412 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000413 SrcHi1, PtrLo, PtrHi,
414 DAG.getConstant (2 + StoreOffset, MVT::i8));
415
Dale Johannesen175fdef2009-02-06 21:50:26 +0000416 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000417 SrcHi2, PtrLo, PtrHi,
418 DAG.getConstant (3 + StoreOffset, MVT::i8));
419
Dale Johannesen175fdef2009-02-06 21:50:26 +0000420 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
421 getChain(Store1), getChain(Store2));
422 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
423 getChain(Store3), getChain(Store4));
424 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000425
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000426 }
427 else {
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000428 assert (0 && "value type not supported");
Duncan Sands7d9834b2008-12-01 11:39:25 +0000429 return SDValue();
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000430 }
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000431}
432
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000433SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
434{
435 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesen175fdef2009-02-06 21:50:26 +0000436 // FIXME there isn't really debug info here
437 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000438
439 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
Sanjiv Guptaa8792002009-04-14 02:49:52 +0000440 SDValue Offset = DAG.getConstant(0, MVT::i8);
441 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES, Offset);
442 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES, Offset);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000443
Dale Johannesen175fdef2009-02-06 21:50:26 +0000444 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000445}
446
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000447// ExpandGlobalAddress -
Duncan Sands7d9834b2008-12-01 11:39:25 +0000448SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000449 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesen175fdef2009-02-06 21:50:26 +0000450 // FIXME there isn't really debug info here
451 DebugLoc dl = G->getDebugLoc();
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000452
453 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
454 G->getOffset());
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000455
Sanjiv Guptaa8792002009-04-14 02:49:52 +0000456 SDValue Offset = DAG.getConstant(0, MVT::i8);
457 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA, Offset);
458 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA, Offset);
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000459
Dale Johannesen175fdef2009-02-06 21:50:26 +0000460 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000461}
462
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000463bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
464 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000465
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000466 if (Op.getOpcode() == ISD::BUILD_PAIR) {
467 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
468 return true;
469 }
470 return false;
471}
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000472
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000473// Return true if DirectAddress is in ROM_SPACE
474bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000475
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000476 // RomAddress is a GlobalAddress in ROM_SPACE_
477 // If the Op is not a GlobalAddress return NULL without checking
478 // anything further.
479 if (!isDirectAddress(Op))
480 return false;
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000481
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000482 // Its a GlobalAddress.
483 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
484 SDValue TGA = Op.getOperand(0).getOperand(0);
485 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
486 const Type *ValueType = GSDN->getGlobal()->getType();
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000487
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000488 if (!isa<PointerType>(ValueType)) {
489 assert(0 && "TGA must be of a PointerType");
490 }
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000491
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000492 int AddrSpace = dyn_cast<PointerType>(ValueType)->getAddressSpace();
493 if (AddrSpace == PIC16ISD::ROM_SPACE)
494 return true;
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000495
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000496 // Any other address space return it false
497 return false;
498}
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000499
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000500
Sanjiv Gupta04c76e02009-03-20 13:42:20 +0000501// GetExpandedParts - This function is on the similiar lines as
502// the GetExpandedInteger in type legalizer is. This returns expanded
503// parts of Op in Lo and Hi.
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000504
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000505void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
506 SDValue &Lo, SDValue &Hi) {
507 SDNode *N = Op.getNode();
Dale Johannesen913ba762009-02-06 01:31:28 +0000508 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta04c76e02009-03-20 13:42:20 +0000509 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000510
Sanjiv Gupta04c76e02009-03-20 13:42:20 +0000511 // Extract the lo component.
512 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
513 DAG.getConstant(0, MVT::i8));
514
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000515 // extract the hi component
Sanjiv Gupta04c76e02009-03-20 13:42:20 +0000516 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
517 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000518}
519
520// Legalize FrameIndex into ExternalSymbol and offset.
521void
522PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
523 SDValue &ES, int &Offset) {
524
525 MachineFunction &MF = DAG.getMachineFunction();
526 const Function *Func = MF.getFunction();
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +0000527 MachineFrameInfo *MFI = MF.getFrameInfo();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000528 const std::string Name = Func->getName();
529
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000530 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptae48b2ee2009-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();
Sanjiv Guptad40fd9e2009-04-21 05:54:51 +0000538 char *tmpName = new char [strlen(Name.c_str()) + 8];
539 if (FIndex < ReservedFrameCount) {
540 sprintf(tmpName, "%s.frame", Name.c_str());
541 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
542 Offset = 0;
543 for (unsigned i=0; i<FIndex ; ++i) {
544 Offset += MFI->getObjectSize(i);
545 }
546 } else {
547 // FrameIndex has been made for some temporary storage
548 sprintf(tmpName, "%s.tmp", Name.c_str());
549 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
550 Offset = GetTmpOffsetForFI(FIndex, MFI->getObjectSize(FIndex));
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +0000551 }
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000552
553 return;
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000554}
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000555
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000556// This function legalizes the PIC16 Addresses. If the Pointer is
557// -- Direct address variable residing
558// --> then a Banksel for that variable will be created.
559// -- Rom variable
560// --> then it will be treated as an indirect address.
561// -- Indirect address
562// --> then the address will be loaded into FSR
563// -- ADD with constant operand
564// --> then constant operand of ADD will be returned as Offset
565// and non-constant operand of ADD will be treated as pointer.
566// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000567
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +0000568void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
569 SDValue &Lo, SDValue &Hi,
570 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000571
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000572 // Offset, by default, should be 0
573 Offset = 0;
Sanjiv Gupta1f7b12b2008-05-14 11:31:39 +0000574
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000575 // If the pointer is ADD with constant,
576 // return the constant value as the offset
577 if (Ptr.getOpcode() == ISD::ADD) {
578 SDValue OperLeft = Ptr.getOperand(0);
579 SDValue OperRight = Ptr.getOperand(1);
580 if (OperLeft.getOpcode() == ISD::Constant) {
581 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
582 Ptr = OperRight;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000583 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000584 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000585 Ptr = OperLeft;
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000586 }
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000587 }
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000588
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000589 // If the pointer is Type i8 and an external symbol
590 // then treat it as direct address.
591 // One example for such case is storing and loading
592 // from function frame during a call
593 if (Ptr.getValueType() == MVT::i8) {
594 switch (Ptr.getOpcode()) {
595 case ISD::TargetExternalSymbol:
596 Lo = Ptr;
597 Hi = DAG.getConstant(1, MVT::i8);
598 return;
599 }
600 }
601
Sanjiv Guptaa8792002009-04-14 02:49:52 +0000602 // Expansion of FrameIndex has Lo/Hi parts
603 if (isDirectAddress(Ptr)) {
604 SDValue TFI = Ptr.getOperand(0).getOperand(0);
605 if (TFI.getOpcode() == ISD::TargetFrameIndex) {
606 int FrameOffset;
607 LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset);
608 Hi = DAG.getConstant(1, MVT::i8);
609 Offset += FrameOffset;
610 return;
611 }
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000612 }
613
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000614 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
615 // Direct addressing case for RAM variables. The Hi part is constant
616 // and the Lo part is the TGA itself.
617 Lo = Ptr.getOperand(0).getOperand(0);
618
619 // For direct addresses Hi is a constant. Value 1 for the constant
620 // signifies that banksel needs to generated for it. Value 0 for
621 // the constant signifies that banksel does not need to be generated
622 // for it. Mark it as 1 now and optimize later.
623 Hi = DAG.getConstant(1, MVT::i8);
624 return;
625 }
626
627 // Indirect addresses. Get the hi and lo parts of ptr.
628 GetExpandedParts(Ptr, DAG, Lo, Hi);
629
630 // Put the hi and lo parts into FSR.
Dale Johannesen24dd9a52009-02-07 00:55:49 +0000631 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
632 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000633
634 return;
635}
636
Duncan Sands7d9834b2008-12-01 11:39:25 +0000637SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000638 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
639 SDValue Chain = LD->getChain();
640 SDValue Ptr = LD->getBasePtr();
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000641 DebugLoc dl = LD->getDebugLoc();
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000642
643 SDValue Load, Offset;
644 SDVTList Tys;
645 MVT VT, NewVT;
646 SDValue PtrLo, PtrHi;
647 unsigned LoadOffset;
648
649 // Legalize direct/indirect addresses. This will give the lo and hi parts
650 // of the address and the offset.
Dale Johannesen24dd9a52009-02-07 00:55:49 +0000651 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000652
653 // Load from the pointer (direct address or FSR)
654 VT = N->getValueType(0);
655 unsigned NumLoads = VT.getSizeInBits() / 8;
656 std::vector<SDValue> PICLoads;
657 unsigned iter;
658 MVT MemVT = LD->getMemoryVT();
659 if(ISD::isNON_EXTLoad(N)) {
660 for (iter=0; iter<NumLoads ; ++iter) {
661 // Add the pointer offset if any
662 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
663 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000664 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000665 Offset);
666 PICLoads.push_back(Load);
667 }
668 } else {
669 // If it is extended load then use PIC16Load for Memory Bytes
670 // and for all extended bytes perform action based on type of
671 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
672
673
674 // For extended loads this is the memory value type
675 // i.e. without any extension
676 MVT MemVT = LD->getMemoryVT();
677 unsigned MemBytes = MemVT.getSizeInBits() / 8;
678 unsigned ExtdBytes = VT.getSizeInBits() / 8;
679 Offset = DAG.getConstant(LoadOffset, MVT::i8);
680
681 Tys = DAG.getVTList(MVT::i8, MVT::Other);
682 // For MemBytes generate PIC16Load with proper offset
683 for (iter=0; iter<MemBytes; ++iter) {
684 // Add the pointer offset if any
685 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000686 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000687 Offset);
688 PICLoads.push_back(Load);
689 }
690
691 // For SignExtendedLoad
692 if (ISD::isSEXTLoad(N)) {
693 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
694 // highest MemByte
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000695 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000696 DAG.getConstant(7, MVT::i8));
697 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
698 PICLoads.push_back(SRA);
699 }
700 } else if (ISD::isZEXTLoad(N)) {
701 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
702 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
703 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
704 PICLoads.push_back(ConstZero);
705 }
706 }
707 }
708 SDValue BP;
709
710 if (VT == MVT::i8) {
711 // Operand of Load is illegal -- Load itself is legal
Duncan Sands7d9834b2008-12-01 11:39:25 +0000712 return PICLoads[0];
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000713 }
714 else if (VT == MVT::i16) {
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000715 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000716 if (MemVT == MVT::i8)
717 Chain = getChain(PICLoads[0]);
718 else
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000719 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
720 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000721 } else if (VT == MVT::i32) {
722 SDValue BPs[2];
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000723 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
724 PICLoads[0], PICLoads[1]);
725 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
726 PICLoads[2], PICLoads[3]);
727 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000728 if (MemVT == MVT::i8)
729 Chain = getChain(PICLoads[0]);
730 else if (MemVT == MVT::i16)
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000731 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
732 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000733 else {
734 SDValue Chains[2];
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000735 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000736 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000737 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000738 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000739 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
740 Chains[0], Chains[1]);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000741 }
742 }
743 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000744 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000745}
746
Sanjiv Gupta691e8132009-01-21 05:44:05 +0000747SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
748 // We should have handled larger operands in type legalizer itself.
749 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
750
751 SDNode *N = Op.getNode();
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000752 SDValue Value = N->getOperand(0);
753 SDValue Amt = N->getOperand(1);
Sanjiv Gupta691e8132009-01-21 05:44:05 +0000754 PIC16ISD::PIC16Libcall CallCode;
755 switch (N->getOpcode()) {
756 case ISD::SRA:
757 CallCode = PIC16ISD::SRA_I8;
758 break;
759 case ISD::SHL:
760 CallCode = PIC16ISD::SLL_I8;
761 break;
762 case ISD::SRL:
763 CallCode = PIC16ISD::SRL_I8;
764 break;
765 default:
766 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000767 return SDValue();
768 }
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000769 SmallVector<SDValue, 2> Ops(2);
770 Ops[0] = Value;
771 Ops[1] = Amt;
Dale Johannesenca6237b2009-01-30 23:10:59 +0000772 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
773 true, DAG, N->getDebugLoc());
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000774 return Call;
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000775}
776
Duncan Sands1497b522009-01-21 09:00:29 +0000777void
778PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
779 SmallVectorImpl<SDValue>&Results,
780 SelectionDAG &DAG) {
781 SDValue Op = SDValue(N, 0);
Sanjiv Gupta691e8132009-01-21 05:44:05 +0000782 SDValue Res;
783 unsigned i;
784 switch (Op.getOpcode()) {
785 case ISD::FORMAL_ARGUMENTS:
786 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
787 case ISD::LOAD:
788 Res = ExpandLoad(Op.getNode(), DAG); break;
789 case ISD::CALL:
790 Res = LowerCALL(Op, DAG); break;
791 default: {
792 // All other operations are handled in LowerOperation.
793 Res = LowerOperation(Op, DAG);
794 if (Res.getNode())
795 Results.push_back(Res);
796
797 return;
798 }
799 }
Duncan Sands1497b522009-01-21 09:00:29 +0000800
801 N = Res.getNode();
Sanjiv Gupta691e8132009-01-21 05:44:05 +0000802 unsigned NumValues = N->getNumValues();
Duncan Sands1497b522009-01-21 09:00:29 +0000803 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta691e8132009-01-21 05:44:05 +0000804 Results.push_back(SDValue(N, i));
805 }
Sanjiv Gupta691e8132009-01-21 05:44:05 +0000806}
807
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000808SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
809 switch (Op.getOpcode()) {
810 case ISD::FORMAL_ARGUMENTS:
811 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000812 case ISD::ADD:
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000813 case ISD::ADDC:
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000814 case ISD::ADDE:
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000815 return LowerADD(Op, DAG);
816 case ISD::SUB:
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000817 case ISD::SUBC:
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000818 case ISD::SUBE:
819 return LowerSUB(Op, DAG);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000820 case ISD::LOAD:
Duncan Sands7d9834b2008-12-01 11:39:25 +0000821 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000822 case ISD::STORE:
Duncan Sands7d9834b2008-12-01 11:39:25 +0000823 return ExpandStore(Op.getNode(), DAG);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000824 case ISD::SHL:
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000825 case ISD::SRA:
826 case ISD::SRL:
Sanjiv Gupta691e8132009-01-21 05:44:05 +0000827 return LowerShift(Op, DAG);
Sanjiv Guptaecfffdb2008-11-26 10:53:50 +0000828 case ISD::OR:
829 case ISD::AND:
830 case ISD::XOR:
831 return LowerBinOp(Op, DAG);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000832 case ISD::CALL:
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000833 return LowerCALL(Op, DAG);
834 case ISD::RET:
835 return LowerRET(Op, DAG);
836 case ISD::BR_CC:
837 return LowerBR_CC(Op, DAG);
838 case ISD::SELECT_CC:
839 return LowerSELECT_CC(Op, DAG);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000840 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000841 return SDValue();
Sanjiv Gupta09bb4202008-05-13 09:02:57 +0000842}
843
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000844SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000845 SelectionDAG &DAG,
846 DebugLoc dl) {
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000847 assert (Op.getValueType() == MVT::i8
848 && "illegal value type to store on stack.");
849
850 MachineFunction &MF = DAG.getMachineFunction();
851 const Function *Func = MF.getFunction();
852 const std::string FuncName = Func->getName();
853
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +0000854 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000855
856 // Put the value on stack.
857 // Get a stack slot index and convert to es.
858 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
859 sprintf(tmpName, "%s.tmp", FuncName.c_str());
860 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
861
862 // Store the value to ES.
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000863 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000864 DAG.getEntryNode(),
865 Op, ES,
866 DAG.getConstant (1, MVT::i8), // Banksel.
Sanjiv Guptaab5e8d92009-04-10 15:10:14 +0000867 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +0000868 MVT::i8));
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000869
870 // Load the value from ES.
871 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesenc0b654d2009-02-05 01:01:16 +0000872 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000873 ES, DAG.getConstant (1, MVT::i8),
Sanjiv Guptaab5e8d92009-04-10 15:10:14 +0000874 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +0000875 MVT::i8));
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +0000876
877 return Load.getValue(0);
878}
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000879
880SDValue PIC16TargetLowering::
881LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
882 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
883 SelectionDAG &DAG) {
884 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
885 unsigned NumOps = TheCall->getNumArgs();
886 DebugLoc dl = TheCall->getDebugLoc();
887
888 // If call has no arguments then do nothing and return.
889 if (NumOps == 0)
890 return Chain;
891
892 std::vector<SDValue> Ops;
893 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
894 SDValue Arg, StoreRet;
Sanjiv Guptaa9b40de2009-04-09 10:29:32 +0000895
896 // For PIC16 ABI the arguments come after the return value.
897 unsigned RetVals = TheCall->getNumRetVals();
898 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000899 // Get the arguments
900 Arg = TheCall->getArg(i);
Sanjiv Guptad40fd9e2009-04-21 05:54:51 +0000901
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000902 Ops.clear();
903 Ops.push_back(Chain);
904 Ops.push_back(Arg);
905 Ops.push_back(DataAddr_Lo);
906 Ops.push_back(DataAddr_Hi);
Sanjiv Guptaa9b40de2009-04-09 10:29:32 +0000907 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000908 Ops.push_back(InFlag);
909
910 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
911
912 Chain = getChain(StoreRet);
913 InFlag = getOutFlag(StoreRet);
Sanjiv Guptaa9b40de2009-04-09 10:29:32 +0000914 ArgOffset++;
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000915 }
916 return Chain;
917}
Sanjiv Guptaa8792002009-04-14 02:49:52 +0000918
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000919SDValue PIC16TargetLowering::
Sanjiv Guptaa8792002009-04-14 02:49:52 +0000920LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue ArgLabel,
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000921 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000922 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
923 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesen9bfc0172009-02-06 23:05:02 +0000924 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000925 std::string Name;
926 SDValue Arg, StoreAt;
927 MVT ArgVT;
928 unsigned Size=0;
929 unsigned ArgCount=0;
930
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000931 // If call has no arguments then do nothing and return.
932 if (NumOps == 0)
933 return Chain;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000934
935 // FIXME: This portion of code currently assumes only
936 // primitive types being passed as arguments.
937
938 // Legalize the address before use
939 SDValue PtrLo, PtrHi;
940 unsigned AddressOffset;
941 int StoreOffset = 0;
Sanjiv Guptaa8792002009-04-14 02:49:52 +0000942 LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000943 SDValue StoreRet;
944
945 std::vector<SDValue> Ops;
946 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
947 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
948 // Get the argument
949 Arg = TheCall->getArg(i);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000950 StoreOffset = (Offset + AddressOffset);
951
952 // Store the argument on frame
953
954 Ops.clear();
955 Ops.push_back(Chain);
Sanjiv Gupta10fe0122009-04-13 09:38:38 +0000956 Ops.push_back(Arg);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000957 Ops.push_back(PtrLo);
958 Ops.push_back(PtrHi);
959 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
960 Ops.push_back(InFlag);
961
Dale Johannesen9bfc0172009-02-06 23:05:02 +0000962 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta4affaea2009-01-13 19:18:47 +0000963
964 Chain = getChain(StoreRet);
965 InFlag = getOutFlag(StoreRet);
966
967 // Update the frame offset to be used for next argument
968 ArgVT = Arg.getValueType();
969 Size = ArgVT.getSizeInBits();
970 Size = Size/8; // Calculate size in bytes
971 Offset += Size; // Increase the frame offset
972 }
973 return Chain;
974}
975
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +0000976SDValue PIC16TargetLowering::
977LowerIndirectCallReturn (SDValue Op, SDValue Chain, SDValue InFlag,
978 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
979 SelectionDAG &DAG) {
980 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
981 DebugLoc dl = TheCall->getDebugLoc();
982 unsigned RetVals = TheCall->getNumRetVals();
983
984 // If call does not have anything to return
985 // then do nothing and go back.
986 if (RetVals == 0)
987 return Chain;
988
989 // Call has something to return
990 std::vector<SDValue> ResultVals;
991 SDValue LoadRet;
992
993 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
994 for(unsigned i=0;i<RetVals;i++) {
995 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
996 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
997 InFlag);
998 InFlag = getOutFlag(LoadRet);
999 Chain = getChain(LoadRet);
1000 ResultVals.push_back(LoadRet);
1001 }
1002 ResultVals.push_back(Chain);
1003 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
1004 return Res;
1005}
1006
1007SDValue PIC16TargetLowering::
Sanjiv Guptaa8792002009-04-14 02:49:52 +00001008LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue RetLabel,
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001009 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001010 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen2bfdee32009-02-05 00:20:09 +00001011 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001012 // Currently handling primitive types only. They will come in
1013 // i8 parts
1014 unsigned RetVals = TheCall->getNumRetVals();
1015
1016 std::vector<SDValue> ResultVals;
1017
1018 // Return immediately if the return type is void
1019 if (RetVals == 0)
1020 return Chain;
1021
1022 // Call has something to return
1023
1024 // Legalize the address before use
1025 SDValue LdLo, LdHi;
1026 unsigned LdOffset;
Sanjiv Guptaa8792002009-04-14 02:49:52 +00001027 LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001028
1029 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1030 SDValue LoadRet;
1031
1032 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1033
Dale Johannesen2bfdee32009-02-05 00:20:09 +00001034 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001035 DAG.getConstant(LdOffset + Offset, MVT::i8),
1036 InFlag);
1037
1038 InFlag = getOutFlag(LoadRet);
1039
1040 Chain = getChain(LoadRet);
1041 Offset++;
1042 ResultVals.push_back(LoadRet);
1043 }
1044
1045 // To return use MERGE_VALUES
1046 ResultVals.push_back(Chain);
Dale Johannesen2bfdee32009-02-05 00:20:09 +00001047 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001048 return Res;
1049}
1050
1051SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001052 SDValue Chain = Op.getOperand(0);
1053 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001054
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001055 if (Op.getNumOperands() == 1) // return void
1056 return Op;
1057
1058 // return should have odd number of operands
1059 if ((Op.getNumOperands() % 2) == 0 ) {
1060 assert(0 && "Do not know how to return this many arguments!");
1061 abort();
1062 }
1063
1064 // Number of values to return
1065 unsigned NumRet = (Op.getNumOperands() / 2);
1066
1067 // Function returns value always on stack with the offset starting
1068 // from 0
1069 MachineFunction &MF = DAG.getMachineFunction();
1070 const Function *F = MF.getFunction();
1071 std::string FuncName = F->getName();
1072
1073 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
1074 sprintf(tmpName, "%s.frame", FuncName.c_str());
1075 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1076 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1077 SDValue BS = DAG.getConstant(1, MVT::i8);
1078 SDValue RetVal;
1079 for(unsigned i=0;i<NumRet; ++i) {
1080 RetVal = Op.getNode()->getOperand(2*i + 1);
1081 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1082 ES, BS,
1083 DAG.getConstant (i, MVT::i8));
1084
1085 }
1086 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001087}
1088
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001089// CALL node may have some operands non-legal to PIC16. Generate new CALL
1090// node with all the operands legal.
1091// Currently only Callee operand of the CALL node is non-legal. This function
1092// legalizes the Callee operand and uses all other operands as are to generate
1093// new CALL node.
1094
1095SDValue PIC16TargetLowering::LegalizeCALL(SDValue Op, SelectionDAG &DAG) {
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001096 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1097 SDValue Chain = TheCall->getChain();
1098 SDValue Callee = TheCall->getCallee();
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001099 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001100 unsigned i =0;
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001101
1102 assert(Callee.getValueType() == MVT::i16 &&
1103 "Don't know how to legalize this call node!!!");
1104 assert(Callee.getOpcode() == ISD::BUILD_PAIR &&
1105 "Don't know how to legalize this call node!!!");
1106
1107 if (isDirectAddress(Callee)) {
1108 // Come here for direct calls
1109 Callee = Callee.getOperand(0).getOperand(0);
1110 } else {
1111 // Come here for indirect calls
1112 SDValue Lo, Hi;
1113 // Indirect addresses. Get the hi and lo parts of ptr.
1114 GetExpandedParts(Callee, DAG, Lo, Hi);
1115 // Connect Lo and Hi parts of the callee with the PIC16Connect
1116 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1117 }
1118 std::vector<SDValue> Ops;
1119 Ops.push_back(Chain);
1120 Ops.push_back(Callee);
1121
1122 // Add the call arguments and their flags
1123 unsigned NumArgs = TheCall->getNumArgs();
1124 for(i=0;i<NumArgs;i++) {
1125 Ops.push_back(TheCall->getArg(i));
1126 Ops.push_back(TheCall->getArgFlagsVal(i));
1127 }
1128 std::vector<MVT> NodeTys;
1129 unsigned NumRets = TheCall->getNumRetVals();
1130 for(i=0;i<NumRets;i++)
1131 NodeTys.push_back(TheCall->getRetValType(i));
1132
1133 // Return a Chain as well
1134 NodeTys.push_back(MVT::Other);
1135
1136 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1137 // Generate new call with all the operands legal
1138 return DAG.getCall(TheCall->getCallingConv(), dl,
1139 TheCall->isVarArg(), TheCall->isTailCall(),
1140 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
1141}
1142
1143void PIC16TargetLowering::
1144GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1145 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1146 SelectionDAG &DAG) {
1147 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1148 && "Don't know what to do of such callee!!");
1149 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1150 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1151 Chain = getChain(SeqStart);
1152 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1153
1154 // Get the Lo and Hi part of code address
1155 SDValue Lo = Callee.getOperand(0);
1156 SDValue Hi = Callee.getOperand(1);
1157
1158 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1159 Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Hi);
1160 // Use the Lo part as is and make CALLW
1161 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1162 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1163 OperFlag);
1164 Chain = getChain(Call);
1165 OperFlag = getOutFlag(Call);
1166 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1167 OperFlag);
1168 Chain = getChain(SeqEnd);
1169 OperFlag = getOutFlag(SeqEnd);
1170
1171 // Low part of Data Address
1172 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1173
1174 // Make the second call.
1175 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1176 Chain = getChain(SeqStart);
1177 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1178
1179 // Add 1 to Lo part for the second code word.
1180 Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, Lo, DAG.getConstant(1, MVT::i8));
1181 // Use new Lo to make another CALLW
1182 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1183 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1184 Chain = getChain(Call);
1185 OperFlag = getOutFlag(Call);
1186 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1187 OperFlag);
1188 Chain = getChain(SeqEnd);
1189 OperFlag = getOutFlag(SeqEnd);
1190 // Hi part of Data Address
1191 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1192}
1193
1194
1195SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
1196 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1197 SDValue Chain = TheCall->getChain();
1198 SDValue Callee = TheCall->getCallee();
1199 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001200 if (Callee.getValueType() == MVT::i16 &&
1201 Callee.getOpcode() == ISD::BUILD_PAIR) {
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001202 // Control should come here only from TypeLegalizer for lowering
1203
1204 // Legalize the non-legal arguments of call and return the
1205 // new call with legal arguments.
1206 return LegalizeCALL(Op, DAG);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001207 }
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001208 // Control should come here from Legalize DAG.
1209 // Here all the operands of CALL node should be legal.
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001210
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001211 // If this is an indirect call then to pass the arguments
1212 // and read the return value back, we need the data address
1213 // of the function being called.
1214 // To get the data address two more calls need to be made.
1215
1216 // The flag to track if this is a direct or indirect call.
1217 bool IsDirectCall = true;
1218 unsigned RetVals = TheCall->getNumRetVals();
1219 unsigned NumArgs = TheCall->getNumArgs();
1220
1221 SDValue DataAddr_Lo, DataAddr_Hi;
1222 if (Callee.getOpcode() == PIC16ISD::PIC16Connect) {
1223 IsDirectCall = false; // This is indirect call
1224 // Read DataAddress only if we have to pass arguments or
1225 // read return value.
1226 if ((RetVals > 0) || (NumArgs > 0))
1227 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1228 }
1229
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001230 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1231
1232 // Start the call sequence.
1233 // Carring the Constant 0 along the CALLSEQSTART
1234 // because there is nothing else to carry.
1235 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1236 Chain = getChain(SeqStart);
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001237 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1238 std::string Name;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001239
1240 // For any direct call - callee will be GlobalAddressNode or
1241 // ExternalSymbol
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001242 SDValue ArgLabel, RetLabel;
1243 if (IsDirectCall) {
1244 // Considering the GlobalAddressNode case here.
1245 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1246 GlobalValue *GV = G->getGlobal();
1247 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1248 Name = G->getGlobal()->getName();
1249 } else {// Considering the ExternalSymbol case here
1250 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1251 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1252 Name = ES->getSymbol();
1253 }
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001254
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001255 // Label for argument passing
1256 char *argFrame = new char [strlen(Name.c_str()) + 8];
1257 sprintf(argFrame, "%s.args", Name.c_str());
1258 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001259
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001260 // Label for reading return value
1261 char *retName = new char [strlen(Name.c_str()) + 8];
1262 sprintf(retName, "%s.retval", Name.c_str());
1263 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1264 } else {
1265 // if indirect call
1266 SDValue CodeAddr_Lo = Callee.getOperand(0);
1267 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001268
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001269 CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1270 DAG.getConstant(2, MVT::i8));
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001271
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001272 // move Hi part in PCLATH
1273 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1274 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1275 CodeAddr_Hi);
1276 }
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001277
1278 // Pass the argument to function before making the call.
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001279 SDValue CallArgs;
1280 if (IsDirectCall) {
1281 CallArgs = LowerDirectCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1282 Chain = getChain(CallArgs);
1283 OperFlag = getOutFlag(CallArgs);
1284 } else {
1285 CallArgs = LowerIndirectCallArguments(Op, Chain, OperFlag, DataAddr_Lo,
1286 DataAddr_Hi, DAG);
1287 Chain = getChain(CallArgs);
1288 OperFlag = getOutFlag(CallArgs);
1289 }
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001290
1291 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001292 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001293 OperFlag);
1294 Chain = getChain(PICCall);
1295 OperFlag = getOutFlag(PICCall);
1296
1297
1298 // Carrying the Constant 0 along the CALLSEQSTART
1299 // because there is nothing else to carry.
1300 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1301 OperFlag);
1302 Chain = getChain(SeqEnd);
1303 OperFlag = getOutFlag(SeqEnd);
1304
1305 // Lower the return value reading after the call.
Sanjiv Gupta4a912ed2009-04-08 05:38:48 +00001306 if (IsDirectCall)
1307 return LowerDirectCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1308 else
1309 return LowerIndirectCallReturn(Op, Chain, OperFlag, DataAddr_Lo,
1310 DataAddr_Hi, DAG);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001311}
1312
1313bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1314 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1315 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1316 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1317 return true;
1318 return false;
1319}
1320
Sanjiv Gupta04c76e02009-03-20 13:42:20 +00001321// NeedToConvertToMemOp - Returns true if one of the operands of the
1322// operation 'Op' needs to be put into memory. Also returns the
1323// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1324// no instruction that can operation on two registers. Most insns take
1325// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001326bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta04c76e02009-03-20 13:42:20 +00001327 // If one of the operand is a constant, return false.
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001328 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1329 Op.getOperand(1).getOpcode() == ISD::Constant)
1330 return false;
Sanjiv Gupta04c76e02009-03-20 13:42:20 +00001331
1332 // Return false if one of the operands is already a direct
1333 // load and that operand has only one use.
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001334 if (isDirectLoad(Op.getOperand(0))) {
1335 if (Op.getOperand(0).hasOneUse())
1336 return false;
1337 else
1338 MemOp = 0;
1339 }
1340 if (isDirectLoad(Op.getOperand(1))) {
1341 if (Op.getOperand(1).hasOneUse())
1342 return false;
1343 else
1344 MemOp = 1;
1345 }
1346 return true;
1347}
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001348
Sanjiv Gupta04c76e02009-03-20 13:42:20 +00001349// LowerBinOp - Lower a commutative binary operation that does not
1350// affect status flag carry.
1351SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001352 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta04c76e02009-03-20 13:42:20 +00001353
Sanjiv Guptaecfffdb2008-11-26 10:53:50 +00001354 // We should have handled larger operands in type legalizer itself.
1355 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta04c76e02009-03-20 13:42:20 +00001356
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001357 unsigned MemOp = 1;
1358 if (NeedToConvertToMemOp(Op, MemOp)) {
1359 // Put one value on stack.
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001360 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptaecfffdb2008-11-26 10:53:50 +00001361
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001362 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001363 NewVal);
1364 }
1365 else {
Sanjiv Guptaecfffdb2008-11-26 10:53:50 +00001366 return Op;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001367 }
Sanjiv Guptaecfffdb2008-11-26 10:53:50 +00001368}
1369
Sanjiv Gupta04c76e02009-03-20 13:42:20 +00001370// LowerADD - Lower all types of ADD operations including the ones
1371// that affects carry.
1372SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001373 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001374 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001375 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001376 unsigned MemOp = 1;
1377 if (NeedToConvertToMemOp(Op, MemOp)) {
1378 // Put one value on stack.
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001379 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001380
Sanjiv Gupta1cc4ddd2009-04-09 04:03:43 +00001381 // ADDC and ADDE produces two results.
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001382 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001383
Sanjiv Gupta1cc4ddd2009-04-09 04:03:43 +00001384 // ADDE has three operands, the last one is a flag.
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001385 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001386 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1387 NewVal, Op.getOperand(2));
Sanjiv Gupta1cc4ddd2009-04-09 04:03:43 +00001388 // ADDC has two operands.
1389 else if (Op.getOpcode() == ISD::ADDC)
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001390 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1391 NewVal);
Sanjiv Gupta1cc4ddd2009-04-09 04:03:43 +00001392 // ADD it is. It produces only one result.
1393 else
1394 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1395 NewVal);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001396 }
Sanjiv Gupta1cc4ddd2009-04-09 04:03:43 +00001397 else if (Op.getOpcode() == ISD::ADD)
Sanjiv Gupta691e8132009-01-21 05:44:05 +00001398 return Op;
Sanjiv Gupta1cc4ddd2009-04-09 04:03:43 +00001399 else
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001400 return SDValue();
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001401}
1402
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001403SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001404 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001405 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001406 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001407
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001408 // Nothing to do if the first operand is already a direct load and it has
1409 // only one use.
1410 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001411 return SDValue();
1412
1413 // Put first operand on stack.
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001414 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001415
1416 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001417 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001418 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001419 Op.getOperand(2));
1420 else
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001421 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001422}
1423
Sanjiv Guptad40fd9e2009-04-21 05:54:51 +00001424void PIC16TargetLowering::InitReservedFrameCount(const Function *F) {
1425 unsigned NumArgs = F->arg_size();
1426
1427 bool isVoidFunc = (F->getReturnType()->getTypeID() == Type::VoidTyID);
1428
1429 if (isVoidFunc)
1430 ReservedFrameCount = NumArgs;
1431 else
1432 ReservedFrameCount = NumArgs + 1;
1433}
1434
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001435// LowerFORMAL_ARGUMENTS - Argument values are loaded from the
1436// <fname>.args + offset. All arguments are already broken to leaglized
1437// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001438
Sanjiv Gupta04c76e02009-03-20 13:42:20 +00001439SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001440 SelectionDAG &DAG) {
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001441 SmallVector<SDValue, 8> ArgValues;
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001442 unsigned NumArgVals = Op.getNode()->getNumValues() - 1;
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001443 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001444 SDValue Chain = Op.getOperand(0); // Formal arguments' chain
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001445
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001446
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001447 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001448 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001449 const Function *F = MF.getFunction();
1450 std::string FuncName = F->getName();
1451
Sanjiv Guptad40fd9e2009-04-21 05:54:51 +00001452 // Reset the map of FI and TmpOffset
1453 ResetTmpOffsetMap();
1454 // Initialize the ReserveFrameCount
1455 InitReservedFrameCount(F);
1456
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001457 // Create the <fname>.args external symbol.
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001458 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
1459 sprintf(tmpName, "%s.args", FuncName.c_str());
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001460 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001461
1462 // Load arg values from the label + offset.
1463 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001464 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001465 for (unsigned i = 0; i < NumArgVals ; ++i) {
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001466 SDValue Offset = DAG.getConstant(i, MVT::i8);
1467 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001468 Offset);
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001469 Chain = getChain(PICLoad);
1470 ArgValues.push_back(PICLoad);
Sanjiv Gupta09bb4202008-05-13 09:02:57 +00001471 }
1472
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001473 // Return a MERGE_VALUE node.
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001474 ArgValues.push_back(Op.getOperand(0));
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001475 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Gupta4ab710d2009-04-06 10:54:50 +00001476 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001477}
1478
Sanjiv Gupta04c76e02009-03-20 13:42:20 +00001479// Perform DAGCombine of PIC16Load.
1480// FIXME - Need a more elaborate comment here.
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001481SDValue PIC16TargetLowering::
1482PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1483 SelectionDAG &DAG = DCI.DAG;
1484 SDValue Chain = N->getOperand(0);
1485 if (N->hasNUsesOfValue(0, 0)) {
1486 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1487 }
1488 return SDValue();
1489}
1490
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001491// For all the functions with arguments some STORE nodes are generated
1492// that store the argument on the frameindex. However in PIC16 the arguments
1493// are passed on stack only. Therefore these STORE nodes are redundant.
1494// To remove these STORE nodes will be removed in PerformStoreCombine
1495//
1496// Currently this function is doint nothing and will be updated for removing
1497// unwanted store operations
1498SDValue PIC16TargetLowering::
1499PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001500 return SDValue(N, 0);
1501 /*
1502 // Storing an undef value is of no use, so remove it
1503 if (isStoringUndef(N, Chain, DAG)) {
1504 return Chain; // remove the store and return the chain
1505 }
1506 //else everything is ok.
1507 return SDValue(N, 0);
1508 */
1509}
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001510
1511SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1512 DAGCombinerInfo &DCI) const {
1513 switch (N->getOpcode()) {
Sanjiv Guptae48b2ee2009-04-02 17:42:00 +00001514 case ISD::STORE:
1515 return PerformStoreCombine(N, DCI);
1516 case PIC16ISD::PIC16Load:
Sanjiv Gupta085ae4f2008-11-19 11:00:54 +00001517 return PerformPIC16LoadCombine(N, DCI);
1518 }
1519 return SDValue();
Sanjiv Gupta09bb4202008-05-13 09:02:57 +00001520}
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001521
1522static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1523 switch (CC) {
1524 default: assert(0 && "Unknown condition code!");
1525 case ISD::SETNE: return PIC16CC::NE;
1526 case ISD::SETEQ: return PIC16CC::EQ;
1527 case ISD::SETGT: return PIC16CC::GT;
1528 case ISD::SETGE: return PIC16CC::GE;
1529 case ISD::SETLT: return PIC16CC::LT;
1530 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta691e8132009-01-21 05:44:05 +00001531 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001532 case ISD::SETULE: return PIC16CC::LE;
1533 case ISD::SETUGE: return PIC16CC::GE;
Sanjiv Gupta691e8132009-01-21 05:44:05 +00001534 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001535 }
1536}
1537
1538// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1539// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1540static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1541 ISD::CondCode CC, unsigned &SPCC) {
1542 if (isa<ConstantSDNode>(RHS) &&
1543 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1544 CC == ISD::SETNE &&
1545 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1546 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1547 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1548 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1549 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1550 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1551 SDValue CMPCC = LHS.getOperand(3);
1552 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1553 LHS = CMPCC.getOperand(0);
1554 RHS = CMPCC.getOperand(1);
1555 }
1556}
1557
1558// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1559SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1560 unsigned CC, SDValue &PIC16CC,
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001561 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001562 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1563
1564 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1565 // i.e. a < 12 can be rewritten as 12 > a.
1566 if (RHS.getOpcode() == ISD::Constant) {
1567
1568 SDValue Tmp = LHS;
1569 LHS = RHS;
1570 RHS = Tmp;
1571
1572 switch (CondCode) {
1573 default: break;
1574 case PIC16CC::LT:
1575 CondCode = PIC16CC::GT;
1576 break;
1577 case PIC16CC::GT:
1578 CondCode = PIC16CC::LT;
1579 break;
Sanjiv Gupta691e8132009-01-21 05:44:05 +00001580 case PIC16CC::ULT:
1581 CondCode = PIC16CC::UGT;
1582 break;
1583 case PIC16CC::UGT:
1584 CondCode = PIC16CC::ULT;
1585 break;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001586 case PIC16CC::GE:
1587 CondCode = PIC16CC::LE;
1588 break;
1589 case PIC16CC::LE:
1590 CondCode = PIC16CC::GE;
1591 break;
Sanjiv Gupta691e8132009-01-21 05:44:05 +00001592 case PIC16CC::ULE:
1593 CondCode = PIC16CC::UGE;
1594 break;
1595 case PIC16CC::UGE:
1596 CondCode = PIC16CC::ULE;
1597 break;
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001598 }
1599 }
1600
1601 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001602
Sanjiv Gupta691e8132009-01-21 05:44:05 +00001603 // These are signed comparisons.
1604 SDValue Mask = DAG.getConstant(128, MVT::i8);
1605 if (isSignedComparison(CondCode)) {
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001606 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1607 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta691e8132009-01-21 05:44:05 +00001608 }
Sanjiv Guptaedcf18b2009-01-30 07:55:25 +00001609
1610 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001611 // We can use a subtract operation to set the condition codes. But
1612 // we need to put one operand in memory if required.
Sanjiv Guptaedcf18b2009-01-30 07:55:25 +00001613 // Nothing to do if the first operand is already a valid type (direct load
1614 // for subwf and literal for sublw) and it is used by this operation only.
1615 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1616 && LHS.hasOneUse())
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001617 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001618
Sanjiv Guptaedcf18b2009-01-30 07:55:25 +00001619 // else convert the first operand to mem.
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001620 LHS = ConvertToMemOperand (LHS, DAG, dl);
1621 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001622}
1623
1624
1625SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1626 SDValue LHS = Op.getOperand(0);
1627 SDValue RHS = Op.getOperand(1);
1628 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1629 SDValue TrueVal = Op.getOperand(2);
1630 SDValue FalseVal = Op.getOperand(3);
1631 unsigned ORIGCC = ~0;
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001632 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001633
1634 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1635 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1636 // i.e.
1637 // A setcc: lhs, rhs, cc is expanded by llvm to
1638 // select_cc: result of setcc, 0, 1, 0, setne
1639 // We can think of it as:
1640 // select_cc: lhs, rhs, 1, 0, cc
1641 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1642 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1643
1644 SDValue PIC16CC;
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001645 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001646
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001647 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001648 FalseVal, PIC16CC, Cmp.getValue(1));
1649}
1650
1651MachineBasicBlock *
1652PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman96d60922009-02-07 16:15:20 +00001653 MachineBasicBlock *BB) const {
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001654 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1655 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesen31b0a602009-02-13 02:29:03 +00001656 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001657
1658 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1659 // control-flow pattern. The incoming instruction knows the destination vreg
1660 // to set, the condition code register to branch on, the true/false values to
1661 // select between, and a branch opcode to use.
1662 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1663 MachineFunction::iterator It = BB;
1664 ++It;
1665
1666 // thisMBB:
1667 // ...
1668 // TrueVal = ...
1669 // [f]bCC copy1MBB
1670 // fallthrough --> copy0MBB
1671 MachineBasicBlock *thisMBB = BB;
1672 MachineFunction *F = BB->getParent();
1673 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1674 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen31b0a602009-02-13 02:29:03 +00001675 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001676 F->insert(It, copy0MBB);
1677 F->insert(It, sinkMBB);
1678
1679 // Update machine-CFG edges by transferring all successors of the current
1680 // block to the new block which will contain the Phi node for the select.
1681 sinkMBB->transferSuccessors(BB);
1682 // Next, add the true and fallthrough blocks as its successors.
1683 BB->addSuccessor(copy0MBB);
1684 BB->addSuccessor(sinkMBB);
1685
1686 // copy0MBB:
1687 // %FalseValue = ...
1688 // # fallthrough to sinkMBB
1689 BB = copy0MBB;
1690
1691 // Update machine-CFG edges
1692 BB->addSuccessor(sinkMBB);
1693
1694 // sinkMBB:
1695 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1696 // ...
1697 BB = sinkMBB;
Dale Johannesen31b0a602009-02-13 02:29:03 +00001698 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001699 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1700 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1701
1702 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1703 return BB;
1704}
1705
1706
1707SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1708 SDValue Chain = Op.getOperand(0);
1709 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1710 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1711 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1712 SDValue Dest = Op.getOperand(4); // BB to jump to
1713 unsigned ORIGCC = ~0;
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001714 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001715
1716 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1717 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1718 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1719 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1720
1721 // Get the Compare insn and condition code.
1722 SDValue PIC16CC;
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001723 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001724
Dale Johannesenc0b654d2009-02-05 01:01:16 +00001725 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta4affaea2009-01-13 19:18:47 +00001726 Cmp.getValue(1));
1727}
1728