blob: ec1db900b3800c2df019d0ad14038e087dde9652 [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001//
2// The LLVM Compiler Infrastructure
3//
4// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the interfaces that PIC16 uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "pic16-lower"
15
16#include "PIC16ISelLowering.h"
17#include "PIC16TargetMachine.h"
18#include "llvm/DerivedTypes.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000019#include "llvm/GlobalValue.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000020#include "llvm/Function.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000021#include "llvm/CallingConv.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000026
27
Sanjiv Gupta0e687712008-05-13 09:02:57 +000028using namespace llvm;
29
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000030static const char *getIntrinsicName(unsigned opcode) {
31 std::string Basename;
32 switch(opcode) {
33 default: assert (0 && "do not know intrinsic name");
Sanjiv Gupta003263b2009-06-16 16:17:35 +000034 // Arithmetic Right shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000035 case PIC16ISD::SRA_I8: Basename = "sra.i8"; break;
36 case RTLIB::SRA_I16: Basename = "sra.i16"; break;
37 case RTLIB::SRA_I32: Basename = "sra.i32"; break;
38
Sanjiv Gupta003263b2009-06-16 16:17:35 +000039 // Left shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000040 case PIC16ISD::SLL_I8: Basename = "sll.i8"; break;
41 case RTLIB::SHL_I16: Basename = "sll.i16"; break;
42 case RTLIB::SHL_I32: Basename = "sll.i32"; break;
43
Sanjiv Gupta003263b2009-06-16 16:17:35 +000044 // Logical Right Shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000045 case PIC16ISD::SRL_I8: Basename = "srl.i8"; break;
46 case RTLIB::SRL_I16: Basename = "srl.i16"; break;
47 case RTLIB::SRL_I32: Basename = "srl.i32"; break;
48
Sanjiv Gupta003263b2009-06-16 16:17:35 +000049 // Multiply for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000050 case PIC16ISD::MUL_I8: Basename = "mul.i8"; break;
51 case RTLIB::MUL_I16: Basename = "mul.i16"; break;
52 case RTLIB::MUL_I32: Basename = "mul.i32"; break;
Sanjiv Gupta46800772009-06-05 14:43:12 +000053
Sanjiv Gupta003263b2009-06-16 16:17:35 +000054 // Signed division for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000055 case RTLIB::SDIV_I16: Basename = "sdiv.i16"; break;
56 case RTLIB::SDIV_I32: Basename = "sdiv.i32"; break;
Sanjiv Gupta003263b2009-06-16 16:17:35 +000057
58 // Unsigned division for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000059 case RTLIB::UDIV_I16: Basename = "udiv.i16"; break;
60 case RTLIB::UDIV_I32: Basename = "udiv.i32"; break;
61
Sanjiv Gupta003263b2009-06-16 16:17:35 +000062 // Signed Modulas for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000063 case RTLIB::SREM_I16: Basename = "srem.i16"; break;
64 case RTLIB::SREM_I32: Basename = "srem.i32"; break;
Sanjiv Gupta003263b2009-06-16 16:17:35 +000065
66 // Unsigned Modulas for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000067 case RTLIB::UREM_I16: Basename = "urem.i16"; break;
68 case RTLIB::UREM_I32: Basename = "urem.i32"; break;
Sanjiv Gupta63963292009-06-11 16:50:48 +000069
Sanjiv Gupta003263b2009-06-16 16:17:35 +000070 //////////////////////
71 // LIBCALLS FOR FLOATS
72 //////////////////////
73
74 // Float to signed integrals
75 case RTLIB::FPTOSINT_F32_I8: Basename = "f32_to_si32"; break;
76 case RTLIB::FPTOSINT_F32_I16: Basename = "f32_to_si32"; break;
77 case RTLIB::FPTOSINT_F32_I32: Basename = "f32_to_si32"; break;
78
79 // Signed integrals to float. char and int are first sign extended to i32
80 // before being converted to float, so an I8_F32 or I16_F32 isn't required.
81 case RTLIB::SINTTOFP_I32_F32: Basename = "si32_to_f32"; break;
82
83 // Float to Unsigned conversions.
84 // Signed conversion can be used for unsigned conversion as well.
85 // In signed and unsigned versions only the interpretation of the
86 // MSB is different. Bit representation remains the same.
87 case RTLIB::FPTOUINT_F32_I8: Basename = "f32_to_si32"; break;
88 case RTLIB::FPTOUINT_F32_I16: Basename = "f32_to_si32"; break;
89 case RTLIB::FPTOUINT_F32_I32: Basename = "f32_to_si32"; break;
90
91 // Unsigned to Float conversions. char and int are first zero extended
92 // before being converted to float.
93 case RTLIB::UINTTOFP_I32_F32: Basename = "ui32_to_f32"; break;
Sanjiv Gupta63963292009-06-11 16:50:48 +000094
Sanjiv Gupta003263b2009-06-16 16:17:35 +000095 // Floating point add, sub, mul, div.
Sanjiv Gupta63963292009-06-11 16:50:48 +000096 case RTLIB::ADD_F32: Basename = "add.f32"; break;
97 case RTLIB::SUB_F32: Basename = "sub.f32"; break;
98 case RTLIB::MUL_F32: Basename = "mul.f32"; break;
99 case RTLIB::DIV_F32: Basename = "div.f32"; break;
Sanjiv Guptacbd5f0a2009-06-25 18:12:06 +0000100
101 // Floating point comparison
102 case RTLIB::O_F32: Basename = "unordered.f32"; break;
103 case RTLIB::UO_F32: Basename = "unordered.f32"; break;
104 case RTLIB::OLE_F32: Basename = "le.f32"; break;
105 case RTLIB::OGE_F32: Basename = "ge.f32"; break;
106 case RTLIB::OLT_F32: Basename = "lt.f32"; break;
107 case RTLIB::OGT_F32: Basename = "gt.f32"; break;
108 case RTLIB::OEQ_F32: Basename = "eq.f32"; break;
109 case RTLIB::UNE_F32: Basename = "neq.f32"; break;
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000110 }
111
112 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
113 std::string tagname = PAN::getTagName(PAN::LIBCALL);
114 std::string Fullname = prefix + tagname + Basename;
115
116 // The name has to live through program life.
117 char *tmp = new char[Fullname.size() + 1];
118 strcpy (tmp, Fullname.c_str());
119
120 return tmp;
121}
122
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000123// PIC16TargetLowering Constructor.
124PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000125 : TargetLowering(TM), TmpSize(0) {
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000126
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000127 Subtarget = &TM.getSubtarget<PIC16Subtarget>();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000128
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000129 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000130
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000131 setShiftAmountType(MVT::i8);
132 setShiftAmountFlavor(Extend);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000133
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000134 // SRA library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000135 setPIC16LibcallName(PIC16ISD::SRA_I8, getIntrinsicName(PIC16ISD::SRA_I8));
136 setLibcallName(RTLIB::SRA_I16, getIntrinsicName(RTLIB::SRA_I16));
137 setLibcallName(RTLIB::SRA_I32, getIntrinsicName(RTLIB::SRA_I32));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000138
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000139 // SHL library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000140 setPIC16LibcallName(PIC16ISD::SLL_I8, getIntrinsicName(PIC16ISD::SLL_I8));
141 setLibcallName(RTLIB::SHL_I16, getIntrinsicName(RTLIB::SHL_I16));
142 setLibcallName(RTLIB::SHL_I32, getIntrinsicName(RTLIB::SHL_I32));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000143
144 // SRL library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000145 setPIC16LibcallName(PIC16ISD::SRL_I8, getIntrinsicName(PIC16ISD::SRL_I8));
146 setLibcallName(RTLIB::SRL_I16, getIntrinsicName(RTLIB::SRL_I16));
147 setLibcallName(RTLIB::SRL_I32, getIntrinsicName(RTLIB::SRL_I32));
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000148
149 // MUL Library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000150 setPIC16LibcallName(PIC16ISD::MUL_I8, getIntrinsicName(PIC16ISD::MUL_I8));
151 setLibcallName(RTLIB::MUL_I16, getIntrinsicName(RTLIB::MUL_I16));
152 setLibcallName(RTLIB::MUL_I32, getIntrinsicName(RTLIB::MUL_I32));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000153
Sanjiv Gupta46800772009-06-05 14:43:12 +0000154 // Signed division lib call names
155 setLibcallName(RTLIB::SDIV_I16, getIntrinsicName(RTLIB::SDIV_I16));
156 setLibcallName(RTLIB::SDIV_I32, getIntrinsicName(RTLIB::SDIV_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000157
Sanjiv Gupta46800772009-06-05 14:43:12 +0000158 // Unsigned division lib call names
159 setLibcallName(RTLIB::UDIV_I16, getIntrinsicName(RTLIB::UDIV_I16));
160 setLibcallName(RTLIB::UDIV_I32, getIntrinsicName(RTLIB::UDIV_I32));
161
162 // Signed remainder lib call names
163 setLibcallName(RTLIB::SREM_I16, getIntrinsicName(RTLIB::SREM_I16));
164 setLibcallName(RTLIB::SREM_I32, getIntrinsicName(RTLIB::SREM_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000165
Sanjiv Gupta46800772009-06-05 14:43:12 +0000166 // Unsigned remainder lib call names
167 setLibcallName(RTLIB::UREM_I16, getIntrinsicName(RTLIB::UREM_I16));
168 setLibcallName(RTLIB::UREM_I32, getIntrinsicName(RTLIB::UREM_I32));
Sanjiv Gupta63963292009-06-11 16:50:48 +0000169
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000170 // Floating point to signed int conversions.
171 setLibcallName(RTLIB::FPTOSINT_F32_I8,
172 getIntrinsicName(RTLIB::FPTOSINT_F32_I8));
173 setLibcallName(RTLIB::FPTOSINT_F32_I16,
174 getIntrinsicName(RTLIB::FPTOSINT_F32_I16));
Sanjiv Gupta63963292009-06-11 16:50:48 +0000175 setLibcallName(RTLIB::FPTOSINT_F32_I32,
176 getIntrinsicName(RTLIB::FPTOSINT_F32_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000177
178 // Signed int to floats.
Sanjiv Gupta63963292009-06-11 16:50:48 +0000179 setLibcallName(RTLIB::SINTTOFP_I32_F32,
180 getIntrinsicName(RTLIB::SINTTOFP_I32_F32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000181
182 // Floating points to unsigned ints.
183 setLibcallName(RTLIB::FPTOUINT_F32_I8,
184 getIntrinsicName(RTLIB::FPTOUINT_F32_I8));
185 setLibcallName(RTLIB::FPTOUINT_F32_I16,
186 getIntrinsicName(RTLIB::FPTOUINT_F32_I16));
187 setLibcallName(RTLIB::FPTOUINT_F32_I32,
188 getIntrinsicName(RTLIB::FPTOUINT_F32_I32));
189
190 // Unsigned int to floats.
191 setLibcallName(RTLIB::UINTTOFP_I32_F32,
192 getIntrinsicName(RTLIB::UINTTOFP_I32_F32));
193
194 // Floating point add, sub, mul ,div.
Sanjiv Gupta63963292009-06-11 16:50:48 +0000195 setLibcallName(RTLIB::ADD_F32, getIntrinsicName(RTLIB::ADD_F32));
196 setLibcallName(RTLIB::SUB_F32, getIntrinsicName(RTLIB::SUB_F32));
197 setLibcallName(RTLIB::MUL_F32, getIntrinsicName(RTLIB::MUL_F32));
198 setLibcallName(RTLIB::DIV_F32, getIntrinsicName(RTLIB::DIV_F32));
199
Sanjiv Guptacbd5f0a2009-06-25 18:12:06 +0000200 // Floationg point comparison
201 setLibcallName(RTLIB::UO_F32, getIntrinsicName(RTLIB::UO_F32));
202 setLibcallName(RTLIB::OLE_F32, getIntrinsicName(RTLIB::OLE_F32));
203 setLibcallName(RTLIB::OGE_F32, getIntrinsicName(RTLIB::OGE_F32));
204 setLibcallName(RTLIB::OLT_F32, getIntrinsicName(RTLIB::OLT_F32));
205 setLibcallName(RTLIB::OGT_F32, getIntrinsicName(RTLIB::OGT_F32));
206 setLibcallName(RTLIB::OEQ_F32, getIntrinsicName(RTLIB::OEQ_F32));
207 setLibcallName(RTLIB::UNE_F32, getIntrinsicName(RTLIB::UNE_F32));
208
209 // Return value comparisons of floating point calls.
210 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
218
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000219 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000220 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000221
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000222 setOperationAction(ISD::LOAD, MVT::i8, Legal);
223 setOperationAction(ISD::LOAD, MVT::i16, Custom);
224 setOperationAction(ISD::LOAD, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000225
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000226 setOperationAction(ISD::STORE, MVT::i8, Legal);
227 setOperationAction(ISD::STORE, MVT::i16, Custom);
228 setOperationAction(ISD::STORE, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000229
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000230 setOperationAction(ISD::ADDE, MVT::i8, Custom);
231 setOperationAction(ISD::ADDC, MVT::i8, Custom);
232 setOperationAction(ISD::SUBE, MVT::i8, Custom);
233 setOperationAction(ISD::SUBC, MVT::i8, Custom);
Sanjiv Gupta3b0a4f12009-06-04 08:52:28 +0000234 setOperationAction(ISD::SUB, MVT::i8, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000235 setOperationAction(ISD::ADD, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000236 setOperationAction(ISD::ADD, MVT::i16, Custom);
237
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000238 setOperationAction(ISD::OR, MVT::i8, Custom);
239 setOperationAction(ISD::AND, MVT::i8, Custom);
240 setOperationAction(ISD::XOR, MVT::i8, Custom);
241
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000242 setOperationAction(ISD::FrameIndex, MVT::i16, Custom);
243 setOperationAction(ISD::CALL, MVT::i16, Custom);
244 setOperationAction(ISD::RET, MVT::Other, Custom);
245
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000246 setOperationAction(ISD::MUL, MVT::i8, Custom);
247 setOperationAction(ISD::MUL, MVT::i16, Expand);
248 setOperationAction(ISD::MUL, MVT::i32, Expand);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000249
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000250 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
251 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
252 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
253 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
254 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
255 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
256 setOperationAction(ISD::MULHU, MVT::i8, Expand);
257 setOperationAction(ISD::MULHU, MVT::i16, Expand);
258 setOperationAction(ISD::MULHU, MVT::i32, Expand);
259 setOperationAction(ISD::MULHS, MVT::i8, Expand);
260 setOperationAction(ISD::MULHS, MVT::i16, Expand);
261 setOperationAction(ISD::MULHS, MVT::i32, Expand);
262
263 setOperationAction(ISD::SRA, MVT::i8, Custom);
264 setOperationAction(ISD::SRA, MVT::i16, Expand);
265 setOperationAction(ISD::SRA, MVT::i32, Expand);
266 setOperationAction(ISD::SHL, MVT::i8, Custom);
267 setOperationAction(ISD::SHL, MVT::i16, Expand);
268 setOperationAction(ISD::SHL, MVT::i32, Expand);
269 setOperationAction(ISD::SRL, MVT::i8, Custom);
270 setOperationAction(ISD::SRL, MVT::i16, Expand);
271 setOperationAction(ISD::SRL, MVT::i32, Expand);
272
273 // PIC16 does not support shift parts
274 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
275 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
276 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
277 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
278 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
279 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
280 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
281 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
282 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
283
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000284
285 // PIC16 does not have a SETCC, expand it to SELECT_CC.
286 setOperationAction(ISD::SETCC, MVT::i8, Expand);
287 setOperationAction(ISD::SELECT, MVT::i8, Expand);
288 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
289 setOperationAction(ISD::BRIND, MVT::Other, Expand);
290
291 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
292 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000293
294 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
295 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
296
297 // Now deduce the information based on the above mentioned
298 // actions
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000299 computeRegisterProperties();
300}
301
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000302// getOutFlag - Extract the flag result if the Op has it.
303static SDValue getOutFlag(SDValue &Op) {
304 // Flag is the last value of the node.
305 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
306
307 assert (Flag.getValueType() == MVT::Flag
308 && "Node does not have an out Flag");
309
310 return Flag;
311}
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000312// Get the TmpOffset for FrameIndex
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000313unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI, unsigned size) {
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000314 std::map<unsigned, unsigned>::iterator
315 MapIt = FiTmpOffsetMap.find(FI);
316 if (MapIt != FiTmpOffsetMap.end())
317 return MapIt->second;
318
319 // This FI (FrameIndex) is not yet mapped, so map it
320 FiTmpOffsetMap[FI] = TmpSize;
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000321 TmpSize += size;
322 return FiTmpOffsetMap[FI];
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000323}
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000324
325// To extract chain value from the SDValue Nodes
326// This function will help to maintain the chain extracting
327// code at one place. In case of any change in future it will
328// help maintain the code.
329static SDValue getChain(SDValue &Op) {
330 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
331
332 // If the last value returned in Flag then the chain is
333 // second last value returned.
334 if (Chain.getValueType() == MVT::Flag)
335 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
336
337 // All nodes may not produce a chain. Therefore following assert
338 // verifies that the node is returning a chain only.
339 assert (Chain.getValueType() == MVT::Other
340 && "Node does not have a chain");
341
342 return Chain;
343}
344
345/// PopulateResults - Helper function to LowerOperation.
346/// If a node wants to return multiple results after lowering,
347/// it stuffs them into an array of SDValue called Results.
348
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000349static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
350 if (N.getOpcode() == ISD::MERGE_VALUES) {
351 int NumResults = N.getNumOperands();
352 for( int i = 0; i < NumResults; i++)
353 Results.push_back(N.getOperand(i));
354 }
355 else
356 Results.push_back(N);
357}
358
359MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
360 return MVT::i8;
361}
362
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000363/// The type legalizer framework of generating legalizer can generate libcalls
364/// only when the operand/result types are illegal.
365/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
366/// For example an arithmetic right shift. These functions are used to lower
367/// such operations that generate libcall for legal types.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000368
369void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000370PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000371 const char *Name) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000372 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000373}
374
375const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000376PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000377 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000378}
379
380SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000381PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000382 MVT RetVT, const SDValue *Ops,
383 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000384 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000385
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000386 TargetLowering::ArgListTy Args;
387 Args.reserve(NumOps);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000388
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000389 TargetLowering::ArgListEntry Entry;
390 for (unsigned i = 0; i != NumOps; ++i) {
391 Entry.Node = Ops[i];
392 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
393 Entry.isSExt = isSigned;
394 Entry.isZExt = !isSigned;
395 Args.push_back(Entry);
396 }
397 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000398
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000399 const Type *RetTy = RetVT.getTypeForMVT();
400 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000401 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000402 false, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000403
404 return CallInfo.first;
405}
406
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000407const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
408 switch (Opcode) {
409 default: return NULL;
410 case PIC16ISD::Lo: return "PIC16ISD::Lo";
411 case PIC16ISD::Hi: return "PIC16ISD::Hi";
412 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
413 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000414 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
415 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000416 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
417 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000418 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000419 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000420 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000421 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000422 case PIC16ISD::BCF: return "PIC16ISD::BCF";
423 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
424 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
425 case PIC16ISD::RLF: return "PIC16ISD::RLF";
426 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000427 case PIC16ISD::CALL: return "PIC16ISD::CALL";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000428 case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000429 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
430 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
431 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000432 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000433 }
434}
435
Duncan Sands1607f052008-12-01 11:39:25 +0000436void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
437 SmallVectorImpl<SDValue>&Results,
438 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000439
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000440 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000441 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000442 Results.push_back(ExpandGlobalAddress(N, DAG));
443 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000444 case ISD::ExternalSymbol:
445 Results.push_back(ExpandExternalSymbol(N, DAG));
446 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000447 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000448 Results.push_back(ExpandStore(N, DAG));
449 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000450 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000451 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000452 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000453 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000454 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000455 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000456 case ISD::FrameIndex:
457 Results.push_back(ExpandFrameIndex(N, DAG));
458 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000459 default:
460 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000461 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000462 }
463}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000464
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000465SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
466
467 // Currently handling FrameIndex of size MVT::i16 only
468 // One example of this scenario is when return value is written on
469 // FrameIndex#0
470
471 if (N->getValueType(0) != MVT::i16)
472 return SDValue();
473
474 // Expand the FrameIndex into ExternalSymbol and a Constant node
475 // The constant will represent the frame index number
476 // Get the current function frame
477 MachineFunction &MF = DAG.getMachineFunction();
478 const Function *Func = MF.getFunction();
479 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000480
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000481 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000482 // FIXME there isn't really debug info here
483 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000484
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000485 // Expand FrameIndex like GlobalAddress and ExternalSymbol
486 // Also use Offset field for lo and hi parts. The default
487 // offset is zero.
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000488
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000489 SDValue ES;
490 int FrameOffset;
491 SDValue FI = SDValue(N,0);
492 LegalizeFrameIndex(FI, DAG, ES, FrameOffset);
493 SDValue Offset = DAG.getConstant(FrameOffset, MVT::i8);
494 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, ES, Offset);
495 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, ES, Offset);
496 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000497}
498
499
500SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000501 StoreSDNode *St = cast<StoreSDNode>(N);
502 SDValue Chain = St->getChain();
503 SDValue Src = St->getValue();
504 SDValue Ptr = St->getBasePtr();
505 MVT ValueType = Src.getValueType();
506 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000507 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000508
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000509 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000510 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000511
512 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000513 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000514 PtrLo, PtrHi,
515 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000516 }
517 else if (ValueType == MVT::i16) {
518 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
519 SDValue SrcLo, SrcHi;
520 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
521 SDValue ChainLo = Chain, ChainHi = Chain;
522 if (Chain.getOpcode() == ISD::TokenFactor) {
523 ChainLo = Chain.getOperand(0);
524 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000525 }
Dale Johannesende064702009-02-06 21:50:26 +0000526 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000527 ChainLo,
528 SrcLo, PtrLo, PtrHi,
529 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000530
Dale Johannesende064702009-02-06 21:50:26 +0000531 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000532 SrcHi, PtrLo, PtrHi,
533 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000534
Dale Johannesende064702009-02-06 21:50:26 +0000535 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000536 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000537 }
538 else if (ValueType == MVT::i32) {
539 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
540 SDValue SrcLo, SrcHi;
541 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000542
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000543 // Get the expanded parts of each of SrcLo and SrcHi.
544 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
545 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
546 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000547
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000548 SDValue ChainLo = Chain, ChainHi = Chain;
549 if (Chain.getOpcode() == ISD::TokenFactor) {
550 ChainLo = Chain.getOperand(0);
551 ChainHi = Chain.getOperand(1);
552 }
553 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
554 ChainHi2 = ChainHi;
555 if (ChainLo.getOpcode() == ISD::TokenFactor) {
556 ChainLo1 = ChainLo.getOperand(0);
557 ChainLo2 = ChainLo.getOperand(1);
558 }
559 if (ChainHi.getOpcode() == ISD::TokenFactor) {
560 ChainHi1 = ChainHi.getOperand(0);
561 ChainHi2 = ChainHi.getOperand(1);
562 }
Dale Johannesende064702009-02-06 21:50:26 +0000563 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000564 ChainLo1,
565 SrcLo1, PtrLo, PtrHi,
566 DAG.getConstant (0 + StoreOffset, MVT::i8));
567
Dale Johannesende064702009-02-06 21:50:26 +0000568 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000569 SrcLo2, PtrLo, PtrHi,
570 DAG.getConstant (1 + StoreOffset, MVT::i8));
571
Dale Johannesende064702009-02-06 21:50:26 +0000572 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000573 SrcHi1, PtrLo, PtrHi,
574 DAG.getConstant (2 + StoreOffset, MVT::i8));
575
Dale Johannesende064702009-02-06 21:50:26 +0000576 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000577 SrcHi2, PtrLo, PtrHi,
578 DAG.getConstant (3 + StoreOffset, MVT::i8));
579
Dale Johannesende064702009-02-06 21:50:26 +0000580 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
581 getChain(Store1), getChain(Store2));
582 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
583 getChain(Store3), getChain(Store4));
584 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000585
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000586 }
587 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000588 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000589 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000590 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000591}
592
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000593SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
594{
595 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000596 // FIXME there isn't really debug info here
597 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000598
599 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000600 SDValue Offset = DAG.getConstant(0, MVT::i8);
601 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES, Offset);
602 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES, Offset);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000603
Dale Johannesende064702009-02-06 21:50:26 +0000604 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000605}
606
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000607// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000608SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000609 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000610 // FIXME there isn't really debug info here
611 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000612
613 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
614 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000615
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000616 SDValue Offset = DAG.getConstant(0, MVT::i8);
617 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA, Offset);
618 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA, Offset);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000619
Dale Johannesende064702009-02-06 21:50:26 +0000620 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000621}
622
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000623bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
624 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000625
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000626 if (Op.getOpcode() == ISD::BUILD_PAIR) {
627 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
628 return true;
629 }
630 return false;
631}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000632
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000633// Return true if DirectAddress is in ROM_SPACE
634bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000635
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000636 // RomAddress is a GlobalAddress in ROM_SPACE_
637 // If the Op is not a GlobalAddress return NULL without checking
638 // anything further.
639 if (!isDirectAddress(Op))
640 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000641
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000642 // Its a GlobalAddress.
643 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
644 SDValue TGA = Op.getOperand(0).getOperand(0);
645 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000646
Sanjiv Guptaa3518a12009-04-29 04:43:24 +0000647 if (GSDN->getAddressSpace() == PIC16ISD::ROM_SPACE)
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000648 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000649
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000650 // Any other address space return it false
651 return false;
652}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000653
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000654
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000655// GetExpandedParts - This function is on the similiar lines as
656// the GetExpandedInteger in type legalizer is. This returns expanded
657// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000658
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000659void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
660 SDValue &Lo, SDValue &Hi) {
661 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000662 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000663 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000664
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000665 // Extract the lo component.
666 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
667 DAG.getConstant(0, MVT::i8));
Bill Wendling51b16f42009-05-30 01:09:53 +0000668
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000669 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000670 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
671 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000672}
673
674// Legalize FrameIndex into ExternalSymbol and offset.
675void
676PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
677 SDValue &ES, int &Offset) {
678
679 MachineFunction &MF = DAG.getMachineFunction();
680 const Function *Func = MF.getFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000681 MachineFrameInfo *MFI = MF.getFrameInfo();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000682 const std::string Name = Func->getName();
683
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000684 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000685
686 // FrameIndices are not stack offsets. But they represent the request
687 // for space on stack. That space requested may be more than one byte.
688 // Therefore, to calculate the stack offset that a FrameIndex aligns
689 // with, we need to traverse all the FrameIndices available earlier in
690 // the list and add their requested size.
691 unsigned FIndex = FR->getIndex();
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000692 const char *tmpName;
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000693 if (FIndex < ReservedFrameCount) {
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000694 tmpName = createESName(PAN::getFrameLabel(Name));
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000695 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
696 Offset = 0;
697 for (unsigned i=0; i<FIndex ; ++i) {
698 Offset += MFI->getObjectSize(i);
699 }
700 } else {
701 // FrameIndex has been made for some temporary storage
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000702 tmpName = createESName(PAN::getTempdataLabel(Name));
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000703 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
704 Offset = GetTmpOffsetForFI(FIndex, MFI->getObjectSize(FIndex));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000705 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000706
707 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000708}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000709
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000710// This function legalizes the PIC16 Addresses. If the Pointer is
711// -- Direct address variable residing
712// --> then a Banksel for that variable will be created.
713// -- Rom variable
714// --> then it will be treated as an indirect address.
715// -- Indirect address
716// --> then the address will be loaded into FSR
717// -- ADD with constant operand
718// --> then constant operand of ADD will be returned as Offset
719// and non-constant operand of ADD will be treated as pointer.
720// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000721
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000722void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
723 SDValue &Lo, SDValue &Hi,
724 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000725
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000726 // Offset, by default, should be 0
727 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000728
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000729 // If the pointer is ADD with constant,
730 // return the constant value as the offset
731 if (Ptr.getOpcode() == ISD::ADD) {
732 SDValue OperLeft = Ptr.getOperand(0);
733 SDValue OperRight = Ptr.getOperand(1);
Sanjiv Guptaae992272009-06-23 07:10:19 +0000734 if ((OperLeft.getOpcode() == ISD::Constant) &&
735 (dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue() < 32 )) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000736 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
737 Ptr = OperRight;
Sanjiv Guptaae992272009-06-23 07:10:19 +0000738 } else if ((OperRight.getOpcode() == ISD::Constant) &&
739 (dyn_cast<ConstantSDNode>(OperRight)->getZExtValue() < 32 )){
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000740 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000741 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000742 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000743 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000744
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000745 // If the pointer is Type i8 and an external symbol
746 // then treat it as direct address.
747 // One example for such case is storing and loading
748 // from function frame during a call
749 if (Ptr.getValueType() == MVT::i8) {
750 switch (Ptr.getOpcode()) {
751 case ISD::TargetExternalSymbol:
752 Lo = Ptr;
753 Hi = DAG.getConstant(1, MVT::i8);
754 return;
755 }
756 }
757
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000758 // Expansion of FrameIndex has Lo/Hi parts
759 if (isDirectAddress(Ptr)) {
760 SDValue TFI = Ptr.getOperand(0).getOperand(0);
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000761 int FrameOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000762 if (TFI.getOpcode() == ISD::TargetFrameIndex) {
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000763 LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset);
764 Hi = DAG.getConstant(1, MVT::i8);
765 Offset += FrameOffset;
766 return;
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000767 } else if (TFI.getOpcode() == ISD::TargetExternalSymbol) {
768 // FrameIndex has already been expanded.
769 // Now just make use of its expansion
770 Lo = TFI;
771 Hi = DAG.getConstant(1, MVT::i8);
772 SDValue FOffset = Ptr.getOperand(0).getOperand(1);
773 assert (FOffset.getOpcode() == ISD::Constant &&
774 "Invalid operand of PIC16ISD::Lo");
775 Offset += dyn_cast<ConstantSDNode>(FOffset)->getZExtValue();
776 return;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000777 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000778 }
779
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000780 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
781 // Direct addressing case for RAM variables. The Hi part is constant
782 // and the Lo part is the TGA itself.
783 Lo = Ptr.getOperand(0).getOperand(0);
784
785 // For direct addresses Hi is a constant. Value 1 for the constant
786 // signifies that banksel needs to generated for it. Value 0 for
787 // the constant signifies that banksel does not need to be generated
788 // for it. Mark it as 1 now and optimize later.
789 Hi = DAG.getConstant(1, MVT::i8);
790 return;
791 }
792
793 // Indirect addresses. Get the hi and lo parts of ptr.
794 GetExpandedParts(Ptr, DAG, Lo, Hi);
795
796 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000797 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
798 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000799
800 return;
801}
802
Duncan Sands1607f052008-12-01 11:39:25 +0000803SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000804 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
805 SDValue Chain = LD->getChain();
806 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000807 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000808
809 SDValue Load, Offset;
810 SDVTList Tys;
811 MVT VT, NewVT;
812 SDValue PtrLo, PtrHi;
813 unsigned LoadOffset;
814
815 // Legalize direct/indirect addresses. This will give the lo and hi parts
816 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000817 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000818
819 // Load from the pointer (direct address or FSR)
820 VT = N->getValueType(0);
821 unsigned NumLoads = VT.getSizeInBits() / 8;
822 std::vector<SDValue> PICLoads;
823 unsigned iter;
824 MVT MemVT = LD->getMemoryVT();
825 if(ISD::isNON_EXTLoad(N)) {
826 for (iter=0; iter<NumLoads ; ++iter) {
827 // Add the pointer offset if any
828 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
829 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000830 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000831 Offset);
832 PICLoads.push_back(Load);
833 }
834 } else {
835 // If it is extended load then use PIC16Load for Memory Bytes
836 // and for all extended bytes perform action based on type of
837 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
838
839
840 // For extended loads this is the memory value type
841 // i.e. without any extension
842 MVT MemVT = LD->getMemoryVT();
843 unsigned MemBytes = MemVT.getSizeInBits() / 8;
844 unsigned ExtdBytes = VT.getSizeInBits() / 8;
845 Offset = DAG.getConstant(LoadOffset, MVT::i8);
846
847 Tys = DAG.getVTList(MVT::i8, MVT::Other);
848 // For MemBytes generate PIC16Load with proper offset
849 for (iter=0; iter<MemBytes; ++iter) {
850 // Add the pointer offset if any
851 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000852 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000853 Offset);
854 PICLoads.push_back(Load);
855 }
856
857 // For SignExtendedLoad
858 if (ISD::isSEXTLoad(N)) {
859 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
860 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000861 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000862 DAG.getConstant(7, MVT::i8));
863 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
864 PICLoads.push_back(SRA);
865 }
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000866 } else if (ISD::isZEXTLoad(N) || ISD::isEXTLoad(N)) {
867 //} else if (ISD::isZEXTLoad(N)) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000868 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
869 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
870 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
871 PICLoads.push_back(ConstZero);
872 }
873 }
874 }
875 SDValue BP;
876
877 if (VT == MVT::i8) {
878 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000879 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000880 }
881 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000882 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000883 if (MemVT == MVT::i8)
884 Chain = getChain(PICLoads[0]);
885 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000886 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
887 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000888 } else if (VT == MVT::i32) {
889 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000890 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
891 PICLoads[0], PICLoads[1]);
892 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
893 PICLoads[2], PICLoads[3]);
894 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000895 if (MemVT == MVT::i8)
896 Chain = getChain(PICLoads[0]);
897 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000898 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
899 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000900 else {
901 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000902 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000903 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000904 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000905 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000906 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
907 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000908 }
909 }
910 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000911 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000912}
913
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000914SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
915 // We should have handled larger operands in type legalizer itself.
916 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
917
918 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000919 SDValue Value = N->getOperand(0);
920 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000921 PIC16ISD::PIC16Libcall CallCode;
922 switch (N->getOpcode()) {
923 case ISD::SRA:
924 CallCode = PIC16ISD::SRA_I8;
925 break;
926 case ISD::SHL:
927 CallCode = PIC16ISD::SLL_I8;
928 break;
929 case ISD::SRL:
930 CallCode = PIC16ISD::SRL_I8;
931 break;
932 default:
933 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000934 return SDValue();
935 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000936 SmallVector<SDValue, 2> Ops(2);
937 Ops[0] = Value;
938 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000939 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
940 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000941 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000942}
943
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000944void
945PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
946 SmallVectorImpl<SDValue>&Results,
947 SelectionDAG &DAG) {
948 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000949 SDValue Res;
950 unsigned i;
951 switch (Op.getOpcode()) {
952 case ISD::FORMAL_ARGUMENTS:
953 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
954 case ISD::LOAD:
955 Res = ExpandLoad(Op.getNode(), DAG); break;
956 case ISD::CALL:
957 Res = LowerCALL(Op, DAG); break;
958 default: {
959 // All other operations are handled in LowerOperation.
960 Res = LowerOperation(Op, DAG);
961 if (Res.getNode())
962 Results.push_back(Res);
963
964 return;
965 }
966 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000967
968 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000969 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000970 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000971 Results.push_back(SDValue(N, i));
972 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000973}
974
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000975SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
976 switch (Op.getOpcode()) {
977 case ISD::FORMAL_ARGUMENTS:
978 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000979 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000980 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000981 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000982 return LowerADD(Op, DAG);
983 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000984 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000985 case ISD::SUBE:
986 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000987 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000988 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000989 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000990 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000991 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000992 case ISD::SRA:
993 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000994 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000995 case ISD::OR:
996 case ISD::AND:
997 case ISD::XOR:
998 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000999 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001000 return LowerCALL(Op, DAG);
1001 case ISD::RET:
1002 return LowerRET(Op, DAG);
1003 case ISD::BR_CC:
1004 return LowerBR_CC(Op, DAG);
1005 case ISD::SELECT_CC:
1006 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001007 }
Dan Gohman475871a2008-07-27 21:46:04 +00001008 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001009}
1010
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001011SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001012 SelectionDAG &DAG,
1013 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001014 assert (Op.getValueType() == MVT::i8
1015 && "illegal value type to store on stack.");
1016
1017 MachineFunction &MF = DAG.getMachineFunction();
1018 const Function *Func = MF.getFunction();
1019 const std::string FuncName = Func->getName();
1020
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001021
1022 // Put the value on stack.
1023 // Get a stack slot index and convert to es.
1024 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001025 const char *tmpName = createESName(PAN::getTempdataLabel(FuncName));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001026 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1027
1028 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001029 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001030 DAG.getEntryNode(),
1031 Op, ES,
1032 DAG.getConstant (1, MVT::i8), // Banksel.
Sanjiv Guptaa3613be2009-04-10 15:10:14 +00001033 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001034 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001035
1036 // Load the value from ES.
1037 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001038 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001039 ES, DAG.getConstant (1, MVT::i8),
Sanjiv Guptaa3613be2009-04-10 15:10:14 +00001040 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001041 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001042
1043 return Load.getValue(0);
1044}
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001045
1046SDValue PIC16TargetLowering::
1047LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
1048 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1049 SelectionDAG &DAG) {
1050 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1051 unsigned NumOps = TheCall->getNumArgs();
1052 DebugLoc dl = TheCall->getDebugLoc();
1053
1054 // If call has no arguments then do nothing and return.
1055 if (NumOps == 0)
1056 return Chain;
1057
1058 std::vector<SDValue> Ops;
1059 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1060 SDValue Arg, StoreRet;
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001061
1062 // For PIC16 ABI the arguments come after the return value.
1063 unsigned RetVals = TheCall->getNumRetVals();
1064 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001065 // Get the arguments
1066 Arg = TheCall->getArg(i);
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001067
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001068 Ops.clear();
1069 Ops.push_back(Chain);
1070 Ops.push_back(Arg);
1071 Ops.push_back(DataAddr_Lo);
1072 Ops.push_back(DataAddr_Hi);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001073 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001074 Ops.push_back(InFlag);
1075
1076 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
1077
1078 Chain = getChain(StoreRet);
1079 InFlag = getOutFlag(StoreRet);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001080 ArgOffset++;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001081 }
1082 return Chain;
1083}
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001084
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001085SDValue PIC16TargetLowering::
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001086LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue ArgLabel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001087 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001088 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1089 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +00001090 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001091 std::string Name;
1092 SDValue Arg, StoreAt;
1093 MVT ArgVT;
1094 unsigned Size=0;
1095 unsigned ArgCount=0;
1096
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001097 // If call has no arguments then do nothing and return.
1098 if (NumOps == 0)
1099 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001100
1101 // FIXME: This portion of code currently assumes only
1102 // primitive types being passed as arguments.
1103
1104 // Legalize the address before use
1105 SDValue PtrLo, PtrHi;
1106 unsigned AddressOffset;
1107 int StoreOffset = 0;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001108 LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001109 SDValue StoreRet;
1110
1111 std::vector<SDValue> Ops;
1112 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1113 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
1114 // Get the argument
1115 Arg = TheCall->getArg(i);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001116 StoreOffset = (Offset + AddressOffset);
1117
1118 // Store the argument on frame
1119
1120 Ops.clear();
1121 Ops.push_back(Chain);
Sanjiv Gupta12f23a82009-04-13 09:38:38 +00001122 Ops.push_back(Arg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001123 Ops.push_back(PtrLo);
1124 Ops.push_back(PtrHi);
1125 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
1126 Ops.push_back(InFlag);
1127
Dale Johannesene8d72302009-02-06 23:05:02 +00001128 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001129
1130 Chain = getChain(StoreRet);
1131 InFlag = getOutFlag(StoreRet);
1132
1133 // Update the frame offset to be used for next argument
1134 ArgVT = Arg.getValueType();
1135 Size = ArgVT.getSizeInBits();
1136 Size = Size/8; // Calculate size in bytes
1137 Offset += Size; // Increase the frame offset
1138 }
1139 return Chain;
1140}
1141
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001142SDValue PIC16TargetLowering::
1143LowerIndirectCallReturn (SDValue Op, SDValue Chain, SDValue InFlag,
1144 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1145 SelectionDAG &DAG) {
1146 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1147 DebugLoc dl = TheCall->getDebugLoc();
1148 unsigned RetVals = TheCall->getNumRetVals();
1149
1150 // If call does not have anything to return
1151 // then do nothing and go back.
1152 if (RetVals == 0)
1153 return Chain;
1154
1155 // Call has something to return
1156 std::vector<SDValue> ResultVals;
1157 SDValue LoadRet;
1158
1159 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1160 for(unsigned i=0;i<RetVals;i++) {
1161 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
1162 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
1163 InFlag);
1164 InFlag = getOutFlag(LoadRet);
1165 Chain = getChain(LoadRet);
1166 ResultVals.push_back(LoadRet);
1167 }
1168 ResultVals.push_back(Chain);
1169 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
1170 return Res;
1171}
1172
1173SDValue PIC16TargetLowering::
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001174LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue RetLabel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001175 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001176 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001177 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001178 // Currently handling primitive types only. They will come in
1179 // i8 parts
1180 unsigned RetVals = TheCall->getNumRetVals();
1181
1182 std::vector<SDValue> ResultVals;
1183
1184 // Return immediately if the return type is void
1185 if (RetVals == 0)
1186 return Chain;
1187
1188 // Call has something to return
1189
1190 // Legalize the address before use
1191 SDValue LdLo, LdHi;
1192 unsigned LdOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001193 LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001194
1195 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1196 SDValue LoadRet;
1197
1198 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1199
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001200 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001201 DAG.getConstant(LdOffset + Offset, MVT::i8),
1202 InFlag);
1203
1204 InFlag = getOutFlag(LoadRet);
1205
1206 Chain = getChain(LoadRet);
1207 Offset++;
1208 ResultVals.push_back(LoadRet);
1209 }
1210
1211 // To return use MERGE_VALUES
1212 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001213 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001214 return Res;
1215}
1216
1217SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001218 SDValue Chain = Op.getOperand(0);
1219 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001220
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001221 if (Op.getNumOperands() == 1) // return void
1222 return Op;
1223
1224 // return should have odd number of operands
1225 if ((Op.getNumOperands() % 2) == 0 ) {
1226 assert(0 && "Do not know how to return this many arguments!");
1227 abort();
1228 }
1229
1230 // Number of values to return
1231 unsigned NumRet = (Op.getNumOperands() / 2);
1232
1233 // Function returns value always on stack with the offset starting
1234 // from 0
1235 MachineFunction &MF = DAG.getMachineFunction();
1236 const Function *F = MF.getFunction();
1237 std::string FuncName = F->getName();
1238
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001239 const char *tmpName = createESName(PAN::getFrameLabel(FuncName));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001240 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1241 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1242 SDValue BS = DAG.getConstant(1, MVT::i8);
1243 SDValue RetVal;
1244 for(unsigned i=0;i<NumRet; ++i) {
1245 RetVal = Op.getNode()->getOperand(2*i + 1);
1246 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1247 ES, BS,
1248 DAG.getConstant (i, MVT::i8));
1249
1250 }
1251 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001252}
1253
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001254// CALL node may have some operands non-legal to PIC16. Generate new CALL
1255// node with all the operands legal.
1256// Currently only Callee operand of the CALL node is non-legal. This function
1257// legalizes the Callee operand and uses all other operands as are to generate
1258// new CALL node.
1259
1260SDValue PIC16TargetLowering::LegalizeCALL(SDValue Op, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001261 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1262 SDValue Chain = TheCall->getChain();
1263 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001264 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001265 unsigned i =0;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001266
1267 assert(Callee.getValueType() == MVT::i16 &&
1268 "Don't know how to legalize this call node!!!");
1269 assert(Callee.getOpcode() == ISD::BUILD_PAIR &&
1270 "Don't know how to legalize this call node!!!");
1271
1272 if (isDirectAddress(Callee)) {
1273 // Come here for direct calls
1274 Callee = Callee.getOperand(0).getOperand(0);
1275 } else {
1276 // Come here for indirect calls
1277 SDValue Lo, Hi;
1278 // Indirect addresses. Get the hi and lo parts of ptr.
1279 GetExpandedParts(Callee, DAG, Lo, Hi);
1280 // Connect Lo and Hi parts of the callee with the PIC16Connect
1281 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1282 }
1283 std::vector<SDValue> Ops;
1284 Ops.push_back(Chain);
1285 Ops.push_back(Callee);
1286
1287 // Add the call arguments and their flags
1288 unsigned NumArgs = TheCall->getNumArgs();
1289 for(i=0;i<NumArgs;i++) {
1290 Ops.push_back(TheCall->getArg(i));
1291 Ops.push_back(TheCall->getArgFlagsVal(i));
1292 }
1293 std::vector<MVT> NodeTys;
1294 unsigned NumRets = TheCall->getNumRetVals();
1295 for(i=0;i<NumRets;i++)
1296 NodeTys.push_back(TheCall->getRetValType(i));
1297
1298 // Return a Chain as well
1299 NodeTys.push_back(MVT::Other);
1300
1301 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1302 // Generate new call with all the operands legal
1303 return DAG.getCall(TheCall->getCallingConv(), dl,
1304 TheCall->isVarArg(), TheCall->isTailCall(),
1305 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
1306}
1307
1308void PIC16TargetLowering::
1309GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1310 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1311 SelectionDAG &DAG) {
1312 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1313 && "Don't know what to do of such callee!!");
1314 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1315 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1316 Chain = getChain(SeqStart);
1317 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1318
1319 // Get the Lo and Hi part of code address
1320 SDValue Lo = Callee.getOperand(0);
1321 SDValue Hi = Callee.getOperand(1);
1322
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001323 SDValue Data_Lo, Data_Hi;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001324 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001325 // Subtract 2 from Address to get the Lower part of DataAddress.
1326 SDVTList VTList = DAG.getVTList(MVT::i8, MVT::Flag);
1327 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1328 DAG.getConstant(2, MVT::i8));
1329 SDValue Ops[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1330 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, Ops, 3);
1331 SDValue PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1332 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001333 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1334 OperFlag);
1335 Chain = getChain(Call);
1336 OperFlag = getOutFlag(Call);
1337 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1338 OperFlag);
1339 Chain = getChain(SeqEnd);
1340 OperFlag = getOutFlag(SeqEnd);
1341
1342 // Low part of Data Address
1343 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1344
1345 // Make the second call.
1346 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1347 Chain = getChain(SeqStart);
1348 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1349
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001350 // Subtract 1 from Address to get high part of data address.
1351 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1352 DAG.getConstant(1, MVT::i8));
1353 SDValue HiOps[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1354 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1355 PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1356
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001357 // Use new Lo to make another CALLW
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001358 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001359 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1360 Chain = getChain(Call);
1361 OperFlag = getOutFlag(Call);
1362 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1363 OperFlag);
1364 Chain = getChain(SeqEnd);
1365 OperFlag = getOutFlag(SeqEnd);
1366 // Hi part of Data Address
1367 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1368}
1369
1370
1371SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
1372 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1373 SDValue Chain = TheCall->getChain();
1374 SDValue Callee = TheCall->getCallee();
1375 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001376 if (Callee.getValueType() == MVT::i16 &&
1377 Callee.getOpcode() == ISD::BUILD_PAIR) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001378 // Control should come here only from TypeLegalizer for lowering
1379
1380 // Legalize the non-legal arguments of call and return the
1381 // new call with legal arguments.
1382 return LegalizeCALL(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001383 }
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001384 // Control should come here from Legalize DAG.
1385 // Here all the operands of CALL node should be legal.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001386
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001387 // If this is an indirect call then to pass the arguments
1388 // and read the return value back, we need the data address
1389 // of the function being called.
1390 // To get the data address two more calls need to be made.
1391
1392 // The flag to track if this is a direct or indirect call.
1393 bool IsDirectCall = true;
1394 unsigned RetVals = TheCall->getNumRetVals();
1395 unsigned NumArgs = TheCall->getNumArgs();
1396
1397 SDValue DataAddr_Lo, DataAddr_Hi;
1398 if (Callee.getOpcode() == PIC16ISD::PIC16Connect) {
1399 IsDirectCall = false; // This is indirect call
1400 // Read DataAddress only if we have to pass arguments or
1401 // read return value.
1402 if ((RetVals > 0) || (NumArgs > 0))
1403 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1404 }
1405
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001406 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1407
1408 // Start the call sequence.
1409 // Carring the Constant 0 along the CALLSEQSTART
1410 // because there is nothing else to carry.
1411 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1412 Chain = getChain(SeqStart);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001413 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1414 std::string Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001415
1416 // For any direct call - callee will be GlobalAddressNode or
1417 // ExternalSymbol
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001418 SDValue ArgLabel, RetLabel;
1419 if (IsDirectCall) {
1420 // Considering the GlobalAddressNode case here.
1421 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1422 GlobalValue *GV = G->getGlobal();
1423 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1424 Name = G->getGlobal()->getName();
1425 } else {// Considering the ExternalSymbol case here
1426 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1427 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1428 Name = ES->getSymbol();
1429 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001430
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001431 // Label for argument passing
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001432 const char *argFrame = createESName(PAN::getArgsLabel(Name));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001433 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001434
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001435 // Label for reading return value
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001436 const char *retName = createESName(PAN::getRetvalLabel(Name));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001437 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1438 } else {
1439 // if indirect call
1440 SDValue CodeAddr_Lo = Callee.getOperand(0);
1441 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001442
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001443 /*CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1444 DAG.getConstant(2, MVT::i8));*/
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001445
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001446 // move Hi part in PCLATH
1447 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1448 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1449 CodeAddr_Hi);
1450 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001451
1452 // Pass the argument to function before making the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001453 SDValue CallArgs;
1454 if (IsDirectCall) {
1455 CallArgs = LowerDirectCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1456 Chain = getChain(CallArgs);
1457 OperFlag = getOutFlag(CallArgs);
1458 } else {
1459 CallArgs = LowerIndirectCallArguments(Op, Chain, OperFlag, DataAddr_Lo,
1460 DataAddr_Hi, DAG);
1461 Chain = getChain(CallArgs);
1462 OperFlag = getOutFlag(CallArgs);
1463 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001464
1465 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001466 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001467 OperFlag);
1468 Chain = getChain(PICCall);
1469 OperFlag = getOutFlag(PICCall);
1470
1471
1472 // Carrying the Constant 0 along the CALLSEQSTART
1473 // because there is nothing else to carry.
1474 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1475 OperFlag);
1476 Chain = getChain(SeqEnd);
1477 OperFlag = getOutFlag(SeqEnd);
1478
1479 // Lower the return value reading after the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001480 if (IsDirectCall)
1481 return LowerDirectCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1482 else
1483 return LowerIndirectCallReturn(Op, Chain, OperFlag, DataAddr_Lo,
1484 DataAddr_Hi, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001485}
1486
1487bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1488 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1489 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1490 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1491 return true;
1492 return false;
1493}
1494
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001495// NeedToConvertToMemOp - Returns true if one of the operands of the
1496// operation 'Op' needs to be put into memory. Also returns the
1497// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1498// no instruction that can operation on two registers. Most insns take
1499// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001500bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001501 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001502 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1503 Op.getOperand(1).getOpcode() == ISD::Constant)
1504 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001505
1506 // Return false if one of the operands is already a direct
1507 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001508 if (isDirectLoad(Op.getOperand(0))) {
1509 if (Op.getOperand(0).hasOneUse())
1510 return false;
1511 else
1512 MemOp = 0;
1513 }
1514 if (isDirectLoad(Op.getOperand(1))) {
1515 if (Op.getOperand(1).hasOneUse())
1516 return false;
1517 else
1518 MemOp = 1;
1519 }
1520 return true;
1521}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001522
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001523// LowerBinOp - Lower a commutative binary operation that does not
1524// affect status flag carry.
1525SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001526 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001527
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001528 // We should have handled larger operands in type legalizer itself.
1529 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001530
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001531 unsigned MemOp = 1;
1532 if (NeedToConvertToMemOp(Op, MemOp)) {
1533 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001534 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001535
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001536 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001537 NewVal);
1538 }
1539 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001540 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001541 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001542}
1543
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001544// LowerADD - Lower all types of ADD operations including the ones
1545// that affects carry.
1546SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001547 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001548 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001549 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001550 unsigned MemOp = 1;
1551 if (NeedToConvertToMemOp(Op, MemOp)) {
1552 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001553 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001554
Dale Johannesen83138992009-06-01 23:13:42 +00001555 // ADDC and ADDE produce two results.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001556 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001557
Dale Johannesen83138992009-06-01 23:13:42 +00001558 // ADDE has three operands, the last one is the carry bit.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001559 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001560 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1561 NewVal, Op.getOperand(2));
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001562 // ADDC has two operands.
1563 else if (Op.getOpcode() == ISD::ADDC)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001564 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1565 NewVal);
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001566 // ADD it is. It produces only one result.
1567 else
1568 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1569 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001570 }
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001571 else
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001572 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001573}
1574
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001575SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001576 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001577 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001578 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001579
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001580 // Nothing to do if the first operand is already a direct load and it has
1581 // only one use.
1582 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001583 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001584
1585 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001586 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001587
1588 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001589 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001590 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001591 Op.getOperand(2));
1592 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001593 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001594}
1595
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001596void PIC16TargetLowering::InitReservedFrameCount(const Function *F) {
1597 unsigned NumArgs = F->arg_size();
1598
1599 bool isVoidFunc = (F->getReturnType()->getTypeID() == Type::VoidTyID);
1600
1601 if (isVoidFunc)
1602 ReservedFrameCount = NumArgs;
1603 else
1604 ReservedFrameCount = NumArgs + 1;
1605}
1606
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001607// LowerFORMAL_ARGUMENTS - Argument values are loaded from the
1608// <fname>.args + offset. All arguments are already broken to leaglized
1609// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001610
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001611SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001612 SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001613 SmallVector<SDValue, 8> ArgValues;
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001614 unsigned NumArgVals = Op.getNode()->getNumValues() - 1;
Dale Johannesene8d72302009-02-06 23:05:02 +00001615 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001616 SDValue Chain = Op.getOperand(0); // Formal arguments' chain
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001617
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001618
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001619 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001620 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001621 const Function *F = MF.getFunction();
1622 std::string FuncName = F->getName();
1623
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001624 // Reset the map of FI and TmpOffset
1625 ResetTmpOffsetMap();
1626 // Initialize the ReserveFrameCount
1627 InitReservedFrameCount(F);
1628
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001629 // Create the <fname>.args external symbol.
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001630 const char *tmpName = createESName(PAN::getArgsLabel(FuncName));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001631 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001632
1633 // Load arg values from the label + offset.
1634 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001635 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001636 for (unsigned i = 0; i < NumArgVals ; ++i) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001637 SDValue Offset = DAG.getConstant(i, MVT::i8);
1638 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001639 Offset);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001640 Chain = getChain(PICLoad);
1641 ArgValues.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001642 }
1643
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001644 // Return a MERGE_VALUE node.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001645 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001646 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001647 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001648}
1649
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001650// Perform DAGCombine of PIC16Load.
1651// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001652SDValue PIC16TargetLowering::
1653PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1654 SelectionDAG &DAG = DCI.DAG;
1655 SDValue Chain = N->getOperand(0);
1656 if (N->hasNUsesOfValue(0, 0)) {
1657 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1658 }
1659 return SDValue();
1660}
1661
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001662// For all the functions with arguments some STORE nodes are generated
1663// that store the argument on the frameindex. However in PIC16 the arguments
1664// are passed on stack only. Therefore these STORE nodes are redundant.
1665// To remove these STORE nodes will be removed in PerformStoreCombine
1666//
1667// Currently this function is doint nothing and will be updated for removing
1668// unwanted store operations
1669SDValue PIC16TargetLowering::
1670PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001671 return SDValue(N, 0);
1672 /*
1673 // Storing an undef value is of no use, so remove it
1674 if (isStoringUndef(N, Chain, DAG)) {
1675 return Chain; // remove the store and return the chain
1676 }
1677 //else everything is ok.
1678 return SDValue(N, 0);
1679 */
1680}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001681
1682SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1683 DAGCombinerInfo &DCI) const {
1684 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001685 case ISD::STORE:
1686 return PerformStoreCombine(N, DCI);
1687 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001688 return PerformPIC16LoadCombine(N, DCI);
1689 }
1690 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001691}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001692
1693static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1694 switch (CC) {
1695 default: assert(0 && "Unknown condition code!");
1696 case ISD::SETNE: return PIC16CC::NE;
1697 case ISD::SETEQ: return PIC16CC::EQ;
1698 case ISD::SETGT: return PIC16CC::GT;
1699 case ISD::SETGE: return PIC16CC::GE;
1700 case ISD::SETLT: return PIC16CC::LT;
1701 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001702 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta703e2352009-06-03 13:36:44 +00001703 case ISD::SETULE: return PIC16CC::ULE;
1704 case ISD::SETUGE: return PIC16CC::UGE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001705 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001706 }
1707}
1708
1709// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1710// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1711static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1712 ISD::CondCode CC, unsigned &SPCC) {
1713 if (isa<ConstantSDNode>(RHS) &&
1714 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1715 CC == ISD::SETNE &&
1716 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1717 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1718 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1719 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1720 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1721 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1722 SDValue CMPCC = LHS.getOperand(3);
1723 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1724 LHS = CMPCC.getOperand(0);
1725 RHS = CMPCC.getOperand(1);
1726 }
1727}
1728
1729// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1730SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1731 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001732 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001733 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1734
1735 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1736 // i.e. a < 12 can be rewritten as 12 > a.
1737 if (RHS.getOpcode() == ISD::Constant) {
1738
1739 SDValue Tmp = LHS;
1740 LHS = RHS;
1741 RHS = Tmp;
1742
1743 switch (CondCode) {
1744 default: break;
1745 case PIC16CC::LT:
1746 CondCode = PIC16CC::GT;
1747 break;
1748 case PIC16CC::GT:
1749 CondCode = PIC16CC::LT;
1750 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001751 case PIC16CC::ULT:
1752 CondCode = PIC16CC::UGT;
1753 break;
1754 case PIC16CC::UGT:
1755 CondCode = PIC16CC::ULT;
1756 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001757 case PIC16CC::GE:
1758 CondCode = PIC16CC::LE;
1759 break;
1760 case PIC16CC::LE:
1761 CondCode = PIC16CC::GE;
1762 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001763 case PIC16CC::ULE:
1764 CondCode = PIC16CC::UGE;
1765 break;
1766 case PIC16CC::UGE:
1767 CondCode = PIC16CC::ULE;
1768 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001769 }
1770 }
1771
1772 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001773
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001774 // These are signed comparisons.
1775 SDValue Mask = DAG.getConstant(128, MVT::i8);
1776 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001777 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1778 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001779 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001780
1781 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001782 // We can use a subtract operation to set the condition codes. But
1783 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001784 // Nothing to do if the first operand is already a valid type (direct load
1785 // for subwf and literal for sublw) and it is used by this operation only.
1786 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1787 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001788 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001789
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001790 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001791 LHS = ConvertToMemOperand (LHS, DAG, dl);
1792 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001793}
1794
1795
1796SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1797 SDValue LHS = Op.getOperand(0);
1798 SDValue RHS = Op.getOperand(1);
1799 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1800 SDValue TrueVal = Op.getOperand(2);
1801 SDValue FalseVal = Op.getOperand(3);
1802 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001803 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001804
1805 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1806 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1807 // i.e.
1808 // A setcc: lhs, rhs, cc is expanded by llvm to
1809 // select_cc: result of setcc, 0, 1, 0, setne
1810 // We can think of it as:
1811 // select_cc: lhs, rhs, 1, 0, cc
1812 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1813 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1814
1815 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001816 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001817
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001818 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001819 FalseVal, PIC16CC, Cmp.getValue(1));
1820}
1821
1822MachineBasicBlock *
1823PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001824 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001825 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1826 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001827 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001828
1829 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1830 // control-flow pattern. The incoming instruction knows the destination vreg
1831 // to set, the condition code register to branch on, the true/false values to
1832 // select between, and a branch opcode to use.
1833 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1834 MachineFunction::iterator It = BB;
1835 ++It;
1836
1837 // thisMBB:
1838 // ...
1839 // TrueVal = ...
1840 // [f]bCC copy1MBB
1841 // fallthrough --> copy0MBB
1842 MachineBasicBlock *thisMBB = BB;
1843 MachineFunction *F = BB->getParent();
1844 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1845 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001846 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001847 F->insert(It, copy0MBB);
1848 F->insert(It, sinkMBB);
1849
1850 // Update machine-CFG edges by transferring all successors of the current
1851 // block to the new block which will contain the Phi node for the select.
1852 sinkMBB->transferSuccessors(BB);
1853 // Next, add the true and fallthrough blocks as its successors.
1854 BB->addSuccessor(copy0MBB);
1855 BB->addSuccessor(sinkMBB);
1856
1857 // copy0MBB:
1858 // %FalseValue = ...
1859 // # fallthrough to sinkMBB
1860 BB = copy0MBB;
1861
1862 // Update machine-CFG edges
1863 BB->addSuccessor(sinkMBB);
1864
1865 // sinkMBB:
1866 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1867 // ...
1868 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001869 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001870 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1871 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1872
1873 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1874 return BB;
1875}
1876
1877
1878SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1879 SDValue Chain = Op.getOperand(0);
1880 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1881 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1882 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1883 SDValue Dest = Op.getOperand(4); // BB to jump to
1884 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001885 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001886
1887 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1888 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1889 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1890 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1891
1892 // Get the Compare insn and condition code.
1893 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001894 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001895
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001896 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001897 Cmp.getValue(1));
1898}
1899