blob: 122af70546d97813f9c2ccf0a8d47828a069b874 [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);
Sanjiv Guptaae992272009-06-23 07:10:19 +0000705 if ((OperLeft.getOpcode() == ISD::Constant) &&
706 (dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue() < 32 )) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000707 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
708 Ptr = OperRight;
Sanjiv Guptaae992272009-06-23 07:10:19 +0000709 } else if ((OperRight.getOpcode() == ISD::Constant) &&
710 (dyn_cast<ConstantSDNode>(OperRight)->getZExtValue() < 32 )){
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000711 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000712 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000713 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000714 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000715
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000716 // If the pointer is Type i8 and an external symbol
717 // then treat it as direct address.
718 // One example for such case is storing and loading
719 // from function frame during a call
720 if (Ptr.getValueType() == MVT::i8) {
721 switch (Ptr.getOpcode()) {
722 case ISD::TargetExternalSymbol:
723 Lo = Ptr;
724 Hi = DAG.getConstant(1, MVT::i8);
725 return;
726 }
727 }
728
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000729 // Expansion of FrameIndex has Lo/Hi parts
730 if (isDirectAddress(Ptr)) {
731 SDValue TFI = Ptr.getOperand(0).getOperand(0);
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000732 int FrameOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000733 if (TFI.getOpcode() == ISD::TargetFrameIndex) {
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000734 LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset);
735 Hi = DAG.getConstant(1, MVT::i8);
736 Offset += FrameOffset;
737 return;
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000738 } else if (TFI.getOpcode() == ISD::TargetExternalSymbol) {
739 // FrameIndex has already been expanded.
740 // Now just make use of its expansion
741 Lo = TFI;
742 Hi = DAG.getConstant(1, MVT::i8);
743 SDValue FOffset = Ptr.getOperand(0).getOperand(1);
744 assert (FOffset.getOpcode() == ISD::Constant &&
745 "Invalid operand of PIC16ISD::Lo");
746 Offset += dyn_cast<ConstantSDNode>(FOffset)->getZExtValue();
747 return;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000748 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000749 }
750
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000751 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
752 // Direct addressing case for RAM variables. The Hi part is constant
753 // and the Lo part is the TGA itself.
754 Lo = Ptr.getOperand(0).getOperand(0);
755
756 // For direct addresses Hi is a constant. Value 1 for the constant
757 // signifies that banksel needs to generated for it. Value 0 for
758 // the constant signifies that banksel does not need to be generated
759 // for it. Mark it as 1 now and optimize later.
760 Hi = DAG.getConstant(1, MVT::i8);
761 return;
762 }
763
764 // Indirect addresses. Get the hi and lo parts of ptr.
765 GetExpandedParts(Ptr, DAG, Lo, Hi);
766
767 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000768 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
769 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000770
771 return;
772}
773
Duncan Sands1607f052008-12-01 11:39:25 +0000774SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000775 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
776 SDValue Chain = LD->getChain();
777 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000778 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000779
780 SDValue Load, Offset;
781 SDVTList Tys;
782 MVT VT, NewVT;
783 SDValue PtrLo, PtrHi;
784 unsigned LoadOffset;
785
786 // Legalize direct/indirect addresses. This will give the lo and hi parts
787 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000788 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000789
790 // Load from the pointer (direct address or FSR)
791 VT = N->getValueType(0);
792 unsigned NumLoads = VT.getSizeInBits() / 8;
793 std::vector<SDValue> PICLoads;
794 unsigned iter;
795 MVT MemVT = LD->getMemoryVT();
796 if(ISD::isNON_EXTLoad(N)) {
797 for (iter=0; iter<NumLoads ; ++iter) {
798 // Add the pointer offset if any
799 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
800 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000801 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000802 Offset);
803 PICLoads.push_back(Load);
804 }
805 } else {
806 // If it is extended load then use PIC16Load for Memory Bytes
807 // and for all extended bytes perform action based on type of
808 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
809
810
811 // For extended loads this is the memory value type
812 // i.e. without any extension
813 MVT MemVT = LD->getMemoryVT();
814 unsigned MemBytes = MemVT.getSizeInBits() / 8;
815 unsigned ExtdBytes = VT.getSizeInBits() / 8;
816 Offset = DAG.getConstant(LoadOffset, MVT::i8);
817
818 Tys = DAG.getVTList(MVT::i8, MVT::Other);
819 // For MemBytes generate PIC16Load with proper offset
820 for (iter=0; iter<MemBytes; ++iter) {
821 // Add the pointer offset if any
822 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000823 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000824 Offset);
825 PICLoads.push_back(Load);
826 }
827
828 // For SignExtendedLoad
829 if (ISD::isSEXTLoad(N)) {
830 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
831 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000832 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000833 DAG.getConstant(7, MVT::i8));
834 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
835 PICLoads.push_back(SRA);
836 }
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000837 } else if (ISD::isZEXTLoad(N) || ISD::isEXTLoad(N)) {
838 //} else if (ISD::isZEXTLoad(N)) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000839 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
840 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
841 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
842 PICLoads.push_back(ConstZero);
843 }
844 }
845 }
846 SDValue BP;
847
848 if (VT == MVT::i8) {
849 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000850 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000851 }
852 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000853 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000854 if (MemVT == MVT::i8)
855 Chain = getChain(PICLoads[0]);
856 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000857 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
858 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000859 } else if (VT == MVT::i32) {
860 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000861 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
862 PICLoads[0], PICLoads[1]);
863 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
864 PICLoads[2], PICLoads[3]);
865 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000866 if (MemVT == MVT::i8)
867 Chain = getChain(PICLoads[0]);
868 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000869 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
870 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000871 else {
872 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000873 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000874 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000875 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000876 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000877 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
878 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000879 }
880 }
881 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000882 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000883}
884
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000885SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
886 // We should have handled larger operands in type legalizer itself.
887 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
888
889 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000890 SDValue Value = N->getOperand(0);
891 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000892 PIC16ISD::PIC16Libcall CallCode;
893 switch (N->getOpcode()) {
894 case ISD::SRA:
895 CallCode = PIC16ISD::SRA_I8;
896 break;
897 case ISD::SHL:
898 CallCode = PIC16ISD::SLL_I8;
899 break;
900 case ISD::SRL:
901 CallCode = PIC16ISD::SRL_I8;
902 break;
903 default:
904 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000905 return SDValue();
906 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000907 SmallVector<SDValue, 2> Ops(2);
908 Ops[0] = Value;
909 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000910 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
911 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000912 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000913}
914
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000915void
916PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
917 SmallVectorImpl<SDValue>&Results,
918 SelectionDAG &DAG) {
919 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000920 SDValue Res;
921 unsigned i;
922 switch (Op.getOpcode()) {
923 case ISD::FORMAL_ARGUMENTS:
924 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
925 case ISD::LOAD:
926 Res = ExpandLoad(Op.getNode(), DAG); break;
927 case ISD::CALL:
928 Res = LowerCALL(Op, DAG); break;
929 default: {
930 // All other operations are handled in LowerOperation.
931 Res = LowerOperation(Op, DAG);
932 if (Res.getNode())
933 Results.push_back(Res);
934
935 return;
936 }
937 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000938
939 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000940 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000941 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000942 Results.push_back(SDValue(N, i));
943 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000944}
945
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000946SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
947 switch (Op.getOpcode()) {
948 case ISD::FORMAL_ARGUMENTS:
949 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000950 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000951 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000952 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000953 return LowerADD(Op, DAG);
954 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000955 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000956 case ISD::SUBE:
957 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000958 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000959 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000960 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000961 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000962 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000963 case ISD::SRA:
964 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000965 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000966 case ISD::OR:
967 case ISD::AND:
968 case ISD::XOR:
969 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000970 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000971 return LowerCALL(Op, DAG);
972 case ISD::RET:
973 return LowerRET(Op, DAG);
974 case ISD::BR_CC:
975 return LowerBR_CC(Op, DAG);
976 case ISD::SELECT_CC:
977 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000978 }
Dan Gohman475871a2008-07-27 21:46:04 +0000979 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000980}
981
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000982SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000983 SelectionDAG &DAG,
984 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000985 assert (Op.getValueType() == MVT::i8
986 && "illegal value type to store on stack.");
987
988 MachineFunction &MF = DAG.getMachineFunction();
989 const Function *Func = MF.getFunction();
990 const std::string FuncName = Func->getName();
991
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000992
993 // Put the value on stack.
994 // Get a stack slot index and convert to es.
995 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000996 const char *tmpName = createESName(PAN::getTempdataLabel(FuncName));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000997 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
998
999 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001000 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001001 DAG.getEntryNode(),
1002 Op, ES,
1003 DAG.getConstant (1, MVT::i8), // Banksel.
Sanjiv Guptaa3613be2009-04-10 15:10:14 +00001004 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001005 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001006
1007 // Load the value from ES.
1008 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001009 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001010 ES, DAG.getConstant (1, MVT::i8),
Sanjiv Guptaa3613be2009-04-10 15:10:14 +00001011 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001012 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001013
1014 return Load.getValue(0);
1015}
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001016
1017SDValue PIC16TargetLowering::
1018LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
1019 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1020 SelectionDAG &DAG) {
1021 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1022 unsigned NumOps = TheCall->getNumArgs();
1023 DebugLoc dl = TheCall->getDebugLoc();
1024
1025 // If call has no arguments then do nothing and return.
1026 if (NumOps == 0)
1027 return Chain;
1028
1029 std::vector<SDValue> Ops;
1030 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1031 SDValue Arg, StoreRet;
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001032
1033 // For PIC16 ABI the arguments come after the return value.
1034 unsigned RetVals = TheCall->getNumRetVals();
1035 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001036 // Get the arguments
1037 Arg = TheCall->getArg(i);
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001038
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001039 Ops.clear();
1040 Ops.push_back(Chain);
1041 Ops.push_back(Arg);
1042 Ops.push_back(DataAddr_Lo);
1043 Ops.push_back(DataAddr_Hi);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001044 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001045 Ops.push_back(InFlag);
1046
1047 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
1048
1049 Chain = getChain(StoreRet);
1050 InFlag = getOutFlag(StoreRet);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001051 ArgOffset++;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001052 }
1053 return Chain;
1054}
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001055
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001056SDValue PIC16TargetLowering::
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001057LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue ArgLabel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001058 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001059 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1060 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +00001061 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001062 std::string Name;
1063 SDValue Arg, StoreAt;
1064 MVT ArgVT;
1065 unsigned Size=0;
1066 unsigned ArgCount=0;
1067
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001068 // If call has no arguments then do nothing and return.
1069 if (NumOps == 0)
1070 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001071
1072 // FIXME: This portion of code currently assumes only
1073 // primitive types being passed as arguments.
1074
1075 // Legalize the address before use
1076 SDValue PtrLo, PtrHi;
1077 unsigned AddressOffset;
1078 int StoreOffset = 0;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001079 LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001080 SDValue StoreRet;
1081
1082 std::vector<SDValue> Ops;
1083 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1084 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
1085 // Get the argument
1086 Arg = TheCall->getArg(i);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001087 StoreOffset = (Offset + AddressOffset);
1088
1089 // Store the argument on frame
1090
1091 Ops.clear();
1092 Ops.push_back(Chain);
Sanjiv Gupta12f23a82009-04-13 09:38:38 +00001093 Ops.push_back(Arg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001094 Ops.push_back(PtrLo);
1095 Ops.push_back(PtrHi);
1096 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
1097 Ops.push_back(InFlag);
1098
Dale Johannesene8d72302009-02-06 23:05:02 +00001099 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001100
1101 Chain = getChain(StoreRet);
1102 InFlag = getOutFlag(StoreRet);
1103
1104 // Update the frame offset to be used for next argument
1105 ArgVT = Arg.getValueType();
1106 Size = ArgVT.getSizeInBits();
1107 Size = Size/8; // Calculate size in bytes
1108 Offset += Size; // Increase the frame offset
1109 }
1110 return Chain;
1111}
1112
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001113SDValue PIC16TargetLowering::
1114LowerIndirectCallReturn (SDValue Op, SDValue Chain, SDValue InFlag,
1115 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1116 SelectionDAG &DAG) {
1117 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1118 DebugLoc dl = TheCall->getDebugLoc();
1119 unsigned RetVals = TheCall->getNumRetVals();
1120
1121 // If call does not have anything to return
1122 // then do nothing and go back.
1123 if (RetVals == 0)
1124 return Chain;
1125
1126 // Call has something to return
1127 std::vector<SDValue> ResultVals;
1128 SDValue LoadRet;
1129
1130 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1131 for(unsigned i=0;i<RetVals;i++) {
1132 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
1133 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
1134 InFlag);
1135 InFlag = getOutFlag(LoadRet);
1136 Chain = getChain(LoadRet);
1137 ResultVals.push_back(LoadRet);
1138 }
1139 ResultVals.push_back(Chain);
1140 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
1141 return Res;
1142}
1143
1144SDValue PIC16TargetLowering::
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001145LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue RetLabel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001146 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001147 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001148 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001149 // Currently handling primitive types only. They will come in
1150 // i8 parts
1151 unsigned RetVals = TheCall->getNumRetVals();
1152
1153 std::vector<SDValue> ResultVals;
1154
1155 // Return immediately if the return type is void
1156 if (RetVals == 0)
1157 return Chain;
1158
1159 // Call has something to return
1160
1161 // Legalize the address before use
1162 SDValue LdLo, LdHi;
1163 unsigned LdOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001164 LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001165
1166 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1167 SDValue LoadRet;
1168
1169 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1170
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001171 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001172 DAG.getConstant(LdOffset + Offset, MVT::i8),
1173 InFlag);
1174
1175 InFlag = getOutFlag(LoadRet);
1176
1177 Chain = getChain(LoadRet);
1178 Offset++;
1179 ResultVals.push_back(LoadRet);
1180 }
1181
1182 // To return use MERGE_VALUES
1183 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001184 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001185 return Res;
1186}
1187
1188SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001189 SDValue Chain = Op.getOperand(0);
1190 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001191
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001192 if (Op.getNumOperands() == 1) // return void
1193 return Op;
1194
1195 // return should have odd number of operands
1196 if ((Op.getNumOperands() % 2) == 0 ) {
1197 assert(0 && "Do not know how to return this many arguments!");
1198 abort();
1199 }
1200
1201 // Number of values to return
1202 unsigned NumRet = (Op.getNumOperands() / 2);
1203
1204 // Function returns value always on stack with the offset starting
1205 // from 0
1206 MachineFunction &MF = DAG.getMachineFunction();
1207 const Function *F = MF.getFunction();
1208 std::string FuncName = F->getName();
1209
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001210 const char *tmpName = createESName(PAN::getFrameLabel(FuncName));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001211 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1212 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1213 SDValue BS = DAG.getConstant(1, MVT::i8);
1214 SDValue RetVal;
1215 for(unsigned i=0;i<NumRet; ++i) {
1216 RetVal = Op.getNode()->getOperand(2*i + 1);
1217 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1218 ES, BS,
1219 DAG.getConstant (i, MVT::i8));
1220
1221 }
1222 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001223}
1224
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001225// CALL node may have some operands non-legal to PIC16. Generate new CALL
1226// node with all the operands legal.
1227// Currently only Callee operand of the CALL node is non-legal. This function
1228// legalizes the Callee operand and uses all other operands as are to generate
1229// new CALL node.
1230
1231SDValue PIC16TargetLowering::LegalizeCALL(SDValue Op, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001232 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1233 SDValue Chain = TheCall->getChain();
1234 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001235 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001236 unsigned i =0;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001237
1238 assert(Callee.getValueType() == MVT::i16 &&
1239 "Don't know how to legalize this call node!!!");
1240 assert(Callee.getOpcode() == ISD::BUILD_PAIR &&
1241 "Don't know how to legalize this call node!!!");
1242
1243 if (isDirectAddress(Callee)) {
1244 // Come here for direct calls
1245 Callee = Callee.getOperand(0).getOperand(0);
1246 } else {
1247 // Come here for indirect calls
1248 SDValue Lo, Hi;
1249 // Indirect addresses. Get the hi and lo parts of ptr.
1250 GetExpandedParts(Callee, DAG, Lo, Hi);
1251 // Connect Lo and Hi parts of the callee with the PIC16Connect
1252 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1253 }
1254 std::vector<SDValue> Ops;
1255 Ops.push_back(Chain);
1256 Ops.push_back(Callee);
1257
1258 // Add the call arguments and their flags
1259 unsigned NumArgs = TheCall->getNumArgs();
1260 for(i=0;i<NumArgs;i++) {
1261 Ops.push_back(TheCall->getArg(i));
1262 Ops.push_back(TheCall->getArgFlagsVal(i));
1263 }
1264 std::vector<MVT> NodeTys;
1265 unsigned NumRets = TheCall->getNumRetVals();
1266 for(i=0;i<NumRets;i++)
1267 NodeTys.push_back(TheCall->getRetValType(i));
1268
1269 // Return a Chain as well
1270 NodeTys.push_back(MVT::Other);
1271
1272 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1273 // Generate new call with all the operands legal
1274 return DAG.getCall(TheCall->getCallingConv(), dl,
1275 TheCall->isVarArg(), TheCall->isTailCall(),
1276 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
1277}
1278
1279void PIC16TargetLowering::
1280GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1281 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1282 SelectionDAG &DAG) {
1283 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1284 && "Don't know what to do of such callee!!");
1285 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1286 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1287 Chain = getChain(SeqStart);
1288 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1289
1290 // Get the Lo and Hi part of code address
1291 SDValue Lo = Callee.getOperand(0);
1292 SDValue Hi = Callee.getOperand(1);
1293
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001294 SDValue Data_Lo, Data_Hi;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001295 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001296 // Subtract 2 from Address to get the Lower part of DataAddress.
1297 SDVTList VTList = DAG.getVTList(MVT::i8, MVT::Flag);
1298 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1299 DAG.getConstant(2, MVT::i8));
1300 SDValue Ops[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1301 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, Ops, 3);
1302 SDValue PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1303 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001304 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1305 OperFlag);
1306 Chain = getChain(Call);
1307 OperFlag = getOutFlag(Call);
1308 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1309 OperFlag);
1310 Chain = getChain(SeqEnd);
1311 OperFlag = getOutFlag(SeqEnd);
1312
1313 // Low part of Data Address
1314 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1315
1316 // Make the second call.
1317 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1318 Chain = getChain(SeqStart);
1319 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1320
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001321 // Subtract 1 from Address to get high part of data address.
1322 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1323 DAG.getConstant(1, MVT::i8));
1324 SDValue HiOps[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1325 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1326 PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1327
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001328 // Use new Lo to make another CALLW
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001329 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001330 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1331 Chain = getChain(Call);
1332 OperFlag = getOutFlag(Call);
1333 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1334 OperFlag);
1335 Chain = getChain(SeqEnd);
1336 OperFlag = getOutFlag(SeqEnd);
1337 // Hi part of Data Address
1338 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1339}
1340
1341
1342SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
1343 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1344 SDValue Chain = TheCall->getChain();
1345 SDValue Callee = TheCall->getCallee();
1346 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001347 if (Callee.getValueType() == MVT::i16 &&
1348 Callee.getOpcode() == ISD::BUILD_PAIR) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001349 // Control should come here only from TypeLegalizer for lowering
1350
1351 // Legalize the non-legal arguments of call and return the
1352 // new call with legal arguments.
1353 return LegalizeCALL(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001354 }
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001355 // Control should come here from Legalize DAG.
1356 // Here all the operands of CALL node should be legal.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001357
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001358 // If this is an indirect call then to pass the arguments
1359 // and read the return value back, we need the data address
1360 // of the function being called.
1361 // To get the data address two more calls need to be made.
1362
1363 // The flag to track if this is a direct or indirect call.
1364 bool IsDirectCall = true;
1365 unsigned RetVals = TheCall->getNumRetVals();
1366 unsigned NumArgs = TheCall->getNumArgs();
1367
1368 SDValue DataAddr_Lo, DataAddr_Hi;
1369 if (Callee.getOpcode() == PIC16ISD::PIC16Connect) {
1370 IsDirectCall = false; // This is indirect call
1371 // Read DataAddress only if we have to pass arguments or
1372 // read return value.
1373 if ((RetVals > 0) || (NumArgs > 0))
1374 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1375 }
1376
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001377 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1378
1379 // Start the call sequence.
1380 // Carring the Constant 0 along the CALLSEQSTART
1381 // because there is nothing else to carry.
1382 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1383 Chain = getChain(SeqStart);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001384 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1385 std::string Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001386
1387 // For any direct call - callee will be GlobalAddressNode or
1388 // ExternalSymbol
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001389 SDValue ArgLabel, RetLabel;
1390 if (IsDirectCall) {
1391 // Considering the GlobalAddressNode case here.
1392 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1393 GlobalValue *GV = G->getGlobal();
1394 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1395 Name = G->getGlobal()->getName();
1396 } else {// Considering the ExternalSymbol case here
1397 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1398 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1399 Name = ES->getSymbol();
1400 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001401
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001402 // Label for argument passing
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001403 const char *argFrame = createESName(PAN::getArgsLabel(Name));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001404 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001405
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001406 // Label for reading return value
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001407 const char *retName = createESName(PAN::getRetvalLabel(Name));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001408 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1409 } else {
1410 // if indirect call
1411 SDValue CodeAddr_Lo = Callee.getOperand(0);
1412 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001413
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001414 /*CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1415 DAG.getConstant(2, MVT::i8));*/
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001416
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001417 // move Hi part in PCLATH
1418 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1419 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1420 CodeAddr_Hi);
1421 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001422
1423 // Pass the argument to function before making the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001424 SDValue CallArgs;
1425 if (IsDirectCall) {
1426 CallArgs = LowerDirectCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1427 Chain = getChain(CallArgs);
1428 OperFlag = getOutFlag(CallArgs);
1429 } else {
1430 CallArgs = LowerIndirectCallArguments(Op, Chain, OperFlag, DataAddr_Lo,
1431 DataAddr_Hi, DAG);
1432 Chain = getChain(CallArgs);
1433 OperFlag = getOutFlag(CallArgs);
1434 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001435
1436 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001437 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001438 OperFlag);
1439 Chain = getChain(PICCall);
1440 OperFlag = getOutFlag(PICCall);
1441
1442
1443 // Carrying the Constant 0 along the CALLSEQSTART
1444 // because there is nothing else to carry.
1445 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1446 OperFlag);
1447 Chain = getChain(SeqEnd);
1448 OperFlag = getOutFlag(SeqEnd);
1449
1450 // Lower the return value reading after the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001451 if (IsDirectCall)
1452 return LowerDirectCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1453 else
1454 return LowerIndirectCallReturn(Op, Chain, OperFlag, DataAddr_Lo,
1455 DataAddr_Hi, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001456}
1457
1458bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1459 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1460 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1461 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1462 return true;
1463 return false;
1464}
1465
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001466// NeedToConvertToMemOp - Returns true if one of the operands of the
1467// operation 'Op' needs to be put into memory. Also returns the
1468// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1469// no instruction that can operation on two registers. Most insns take
1470// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001471bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001472 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001473 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1474 Op.getOperand(1).getOpcode() == ISD::Constant)
1475 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001476
1477 // Return false if one of the operands is already a direct
1478 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001479 if (isDirectLoad(Op.getOperand(0))) {
1480 if (Op.getOperand(0).hasOneUse())
1481 return false;
1482 else
1483 MemOp = 0;
1484 }
1485 if (isDirectLoad(Op.getOperand(1))) {
1486 if (Op.getOperand(1).hasOneUse())
1487 return false;
1488 else
1489 MemOp = 1;
1490 }
1491 return true;
1492}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001493
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001494// LowerBinOp - Lower a commutative binary operation that does not
1495// affect status flag carry.
1496SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001497 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001498
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001499 // We should have handled larger operands in type legalizer itself.
1500 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001501
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001502 unsigned MemOp = 1;
1503 if (NeedToConvertToMemOp(Op, MemOp)) {
1504 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001505 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001506
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001507 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001508 NewVal);
1509 }
1510 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001511 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001512 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001513}
1514
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001515// LowerADD - Lower all types of ADD operations including the ones
1516// that affects carry.
1517SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001518 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001519 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001520 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001521 unsigned MemOp = 1;
1522 if (NeedToConvertToMemOp(Op, MemOp)) {
1523 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001524 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001525
Dale Johannesen83138992009-06-01 23:13:42 +00001526 // ADDC and ADDE produce two results.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001527 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001528
Dale Johannesen83138992009-06-01 23:13:42 +00001529 // ADDE has three operands, the last one is the carry bit.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001530 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001531 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1532 NewVal, Op.getOperand(2));
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001533 // ADDC has two operands.
1534 else if (Op.getOpcode() == ISD::ADDC)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001535 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1536 NewVal);
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001537 // ADD it is. It produces only one result.
1538 else
1539 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1540 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001541 }
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001542 else
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001543 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001544}
1545
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001546SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001547 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001548 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001549 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001550
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001551 // Nothing to do if the first operand is already a direct load and it has
1552 // only one use.
1553 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001554 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001555
1556 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001557 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001558
1559 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001560 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001561 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001562 Op.getOperand(2));
1563 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001564 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001565}
1566
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001567void PIC16TargetLowering::InitReservedFrameCount(const Function *F) {
1568 unsigned NumArgs = F->arg_size();
1569
1570 bool isVoidFunc = (F->getReturnType()->getTypeID() == Type::VoidTyID);
1571
1572 if (isVoidFunc)
1573 ReservedFrameCount = NumArgs;
1574 else
1575 ReservedFrameCount = NumArgs + 1;
1576}
1577
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001578// LowerFORMAL_ARGUMENTS - Argument values are loaded from the
1579// <fname>.args + offset. All arguments are already broken to leaglized
1580// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001581
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001582SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001583 SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001584 SmallVector<SDValue, 8> ArgValues;
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001585 unsigned NumArgVals = Op.getNode()->getNumValues() - 1;
Dale Johannesene8d72302009-02-06 23:05:02 +00001586 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001587 SDValue Chain = Op.getOperand(0); // Formal arguments' chain
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001588
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001589
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001590 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001591 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001592 const Function *F = MF.getFunction();
1593 std::string FuncName = F->getName();
1594
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001595 // Reset the map of FI and TmpOffset
1596 ResetTmpOffsetMap();
1597 // Initialize the ReserveFrameCount
1598 InitReservedFrameCount(F);
1599
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001600 // Create the <fname>.args external symbol.
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001601 const char *tmpName = createESName(PAN::getArgsLabel(FuncName));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001602 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001603
1604 // Load arg values from the label + offset.
1605 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001606 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001607 for (unsigned i = 0; i < NumArgVals ; ++i) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001608 SDValue Offset = DAG.getConstant(i, MVT::i8);
1609 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001610 Offset);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001611 Chain = getChain(PICLoad);
1612 ArgValues.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001613 }
1614
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001615 // Return a MERGE_VALUE node.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001616 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001617 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001618 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001619}
1620
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001621// Perform DAGCombine of PIC16Load.
1622// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001623SDValue PIC16TargetLowering::
1624PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1625 SelectionDAG &DAG = DCI.DAG;
1626 SDValue Chain = N->getOperand(0);
1627 if (N->hasNUsesOfValue(0, 0)) {
1628 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1629 }
1630 return SDValue();
1631}
1632
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001633// For all the functions with arguments some STORE nodes are generated
1634// that store the argument on the frameindex. However in PIC16 the arguments
1635// are passed on stack only. Therefore these STORE nodes are redundant.
1636// To remove these STORE nodes will be removed in PerformStoreCombine
1637//
1638// Currently this function is doint nothing and will be updated for removing
1639// unwanted store operations
1640SDValue PIC16TargetLowering::
1641PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001642 return SDValue(N, 0);
1643 /*
1644 // Storing an undef value is of no use, so remove it
1645 if (isStoringUndef(N, Chain, DAG)) {
1646 return Chain; // remove the store and return the chain
1647 }
1648 //else everything is ok.
1649 return SDValue(N, 0);
1650 */
1651}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001652
1653SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1654 DAGCombinerInfo &DCI) const {
1655 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001656 case ISD::STORE:
1657 return PerformStoreCombine(N, DCI);
1658 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001659 return PerformPIC16LoadCombine(N, DCI);
1660 }
1661 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001662}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001663
1664static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1665 switch (CC) {
1666 default: assert(0 && "Unknown condition code!");
1667 case ISD::SETNE: return PIC16CC::NE;
1668 case ISD::SETEQ: return PIC16CC::EQ;
1669 case ISD::SETGT: return PIC16CC::GT;
1670 case ISD::SETGE: return PIC16CC::GE;
1671 case ISD::SETLT: return PIC16CC::LT;
1672 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001673 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta703e2352009-06-03 13:36:44 +00001674 case ISD::SETULE: return PIC16CC::ULE;
1675 case ISD::SETUGE: return PIC16CC::UGE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001676 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001677 }
1678}
1679
1680// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1681// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1682static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1683 ISD::CondCode CC, unsigned &SPCC) {
1684 if (isa<ConstantSDNode>(RHS) &&
1685 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1686 CC == ISD::SETNE &&
1687 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1688 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1689 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1690 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1691 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1692 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1693 SDValue CMPCC = LHS.getOperand(3);
1694 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1695 LHS = CMPCC.getOperand(0);
1696 RHS = CMPCC.getOperand(1);
1697 }
1698}
1699
1700// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1701SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1702 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001703 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001704 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1705
1706 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1707 // i.e. a < 12 can be rewritten as 12 > a.
1708 if (RHS.getOpcode() == ISD::Constant) {
1709
1710 SDValue Tmp = LHS;
1711 LHS = RHS;
1712 RHS = Tmp;
1713
1714 switch (CondCode) {
1715 default: break;
1716 case PIC16CC::LT:
1717 CondCode = PIC16CC::GT;
1718 break;
1719 case PIC16CC::GT:
1720 CondCode = PIC16CC::LT;
1721 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001722 case PIC16CC::ULT:
1723 CondCode = PIC16CC::UGT;
1724 break;
1725 case PIC16CC::UGT:
1726 CondCode = PIC16CC::ULT;
1727 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001728 case PIC16CC::GE:
1729 CondCode = PIC16CC::LE;
1730 break;
1731 case PIC16CC::LE:
1732 CondCode = PIC16CC::GE;
1733 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001734 case PIC16CC::ULE:
1735 CondCode = PIC16CC::UGE;
1736 break;
1737 case PIC16CC::UGE:
1738 CondCode = PIC16CC::ULE;
1739 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001740 }
1741 }
1742
1743 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001744
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001745 // These are signed comparisons.
1746 SDValue Mask = DAG.getConstant(128, MVT::i8);
1747 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001748 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1749 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001750 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001751
1752 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001753 // We can use a subtract operation to set the condition codes. But
1754 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001755 // Nothing to do if the first operand is already a valid type (direct load
1756 // for subwf and literal for sublw) and it is used by this operation only.
1757 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1758 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001759 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001760
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001761 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001762 LHS = ConvertToMemOperand (LHS, DAG, dl);
1763 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001764}
1765
1766
1767SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1768 SDValue LHS = Op.getOperand(0);
1769 SDValue RHS = Op.getOperand(1);
1770 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1771 SDValue TrueVal = Op.getOperand(2);
1772 SDValue FalseVal = Op.getOperand(3);
1773 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001774 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001775
1776 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1777 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1778 // i.e.
1779 // A setcc: lhs, rhs, cc is expanded by llvm to
1780 // select_cc: result of setcc, 0, 1, 0, setne
1781 // We can think of it as:
1782 // select_cc: lhs, rhs, 1, 0, cc
1783 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1784 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1785
1786 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001787 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001788
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001789 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001790 FalseVal, PIC16CC, Cmp.getValue(1));
1791}
1792
1793MachineBasicBlock *
1794PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001795 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001796 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1797 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001798 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001799
1800 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1801 // control-flow pattern. The incoming instruction knows the destination vreg
1802 // to set, the condition code register to branch on, the true/false values to
1803 // select between, and a branch opcode to use.
1804 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1805 MachineFunction::iterator It = BB;
1806 ++It;
1807
1808 // thisMBB:
1809 // ...
1810 // TrueVal = ...
1811 // [f]bCC copy1MBB
1812 // fallthrough --> copy0MBB
1813 MachineBasicBlock *thisMBB = BB;
1814 MachineFunction *F = BB->getParent();
1815 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1816 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001817 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001818 F->insert(It, copy0MBB);
1819 F->insert(It, sinkMBB);
1820
1821 // Update machine-CFG edges by transferring all successors of the current
1822 // block to the new block which will contain the Phi node for the select.
1823 sinkMBB->transferSuccessors(BB);
1824 // Next, add the true and fallthrough blocks as its successors.
1825 BB->addSuccessor(copy0MBB);
1826 BB->addSuccessor(sinkMBB);
1827
1828 // copy0MBB:
1829 // %FalseValue = ...
1830 // # fallthrough to sinkMBB
1831 BB = copy0MBB;
1832
1833 // Update machine-CFG edges
1834 BB->addSuccessor(sinkMBB);
1835
1836 // sinkMBB:
1837 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1838 // ...
1839 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001840 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001841 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1842 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1843
1844 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1845 return BB;
1846}
1847
1848
1849SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1850 SDValue Chain = Op.getOperand(0);
1851 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1852 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1853 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1854 SDValue Dest = Op.getOperand(4); // BB to jump to
1855 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001856 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001857
1858 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1859 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1860 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1861 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1862
1863 // Get the Compare insn and condition code.
1864 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001865 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001866
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001867 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001868 Cmp.getValue(1));
1869}
1870