blob: f113a483d692461d42e2da78217995783f2a1372 [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"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000026
27
Sanjiv Gupta0e687712008-05-13 09:02:57 +000028using namespace llvm;
29
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000030static const char *getIntrinsicName(unsigned opcode) {
31 std::string Basename;
32 switch(opcode) {
33 default: assert (0 && "do not know intrinsic name");
Sanjiv Gupta003263b2009-06-16 16:17:35 +000034 // Arithmetic Right shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000035 case PIC16ISD::SRA_I8: Basename = "sra.i8"; break;
36 case RTLIB::SRA_I16: Basename = "sra.i16"; break;
37 case RTLIB::SRA_I32: Basename = "sra.i32"; break;
38
Sanjiv Gupta003263b2009-06-16 16:17:35 +000039 // Left shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000040 case PIC16ISD::SLL_I8: Basename = "sll.i8"; break;
41 case RTLIB::SHL_I16: Basename = "sll.i16"; break;
42 case RTLIB::SHL_I32: Basename = "sll.i32"; break;
43
Sanjiv Gupta003263b2009-06-16 16:17:35 +000044 // Logical Right Shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000045 case PIC16ISD::SRL_I8: Basename = "srl.i8"; break;
46 case RTLIB::SRL_I16: Basename = "srl.i16"; break;
47 case RTLIB::SRL_I32: Basename = "srl.i32"; break;
48
Sanjiv Gupta003263b2009-06-16 16:17:35 +000049 // Multiply for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000050 case PIC16ISD::MUL_I8: Basename = "mul.i8"; break;
51 case RTLIB::MUL_I16: Basename = "mul.i16"; break;
52 case RTLIB::MUL_I32: Basename = "mul.i32"; break;
Sanjiv Gupta46800772009-06-05 14:43:12 +000053
Sanjiv Gupta003263b2009-06-16 16:17:35 +000054 // Signed division for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000055 case RTLIB::SDIV_I16: Basename = "sdiv.i16"; break;
56 case RTLIB::SDIV_I32: Basename = "sdiv.i32"; break;
Sanjiv Gupta003263b2009-06-16 16:17:35 +000057
58 // Unsigned division for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000059 case RTLIB::UDIV_I16: Basename = "udiv.i16"; break;
60 case RTLIB::UDIV_I32: Basename = "udiv.i32"; break;
61
Sanjiv Gupta003263b2009-06-16 16:17:35 +000062 // Signed Modulas for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000063 case RTLIB::SREM_I16: Basename = "srem.i16"; break;
64 case RTLIB::SREM_I32: Basename = "srem.i32"; break;
Sanjiv Gupta003263b2009-06-16 16:17:35 +000065
66 // Unsigned Modulas for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000067 case RTLIB::UREM_I16: Basename = "urem.i16"; break;
68 case RTLIB::UREM_I32: Basename = "urem.i32"; break;
Sanjiv Gupta63963292009-06-11 16:50:48 +000069
Sanjiv Gupta003263b2009-06-16 16:17:35 +000070 //////////////////////
71 // LIBCALLS FOR FLOATS
72 //////////////////////
73
74 // Float to signed integrals
75 case RTLIB::FPTOSINT_F32_I8: Basename = "f32_to_si32"; break;
76 case RTLIB::FPTOSINT_F32_I16: Basename = "f32_to_si32"; break;
77 case RTLIB::FPTOSINT_F32_I32: Basename = "f32_to_si32"; break;
78
79 // Signed integrals to float. char and int are first sign extended to i32
80 // before being converted to float, so an I8_F32 or I16_F32 isn't required.
81 case RTLIB::SINTTOFP_I32_F32: Basename = "si32_to_f32"; break;
82
83 // Float to Unsigned conversions.
84 // Signed conversion can be used for unsigned conversion as well.
85 // In signed and unsigned versions only the interpretation of the
86 // MSB is different. Bit representation remains the same.
87 case RTLIB::FPTOUINT_F32_I8: Basename = "f32_to_si32"; break;
88 case RTLIB::FPTOUINT_F32_I16: Basename = "f32_to_si32"; break;
89 case RTLIB::FPTOUINT_F32_I32: Basename = "f32_to_si32"; break;
90
91 // Unsigned to Float conversions. char and int are first zero extended
92 // before being converted to float.
93 case RTLIB::UINTTOFP_I32_F32: Basename = "ui32_to_f32"; break;
Sanjiv Gupta63963292009-06-11 16:50:48 +000094
Sanjiv Gupta003263b2009-06-16 16:17:35 +000095 // Floating point add, sub, mul, div.
Sanjiv Gupta63963292009-06-11 16:50:48 +000096 case RTLIB::ADD_F32: Basename = "add.f32"; break;
97 case RTLIB::SUB_F32: Basename = "sub.f32"; break;
98 case RTLIB::MUL_F32: Basename = "mul.f32"; break;
99 case RTLIB::DIV_F32: Basename = "div.f32"; break;
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000100 }
101
102 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
103 std::string tagname = PAN::getTagName(PAN::LIBCALL);
104 std::string Fullname = prefix + tagname + Basename;
105
106 // The name has to live through program life.
107 char *tmp = new char[Fullname.size() + 1];
108 strcpy (tmp, Fullname.c_str());
109
110 return tmp;
111}
112
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000113// PIC16TargetLowering Constructor.
114PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000115 : TargetLowering(TM), TmpSize(0) {
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000116
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000117 Subtarget = &TM.getSubtarget<PIC16Subtarget>();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000118
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000119 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000120
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000121 setShiftAmountType(MVT::i8);
122 setShiftAmountFlavor(Extend);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000123
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000124 // SRA library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000125 setPIC16LibcallName(PIC16ISD::SRA_I8, getIntrinsicName(PIC16ISD::SRA_I8));
126 setLibcallName(RTLIB::SRA_I16, getIntrinsicName(RTLIB::SRA_I16));
127 setLibcallName(RTLIB::SRA_I32, getIntrinsicName(RTLIB::SRA_I32));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000128
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000129 // SHL library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000130 setPIC16LibcallName(PIC16ISD::SLL_I8, getIntrinsicName(PIC16ISD::SLL_I8));
131 setLibcallName(RTLIB::SHL_I16, getIntrinsicName(RTLIB::SHL_I16));
132 setLibcallName(RTLIB::SHL_I32, getIntrinsicName(RTLIB::SHL_I32));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000133
134 // SRL library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000135 setPIC16LibcallName(PIC16ISD::SRL_I8, getIntrinsicName(PIC16ISD::SRL_I8));
136 setLibcallName(RTLIB::SRL_I16, getIntrinsicName(RTLIB::SRL_I16));
137 setLibcallName(RTLIB::SRL_I32, getIntrinsicName(RTLIB::SRL_I32));
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000138
139 // MUL Library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000140 setPIC16LibcallName(PIC16ISD::MUL_I8, getIntrinsicName(PIC16ISD::MUL_I8));
141 setLibcallName(RTLIB::MUL_I16, getIntrinsicName(RTLIB::MUL_I16));
142 setLibcallName(RTLIB::MUL_I32, getIntrinsicName(RTLIB::MUL_I32));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000143
Sanjiv Gupta46800772009-06-05 14:43:12 +0000144 // Signed division lib call names
145 setLibcallName(RTLIB::SDIV_I16, getIntrinsicName(RTLIB::SDIV_I16));
146 setLibcallName(RTLIB::SDIV_I32, getIntrinsicName(RTLIB::SDIV_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000147
Sanjiv Gupta46800772009-06-05 14:43:12 +0000148 // Unsigned division lib call names
149 setLibcallName(RTLIB::UDIV_I16, getIntrinsicName(RTLIB::UDIV_I16));
150 setLibcallName(RTLIB::UDIV_I32, getIntrinsicName(RTLIB::UDIV_I32));
151
152 // Signed remainder lib call names
153 setLibcallName(RTLIB::SREM_I16, getIntrinsicName(RTLIB::SREM_I16));
154 setLibcallName(RTLIB::SREM_I32, getIntrinsicName(RTLIB::SREM_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000155
Sanjiv Gupta46800772009-06-05 14:43:12 +0000156 // Unsigned remainder lib call names
157 setLibcallName(RTLIB::UREM_I16, getIntrinsicName(RTLIB::UREM_I16));
158 setLibcallName(RTLIB::UREM_I32, getIntrinsicName(RTLIB::UREM_I32));
Sanjiv Gupta63963292009-06-11 16:50:48 +0000159
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000160 // Floating point to signed int conversions.
161 setLibcallName(RTLIB::FPTOSINT_F32_I8,
162 getIntrinsicName(RTLIB::FPTOSINT_F32_I8));
163 setLibcallName(RTLIB::FPTOSINT_F32_I16,
164 getIntrinsicName(RTLIB::FPTOSINT_F32_I16));
Sanjiv Gupta63963292009-06-11 16:50:48 +0000165 setLibcallName(RTLIB::FPTOSINT_F32_I32,
166 getIntrinsicName(RTLIB::FPTOSINT_F32_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000167
168 // Signed int to floats.
Sanjiv Gupta63963292009-06-11 16:50:48 +0000169 setLibcallName(RTLIB::SINTTOFP_I32_F32,
170 getIntrinsicName(RTLIB::SINTTOFP_I32_F32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000171
172 // Floating points to unsigned ints.
173 setLibcallName(RTLIB::FPTOUINT_F32_I8,
174 getIntrinsicName(RTLIB::FPTOUINT_F32_I8));
175 setLibcallName(RTLIB::FPTOUINT_F32_I16,
176 getIntrinsicName(RTLIB::FPTOUINT_F32_I16));
177 setLibcallName(RTLIB::FPTOUINT_F32_I32,
178 getIntrinsicName(RTLIB::FPTOUINT_F32_I32));
179
180 // Unsigned int to floats.
181 setLibcallName(RTLIB::UINTTOFP_I32_F32,
182 getIntrinsicName(RTLIB::UINTTOFP_I32_F32));
183
184 // Floating point add, sub, mul ,div.
Sanjiv Gupta63963292009-06-11 16:50:48 +0000185 setLibcallName(RTLIB::ADD_F32, getIntrinsicName(RTLIB::ADD_F32));
186 setLibcallName(RTLIB::SUB_F32, getIntrinsicName(RTLIB::SUB_F32));
187 setLibcallName(RTLIB::MUL_F32, getIntrinsicName(RTLIB::MUL_F32));
188 setLibcallName(RTLIB::DIV_F32, getIntrinsicName(RTLIB::DIV_F32));
189
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000190 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000191 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000192
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000193 setOperationAction(ISD::LOAD, MVT::i8, Legal);
194 setOperationAction(ISD::LOAD, MVT::i16, Custom);
195 setOperationAction(ISD::LOAD, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000196
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000197 setOperationAction(ISD::STORE, MVT::i8, Legal);
198 setOperationAction(ISD::STORE, MVT::i16, Custom);
199 setOperationAction(ISD::STORE, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000200
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000201 setOperationAction(ISD::ADDE, MVT::i8, Custom);
202 setOperationAction(ISD::ADDC, MVT::i8, Custom);
203 setOperationAction(ISD::SUBE, MVT::i8, Custom);
204 setOperationAction(ISD::SUBC, MVT::i8, Custom);
Sanjiv Gupta3b0a4f12009-06-04 08:52:28 +0000205 setOperationAction(ISD::SUB, MVT::i8, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000206 setOperationAction(ISD::ADD, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000207 setOperationAction(ISD::ADD, MVT::i16, Custom);
208
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000209 setOperationAction(ISD::OR, MVT::i8, Custom);
210 setOperationAction(ISD::AND, MVT::i8, Custom);
211 setOperationAction(ISD::XOR, MVT::i8, Custom);
212
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000213 setOperationAction(ISD::FrameIndex, MVT::i16, Custom);
214 setOperationAction(ISD::CALL, MVT::i16, Custom);
215 setOperationAction(ISD::RET, MVT::Other, Custom);
216
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000217 setOperationAction(ISD::MUL, MVT::i8, Custom);
218 setOperationAction(ISD::MUL, MVT::i16, Expand);
219 setOperationAction(ISD::MUL, MVT::i32, Expand);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000220
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000221 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
222 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
223 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
224 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
225 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
226 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
227 setOperationAction(ISD::MULHU, MVT::i8, Expand);
228 setOperationAction(ISD::MULHU, MVT::i16, Expand);
229 setOperationAction(ISD::MULHU, MVT::i32, Expand);
230 setOperationAction(ISD::MULHS, MVT::i8, Expand);
231 setOperationAction(ISD::MULHS, MVT::i16, Expand);
232 setOperationAction(ISD::MULHS, MVT::i32, Expand);
233
234 setOperationAction(ISD::SRA, MVT::i8, Custom);
235 setOperationAction(ISD::SRA, MVT::i16, Expand);
236 setOperationAction(ISD::SRA, MVT::i32, Expand);
237 setOperationAction(ISD::SHL, MVT::i8, Custom);
238 setOperationAction(ISD::SHL, MVT::i16, Expand);
239 setOperationAction(ISD::SHL, MVT::i32, Expand);
240 setOperationAction(ISD::SRL, MVT::i8, Custom);
241 setOperationAction(ISD::SRL, MVT::i16, Expand);
242 setOperationAction(ISD::SRL, MVT::i32, Expand);
243
244 // PIC16 does not support shift parts
245 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
246 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
247 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
248 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
249 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
250 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
251 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
252 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
253 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
254
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000255
256 // PIC16 does not have a SETCC, expand it to SELECT_CC.
257 setOperationAction(ISD::SETCC, MVT::i8, Expand);
258 setOperationAction(ISD::SELECT, MVT::i8, Expand);
259 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
260 setOperationAction(ISD::BRIND, MVT::Other, Expand);
261
262 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
263 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000264
265 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
266 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
267
268 // Now deduce the information based on the above mentioned
269 // actions
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000270 computeRegisterProperties();
271}
272
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000273// getOutFlag - Extract the flag result if the Op has it.
274static SDValue getOutFlag(SDValue &Op) {
275 // Flag is the last value of the node.
276 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
277
278 assert (Flag.getValueType() == MVT::Flag
279 && "Node does not have an out Flag");
280
281 return Flag;
282}
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000283// Get the TmpOffset for FrameIndex
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000284unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI, unsigned size) {
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000285 std::map<unsigned, unsigned>::iterator
286 MapIt = FiTmpOffsetMap.find(FI);
287 if (MapIt != FiTmpOffsetMap.end())
288 return MapIt->second;
289
290 // This FI (FrameIndex) is not yet mapped, so map it
291 FiTmpOffsetMap[FI] = TmpSize;
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000292 TmpSize += size;
293 return FiTmpOffsetMap[FI];
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000294}
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000295
296// To extract chain value from the SDValue Nodes
297// This function will help to maintain the chain extracting
298// code at one place. In case of any change in future it will
299// help maintain the code.
300static SDValue getChain(SDValue &Op) {
301 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
302
303 // If the last value returned in Flag then the chain is
304 // second last value returned.
305 if (Chain.getValueType() == MVT::Flag)
306 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
307
308 // All nodes may not produce a chain. Therefore following assert
309 // verifies that the node is returning a chain only.
310 assert (Chain.getValueType() == MVT::Other
311 && "Node does not have a chain");
312
313 return Chain;
314}
315
316/// PopulateResults - Helper function to LowerOperation.
317/// If a node wants to return multiple results after lowering,
318/// it stuffs them into an array of SDValue called Results.
319
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000320static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
321 if (N.getOpcode() == ISD::MERGE_VALUES) {
322 int NumResults = N.getNumOperands();
323 for( int i = 0; i < NumResults; i++)
324 Results.push_back(N.getOperand(i));
325 }
326 else
327 Results.push_back(N);
328}
329
330MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
331 return MVT::i8;
332}
333
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000334/// The type legalizer framework of generating legalizer can generate libcalls
335/// only when the operand/result types are illegal.
336/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
337/// For example an arithmetic right shift. These functions are used to lower
338/// such operations that generate libcall for legal types.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000339
340void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000341PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000342 const char *Name) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000343 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000344}
345
346const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000347PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000348 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000349}
350
351SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000352PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000353 MVT RetVT, const SDValue *Ops,
354 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000355 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000356
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000357 TargetLowering::ArgListTy Args;
358 Args.reserve(NumOps);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000359
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000360 TargetLowering::ArgListEntry Entry;
361 for (unsigned i = 0; i != NumOps; ++i) {
362 Entry.Node = Ops[i];
363 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
364 Entry.isSExt = isSigned;
365 Entry.isZExt = !isSigned;
366 Args.push_back(Entry);
367 }
368 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000369
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000370 const Type *RetTy = RetVT.getTypeForMVT();
371 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000372 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000373 false, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000374
375 return CallInfo.first;
376}
377
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000378const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
379 switch (Opcode) {
380 default: return NULL;
381 case PIC16ISD::Lo: return "PIC16ISD::Lo";
382 case PIC16ISD::Hi: return "PIC16ISD::Hi";
383 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
384 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000385 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
386 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000387 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
388 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000389 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000390 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000391 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000392 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000393 case PIC16ISD::BCF: return "PIC16ISD::BCF";
394 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
395 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
396 case PIC16ISD::RLF: return "PIC16ISD::RLF";
397 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000398 case PIC16ISD::CALL: return "PIC16ISD::CALL";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000399 case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000400 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
401 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
402 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000403 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000404 }
405}
406
Duncan Sands1607f052008-12-01 11:39:25 +0000407void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
408 SmallVectorImpl<SDValue>&Results,
409 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000410
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000411 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000412 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000413 Results.push_back(ExpandGlobalAddress(N, DAG));
414 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000415 case ISD::ExternalSymbol:
416 Results.push_back(ExpandExternalSymbol(N, DAG));
417 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000418 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000419 Results.push_back(ExpandStore(N, DAG));
420 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000421 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000422 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000423 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000424 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000425 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000426 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000427 case ISD::FrameIndex:
428 Results.push_back(ExpandFrameIndex(N, DAG));
429 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000430 default:
431 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000432 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000433 }
434}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000435
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000436SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
437
438 // Currently handling FrameIndex of size MVT::i16 only
439 // One example of this scenario is when return value is written on
440 // FrameIndex#0
441
442 if (N->getValueType(0) != MVT::i16)
443 return SDValue();
444
445 // Expand the FrameIndex into ExternalSymbol and a Constant node
446 // The constant will represent the frame index number
447 // Get the current function frame
448 MachineFunction &MF = DAG.getMachineFunction();
449 const Function *Func = MF.getFunction();
450 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000451
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000452 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000453 // FIXME there isn't really debug info here
454 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000455
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000456 // Expand FrameIndex like GlobalAddress and ExternalSymbol
457 // Also use Offset field for lo and hi parts. The default
458 // offset is zero.
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000459
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000460 SDValue ES;
461 int FrameOffset;
462 SDValue FI = SDValue(N,0);
463 LegalizeFrameIndex(FI, DAG, ES, FrameOffset);
464 SDValue Offset = DAG.getConstant(FrameOffset, MVT::i8);
465 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, ES, Offset);
466 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, ES, Offset);
467 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000468}
469
470
471SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000472 StoreSDNode *St = cast<StoreSDNode>(N);
473 SDValue Chain = St->getChain();
474 SDValue Src = St->getValue();
475 SDValue Ptr = St->getBasePtr();
476 MVT ValueType = Src.getValueType();
477 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000478 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000479
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000480 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000481 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000482
483 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000484 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000485 PtrLo, PtrHi,
486 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000487 }
488 else if (ValueType == MVT::i16) {
489 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
490 SDValue SrcLo, SrcHi;
491 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
492 SDValue ChainLo = Chain, ChainHi = Chain;
493 if (Chain.getOpcode() == ISD::TokenFactor) {
494 ChainLo = Chain.getOperand(0);
495 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000496 }
Dale Johannesende064702009-02-06 21:50:26 +0000497 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000498 ChainLo,
499 SrcLo, PtrLo, PtrHi,
500 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000501
Dale Johannesende064702009-02-06 21:50:26 +0000502 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000503 SrcHi, PtrLo, PtrHi,
504 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000505
Dale Johannesende064702009-02-06 21:50:26 +0000506 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000507 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000508 }
509 else if (ValueType == MVT::i32) {
510 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
511 SDValue SrcLo, SrcHi;
512 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000513
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000514 // Get the expanded parts of each of SrcLo and SrcHi.
515 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
516 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
517 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000518
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000519 SDValue ChainLo = Chain, ChainHi = Chain;
520 if (Chain.getOpcode() == ISD::TokenFactor) {
521 ChainLo = Chain.getOperand(0);
522 ChainHi = Chain.getOperand(1);
523 }
524 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
525 ChainHi2 = ChainHi;
526 if (ChainLo.getOpcode() == ISD::TokenFactor) {
527 ChainLo1 = ChainLo.getOperand(0);
528 ChainLo2 = ChainLo.getOperand(1);
529 }
530 if (ChainHi.getOpcode() == ISD::TokenFactor) {
531 ChainHi1 = ChainHi.getOperand(0);
532 ChainHi2 = ChainHi.getOperand(1);
533 }
Dale Johannesende064702009-02-06 21:50:26 +0000534 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000535 ChainLo1,
536 SrcLo1, PtrLo, PtrHi,
537 DAG.getConstant (0 + StoreOffset, MVT::i8));
538
Dale Johannesende064702009-02-06 21:50:26 +0000539 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000540 SrcLo2, PtrLo, PtrHi,
541 DAG.getConstant (1 + StoreOffset, MVT::i8));
542
Dale Johannesende064702009-02-06 21:50:26 +0000543 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000544 SrcHi1, PtrLo, PtrHi,
545 DAG.getConstant (2 + StoreOffset, MVT::i8));
546
Dale Johannesende064702009-02-06 21:50:26 +0000547 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000548 SrcHi2, PtrLo, PtrHi,
549 DAG.getConstant (3 + StoreOffset, MVT::i8));
550
Dale Johannesende064702009-02-06 21:50:26 +0000551 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
552 getChain(Store1), getChain(Store2));
553 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
554 getChain(Store3), getChain(Store4));
555 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000556
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000557 }
558 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000559 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000560 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000561 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000562}
563
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000564SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
565{
566 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000567 // FIXME there isn't really debug info here
568 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000569
570 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000571 SDValue Offset = DAG.getConstant(0, MVT::i8);
572 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES, Offset);
573 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES, Offset);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000574
Dale Johannesende064702009-02-06 21:50:26 +0000575 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000576}
577
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000578// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000579SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000580 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000581 // FIXME there isn't really debug info here
582 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000583
584 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
585 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000586
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000587 SDValue Offset = DAG.getConstant(0, MVT::i8);
588 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA, Offset);
589 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA, Offset);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000590
Dale Johannesende064702009-02-06 21:50:26 +0000591 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000592}
593
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000594bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
595 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000596
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000597 if (Op.getOpcode() == ISD::BUILD_PAIR) {
598 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
599 return true;
600 }
601 return false;
602}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000603
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000604// Return true if DirectAddress is in ROM_SPACE
605bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000606
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000607 // RomAddress is a GlobalAddress in ROM_SPACE_
608 // If the Op is not a GlobalAddress return NULL without checking
609 // anything further.
610 if (!isDirectAddress(Op))
611 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000612
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000613 // Its a GlobalAddress.
614 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
615 SDValue TGA = Op.getOperand(0).getOperand(0);
616 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000617
Sanjiv Guptaa3518a12009-04-29 04:43:24 +0000618 if (GSDN->getAddressSpace() == PIC16ISD::ROM_SPACE)
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000619 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000620
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000621 // Any other address space return it false
622 return false;
623}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000624
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000625
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000626// GetExpandedParts - This function is on the similiar lines as
627// the GetExpandedInteger in type legalizer is. This returns expanded
628// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000629
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000630void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
631 SDValue &Lo, SDValue &Hi) {
632 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000633 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000634 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000635
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000636 // Extract the lo component.
637 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
638 DAG.getConstant(0, MVT::i8));
Bill Wendling51b16f42009-05-30 01:09:53 +0000639
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000640 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000641 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
642 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000643}
644
645// Legalize FrameIndex into ExternalSymbol and offset.
646void
647PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
648 SDValue &ES, int &Offset) {
649
650 MachineFunction &MF = DAG.getMachineFunction();
651 const Function *Func = MF.getFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000652 MachineFrameInfo *MFI = MF.getFrameInfo();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000653 const std::string Name = Func->getName();
654
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000655 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000656
657 // FrameIndices are not stack offsets. But they represent the request
658 // for space on stack. That space requested may be more than one byte.
659 // Therefore, to calculate the stack offset that a FrameIndex aligns
660 // with, we need to traverse all the FrameIndices available earlier in
661 // the list and add their requested size.
662 unsigned FIndex = FR->getIndex();
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000663 const char *tmpName;
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000664 if (FIndex < ReservedFrameCount) {
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000665 tmpName = createESName(PAN::getFrameLabel(Name));
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000666 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
667 Offset = 0;
668 for (unsigned i=0; i<FIndex ; ++i) {
669 Offset += MFI->getObjectSize(i);
670 }
671 } else {
672 // FrameIndex has been made for some temporary storage
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000673 tmpName = createESName(PAN::getTempdataLabel(Name));
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000674 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
675 Offset = GetTmpOffsetForFI(FIndex, MFI->getObjectSize(FIndex));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000676 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000677
678 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000679}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000680
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000681// This function legalizes the PIC16 Addresses. If the Pointer is
682// -- Direct address variable residing
683// --> then a Banksel for that variable will be created.
684// -- Rom variable
685// --> then it will be treated as an indirect address.
686// -- Indirect address
687// --> then the address will be loaded into FSR
688// -- ADD with constant operand
689// --> then constant operand of ADD will be returned as Offset
690// and non-constant operand of ADD will be treated as pointer.
691// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000692
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000693void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
694 SDValue &Lo, SDValue &Hi,
695 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000696
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000697 // Offset, by default, should be 0
698 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000699
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000700 // If the pointer is ADD with constant,
701 // return the constant value as the offset
702 if (Ptr.getOpcode() == ISD::ADD) {
703 SDValue OperLeft = Ptr.getOperand(0);
704 SDValue OperRight = Ptr.getOperand(1);
705 if (OperLeft.getOpcode() == ISD::Constant) {
706 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
707 Ptr = OperRight;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000708 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000709 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000710 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000711 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000712 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000713
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000714 // If the pointer is Type i8 and an external symbol
715 // then treat it as direct address.
716 // One example for such case is storing and loading
717 // from function frame during a call
718 if (Ptr.getValueType() == MVT::i8) {
719 switch (Ptr.getOpcode()) {
720 case ISD::TargetExternalSymbol:
721 Lo = Ptr;
722 Hi = DAG.getConstant(1, MVT::i8);
723 return;
724 }
725 }
726
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000727 // Expansion of FrameIndex has Lo/Hi parts
728 if (isDirectAddress(Ptr)) {
729 SDValue TFI = Ptr.getOperand(0).getOperand(0);
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000730 int FrameOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000731 if (TFI.getOpcode() == ISD::TargetFrameIndex) {
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000732 LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset);
733 Hi = DAG.getConstant(1, MVT::i8);
734 Offset += FrameOffset;
735 return;
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000736 } else if (TFI.getOpcode() == ISD::TargetExternalSymbol) {
737 // FrameIndex has already been expanded.
738 // Now just make use of its expansion
739 Lo = TFI;
740 Hi = DAG.getConstant(1, MVT::i8);
741 SDValue FOffset = Ptr.getOperand(0).getOperand(1);
742 assert (FOffset.getOpcode() == ISD::Constant &&
743 "Invalid operand of PIC16ISD::Lo");
744 Offset += dyn_cast<ConstantSDNode>(FOffset)->getZExtValue();
745 return;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000746 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000747 }
748
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000749 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
750 // Direct addressing case for RAM variables. The Hi part is constant
751 // and the Lo part is the TGA itself.
752 Lo = Ptr.getOperand(0).getOperand(0);
753
754 // For direct addresses Hi is a constant. Value 1 for the constant
755 // signifies that banksel needs to generated for it. Value 0 for
756 // the constant signifies that banksel does not need to be generated
757 // for it. Mark it as 1 now and optimize later.
758 Hi = DAG.getConstant(1, MVT::i8);
759 return;
760 }
761
762 // Indirect addresses. Get the hi and lo parts of ptr.
763 GetExpandedParts(Ptr, DAG, Lo, Hi);
764
765 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000766 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
767 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000768
769 return;
770}
771
Duncan Sands1607f052008-12-01 11:39:25 +0000772SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000773 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
774 SDValue Chain = LD->getChain();
775 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000776 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000777
778 SDValue Load, Offset;
779 SDVTList Tys;
780 MVT VT, NewVT;
781 SDValue PtrLo, PtrHi;
782 unsigned LoadOffset;
783
784 // Legalize direct/indirect addresses. This will give the lo and hi parts
785 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000786 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000787
788 // Load from the pointer (direct address or FSR)
789 VT = N->getValueType(0);
790 unsigned NumLoads = VT.getSizeInBits() / 8;
791 std::vector<SDValue> PICLoads;
792 unsigned iter;
793 MVT MemVT = LD->getMemoryVT();
794 if(ISD::isNON_EXTLoad(N)) {
795 for (iter=0; iter<NumLoads ; ++iter) {
796 // Add the pointer offset if any
797 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
798 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000799 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000800 Offset);
801 PICLoads.push_back(Load);
802 }
803 } else {
804 // If it is extended load then use PIC16Load for Memory Bytes
805 // and for all extended bytes perform action based on type of
806 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
807
808
809 // For extended loads this is the memory value type
810 // i.e. without any extension
811 MVT MemVT = LD->getMemoryVT();
812 unsigned MemBytes = MemVT.getSizeInBits() / 8;
813 unsigned ExtdBytes = VT.getSizeInBits() / 8;
814 Offset = DAG.getConstant(LoadOffset, MVT::i8);
815
816 Tys = DAG.getVTList(MVT::i8, MVT::Other);
817 // For MemBytes generate PIC16Load with proper offset
818 for (iter=0; iter<MemBytes; ++iter) {
819 // Add the pointer offset if any
820 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000821 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000822 Offset);
823 PICLoads.push_back(Load);
824 }
825
826 // For SignExtendedLoad
827 if (ISD::isSEXTLoad(N)) {
828 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
829 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000830 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000831 DAG.getConstant(7, MVT::i8));
832 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
833 PICLoads.push_back(SRA);
834 }
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000835 } else if (ISD::isZEXTLoad(N) || ISD::isEXTLoad(N)) {
836 //} else if (ISD::isZEXTLoad(N)) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000837 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
838 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
839 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
840 PICLoads.push_back(ConstZero);
841 }
842 }
843 }
844 SDValue BP;
845
846 if (VT == MVT::i8) {
847 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000848 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000849 }
850 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000851 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000852 if (MemVT == MVT::i8)
853 Chain = getChain(PICLoads[0]);
854 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000855 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
856 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000857 } else if (VT == MVT::i32) {
858 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000859 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
860 PICLoads[0], PICLoads[1]);
861 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
862 PICLoads[2], PICLoads[3]);
863 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000864 if (MemVT == MVT::i8)
865 Chain = getChain(PICLoads[0]);
866 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000867 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
868 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000869 else {
870 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000871 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000872 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000873 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000874 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000875 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
876 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000877 }
878 }
879 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000880 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000881}
882
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000883SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
884 // We should have handled larger operands in type legalizer itself.
885 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
886
887 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000888 SDValue Value = N->getOperand(0);
889 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000890 PIC16ISD::PIC16Libcall CallCode;
891 switch (N->getOpcode()) {
892 case ISD::SRA:
893 CallCode = PIC16ISD::SRA_I8;
894 break;
895 case ISD::SHL:
896 CallCode = PIC16ISD::SLL_I8;
897 break;
898 case ISD::SRL:
899 CallCode = PIC16ISD::SRL_I8;
900 break;
901 default:
902 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000903 return SDValue();
904 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000905 SmallVector<SDValue, 2> Ops(2);
906 Ops[0] = Value;
907 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000908 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
909 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000910 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000911}
912
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000913void
914PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
915 SmallVectorImpl<SDValue>&Results,
916 SelectionDAG &DAG) {
917 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000918 SDValue Res;
919 unsigned i;
920 switch (Op.getOpcode()) {
921 case ISD::FORMAL_ARGUMENTS:
922 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
923 case ISD::LOAD:
924 Res = ExpandLoad(Op.getNode(), DAG); break;
925 case ISD::CALL:
926 Res = LowerCALL(Op, DAG); break;
927 default: {
928 // All other operations are handled in LowerOperation.
929 Res = LowerOperation(Op, DAG);
930 if (Res.getNode())
931 Results.push_back(Res);
932
933 return;
934 }
935 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000936
937 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000938 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000939 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000940 Results.push_back(SDValue(N, i));
941 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000942}
943
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000944SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
945 switch (Op.getOpcode()) {
946 case ISD::FORMAL_ARGUMENTS:
947 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000948 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000949 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000950 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000951 return LowerADD(Op, DAG);
952 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000953 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000954 case ISD::SUBE:
955 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000956 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000957 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000958 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000959 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000960 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000961 case ISD::SRA:
962 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000963 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000964 case ISD::OR:
965 case ISD::AND:
966 case ISD::XOR:
967 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000968 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000969 return LowerCALL(Op, DAG);
970 case ISD::RET:
971 return LowerRET(Op, DAG);
972 case ISD::BR_CC:
973 return LowerBR_CC(Op, DAG);
974 case ISD::SELECT_CC:
975 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000976 }
Dan Gohman475871a2008-07-27 21:46:04 +0000977 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000978}
979
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000980SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000981 SelectionDAG &DAG,
982 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000983 assert (Op.getValueType() == MVT::i8
984 && "illegal value type to store on stack.");
985
986 MachineFunction &MF = DAG.getMachineFunction();
987 const Function *Func = MF.getFunction();
988 const std::string FuncName = Func->getName();
989
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000990
991 // Put the value on stack.
992 // Get a stack slot index and convert to es.
993 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000994 const char *tmpName = createESName(PAN::getTempdataLabel(FuncName));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000995 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
996
997 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000998 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000999 DAG.getEntryNode(),
1000 Op, ES,
1001 DAG.getConstant (1, MVT::i8), // Banksel.
Sanjiv Guptaa3613be2009-04-10 15:10:14 +00001002 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001003 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001004
1005 // Load the value from ES.
1006 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001007 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001008 ES, DAG.getConstant (1, MVT::i8),
Sanjiv Guptaa3613be2009-04-10 15:10:14 +00001009 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001010 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001011
1012 return Load.getValue(0);
1013}
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001014
1015SDValue PIC16TargetLowering::
1016LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
1017 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1018 SelectionDAG &DAG) {
1019 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1020 unsigned NumOps = TheCall->getNumArgs();
1021 DebugLoc dl = TheCall->getDebugLoc();
1022
1023 // If call has no arguments then do nothing and return.
1024 if (NumOps == 0)
1025 return Chain;
1026
1027 std::vector<SDValue> Ops;
1028 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1029 SDValue Arg, StoreRet;
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001030
1031 // For PIC16 ABI the arguments come after the return value.
1032 unsigned RetVals = TheCall->getNumRetVals();
1033 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001034 // Get the arguments
1035 Arg = TheCall->getArg(i);
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001036
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001037 Ops.clear();
1038 Ops.push_back(Chain);
1039 Ops.push_back(Arg);
1040 Ops.push_back(DataAddr_Lo);
1041 Ops.push_back(DataAddr_Hi);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001042 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001043 Ops.push_back(InFlag);
1044
1045 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
1046
1047 Chain = getChain(StoreRet);
1048 InFlag = getOutFlag(StoreRet);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001049 ArgOffset++;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001050 }
1051 return Chain;
1052}
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001053
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001054SDValue PIC16TargetLowering::
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001055LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue ArgLabel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001056 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001057 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1058 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +00001059 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001060 std::string Name;
1061 SDValue Arg, StoreAt;
1062 MVT ArgVT;
1063 unsigned Size=0;
1064 unsigned ArgCount=0;
1065
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001066 // If call has no arguments then do nothing and return.
1067 if (NumOps == 0)
1068 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001069
1070 // FIXME: This portion of code currently assumes only
1071 // primitive types being passed as arguments.
1072
1073 // Legalize the address before use
1074 SDValue PtrLo, PtrHi;
1075 unsigned AddressOffset;
1076 int StoreOffset = 0;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001077 LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001078 SDValue StoreRet;
1079
1080 std::vector<SDValue> Ops;
1081 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1082 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
1083 // Get the argument
1084 Arg = TheCall->getArg(i);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001085 StoreOffset = (Offset + AddressOffset);
1086
1087 // Store the argument on frame
1088
1089 Ops.clear();
1090 Ops.push_back(Chain);
Sanjiv Gupta12f23a82009-04-13 09:38:38 +00001091 Ops.push_back(Arg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001092 Ops.push_back(PtrLo);
1093 Ops.push_back(PtrHi);
1094 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
1095 Ops.push_back(InFlag);
1096
Dale Johannesene8d72302009-02-06 23:05:02 +00001097 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001098
1099 Chain = getChain(StoreRet);
1100 InFlag = getOutFlag(StoreRet);
1101
1102 // Update the frame offset to be used for next argument
1103 ArgVT = Arg.getValueType();
1104 Size = ArgVT.getSizeInBits();
1105 Size = Size/8; // Calculate size in bytes
1106 Offset += Size; // Increase the frame offset
1107 }
1108 return Chain;
1109}
1110
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001111SDValue PIC16TargetLowering::
1112LowerIndirectCallReturn (SDValue Op, SDValue Chain, SDValue InFlag,
1113 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1114 SelectionDAG &DAG) {
1115 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1116 DebugLoc dl = TheCall->getDebugLoc();
1117 unsigned RetVals = TheCall->getNumRetVals();
1118
1119 // If call does not have anything to return
1120 // then do nothing and go back.
1121 if (RetVals == 0)
1122 return Chain;
1123
1124 // Call has something to return
1125 std::vector<SDValue> ResultVals;
1126 SDValue LoadRet;
1127
1128 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1129 for(unsigned i=0;i<RetVals;i++) {
1130 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
1131 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
1132 InFlag);
1133 InFlag = getOutFlag(LoadRet);
1134 Chain = getChain(LoadRet);
1135 ResultVals.push_back(LoadRet);
1136 }
1137 ResultVals.push_back(Chain);
1138 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
1139 return Res;
1140}
1141
1142SDValue PIC16TargetLowering::
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001143LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue RetLabel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001144 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001145 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001146 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001147 // Currently handling primitive types only. They will come in
1148 // i8 parts
1149 unsigned RetVals = TheCall->getNumRetVals();
1150
1151 std::vector<SDValue> ResultVals;
1152
1153 // Return immediately if the return type is void
1154 if (RetVals == 0)
1155 return Chain;
1156
1157 // Call has something to return
1158
1159 // Legalize the address before use
1160 SDValue LdLo, LdHi;
1161 unsigned LdOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001162 LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001163
1164 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1165 SDValue LoadRet;
1166
1167 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1168
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001169 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001170 DAG.getConstant(LdOffset + Offset, MVT::i8),
1171 InFlag);
1172
1173 InFlag = getOutFlag(LoadRet);
1174
1175 Chain = getChain(LoadRet);
1176 Offset++;
1177 ResultVals.push_back(LoadRet);
1178 }
1179
1180 // To return use MERGE_VALUES
1181 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001182 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001183 return Res;
1184}
1185
1186SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001187 SDValue Chain = Op.getOperand(0);
1188 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001189
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001190 if (Op.getNumOperands() == 1) // return void
1191 return Op;
1192
1193 // return should have odd number of operands
1194 if ((Op.getNumOperands() % 2) == 0 ) {
1195 assert(0 && "Do not know how to return this many arguments!");
1196 abort();
1197 }
1198
1199 // Number of values to return
1200 unsigned NumRet = (Op.getNumOperands() / 2);
1201
1202 // Function returns value always on stack with the offset starting
1203 // from 0
1204 MachineFunction &MF = DAG.getMachineFunction();
1205 const Function *F = MF.getFunction();
1206 std::string FuncName = F->getName();
1207
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001208 const char *tmpName = createESName(PAN::getFrameLabel(FuncName));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001209 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1210 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1211 SDValue BS = DAG.getConstant(1, MVT::i8);
1212 SDValue RetVal;
1213 for(unsigned i=0;i<NumRet; ++i) {
1214 RetVal = Op.getNode()->getOperand(2*i + 1);
1215 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1216 ES, BS,
1217 DAG.getConstant (i, MVT::i8));
1218
1219 }
1220 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001221}
1222
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001223// CALL node may have some operands non-legal to PIC16. Generate new CALL
1224// node with all the operands legal.
1225// Currently only Callee operand of the CALL node is non-legal. This function
1226// legalizes the Callee operand and uses all other operands as are to generate
1227// new CALL node.
1228
1229SDValue PIC16TargetLowering::LegalizeCALL(SDValue Op, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001230 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1231 SDValue Chain = TheCall->getChain();
1232 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001233 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001234 unsigned i =0;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001235
1236 assert(Callee.getValueType() == MVT::i16 &&
1237 "Don't know how to legalize this call node!!!");
1238 assert(Callee.getOpcode() == ISD::BUILD_PAIR &&
1239 "Don't know how to legalize this call node!!!");
1240
1241 if (isDirectAddress(Callee)) {
1242 // Come here for direct calls
1243 Callee = Callee.getOperand(0).getOperand(0);
1244 } else {
1245 // Come here for indirect calls
1246 SDValue Lo, Hi;
1247 // Indirect addresses. Get the hi and lo parts of ptr.
1248 GetExpandedParts(Callee, DAG, Lo, Hi);
1249 // Connect Lo and Hi parts of the callee with the PIC16Connect
1250 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1251 }
1252 std::vector<SDValue> Ops;
1253 Ops.push_back(Chain);
1254 Ops.push_back(Callee);
1255
1256 // Add the call arguments and their flags
1257 unsigned NumArgs = TheCall->getNumArgs();
1258 for(i=0;i<NumArgs;i++) {
1259 Ops.push_back(TheCall->getArg(i));
1260 Ops.push_back(TheCall->getArgFlagsVal(i));
1261 }
1262 std::vector<MVT> NodeTys;
1263 unsigned NumRets = TheCall->getNumRetVals();
1264 for(i=0;i<NumRets;i++)
1265 NodeTys.push_back(TheCall->getRetValType(i));
1266
1267 // Return a Chain as well
1268 NodeTys.push_back(MVT::Other);
1269
1270 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1271 // Generate new call with all the operands legal
1272 return DAG.getCall(TheCall->getCallingConv(), dl,
1273 TheCall->isVarArg(), TheCall->isTailCall(),
1274 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
1275}
1276
1277void PIC16TargetLowering::
1278GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1279 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1280 SelectionDAG &DAG) {
1281 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1282 && "Don't know what to do of such callee!!");
1283 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1284 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1285 Chain = getChain(SeqStart);
1286 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1287
1288 // Get the Lo and Hi part of code address
1289 SDValue Lo = Callee.getOperand(0);
1290 SDValue Hi = Callee.getOperand(1);
1291
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001292 SDValue Data_Lo, Data_Hi;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001293 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001294 // Subtract 2 from Address to get the Lower part of DataAddress.
1295 SDVTList VTList = DAG.getVTList(MVT::i8, MVT::Flag);
1296 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1297 DAG.getConstant(2, MVT::i8));
1298 SDValue Ops[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1299 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, Ops, 3);
1300 SDValue PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1301 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001302 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1303 OperFlag);
1304 Chain = getChain(Call);
1305 OperFlag = getOutFlag(Call);
1306 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1307 OperFlag);
1308 Chain = getChain(SeqEnd);
1309 OperFlag = getOutFlag(SeqEnd);
1310
1311 // Low part of Data Address
1312 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1313
1314 // Make the second call.
1315 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1316 Chain = getChain(SeqStart);
1317 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1318
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001319 // Subtract 1 from Address to get high part of data address.
1320 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1321 DAG.getConstant(1, MVT::i8));
1322 SDValue HiOps[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1323 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1324 PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1325
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001326 // Use new Lo to make another CALLW
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001327 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001328 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1329 Chain = getChain(Call);
1330 OperFlag = getOutFlag(Call);
1331 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1332 OperFlag);
1333 Chain = getChain(SeqEnd);
1334 OperFlag = getOutFlag(SeqEnd);
1335 // Hi part of Data Address
1336 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1337}
1338
1339
1340SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
1341 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1342 SDValue Chain = TheCall->getChain();
1343 SDValue Callee = TheCall->getCallee();
1344 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001345 if (Callee.getValueType() == MVT::i16 &&
1346 Callee.getOpcode() == ISD::BUILD_PAIR) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001347 // Control should come here only from TypeLegalizer for lowering
1348
1349 // Legalize the non-legal arguments of call and return the
1350 // new call with legal arguments.
1351 return LegalizeCALL(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001352 }
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001353 // Control should come here from Legalize DAG.
1354 // Here all the operands of CALL node should be legal.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001355
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001356 // If this is an indirect call then to pass the arguments
1357 // and read the return value back, we need the data address
1358 // of the function being called.
1359 // To get the data address two more calls need to be made.
1360
1361 // The flag to track if this is a direct or indirect call.
1362 bool IsDirectCall = true;
1363 unsigned RetVals = TheCall->getNumRetVals();
1364 unsigned NumArgs = TheCall->getNumArgs();
1365
1366 SDValue DataAddr_Lo, DataAddr_Hi;
1367 if (Callee.getOpcode() == PIC16ISD::PIC16Connect) {
1368 IsDirectCall = false; // This is indirect call
1369 // Read DataAddress only if we have to pass arguments or
1370 // read return value.
1371 if ((RetVals > 0) || (NumArgs > 0))
1372 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1373 }
1374
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001375 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1376
1377 // Start the call sequence.
1378 // Carring the Constant 0 along the CALLSEQSTART
1379 // because there is nothing else to carry.
1380 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1381 Chain = getChain(SeqStart);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001382 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1383 std::string Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001384
1385 // For any direct call - callee will be GlobalAddressNode or
1386 // ExternalSymbol
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001387 SDValue ArgLabel, RetLabel;
1388 if (IsDirectCall) {
1389 // Considering the GlobalAddressNode case here.
1390 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1391 GlobalValue *GV = G->getGlobal();
1392 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1393 Name = G->getGlobal()->getName();
1394 } else {// Considering the ExternalSymbol case here
1395 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1396 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1397 Name = ES->getSymbol();
1398 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001399
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001400 // Label for argument passing
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001401 const char *argFrame = createESName(PAN::getArgsLabel(Name));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001402 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001403
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001404 // Label for reading return value
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001405 const char *retName = createESName(PAN::getRetvalLabel(Name));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001406 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1407 } else {
1408 // if indirect call
1409 SDValue CodeAddr_Lo = Callee.getOperand(0);
1410 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001411
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001412 /*CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1413 DAG.getConstant(2, MVT::i8));*/
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001414
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001415 // move Hi part in PCLATH
1416 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1417 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1418 CodeAddr_Hi);
1419 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001420
1421 // Pass the argument to function before making the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001422 SDValue CallArgs;
1423 if (IsDirectCall) {
1424 CallArgs = LowerDirectCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1425 Chain = getChain(CallArgs);
1426 OperFlag = getOutFlag(CallArgs);
1427 } else {
1428 CallArgs = LowerIndirectCallArguments(Op, Chain, OperFlag, DataAddr_Lo,
1429 DataAddr_Hi, DAG);
1430 Chain = getChain(CallArgs);
1431 OperFlag = getOutFlag(CallArgs);
1432 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001433
1434 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001435 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001436 OperFlag);
1437 Chain = getChain(PICCall);
1438 OperFlag = getOutFlag(PICCall);
1439
1440
1441 // Carrying the Constant 0 along the CALLSEQSTART
1442 // because there is nothing else to carry.
1443 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1444 OperFlag);
1445 Chain = getChain(SeqEnd);
1446 OperFlag = getOutFlag(SeqEnd);
1447
1448 // Lower the return value reading after the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001449 if (IsDirectCall)
1450 return LowerDirectCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1451 else
1452 return LowerIndirectCallReturn(Op, Chain, OperFlag, DataAddr_Lo,
1453 DataAddr_Hi, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001454}
1455
1456bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1457 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1458 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1459 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1460 return true;
1461 return false;
1462}
1463
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001464// NeedToConvertToMemOp - Returns true if one of the operands of the
1465// operation 'Op' needs to be put into memory. Also returns the
1466// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1467// no instruction that can operation on two registers. Most insns take
1468// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001469bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001470 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001471 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1472 Op.getOperand(1).getOpcode() == ISD::Constant)
1473 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001474
1475 // Return false if one of the operands is already a direct
1476 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001477 if (isDirectLoad(Op.getOperand(0))) {
1478 if (Op.getOperand(0).hasOneUse())
1479 return false;
1480 else
1481 MemOp = 0;
1482 }
1483 if (isDirectLoad(Op.getOperand(1))) {
1484 if (Op.getOperand(1).hasOneUse())
1485 return false;
1486 else
1487 MemOp = 1;
1488 }
1489 return true;
1490}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001491
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001492// LowerBinOp - Lower a commutative binary operation that does not
1493// affect status flag carry.
1494SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001495 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001496
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001497 // We should have handled larger operands in type legalizer itself.
1498 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001499
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001500 unsigned MemOp = 1;
1501 if (NeedToConvertToMemOp(Op, MemOp)) {
1502 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001503 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001504
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001505 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001506 NewVal);
1507 }
1508 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001509 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001510 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001511}
1512
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001513// LowerADD - Lower all types of ADD operations including the ones
1514// that affects carry.
1515SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001516 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001517 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001518 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001519 unsigned MemOp = 1;
1520 if (NeedToConvertToMemOp(Op, MemOp)) {
1521 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001522 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001523
Dale Johannesen83138992009-06-01 23:13:42 +00001524 // ADDC and ADDE produce two results.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001525 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001526
Dale Johannesen83138992009-06-01 23:13:42 +00001527 // ADDE has three operands, the last one is the carry bit.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001528 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001529 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1530 NewVal, Op.getOperand(2));
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001531 // ADDC has two operands.
1532 else if (Op.getOpcode() == ISD::ADDC)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001533 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1534 NewVal);
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001535 // ADD it is. It produces only one result.
1536 else
1537 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1538 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001539 }
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001540 else
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001541 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001542}
1543
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001544SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001545 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001546 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001547 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001548
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001549 // Nothing to do if the first operand is already a direct load and it has
1550 // only one use.
1551 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001552 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001553
1554 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001555 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001556
1557 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001558 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001559 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001560 Op.getOperand(2));
1561 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001562 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001563}
1564
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001565void PIC16TargetLowering::InitReservedFrameCount(const Function *F) {
1566 unsigned NumArgs = F->arg_size();
1567
1568 bool isVoidFunc = (F->getReturnType()->getTypeID() == Type::VoidTyID);
1569
1570 if (isVoidFunc)
1571 ReservedFrameCount = NumArgs;
1572 else
1573 ReservedFrameCount = NumArgs + 1;
1574}
1575
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001576// LowerFORMAL_ARGUMENTS - Argument values are loaded from the
1577// <fname>.args + offset. All arguments are already broken to leaglized
1578// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001579
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001580SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001581 SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001582 SmallVector<SDValue, 8> ArgValues;
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001583 unsigned NumArgVals = Op.getNode()->getNumValues() - 1;
Dale Johannesene8d72302009-02-06 23:05:02 +00001584 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001585 SDValue Chain = Op.getOperand(0); // Formal arguments' chain
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001586
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001587
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001588 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001589 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001590 const Function *F = MF.getFunction();
1591 std::string FuncName = F->getName();
1592
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001593 // Reset the map of FI and TmpOffset
1594 ResetTmpOffsetMap();
1595 // Initialize the ReserveFrameCount
1596 InitReservedFrameCount(F);
1597
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001598 // Create the <fname>.args external symbol.
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001599 const char *tmpName = createESName(PAN::getArgsLabel(FuncName));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001600 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001601
1602 // Load arg values from the label + offset.
1603 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001604 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001605 for (unsigned i = 0; i < NumArgVals ; ++i) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001606 SDValue Offset = DAG.getConstant(i, MVT::i8);
1607 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001608 Offset);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001609 Chain = getChain(PICLoad);
1610 ArgValues.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001611 }
1612
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001613 // Return a MERGE_VALUE node.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001614 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001615 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001616 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001617}
1618
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001619// Perform DAGCombine of PIC16Load.
1620// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001621SDValue PIC16TargetLowering::
1622PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1623 SelectionDAG &DAG = DCI.DAG;
1624 SDValue Chain = N->getOperand(0);
1625 if (N->hasNUsesOfValue(0, 0)) {
1626 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1627 }
1628 return SDValue();
1629}
1630
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001631// For all the functions with arguments some STORE nodes are generated
1632// that store the argument on the frameindex. However in PIC16 the arguments
1633// are passed on stack only. Therefore these STORE nodes are redundant.
1634// To remove these STORE nodes will be removed in PerformStoreCombine
1635//
1636// Currently this function is doint nothing and will be updated for removing
1637// unwanted store operations
1638SDValue PIC16TargetLowering::
1639PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001640 return SDValue(N, 0);
1641 /*
1642 // Storing an undef value is of no use, so remove it
1643 if (isStoringUndef(N, Chain, DAG)) {
1644 return Chain; // remove the store and return the chain
1645 }
1646 //else everything is ok.
1647 return SDValue(N, 0);
1648 */
1649}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001650
1651SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1652 DAGCombinerInfo &DCI) const {
1653 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001654 case ISD::STORE:
1655 return PerformStoreCombine(N, DCI);
1656 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001657 return PerformPIC16LoadCombine(N, DCI);
1658 }
1659 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001660}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001661
1662static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1663 switch (CC) {
1664 default: assert(0 && "Unknown condition code!");
1665 case ISD::SETNE: return PIC16CC::NE;
1666 case ISD::SETEQ: return PIC16CC::EQ;
1667 case ISD::SETGT: return PIC16CC::GT;
1668 case ISD::SETGE: return PIC16CC::GE;
1669 case ISD::SETLT: return PIC16CC::LT;
1670 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001671 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta703e2352009-06-03 13:36:44 +00001672 case ISD::SETULE: return PIC16CC::ULE;
1673 case ISD::SETUGE: return PIC16CC::UGE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001674 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001675 }
1676}
1677
1678// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1679// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1680static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1681 ISD::CondCode CC, unsigned &SPCC) {
1682 if (isa<ConstantSDNode>(RHS) &&
1683 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1684 CC == ISD::SETNE &&
1685 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1686 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1687 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1688 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1689 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1690 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1691 SDValue CMPCC = LHS.getOperand(3);
1692 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1693 LHS = CMPCC.getOperand(0);
1694 RHS = CMPCC.getOperand(1);
1695 }
1696}
1697
1698// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1699SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1700 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001701 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001702 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1703
1704 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1705 // i.e. a < 12 can be rewritten as 12 > a.
1706 if (RHS.getOpcode() == ISD::Constant) {
1707
1708 SDValue Tmp = LHS;
1709 LHS = RHS;
1710 RHS = Tmp;
1711
1712 switch (CondCode) {
1713 default: break;
1714 case PIC16CC::LT:
1715 CondCode = PIC16CC::GT;
1716 break;
1717 case PIC16CC::GT:
1718 CondCode = PIC16CC::LT;
1719 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001720 case PIC16CC::ULT:
1721 CondCode = PIC16CC::UGT;
1722 break;
1723 case PIC16CC::UGT:
1724 CondCode = PIC16CC::ULT;
1725 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001726 case PIC16CC::GE:
1727 CondCode = PIC16CC::LE;
1728 break;
1729 case PIC16CC::LE:
1730 CondCode = PIC16CC::GE;
1731 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001732 case PIC16CC::ULE:
1733 CondCode = PIC16CC::UGE;
1734 break;
1735 case PIC16CC::UGE:
1736 CondCode = PIC16CC::ULE;
1737 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001738 }
1739 }
1740
1741 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001742
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001743 // These are signed comparisons.
1744 SDValue Mask = DAG.getConstant(128, MVT::i8);
1745 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001746 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1747 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001748 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001749
1750 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001751 // We can use a subtract operation to set the condition codes. But
1752 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001753 // Nothing to do if the first operand is already a valid type (direct load
1754 // for subwf and literal for sublw) and it is used by this operation only.
1755 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1756 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001757 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001758
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001759 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001760 LHS = ConvertToMemOperand (LHS, DAG, dl);
1761 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001762}
1763
1764
1765SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1766 SDValue LHS = Op.getOperand(0);
1767 SDValue RHS = Op.getOperand(1);
1768 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1769 SDValue TrueVal = Op.getOperand(2);
1770 SDValue FalseVal = Op.getOperand(3);
1771 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001772 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001773
1774 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1775 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1776 // i.e.
1777 // A setcc: lhs, rhs, cc is expanded by llvm to
1778 // select_cc: result of setcc, 0, 1, 0, setne
1779 // We can think of it as:
1780 // select_cc: lhs, rhs, 1, 0, cc
1781 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1782 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1783
1784 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001785 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001786
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001787 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001788 FalseVal, PIC16CC, Cmp.getValue(1));
1789}
1790
1791MachineBasicBlock *
1792PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001793 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001794 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1795 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001796 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001797
1798 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1799 // control-flow pattern. The incoming instruction knows the destination vreg
1800 // to set, the condition code register to branch on, the true/false values to
1801 // select between, and a branch opcode to use.
1802 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1803 MachineFunction::iterator It = BB;
1804 ++It;
1805
1806 // thisMBB:
1807 // ...
1808 // TrueVal = ...
1809 // [f]bCC copy1MBB
1810 // fallthrough --> copy0MBB
1811 MachineBasicBlock *thisMBB = BB;
1812 MachineFunction *F = BB->getParent();
1813 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1814 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001815 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001816 F->insert(It, copy0MBB);
1817 F->insert(It, sinkMBB);
1818
1819 // Update machine-CFG edges by transferring all successors of the current
1820 // block to the new block which will contain the Phi node for the select.
1821 sinkMBB->transferSuccessors(BB);
1822 // Next, add the true and fallthrough blocks as its successors.
1823 BB->addSuccessor(copy0MBB);
1824 BB->addSuccessor(sinkMBB);
1825
1826 // copy0MBB:
1827 // %FalseValue = ...
1828 // # fallthrough to sinkMBB
1829 BB = copy0MBB;
1830
1831 // Update machine-CFG edges
1832 BB->addSuccessor(sinkMBB);
1833
1834 // sinkMBB:
1835 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1836 // ...
1837 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001838 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001839 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1840 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1841
1842 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1843 return BB;
1844}
1845
1846
1847SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1848 SDValue Chain = Op.getOperand(0);
1849 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1850 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1851 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1852 SDValue Dest = Op.getOperand(4); // BB to jump to
1853 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001854 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001855
1856 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1857 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1858 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1859 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1860
1861 // Get the Compare insn and condition code.
1862 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001863 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001864
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001865 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001866 Cmp.getValue(1));
1867}
1868