blob: 8ad61aed584af3e7728d764f52c5519f31f6d2d8 [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"
Torok Edwin804e0fe2009-07-08 19:04:27 +000026#include "llvm/Support/ErrorHandling.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000027
28
Sanjiv Gupta0e687712008-05-13 09:02:57 +000029using namespace llvm;
30
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000031static const char *getIntrinsicName(unsigned opcode) {
32 std::string Basename;
33 switch(opcode) {
34 default: assert (0 && "do not know intrinsic name");
Sanjiv Gupta003263b2009-06-16 16:17:35 +000035 // Arithmetic Right shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000036 case PIC16ISD::SRA_I8: Basename = "sra.i8"; break;
37 case RTLIB::SRA_I16: Basename = "sra.i16"; break;
38 case RTLIB::SRA_I32: Basename = "sra.i32"; break;
39
Sanjiv Gupta003263b2009-06-16 16:17:35 +000040 // Left shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000041 case PIC16ISD::SLL_I8: Basename = "sll.i8"; break;
42 case RTLIB::SHL_I16: Basename = "sll.i16"; break;
43 case RTLIB::SHL_I32: Basename = "sll.i32"; break;
44
Sanjiv Gupta003263b2009-06-16 16:17:35 +000045 // Logical Right Shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000046 case PIC16ISD::SRL_I8: Basename = "srl.i8"; break;
47 case RTLIB::SRL_I16: Basename = "srl.i16"; break;
48 case RTLIB::SRL_I32: Basename = "srl.i32"; break;
49
Sanjiv Gupta003263b2009-06-16 16:17:35 +000050 // Multiply for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000051 case PIC16ISD::MUL_I8: Basename = "mul.i8"; break;
52 case RTLIB::MUL_I16: Basename = "mul.i16"; break;
53 case RTLIB::MUL_I32: Basename = "mul.i32"; break;
Sanjiv Gupta46800772009-06-05 14:43:12 +000054
Sanjiv Gupta003263b2009-06-16 16:17:35 +000055 // Signed division for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000056 case RTLIB::SDIV_I16: Basename = "sdiv.i16"; break;
57 case RTLIB::SDIV_I32: Basename = "sdiv.i32"; break;
Sanjiv Gupta003263b2009-06-16 16:17:35 +000058
59 // Unsigned division for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000060 case RTLIB::UDIV_I16: Basename = "udiv.i16"; break;
61 case RTLIB::UDIV_I32: Basename = "udiv.i32"; break;
62
Sanjiv Gupta003263b2009-06-16 16:17:35 +000063 // Signed Modulas for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000064 case RTLIB::SREM_I16: Basename = "srem.i16"; break;
65 case RTLIB::SREM_I32: Basename = "srem.i32"; break;
Sanjiv Gupta003263b2009-06-16 16:17:35 +000066
67 // Unsigned Modulas for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000068 case RTLIB::UREM_I16: Basename = "urem.i16"; break;
69 case RTLIB::UREM_I32: Basename = "urem.i32"; break;
Sanjiv Gupta63963292009-06-11 16:50:48 +000070
Sanjiv Gupta003263b2009-06-16 16:17:35 +000071 //////////////////////
72 // LIBCALLS FOR FLOATS
73 //////////////////////
74
75 // Float to signed integrals
76 case RTLIB::FPTOSINT_F32_I8: Basename = "f32_to_si32"; break;
77 case RTLIB::FPTOSINT_F32_I16: Basename = "f32_to_si32"; break;
78 case RTLIB::FPTOSINT_F32_I32: Basename = "f32_to_si32"; break;
79
80 // Signed integrals to float. char and int are first sign extended to i32
81 // before being converted to float, so an I8_F32 or I16_F32 isn't required.
82 case RTLIB::SINTTOFP_I32_F32: Basename = "si32_to_f32"; break;
83
84 // Float to Unsigned conversions.
85 // Signed conversion can be used for unsigned conversion as well.
86 // In signed and unsigned versions only the interpretation of the
87 // MSB is different. Bit representation remains the same.
88 case RTLIB::FPTOUINT_F32_I8: Basename = "f32_to_si32"; break;
89 case RTLIB::FPTOUINT_F32_I16: Basename = "f32_to_si32"; break;
90 case RTLIB::FPTOUINT_F32_I32: Basename = "f32_to_si32"; break;
91
92 // Unsigned to Float conversions. char and int are first zero extended
93 // before being converted to float.
94 case RTLIB::UINTTOFP_I32_F32: Basename = "ui32_to_f32"; break;
Sanjiv Gupta63963292009-06-11 16:50:48 +000095
Sanjiv Gupta003263b2009-06-16 16:17:35 +000096 // Floating point add, sub, mul, div.
Sanjiv Gupta63963292009-06-11 16:50:48 +000097 case RTLIB::ADD_F32: Basename = "add.f32"; break;
98 case RTLIB::SUB_F32: Basename = "sub.f32"; break;
99 case RTLIB::MUL_F32: Basename = "mul.f32"; break;
100 case RTLIB::DIV_F32: Basename = "div.f32"; break;
Sanjiv Guptacbd5f0a2009-06-25 18:12:06 +0000101
102 // Floating point comparison
103 case RTLIB::O_F32: Basename = "unordered.f32"; break;
104 case RTLIB::UO_F32: Basename = "unordered.f32"; break;
105 case RTLIB::OLE_F32: Basename = "le.f32"; break;
106 case RTLIB::OGE_F32: Basename = "ge.f32"; break;
107 case RTLIB::OLT_F32: Basename = "lt.f32"; break;
108 case RTLIB::OGT_F32: Basename = "gt.f32"; break;
109 case RTLIB::OEQ_F32: Basename = "eq.f32"; break;
110 case RTLIB::UNE_F32: Basename = "neq.f32"; break;
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000111 }
112
113 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
114 std::string tagname = PAN::getTagName(PAN::LIBCALL);
115 std::string Fullname = prefix + tagname + Basename;
116
117 // The name has to live through program life.
118 char *tmp = new char[Fullname.size() + 1];
119 strcpy (tmp, Fullname.c_str());
120
121 return tmp;
122}
123
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000124// PIC16TargetLowering Constructor.
125PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000126 : TargetLowering(TM), TmpSize(0) {
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000127
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000128 Subtarget = &TM.getSubtarget<PIC16Subtarget>();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000129
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000130 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000131
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000132 setShiftAmountType(MVT::i8);
133 setShiftAmountFlavor(Extend);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000134
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000135 // SRA library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000136 setPIC16LibcallName(PIC16ISD::SRA_I8, getIntrinsicName(PIC16ISD::SRA_I8));
137 setLibcallName(RTLIB::SRA_I16, getIntrinsicName(RTLIB::SRA_I16));
138 setLibcallName(RTLIB::SRA_I32, getIntrinsicName(RTLIB::SRA_I32));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000139
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000140 // SHL library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000141 setPIC16LibcallName(PIC16ISD::SLL_I8, getIntrinsicName(PIC16ISD::SLL_I8));
142 setLibcallName(RTLIB::SHL_I16, getIntrinsicName(RTLIB::SHL_I16));
143 setLibcallName(RTLIB::SHL_I32, getIntrinsicName(RTLIB::SHL_I32));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000144
145 // SRL library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000146 setPIC16LibcallName(PIC16ISD::SRL_I8, getIntrinsicName(PIC16ISD::SRL_I8));
147 setLibcallName(RTLIB::SRL_I16, getIntrinsicName(RTLIB::SRL_I16));
148 setLibcallName(RTLIB::SRL_I32, getIntrinsicName(RTLIB::SRL_I32));
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000149
150 // MUL Library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000151 setPIC16LibcallName(PIC16ISD::MUL_I8, getIntrinsicName(PIC16ISD::MUL_I8));
152 setLibcallName(RTLIB::MUL_I16, getIntrinsicName(RTLIB::MUL_I16));
153 setLibcallName(RTLIB::MUL_I32, getIntrinsicName(RTLIB::MUL_I32));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000154
Sanjiv Gupta46800772009-06-05 14:43:12 +0000155 // Signed division lib call names
156 setLibcallName(RTLIB::SDIV_I16, getIntrinsicName(RTLIB::SDIV_I16));
157 setLibcallName(RTLIB::SDIV_I32, getIntrinsicName(RTLIB::SDIV_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000158
Sanjiv Gupta46800772009-06-05 14:43:12 +0000159 // Unsigned division lib call names
160 setLibcallName(RTLIB::UDIV_I16, getIntrinsicName(RTLIB::UDIV_I16));
161 setLibcallName(RTLIB::UDIV_I32, getIntrinsicName(RTLIB::UDIV_I32));
162
163 // Signed remainder lib call names
164 setLibcallName(RTLIB::SREM_I16, getIntrinsicName(RTLIB::SREM_I16));
165 setLibcallName(RTLIB::SREM_I32, getIntrinsicName(RTLIB::SREM_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000166
Sanjiv Gupta46800772009-06-05 14:43:12 +0000167 // Unsigned remainder lib call names
168 setLibcallName(RTLIB::UREM_I16, getIntrinsicName(RTLIB::UREM_I16));
169 setLibcallName(RTLIB::UREM_I32, getIntrinsicName(RTLIB::UREM_I32));
Sanjiv Gupta63963292009-06-11 16:50:48 +0000170
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000171 // Floating point to signed int conversions.
172 setLibcallName(RTLIB::FPTOSINT_F32_I8,
173 getIntrinsicName(RTLIB::FPTOSINT_F32_I8));
174 setLibcallName(RTLIB::FPTOSINT_F32_I16,
175 getIntrinsicName(RTLIB::FPTOSINT_F32_I16));
Sanjiv Gupta63963292009-06-11 16:50:48 +0000176 setLibcallName(RTLIB::FPTOSINT_F32_I32,
177 getIntrinsicName(RTLIB::FPTOSINT_F32_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000178
179 // Signed int to floats.
Sanjiv Gupta63963292009-06-11 16:50:48 +0000180 setLibcallName(RTLIB::SINTTOFP_I32_F32,
181 getIntrinsicName(RTLIB::SINTTOFP_I32_F32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000182
183 // Floating points to unsigned ints.
184 setLibcallName(RTLIB::FPTOUINT_F32_I8,
185 getIntrinsicName(RTLIB::FPTOUINT_F32_I8));
186 setLibcallName(RTLIB::FPTOUINT_F32_I16,
187 getIntrinsicName(RTLIB::FPTOUINT_F32_I16));
188 setLibcallName(RTLIB::FPTOUINT_F32_I32,
189 getIntrinsicName(RTLIB::FPTOUINT_F32_I32));
190
191 // Unsigned int to floats.
192 setLibcallName(RTLIB::UINTTOFP_I32_F32,
193 getIntrinsicName(RTLIB::UINTTOFP_I32_F32));
194
195 // Floating point add, sub, mul ,div.
Sanjiv Gupta63963292009-06-11 16:50:48 +0000196 setLibcallName(RTLIB::ADD_F32, getIntrinsicName(RTLIB::ADD_F32));
197 setLibcallName(RTLIB::SUB_F32, getIntrinsicName(RTLIB::SUB_F32));
198 setLibcallName(RTLIB::MUL_F32, getIntrinsicName(RTLIB::MUL_F32));
199 setLibcallName(RTLIB::DIV_F32, getIntrinsicName(RTLIB::DIV_F32));
200
Sanjiv Guptacbd5f0a2009-06-25 18:12:06 +0000201 // Floationg point comparison
202 setLibcallName(RTLIB::UO_F32, getIntrinsicName(RTLIB::UO_F32));
203 setLibcallName(RTLIB::OLE_F32, getIntrinsicName(RTLIB::OLE_F32));
204 setLibcallName(RTLIB::OGE_F32, getIntrinsicName(RTLIB::OGE_F32));
205 setLibcallName(RTLIB::OLT_F32, getIntrinsicName(RTLIB::OLT_F32));
206 setLibcallName(RTLIB::OGT_F32, getIntrinsicName(RTLIB::OGT_F32));
207 setLibcallName(RTLIB::OEQ_F32, getIntrinsicName(RTLIB::OEQ_F32));
208 setLibcallName(RTLIB::UNE_F32, getIntrinsicName(RTLIB::UNE_F32));
209
210 // Return value comparisons of floating point calls.
211 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
219
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000220 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000221 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000222
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000223 setOperationAction(ISD::LOAD, MVT::i8, Legal);
224 setOperationAction(ISD::LOAD, MVT::i16, Custom);
225 setOperationAction(ISD::LOAD, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000226
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000227 setOperationAction(ISD::STORE, MVT::i8, Legal);
228 setOperationAction(ISD::STORE, MVT::i16, Custom);
229 setOperationAction(ISD::STORE, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000230
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000231 setOperationAction(ISD::ADDE, MVT::i8, Custom);
232 setOperationAction(ISD::ADDC, MVT::i8, Custom);
233 setOperationAction(ISD::SUBE, MVT::i8, Custom);
234 setOperationAction(ISD::SUBC, MVT::i8, Custom);
Sanjiv Gupta3b0a4f12009-06-04 08:52:28 +0000235 setOperationAction(ISD::SUB, MVT::i8, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000236 setOperationAction(ISD::ADD, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000237 setOperationAction(ISD::ADD, MVT::i16, Custom);
238
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000239 setOperationAction(ISD::OR, MVT::i8, Custom);
240 setOperationAction(ISD::AND, MVT::i8, Custom);
241 setOperationAction(ISD::XOR, MVT::i8, Custom);
242
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000243 setOperationAction(ISD::FrameIndex, MVT::i16, Custom);
244 setOperationAction(ISD::CALL, MVT::i16, Custom);
245 setOperationAction(ISD::RET, MVT::Other, Custom);
246
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000247 setOperationAction(ISD::MUL, MVT::i8, Custom);
248 setOperationAction(ISD::MUL, MVT::i16, Expand);
249 setOperationAction(ISD::MUL, MVT::i32, Expand);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000250
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000251 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
252 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
253 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
254 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
255 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
256 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
257 setOperationAction(ISD::MULHU, MVT::i8, Expand);
258 setOperationAction(ISD::MULHU, MVT::i16, Expand);
259 setOperationAction(ISD::MULHU, MVT::i32, Expand);
260 setOperationAction(ISD::MULHS, MVT::i8, Expand);
261 setOperationAction(ISD::MULHS, MVT::i16, Expand);
262 setOperationAction(ISD::MULHS, MVT::i32, Expand);
263
264 setOperationAction(ISD::SRA, MVT::i8, Custom);
265 setOperationAction(ISD::SRA, MVT::i16, Expand);
266 setOperationAction(ISD::SRA, MVT::i32, Expand);
267 setOperationAction(ISD::SHL, MVT::i8, Custom);
268 setOperationAction(ISD::SHL, MVT::i16, Expand);
269 setOperationAction(ISD::SHL, MVT::i32, Expand);
270 setOperationAction(ISD::SRL, MVT::i8, Custom);
271 setOperationAction(ISD::SRL, MVT::i16, Expand);
272 setOperationAction(ISD::SRL, MVT::i32, Expand);
273
274 // PIC16 does not support shift parts
275 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
276 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
277 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
278 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
279 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
280 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
281 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
282 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
283 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
284
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000285
286 // PIC16 does not have a SETCC, expand it to SELECT_CC.
287 setOperationAction(ISD::SETCC, MVT::i8, Expand);
288 setOperationAction(ISD::SELECT, MVT::i8, Expand);
289 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
290 setOperationAction(ISD::BRIND, MVT::Other, Expand);
291
292 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
293 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000294
295 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
296 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
297
298 // Now deduce the information based on the above mentioned
299 // actions
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000300 computeRegisterProperties();
301}
302
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000303// getOutFlag - Extract the flag result if the Op has it.
304static SDValue getOutFlag(SDValue &Op) {
305 // Flag is the last value of the node.
306 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
307
308 assert (Flag.getValueType() == MVT::Flag
309 && "Node does not have an out Flag");
310
311 return Flag;
312}
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000313// Get the TmpOffset for FrameIndex
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000314unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI, unsigned size) {
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000315 std::map<unsigned, unsigned>::iterator
316 MapIt = FiTmpOffsetMap.find(FI);
317 if (MapIt != FiTmpOffsetMap.end())
318 return MapIt->second;
319
320 // This FI (FrameIndex) is not yet mapped, so map it
321 FiTmpOffsetMap[FI] = TmpSize;
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000322 TmpSize += size;
323 return FiTmpOffsetMap[FI];
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000324}
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000325
326// To extract chain value from the SDValue Nodes
327// This function will help to maintain the chain extracting
328// code at one place. In case of any change in future it will
329// help maintain the code.
330static SDValue getChain(SDValue &Op) {
331 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
332
333 // If the last value returned in Flag then the chain is
334 // second last value returned.
335 if (Chain.getValueType() == MVT::Flag)
336 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
337
338 // All nodes may not produce a chain. Therefore following assert
339 // verifies that the node is returning a chain only.
340 assert (Chain.getValueType() == MVT::Other
341 && "Node does not have a chain");
342
343 return Chain;
344}
345
346/// PopulateResults - Helper function to LowerOperation.
347/// If a node wants to return multiple results after lowering,
348/// it stuffs them into an array of SDValue called Results.
349
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000350static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
351 if (N.getOpcode() == ISD::MERGE_VALUES) {
352 int NumResults = N.getNumOperands();
353 for( int i = 0; i < NumResults; i++)
354 Results.push_back(N.getOperand(i));
355 }
356 else
357 Results.push_back(N);
358}
359
360MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
361 return MVT::i8;
362}
363
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000364/// The type legalizer framework of generating legalizer can generate libcalls
365/// only when the operand/result types are illegal.
366/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
367/// For example an arithmetic right shift. These functions are used to lower
368/// such operations that generate libcall for legal types.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000369
370void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000371PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000372 const char *Name) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000373 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000374}
375
376const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000377PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000378 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000379}
380
381SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000382PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000383 MVT RetVT, const SDValue *Ops,
384 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000385 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000386
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000387 TargetLowering::ArgListTy Args;
388 Args.reserve(NumOps);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000389
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000390 TargetLowering::ArgListEntry Entry;
391 for (unsigned i = 0; i != NumOps; ++i) {
392 Entry.Node = Ops[i];
393 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
394 Entry.isSExt = isSigned;
395 Entry.isZExt = !isSigned;
396 Args.push_back(Entry);
397 }
398 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000399
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000400 const Type *RetTy = RetVT.getTypeForMVT();
401 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000402 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +0000403 false, 0, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000404
405 return CallInfo.first;
406}
407
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000408const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
409 switch (Opcode) {
410 default: return NULL;
411 case PIC16ISD::Lo: return "PIC16ISD::Lo";
412 case PIC16ISD::Hi: return "PIC16ISD::Hi";
413 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
414 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000415 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
416 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000417 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
418 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000419 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000420 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000421 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000422 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000423 case PIC16ISD::BCF: return "PIC16ISD::BCF";
424 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
425 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
426 case PIC16ISD::RLF: return "PIC16ISD::RLF";
427 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000428 case PIC16ISD::CALL: return "PIC16ISD::CALL";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000429 case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000430 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
431 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
432 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000433 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000434 }
435}
436
Duncan Sands1607f052008-12-01 11:39:25 +0000437void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
438 SmallVectorImpl<SDValue>&Results,
439 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000440
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000441 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000442 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000443 Results.push_back(ExpandGlobalAddress(N, DAG));
444 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000445 case ISD::ExternalSymbol:
446 Results.push_back(ExpandExternalSymbol(N, DAG));
447 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000448 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000449 Results.push_back(ExpandStore(N, DAG));
450 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000451 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000452 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000453 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000454 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000455 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000456 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000457 case ISD::FrameIndex:
458 Results.push_back(ExpandFrameIndex(N, DAG));
459 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000460 default:
461 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000462 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000463 }
464}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000465
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000466SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
467
468 // Currently handling FrameIndex of size MVT::i16 only
469 // One example of this scenario is when return value is written on
470 // FrameIndex#0
471
472 if (N->getValueType(0) != MVT::i16)
473 return SDValue();
474
475 // Expand the FrameIndex into ExternalSymbol and a Constant node
476 // The constant will represent the frame index number
477 // Get the current function frame
478 MachineFunction &MF = DAG.getMachineFunction();
479 const Function *Func = MF.getFunction();
480 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000481
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000482 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000483 // FIXME there isn't really debug info here
484 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000485
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000486 // Expand FrameIndex like GlobalAddress and ExternalSymbol
487 // Also use Offset field for lo and hi parts. The default
488 // offset is zero.
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000489
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000490 SDValue ES;
491 int FrameOffset;
492 SDValue FI = SDValue(N,0);
493 LegalizeFrameIndex(FI, DAG, ES, FrameOffset);
494 SDValue Offset = DAG.getConstant(FrameOffset, MVT::i8);
495 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, ES, Offset);
496 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, ES, Offset);
497 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000498}
499
500
501SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000502 StoreSDNode *St = cast<StoreSDNode>(N);
503 SDValue Chain = St->getChain();
504 SDValue Src = St->getValue();
505 SDValue Ptr = St->getBasePtr();
506 MVT ValueType = Src.getValueType();
507 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000508 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000509
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000510 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000511 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000512
513 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000514 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000515 PtrLo, PtrHi,
516 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000517 }
518 else if (ValueType == MVT::i16) {
519 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
520 SDValue SrcLo, SrcHi;
521 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
522 SDValue ChainLo = Chain, ChainHi = Chain;
523 if (Chain.getOpcode() == ISD::TokenFactor) {
524 ChainLo = Chain.getOperand(0);
525 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000526 }
Dale Johannesende064702009-02-06 21:50:26 +0000527 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000528 ChainLo,
529 SrcLo, PtrLo, PtrHi,
530 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000531
Dale Johannesende064702009-02-06 21:50:26 +0000532 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000533 SrcHi, PtrLo, PtrHi,
534 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000535
Dale Johannesende064702009-02-06 21:50:26 +0000536 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000537 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000538 }
539 else if (ValueType == MVT::i32) {
540 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
541 SDValue SrcLo, SrcHi;
542 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000543
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000544 // Get the expanded parts of each of SrcLo and SrcHi.
545 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
546 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
547 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000548
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000549 SDValue ChainLo = Chain, ChainHi = Chain;
550 if (Chain.getOpcode() == ISD::TokenFactor) {
551 ChainLo = Chain.getOperand(0);
552 ChainHi = Chain.getOperand(1);
553 }
554 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
555 ChainHi2 = ChainHi;
556 if (ChainLo.getOpcode() == ISD::TokenFactor) {
557 ChainLo1 = ChainLo.getOperand(0);
558 ChainLo2 = ChainLo.getOperand(1);
559 }
560 if (ChainHi.getOpcode() == ISD::TokenFactor) {
561 ChainHi1 = ChainHi.getOperand(0);
562 ChainHi2 = ChainHi.getOperand(1);
563 }
Dale Johannesende064702009-02-06 21:50:26 +0000564 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000565 ChainLo1,
566 SrcLo1, PtrLo, PtrHi,
567 DAG.getConstant (0 + StoreOffset, MVT::i8));
568
Dale Johannesende064702009-02-06 21:50:26 +0000569 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000570 SrcLo2, PtrLo, PtrHi,
571 DAG.getConstant (1 + StoreOffset, MVT::i8));
572
Dale Johannesende064702009-02-06 21:50:26 +0000573 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000574 SrcHi1, PtrLo, PtrHi,
575 DAG.getConstant (2 + StoreOffset, MVT::i8));
576
Dale Johannesende064702009-02-06 21:50:26 +0000577 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000578 SrcHi2, PtrLo, PtrHi,
579 DAG.getConstant (3 + StoreOffset, MVT::i8));
580
Dale Johannesende064702009-02-06 21:50:26 +0000581 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
582 getChain(Store1), getChain(Store2));
583 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
584 getChain(Store3), getChain(Store4));
585 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000586
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000587 }
588 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000589 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000590 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000591 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000592}
593
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000594SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
595{
596 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000597 // FIXME there isn't really debug info here
598 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000599
600 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000601 SDValue Offset = DAG.getConstant(0, MVT::i8);
602 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES, Offset);
603 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES, Offset);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000604
Dale Johannesende064702009-02-06 21:50:26 +0000605 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000606}
607
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000608// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000609SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000610 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000611 // FIXME there isn't really debug info here
612 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000613
614 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
615 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000616
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000617 SDValue Offset = DAG.getConstant(0, MVT::i8);
618 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA, Offset);
619 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA, Offset);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000620
Dale Johannesende064702009-02-06 21:50:26 +0000621 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000622}
623
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000624bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
625 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000626
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000627 if (Op.getOpcode() == ISD::BUILD_PAIR) {
628 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
629 return true;
630 }
631 return false;
632}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000633
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000634// Return true if DirectAddress is in ROM_SPACE
635bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000636
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000637 // RomAddress is a GlobalAddress in ROM_SPACE_
638 // If the Op is not a GlobalAddress return NULL without checking
639 // anything further.
640 if (!isDirectAddress(Op))
641 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000642
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000643 // Its a GlobalAddress.
644 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
645 SDValue TGA = Op.getOperand(0).getOperand(0);
646 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000647
Sanjiv Guptaa3518a12009-04-29 04:43:24 +0000648 if (GSDN->getAddressSpace() == PIC16ISD::ROM_SPACE)
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000649 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000650
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000651 // Any other address space return it false
652 return false;
653}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000654
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000655
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000656// GetExpandedParts - This function is on the similiar lines as
657// the GetExpandedInteger in type legalizer is. This returns expanded
658// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000659
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000660void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
661 SDValue &Lo, SDValue &Hi) {
662 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000663 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000664 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000665
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000666 // Extract the lo component.
667 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
668 DAG.getConstant(0, MVT::i8));
Bill Wendling51b16f42009-05-30 01:09:53 +0000669
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000670 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000671 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
672 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000673}
674
675// Legalize FrameIndex into ExternalSymbol and offset.
676void
677PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
678 SDValue &ES, int &Offset) {
679
680 MachineFunction &MF = DAG.getMachineFunction();
681 const Function *Func = MF.getFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000682 MachineFrameInfo *MFI = MF.getFrameInfo();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000683 const std::string Name = Func->getName();
684
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000685 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000686
687 // FrameIndices are not stack offsets. But they represent the request
688 // for space on stack. That space requested may be more than one byte.
689 // Therefore, to calculate the stack offset that a FrameIndex aligns
690 // with, we need to traverse all the FrameIndices available earlier in
691 // the list and add their requested size.
692 unsigned FIndex = FR->getIndex();
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000693 const char *tmpName;
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000694 if (FIndex < ReservedFrameCount) {
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000695 tmpName = createESName(PAN::getFrameLabel(Name));
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000696 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
697 Offset = 0;
698 for (unsigned i=0; i<FIndex ; ++i) {
699 Offset += MFI->getObjectSize(i);
700 }
701 } else {
702 // FrameIndex has been made for some temporary storage
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000703 tmpName = createESName(PAN::getTempdataLabel(Name));
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000704 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
705 Offset = GetTmpOffsetForFI(FIndex, MFI->getObjectSize(FIndex));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000706 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000707
708 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000709}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000710
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000711// This function legalizes the PIC16 Addresses. If the Pointer is
712// -- Direct address variable residing
713// --> then a Banksel for that variable will be created.
714// -- Rom variable
715// --> then it will be treated as an indirect address.
716// -- Indirect address
717// --> then the address will be loaded into FSR
718// -- ADD with constant operand
719// --> then constant operand of ADD will be returned as Offset
720// and non-constant operand of ADD will be treated as pointer.
721// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000722
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000723void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
724 SDValue &Lo, SDValue &Hi,
725 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000726
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000727 // Offset, by default, should be 0
728 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000729
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000730 // If the pointer is ADD with constant,
731 // return the constant value as the offset
732 if (Ptr.getOpcode() == ISD::ADD) {
733 SDValue OperLeft = Ptr.getOperand(0);
734 SDValue OperRight = Ptr.getOperand(1);
Sanjiv Guptaae992272009-06-23 07:10:19 +0000735 if ((OperLeft.getOpcode() == ISD::Constant) &&
736 (dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue() < 32 )) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000737 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
738 Ptr = OperRight;
Sanjiv Guptaae992272009-06-23 07:10:19 +0000739 } else if ((OperRight.getOpcode() == ISD::Constant) &&
740 (dyn_cast<ConstantSDNode>(OperRight)->getZExtValue() < 32 )){
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000741 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000742 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000743 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000744 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000745
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000746 // If the pointer is Type i8 and an external symbol
747 // then treat it as direct address.
748 // One example for such case is storing and loading
749 // from function frame during a call
750 if (Ptr.getValueType() == MVT::i8) {
751 switch (Ptr.getOpcode()) {
752 case ISD::TargetExternalSymbol:
753 Lo = Ptr;
754 Hi = DAG.getConstant(1, MVT::i8);
755 return;
756 }
757 }
758
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000759 // Expansion of FrameIndex has Lo/Hi parts
760 if (isDirectAddress(Ptr)) {
761 SDValue TFI = Ptr.getOperand(0).getOperand(0);
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000762 int FrameOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000763 if (TFI.getOpcode() == ISD::TargetFrameIndex) {
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000764 LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset);
765 Hi = DAG.getConstant(1, MVT::i8);
766 Offset += FrameOffset;
767 return;
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000768 } else if (TFI.getOpcode() == ISD::TargetExternalSymbol) {
769 // FrameIndex has already been expanded.
770 // Now just make use of its expansion
771 Lo = TFI;
772 Hi = DAG.getConstant(1, MVT::i8);
773 SDValue FOffset = Ptr.getOperand(0).getOperand(1);
774 assert (FOffset.getOpcode() == ISD::Constant &&
775 "Invalid operand of PIC16ISD::Lo");
776 Offset += dyn_cast<ConstantSDNode>(FOffset)->getZExtValue();
777 return;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000778 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000779 }
780
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000781 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
782 // Direct addressing case for RAM variables. The Hi part is constant
783 // and the Lo part is the TGA itself.
784 Lo = Ptr.getOperand(0).getOperand(0);
785
786 // For direct addresses Hi is a constant. Value 1 for the constant
787 // signifies that banksel needs to generated for it. Value 0 for
788 // the constant signifies that banksel does not need to be generated
789 // for it. Mark it as 1 now and optimize later.
790 Hi = DAG.getConstant(1, MVT::i8);
791 return;
792 }
793
794 // Indirect addresses. Get the hi and lo parts of ptr.
795 GetExpandedParts(Ptr, DAG, Lo, Hi);
796
797 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000798 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
799 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000800
801 return;
802}
803
Duncan Sands1607f052008-12-01 11:39:25 +0000804SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000805 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
806 SDValue Chain = LD->getChain();
807 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000808 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000809
810 SDValue Load, Offset;
811 SDVTList Tys;
812 MVT VT, NewVT;
813 SDValue PtrLo, PtrHi;
814 unsigned LoadOffset;
815
816 // Legalize direct/indirect addresses. This will give the lo and hi parts
817 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000818 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000819
820 // Load from the pointer (direct address or FSR)
821 VT = N->getValueType(0);
822 unsigned NumLoads = VT.getSizeInBits() / 8;
823 std::vector<SDValue> PICLoads;
824 unsigned iter;
825 MVT MemVT = LD->getMemoryVT();
826 if(ISD::isNON_EXTLoad(N)) {
827 for (iter=0; iter<NumLoads ; ++iter) {
828 // Add the pointer offset if any
829 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
830 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000831 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000832 Offset);
833 PICLoads.push_back(Load);
834 }
835 } else {
836 // If it is extended load then use PIC16Load for Memory Bytes
837 // and for all extended bytes perform action based on type of
838 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
839
840
841 // For extended loads this is the memory value type
842 // i.e. without any extension
843 MVT MemVT = LD->getMemoryVT();
844 unsigned MemBytes = MemVT.getSizeInBits() / 8;
Sanjiv Gupta1d2e6ed2009-07-03 07:58:59 +0000845 // if MVT::i1 is extended to MVT::i8 then MemBytes will be zero
846 // So set it to one
847 if (MemBytes == 0) MemBytes = 1;
848
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000849 unsigned ExtdBytes = VT.getSizeInBits() / 8;
850 Offset = DAG.getConstant(LoadOffset, MVT::i8);
851
852 Tys = DAG.getVTList(MVT::i8, MVT::Other);
853 // For MemBytes generate PIC16Load with proper offset
Sanjiv Gupta1d2e6ed2009-07-03 07:58:59 +0000854 for (iter=0; iter < MemBytes; ++iter) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000855 // Add the pointer offset if any
856 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000857 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000858 Offset);
859 PICLoads.push_back(Load);
860 }
861
862 // For SignExtendedLoad
863 if (ISD::isSEXTLoad(N)) {
864 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
865 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000866 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000867 DAG.getConstant(7, MVT::i8));
868 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
869 PICLoads.push_back(SRA);
870 }
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000871 } else if (ISD::isZEXTLoad(N) || ISD::isEXTLoad(N)) {
872 //} else if (ISD::isZEXTLoad(N)) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000873 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
874 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
875 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
876 PICLoads.push_back(ConstZero);
877 }
878 }
879 }
880 SDValue BP;
881
882 if (VT == MVT::i8) {
883 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000884 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000885 }
886 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000887 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000888 if (MemVT == MVT::i8)
889 Chain = getChain(PICLoads[0]);
890 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000891 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
892 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000893 } else if (VT == MVT::i32) {
894 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000895 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
896 PICLoads[0], PICLoads[1]);
897 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
898 PICLoads[2], PICLoads[3]);
899 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000900 if (MemVT == MVT::i8)
901 Chain = getChain(PICLoads[0]);
902 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000903 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
904 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000905 else {
906 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000907 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000908 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000909 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000910 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000911 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
912 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000913 }
914 }
915 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000916 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000917}
918
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000919SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
920 // We should have handled larger operands in type legalizer itself.
921 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
922
923 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000924 SDValue Value = N->getOperand(0);
925 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000926 PIC16ISD::PIC16Libcall CallCode;
927 switch (N->getOpcode()) {
928 case ISD::SRA:
929 CallCode = PIC16ISD::SRA_I8;
930 break;
931 case ISD::SHL:
932 CallCode = PIC16ISD::SLL_I8;
933 break;
934 case ISD::SRL:
935 CallCode = PIC16ISD::SRL_I8;
936 break;
937 default:
938 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000939 return SDValue();
940 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000941 SmallVector<SDValue, 2> Ops(2);
942 Ops[0] = Value;
943 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000944 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
945 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000946 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000947}
948
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000949void
950PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
951 SmallVectorImpl<SDValue>&Results,
952 SelectionDAG &DAG) {
953 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000954 SDValue Res;
955 unsigned i;
956 switch (Op.getOpcode()) {
957 case ISD::FORMAL_ARGUMENTS:
958 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
959 case ISD::LOAD:
960 Res = ExpandLoad(Op.getNode(), DAG); break;
961 case ISD::CALL:
962 Res = LowerCALL(Op, DAG); break;
963 default: {
964 // All other operations are handled in LowerOperation.
965 Res = LowerOperation(Op, DAG);
966 if (Res.getNode())
967 Results.push_back(Res);
968
969 return;
970 }
971 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000972
973 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000974 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000975 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000976 Results.push_back(SDValue(N, i));
977 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000978}
979
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000980SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
981 switch (Op.getOpcode()) {
982 case ISD::FORMAL_ARGUMENTS:
983 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000984 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000985 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000986 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000987 return LowerADD(Op, DAG);
988 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000989 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000990 case ISD::SUBE:
991 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000992 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000993 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000994 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000995 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000996 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000997 case ISD::SRA:
998 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000999 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001000 case ISD::OR:
1001 case ISD::AND:
1002 case ISD::XOR:
1003 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001004 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001005 return LowerCALL(Op, DAG);
1006 case ISD::RET:
1007 return LowerRET(Op, DAG);
1008 case ISD::BR_CC:
1009 return LowerBR_CC(Op, DAG);
1010 case ISD::SELECT_CC:
1011 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001012 }
Dan Gohman475871a2008-07-27 21:46:04 +00001013 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001014}
1015
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001016SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001017 SelectionDAG &DAG,
1018 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001019 assert (Op.getValueType() == MVT::i8
1020 && "illegal value type to store on stack.");
1021
1022 MachineFunction &MF = DAG.getMachineFunction();
1023 const Function *Func = MF.getFunction();
1024 const std::string FuncName = Func->getName();
1025
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001026
1027 // Put the value on stack.
1028 // Get a stack slot index and convert to es.
1029 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001030 const char *tmpName = createESName(PAN::getTempdataLabel(FuncName));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001031 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1032
1033 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001034 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001035 DAG.getEntryNode(),
1036 Op, ES,
1037 DAG.getConstant (1, MVT::i8), // Banksel.
Sanjiv Guptaa3613be2009-04-10 15:10:14 +00001038 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001039 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001040
1041 // Load the value from ES.
1042 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001043 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001044 ES, DAG.getConstant (1, MVT::i8),
Sanjiv Guptaa3613be2009-04-10 15:10:14 +00001045 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001046 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001047
1048 return Load.getValue(0);
1049}
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001050
1051SDValue PIC16TargetLowering::
1052LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
1053 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1054 SelectionDAG &DAG) {
1055 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1056 unsigned NumOps = TheCall->getNumArgs();
1057 DebugLoc dl = TheCall->getDebugLoc();
1058
1059 // If call has no arguments then do nothing and return.
1060 if (NumOps == 0)
1061 return Chain;
1062
1063 std::vector<SDValue> Ops;
1064 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1065 SDValue Arg, StoreRet;
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001066
1067 // For PIC16 ABI the arguments come after the return value.
1068 unsigned RetVals = TheCall->getNumRetVals();
1069 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001070 // Get the arguments
1071 Arg = TheCall->getArg(i);
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001072
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001073 Ops.clear();
1074 Ops.push_back(Chain);
1075 Ops.push_back(Arg);
1076 Ops.push_back(DataAddr_Lo);
1077 Ops.push_back(DataAddr_Hi);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001078 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001079 Ops.push_back(InFlag);
1080
1081 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
1082
1083 Chain = getChain(StoreRet);
1084 InFlag = getOutFlag(StoreRet);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001085 ArgOffset++;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001086 }
1087 return Chain;
1088}
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001089
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001090SDValue PIC16TargetLowering::
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001091LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue ArgLabel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001092 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001093 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1094 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +00001095 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001096 std::string Name;
1097 SDValue Arg, StoreAt;
1098 MVT ArgVT;
1099 unsigned Size=0;
1100 unsigned ArgCount=0;
1101
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001102 // If call has no arguments then do nothing and return.
1103 if (NumOps == 0)
1104 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001105
1106 // FIXME: This portion of code currently assumes only
1107 // primitive types being passed as arguments.
1108
1109 // Legalize the address before use
1110 SDValue PtrLo, PtrHi;
1111 unsigned AddressOffset;
1112 int StoreOffset = 0;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001113 LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001114 SDValue StoreRet;
1115
1116 std::vector<SDValue> Ops;
1117 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1118 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
1119 // Get the argument
1120 Arg = TheCall->getArg(i);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001121 StoreOffset = (Offset + AddressOffset);
1122
1123 // Store the argument on frame
1124
1125 Ops.clear();
1126 Ops.push_back(Chain);
Sanjiv Gupta12f23a82009-04-13 09:38:38 +00001127 Ops.push_back(Arg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001128 Ops.push_back(PtrLo);
1129 Ops.push_back(PtrHi);
1130 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
1131 Ops.push_back(InFlag);
1132
Dale Johannesene8d72302009-02-06 23:05:02 +00001133 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001134
1135 Chain = getChain(StoreRet);
1136 InFlag = getOutFlag(StoreRet);
1137
1138 // Update the frame offset to be used for next argument
1139 ArgVT = Arg.getValueType();
1140 Size = ArgVT.getSizeInBits();
1141 Size = Size/8; // Calculate size in bytes
1142 Offset += Size; // Increase the frame offset
1143 }
1144 return Chain;
1145}
1146
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001147SDValue PIC16TargetLowering::
1148LowerIndirectCallReturn (SDValue Op, SDValue Chain, SDValue InFlag,
1149 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1150 SelectionDAG &DAG) {
1151 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1152 DebugLoc dl = TheCall->getDebugLoc();
1153 unsigned RetVals = TheCall->getNumRetVals();
1154
1155 // If call does not have anything to return
1156 // then do nothing and go back.
1157 if (RetVals == 0)
1158 return Chain;
1159
1160 // Call has something to return
1161 std::vector<SDValue> ResultVals;
1162 SDValue LoadRet;
1163
1164 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1165 for(unsigned i=0;i<RetVals;i++) {
1166 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
1167 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
1168 InFlag);
1169 InFlag = getOutFlag(LoadRet);
1170 Chain = getChain(LoadRet);
1171 ResultVals.push_back(LoadRet);
1172 }
1173 ResultVals.push_back(Chain);
1174 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
1175 return Res;
1176}
1177
1178SDValue PIC16TargetLowering::
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001179LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue RetLabel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001180 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001181 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001182 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001183 // Currently handling primitive types only. They will come in
1184 // i8 parts
1185 unsigned RetVals = TheCall->getNumRetVals();
1186
1187 std::vector<SDValue> ResultVals;
1188
1189 // Return immediately if the return type is void
1190 if (RetVals == 0)
1191 return Chain;
1192
1193 // Call has something to return
1194
1195 // Legalize the address before use
1196 SDValue LdLo, LdHi;
1197 unsigned LdOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001198 LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001199
1200 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1201 SDValue LoadRet;
1202
1203 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1204
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001205 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001206 DAG.getConstant(LdOffset + Offset, MVT::i8),
1207 InFlag);
1208
1209 InFlag = getOutFlag(LoadRet);
1210
1211 Chain = getChain(LoadRet);
1212 Offset++;
1213 ResultVals.push_back(LoadRet);
1214 }
1215
1216 // To return use MERGE_VALUES
1217 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001218 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001219 return Res;
1220}
1221
1222SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001223 SDValue Chain = Op.getOperand(0);
1224 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001225
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001226 if (Op.getNumOperands() == 1) // return void
1227 return Op;
1228
1229 // return should have odd number of operands
1230 if ((Op.getNumOperands() % 2) == 0 ) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001231 LLVM_UNREACHABLE("Do not know how to return this many arguments!");
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001232 }
1233
1234 // Number of values to return
1235 unsigned NumRet = (Op.getNumOperands() / 2);
1236
1237 // Function returns value always on stack with the offset starting
1238 // from 0
1239 MachineFunction &MF = DAG.getMachineFunction();
1240 const Function *F = MF.getFunction();
1241 std::string FuncName = F->getName();
1242
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001243 const char *tmpName = createESName(PAN::getFrameLabel(FuncName));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001244 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1245 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1246 SDValue BS = DAG.getConstant(1, MVT::i8);
1247 SDValue RetVal;
1248 for(unsigned i=0;i<NumRet; ++i) {
1249 RetVal = Op.getNode()->getOperand(2*i + 1);
1250 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1251 ES, BS,
1252 DAG.getConstant (i, MVT::i8));
1253
1254 }
1255 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001256}
1257
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001258// CALL node may have some operands non-legal to PIC16. Generate new CALL
1259// node with all the operands legal.
1260// Currently only Callee operand of the CALL node is non-legal. This function
1261// legalizes the Callee operand and uses all other operands as are to generate
1262// new CALL node.
1263
1264SDValue PIC16TargetLowering::LegalizeCALL(SDValue Op, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001265 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1266 SDValue Chain = TheCall->getChain();
1267 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001268 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001269 unsigned i =0;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001270
1271 assert(Callee.getValueType() == MVT::i16 &&
1272 "Don't know how to legalize this call node!!!");
1273 assert(Callee.getOpcode() == ISD::BUILD_PAIR &&
1274 "Don't know how to legalize this call node!!!");
1275
1276 if (isDirectAddress(Callee)) {
1277 // Come here for direct calls
1278 Callee = Callee.getOperand(0).getOperand(0);
1279 } else {
1280 // Come here for indirect calls
1281 SDValue Lo, Hi;
1282 // Indirect addresses. Get the hi and lo parts of ptr.
1283 GetExpandedParts(Callee, DAG, Lo, Hi);
1284 // Connect Lo and Hi parts of the callee with the PIC16Connect
1285 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1286 }
1287 std::vector<SDValue> Ops;
1288 Ops.push_back(Chain);
1289 Ops.push_back(Callee);
1290
1291 // Add the call arguments and their flags
1292 unsigned NumArgs = TheCall->getNumArgs();
1293 for(i=0;i<NumArgs;i++) {
1294 Ops.push_back(TheCall->getArg(i));
1295 Ops.push_back(TheCall->getArgFlagsVal(i));
1296 }
1297 std::vector<MVT> NodeTys;
1298 unsigned NumRets = TheCall->getNumRetVals();
1299 for(i=0;i<NumRets;i++)
1300 NodeTys.push_back(TheCall->getRetValType(i));
1301
1302 // Return a Chain as well
1303 NodeTys.push_back(MVT::Other);
1304
1305 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1306 // Generate new call with all the operands legal
1307 return DAG.getCall(TheCall->getCallingConv(), dl,
1308 TheCall->isVarArg(), TheCall->isTailCall(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00001309 TheCall->isInreg(), VTs, &Ops[0], Ops.size(),
1310 TheCall->getNumFixedArgs());
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001311}
1312
1313void PIC16TargetLowering::
1314GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1315 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1316 SelectionDAG &DAG) {
1317 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1318 && "Don't know what to do of such callee!!");
1319 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1320 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1321 Chain = getChain(SeqStart);
1322 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1323
1324 // Get the Lo and Hi part of code address
1325 SDValue Lo = Callee.getOperand(0);
1326 SDValue Hi = Callee.getOperand(1);
1327
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001328 SDValue Data_Lo, Data_Hi;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001329 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001330 // Subtract 2 from Address to get the Lower part of DataAddress.
1331 SDVTList VTList = DAG.getVTList(MVT::i8, MVT::Flag);
1332 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1333 DAG.getConstant(2, MVT::i8));
1334 SDValue Ops[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1335 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, Ops, 3);
1336 SDValue PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1337 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001338 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1339 OperFlag);
1340 Chain = getChain(Call);
1341 OperFlag = getOutFlag(Call);
1342 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1343 OperFlag);
1344 Chain = getChain(SeqEnd);
1345 OperFlag = getOutFlag(SeqEnd);
1346
1347 // Low part of Data Address
1348 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1349
1350 // Make the second call.
1351 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1352 Chain = getChain(SeqStart);
1353 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1354
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001355 // Subtract 1 from Address to get high part of data address.
1356 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1357 DAG.getConstant(1, MVT::i8));
1358 SDValue HiOps[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1359 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1360 PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1361
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001362 // Use new Lo to make another CALLW
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001363 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001364 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1365 Chain = getChain(Call);
1366 OperFlag = getOutFlag(Call);
1367 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1368 OperFlag);
1369 Chain = getChain(SeqEnd);
1370 OperFlag = getOutFlag(SeqEnd);
1371 // Hi part of Data Address
1372 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1373}
1374
1375
1376SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
1377 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1378 SDValue Chain = TheCall->getChain();
1379 SDValue Callee = TheCall->getCallee();
1380 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001381 if (Callee.getValueType() == MVT::i16 &&
1382 Callee.getOpcode() == ISD::BUILD_PAIR) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001383 // Control should come here only from TypeLegalizer for lowering
1384
1385 // Legalize the non-legal arguments of call and return the
1386 // new call with legal arguments.
1387 return LegalizeCALL(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001388 }
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001389 // Control should come here from Legalize DAG.
1390 // Here all the operands of CALL node should be legal.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001391
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001392 // If this is an indirect call then to pass the arguments
1393 // and read the return value back, we need the data address
1394 // of the function being called.
1395 // To get the data address two more calls need to be made.
1396
1397 // The flag to track if this is a direct or indirect call.
1398 bool IsDirectCall = true;
1399 unsigned RetVals = TheCall->getNumRetVals();
1400 unsigned NumArgs = TheCall->getNumArgs();
1401
1402 SDValue DataAddr_Lo, DataAddr_Hi;
1403 if (Callee.getOpcode() == PIC16ISD::PIC16Connect) {
1404 IsDirectCall = false; // This is indirect call
1405 // Read DataAddress only if we have to pass arguments or
1406 // read return value.
1407 if ((RetVals > 0) || (NumArgs > 0))
1408 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1409 }
1410
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001411 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1412
1413 // Start the call sequence.
1414 // Carring the Constant 0 along the CALLSEQSTART
1415 // because there is nothing else to carry.
1416 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1417 Chain = getChain(SeqStart);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001418 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1419 std::string Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001420
1421 // For any direct call - callee will be GlobalAddressNode or
1422 // ExternalSymbol
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001423 SDValue ArgLabel, RetLabel;
1424 if (IsDirectCall) {
1425 // Considering the GlobalAddressNode case here.
1426 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1427 GlobalValue *GV = G->getGlobal();
1428 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1429 Name = G->getGlobal()->getName();
1430 } else {// Considering the ExternalSymbol case here
1431 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1432 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1433 Name = ES->getSymbol();
1434 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001435
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001436 // Label for argument passing
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001437 const char *argFrame = createESName(PAN::getArgsLabel(Name));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001438 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001439
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001440 // Label for reading return value
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001441 const char *retName = createESName(PAN::getRetvalLabel(Name));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001442 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1443 } else {
1444 // if indirect call
1445 SDValue CodeAddr_Lo = Callee.getOperand(0);
1446 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001447
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001448 /*CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1449 DAG.getConstant(2, MVT::i8));*/
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001450
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001451 // move Hi part in PCLATH
1452 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1453 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1454 CodeAddr_Hi);
1455 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001456
1457 // Pass the argument to function before making the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001458 SDValue CallArgs;
1459 if (IsDirectCall) {
1460 CallArgs = LowerDirectCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1461 Chain = getChain(CallArgs);
1462 OperFlag = getOutFlag(CallArgs);
1463 } else {
1464 CallArgs = LowerIndirectCallArguments(Op, Chain, OperFlag, DataAddr_Lo,
1465 DataAddr_Hi, DAG);
1466 Chain = getChain(CallArgs);
1467 OperFlag = getOutFlag(CallArgs);
1468 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001469
1470 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001471 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001472 OperFlag);
1473 Chain = getChain(PICCall);
1474 OperFlag = getOutFlag(PICCall);
1475
1476
1477 // Carrying the Constant 0 along the CALLSEQSTART
1478 // because there is nothing else to carry.
1479 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1480 OperFlag);
1481 Chain = getChain(SeqEnd);
1482 OperFlag = getOutFlag(SeqEnd);
1483
1484 // Lower the return value reading after the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001485 if (IsDirectCall)
1486 return LowerDirectCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1487 else
1488 return LowerIndirectCallReturn(Op, Chain, OperFlag, DataAddr_Lo,
1489 DataAddr_Hi, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001490}
1491
1492bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1493 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1494 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1495 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1496 return true;
1497 return false;
1498}
1499
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001500// NeedToConvertToMemOp - Returns true if one of the operands of the
1501// operation 'Op' needs to be put into memory. Also returns the
1502// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1503// no instruction that can operation on two registers. Most insns take
1504// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001505bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001506 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001507 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1508 Op.getOperand(1).getOpcode() == ISD::Constant)
1509 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001510
1511 // Return false if one of the operands is already a direct
1512 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001513 if (isDirectLoad(Op.getOperand(0))) {
1514 if (Op.getOperand(0).hasOneUse())
1515 return false;
1516 else
1517 MemOp = 0;
1518 }
1519 if (isDirectLoad(Op.getOperand(1))) {
1520 if (Op.getOperand(1).hasOneUse())
1521 return false;
1522 else
1523 MemOp = 1;
1524 }
1525 return true;
1526}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001527
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001528// LowerBinOp - Lower a commutative binary operation that does not
1529// affect status flag carry.
1530SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001531 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001532
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001533 // We should have handled larger operands in type legalizer itself.
1534 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001535
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001536 unsigned MemOp = 1;
1537 if (NeedToConvertToMemOp(Op, MemOp)) {
1538 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001539 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001540
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001541 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001542 NewVal);
1543 }
1544 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001545 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001546 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001547}
1548
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001549// LowerADD - Lower all types of ADD operations including the ones
1550// that affects carry.
1551SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001552 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001553 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001554 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001555 unsigned MemOp = 1;
1556 if (NeedToConvertToMemOp(Op, MemOp)) {
1557 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001558 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001559
Dale Johannesen83138992009-06-01 23:13:42 +00001560 // ADDC and ADDE produce two results.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001561 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001562
Dale Johannesen83138992009-06-01 23:13:42 +00001563 // ADDE has three operands, the last one is the carry bit.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001564 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001565 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1566 NewVal, Op.getOperand(2));
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001567 // ADDC has two operands.
1568 else if (Op.getOpcode() == ISD::ADDC)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001569 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1570 NewVal);
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001571 // ADD it is. It produces only one result.
1572 else
1573 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1574 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001575 }
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001576 else
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001577 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001578}
1579
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001580SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001581 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001582 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001583 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001584
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001585 // Nothing to do if the first operand is already a direct load and it has
1586 // only one use.
1587 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001588 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001589
1590 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001591 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001592
1593 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001594 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001595 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001596 Op.getOperand(2));
1597 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001598 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001599}
1600
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001601void PIC16TargetLowering::InitReservedFrameCount(const Function *F) {
1602 unsigned NumArgs = F->arg_size();
1603
1604 bool isVoidFunc = (F->getReturnType()->getTypeID() == Type::VoidTyID);
1605
1606 if (isVoidFunc)
1607 ReservedFrameCount = NumArgs;
1608 else
1609 ReservedFrameCount = NumArgs + 1;
1610}
1611
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001612// LowerFORMAL_ARGUMENTS - Argument values are loaded from the
1613// <fname>.args + offset. All arguments are already broken to leaglized
1614// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001615
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001616SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001617 SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001618 SmallVector<SDValue, 8> ArgValues;
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001619 unsigned NumArgVals = Op.getNode()->getNumValues() - 1;
Dale Johannesene8d72302009-02-06 23:05:02 +00001620 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001621 SDValue Chain = Op.getOperand(0); // Formal arguments' chain
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001622
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001623
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001624 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001625 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001626 const Function *F = MF.getFunction();
1627 std::string FuncName = F->getName();
1628
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001629 // Reset the map of FI and TmpOffset
1630 ResetTmpOffsetMap();
1631 // Initialize the ReserveFrameCount
1632 InitReservedFrameCount(F);
1633
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001634 // Create the <fname>.args external symbol.
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001635 const char *tmpName = createESName(PAN::getArgsLabel(FuncName));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001636 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001637
1638 // Load arg values from the label + offset.
1639 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001640 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001641 for (unsigned i = 0; i < NumArgVals ; ++i) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001642 SDValue Offset = DAG.getConstant(i, MVT::i8);
1643 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001644 Offset);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001645 Chain = getChain(PICLoad);
1646 ArgValues.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001647 }
1648
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001649 // Return a MERGE_VALUE node.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001650 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001651 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001652 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001653}
1654
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001655// Perform DAGCombine of PIC16Load.
1656// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001657SDValue PIC16TargetLowering::
1658PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1659 SelectionDAG &DAG = DCI.DAG;
1660 SDValue Chain = N->getOperand(0);
1661 if (N->hasNUsesOfValue(0, 0)) {
1662 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1663 }
1664 return SDValue();
1665}
1666
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001667// For all the functions with arguments some STORE nodes are generated
1668// that store the argument on the frameindex. However in PIC16 the arguments
1669// are passed on stack only. Therefore these STORE nodes are redundant.
1670// To remove these STORE nodes will be removed in PerformStoreCombine
1671//
1672// Currently this function is doint nothing and will be updated for removing
1673// unwanted store operations
1674SDValue PIC16TargetLowering::
1675PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001676 return SDValue(N, 0);
1677 /*
1678 // Storing an undef value is of no use, so remove it
1679 if (isStoringUndef(N, Chain, DAG)) {
1680 return Chain; // remove the store and return the chain
1681 }
1682 //else everything is ok.
1683 return SDValue(N, 0);
1684 */
1685}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001686
1687SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1688 DAGCombinerInfo &DCI) const {
1689 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001690 case ISD::STORE:
1691 return PerformStoreCombine(N, DCI);
1692 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001693 return PerformPIC16LoadCombine(N, DCI);
1694 }
1695 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001696}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001697
1698static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1699 switch (CC) {
1700 default: assert(0 && "Unknown condition code!");
1701 case ISD::SETNE: return PIC16CC::NE;
1702 case ISD::SETEQ: return PIC16CC::EQ;
1703 case ISD::SETGT: return PIC16CC::GT;
1704 case ISD::SETGE: return PIC16CC::GE;
1705 case ISD::SETLT: return PIC16CC::LT;
1706 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001707 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta703e2352009-06-03 13:36:44 +00001708 case ISD::SETULE: return PIC16CC::ULE;
1709 case ISD::SETUGE: return PIC16CC::UGE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001710 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001711 }
1712}
1713
1714// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1715// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1716static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1717 ISD::CondCode CC, unsigned &SPCC) {
1718 if (isa<ConstantSDNode>(RHS) &&
1719 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1720 CC == ISD::SETNE &&
1721 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1722 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1723 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1724 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1725 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1726 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1727 SDValue CMPCC = LHS.getOperand(3);
1728 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1729 LHS = CMPCC.getOperand(0);
1730 RHS = CMPCC.getOperand(1);
1731 }
1732}
1733
1734// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1735SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1736 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001737 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001738 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1739
1740 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1741 // i.e. a < 12 can be rewritten as 12 > a.
1742 if (RHS.getOpcode() == ISD::Constant) {
1743
1744 SDValue Tmp = LHS;
1745 LHS = RHS;
1746 RHS = Tmp;
1747
1748 switch (CondCode) {
1749 default: break;
1750 case PIC16CC::LT:
1751 CondCode = PIC16CC::GT;
1752 break;
1753 case PIC16CC::GT:
1754 CondCode = PIC16CC::LT;
1755 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001756 case PIC16CC::ULT:
1757 CondCode = PIC16CC::UGT;
1758 break;
1759 case PIC16CC::UGT:
1760 CondCode = PIC16CC::ULT;
1761 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001762 case PIC16CC::GE:
1763 CondCode = PIC16CC::LE;
1764 break;
1765 case PIC16CC::LE:
1766 CondCode = PIC16CC::GE;
1767 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001768 case PIC16CC::ULE:
1769 CondCode = PIC16CC::UGE;
1770 break;
1771 case PIC16CC::UGE:
1772 CondCode = PIC16CC::ULE;
1773 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001774 }
1775 }
1776
1777 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001778
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001779 // These are signed comparisons.
1780 SDValue Mask = DAG.getConstant(128, MVT::i8);
1781 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001782 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1783 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001784 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001785
1786 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001787 // We can use a subtract operation to set the condition codes. But
1788 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001789 // Nothing to do if the first operand is already a valid type (direct load
1790 // for subwf and literal for sublw) and it is used by this operation only.
1791 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1792 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001793 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001794
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001795 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001796 LHS = ConvertToMemOperand (LHS, DAG, dl);
1797 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001798}
1799
1800
1801SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1802 SDValue LHS = Op.getOperand(0);
1803 SDValue RHS = Op.getOperand(1);
1804 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1805 SDValue TrueVal = Op.getOperand(2);
1806 SDValue FalseVal = Op.getOperand(3);
1807 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001808 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001809
1810 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1811 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1812 // i.e.
1813 // A setcc: lhs, rhs, cc is expanded by llvm to
1814 // select_cc: result of setcc, 0, 1, 0, setne
1815 // We can think of it as:
1816 // select_cc: lhs, rhs, 1, 0, cc
1817 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1818 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1819
1820 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001821 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001822
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001823 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001824 FalseVal, PIC16CC, Cmp.getValue(1));
1825}
1826
1827MachineBasicBlock *
1828PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001829 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001830 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1831 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001832 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001833
1834 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1835 // control-flow pattern. The incoming instruction knows the destination vreg
1836 // to set, the condition code register to branch on, the true/false values to
1837 // select between, and a branch opcode to use.
1838 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1839 MachineFunction::iterator It = BB;
1840 ++It;
1841
1842 // thisMBB:
1843 // ...
1844 // TrueVal = ...
1845 // [f]bCC copy1MBB
1846 // fallthrough --> copy0MBB
1847 MachineBasicBlock *thisMBB = BB;
1848 MachineFunction *F = BB->getParent();
1849 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1850 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001851 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001852 F->insert(It, copy0MBB);
1853 F->insert(It, sinkMBB);
1854
1855 // Update machine-CFG edges by transferring all successors of the current
1856 // block to the new block which will contain the Phi node for the select.
1857 sinkMBB->transferSuccessors(BB);
1858 // Next, add the true and fallthrough blocks as its successors.
1859 BB->addSuccessor(copy0MBB);
1860 BB->addSuccessor(sinkMBB);
1861
1862 // copy0MBB:
1863 // %FalseValue = ...
1864 // # fallthrough to sinkMBB
1865 BB = copy0MBB;
1866
1867 // Update machine-CFG edges
1868 BB->addSuccessor(sinkMBB);
1869
1870 // sinkMBB:
1871 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1872 // ...
1873 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001874 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001875 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1876 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1877
1878 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1879 return BB;
1880}
1881
1882
1883SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1884 SDValue Chain = Op.getOperand(0);
1885 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1886 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1887 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1888 SDValue Dest = Op.getOperand(4); // BB to jump to
1889 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001890 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001891
1892 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1893 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1894 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1895 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1896
1897 // Get the Compare insn and condition code.
1898 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001899 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001900
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001901 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001902 Cmp.getValue(1));
1903}
1904