blob: 915669b72787159a19dcd6eff68d9b2786e10249 [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 Gupta0e687712008-05-13 09:02:57 +000015#include "PIC16ISelLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000016#include "PIC16TargetObjectFile.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000017#include "PIC16TargetMachine.h"
18#include "llvm/DerivedTypes.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000019#include "llvm/GlobalValue.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000020#include "llvm/Function.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000021#include "llvm/CallingConv.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
Torok Edwin804e0fe2009-07-08 19:04:27 +000026#include "llvm/Support/ErrorHandling.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000027
28
Sanjiv Gupta0e687712008-05-13 09:02:57 +000029using namespace llvm;
30
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000031static const char *getIntrinsicName(unsigned opcode) {
32 std::string Basename;
33 switch(opcode) {
Sanjiv Guptaaa939172009-07-31 07:35:57 +000034 default: llvm_unreachable("do not know intrinsic name");
Sanjiv Gupta003263b2009-06-16 16:17:35 +000035 // Arithmetic Right shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000036 case PIC16ISD::SRA_I8: Basename = "sra.i8"; break;
37 case RTLIB::SRA_I16: Basename = "sra.i16"; break;
38 case RTLIB::SRA_I32: Basename = "sra.i32"; break;
39
Sanjiv Gupta003263b2009-06-16 16:17:35 +000040 // Left shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000041 case PIC16ISD::SLL_I8: Basename = "sll.i8"; break;
42 case RTLIB::SHL_I16: Basename = "sll.i16"; break;
43 case RTLIB::SHL_I32: Basename = "sll.i32"; break;
44
Sanjiv Gupta003263b2009-06-16 16:17:35 +000045 // Logical Right Shift for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000046 case PIC16ISD::SRL_I8: Basename = "srl.i8"; break;
47 case RTLIB::SRL_I16: Basename = "srl.i16"; break;
48 case RTLIB::SRL_I32: Basename = "srl.i32"; break;
49
Sanjiv Gupta003263b2009-06-16 16:17:35 +000050 // Multiply for integer types.
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000051 case PIC16ISD::MUL_I8: Basename = "mul.i8"; break;
52 case RTLIB::MUL_I16: Basename = "mul.i16"; break;
53 case RTLIB::MUL_I32: Basename = "mul.i32"; break;
Sanjiv Gupta46800772009-06-05 14:43:12 +000054
Sanjiv Gupta003263b2009-06-16 16:17:35 +000055 // Signed division for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000056 case RTLIB::SDIV_I16: Basename = "sdiv.i16"; break;
57 case RTLIB::SDIV_I32: Basename = "sdiv.i32"; break;
Sanjiv Gupta003263b2009-06-16 16:17:35 +000058
59 // Unsigned division for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000060 case RTLIB::UDIV_I16: Basename = "udiv.i16"; break;
61 case RTLIB::UDIV_I32: Basename = "udiv.i32"; break;
62
Sanjiv Gupta003263b2009-06-16 16:17:35 +000063 // Signed Modulas for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000064 case RTLIB::SREM_I16: Basename = "srem.i16"; break;
65 case RTLIB::SREM_I32: Basename = "srem.i32"; break;
Sanjiv Gupta003263b2009-06-16 16:17:35 +000066
67 // Unsigned Modulas for integers.
Sanjiv Gupta46800772009-06-05 14:43:12 +000068 case RTLIB::UREM_I16: Basename = "urem.i16"; break;
69 case RTLIB::UREM_I32: Basename = "urem.i32"; break;
Sanjiv Gupta63963292009-06-11 16:50:48 +000070
Sanjiv Gupta003263b2009-06-16 16:17:35 +000071 //////////////////////
72 // LIBCALLS FOR FLOATS
73 //////////////////////
74
75 // Float to signed integrals
76 case RTLIB::FPTOSINT_F32_I8: Basename = "f32_to_si32"; break;
77 case RTLIB::FPTOSINT_F32_I16: Basename = "f32_to_si32"; break;
78 case RTLIB::FPTOSINT_F32_I32: Basename = "f32_to_si32"; break;
79
80 // Signed integrals to float. char and int are first sign extended to i32
81 // before being converted to float, so an I8_F32 or I16_F32 isn't required.
82 case RTLIB::SINTTOFP_I32_F32: Basename = "si32_to_f32"; break;
83
84 // Float to Unsigned conversions.
85 // Signed conversion can be used for unsigned conversion as well.
86 // In signed and unsigned versions only the interpretation of the
87 // MSB is different. Bit representation remains the same.
88 case RTLIB::FPTOUINT_F32_I8: Basename = "f32_to_si32"; break;
89 case RTLIB::FPTOUINT_F32_I16: Basename = "f32_to_si32"; break;
90 case RTLIB::FPTOUINT_F32_I32: Basename = "f32_to_si32"; break;
91
92 // Unsigned to Float conversions. char and int are first zero extended
93 // before being converted to float.
94 case RTLIB::UINTTOFP_I32_F32: Basename = "ui32_to_f32"; break;
Sanjiv Gupta63963292009-06-11 16:50:48 +000095
Sanjiv Gupta003263b2009-06-16 16:17:35 +000096 // Floating point add, sub, mul, div.
Sanjiv Gupta63963292009-06-11 16:50:48 +000097 case RTLIB::ADD_F32: Basename = "add.f32"; break;
98 case RTLIB::SUB_F32: Basename = "sub.f32"; break;
99 case RTLIB::MUL_F32: Basename = "mul.f32"; break;
100 case RTLIB::DIV_F32: Basename = "div.f32"; break;
Sanjiv Guptacbd5f0a2009-06-25 18:12:06 +0000101
102 // Floating point comparison
103 case RTLIB::O_F32: Basename = "unordered.f32"; break;
104 case RTLIB::UO_F32: Basename = "unordered.f32"; break;
105 case RTLIB::OLE_F32: Basename = "le.f32"; break;
106 case RTLIB::OGE_F32: Basename = "ge.f32"; break;
107 case RTLIB::OLT_F32: Basename = "lt.f32"; break;
108 case RTLIB::OGT_F32: Basename = "gt.f32"; break;
109 case RTLIB::OEQ_F32: Basename = "eq.f32"; break;
110 case RTLIB::UNE_F32: Basename = "neq.f32"; break;
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000111 }
112
113 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
114 std::string tagname = PAN::getTagName(PAN::LIBCALL);
115 std::string Fullname = prefix + tagname + Basename;
116
117 // The name has to live through program life.
Sanjiv Guptaaa939172009-07-31 07:35:57 +0000118 return createESName(Fullname);
119}
120
121// getStdLibCallName - Get the name for the standard library function.
122static const char *getStdLibCallName(unsigned opcode) {
123 std::string BaseName;
124 switch(opcode) {
125 case RTLIB::COS_F32: BaseName = "cos";
126 break;
127 case RTLIB::SIN_F32: BaseName = "sin";
128 break;
129 case RTLIB::MEMCPY: BaseName = "memcpy";
130 break;
131 case RTLIB::MEMSET: BaseName = "memset";
132 break;
133 case RTLIB::MEMMOVE: BaseName = "memmove";
134 break;
135 default: llvm_unreachable("do not know std lib call name");
136 }
137 std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
138 std::string LibCallName = prefix + BaseName;
139
140 // The name has to live through program life.
141 return createESName(LibCallName);
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000142}
143
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000144// PIC16TargetLowering Constructor.
145PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
Chris Lattnera87dea42009-07-31 18:48:30 +0000146 : TargetLowering(TM, new PIC16TargetObjectFile()), TmpSize(0) {
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000147
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000148 Subtarget = &TM.getSubtarget<PIC16Subtarget>();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000149
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000150 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000151
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000152 setShiftAmountType(MVT::i8);
Sanjiv Guptaaa939172009-07-31 07:35:57 +0000153
154 // Std lib call names
155 setLibcallName(RTLIB::COS_F32, getStdLibCallName(RTLIB::COS_F32));
156 setLibcallName(RTLIB::SIN_F32, getStdLibCallName(RTLIB::SIN_F32));
157 setLibcallName(RTLIB::MEMCPY, getStdLibCallName(RTLIB::MEMCPY));
158 setLibcallName(RTLIB::MEMSET, getStdLibCallName(RTLIB::MEMSET));
159 setLibcallName(RTLIB::MEMMOVE, getStdLibCallName(RTLIB::MEMMOVE));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000160
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000161 // SRA library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000162 setPIC16LibcallName(PIC16ISD::SRA_I8, getIntrinsicName(PIC16ISD::SRA_I8));
163 setLibcallName(RTLIB::SRA_I16, getIntrinsicName(RTLIB::SRA_I16));
164 setLibcallName(RTLIB::SRA_I32, getIntrinsicName(RTLIB::SRA_I32));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000165
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000166 // SHL library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000167 setPIC16LibcallName(PIC16ISD::SLL_I8, getIntrinsicName(PIC16ISD::SLL_I8));
168 setLibcallName(RTLIB::SHL_I16, getIntrinsicName(RTLIB::SHL_I16));
169 setLibcallName(RTLIB::SHL_I32, getIntrinsicName(RTLIB::SHL_I32));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000170
171 // SRL library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000172 setPIC16LibcallName(PIC16ISD::SRL_I8, getIntrinsicName(PIC16ISD::SRL_I8));
173 setLibcallName(RTLIB::SRL_I16, getIntrinsicName(RTLIB::SRL_I16));
174 setLibcallName(RTLIB::SRL_I32, getIntrinsicName(RTLIB::SRL_I32));
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000175
176 // MUL Library call names
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000177 setPIC16LibcallName(PIC16ISD::MUL_I8, getIntrinsicName(PIC16ISD::MUL_I8));
178 setLibcallName(RTLIB::MUL_I16, getIntrinsicName(RTLIB::MUL_I16));
179 setLibcallName(RTLIB::MUL_I32, getIntrinsicName(RTLIB::MUL_I32));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000180
Sanjiv Gupta46800772009-06-05 14:43:12 +0000181 // Signed division lib call names
182 setLibcallName(RTLIB::SDIV_I16, getIntrinsicName(RTLIB::SDIV_I16));
183 setLibcallName(RTLIB::SDIV_I32, getIntrinsicName(RTLIB::SDIV_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000184
Sanjiv Gupta46800772009-06-05 14:43:12 +0000185 // Unsigned division lib call names
186 setLibcallName(RTLIB::UDIV_I16, getIntrinsicName(RTLIB::UDIV_I16));
187 setLibcallName(RTLIB::UDIV_I32, getIntrinsicName(RTLIB::UDIV_I32));
188
189 // Signed remainder lib call names
190 setLibcallName(RTLIB::SREM_I16, getIntrinsicName(RTLIB::SREM_I16));
191 setLibcallName(RTLIB::SREM_I32, getIntrinsicName(RTLIB::SREM_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000192
Sanjiv Gupta46800772009-06-05 14:43:12 +0000193 // Unsigned remainder lib call names
194 setLibcallName(RTLIB::UREM_I16, getIntrinsicName(RTLIB::UREM_I16));
195 setLibcallName(RTLIB::UREM_I32, getIntrinsicName(RTLIB::UREM_I32));
Sanjiv Gupta63963292009-06-11 16:50:48 +0000196
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000197 // Floating point to signed int conversions.
198 setLibcallName(RTLIB::FPTOSINT_F32_I8,
199 getIntrinsicName(RTLIB::FPTOSINT_F32_I8));
200 setLibcallName(RTLIB::FPTOSINT_F32_I16,
201 getIntrinsicName(RTLIB::FPTOSINT_F32_I16));
Sanjiv Gupta63963292009-06-11 16:50:48 +0000202 setLibcallName(RTLIB::FPTOSINT_F32_I32,
203 getIntrinsicName(RTLIB::FPTOSINT_F32_I32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000204
205 // Signed int to floats.
Sanjiv Gupta63963292009-06-11 16:50:48 +0000206 setLibcallName(RTLIB::SINTTOFP_I32_F32,
207 getIntrinsicName(RTLIB::SINTTOFP_I32_F32));
Sanjiv Gupta003263b2009-06-16 16:17:35 +0000208
209 // Floating points to unsigned ints.
210 setLibcallName(RTLIB::FPTOUINT_F32_I8,
211 getIntrinsicName(RTLIB::FPTOUINT_F32_I8));
212 setLibcallName(RTLIB::FPTOUINT_F32_I16,
213 getIntrinsicName(RTLIB::FPTOUINT_F32_I16));
214 setLibcallName(RTLIB::FPTOUINT_F32_I32,
215 getIntrinsicName(RTLIB::FPTOUINT_F32_I32));
216
217 // Unsigned int to floats.
218 setLibcallName(RTLIB::UINTTOFP_I32_F32,
219 getIntrinsicName(RTLIB::UINTTOFP_I32_F32));
220
221 // Floating point add, sub, mul ,div.
Sanjiv Gupta63963292009-06-11 16:50:48 +0000222 setLibcallName(RTLIB::ADD_F32, getIntrinsicName(RTLIB::ADD_F32));
223 setLibcallName(RTLIB::SUB_F32, getIntrinsicName(RTLIB::SUB_F32));
224 setLibcallName(RTLIB::MUL_F32, getIntrinsicName(RTLIB::MUL_F32));
225 setLibcallName(RTLIB::DIV_F32, getIntrinsicName(RTLIB::DIV_F32));
226
Sanjiv Guptacbd5f0a2009-06-25 18:12:06 +0000227 // Floationg point comparison
228 setLibcallName(RTLIB::UO_F32, getIntrinsicName(RTLIB::UO_F32));
229 setLibcallName(RTLIB::OLE_F32, getIntrinsicName(RTLIB::OLE_F32));
230 setLibcallName(RTLIB::OGE_F32, getIntrinsicName(RTLIB::OGE_F32));
231 setLibcallName(RTLIB::OLT_F32, getIntrinsicName(RTLIB::OLT_F32));
232 setLibcallName(RTLIB::OGT_F32, getIntrinsicName(RTLIB::OGT_F32));
233 setLibcallName(RTLIB::OEQ_F32, getIntrinsicName(RTLIB::OEQ_F32));
234 setLibcallName(RTLIB::UNE_F32, getIntrinsicName(RTLIB::UNE_F32));
235
236 // Return value comparisons of floating point calls.
237 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
238 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
239 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
240 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
241 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
242 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
243 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
244 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
245
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000246 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000247 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000248
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000249 setOperationAction(ISD::LOAD, MVT::i8, Legal);
250 setOperationAction(ISD::LOAD, MVT::i16, Custom);
251 setOperationAction(ISD::LOAD, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000252
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000253 setOperationAction(ISD::STORE, MVT::i8, Legal);
254 setOperationAction(ISD::STORE, MVT::i16, Custom);
255 setOperationAction(ISD::STORE, MVT::i32, Custom);
Sanjiv Guptaada097a2009-08-04 17:59:16 +0000256 setOperationAction(ISD::STORE, MVT::i64, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000257
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000258 setOperationAction(ISD::ADDE, MVT::i8, Custom);
259 setOperationAction(ISD::ADDC, MVT::i8, Custom);
260 setOperationAction(ISD::SUBE, MVT::i8, Custom);
261 setOperationAction(ISD::SUBC, MVT::i8, Custom);
Sanjiv Gupta3b0a4f12009-06-04 08:52:28 +0000262 setOperationAction(ISD::SUB, MVT::i8, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000263 setOperationAction(ISD::ADD, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000264 setOperationAction(ISD::ADD, MVT::i16, Custom);
265
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000266 setOperationAction(ISD::OR, MVT::i8, Custom);
267 setOperationAction(ISD::AND, MVT::i8, Custom);
268 setOperationAction(ISD::XOR, MVT::i8, Custom);
269
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000270 setOperationAction(ISD::FrameIndex, MVT::i16, Custom);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000271
Eli Friedmane91bf2c2009-07-17 07:03:00 +0000272 setOperationAction(ISD::MUL, MVT::i8, Custom);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000273
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000274 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000275 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000276 setOperationAction(ISD::MULHU, MVT::i8, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000277 setOperationAction(ISD::MULHS, MVT::i8, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000278
279 setOperationAction(ISD::SRA, MVT::i8, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000280 setOperationAction(ISD::SHL, MVT::i8, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000281 setOperationAction(ISD::SRL, MVT::i8, Custom);
Eli Friedmane91bf2c2009-07-17 07:03:00 +0000282
283 setOperationAction(ISD::ROTL, MVT::i8, Expand);
284 setOperationAction(ISD::ROTR, MVT::i8, Expand);
285
286 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000287
288 // PIC16 does not support shift parts
Eli Friedmane91bf2c2009-07-17 07:03:00 +0000289 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000290 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000291 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000292
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000293
294 // PIC16 does not have a SETCC, expand it to SELECT_CC.
295 setOperationAction(ISD::SETCC, MVT::i8, Expand);
296 setOperationAction(ISD::SELECT, MVT::i8, Expand);
297 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
298 setOperationAction(ISD::BRIND, MVT::Other, Expand);
299
300 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
301 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000302
303 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
304 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
305
306 // Now deduce the information based on the above mentioned
307 // actions
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000308 computeRegisterProperties();
309}
310
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000311// getOutFlag - Extract the flag result if the Op has it.
312static SDValue getOutFlag(SDValue &Op) {
313 // Flag is the last value of the node.
314 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
315
316 assert (Flag.getValueType() == MVT::Flag
317 && "Node does not have an out Flag");
318
319 return Flag;
320}
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000321// Get the TmpOffset for FrameIndex
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000322unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI, unsigned size) {
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000323 std::map<unsigned, unsigned>::iterator
324 MapIt = FiTmpOffsetMap.find(FI);
325 if (MapIt != FiTmpOffsetMap.end())
326 return MapIt->second;
327
328 // This FI (FrameIndex) is not yet mapped, so map it
329 FiTmpOffsetMap[FI] = TmpSize;
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000330 TmpSize += size;
331 return FiTmpOffsetMap[FI];
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000332}
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000333
334// To extract chain value from the SDValue Nodes
335// This function will help to maintain the chain extracting
336// code at one place. In case of any change in future it will
337// help maintain the code.
338static SDValue getChain(SDValue &Op) {
339 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
340
341 // If the last value returned in Flag then the chain is
342 // second last value returned.
343 if (Chain.getValueType() == MVT::Flag)
344 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
345
346 // All nodes may not produce a chain. Therefore following assert
347 // verifies that the node is returning a chain only.
348 assert (Chain.getValueType() == MVT::Other
349 && "Node does not have a chain");
350
351 return Chain;
352}
353
354/// PopulateResults - Helper function to LowerOperation.
355/// If a node wants to return multiple results after lowering,
356/// it stuffs them into an array of SDValue called Results.
357
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000358static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
359 if (N.getOpcode() == ISD::MERGE_VALUES) {
360 int NumResults = N.getNumOperands();
361 for( int i = 0; i < NumResults; i++)
362 Results.push_back(N.getOperand(i));
363 }
364 else
365 Results.push_back(N);
366}
367
368MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
369 return MVT::i8;
370}
371
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000372/// The type legalizer framework of generating legalizer can generate libcalls
373/// only when the operand/result types are illegal.
374/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
375/// For example an arithmetic right shift. These functions are used to lower
376/// such operations that generate libcall for legal types.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000377
378void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000379PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000380 const char *Name) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000381 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000382}
383
384const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000385PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000386 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000387}
388
389SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000390PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000391 MVT RetVT, const SDValue *Ops,
392 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000393 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000394
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000395 TargetLowering::ArgListTy Args;
396 Args.reserve(NumOps);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000397
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000398 TargetLowering::ArgListEntry Entry;
399 for (unsigned i = 0; i != NumOps; ++i) {
400 Entry.Node = Ops[i];
Owen Andersondebcb012009-07-29 22:17:13 +0000401 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000402 Entry.isSExt = isSigned;
403 Entry.isZExt = !isSigned;
404 Args.push_back(Entry);
405 }
406 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000407
Owen Andersondebcb012009-07-29 22:17:13 +0000408 const Type *RetTy = RetVT.getTypeForMVT();
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000409 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000410 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000411 false, 0, CallingConv::C, false,
412 /*isReturnValueUsed=*/true,
413 Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000414
415 return CallInfo.first;
416}
417
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000418const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
419 switch (Opcode) {
420 default: return NULL;
421 case PIC16ISD::Lo: return "PIC16ISD::Lo";
422 case PIC16ISD::Hi: return "PIC16ISD::Hi";
423 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
424 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000425 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
426 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000427 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
428 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000429 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000430 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000431 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000432 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000433 case PIC16ISD::BCF: return "PIC16ISD::BCF";
434 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
435 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
436 case PIC16ISD::RLF: return "PIC16ISD::RLF";
437 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000438 case PIC16ISD::CALL: return "PIC16ISD::CALL";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000439 case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000440 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
441 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
442 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Dan Gohman98ca4f22009-08-05 01:29:28 +0000443 case PIC16ISD::RET: return "PIC16ISD::RET";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000444 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000445 }
446}
447
Duncan Sands1607f052008-12-01 11:39:25 +0000448void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
449 SmallVectorImpl<SDValue>&Results,
450 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000451
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000452 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000453 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000454 Results.push_back(ExpandGlobalAddress(N, DAG));
455 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000456 case ISD::ExternalSymbol:
457 Results.push_back(ExpandExternalSymbol(N, DAG));
458 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000459 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000460 Results.push_back(ExpandStore(N, DAG));
461 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000462 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000463 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000464 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000465 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000466 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000467 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000468 case ISD::FrameIndex:
469 Results.push_back(ExpandFrameIndex(N, DAG));
470 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000471 default:
472 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000473 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000474 }
475}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000476
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000477SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
478
479 // Currently handling FrameIndex of size MVT::i16 only
480 // One example of this scenario is when return value is written on
481 // FrameIndex#0
482
483 if (N->getValueType(0) != MVT::i16)
484 return SDValue();
485
486 // Expand the FrameIndex into ExternalSymbol and a Constant node
487 // The constant will represent the frame index number
488 // Get the current function frame
489 MachineFunction &MF = DAG.getMachineFunction();
490 const Function *Func = MF.getFunction();
491 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000492
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000493 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000494 // FIXME there isn't really debug info here
495 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000496
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000497 // Expand FrameIndex like GlobalAddress and ExternalSymbol
498 // Also use Offset field for lo and hi parts. The default
499 // offset is zero.
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000500
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000501 SDValue ES;
502 int FrameOffset;
503 SDValue FI = SDValue(N,0);
504 LegalizeFrameIndex(FI, DAG, ES, FrameOffset);
505 SDValue Offset = DAG.getConstant(FrameOffset, MVT::i8);
506 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, ES, Offset);
507 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, ES, Offset);
508 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000509}
510
511
512SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000513 StoreSDNode *St = cast<StoreSDNode>(N);
514 SDValue Chain = St->getChain();
515 SDValue Src = St->getValue();
516 SDValue Ptr = St->getBasePtr();
517 MVT ValueType = Src.getValueType();
518 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000519 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000520
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000521 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000522 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000523
524 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000525 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000526 PtrLo, PtrHi,
527 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000528 }
529 else if (ValueType == MVT::i16) {
530 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
531 SDValue SrcLo, SrcHi;
532 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
533 SDValue ChainLo = Chain, ChainHi = Chain;
534 if (Chain.getOpcode() == ISD::TokenFactor) {
535 ChainLo = Chain.getOperand(0);
536 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000537 }
Dale Johannesende064702009-02-06 21:50:26 +0000538 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000539 ChainLo,
540 SrcLo, PtrLo, PtrHi,
541 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000542
Dale Johannesende064702009-02-06 21:50:26 +0000543 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000544 SrcHi, PtrLo, PtrHi,
545 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000546
Dale Johannesende064702009-02-06 21:50:26 +0000547 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000548 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000549 }
550 else if (ValueType == MVT::i32) {
551 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
552 SDValue SrcLo, SrcHi;
553 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000554
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000555 // Get the expanded parts of each of SrcLo and SrcHi.
556 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
557 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
558 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000559
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000560 SDValue ChainLo = Chain, ChainHi = Chain;
561 if (Chain.getOpcode() == ISD::TokenFactor) {
562 ChainLo = Chain.getOperand(0);
563 ChainHi = Chain.getOperand(1);
564 }
565 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
566 ChainHi2 = ChainHi;
567 if (ChainLo.getOpcode() == ISD::TokenFactor) {
568 ChainLo1 = ChainLo.getOperand(0);
569 ChainLo2 = ChainLo.getOperand(1);
570 }
571 if (ChainHi.getOpcode() == ISD::TokenFactor) {
572 ChainHi1 = ChainHi.getOperand(0);
573 ChainHi2 = ChainHi.getOperand(1);
574 }
Dale Johannesende064702009-02-06 21:50:26 +0000575 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000576 ChainLo1,
577 SrcLo1, PtrLo, PtrHi,
578 DAG.getConstant (0 + StoreOffset, MVT::i8));
579
Dale Johannesende064702009-02-06 21:50:26 +0000580 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000581 SrcLo2, PtrLo, PtrHi,
582 DAG.getConstant (1 + StoreOffset, MVT::i8));
583
Dale Johannesende064702009-02-06 21:50:26 +0000584 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000585 SrcHi1, PtrLo, PtrHi,
586 DAG.getConstant (2 + StoreOffset, MVT::i8));
587
Dale Johannesende064702009-02-06 21:50:26 +0000588 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000589 SrcHi2, PtrLo, PtrHi,
590 DAG.getConstant (3 + StoreOffset, MVT::i8));
591
Dale Johannesende064702009-02-06 21:50:26 +0000592 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
593 getChain(Store1), getChain(Store2));
594 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
595 getChain(Store3), getChain(Store4));
596 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000597
Sanjiv Guptaada097a2009-08-04 17:59:16 +0000598 } else if (ValueType == MVT::i64) {
599 SDValue SrcLo, SrcHi;
600 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
601 SDValue ChainLo = Chain, ChainHi = Chain;
602 if (Chain.getOpcode() == ISD::TokenFactor) {
603 ChainLo = Chain.getOperand(0);
604 ChainHi = Chain.getOperand(1);
605 }
606 SDValue Store1 = DAG.getStore(ChainLo, dl, SrcLo, Ptr, NULL,
607 0 + StoreOffset);
608
609 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
610 DAG.getConstant(4, Ptr.getValueType()));
611 SDValue Store2 = DAG.getStore(ChainHi, dl, SrcHi, Ptr, NULL,
612 1 + StoreOffset);
613
614 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1,
615 Store2);
616 } else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000617 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000618 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000619 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000620}
621
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000622SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
623{
624 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000625 // FIXME there isn't really debug info here
626 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000627
628 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000629 SDValue Offset = DAG.getConstant(0, MVT::i8);
630 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES, Offset);
631 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES, Offset);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000632
Dale Johannesende064702009-02-06 21:50:26 +0000633 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000634}
635
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000636// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000637SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000638 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000639 // FIXME there isn't really debug info here
640 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000641
642 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
643 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000644
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000645 SDValue Offset = DAG.getConstant(0, MVT::i8);
646 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA, Offset);
647 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA, Offset);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000648
Dale Johannesende064702009-02-06 21:50:26 +0000649 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000650}
651
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000652bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
653 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000654
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000655 if (Op.getOpcode() == ISD::BUILD_PAIR) {
656 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
657 return true;
658 }
659 return false;
660}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000661
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000662// Return true if DirectAddress is in ROM_SPACE
663bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000664
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000665 // RomAddress is a GlobalAddress in ROM_SPACE_
666 // If the Op is not a GlobalAddress return NULL without checking
667 // anything further.
668 if (!isDirectAddress(Op))
669 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000670
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000671 // Its a GlobalAddress.
672 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
673 SDValue TGA = Op.getOperand(0).getOperand(0);
674 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000675
Sanjiv Guptaa3518a12009-04-29 04:43:24 +0000676 if (GSDN->getAddressSpace() == PIC16ISD::ROM_SPACE)
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000677 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000678
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000679 // Any other address space return it false
680 return false;
681}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000682
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000683
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000684// GetExpandedParts - This function is on the similiar lines as
685// the GetExpandedInteger in type legalizer is. This returns expanded
686// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000687
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000688void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
689 SDValue &Lo, SDValue &Hi) {
690 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000691 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000692 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000693
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000694 // Extract the lo component.
695 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
696 DAG.getConstant(0, MVT::i8));
Bill Wendling51b16f42009-05-30 01:09:53 +0000697
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000698 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000699 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
700 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000701}
702
703// Legalize FrameIndex into ExternalSymbol and offset.
704void
705PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
706 SDValue &ES, int &Offset) {
707
708 MachineFunction &MF = DAG.getMachineFunction();
709 const Function *Func = MF.getFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000710 MachineFrameInfo *MFI = MF.getFrameInfo();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000711 const std::string Name = Func->getName();
712
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000713 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000714
715 // FrameIndices are not stack offsets. But they represent the request
716 // for space on stack. That space requested may be more than one byte.
717 // Therefore, to calculate the stack offset that a FrameIndex aligns
718 // with, we need to traverse all the FrameIndices available earlier in
719 // the list and add their requested size.
720 unsigned FIndex = FR->getIndex();
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000721 const char *tmpName;
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000722 if (FIndex < ReservedFrameCount) {
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000723 tmpName = createESName(PAN::getFrameLabel(Name));
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000724 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
725 Offset = 0;
726 for (unsigned i=0; i<FIndex ; ++i) {
727 Offset += MFI->getObjectSize(i);
728 }
729 } else {
730 // FrameIndex has been made for some temporary storage
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000731 tmpName = createESName(PAN::getTempdataLabel(Name));
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000732 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
733 Offset = GetTmpOffsetForFI(FIndex, MFI->getObjectSize(FIndex));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000734 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000735
736 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000737}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000738
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000739// This function legalizes the PIC16 Addresses. If the Pointer is
740// -- Direct address variable residing
741// --> then a Banksel for that variable will be created.
742// -- Rom variable
743// --> then it will be treated as an indirect address.
744// -- Indirect address
745// --> then the address will be loaded into FSR
746// -- ADD with constant operand
747// --> then constant operand of ADD will be returned as Offset
748// and non-constant operand of ADD will be treated as pointer.
749// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000750
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000751void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
752 SDValue &Lo, SDValue &Hi,
753 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000754
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000755 // Offset, by default, should be 0
756 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000757
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000758 // If the pointer is ADD with constant,
759 // return the constant value as the offset
760 if (Ptr.getOpcode() == ISD::ADD) {
761 SDValue OperLeft = Ptr.getOperand(0);
762 SDValue OperRight = Ptr.getOperand(1);
Sanjiv Guptaae992272009-06-23 07:10:19 +0000763 if ((OperLeft.getOpcode() == ISD::Constant) &&
764 (dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue() < 32 )) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000765 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
766 Ptr = OperRight;
Sanjiv Guptaae992272009-06-23 07:10:19 +0000767 } else if ((OperRight.getOpcode() == ISD::Constant) &&
768 (dyn_cast<ConstantSDNode>(OperRight)->getZExtValue() < 32 )){
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000769 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000770 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000771 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000772 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000773
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000774 // If the pointer is Type i8 and an external symbol
775 // then treat it as direct address.
776 // One example for such case is storing and loading
777 // from function frame during a call
778 if (Ptr.getValueType() == MVT::i8) {
779 switch (Ptr.getOpcode()) {
780 case ISD::TargetExternalSymbol:
781 Lo = Ptr;
782 Hi = DAG.getConstant(1, MVT::i8);
783 return;
784 }
785 }
786
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000787 // Expansion of FrameIndex has Lo/Hi parts
788 if (isDirectAddress(Ptr)) {
789 SDValue TFI = Ptr.getOperand(0).getOperand(0);
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000790 int FrameOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000791 if (TFI.getOpcode() == ISD::TargetFrameIndex) {
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000792 LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset);
793 Hi = DAG.getConstant(1, MVT::i8);
794 Offset += FrameOffset;
795 return;
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000796 } else if (TFI.getOpcode() == ISD::TargetExternalSymbol) {
797 // FrameIndex has already been expanded.
798 // Now just make use of its expansion
799 Lo = TFI;
800 Hi = DAG.getConstant(1, MVT::i8);
801 SDValue FOffset = Ptr.getOperand(0).getOperand(1);
802 assert (FOffset.getOpcode() == ISD::Constant &&
803 "Invalid operand of PIC16ISD::Lo");
804 Offset += dyn_cast<ConstantSDNode>(FOffset)->getZExtValue();
805 return;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000806 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000807 }
808
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000809 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
810 // Direct addressing case for RAM variables. The Hi part is constant
811 // and the Lo part is the TGA itself.
812 Lo = Ptr.getOperand(0).getOperand(0);
813
814 // For direct addresses Hi is a constant. Value 1 for the constant
815 // signifies that banksel needs to generated for it. Value 0 for
816 // the constant signifies that banksel does not need to be generated
817 // for it. Mark it as 1 now and optimize later.
818 Hi = DAG.getConstant(1, MVT::i8);
819 return;
820 }
821
822 // Indirect addresses. Get the hi and lo parts of ptr.
823 GetExpandedParts(Ptr, DAG, Lo, Hi);
824
825 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000826 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
827 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000828
829 return;
830}
831
Duncan Sands1607f052008-12-01 11:39:25 +0000832SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000833 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
834 SDValue Chain = LD->getChain();
835 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000836 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000837
838 SDValue Load, Offset;
839 SDVTList Tys;
840 MVT VT, NewVT;
841 SDValue PtrLo, PtrHi;
842 unsigned LoadOffset;
843
844 // Legalize direct/indirect addresses. This will give the lo and hi parts
845 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000846 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000847
848 // Load from the pointer (direct address or FSR)
849 VT = N->getValueType(0);
850 unsigned NumLoads = VT.getSizeInBits() / 8;
851 std::vector<SDValue> PICLoads;
852 unsigned iter;
853 MVT MemVT = LD->getMemoryVT();
854 if(ISD::isNON_EXTLoad(N)) {
855 for (iter=0; iter<NumLoads ; ++iter) {
856 // Add the pointer offset if any
857 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
858 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000859 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000860 Offset);
861 PICLoads.push_back(Load);
862 }
863 } else {
864 // If it is extended load then use PIC16Load for Memory Bytes
865 // and for all extended bytes perform action based on type of
866 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
867
868
869 // For extended loads this is the memory value type
870 // i.e. without any extension
871 MVT MemVT = LD->getMemoryVT();
872 unsigned MemBytes = MemVT.getSizeInBits() / 8;
Sanjiv Gupta1d2e6ed2009-07-03 07:58:59 +0000873 // if MVT::i1 is extended to MVT::i8 then MemBytes will be zero
874 // So set it to one
875 if (MemBytes == 0) MemBytes = 1;
876
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000877 unsigned ExtdBytes = VT.getSizeInBits() / 8;
878 Offset = DAG.getConstant(LoadOffset, MVT::i8);
879
880 Tys = DAG.getVTList(MVT::i8, MVT::Other);
881 // For MemBytes generate PIC16Load with proper offset
Sanjiv Gupta1d2e6ed2009-07-03 07:58:59 +0000882 for (iter=0; iter < MemBytes; ++iter) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000883 // Add the pointer offset if any
884 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000885 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000886 Offset);
887 PICLoads.push_back(Load);
888 }
889
890 // For SignExtendedLoad
891 if (ISD::isSEXTLoad(N)) {
892 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
893 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000894 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000895 DAG.getConstant(7, MVT::i8));
896 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
897 PICLoads.push_back(SRA);
898 }
Sanjiv Gupta892c8ca2009-06-03 15:31:12 +0000899 } else if (ISD::isZEXTLoad(N) || ISD::isEXTLoad(N)) {
900 //} else if (ISD::isZEXTLoad(N)) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000901 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
902 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
903 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
904 PICLoads.push_back(ConstZero);
905 }
906 }
907 }
908 SDValue BP;
909
910 if (VT == MVT::i8) {
911 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000912 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000913 }
914 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000915 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000916 if (MemVT == MVT::i8)
917 Chain = getChain(PICLoads[0]);
918 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000919 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
920 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000921 } else if (VT == MVT::i32) {
922 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000923 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
924 PICLoads[0], PICLoads[1]);
925 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
926 PICLoads[2], PICLoads[3]);
927 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000928 if (MemVT == MVT::i8)
929 Chain = getChain(PICLoads[0]);
930 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000931 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
932 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000933 else {
934 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000935 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000936 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000937 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000938 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000939 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
940 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000941 }
942 }
943 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000944 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000945}
946
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000947SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
948 // We should have handled larger operands in type legalizer itself.
949 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
950
951 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000952 SDValue Value = N->getOperand(0);
953 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000954 PIC16ISD::PIC16Libcall CallCode;
955 switch (N->getOpcode()) {
956 case ISD::SRA:
957 CallCode = PIC16ISD::SRA_I8;
958 break;
959 case ISD::SHL:
960 CallCode = PIC16ISD::SLL_I8;
961 break;
962 case ISD::SRL:
963 CallCode = PIC16ISD::SRL_I8;
964 break;
965 default:
966 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000967 return SDValue();
968 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000969 SmallVector<SDValue, 2> Ops(2);
970 Ops[0] = Value;
971 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000972 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
973 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000974 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000975}
976
Sanjiv Gupta8e14eab2009-07-27 02:44:46 +0000977SDValue PIC16TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) {
978 // We should have handled larger operands in type legalizer itself.
979 assert (Op.getValueType() == MVT::i8 && "illegal multiply to lower");
980
981 SDNode *N = Op.getNode();
982 SmallVector<SDValue, 2> Ops(2);
983 Ops[0] = N->getOperand(0);
984 Ops[1] = N->getOperand(1);
985 SDValue Call = MakePIC16Libcall(PIC16ISD::MUL_I8, N->getValueType(0),
986 &Ops[0], 2, true, DAG, N->getDebugLoc());
987 return Call;
988}
989
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000990void
991PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
992 SmallVectorImpl<SDValue>&Results,
993 SelectionDAG &DAG) {
994 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000995 SDValue Res;
996 unsigned i;
997 switch (Op.getOpcode()) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000998 case ISD::LOAD:
999 Res = ExpandLoad(Op.getNode(), DAG); break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001000 default: {
1001 // All other operations are handled in LowerOperation.
1002 Res = LowerOperation(Op, DAG);
1003 if (Res.getNode())
1004 Results.push_back(Res);
1005
1006 return;
1007 }
1008 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +00001009
1010 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001011 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +00001012 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001013 Results.push_back(SDValue(N, i));
1014 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001015}
1016
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001017SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
1018 switch (Op.getOpcode()) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001019 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001020 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001021 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001022 return LowerADD(Op, DAG);
1023 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001024 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001025 case ISD::SUBE:
1026 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001027 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +00001028 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001029 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +00001030 return ExpandStore(Op.getNode(), DAG);
Sanjiv Gupta8e14eab2009-07-27 02:44:46 +00001031 case ISD::MUL:
1032 return LowerMUL(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001033 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001034 case ISD::SRA:
1035 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001036 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001037 case ISD::OR:
1038 case ISD::AND:
1039 case ISD::XOR:
1040 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001041 case ISD::BR_CC:
1042 return LowerBR_CC(Op, DAG);
1043 case ISD::SELECT_CC:
1044 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001045 }
Dan Gohman475871a2008-07-27 21:46:04 +00001046 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001047}
1048
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001049SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001050 SelectionDAG &DAG,
1051 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001052 assert (Op.getValueType() == MVT::i8
1053 && "illegal value type to store on stack.");
1054
1055 MachineFunction &MF = DAG.getMachineFunction();
1056 const Function *Func = MF.getFunction();
1057 const std::string FuncName = Func->getName();
1058
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001059
1060 // Put the value on stack.
1061 // Get a stack slot index and convert to es.
1062 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001063 const char *tmpName = createESName(PAN::getTempdataLabel(FuncName));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001064 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1065
1066 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001067 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001068 DAG.getEntryNode(),
1069 Op, ES,
1070 DAG.getConstant (1, MVT::i8), // Banksel.
Sanjiv Guptaa3613be2009-04-10 15:10:14 +00001071 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001072 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001073
1074 // Load the value from ES.
1075 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001076 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001077 ES, DAG.getConstant (1, MVT::i8),
Sanjiv Guptaa3613be2009-04-10 15:10:14 +00001078 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001079 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001080
1081 return Load.getValue(0);
1082}
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001083
1084SDValue PIC16TargetLowering::
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085LowerIndirectCallArguments(SDValue Chain, SDValue InFlag,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001086 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087 const SmallVectorImpl<ISD::OutputArg> &Outs,
1088 DebugLoc dl, SelectionDAG &DAG) {
1089 unsigned NumOps = Outs.size();
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001090
1091 // If call has no arguments then do nothing and return.
1092 if (NumOps == 0)
1093 return Chain;
1094
1095 std::vector<SDValue> Ops;
1096 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
1097 SDValue Arg, StoreRet;
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001098
1099 // For PIC16 ABI the arguments come after the return value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001100 unsigned RetVals = Outs.size();
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001101 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001102 // Get the arguments
Dan Gohman98ca4f22009-08-05 01:29:28 +00001103 Arg = Outs[i].Val;
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001104
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001105 Ops.clear();
1106 Ops.push_back(Chain);
1107 Ops.push_back(Arg);
1108 Ops.push_back(DataAddr_Lo);
1109 Ops.push_back(DataAddr_Hi);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001110 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001111 Ops.push_back(InFlag);
1112
1113 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
1114
1115 Chain = getChain(StoreRet);
1116 InFlag = getOutFlag(StoreRet);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +00001117 ArgOffset++;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001118 }
1119 return Chain;
1120}
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001121
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001122SDValue PIC16TargetLowering::
Dan Gohman98ca4f22009-08-05 01:29:28 +00001123LowerDirectCallArguments(SDValue ArgLabel, SDValue Chain, SDValue InFlag,
1124 const SmallVectorImpl<ISD::OutputArg> &Outs,
1125 DebugLoc dl, SelectionDAG &DAG) {
1126 unsigned NumOps = Outs.size();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001127 std::string Name;
1128 SDValue Arg, StoreAt;
1129 MVT ArgVT;
1130 unsigned Size=0;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001131
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001132 // If call has no arguments then do nothing and return.
1133 if (NumOps == 0)
1134 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001135
1136 // FIXME: This portion of code currently assumes only
1137 // primitive types being passed as arguments.
1138
1139 // Legalize the address before use
1140 SDValue PtrLo, PtrHi;
1141 unsigned AddressOffset;
1142 int StoreOffset = 0;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001143 LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001144 SDValue StoreRet;
1145
1146 std::vector<SDValue> Ops;
1147 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001148 for (unsigned i=0, Offset = 0; i<NumOps; i++) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001149 // Get the argument
Dan Gohman98ca4f22009-08-05 01:29:28 +00001150 Arg = Outs[i].Val;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001151 StoreOffset = (Offset + AddressOffset);
1152
1153 // Store the argument on frame
1154
1155 Ops.clear();
1156 Ops.push_back(Chain);
Sanjiv Gupta12f23a82009-04-13 09:38:38 +00001157 Ops.push_back(Arg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001158 Ops.push_back(PtrLo);
1159 Ops.push_back(PtrHi);
1160 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
1161 Ops.push_back(InFlag);
1162
Dale Johannesene8d72302009-02-06 23:05:02 +00001163 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001164
1165 Chain = getChain(StoreRet);
1166 InFlag = getOutFlag(StoreRet);
1167
1168 // Update the frame offset to be used for next argument
1169 ArgVT = Arg.getValueType();
1170 Size = ArgVT.getSizeInBits();
1171 Size = Size/8; // Calculate size in bytes
1172 Offset += Size; // Increase the frame offset
1173 }
1174 return Chain;
1175}
1176
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001177SDValue PIC16TargetLowering::
Dan Gohman98ca4f22009-08-05 01:29:28 +00001178LowerIndirectCallReturn(SDValue Chain, SDValue InFlag,
1179 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1180 const SmallVectorImpl<ISD::InputArg> &Ins,
1181 DebugLoc dl, SelectionDAG &DAG,
1182 SmallVectorImpl<SDValue> &InVals) {
1183 unsigned RetVals = Ins.size();
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001184
1185 // If call does not have anything to return
1186 // then do nothing and go back.
1187 if (RetVals == 0)
1188 return Chain;
1189
1190 // Call has something to return
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001191 SDValue LoadRet;
1192
1193 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1194 for(unsigned i=0;i<RetVals;i++) {
1195 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
1196 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
1197 InFlag);
1198 InFlag = getOutFlag(LoadRet);
1199 Chain = getChain(LoadRet);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001200 InVals.push_back(LoadRet);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001201 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 return Chain;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001203}
1204
1205SDValue PIC16TargetLowering::
Dan Gohman98ca4f22009-08-05 01:29:28 +00001206LowerDirectCallReturn(SDValue RetLabel, SDValue Chain, SDValue InFlag,
1207 const SmallVectorImpl<ISD::InputArg> &Ins,
1208 DebugLoc dl, SelectionDAG &DAG,
1209 SmallVectorImpl<SDValue> &InVals) {
1210
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001211 // Currently handling primitive types only. They will come in
1212 // i8 parts
Dan Gohman98ca4f22009-08-05 01:29:28 +00001213 unsigned RetVals = Ins.size();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001214
1215 // Return immediately if the return type is void
1216 if (RetVals == 0)
1217 return Chain;
1218
1219 // Call has something to return
1220
1221 // Legalize the address before use
1222 SDValue LdLo, LdHi;
1223 unsigned LdOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001224 LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001225
1226 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1227 SDValue LoadRet;
1228
1229 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1230
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001231 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001232 DAG.getConstant(LdOffset + Offset, MVT::i8),
1233 InFlag);
1234
1235 InFlag = getOutFlag(LoadRet);
1236
1237 Chain = getChain(LoadRet);
1238 Offset++;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001239 InVals.push_back(LoadRet);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001240 }
1241
Dan Gohman98ca4f22009-08-05 01:29:28 +00001242 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001243}
1244
Dan Gohman98ca4f22009-08-05 01:29:28 +00001245SDValue
1246PIC16TargetLowering::LowerReturn(SDValue Chain,
1247 unsigned CallConv, bool isVarArg,
1248 const SmallVectorImpl<ISD::OutputArg> &Outs,
1249 DebugLoc dl, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001250
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001251 // Number of values to return
Dan Gohman98ca4f22009-08-05 01:29:28 +00001252 unsigned NumRet = Outs.size();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001253
1254 // Function returns value always on stack with the offset starting
1255 // from 0
1256 MachineFunction &MF = DAG.getMachineFunction();
1257 const Function *F = MF.getFunction();
1258 std::string FuncName = F->getName();
1259
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001260 const char *tmpName = createESName(PAN::getFrameLabel(FuncName));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001261 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1262 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1263 SDValue BS = DAG.getConstant(1, MVT::i8);
1264 SDValue RetVal;
1265 for(unsigned i=0;i<NumRet; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001266 RetVal = Outs[i].Val;
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001267 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1268 ES, BS,
1269 DAG.getConstant (i, MVT::i8));
1270
1271 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001272 return DAG.getNode(PIC16ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001273}
1274
1275void PIC16TargetLowering::
1276GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1277 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1278 SelectionDAG &DAG) {
1279 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1280 && "Don't know what to do of such callee!!");
1281 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1282 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1283 Chain = getChain(SeqStart);
1284 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1285
1286 // Get the Lo and Hi part of code address
1287 SDValue Lo = Callee.getOperand(0);
1288 SDValue Hi = Callee.getOperand(1);
1289
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001290 SDValue Data_Lo, Data_Hi;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001291 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001292 // Subtract 2 from Address to get the Lower part of DataAddress.
1293 SDVTList VTList = DAG.getVTList(MVT::i8, MVT::Flag);
1294 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1295 DAG.getConstant(2, MVT::i8));
1296 SDValue Ops[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1297 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, Ops, 3);
1298 SDValue PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1299 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001300 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1301 OperFlag);
1302 Chain = getChain(Call);
1303 OperFlag = getOutFlag(Call);
1304 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1305 OperFlag);
1306 Chain = getChain(SeqEnd);
1307 OperFlag = getOutFlag(SeqEnd);
1308
1309 // Low part of Data Address
1310 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1311
1312 // Make the second call.
1313 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1314 Chain = getChain(SeqStart);
1315 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1316
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001317 // Subtract 1 from Address to get high part of data address.
1318 Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
1319 DAG.getConstant(1, MVT::i8));
1320 SDValue HiOps[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
1321 Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1322 PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
1323
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001324 // Use new Lo to make another CALLW
Sanjiv Gupta09560f82009-05-09 05:11:19 +00001325 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001326 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1327 Chain = getChain(Call);
1328 OperFlag = getOutFlag(Call);
1329 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1330 OperFlag);
1331 Chain = getChain(SeqEnd);
1332 OperFlag = getOutFlag(SeqEnd);
1333 // Hi part of Data Address
1334 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1335}
1336
Dan Gohman98ca4f22009-08-05 01:29:28 +00001337SDValue
1338PIC16TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
1339 unsigned CallConv, bool isVarArg,
1340 bool isTailCall,
1341 const SmallVectorImpl<ISD::OutputArg> &Outs,
1342 const SmallVectorImpl<ISD::InputArg> &Ins,
1343 DebugLoc dl, SelectionDAG &DAG,
1344 SmallVectorImpl<SDValue> &InVals) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001345
Dan Gohman98ca4f22009-08-05 01:29:28 +00001346 assert(Callee.getValueType() == MVT::i16 &&
1347 "Don't know how to legalize this call node!!!");
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001348
1349 // The flag to track if this is a direct or indirect call.
1350 bool IsDirectCall = true;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001351 unsigned RetVals = Ins.size();
1352 unsigned NumArgs = Outs.size();
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001353
1354 SDValue DataAddr_Lo, DataAddr_Hi;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001355 if (!isa<GlobalAddressSDNode>(Callee) &&
1356 !isa<ExternalSymbolSDNode>(Callee)) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001357 IsDirectCall = false; // This is indirect call
Dan Gohman98ca4f22009-08-05 01:29:28 +00001358
1359 // If this is an indirect call then to pass the arguments
1360 // and read the return value back, we need the data address
1361 // of the function being called.
1362 // To get the data address two more calls need to be made.
1363
1364 // Come here for indirect calls
1365 SDValue Lo, Hi;
1366 // Indirect addresses. Get the hi and lo parts of ptr.
1367 GetExpandedParts(Callee, DAG, Lo, Hi);
1368 // Connect Lo and Hi parts of the callee with the PIC16Connect
1369 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1370
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001371 // Read DataAddress only if we have to pass arguments or
1372 // read return value.
1373 if ((RetVals > 0) || (NumArgs > 0))
1374 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1375 }
1376
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001377 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1378
1379 // Start the call sequence.
1380 // Carring the Constant 0 along the CALLSEQSTART
1381 // because there is nothing else to carry.
1382 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1383 Chain = getChain(SeqStart);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001384 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1385 std::string Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001386
1387 // For any direct call - callee will be GlobalAddressNode or
1388 // ExternalSymbol
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001389 SDValue ArgLabel, RetLabel;
1390 if (IsDirectCall) {
1391 // Considering the GlobalAddressNode case here.
1392 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1393 GlobalValue *GV = G->getGlobal();
1394 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1395 Name = G->getGlobal()->getName();
1396 } else {// Considering the ExternalSymbol case here
1397 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1398 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1399 Name = ES->getSymbol();
1400 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001401
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001402 // Label for argument passing
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001403 const char *argFrame = createESName(PAN::getArgsLabel(Name));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001404 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001405
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001406 // Label for reading return value
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001407 const char *retName = createESName(PAN::getRetvalLabel(Name));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001408 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1409 } else {
1410 // if indirect call
1411 SDValue CodeAddr_Lo = Callee.getOperand(0);
1412 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001413
Sanjiv Guptadd92dba2009-04-22 12:02:36 +00001414 /*CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1415 DAG.getConstant(2, MVT::i8));*/
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001416
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001417 // move Hi part in PCLATH
1418 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1419 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1420 CodeAddr_Hi);
1421 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001422
1423 // Pass the argument to function before making the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001424 SDValue CallArgs;
1425 if (IsDirectCall) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001426 CallArgs = LowerDirectCallArguments(ArgLabel, Chain, OperFlag,
1427 Outs, dl, DAG);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001428 Chain = getChain(CallArgs);
1429 OperFlag = getOutFlag(CallArgs);
1430 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001431 CallArgs = LowerIndirectCallArguments(Chain, OperFlag, DataAddr_Lo,
1432 DataAddr_Hi, Outs, dl, DAG);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001433 Chain = getChain(CallArgs);
1434 OperFlag = getOutFlag(CallArgs);
1435 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001436
1437 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001438 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001439 OperFlag);
1440 Chain = getChain(PICCall);
1441 OperFlag = getOutFlag(PICCall);
1442
1443
1444 // Carrying the Constant 0 along the CALLSEQSTART
1445 // because there is nothing else to carry.
1446 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1447 OperFlag);
1448 Chain = getChain(SeqEnd);
1449 OperFlag = getOutFlag(SeqEnd);
1450
1451 // Lower the return value reading after the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001452 if (IsDirectCall)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001453 return LowerDirectCallReturn(RetLabel, Chain, OperFlag,
1454 Ins, dl, DAG, InVals);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001455 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001456 return LowerIndirectCallReturn(Chain, OperFlag, DataAddr_Lo,
1457 DataAddr_Hi, Ins, dl, DAG, InVals);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001458}
1459
1460bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1461 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1462 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1463 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1464 return true;
1465 return false;
1466}
1467
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001468// NeedToConvertToMemOp - Returns true if one of the operands of the
1469// operation 'Op' needs to be put into memory. Also returns the
1470// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1471// no instruction that can operation on two registers. Most insns take
1472// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001473bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001474 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001475 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1476 Op.getOperand(1).getOpcode() == ISD::Constant)
1477 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001478
1479 // Return false if one of the operands is already a direct
1480 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001481 if (isDirectLoad(Op.getOperand(0))) {
1482 if (Op.getOperand(0).hasOneUse())
1483 return false;
1484 else
1485 MemOp = 0;
1486 }
1487 if (isDirectLoad(Op.getOperand(1))) {
1488 if (Op.getOperand(1).hasOneUse())
1489 return false;
1490 else
1491 MemOp = 1;
1492 }
1493 return true;
1494}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001495
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001496// LowerBinOp - Lower a commutative binary operation that does not
1497// affect status flag carry.
1498SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001499 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001500
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001501 // We should have handled larger operands in type legalizer itself.
1502 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001503
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001504 unsigned MemOp = 1;
1505 if (NeedToConvertToMemOp(Op, MemOp)) {
1506 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001507 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001508
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001509 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001510 NewVal);
1511 }
1512 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001513 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001514 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001515}
1516
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001517// LowerADD - Lower all types of ADD operations including the ones
1518// that affects carry.
1519SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001520 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001521 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001522 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001523 unsigned MemOp = 1;
1524 if (NeedToConvertToMemOp(Op, MemOp)) {
1525 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001526 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001527
Dale Johannesen83138992009-06-01 23:13:42 +00001528 // ADDC and ADDE produce two results.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001529 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001530
Dale Johannesen83138992009-06-01 23:13:42 +00001531 // ADDE has three operands, the last one is the carry bit.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001532 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001533 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1534 NewVal, Op.getOperand(2));
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001535 // ADDC has two operands.
1536 else if (Op.getOpcode() == ISD::ADDC)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001537 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1538 NewVal);
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001539 // ADD it is. It produces only one result.
1540 else
1541 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1542 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001543 }
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001544 else
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001545 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001546}
1547
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001548SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001549 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001550 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001551 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001552
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001553 // Nothing to do if the first operand is already a direct load and it has
1554 // only one use.
1555 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptadf75a272009-05-28 06:59:57 +00001556 return Op;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001557
1558 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001559 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001560
1561 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptabf36caf2009-07-27 02:26:06 +00001562 switch (Op.getOpcode()) {
Daniel Dunbar7145e9b2009-08-04 16:46:12 +00001563 default:
1564 assert (0 && "Opcode unknown.");
Sanjiv Guptabf36caf2009-07-27 02:26:06 +00001565 case ISD::SUBE:
1566 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
1567 Op.getOperand(2));
1568 break;
1569 case ISD::SUBC:
1570 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
1571 break;
1572 case ISD::SUB:
1573 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, NewVal, Op.getOperand(1));
1574 break;
Sanjiv Guptabf36caf2009-07-27 02:26:06 +00001575 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001576}
1577
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001578void PIC16TargetLowering::InitReservedFrameCount(const Function *F) {
1579 unsigned NumArgs = F->arg_size();
1580
1581 bool isVoidFunc = (F->getReturnType()->getTypeID() == Type::VoidTyID);
1582
1583 if (isVoidFunc)
1584 ReservedFrameCount = NumArgs;
1585 else
1586 ReservedFrameCount = NumArgs + 1;
1587}
1588
Dan Gohman98ca4f22009-08-05 01:29:28 +00001589// LowerFormalArguments - Argument values are loaded from the
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001590// <fname>.args + offset. All arguments are already broken to leaglized
1591// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001592
Dan Gohman98ca4f22009-08-05 01:29:28 +00001593SDValue
1594PIC16TargetLowering::LowerFormalArguments(SDValue Chain,
1595 unsigned CallConv,
1596 bool isVarArg,
1597 const SmallVectorImpl<ISD::InputArg> &Ins,
1598 DebugLoc dl,
1599 SelectionDAG &DAG,
1600 SmallVectorImpl<SDValue> &InVals) {
1601 unsigned NumArgVals = Ins.size();
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001602
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001603 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001604 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001605 const Function *F = MF.getFunction();
1606 std::string FuncName = F->getName();
1607
Sanjiv Guptae16178b2009-04-21 05:54:51 +00001608 // Reset the map of FI and TmpOffset
1609 ResetTmpOffsetMap();
1610 // Initialize the ReserveFrameCount
1611 InitReservedFrameCount(F);
1612
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001613 // Create the <fname>.args external symbol.
Sanjiv Gupta211f3622009-05-10 05:23:47 +00001614 const char *tmpName = createESName(PAN::getArgsLabel(FuncName));
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001615 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001616
1617 // Load arg values from the label + offset.
1618 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001619 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001620 for (unsigned i = 0; i < NumArgVals ; ++i) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001621 SDValue Offset = DAG.getConstant(i, MVT::i8);
1622 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001623 Offset);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001624 Chain = getChain(PICLoad);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 InVals.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001626 }
1627
Dan Gohman98ca4f22009-08-05 01:29:28 +00001628 return Chain;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001629}
1630
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001631// Perform DAGCombine of PIC16Load.
1632// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001633SDValue PIC16TargetLowering::
1634PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1635 SelectionDAG &DAG = DCI.DAG;
1636 SDValue Chain = N->getOperand(0);
1637 if (N->hasNUsesOfValue(0, 0)) {
1638 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1639 }
1640 return SDValue();
1641}
1642
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001643// For all the functions with arguments some STORE nodes are generated
1644// that store the argument on the frameindex. However in PIC16 the arguments
1645// are passed on stack only. Therefore these STORE nodes are redundant.
1646// To remove these STORE nodes will be removed in PerformStoreCombine
1647//
1648// Currently this function is doint nothing and will be updated for removing
1649// unwanted store operations
1650SDValue PIC16TargetLowering::
1651PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001652 return SDValue(N, 0);
1653 /*
1654 // Storing an undef value is of no use, so remove it
1655 if (isStoringUndef(N, Chain, DAG)) {
1656 return Chain; // remove the store and return the chain
1657 }
1658 //else everything is ok.
1659 return SDValue(N, 0);
1660 */
1661}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001662
1663SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1664 DAGCombinerInfo &DCI) const {
1665 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001666 case ISD::STORE:
1667 return PerformStoreCombine(N, DCI);
1668 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001669 return PerformPIC16LoadCombine(N, DCI);
1670 }
1671 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001672}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001673
1674static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1675 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001676 default: llvm_unreachable("Unknown condition code!");
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001677 case ISD::SETNE: return PIC16CC::NE;
1678 case ISD::SETEQ: return PIC16CC::EQ;
1679 case ISD::SETGT: return PIC16CC::GT;
1680 case ISD::SETGE: return PIC16CC::GE;
1681 case ISD::SETLT: return PIC16CC::LT;
1682 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001683 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta703e2352009-06-03 13:36:44 +00001684 case ISD::SETULE: return PIC16CC::ULE;
1685 case ISD::SETUGE: return PIC16CC::UGE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001686 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001687 }
1688}
1689
1690// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1691// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1692static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1693 ISD::CondCode CC, unsigned &SPCC) {
1694 if (isa<ConstantSDNode>(RHS) &&
1695 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1696 CC == ISD::SETNE &&
1697 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1698 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1699 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1700 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1701 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1702 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1703 SDValue CMPCC = LHS.getOperand(3);
1704 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1705 LHS = CMPCC.getOperand(0);
1706 RHS = CMPCC.getOperand(1);
1707 }
1708}
1709
1710// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1711SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1712 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001713 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001714 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1715
1716 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1717 // i.e. a < 12 can be rewritten as 12 > a.
1718 if (RHS.getOpcode() == ISD::Constant) {
1719
1720 SDValue Tmp = LHS;
1721 LHS = RHS;
1722 RHS = Tmp;
1723
1724 switch (CondCode) {
1725 default: break;
1726 case PIC16CC::LT:
1727 CondCode = PIC16CC::GT;
1728 break;
1729 case PIC16CC::GT:
1730 CondCode = PIC16CC::LT;
1731 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001732 case PIC16CC::ULT:
1733 CondCode = PIC16CC::UGT;
1734 break;
1735 case PIC16CC::UGT:
1736 CondCode = PIC16CC::ULT;
1737 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001738 case PIC16CC::GE:
1739 CondCode = PIC16CC::LE;
1740 break;
1741 case PIC16CC::LE:
1742 CondCode = PIC16CC::GE;
1743 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001744 case PIC16CC::ULE:
1745 CondCode = PIC16CC::UGE;
1746 break;
1747 case PIC16CC::UGE:
1748 CondCode = PIC16CC::ULE;
1749 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001750 }
1751 }
1752
1753 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001754
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001755 // These are signed comparisons.
1756 SDValue Mask = DAG.getConstant(128, MVT::i8);
1757 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001758 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1759 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001760 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001761
1762 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001763 // We can use a subtract operation to set the condition codes. But
1764 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001765 // Nothing to do if the first operand is already a valid type (direct load
1766 // for subwf and literal for sublw) and it is used by this operation only.
1767 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1768 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001769 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001770
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001771 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001772 LHS = ConvertToMemOperand (LHS, DAG, dl);
1773 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001774}
1775
1776
1777SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1778 SDValue LHS = Op.getOperand(0);
1779 SDValue RHS = Op.getOperand(1);
1780 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1781 SDValue TrueVal = Op.getOperand(2);
1782 SDValue FalseVal = Op.getOperand(3);
1783 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001784 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001785
1786 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1787 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1788 // i.e.
1789 // A setcc: lhs, rhs, cc is expanded by llvm to
1790 // select_cc: result of setcc, 0, 1, 0, setne
1791 // We can think of it as:
1792 // select_cc: lhs, rhs, 1, 0, cc
1793 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1794 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1795
1796 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001797 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001798
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001799 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001800 FalseVal, PIC16CC, Cmp.getValue(1));
1801}
1802
1803MachineBasicBlock *
1804PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001805 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001806 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1807 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001808 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001809
1810 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1811 // control-flow pattern. The incoming instruction knows the destination vreg
1812 // to set, the condition code register to branch on, the true/false values to
1813 // select between, and a branch opcode to use.
1814 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1815 MachineFunction::iterator It = BB;
1816 ++It;
1817
1818 // thisMBB:
1819 // ...
1820 // TrueVal = ...
1821 // [f]bCC copy1MBB
1822 // fallthrough --> copy0MBB
1823 MachineBasicBlock *thisMBB = BB;
1824 MachineFunction *F = BB->getParent();
1825 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1826 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001827 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001828 F->insert(It, copy0MBB);
1829 F->insert(It, sinkMBB);
1830
1831 // Update machine-CFG edges by transferring all successors of the current
1832 // block to the new block which will contain the Phi node for the select.
1833 sinkMBB->transferSuccessors(BB);
1834 // Next, add the true and fallthrough blocks as its successors.
1835 BB->addSuccessor(copy0MBB);
1836 BB->addSuccessor(sinkMBB);
1837
1838 // copy0MBB:
1839 // %FalseValue = ...
1840 // # fallthrough to sinkMBB
1841 BB = copy0MBB;
1842
1843 // Update machine-CFG edges
1844 BB->addSuccessor(sinkMBB);
1845
1846 // sinkMBB:
1847 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1848 // ...
1849 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001850 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001851 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1852 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1853
1854 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1855 return BB;
1856}
1857
1858
1859SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1860 SDValue Chain = Op.getOperand(0);
1861 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1862 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1863 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1864 SDValue Dest = Op.getOperand(4); // BB to jump to
1865 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001866 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001867
1868 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1869 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1870 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1871 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1872
1873 // Get the Compare insn and condition code.
1874 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001875 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001876
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001877 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001878 Cmp.getValue(1));
1879}
1880