blob: de5d3ac314492b9ab8b9b18f77d7081f699de240 [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"
Sanjiv Gupta753ec152009-10-15 19:26:25 +000015#include "PIC16ABINames.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000016#include "PIC16ISelLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000017#include "PIC16TargetObjectFile.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000018#include "PIC16TargetMachine.h"
Dan Gohman1e93df62010-04-17 14:41:14 +000019#include "PIC16MachineFunctionInfo.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000020#include "llvm/DerivedTypes.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000021#include "llvm/GlobalValue.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000022#include "llvm/Function.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000023#include "llvm/CallingConv.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwin804e0fe2009-07-08 19:04:27 +000028#include "llvm/Support/ErrorHandling.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000029
30
Sanjiv Gupta0e687712008-05-13 09:02:57 +000031using namespace llvm;
32
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000033static const char *getIntrinsicName(unsigned opcode) {
34 std::string Basename;
35 switch(opcode) {
Sanjiv Guptaaa939172009-07-31 07:35:57 +000036 default: llvm_unreachable("do not know intrinsic name");
Sanjiv Gupta003263b2009-06-16 16:17:35 +000037 // Arithmetic Right shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000038 case PIC16ISD::SRA_I8: Basename = "sra.i8"; break;
39 case RTLIB::SRA_I16: Basename = "sra.i16"; break;
40 case RTLIB::SRA_I32: Basename = "sra.i32"; break;
41
Sanjiv Gupta003263b2009-06-16 16:17:35 +000042 // Left shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000043 case PIC16ISD::SLL_I8: Basename = "sll.i8"; break;
44 case RTLIB::SHL_I16: Basename = "sll.i16"; break;
45 case RTLIB::SHL_I32: Basename = "sll.i32"; break;
46
Sanjiv Gupta003263b2009-06-16 16:17:35 +000047 // Logical Right Shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000048 case PIC16ISD::SRL_I8: Basename = "srl.i8"; break;
49 case RTLIB::SRL_I16: Basename = "srl.i16"; break;
50 case RTLIB::SRL_I32: Basename = "srl.i32"; break;
51
Sanjiv Gupta003263b2009-06-16 16:17:35 +000052 // Multiply for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000053 case PIC16ISD::MUL_I8: Basename = "mul.i8"; break;
54 case RTLIB::MUL_I16: Basename = "mul.i16"; break;
55 case RTLIB::MUL_I32: Basename = "mul.i32"; break;
Sanjiv Gupta46800772009-06-05 14:43:12 +000056
Sanjiv Gupta003263b2009-06-16 16:17:35 +000057 // Signed division for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000058 case RTLIB::SDIV_I16: Basename = "sdiv.i16"; break;
59 case RTLIB::SDIV_I32: Basename = "sdiv.i32"; break;
Sanjiv Gupta003263b2009-06-16 16:17:35 +000060
61 // Unsigned division for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000062 case RTLIB::UDIV_I16: Basename = "udiv.i16"; break;
63 case RTLIB::UDIV_I32: Basename = "udiv.i32"; break;
64
Sanjiv Gupta003263b2009-06-16 16:17:35 +000065 // Signed Modulas for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000066 case RTLIB::SREM_I16: Basename = "srem.i16"; break;
67 case RTLIB::SREM_I32: Basename = "srem.i32"; break;
Sanjiv Gupta003263b2009-06-16 16:17:35 +000068
69 // Unsigned Modulas for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000070 case RTLIB::UREM_I16: Basename = "urem.i16"; break;
71 case RTLIB::UREM_I32: Basename = "urem.i32"; break;
Sanjiv Gupta63963292009-06-11 16:50:48 +000072
Sanjiv Gupta003263b2009-06-16 16:17:35 +000073 //////////////////////
74 // LIBCALLS FOR FLOATS
75 //////////////////////
76
77 // Float to signed integrals
78 case RTLIB::FPTOSINT_F32_I8: Basename = "f32_to_si32"; break;
79 case RTLIB::FPTOSINT_F32_I16: Basename = "f32_to_si32"; break;
80 case RTLIB::FPTOSINT_F32_I32: Basename = "f32_to_si32"; break;
81
82 // Signed integrals to float. char and int are first sign extended to i32
83 // before being converted to float, so an I8_F32 or I16_F32 isn't required.
84 case RTLIB::SINTTOFP_I32_F32: Basename = "si32_to_f32"; break;
85
86 // Float to Unsigned conversions.
87 // Signed conversion can be used for unsigned conversion as well.
88 // In signed and unsigned versions only the interpretation of the
89 // MSB is different. Bit representation remains the same.
90 case RTLIB::FPTOUINT_F32_I8: Basename = "f32_to_si32"; break;
91 case RTLIB::FPTOUINT_F32_I16: Basename = "f32_to_si32"; break;
92 case RTLIB::FPTOUINT_F32_I32: Basename = "f32_to_si32"; break;
93
94 // Unsigned to Float conversions. char and int are first zero extended
95 // before being converted to float.
96 case RTLIB::UINTTOFP_I32_F32: Basename = "ui32_to_f32"; break;
Sanjiv Gupta63963292009-06-11 16:50:48 +000097
Sanjiv Gupta003263b2009-06-16 16:17:35 +000098 // Floating point add, sub, mul, div.
Sanjiv Gupta63963292009-06-11 16:50:48 +000099 case RTLIB::ADD_F32: Basename = "add.f32"; break;
100 case RTLIB::SUB_F32: Basename = "sub.f32"; break;
101 case RTLIB::MUL_F32: Basename = "mul.f32"; break;
102 case RTLIB::DIV_F32: Basename = "div.f32"; break;
Sanjiv Guptacbd5f0a2009-06-25 18:12:06 +0000103
104 // Floating point comparison
105 case RTLIB::O_F32: Basename = "unordered.f32"; break;
106 case RTLIB::UO_F32: Basename = "unordered.f32"; break;
107 case RTLIB::OLE_F32: Basename = "le.f32"; break;
108 case RTLIB::OGE_F32: Basename = "ge.f32"; break;
109 case RTLIB::OLT_F32: Basename = "lt.f32"; break;
110 case RTLIB::OGT_F32: Basename = "gt.f32"; break;
111 case RTLIB::OEQ_F32: Basename = "eq.f32"; break;
112 case RTLIB::UNE_F32: Basename = "neq.f32"; break;
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000113 }
114
115 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
116 std::string tagname = PAN::getTagName(PAN::LIBCALL);
117 std::string Fullname = prefix + tagname + Basename;
118
119 // The name has to live through program life.
Sanjiv Guptad49baef2010-04-07 03:36:01 +0000120 return ESNames::createESName(Fullname);
Sanjiv Guptaaa939172009-07-31 07:35:57 +0000121}
122
123// getStdLibCallName - Get the name for the standard library function.
124static const char *getStdLibCallName(unsigned opcode) {
125 std::string BaseName;
126 switch(opcode) {
127 case RTLIB::COS_F32: BaseName = "cos";
128 break;
129 case RTLIB::SIN_F32: BaseName = "sin";
130 break;
131 case RTLIB::MEMCPY: BaseName = "memcpy";
132 break;
133 case RTLIB::MEMSET: BaseName = "memset";
134 break;
135 case RTLIB::MEMMOVE: BaseName = "memmove";
136 break;
137 default: llvm_unreachable("do not know std lib call name");
138 }
139 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
140 std::string LibCallName = prefix + BaseName;
141
142 // The name has to live through program life.
Sanjiv Guptad49baef2010-04-07 03:36:01 +0000143 return ESNames::createESName(LibCallName);
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000144}
145
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000146// PIC16TargetLowering Constructor.
147PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
Dan Gohman1e93df62010-04-17 14:41:14 +0000148 : TargetLowering(TM, new PIC16TargetObjectFile()) {
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000149
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000150 Subtarget = &TM.getSubtarget<PIC16Subtarget>();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000151
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000153
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setShiftAmountType(MVT::i8);
Sanjiv Guptaaa939172009-07-31 07:35:57 +0000155
156 // Std lib call names
157 setLibcallName(RTLIB::COS_F32, getStdLibCallName(RTLIB::COS_F32));
158 setLibcallName(RTLIB::SIN_F32, getStdLibCallName(RTLIB::SIN_F32));
159 setLibcallName(RTLIB::MEMCPY, getStdLibCallName(RTLIB::MEMCPY));
160 setLibcallName(RTLIB::MEMSET, getStdLibCallName(RTLIB::MEMSET));
161 setLibcallName(RTLIB::MEMMOVE, getStdLibCallName(RTLIB::MEMMOVE));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000162
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000163 // SRA library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000164 setPIC16LibcallName(PIC16ISD::SRA_I8, getIntrinsicName(PIC16ISD::SRA_I8));
165 setLibcallName(RTLIB::SRA_I16, getIntrinsicName(RTLIB::SRA_I16));
166 setLibcallName(RTLIB::SRA_I32, getIntrinsicName(RTLIB::SRA_I32));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000167
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000168 // SHL library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000169 setPIC16LibcallName(PIC16ISD::SLL_I8, getIntrinsicName(PIC16ISD::SLL_I8));
170 setLibcallName(RTLIB::SHL_I16, getIntrinsicName(RTLIB::SHL_I16));
171 setLibcallName(RTLIB::SHL_I32, getIntrinsicName(RTLIB::SHL_I32));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000172
173 // SRL library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000174 setPIC16LibcallName(PIC16ISD::SRL_I8, getIntrinsicName(PIC16ISD::SRL_I8));
175 setLibcallName(RTLIB::SRL_I16, getIntrinsicName(RTLIB::SRL_I16));
176 setLibcallName(RTLIB::SRL_I32, getIntrinsicName(RTLIB::SRL_I32));
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000177
178 // MUL Library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000179 setPIC16LibcallName(PIC16ISD::MUL_I8, getIntrinsicName(PIC16ISD::MUL_I8));
180 setLibcallName(RTLIB::MUL_I16, getIntrinsicName(RTLIB::MUL_I16));
181 setLibcallName(RTLIB::MUL_I32, getIntrinsicName(RTLIB::MUL_I32));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000182
Sanjiv Gupta46800772009-06-05 14:43:12 +0000183 // Signed division lib call names
184 setLibcallName(RTLIB::SDIV_I16, getIntrinsicName(RTLIB::SDIV_I16));
185 setLibcallName(RTLIB::SDIV_I32, getIntrinsicName(RTLIB::SDIV_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000186
Sanjiv Gupta46800772009-06-05 14:43:12 +0000187 // Unsigned division lib call names
188 setLibcallName(RTLIB::UDIV_I16, getIntrinsicName(RTLIB::UDIV_I16));
189 setLibcallName(RTLIB::UDIV_I32, getIntrinsicName(RTLIB::UDIV_I32));
190
191 // Signed remainder lib call names
192 setLibcallName(RTLIB::SREM_I16, getIntrinsicName(RTLIB::SREM_I16));
193 setLibcallName(RTLIB::SREM_I32, getIntrinsicName(RTLIB::SREM_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000194
Sanjiv Gupta46800772009-06-05 14:43:12 +0000195 // Unsigned remainder lib call names
196 setLibcallName(RTLIB::UREM_I16, getIntrinsicName(RTLIB::UREM_I16));
197 setLibcallName(RTLIB::UREM_I32, getIntrinsicName(RTLIB::UREM_I32));
Sanjiv Gupta63963292009-06-11 16:50:48 +0000198
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000199 // Floating point to signed int conversions.
200 setLibcallName(RTLIB::FPTOSINT_F32_I8,
201 getIntrinsicName(RTLIB::FPTOSINT_F32_I8));
202 setLibcallName(RTLIB::FPTOSINT_F32_I16,
203 getIntrinsicName(RTLIB::FPTOSINT_F32_I16));
Sanjiv Gupta63963292009-06-11 16:50:48 +0000204 setLibcallName(RTLIB::FPTOSINT_F32_I32,
205 getIntrinsicName(RTLIB::FPTOSINT_F32_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000206
207 // Signed int to floats.
Sanjiv Gupta63963292009-06-11 16:50:48 +0000208 setLibcallName(RTLIB::SINTTOFP_I32_F32,
209 getIntrinsicName(RTLIB::SINTTOFP_I32_F32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000210
211 // Floating points to unsigned ints.
212 setLibcallName(RTLIB::FPTOUINT_F32_I8,
213 getIntrinsicName(RTLIB::FPTOUINT_F32_I8));
214 setLibcallName(RTLIB::FPTOUINT_F32_I16,
215 getIntrinsicName(RTLIB::FPTOUINT_F32_I16));
216 setLibcallName(RTLIB::FPTOUINT_F32_I32,
217 getIntrinsicName(RTLIB::FPTOUINT_F32_I32));
218
219 // Unsigned int to floats.
220 setLibcallName(RTLIB::UINTTOFP_I32_F32,
221 getIntrinsicName(RTLIB::UINTTOFP_I32_F32));
222
223 // Floating point add, sub, mul ,div.
Sanjiv Gupta63963292009-06-11 16:50:48 +0000224 setLibcallName(RTLIB::ADD_F32, getIntrinsicName(RTLIB::ADD_F32));
225 setLibcallName(RTLIB::SUB_F32, getIntrinsicName(RTLIB::SUB_F32));
226 setLibcallName(RTLIB::MUL_F32, getIntrinsicName(RTLIB::MUL_F32));
227 setLibcallName(RTLIB::DIV_F32, getIntrinsicName(RTLIB::DIV_F32));
228
Sanjiv Guptacbd5f0a2009-06-25 18:12:06 +0000229 // Floationg point comparison
Sanjiv Gupta223497e2009-12-29 03:24:34 +0000230 setLibcallName(RTLIB::O_F32, getIntrinsicName(RTLIB::O_F32));
Sanjiv Guptacbd5f0a2009-06-25 18:12:06 +0000231 setLibcallName(RTLIB::UO_F32, getIntrinsicName(RTLIB::UO_F32));
232 setLibcallName(RTLIB::OLE_F32, getIntrinsicName(RTLIB::OLE_F32));
233 setLibcallName(RTLIB::OGE_F32, getIntrinsicName(RTLIB::OGE_F32));
234 setLibcallName(RTLIB::OLT_F32, getIntrinsicName(RTLIB::OLT_F32));
235 setLibcallName(RTLIB::OGT_F32, getIntrinsicName(RTLIB::OGT_F32));
236 setLibcallName(RTLIB::OEQ_F32, getIntrinsicName(RTLIB::OEQ_F32));
237 setLibcallName(RTLIB::UNE_F32, getIntrinsicName(RTLIB::UNE_F32));
238
239 // Return value comparisons of floating point calls.
240 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
241 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
242 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
243 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
244 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
245 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
246 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
247 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
248
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
250 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000251
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::LOAD, MVT::i8, Legal);
253 setOperationAction(ISD::LOAD, MVT::i16, Custom);
254 setOperationAction(ISD::LOAD, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000255
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 setOperationAction(ISD::STORE, MVT::i8, Legal);
257 setOperationAction(ISD::STORE, MVT::i16, Custom);
258 setOperationAction(ISD::STORE, MVT::i32, Custom);
259 setOperationAction(ISD::STORE, MVT::i64, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000260
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setOperationAction(ISD::ADDE, MVT::i8, Custom);
262 setOperationAction(ISD::ADDC, MVT::i8, Custom);
263 setOperationAction(ISD::SUBE, MVT::i8, Custom);
264 setOperationAction(ISD::SUBC, MVT::i8, Custom);
265 setOperationAction(ISD::SUB, MVT::i8, Custom);
266 setOperationAction(ISD::ADD, MVT::i8, Custom);
267 setOperationAction(ISD::ADD, MVT::i16, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000268
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setOperationAction(ISD::OR, MVT::i8, Custom);
270 setOperationAction(ISD::AND, MVT::i8, Custom);
271 setOperationAction(ISD::XOR, MVT::i8, Custom);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000272
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::FrameIndex, MVT::i16, Custom);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000274
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::MUL, MVT::i8, Custom);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000276
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
278 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
279 setOperationAction(ISD::MULHU, MVT::i8, Expand);
280 setOperationAction(ISD::MULHS, MVT::i8, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000281
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::SRA, MVT::i8, Custom);
283 setOperationAction(ISD::SHL, MVT::i8, Custom);
284 setOperationAction(ISD::SRL, MVT::i8, Custom);
Eli Friedmane91bf2c2009-07-17 07:03:00 +0000285
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::ROTL, MVT::i8, Expand);
287 setOperationAction(ISD::ROTR, MVT::i8, Expand);
Eli Friedmane91bf2c2009-07-17 07:03:00 +0000288
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000290
291 // PIC16 does not support shift parts
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
293 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
294 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000295
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000296
297 // PIC16 does not have a SETCC, expand it to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setOperationAction(ISD::SETCC, MVT::i8, Expand);
299 setOperationAction(ISD::SELECT, MVT::i8, Expand);
300 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
301 setOperationAction(ISD::BRIND, MVT::Other, Expand);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000302
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
304 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000305
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
307 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000308
309 // Now deduce the information based on the above mentioned
310 // actions
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000311 computeRegisterProperties();
312}
313
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000314// getOutFlag - Extract the flag result if the Op has it.
315static SDValue getOutFlag(SDValue &Op) {
316 // Flag is the last value of the node.
317 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
318
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 assert (Flag.getValueType() == MVT::Flag
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000320 && "Node does not have an out Flag");
321
322 return Flag;
323}
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000324// Get the TmpOffset for FrameIndex
Dan Gohman1e93df62010-04-17 14:41:14 +0000325unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI, unsigned size,
326 MachineFunction &MF) {
327 PIC16MachineFunctionInfo *FuncInfo = MF.getInfo<PIC16MachineFunctionInfo>();
328 std::map<unsigned, unsigned> &FiTmpOffsetMap = FuncInfo->getFiTmpOffsetMap();
329
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000330 std::map<unsigned, unsigned>::iterator
331 MapIt = FiTmpOffsetMap.find(FI);
332 if (MapIt != FiTmpOffsetMap.end())
333 return MapIt->second;
334
335 // This FI (FrameIndex) is not yet mapped, so map it
Dan Gohman1e93df62010-04-17 14:41:14 +0000336 FiTmpOffsetMap[FI] = FuncInfo->getTmpSize();
337 FuncInfo->setTmpSize(FuncInfo->getTmpSize() + size);
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000338 return FiTmpOffsetMap[FI];
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000339}
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000340
Dan Gohman1e93df62010-04-17 14:41:14 +0000341void PIC16TargetLowering::ResetTmpOffsetMap(SelectionDAG &DAG) {
342 MachineFunction &MF = DAG.getMachineFunction();
343 PIC16MachineFunctionInfo *FuncInfo = MF.getInfo<PIC16MachineFunctionInfo>();
344 FuncInfo->getFiTmpOffsetMap().clear();
345 FuncInfo->setTmpSize(0);
346}
347
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000348// To extract chain value from the SDValue Nodes
349// This function will help to maintain the chain extracting
350// code at one place. In case of any change in future it will
351// help maintain the code.
352static SDValue getChain(SDValue &Op) {
353 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
354
355 // If the last value returned in Flag then the chain is
356 // second last value returned.
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 if (Chain.getValueType() == MVT::Flag)
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000358 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
359
360 // All nodes may not produce a chain. Therefore following assert
361 // verifies that the node is returning a chain only.
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 assert (Chain.getValueType() == MVT::Other
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000363 && "Node does not have a chain");
364
365 return Chain;
366}
367
368/// PopulateResults - Helper function to LowerOperation.
369/// If a node wants to return multiple results after lowering,
370/// it stuffs them into an array of SDValue called Results.
371
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000372static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
373 if (N.getOpcode() == ISD::MERGE_VALUES) {
374 int NumResults = N.getNumOperands();
375 for( int i = 0; i < NumResults; i++)
376 Results.push_back(N.getOperand(i));
377 }
378 else
379 Results.push_back(N);
380}
381
Owen Anderson825b72b2009-08-11 20:47:22 +0000382MVT::SimpleValueType
Owen Andersone50ed302009-08-10 22:56:29 +0000383PIC16TargetLowering::getSetCCResultType(EVT ValType) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 return MVT::i8;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000385}
386
Sanjiv Gupta8f17a362009-12-28 02:40:33 +0000387MVT::SimpleValueType
388PIC16TargetLowering::getCmpLibcallReturnType() const {
389 return MVT::i8;
390}
391
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000392/// The type legalizer framework of generating legalizer can generate libcalls
393/// only when the operand/result types are illegal.
394/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
395/// For example an arithmetic right shift. These functions are used to lower
396/// such operations that generate libcall for legal types.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000397
398void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000399PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000400 const char *Name) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000401 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000402}
403
404const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000405PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000406 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000407}
408
409SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000410PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Owen Andersone50ed302009-08-10 22:56:29 +0000411 EVT RetVT, const SDValue *Ops,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000412 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000413 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000414
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000415 TargetLowering::ArgListTy Args;
416 Args.reserve(NumOps);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000417
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000418 TargetLowering::ArgListEntry Entry;
419 for (unsigned i = 0; i != NumOps; ++i) {
420 Entry.Node = Ops[i];
Owen Anderson23b9b192009-08-12 00:36:31 +0000421 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000422 Entry.isSExt = isSigned;
423 Entry.isZExt = !isSigned;
424 Args.push_back(Entry);
425 }
Sanjiv Guptaa4186282009-08-11 01:51:55 +0000426
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i16);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000428
Owen Anderson23b9b192009-08-12 00:36:31 +0000429 const Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000430 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000431 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000432 false, 0, CallingConv::C, false,
433 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +0000434 Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000435
436 return CallInfo.first;
437}
438
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000439const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
440 switch (Opcode) {
441 default: return NULL;
442 case PIC16ISD::Lo: return "PIC16ISD::Lo";
443 case PIC16ISD::Hi: return "PIC16ISD::Hi";
444 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
445 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000446 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
447 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000448 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
449 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000450 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000451 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000452 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000453 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000454 case PIC16ISD::BCF: return "PIC16ISD::BCF";
455 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
456 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
457 case PIC16ISD::RLF: return "PIC16ISD::RLF";
458 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000459 case PIC16ISD::CALL: return "PIC16ISD::CALL";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000460 case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000461 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
462 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
463 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Dan Gohman98ca4f22009-08-05 01:29:28 +0000464 case PIC16ISD::RET: return "PIC16ISD::RET";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000465 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000466 }
467}
468
Duncan Sands1607f052008-12-01 11:39:25 +0000469void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
470 SmallVectorImpl<SDValue>&Results,
471 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000472
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000473 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000474 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000475 Results.push_back(ExpandGlobalAddress(N, DAG));
476 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000477 case ISD::ExternalSymbol:
478 Results.push_back(ExpandExternalSymbol(N, DAG));
479 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000480 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000481 Results.push_back(ExpandStore(N, DAG));
482 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000483 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000484 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000485 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000486 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000487 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000488 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000489 case ISD::FrameIndex:
490 Results.push_back(ExpandFrameIndex(N, DAG));
491 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000492 default:
493 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000494 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000495 }
496}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000497
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000498SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
499
Owen Anderson825b72b2009-08-11 20:47:22 +0000500 // Currently handling FrameIndex of size MVT::i16 only
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000501 // One example of this scenario is when return value is written on
502 // FrameIndex#0
503
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 if (N->getValueType(0) != MVT::i16)
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000505 return SDValue();
506
507 // Expand the FrameIndex into ExternalSymbol and a Constant node
508 // The constant will represent the frame index number
509 // Get the current function frame
510 MachineFunction &MF = DAG.getMachineFunction();
511 const Function *Func = MF.getFunction();
512 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000513
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000514 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000515 // FIXME there isn't really debug info here
516 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000517
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000518 // Expand FrameIndex like GlobalAddress and ExternalSymbol
519 // Also use Offset field for lo and hi parts. The default
520 // offset is zero.
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000521
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000522 SDValue ES;
523 int FrameOffset;
524 SDValue FI = SDValue(N,0);
525 LegalizeFrameIndex(FI, DAG, ES, FrameOffset);
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 SDValue Offset = DAG.getConstant(FrameOffset, MVT::i8);
527 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, ES, Offset);
528 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, ES, Offset);
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000529 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000530}
531
532
533SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000534 StoreSDNode *St = cast<StoreSDNode>(N);
535 SDValue Chain = St->getChain();
536 SDValue Src = St->getValue();
537 SDValue Ptr = St->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000538 EVT ValueType = Src.getValueType();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000539 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000540 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000541
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000542 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000543 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000544
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 if (ValueType == MVT::i8) {
546 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000547 PtrLo, PtrHi,
Owen Anderson825b72b2009-08-11 20:47:22 +0000548 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000549 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 else if (ValueType == MVT::i16) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000551 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
552 SDValue SrcLo, SrcHi;
553 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
554 SDValue ChainLo = Chain, ChainHi = Chain;
Dan Gohman6ff5de42009-08-26 16:06:11 +0000555 // FIXME: This makes unsafe assumptions. The Chain may be a TokenFactor
556 // created for an unrelated purpose, in which case it may not have
557 // exactly two operands. Also, even if it does have two operands, they
558 // may not be the low and high parts of an aligned load that was split.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000559 if (Chain.getOpcode() == ISD::TokenFactor) {
560 ChainLo = Chain.getOperand(0);
561 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000562 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000564 ChainLo,
565 SrcLo, PtrLo, PtrHi,
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000567
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000569 SrcHi, PtrLo, PtrHi,
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000573 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000574 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000575 else if (ValueType == MVT::i32) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000576 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
577 SDValue SrcLo, SrcHi;
578 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000579
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000580 // Get the expanded parts of each of SrcLo and SrcHi.
581 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
582 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
583 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000584
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000585 SDValue ChainLo = Chain, ChainHi = Chain;
Dan Gohman6ff5de42009-08-26 16:06:11 +0000586 // FIXME: This makes unsafe assumptions; see the FIXME above.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000587 if (Chain.getOpcode() == ISD::TokenFactor) {
588 ChainLo = Chain.getOperand(0);
589 ChainHi = Chain.getOperand(1);
590 }
591 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
592 ChainHi2 = ChainHi;
Dan Gohman6ff5de42009-08-26 16:06:11 +0000593 // FIXME: This makes unsafe assumptions; see the FIXME above.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000594 if (ChainLo.getOpcode() == ISD::TokenFactor) {
595 ChainLo1 = ChainLo.getOperand(0);
596 ChainLo2 = ChainLo.getOperand(1);
597 }
Dan Gohman6ff5de42009-08-26 16:06:11 +0000598 // FIXME: This makes unsafe assumptions; see the FIXME above.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000599 if (ChainHi.getOpcode() == ISD::TokenFactor) {
600 ChainHi1 = ChainHi.getOperand(0);
601 ChainHi2 = ChainHi.getOperand(1);
602 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000604 ChainLo1,
605 SrcLo1, PtrLo, PtrHi,
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000607
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000609 SrcLo2, PtrLo, PtrHi,
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000611
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000613 SrcHi1, PtrLo, PtrHi,
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 DAG.getConstant (2 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000615
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000617 SrcHi2, PtrLo, PtrHi,
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 DAG.getConstant (3 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000619
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +0000621 getChain(Store1), getChain(Store2));
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesende064702009-02-06 21:50:26 +0000623 getChain(Store3), getChain(Store4));
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000625
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 } else if (ValueType == MVT::i64) {
Sanjiv Guptaada097a2009-08-04 17:59:16 +0000627 SDValue SrcLo, SrcHi;
628 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
629 SDValue ChainLo = Chain, ChainHi = Chain;
Dan Gohman6ff5de42009-08-26 16:06:11 +0000630 // FIXME: This makes unsafe assumptions; see the FIXME above.
Sanjiv Guptaada097a2009-08-04 17:59:16 +0000631 if (Chain.getOpcode() == ISD::TokenFactor) {
632 ChainLo = Chain.getOperand(0);
633 ChainHi = Chain.getOperand(1);
634 }
635 SDValue Store1 = DAG.getStore(ChainLo, dl, SrcLo, Ptr, NULL,
David Greene74a6a792010-02-15 16:56:34 +0000636 0 + StoreOffset, false, false, 0);
Sanjiv Guptaada097a2009-08-04 17:59:16 +0000637
638 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
639 DAG.getConstant(4, Ptr.getValueType()));
640 SDValue Store2 = DAG.getStore(ChainHi, dl, SrcHi, Ptr, NULL,
David Greene74a6a792010-02-15 16:56:34 +0000641 1 + StoreOffset, false, false, 0);
Sanjiv Guptaada097a2009-08-04 17:59:16 +0000642
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1,
Sanjiv Guptaada097a2009-08-04 17:59:16 +0000644 Store2);
645 } else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000646 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000647 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000648 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000649}
650
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000651SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
652{
653 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000654 // FIXME there isn't really debug info here
655 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000656
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
658 SDValue Offset = DAG.getConstant(0, MVT::i8);
659 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES, Offset);
660 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES, Offset);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000663}
664
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000665// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000666SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000667 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000668 // FIXME there isn't really debug info here
669 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000670
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000672 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 SDValue Offset = DAG.getConstant(0, MVT::i8);
675 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA, Offset);
676 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA, Offset);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000677
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000679}
680
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000681bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
682 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000683
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000684 if (Op.getOpcode() == ISD::BUILD_PAIR) {
685 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
686 return true;
687 }
688 return false;
689}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000690
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000691// Return true if DirectAddress is in ROM_SPACE
692bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000693
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000694 // RomAddress is a GlobalAddress in ROM_SPACE_
695 // If the Op is not a GlobalAddress return NULL without checking
696 // anything further.
697 if (!isDirectAddress(Op))
698 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000699
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000700 // Its a GlobalAddress.
701 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
702 SDValue TGA = Op.getOperand(0).getOperand(0);
703 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000704
Sanjiv Guptaa3518a12009-04-29 04:43:24 +0000705 if (GSDN->getAddressSpace() == PIC16ISD::ROM_SPACE)
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000706 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000707
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000708 // Any other address space return it false
709 return false;
710}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000711
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000712
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000713// GetExpandedParts - This function is on the similiar lines as
714// the GetExpandedInteger in type legalizer is. This returns expanded
715// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000716
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000717void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
718 SDValue &Lo, SDValue &Hi) {
719 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000720 DebugLoc dl = N->getDebugLoc();
Owen Anderson23b9b192009-08-12 00:36:31 +0000721 EVT NewVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000722
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000723 // Extract the lo component.
724 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 DAG.getConstant(0, MVT::i8));
Bill Wendling51b16f42009-05-30 01:09:53 +0000726
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000727 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000728 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
Owen Anderson825b72b2009-08-11 20:47:22 +0000729 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000730}
731
732// Legalize FrameIndex into ExternalSymbol and offset.
733void
734PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
735 SDValue &ES, int &Offset) {
736
737 MachineFunction &MF = DAG.getMachineFunction();
738 const Function *Func = MF.getFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000739 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +0000740 PIC16MachineFunctionInfo *FuncInfo = MF.getInfo<PIC16MachineFunctionInfo>();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000741 const std::string Name = Func->getName();
742
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000743 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000744
745 // FrameIndices are not stack offsets. But they represent the request
746 // for space on stack. That space requested may be more than one byte.
747 // Therefore, to calculate the stack offset that a FrameIndex aligns
748 // with, we need to traverse all the FrameIndices available earlier in
749 // the list and add their requested size.
750 unsigned FIndex = FR->getIndex();
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000751 const char *tmpName;
Dan Gohman1e93df62010-04-17 14:41:14 +0000752 if (FIndex < FuncInfo->getReservedFrameCount()) {
Sanjiv Guptad49baef2010-04-07 03:36:01 +0000753 tmpName = ESNames::createESName(PAN::getFrameLabel(Name));
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000755 Offset = 0;
756 for (unsigned i=0; i<FIndex ; ++i) {
757 Offset += MFI->getObjectSize(i);
758 }
759 } else {
760 // FrameIndex has been made for some temporary storage
Sanjiv Guptad49baef2010-04-07 03:36:01 +0000761 tmpName = ESNames::createESName(PAN::getTempdataLabel(Name));
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Dan Gohman1e93df62010-04-17 14:41:14 +0000763 Offset = GetTmpOffsetForFI(FIndex, MFI->getObjectSize(FIndex), MF);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000764 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000765
766 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000767}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000768
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000769// This function legalizes the PIC16 Addresses. If the Pointer is
770// -- Direct address variable residing
771// --> then a Banksel for that variable will be created.
772// -- Rom variable
773// --> then it will be treated as an indirect address.
774// -- Indirect address
775// --> then the address will be loaded into FSR
776// -- ADD with constant operand
777// --> then constant operand of ADD will be returned as Offset
778// and non-constant operand of ADD will be treated as pointer.
779// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000780
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000781void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
782 SDValue &Lo, SDValue &Hi,
783 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000784
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000785 // Offset, by default, should be 0
786 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000787
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000788 // If the pointer is ADD with constant,
789 // return the constant value as the offset
790 if (Ptr.getOpcode() == ISD::ADD) {
791 SDValue OperLeft = Ptr.getOperand(0);
792 SDValue OperRight = Ptr.getOperand(1);
Sanjiv Guptaae992272009-06-23 07:10:19 +0000793 if ((OperLeft.getOpcode() == ISD::Constant) &&
794 (dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue() < 32 )) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000795 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
796 Ptr = OperRight;
Sanjiv Guptaae992272009-06-23 07:10:19 +0000797 } else if ((OperRight.getOpcode() == ISD::Constant) &&
798 (dyn_cast<ConstantSDNode>(OperRight)->getZExtValue() < 32 )){
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000799 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000800 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000801 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000802 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000803
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000804 // If the pointer is Type i8 and an external symbol
805 // then treat it as direct address.
806 // One example for such case is storing and loading
807 // from function frame during a call
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 if (Ptr.getValueType() == MVT::i8) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000809 switch (Ptr.getOpcode()) {
810 case ISD::TargetExternalSymbol:
811 Lo = Ptr;
Owen Anderson825b72b2009-08-11 20:47:22 +0000812 Hi = DAG.getConstant(1, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000813 return;
814 }
815 }
816
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000817 // Expansion of FrameIndex has Lo/Hi parts
818 if (isDirectAddress(Ptr)) {
819 SDValue TFI = Ptr.getOperand(0).getOperand(0);
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000820 int FrameOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000821 if (TFI.getOpcode() == ISD::TargetFrameIndex) {
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000822 LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset);
Owen Anderson825b72b2009-08-11 20:47:22 +0000823 Hi = DAG.getConstant(1, MVT::i8);
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000824 Offset += FrameOffset;
825 return;
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000826 } else if (TFI.getOpcode() == ISD::TargetExternalSymbol) {
827 // FrameIndex has already been expanded.
828 // Now just make use of its expansion
829 Lo = TFI;
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 Hi = DAG.getConstant(1, MVT::i8);
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000831 SDValue FOffset = Ptr.getOperand(0).getOperand(1);
832 assert (FOffset.getOpcode() == ISD::Constant &&
833 "Invalid operand of PIC16ISD::Lo");
834 Offset += dyn_cast<ConstantSDNode>(FOffset)->getZExtValue();
835 return;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000836 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000837 }
838
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000839 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
840 // Direct addressing case for RAM variables. The Hi part is constant
841 // and the Lo part is the TGA itself.
842 Lo = Ptr.getOperand(0).getOperand(0);
843
844 // For direct addresses Hi is a constant. Value 1 for the constant
845 // signifies that banksel needs to generated for it. Value 0 for
846 // the constant signifies that banksel does not need to be generated
847 // for it. Mark it as 1 now and optimize later.
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 Hi = DAG.getConstant(1, MVT::i8);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000849 return;
850 }
851
852 // Indirect addresses. Get the hi and lo parts of ptr.
853 GetExpandedParts(Ptr, DAG, Lo, Hi);
854
855 // Put the hi and lo parts into FSR.
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
857 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000858
859 return;
860}
861
Duncan Sands1607f052008-12-01 11:39:25 +0000862SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000863 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
864 SDValue Chain = LD->getChain();
865 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000866 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000867
868 SDValue Load, Offset;
869 SDVTList Tys;
Owen Andersone50ed302009-08-10 22:56:29 +0000870 EVT VT, NewVT;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000871 SDValue PtrLo, PtrHi;
872 unsigned LoadOffset;
873
874 // Legalize direct/indirect addresses. This will give the lo and hi parts
875 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000876 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000877
878 // Load from the pointer (direct address or FSR)
879 VT = N->getValueType(0);
880 unsigned NumLoads = VT.getSizeInBits() / 8;
881 std::vector<SDValue> PICLoads;
882 unsigned iter;
Owen Andersone50ed302009-08-10 22:56:29 +0000883 EVT MemVT = LD->getMemoryVT();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000884 if(ISD::isNON_EXTLoad(N)) {
885 for (iter=0; iter<NumLoads ; ++iter) {
886 // Add the pointer offset if any
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
888 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000889 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000890 Offset);
891 PICLoads.push_back(Load);
892 }
893 } else {
894 // If it is extended load then use PIC16Load for Memory Bytes
895 // and for all extended bytes perform action based on type of
896 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
897
898
899 // For extended loads this is the memory value type
900 // i.e. without any extension
Owen Andersone50ed302009-08-10 22:56:29 +0000901 EVT MemVT = LD->getMemoryVT();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000902 unsigned MemBytes = MemVT.getSizeInBits() / 8;
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 // if MVT::i1 is extended to MVT::i8 then MemBytes will be zero
Sanjiv Gupta1d2e6ed2009-07-03 07:58:59 +0000904 // So set it to one
905 if (MemBytes == 0) MemBytes = 1;
906
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000907 unsigned ExtdBytes = VT.getSizeInBits() / 8;
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 Offset = DAG.getConstant(LoadOffset, MVT::i8);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000909
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000911 // For MemBytes generate PIC16Load with proper offset
Sanjiv Gupta1d2e6ed2009-07-03 07:58:59 +0000912 for (iter=0; iter < MemBytes; ++iter) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000913 // Add the pointer offset if any
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000915 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000916 Offset);
917 PICLoads.push_back(Load);
918 }
919
920 // For SignExtendedLoad
921 if (ISD::isSEXTLoad(N)) {
922 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
923 // highest MemByte
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
925 DAG.getConstant(7, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000926 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
927 PICLoads.push_back(SRA);
928 }
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000929 } else if (ISD::isZEXTLoad(N) || ISD::isEXTLoad(N)) {
930 //} else if (ISD::isZEXTLoad(N)) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000931 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000933 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
934 PICLoads.push_back(ConstZero);
935 }
936 }
937 }
938 SDValue BP;
939
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 if (VT == MVT::i8) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000941 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000942 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000943 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000945 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Gupta917283a2009-12-28 04:53:24 +0000946 if ((MemVT == MVT::i8) || (MemVT == MVT::i1))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000947 Chain = getChain(PICLoads[0]);
948 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000950 getChain(PICLoads[0]), getChain(PICLoads[1]));
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 } else if (VT == MVT::i32) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000952 SDValue BPs[2];
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000954 PICLoads[0], PICLoads[1]);
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000956 PICLoads[2], PICLoads[3]);
957 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Gupta917283a2009-12-28 04:53:24 +0000958 if ((MemVT == MVT::i8) || (MemVT == MVT::i1))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000959 Chain = getChain(PICLoads[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 else if (MemVT == MVT::i16)
961 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000962 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000963 else {
964 SDValue Chains[2];
Owen Anderson825b72b2009-08-11 20:47:22 +0000965 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000966 getChain(PICLoads[0]), getChain(PICLoads[1]));
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000968 getChain(PICLoads[2]), getChain(PICLoads[3]));
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000970 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000971 }
972 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000974 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000975}
976
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000977SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
978 // We should have handled larger operands in type legalizer itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000979 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000980
981 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000982 SDValue Value = N->getOperand(0);
983 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000984 PIC16ISD::PIC16Libcall CallCode;
985 switch (N->getOpcode()) {
986 case ISD::SRA:
987 CallCode = PIC16ISD::SRA_I8;
988 break;
989 case ISD::SHL:
990 CallCode = PIC16ISD::SLL_I8;
991 break;
992 case ISD::SRL:
993 CallCode = PIC16ISD::SRL_I8;
994 break;
995 default:
996 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000997 return SDValue();
998 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000999 SmallVector<SDValue, 2> Ops(2);
1000 Ops[0] = Value;
1001 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001002 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
1003 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001004 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001005}
1006
Sanjiv Gupta8e14eab2009-07-27 02:44:46 +00001007SDValue PIC16TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) {
1008 // We should have handled larger operands in type legalizer itself.
Owen Anderson825b72b2009-08-11 20:47:22 +00001009 assert (Op.getValueType() == MVT::i8 && "illegal multiply to lower");
Sanjiv Gupta8e14eab2009-07-27 02:44:46 +00001010
1011 SDNode *N = Op.getNode();
1012 SmallVector<SDValue, 2> Ops(2);
1013 Ops[0] = N->getOperand(0);
1014 Ops[1] = N->getOperand(1);
1015 SDValue Call = MakePIC16Libcall(PIC16ISD::MUL_I8, N->getValueType(0),
1016 &Ops[0], 2, true, DAG, N->getDebugLoc());
1017 return Call;
1018}
1019
Duncan Sands9fbc7e22009-01-21 09:00:29 +00001020void
1021PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
1022 SmallVectorImpl<SDValue>&Results,
1023 SelectionDAG &DAG) {
1024 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001025 SDValue Res;
1026 unsigned i;
1027 switch (Op.getOpcode()) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001028 case ISD::LOAD:
1029 Res = ExpandLoad(Op.getNode(), DAG); break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001030 default: {
1031 // All other operations are handled in LowerOperation.
1032 Res = LowerOperation(Op, DAG);
1033 if (Res.getNode())
1034 Results.push_back(Res);
1035
1036 return;
1037 }
1038 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +00001039
1040 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001041 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +00001042 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001043 Results.push_back(SDValue(N, i));
1044 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001045}
1046
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001047SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
1048 switch (Op.getOpcode()) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001049 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001050 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001051 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001052 return LowerADD(Op, DAG);
1053 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001054 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001055 case ISD::SUBE:
1056 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001057 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +00001058 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001059 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +00001060 return ExpandStore(Op.getNode(), DAG);
Sanjiv Gupta8e14eab2009-07-27 02:44:46 +00001061 case ISD::MUL:
1062 return LowerMUL(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001063 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001064 case ISD::SRA:
1065 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001066 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001067 case ISD::OR:
1068 case ISD::AND:
1069 case ISD::XOR:
1070 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001071 case ISD::BR_CC:
1072 return LowerBR_CC(Op, DAG);
1073 case ISD::SELECT_CC:
1074 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001075 }
Dan Gohman475871a2008-07-27 21:46:04 +00001076 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001077}
1078
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001079SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001080 SelectionDAG &DAG,
1081 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 assert (Op.getValueType() == MVT::i8
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001083 && "illegal value type to store on stack.");
1084
1085 MachineFunction &MF = DAG.getMachineFunction();
1086 const Function *Func = MF.getFunction();
1087 const std::string FuncName = Func->getName();
1088
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001089
1090 // Put the value on stack.
1091 // Get a stack slot index and convert to es.
David Greene3f2bf852009-11-12 20:49:22 +00001092 int FI = MF.getFrameInfo()->CreateStackObject(1, 1, false);
Sanjiv Guptad49baef2010-04-07 03:36:01 +00001093 const char *tmpName = ESNames::createESName(PAN::getTempdataLabel(FuncName));
Owen Anderson825b72b2009-08-11 20:47:22 +00001094 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001095
1096 // Store the value to ES.
Owen Anderson825b72b2009-08-11 20:47:22 +00001097 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001098 DAG.getEntryNode(),
1099 Op, ES,
Owen Anderson825b72b2009-08-11 20:47:22 +00001100 DAG.getConstant (1, MVT::i8), // Banksel.
Dan Gohman1e93df62010-04-17 14:41:14 +00001101 DAG.getConstant (GetTmpOffsetForFI(FI, 1, MF),
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001103
1104 // Load the value from ES.
Owen Anderson825b72b2009-08-11 20:47:22 +00001105 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001106 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 ES, DAG.getConstant (1, MVT::i8),
Dan Gohman1e93df62010-04-17 14:41:14 +00001108 DAG.getConstant (GetTmpOffsetForFI(FI, 1, MF),
Owen Anderson825b72b2009-08-11 20:47:22 +00001109 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001110
1111 return Load.getValue(0);
1112}
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001113
1114SDValue PIC16TargetLowering::
Dan Gohman98ca4f22009-08-05 01:29:28 +00001115LowerIndirectCallArguments(SDValue Chain, SDValue InFlag,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001116 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001117 const SmallVectorImpl<ISD::OutputArg> &Outs,
Sanjiv Guptaa4186282009-08-11 01:51:55 +00001118 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001119 DebugLoc dl, SelectionDAG &DAG) {
1120 unsigned NumOps = Outs.size();
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001121
1122 // If call has no arguments then do nothing and return.
1123 if (NumOps == 0)
1124 return Chain;
1125
1126 std::vector<SDValue> Ops;
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001128 SDValue Arg, StoreRet;
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001129
1130 // For PIC16 ABI the arguments come after the return value.
Sanjiv Guptaa4186282009-08-11 01:51:55 +00001131 unsigned RetVals = Ins.size();
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001132 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001133 // Get the arguments
Dan Gohman98ca4f22009-08-05 01:29:28 +00001134 Arg = Outs[i].Val;
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001135
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001136 Ops.clear();
1137 Ops.push_back(Chain);
1138 Ops.push_back(Arg);
1139 Ops.push_back(DataAddr_Lo);
1140 Ops.push_back(DataAddr_Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001141 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001142 Ops.push_back(InFlag);
1143
1144 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
1145
1146 Chain = getChain(StoreRet);
1147 InFlag = getOutFlag(StoreRet);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001148 ArgOffset++;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001149 }
1150 return Chain;
1151}
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001152
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001153SDValue PIC16TargetLowering::
Dan Gohman98ca4f22009-08-05 01:29:28 +00001154LowerDirectCallArguments(SDValue ArgLabel, SDValue Chain, SDValue InFlag,
1155 const SmallVectorImpl<ISD::OutputArg> &Outs,
1156 DebugLoc dl, SelectionDAG &DAG) {
1157 unsigned NumOps = Outs.size();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001158 std::string Name;
1159 SDValue Arg, StoreAt;
Owen Andersone50ed302009-08-10 22:56:29 +00001160 EVT ArgVT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001161 unsigned Size=0;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001162
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001163 // If call has no arguments then do nothing and return.
1164 if (NumOps == 0)
1165 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001166
1167 // FIXME: This portion of code currently assumes only
1168 // primitive types being passed as arguments.
1169
1170 // Legalize the address before use
1171 SDValue PtrLo, PtrHi;
1172 unsigned AddressOffset;
1173 int StoreOffset = 0;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001174 LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001175 SDValue StoreRet;
1176
1177 std::vector<SDValue> Ops;
Owen Anderson825b72b2009-08-11 20:47:22 +00001178 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179 for (unsigned i=0, Offset = 0; i<NumOps; i++) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001180 // Get the argument
Dan Gohman98ca4f22009-08-05 01:29:28 +00001181 Arg = Outs[i].Val;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001182 StoreOffset = (Offset + AddressOffset);
1183
1184 // Store the argument on frame
1185
1186 Ops.clear();
1187 Ops.push_back(Chain);
Sanjiv Gupta12f23a82009-04-13 09:38:38 +00001188 Ops.push_back(Arg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001189 Ops.push_back(PtrLo);
1190 Ops.push_back(PtrHi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001191 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001192 Ops.push_back(InFlag);
1193
Dale Johannesene8d72302009-02-06 23:05:02 +00001194 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001195
1196 Chain = getChain(StoreRet);
1197 InFlag = getOutFlag(StoreRet);
1198
1199 // Update the frame offset to be used for next argument
1200 ArgVT = Arg.getValueType();
1201 Size = ArgVT.getSizeInBits();
1202 Size = Size/8; // Calculate size in bytes
1203 Offset += Size; // Increase the frame offset
1204 }
1205 return Chain;
1206}
1207
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001208SDValue PIC16TargetLowering::
Dan Gohman98ca4f22009-08-05 01:29:28 +00001209LowerIndirectCallReturn(SDValue Chain, SDValue InFlag,
1210 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1211 const SmallVectorImpl<ISD::InputArg> &Ins,
1212 DebugLoc dl, SelectionDAG &DAG,
1213 SmallVectorImpl<SDValue> &InVals) {
1214 unsigned RetVals = Ins.size();
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001215
1216 // If call does not have anything to return
1217 // then do nothing and go back.
1218 if (RetVals == 0)
1219 return Chain;
1220
1221 // Call has something to return
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001222 SDValue LoadRet;
1223
Owen Anderson825b72b2009-08-11 20:47:22 +00001224 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001225 for(unsigned i=0;i<RetVals;i++) {
1226 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001228 InFlag);
1229 InFlag = getOutFlag(LoadRet);
1230 Chain = getChain(LoadRet);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001231 InVals.push_back(LoadRet);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001232 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001233 return Chain;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001234}
1235
1236SDValue PIC16TargetLowering::
Dan Gohman98ca4f22009-08-05 01:29:28 +00001237LowerDirectCallReturn(SDValue RetLabel, SDValue Chain, SDValue InFlag,
1238 const SmallVectorImpl<ISD::InputArg> &Ins,
1239 DebugLoc dl, SelectionDAG &DAG,
1240 SmallVectorImpl<SDValue> &InVals) {
1241
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001242 // Currently handling primitive types only. They will come in
1243 // i8 parts
Dan Gohman98ca4f22009-08-05 01:29:28 +00001244 unsigned RetVals = Ins.size();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001245
1246 // Return immediately if the return type is void
1247 if (RetVals == 0)
1248 return Chain;
1249
1250 // Call has something to return
1251
1252 // Legalize the address before use
1253 SDValue LdLo, LdHi;
1254 unsigned LdOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001255 LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001256
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001258 SDValue LoadRet;
1259
1260 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1261
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001262 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 DAG.getConstant(LdOffset + Offset, MVT::i8),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001264 InFlag);
1265
1266 InFlag = getOutFlag(LoadRet);
1267
1268 Chain = getChain(LoadRet);
1269 Offset++;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001270 InVals.push_back(LoadRet);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001271 }
1272
Dan Gohman98ca4f22009-08-05 01:29:28 +00001273 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001274}
1275
Dan Gohman98ca4f22009-08-05 01:29:28 +00001276SDValue
1277PIC16TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001278 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001279 const SmallVectorImpl<ISD::OutputArg> &Outs,
1280 DebugLoc dl, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001281
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001282 // Number of values to return
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 unsigned NumRet = Outs.size();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001284
1285 // Function returns value always on stack with the offset starting
1286 // from 0
1287 MachineFunction &MF = DAG.getMachineFunction();
1288 const Function *F = MF.getFunction();
1289 std::string FuncName = F->getName();
1290
Sanjiv Guptad49baef2010-04-07 03:36:01 +00001291 const char *tmpName = ESNames::createESName(PAN::getFrameLabel(FuncName));
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1293 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001294 SDValue RetVal;
1295 for(unsigned i=0;i<NumRet; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001296 RetVal = Outs[i].Val;
Owen Anderson825b72b2009-08-11 20:47:22 +00001297 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001298 ES, BS,
Owen Anderson825b72b2009-08-11 20:47:22 +00001299 DAG.getConstant (i, MVT::i8));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001300
1301 }
Chris Lattner35a27c82009-08-21 23:12:15 +00001302 return DAG.getNode(PIC16ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001303}
1304
1305void PIC16TargetLowering::
1306GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1307 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1308 SelectionDAG &DAG) {
1309 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1310 && "Don't know what to do of such callee!!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001311 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001312 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1313 Chain = getChain(SeqStart);
1314 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1315
1316 // Get the Lo and Hi part of code address
1317 SDValue Lo = Callee.getOperand(0);
1318 SDValue Hi = Callee.getOperand(1);
1319
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001320 SDValue Data_Lo, Data_Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +00001321 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001322 // Subtract 2 from Address to get the Lower part of DataAddress.
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 SDVTList VTList = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001324 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
Owen Anderson825b72b2009-08-11 20:47:22 +00001325 DAG.getConstant(2, MVT::i8));
1326 SDValue Ops[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001327 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, Ops, 3);
Owen Anderson825b72b2009-08-11 20:47:22 +00001328 SDValue PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1329 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001330 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1331 OperFlag);
1332 Chain = getChain(Call);
1333 OperFlag = getOutFlag(Call);
1334 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1335 OperFlag);
1336 Chain = getChain(SeqEnd);
1337 OperFlag = getOutFlag(SeqEnd);
1338
1339 // Low part of Data Address
Owen Anderson825b72b2009-08-11 20:47:22 +00001340 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001341
1342 // Make the second call.
1343 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1344 Chain = getChain(SeqStart);
1345 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1346
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001347 // Subtract 1 from Address to get high part of data address.
1348 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
Owen Anderson825b72b2009-08-11 20:47:22 +00001349 DAG.getConstant(1, MVT::i8));
1350 SDValue HiOps[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001351 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
Owen Anderson825b72b2009-08-11 20:47:22 +00001352 PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001353
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001354 // Use new Lo to make another CALLW
Owen Anderson825b72b2009-08-11 20:47:22 +00001355 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001356 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1357 Chain = getChain(Call);
1358 OperFlag = getOutFlag(Call);
1359 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1360 OperFlag);
1361 Chain = getChain(SeqEnd);
1362 OperFlag = getOutFlag(SeqEnd);
1363 // Hi part of Data Address
Owen Anderson825b72b2009-08-11 20:47:22 +00001364 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001365}
1366
Dan Gohman98ca4f22009-08-05 01:29:28 +00001367SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001368PIC16TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001369 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001370 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001371 const SmallVectorImpl<ISD::OutputArg> &Outs,
1372 const SmallVectorImpl<ISD::InputArg> &Ins,
1373 DebugLoc dl, SelectionDAG &DAG,
1374 SmallVectorImpl<SDValue> &InVals) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001375 // PIC16 target does not yet support tail call optimization.
1376 isTailCall = false;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001377
Owen Anderson825b72b2009-08-11 20:47:22 +00001378 assert(Callee.getValueType() == MVT::i16 &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001379 "Don't know how to legalize this call node!!!");
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001380
1381 // The flag to track if this is a direct or indirect call.
1382 bool IsDirectCall = true;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001383 unsigned RetVals = Ins.size();
1384 unsigned NumArgs = Outs.size();
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001385
1386 SDValue DataAddr_Lo, DataAddr_Hi;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001387 if (!isa<GlobalAddressSDNode>(Callee) &&
1388 !isa<ExternalSymbolSDNode>(Callee)) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001389 IsDirectCall = false; // This is indirect call
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390
1391 // If this is an indirect call then to pass the arguments
1392 // and read the return value back, we need the data address
1393 // of the function being called.
1394 // To get the data address two more calls need to be made.
1395
1396 // Come here for indirect calls
1397 SDValue Lo, Hi;
1398 // Indirect addresses. Get the hi and lo parts of ptr.
1399 GetExpandedParts(Callee, DAG, Lo, Hi);
1400 // Connect Lo and Hi parts of the callee with the PIC16Connect
Owen Anderson825b72b2009-08-11 20:47:22 +00001401 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001402
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001403 // Read DataAddress only if we have to pass arguments or
1404 // read return value.
1405 if ((RetVals > 0) || (NumArgs > 0))
1406 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1407 }
1408
Owen Anderson825b72b2009-08-11 20:47:22 +00001409 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001410
1411 // Start the call sequence.
1412 // Carring the Constant 0 along the CALLSEQSTART
1413 // because there is nothing else to carry.
1414 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1415 Chain = getChain(SeqStart);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001416 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1417 std::string Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001418
1419 // For any direct call - callee will be GlobalAddressNode or
1420 // ExternalSymbol
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001421 SDValue ArgLabel, RetLabel;
1422 if (IsDirectCall) {
1423 // Considering the GlobalAddressNode case here.
1424 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001425 const GlobalValue *GV = G->getGlobal();
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001427 Name = G->getGlobal()->getName();
1428 } else {// Considering the ExternalSymbol case here
1429 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
Owen Anderson825b72b2009-08-11 20:47:22 +00001430 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001431 Name = ES->getSymbol();
1432 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001433
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001434 // Label for argument passing
Sanjiv Guptad49baef2010-04-07 03:36:01 +00001435 const char *argFrame = ESNames::createESName(PAN::getArgsLabel(Name));
Owen Anderson825b72b2009-08-11 20:47:22 +00001436 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001437
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001438 // Label for reading return value
Sanjiv Guptad49baef2010-04-07 03:36:01 +00001439 const char *retName = ESNames::createESName(PAN::getRetvalLabel(Name));
Owen Anderson825b72b2009-08-11 20:47:22 +00001440 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001441 } else {
1442 // if indirect call
1443 SDValue CodeAddr_Lo = Callee.getOperand(0);
1444 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001445
Owen Anderson825b72b2009-08-11 20:47:22 +00001446 /*CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1447 DAG.getConstant(2, MVT::i8));*/
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001448
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001449 // move Hi part in PCLATH
Owen Anderson825b72b2009-08-11 20:47:22 +00001450 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1451 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001452 CodeAddr_Hi);
1453 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001454
1455 // Pass the argument to function before making the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001456 SDValue CallArgs;
1457 if (IsDirectCall) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001458 CallArgs = LowerDirectCallArguments(ArgLabel, Chain, OperFlag,
1459 Outs, dl, DAG);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001460 Chain = getChain(CallArgs);
1461 OperFlag = getOutFlag(CallArgs);
1462 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001463 CallArgs = LowerIndirectCallArguments(Chain, OperFlag, DataAddr_Lo,
Sanjiv Guptaa4186282009-08-11 01:51:55 +00001464 DataAddr_Hi, Outs, Ins, dl, DAG);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001465 Chain = getChain(CallArgs);
1466 OperFlag = getOutFlag(CallArgs);
1467 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001468
Owen Anderson825b72b2009-08-11 20:47:22 +00001469 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001470 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001471 OperFlag);
1472 Chain = getChain(PICCall);
1473 OperFlag = getOutFlag(PICCall);
1474
1475
1476 // Carrying the Constant 0 along the CALLSEQSTART
1477 // because there is nothing else to carry.
1478 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1479 OperFlag);
1480 Chain = getChain(SeqEnd);
1481 OperFlag = getOutFlag(SeqEnd);
1482
1483 // Lower the return value reading after the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001484 if (IsDirectCall)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001485 return LowerDirectCallReturn(RetLabel, Chain, OperFlag,
1486 Ins, dl, DAG, InVals);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001487 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001488 return LowerIndirectCallReturn(Chain, OperFlag, DataAddr_Lo,
1489 DataAddr_Hi, Ins, dl, DAG, InVals);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001490}
1491
1492bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1493 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1494 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1495 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1496 return true;
1497 return false;
1498}
1499
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001500// NeedToConvertToMemOp - Returns true if one of the operands of the
1501// operation 'Op' needs to be put into memory. Also returns the
1502// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1503// no instruction that can operation on two registers. Most insns take
1504// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +00001505bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp,
1506 SelectionDAG &DAG) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001507 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001508 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1509 Op.getOperand(1).getOpcode() == ISD::Constant)
1510 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001511
1512 // Return false if one of the operands is already a direct
1513 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001514 if (isDirectLoad(Op.getOperand(0))) {
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +00001515 if (Op.getOperand(0).hasOneUse()) {
1516 // Legal and profitable folding check uses the NodeId of DAG nodes.
1517 // This NodeId is assigned by topological order. Therefore first
1518 // assign topological order then perform legal and profitable check.
1519 // Note:- Though this ordering is done before begining with legalization,
1520 // newly added node during legalization process have NodeId=-1 (NewNode)
1521 // therefore before performing any check proper ordering of the node is
1522 // required.
1523 DAG.AssignTopologicalOrder();
1524
1525 // Direct load operands are folded in binary operations. But before folding
1526 // verify if this folding is legal. Fold only if it is legal otherwise
1527 // convert this direct load to a separate memory operation.
Evan Cheng014bf212010-02-15 19:41:07 +00001528 if(ISel->IsLegalToFold(Op.getOperand(0), Op.getNode(), Op.getNode()))
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +00001529 return false;
1530 else
1531 MemOp = 0;
1532 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001533 }
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +00001534
1535 // For operations that are non-cummutative there is no need to check
1536 // for right operand because folding right operand may result in
1537 // incorrect operation.
1538 if (! SelectionDAG::isCommutativeBinOp(Op.getOpcode()))
1539 return true;
1540
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001541 if (isDirectLoad(Op.getOperand(1))) {
Sanjiv Gupta2b007022010-02-26 17:59:28 +00001542 if (Op.getOperand(1).hasOneUse()) {
1543 // Legal and profitable folding check uses the NodeId of DAG nodes.
1544 // This NodeId is assigned by topological order. Therefore first
1545 // assign topological order then perform legal and profitable check.
1546 // Note:- Though this ordering is done before begining with legalization,
1547 // newly added node during legalization process have NodeId=-1 (NewNode)
1548 // therefore before performing any check proper ordering of the node is
1549 // required.
1550 DAG.AssignTopologicalOrder();
1551
1552 // Direct load operands are folded in binary operations. But before folding
1553 // verify if this folding is legal. Fold only if it is legal otherwise
1554 // convert this direct load to a separate memory operation.
1555 if(ISel->IsLegalToFold(Op.getOperand(1), Op.getNode(), Op.getNode()))
1556 return false;
1557 else
1558 MemOp = 1;
1559 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001560 }
1561 return true;
1562}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001563
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001564// LowerBinOp - Lower a commutative binary operation that does not
1565// affect status flag carry.
1566SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001567 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001568
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001569 // We should have handled larger operands in type legalizer itself.
Owen Anderson825b72b2009-08-11 20:47:22 +00001570 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001571
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001572 unsigned MemOp = 1;
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +00001573 if (NeedToConvertToMemOp(Op, MemOp, DAG)) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001574 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001575 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001576
Owen Anderson825b72b2009-08-11 20:47:22 +00001577 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001578 NewVal);
1579 }
1580 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001581 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001582 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001583}
1584
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001585// LowerADD - Lower all types of ADD operations including the ones
1586// that affects carry.
1587SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001588 // We should have handled larger operands in type legalizer itself.
Owen Anderson825b72b2009-08-11 20:47:22 +00001589 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001590 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001591 unsigned MemOp = 1;
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +00001592 if (NeedToConvertToMemOp(Op, MemOp, DAG)) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001593 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001594 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001595
Dale Johannesen83138992009-06-01 23:13:42 +00001596 // ADDC and ADDE produce two results.
Owen Anderson825b72b2009-08-11 20:47:22 +00001597 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001598
Dale Johannesen83138992009-06-01 23:13:42 +00001599 // ADDE has three operands, the last one is the carry bit.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001600 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001601 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1602 NewVal, Op.getOperand(2));
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001603 // ADDC has two operands.
1604 else if (Op.getOpcode() == ISD::ADDC)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001605 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1606 NewVal);
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001607 // ADD it is. It produces only one result.
1608 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001609 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001610 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001611 }
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001612 else
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001613 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001614}
1615
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001616SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001617 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001618 // We should have handled larger operands in type legalizer itself.
Owen Anderson825b72b2009-08-11 20:47:22 +00001619 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Gupta7fb0ead2009-12-23 10:56:02 +00001620 unsigned MemOp = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001621 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta7fb0ead2009-12-23 10:56:02 +00001622
1623 // Since we don't have an instruction for X - c ,
1624 // we can change it to X + (-c)
1625 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
1626 if (C && (Op.getOpcode() == ISD::SUB))
1627 {
1628 return DAG.getNode(ISD::ADD,
1629 dl, MVT::i8, Op.getOperand(0),
1630 DAG.getConstant(0-(C->getZExtValue()), MVT::i8));
1631 }
1632
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +00001633 if (NeedToConvertToMemOp(Op, MemOp, DAG) ||
Sanjiv Gupta7fb0ead2009-12-23 10:56:02 +00001634 (isDirectLoad(Op.getOperand(1)) &&
1635 (!isDirectLoad(Op.getOperand(0))) &&
1636 (Op.getOperand(0).getOpcode() != ISD::Constant)))
1637 {
1638 // Put first operand on stack.
1639 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
1640
1641 switch (Op.getOpcode()) {
1642 default:
1643 assert (0 && "Opcode unknown.");
1644 case ISD::SUBE:
1645 return DAG.getNode(Op.getOpcode(),
1646 dl, Tys, NewVal, Op.getOperand(1),
1647 Op.getOperand(2));
1648 break;
1649 case ISD::SUBC:
1650 return DAG.getNode(Op.getOpcode(),
1651 dl, Tys, NewVal, Op.getOperand(1));
1652 break;
1653 case ISD::SUB:
1654 return DAG.getNode(Op.getOpcode(),
1655 dl, MVT::i8, NewVal, Op.getOperand(1));
1656 break;
1657 }
1658 }
1659 else
1660 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001661}
1662
Dan Gohman1e93df62010-04-17 14:41:14 +00001663void PIC16TargetLowering::InitReservedFrameCount(const Function *F,
1664 SelectionDAG &DAG) {
1665 MachineFunction &MF = DAG.getMachineFunction();
1666 PIC16MachineFunctionInfo *FuncInfo = MF.getInfo<PIC16MachineFunctionInfo>();
1667
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001668 unsigned NumArgs = F->arg_size();
1669
1670 bool isVoidFunc = (F->getReturnType()->getTypeID() == Type::VoidTyID);
1671
1672 if (isVoidFunc)
Dan Gohman1e93df62010-04-17 14:41:14 +00001673 FuncInfo->setReservedFrameCount(NumArgs);
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001674 else
Dan Gohman1e93df62010-04-17 14:41:14 +00001675 FuncInfo->setReservedFrameCount(NumArgs + 1);
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001676}
1677
Dan Gohman98ca4f22009-08-05 01:29:28 +00001678// LowerFormalArguments - Argument values are loaded from the
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001679// <fname>.args + offset. All arguments are already broken to leaglized
1680// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001681
Dan Gohman98ca4f22009-08-05 01:29:28 +00001682SDValue
1683PIC16TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001684 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001685 bool isVarArg,
1686 const SmallVectorImpl<ISD::InputArg> &Ins,
1687 DebugLoc dl,
1688 SelectionDAG &DAG,
1689 SmallVectorImpl<SDValue> &InVals) {
1690 unsigned NumArgVals = Ins.size();
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001691
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001692 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001693 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001694 const Function *F = MF.getFunction();
1695 std::string FuncName = F->getName();
1696
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001697 // Reset the map of FI and TmpOffset
Dan Gohman1e93df62010-04-17 14:41:14 +00001698 ResetTmpOffsetMap(DAG);
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001699 // Initialize the ReserveFrameCount
Dan Gohman1e93df62010-04-17 14:41:14 +00001700 InitReservedFrameCount(F, DAG);
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001701
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001702 // Create the <fname>.args external symbol.
Sanjiv Guptad49baef2010-04-07 03:36:01 +00001703 const char *tmpName = ESNames::createESName(PAN::getArgsLabel(FuncName));
Owen Anderson825b72b2009-08-11 20:47:22 +00001704 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001705
1706 // Load arg values from the label + offset.
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1708 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001709 for (unsigned i = 0; i < NumArgVals ; ++i) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001710 SDValue Offset = DAG.getConstant(i, MVT::i8);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001711 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001712 Offset);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001713 Chain = getChain(PICLoad);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 InVals.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001715 }
1716
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717 return Chain;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001718}
1719
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001720// Perform DAGCombine of PIC16Load.
1721// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001722SDValue PIC16TargetLowering::
1723PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1724 SelectionDAG &DAG = DCI.DAG;
1725 SDValue Chain = N->getOperand(0);
1726 if (N->hasNUsesOfValue(0, 0)) {
1727 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1728 }
1729 return SDValue();
1730}
1731
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001732// For all the functions with arguments some STORE nodes are generated
1733// that store the argument on the frameindex. However in PIC16 the arguments
1734// are passed on stack only. Therefore these STORE nodes are redundant.
1735// To remove these STORE nodes will be removed in PerformStoreCombine
1736//
1737// Currently this function is doint nothing and will be updated for removing
1738// unwanted store operations
1739SDValue PIC16TargetLowering::
1740PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001741 return SDValue(N, 0);
1742 /*
1743 // Storing an undef value is of no use, so remove it
1744 if (isStoringUndef(N, Chain, DAG)) {
1745 return Chain; // remove the store and return the chain
1746 }
1747 //else everything is ok.
1748 return SDValue(N, 0);
1749 */
1750}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001751
1752SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1753 DAGCombinerInfo &DCI) const {
1754 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001755 case ISD::STORE:
1756 return PerformStoreCombine(N, DCI);
1757 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001758 return PerformPIC16LoadCombine(N, DCI);
1759 }
1760 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001761}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001762
1763static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1764 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001765 default: llvm_unreachable("Unknown condition code!");
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001766 case ISD::SETNE: return PIC16CC::NE;
1767 case ISD::SETEQ: return PIC16CC::EQ;
1768 case ISD::SETGT: return PIC16CC::GT;
1769 case ISD::SETGE: return PIC16CC::GE;
1770 case ISD::SETLT: return PIC16CC::LT;
1771 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001772 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta703e2352009-06-03 13:36:44 +00001773 case ISD::SETULE: return PIC16CC::ULE;
1774 case ISD::SETUGE: return PIC16CC::UGE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001775 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001776 }
1777}
1778
1779// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1780// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1781static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1782 ISD::CondCode CC, unsigned &SPCC) {
1783 if (isa<ConstantSDNode>(RHS) &&
1784 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1785 CC == ISD::SETNE &&
1786 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1787 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1788 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1789 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1790 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1791 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1792 SDValue CMPCC = LHS.getOperand(3);
1793 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1794 LHS = CMPCC.getOperand(0);
1795 RHS = CMPCC.getOperand(1);
1796 }
1797}
1798
1799// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1800SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1801 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001802 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001803 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1804
1805 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1806 // i.e. a < 12 can be rewritten as 12 > a.
1807 if (RHS.getOpcode() == ISD::Constant) {
1808
1809 SDValue Tmp = LHS;
1810 LHS = RHS;
1811 RHS = Tmp;
1812
1813 switch (CondCode) {
1814 default: break;
1815 case PIC16CC::LT:
1816 CondCode = PIC16CC::GT;
1817 break;
1818 case PIC16CC::GT:
1819 CondCode = PIC16CC::LT;
1820 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001821 case PIC16CC::ULT:
1822 CondCode = PIC16CC::UGT;
1823 break;
1824 case PIC16CC::UGT:
1825 CondCode = PIC16CC::ULT;
1826 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001827 case PIC16CC::GE:
1828 CondCode = PIC16CC::LE;
1829 break;
1830 case PIC16CC::LE:
1831 CondCode = PIC16CC::GE;
1832 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001833 case PIC16CC::ULE:
1834 CondCode = PIC16CC::UGE;
1835 break;
1836 case PIC16CC::UGE:
1837 CondCode = PIC16CC::ULE;
1838 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001839 }
1840 }
1841
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001843
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001844 // These are signed comparisons.
Owen Anderson825b72b2009-08-11 20:47:22 +00001845 SDValue Mask = DAG.getConstant(128, MVT::i8);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001846 if (isSignedComparison(CondCode)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001847 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1848 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001849 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001850
Owen Anderson825b72b2009-08-11 20:47:22 +00001851 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001852 // We can use a subtract operation to set the condition codes. But
1853 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001854 // Nothing to do if the first operand is already a valid type (direct load
1855 // for subwf and literal for sublw) and it is used by this operation only.
1856 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1857 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001858 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001859
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001860 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001861 LHS = ConvertToMemOperand (LHS, DAG, dl);
1862 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001863}
1864
1865
1866SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1867 SDValue LHS = Op.getOperand(0);
1868 SDValue RHS = Op.getOperand(1);
1869 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1870 SDValue TrueVal = Op.getOperand(2);
1871 SDValue FalseVal = Op.getOperand(3);
1872 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001873 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001874
1875 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1876 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1877 // i.e.
1878 // A setcc: lhs, rhs, cc is expanded by llvm to
1879 // select_cc: result of setcc, 0, 1, 0, setne
1880 // We can think of it as:
1881 // select_cc: lhs, rhs, 1, 0, cc
1882 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1883 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1884
1885 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001886 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001887
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001888 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001889 FalseVal, PIC16CC, Cmp.getValue(1));
1890}
1891
1892MachineBasicBlock *
1893PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00001894 MachineBasicBlock *BB,
1895 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001896 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1897 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001898 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001899
1900 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1901 // control-flow pattern. The incoming instruction knows the destination vreg
1902 // to set, the condition code register to branch on, the true/false values to
1903 // select between, and a branch opcode to use.
1904 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1905 MachineFunction::iterator It = BB;
1906 ++It;
1907
1908 // thisMBB:
1909 // ...
1910 // TrueVal = ...
1911 // [f]bCC copy1MBB
1912 // fallthrough --> copy0MBB
1913 MachineBasicBlock *thisMBB = BB;
1914 MachineFunction *F = BB->getParent();
1915 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1916 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001917 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001918 F->insert(It, copy0MBB);
1919 F->insert(It, sinkMBB);
1920
Evan Chengce319102009-09-19 09:51:03 +00001921 // Update machine-CFG edges by first adding all successors of the current
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001922 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00001923 // Also inform sdisel of the edge changes.
1924 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
1925 E = BB->succ_end(); I != E; ++I) {
1926 EM->insert(std::make_pair(*I, sinkMBB));
1927 sinkMBB->addSuccessor(*I);
1928 }
1929 // Next, remove all successors of the current block, and add the true
1930 // and fallthrough blocks as its successors.
1931 while (!BB->succ_empty())
1932 BB->removeSuccessor(BB->succ_begin());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001933 // Next, add the true and fallthrough blocks as its successors.
1934 BB->addSuccessor(copy0MBB);
1935 BB->addSuccessor(sinkMBB);
1936
1937 // copy0MBB:
1938 // %FalseValue = ...
1939 // # fallthrough to sinkMBB
1940 BB = copy0MBB;
1941
1942 // Update machine-CFG edges
1943 BB->addSuccessor(sinkMBB);
1944
1945 // sinkMBB:
1946 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1947 // ...
1948 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001949 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001950 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1951 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1952
1953 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1954 return BB;
1955}
1956
1957
1958SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1959 SDValue Chain = Op.getOperand(0);
1960 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1961 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1962 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1963 SDValue Dest = Op.getOperand(4); // BB to jump to
1964 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001965 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001966
1967 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1968 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1969 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1970 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1971
1972 // Get the Compare insn and condition code.
1973 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001974 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001975
Owen Anderson825b72b2009-08-11 20:47:22 +00001976 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001977 Cmp.getValue(1));
1978}
1979