blob: 0635c354953ba48ba2af90529eb66baa094bd1eb [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001//
2// The LLVM Compiler Infrastructure
3//
4// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the interfaces that PIC16 uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "pic16-lower"
15
16#include "PIC16ISelLowering.h"
17#include "PIC16TargetMachine.h"
18#include "llvm/DerivedTypes.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000019#include "llvm/GlobalValue.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000020#include "llvm/Function.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000021#include "llvm/CallingConv.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
Duncan Sandsc6dbe7f2008-11-28 10:20:03 +000026#include <cstdio>
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000027
28
Sanjiv Gupta0e687712008-05-13 09:02:57 +000029using namespace llvm;
30
Sanjiv Gupta0e687712008-05-13 09:02:57 +000031
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000032// PIC16TargetLowering Constructor.
33PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
34 : TargetLowering(TM) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000035
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000036 Subtarget = &TM.getSubtarget<PIC16Subtarget>();
Sanjiv Gupta0e687712008-05-13 09:02:57 +000037
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000038 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000039
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000040 setShiftAmountType(MVT::i8);
41 setShiftAmountFlavor(Extend);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000042
Sanjiv Gupta1b046942009-01-13 19:18:47 +000043 // SRA library call names
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000044 setPIC16LibcallName(PIC16ISD::SRA_I8, "__intrinsics.sra.i8");
45 setLibcallName(RTLIB::SRA_I16, "__intrinsics.sra.i16");
46 setLibcallName(RTLIB::SRA_I32, "__intrinsics.sra.i32");
Sanjiv Gupta1b046942009-01-13 19:18:47 +000047
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000048 // SHL library call names
49 setPIC16LibcallName(PIC16ISD::SLL_I8, "__intrinsics.sll.i8");
50 setLibcallName(RTLIB::SHL_I16, "__intrinsics.sll.i16");
51 setLibcallName(RTLIB::SHL_I32, "__intrinsics.sll.i32");
Sanjiv Gupta1b046942009-01-13 19:18:47 +000052
53 // SRL library call names
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000054 setPIC16LibcallName(PIC16ISD::SRL_I8, "__intrinsics.srl.i8");
55 setLibcallName(RTLIB::SRL_I16, "__intrinsics.srl.i16");
56 setLibcallName(RTLIB::SRL_I32, "__intrinsics.srl.i32");
57
58 // MUL Library call names
59 setPIC16LibcallName(PIC16ISD::MUL_I8, "__intrinsics.mul.i8");
60 setLibcallName(RTLIB::MUL_I16, "__intrinsics.mul.i16");
61 setLibcallName(RTLIB::MUL_I32, "__intrinsics.mul.i32");
Sanjiv Gupta0e687712008-05-13 09:02:57 +000062
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000063 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000064 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000065
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000066 setOperationAction(ISD::LOAD, MVT::i8, Legal);
67 setOperationAction(ISD::LOAD, MVT::i16, Custom);
68 setOperationAction(ISD::LOAD, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000069
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000070 setOperationAction(ISD::STORE, MVT::i8, Legal);
71 setOperationAction(ISD::STORE, MVT::i16, Custom);
72 setOperationAction(ISD::STORE, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000073
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000074 setOperationAction(ISD::ADDE, MVT::i8, Custom);
75 setOperationAction(ISD::ADDC, MVT::i8, Custom);
76 setOperationAction(ISD::SUBE, MVT::i8, Custom);
77 setOperationAction(ISD::SUBC, MVT::i8, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000078 setOperationAction(ISD::ADD, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000079 setOperationAction(ISD::ADD, MVT::i16, Custom);
80
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +000081 setOperationAction(ISD::OR, MVT::i8, Custom);
82 setOperationAction(ISD::AND, MVT::i8, Custom);
83 setOperationAction(ISD::XOR, MVT::i8, Custom);
84
Sanjiv Gupta1b046942009-01-13 19:18:47 +000085 setOperationAction(ISD::FrameIndex, MVT::i16, Custom);
86 setOperationAction(ISD::CALL, MVT::i16, Custom);
87 setOperationAction(ISD::RET, MVT::Other, Custom);
88
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000089 setOperationAction(ISD::MUL, MVT::i8, Custom);
90 setOperationAction(ISD::MUL, MVT::i16, Expand);
91 setOperationAction(ISD::MUL, MVT::i32, Expand);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000092
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000093 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
94 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
95 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
96 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
97 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
98 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
99 setOperationAction(ISD::MULHU, MVT::i8, Expand);
100 setOperationAction(ISD::MULHU, MVT::i16, Expand);
101 setOperationAction(ISD::MULHU, MVT::i32, Expand);
102 setOperationAction(ISD::MULHS, MVT::i8, Expand);
103 setOperationAction(ISD::MULHS, MVT::i16, Expand);
104 setOperationAction(ISD::MULHS, MVT::i32, Expand);
105
106 setOperationAction(ISD::SRA, MVT::i8, Custom);
107 setOperationAction(ISD::SRA, MVT::i16, Expand);
108 setOperationAction(ISD::SRA, MVT::i32, Expand);
109 setOperationAction(ISD::SHL, MVT::i8, Custom);
110 setOperationAction(ISD::SHL, MVT::i16, Expand);
111 setOperationAction(ISD::SHL, MVT::i32, Expand);
112 setOperationAction(ISD::SRL, MVT::i8, Custom);
113 setOperationAction(ISD::SRL, MVT::i16, Expand);
114 setOperationAction(ISD::SRL, MVT::i32, Expand);
115
116 // PIC16 does not support shift parts
117 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
118 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
119 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
120 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
121 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
122 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
123 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
124 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
125 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
126
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000127
128 // PIC16 does not have a SETCC, expand it to SELECT_CC.
129 setOperationAction(ISD::SETCC, MVT::i8, Expand);
130 setOperationAction(ISD::SELECT, MVT::i8, Expand);
131 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
132 setOperationAction(ISD::BRIND, MVT::Other, Expand);
133
134 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
135 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000136
137 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
138 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
139
140 // Now deduce the information based on the above mentioned
141 // actions
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000142 computeRegisterProperties();
143}
144
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000145static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
146 if (N.getOpcode() == ISD::MERGE_VALUES) {
147 int NumResults = N.getNumOperands();
148 for( int i = 0; i < NumResults; i++)
149 Results.push_back(N.getOperand(i));
150 }
151 else
152 Results.push_back(N);
153}
154
155MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
156 return MVT::i8;
157}
158
159
160void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000161PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000162 const char *Name) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000163 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000164}
165
166const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000167PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
168 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000169}
170
171SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000172PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000173 MVT RetVT, const SDValue *Ops,
174 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000175 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000176
177 TargetLowering::ArgListTy Args;
178 Args.reserve(NumOps);
179
180 TargetLowering::ArgListEntry Entry;
181 for (unsigned i = 0; i != NumOps; ++i) {
182 Entry.Node = Ops[i];
183 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
184 Entry.isSExt = isSigned;
185 Entry.isZExt = !isSigned;
186 Args.push_back(Entry);
187 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000188 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000189
190 const Type *RetTy = RetVT.getTypeForMVT();
191 std::pair<SDValue,SDValue> CallInfo =
192 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000193 false, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000194
195 return CallInfo.first;
196}
197
198SDValue
199PIC16TargetLowering::getCurrentFrame(SelectionDAG &DAG) {
200 MachineFunction &MF = DAG.getMachineFunction();
201 const Function *Func = MF.getFunction();
202 const std::string FuncName = Func->getName();
203
204 // this is causing memory waste
205 // because for every call new memory will be allocated
206 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
207 sprintf(tmpName, "%s.tmp", FuncName.c_str());
208
209 // if the external symbol of the same name already exists then
210 // it will not create the new one.
211 return DAG.getTargetExternalSymbol(tmpName, MVT::i8);
212}
213
214void
215PIC16TargetLowering::getCurrentFrameIndex(SelectionDAG &DAG, SDValue &ES,
216 unsigned SlotSize, int &FromFI) {
217 MachineFunction &MF = DAG.getMachineFunction();
218 const Function *Func = MF.getFunction();
219 const std::string FuncName = Func->getName();
220
221 // this is causing memory waste
222 // because for every call new memory will be allocated
223 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
224 sprintf(tmpName, "%s.tmp", FuncName.c_str());
225
226 // if the external symbol of the same name already exists then
227 // it will not create the new one.
228 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
229
230 // Alignment is always 1
231 //FromFI = MF.getFrameInfo()->CreateStackObject(SlotSize, 1);
232 FromFI = MF.getFrameInfo()->CreateStackObject(1, 1);
233 int FI;
234 for(unsigned i=1;i<SlotSize; ++i) {
235 FI = MF.getFrameInfo()->CreateStackObject(1, 1);
236 }
237}
238
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000239const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
240 switch (Opcode) {
241 default: return NULL;
242 case PIC16ISD::Lo: return "PIC16ISD::Lo";
243 case PIC16ISD::Hi: return "PIC16ISD::Hi";
244 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
245 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
246 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
247 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000248 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000249 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000250 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000251 case PIC16ISD::BCF: return "PIC16ISD::BCF";
252 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
253 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
254 case PIC16ISD::RLF: return "PIC16ISD::RLF";
255 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000256 case PIC16ISD::CALL: return "PIC16ISD::CALL";
257 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
258 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
259 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000260 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000261 }
262}
263
Duncan Sands1607f052008-12-01 11:39:25 +0000264void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
265 SmallVectorImpl<SDValue>&Results,
266 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000267
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000268 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000269 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000270 Results.push_back(ExpandGlobalAddress(N, DAG));
271 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000272 case ISD::ExternalSymbol:
273 Results.push_back(ExpandExternalSymbol(N, DAG));
274 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000275 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000276 Results.push_back(ExpandStore(N, DAG));
277 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000278 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000279 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000280 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000281 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000282 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000283 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000284 case ISD::FrameIndex:
285 Results.push_back(ExpandFrameIndex(N, DAG));
286 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000287 default:
288 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000289 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000290 }
291}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000292
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000293SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
294
295 // Currently handling FrameIndex of size MVT::i16 only
296 // One example of this scenario is when return value is written on
297 // FrameIndex#0
298
299 if (N->getValueType(0) != MVT::i16)
300 return SDValue();
301
302 // Expand the FrameIndex into ExternalSymbol and a Constant node
303 // The constant will represent the frame index number
304 // Get the current function frame
305 MachineFunction &MF = DAG.getMachineFunction();
306 const Function *Func = MF.getFunction();
307 const std::string Name = Func->getName();
308
309 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
310 int Index = FR->getIndex();
311
312 SDValue FI[2];
313 FI[0] = DAG.getTargetFrameIndex(Index, MVT::i8);
314 FI[1] = DAG.getTargetFrameIndex(Index + 1, MVT::i8);
315 return DAG.getNode(ISD::BUILD_PAIR, N->getValueType(0), FI[0], FI[1]);
316}
317
318
319SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000320 StoreSDNode *St = cast<StoreSDNode>(N);
321 SDValue Chain = St->getChain();
322 SDValue Src = St->getValue();
323 SDValue Ptr = St->getBasePtr();
324 MVT ValueType = Src.getValueType();
325 unsigned StoreOffset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000326
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000327 SDValue PtrLo, PtrHi;
328 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset);
329
330 if (ValueType == MVT::i8) {
Duncan Sands1607f052008-12-01 11:39:25 +0000331 return DAG.getNode (PIC16ISD::PIC16Store, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000332 PtrLo, PtrHi,
333 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000334 }
335 else if (ValueType == MVT::i16) {
336 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
337 SDValue SrcLo, SrcHi;
338 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
339 SDValue ChainLo = Chain, ChainHi = Chain;
340 if (Chain.getOpcode() == ISD::TokenFactor) {
341 ChainLo = Chain.getOperand(0);
342 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000343 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000344 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, MVT::Other,
345 ChainLo,
346 SrcLo, PtrLo, PtrHi,
347 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000348
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000349 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, MVT::Other, ChainHi,
350 SrcHi, PtrLo, PtrHi,
351 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000352
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000353 return DAG.getNode(ISD::TokenFactor, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000354 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000355 }
356 else if (ValueType == MVT::i32) {
357 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
358 SDValue SrcLo, SrcHi;
359 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000360
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000361 // Get the expanded parts of each of SrcLo and SrcHi.
362 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
363 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
364 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000365
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000366 SDValue ChainLo = Chain, ChainHi = Chain;
367 if (Chain.getOpcode() == ISD::TokenFactor) {
368 ChainLo = Chain.getOperand(0);
369 ChainHi = Chain.getOperand(1);
370 }
371 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
372 ChainHi2 = ChainHi;
373 if (ChainLo.getOpcode() == ISD::TokenFactor) {
374 ChainLo1 = ChainLo.getOperand(0);
375 ChainLo2 = ChainLo.getOperand(1);
376 }
377 if (ChainHi.getOpcode() == ISD::TokenFactor) {
378 ChainHi1 = ChainHi.getOperand(0);
379 ChainHi2 = ChainHi.getOperand(1);
380 }
381 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, MVT::Other,
382 ChainLo1,
383 SrcLo1, PtrLo, PtrHi,
384 DAG.getConstant (0 + StoreOffset, MVT::i8));
385
386 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, MVT::Other, ChainLo2,
387 SrcLo2, PtrLo, PtrHi,
388 DAG.getConstant (1 + StoreOffset, MVT::i8));
389
390 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, MVT::Other, ChainHi1,
391 SrcHi1, PtrLo, PtrHi,
392 DAG.getConstant (2 + StoreOffset, MVT::i8));
393
394 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, MVT::Other, ChainHi2,
395 SrcHi2, PtrLo, PtrHi,
396 DAG.getConstant (3 + StoreOffset, MVT::i8));
397
398 SDValue RetLo = DAG.getNode(ISD::TokenFactor, MVT::Other, getChain(Store1),
399 getChain(Store2));
400 SDValue RetHi = DAG.getNode(ISD::TokenFactor, MVT::Other, getChain(Store3),
401 getChain(Store4));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000402 return DAG.getNode(ISD::TokenFactor, MVT::Other, RetLo, RetHi);
403
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000404 }
405 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000406 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000407 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000408 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000409}
410
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000411SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
412{
413 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
414
415 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
416
417 SDValue Lo = DAG.getNode(PIC16ISD::Lo, MVT::i8, TES);
418 SDValue Hi = DAG.getNode(PIC16ISD::Hi, MVT::i8, TES);
419
420 return DAG.getNode(ISD::BUILD_PAIR, MVT::i16, Lo, Hi);
421}
422
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000423// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000424SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000425 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
426
427 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
428 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000429
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000430 SDValue Lo = DAG.getNode(PIC16ISD::Lo, MVT::i8, TGA);
431 SDValue Hi = DAG.getNode(PIC16ISD::Hi, MVT::i8, TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000432
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000433 return DAG.getNode(ISD::BUILD_PAIR, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000434}
435
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000436bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
437 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000438
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000439 if (Op.getOpcode() == ISD::BUILD_PAIR) {
440 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
441 return true;
442 }
443 return false;
444}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000445
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000446// Return true if DirectAddress is in ROM_SPACE
447bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000448
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000449 // RomAddress is a GlobalAddress in ROM_SPACE_
450 // If the Op is not a GlobalAddress return NULL without checking
451 // anything further.
452 if (!isDirectAddress(Op))
453 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000454
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000455 // Its a GlobalAddress.
456 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
457 SDValue TGA = Op.getOperand(0).getOperand(0);
458 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
459 const Type *ValueType = GSDN->getGlobal()->getType();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000460
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000461 if (!isa<PointerType>(ValueType)) {
462 assert(0 && "TGA must be of a PointerType");
463 }
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000464
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000465 int AddrSpace = dyn_cast<PointerType>(ValueType)->getAddressSpace();
466 if (AddrSpace == PIC16ISD::ROM_SPACE)
467 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000468
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000469 // Any other address space return it false
470 return false;
471}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000472
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000473// Extract the out flag
474SDValue PIC16TargetLowering::getOutFlag(SDValue &Op) {
475 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
476
477 assert (Flag.getValueType() == MVT::Flag && "Node does not have an out Flag");
478
479 return Flag;
480}
481
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000482// To extract chain value from the SDValue Nodes
483// This function will help to maintain the chain extracting
484// code at one place. In case of any change in future it will
485// help maintain the code.
486SDValue PIC16TargetLowering::getChain(SDValue &Op) {
487 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000488
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000489 // If the last value returned in Flag then the chain is
490 // second last value returned.
491 if (Chain.getValueType() == MVT::Flag)
492 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
493
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000494 // All nodes may not produce a chain. Therefore following assert
495 // verifies that the node is returning a chain only.
496 assert (Chain.getValueType() == MVT::Other && "Node does not have a chain");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000497
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000498 return Chain;
499}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000500
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000501void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
502 SDValue &Lo, SDValue &Hi) {
503 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000504 DebugLoc dl = N->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000505 MVT NewVT;
506 std::vector<SDValue> Opers;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000507 NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000508
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000509 // extract the lo component
510 Opers.push_back(Op);
511 Opers.push_back(DAG.getConstant(0,MVT::i8));
Dale Johannesened2eee62009-02-06 01:31:28 +0000512 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT,dl,NewVT,&Opers[0],Opers.size());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000513 // extract the hi component
514 Opers.clear();
515 Opers.push_back(Op);
516 Opers.push_back(DAG.getConstant(1,MVT::i8));
Dale Johannesened2eee62009-02-06 01:31:28 +0000517 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT,dl,NewVT,&Opers[0],Opers.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000518}
519
520// Legalize FrameIndex into ExternalSymbol and offset.
521void
522PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
523 SDValue &ES, int &Offset) {
524
525 MachineFunction &MF = DAG.getMachineFunction();
526 const Function *Func = MF.getFunction();
527 const std::string Name = Func->getName();
528
529 char *tmpName = new char [strlen(Name.c_str()) + 8];
530 sprintf(tmpName, "%s.args", Name.c_str());
531 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
532 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
533 Offset = FR->getIndex();
534
535 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000536}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000537
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000538// This function legalizes the PIC16 Addresses. If the Pointer is
539// -- Direct address variable residing
540// --> then a Banksel for that variable will be created.
541// -- Rom variable
542// --> then it will be treated as an indirect address.
543// -- Indirect address
544// --> then the address will be loaded into FSR
545// -- ADD with constant operand
546// --> then constant operand of ADD will be returned as Offset
547// and non-constant operand of ADD will be treated as pointer.
548// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000549
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000550void PIC16TargetLowering:: LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
551 SDValue &Lo, SDValue &Hi,
552 unsigned &Offset) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000553
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000554 // Offset, by default, should be 0
555 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000556
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000557 // If the pointer is ADD with constant,
558 // return the constant value as the offset
559 if (Ptr.getOpcode() == ISD::ADD) {
560 SDValue OperLeft = Ptr.getOperand(0);
561 SDValue OperRight = Ptr.getOperand(1);
562 if (OperLeft.getOpcode() == ISD::Constant) {
563 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
564 Ptr = OperRight;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000565 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000566 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000567 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000568 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000569 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000570
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000571 // If the pointer is Type i8 and an external symbol
572 // then treat it as direct address.
573 // One example for such case is storing and loading
574 // from function frame during a call
575 if (Ptr.getValueType() == MVT::i8) {
576 switch (Ptr.getOpcode()) {
577 case ISD::TargetExternalSymbol:
578 Lo = Ptr;
579 Hi = DAG.getConstant(1, MVT::i8);
580 return;
581 }
582 }
583
584 if (Ptr.getOpcode() == ISD::BUILD_PAIR &&
585 Ptr.getOperand(0).getOpcode() == ISD::TargetFrameIndex) {
586
587 int FrameOffset;
588 LegalizeFrameIndex(Ptr.getOperand(0), DAG, Lo, FrameOffset);
589 Hi = DAG.getConstant(1, MVT::i8);
590 Offset += FrameOffset;
591 return;
592 }
593
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000594 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
595 // Direct addressing case for RAM variables. The Hi part is constant
596 // and the Lo part is the TGA itself.
597 Lo = Ptr.getOperand(0).getOperand(0);
598
599 // For direct addresses Hi is a constant. Value 1 for the constant
600 // signifies that banksel needs to generated for it. Value 0 for
601 // the constant signifies that banksel does not need to be generated
602 // for it. Mark it as 1 now and optimize later.
603 Hi = DAG.getConstant(1, MVT::i8);
604 return;
605 }
606
607 // Indirect addresses. Get the hi and lo parts of ptr.
608 GetExpandedParts(Ptr, DAG, Lo, Hi);
609
610 // Put the hi and lo parts into FSR.
611 Lo = DAG.getNode(PIC16ISD::MTLO, MVT::i8, Lo);
612 Hi = DAG.getNode(PIC16ISD::MTHI, MVT::i8, Hi);
613
614 return;
615}
616
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000617//SDValue PIC16TargetLowering::ExpandAdd(SDNode *N, SelectionDAG &DAG) {
618 //SDValue OperLeft = N->getOperand(0);
619 //SDValue OperRight = N->getOperand(1);
620
621 //if((OperLeft.getOpcode() == ISD::Constant) ||
622 //(OperRight.getOpcode() == ISD::Constant)) {
623 //return SDValue();
624 //}
625
626 // These case are yet to be handled
627 //return SDValue();
Duncan Sands1607f052008-12-01 11:39:25 +0000628//}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000629
Duncan Sands1607f052008-12-01 11:39:25 +0000630SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000631 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
632 SDValue Chain = LD->getChain();
633 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000634 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000635
636 SDValue Load, Offset;
637 SDVTList Tys;
638 MVT VT, NewVT;
639 SDValue PtrLo, PtrHi;
640 unsigned LoadOffset;
641
642 // Legalize direct/indirect addresses. This will give the lo and hi parts
643 // of the address and the offset.
644 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset);
645
646 // Load from the pointer (direct address or FSR)
647 VT = N->getValueType(0);
648 unsigned NumLoads = VT.getSizeInBits() / 8;
649 std::vector<SDValue> PICLoads;
650 unsigned iter;
651 MVT MemVT = LD->getMemoryVT();
652 if(ISD::isNON_EXTLoad(N)) {
653 for (iter=0; iter<NumLoads ; ++iter) {
654 // Add the pointer offset if any
655 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
656 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000657 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000658 Offset);
659 PICLoads.push_back(Load);
660 }
661 } else {
662 // If it is extended load then use PIC16Load for Memory Bytes
663 // and for all extended bytes perform action based on type of
664 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
665
666
667 // For extended loads this is the memory value type
668 // i.e. without any extension
669 MVT MemVT = LD->getMemoryVT();
670 unsigned MemBytes = MemVT.getSizeInBits() / 8;
671 unsigned ExtdBytes = VT.getSizeInBits() / 8;
672 Offset = DAG.getConstant(LoadOffset, MVT::i8);
673
674 Tys = DAG.getVTList(MVT::i8, MVT::Other);
675 // For MemBytes generate PIC16Load with proper offset
676 for (iter=0; iter<MemBytes; ++iter) {
677 // Add the pointer offset if any
678 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000679 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000680 Offset);
681 PICLoads.push_back(Load);
682 }
683
684 // For SignExtendedLoad
685 if (ISD::isSEXTLoad(N)) {
686 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
687 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000688 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000689 DAG.getConstant(7, MVT::i8));
690 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
691 PICLoads.push_back(SRA);
692 }
693 } else if (ISD::isZEXTLoad(N)) {
694 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
695 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
696 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
697 PICLoads.push_back(ConstZero);
698 }
699 }
700 }
701 SDValue BP;
702
703 if (VT == MVT::i8) {
704 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000705 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000706 }
707 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000708 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000709 if (MemVT == MVT::i8)
710 Chain = getChain(PICLoads[0]);
711 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000712 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
713 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000714 } else if (VT == MVT::i32) {
715 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000716 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
717 PICLoads[0], PICLoads[1]);
718 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
719 PICLoads[2], PICLoads[3]);
720 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000721 if (MemVT == MVT::i8)
722 Chain = getChain(PICLoads[0]);
723 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000724 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
725 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000726 else {
727 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000728 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000729 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000730 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000731 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000732 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
733 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000734 }
735 }
736 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000737 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000738}
739
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000740SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
741 // We should have handled larger operands in type legalizer itself.
742 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
743
744 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000745 SDValue Value = N->getOperand(0);
746 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000747 PIC16ISD::PIC16Libcall CallCode;
748 switch (N->getOpcode()) {
749 case ISD::SRA:
750 CallCode = PIC16ISD::SRA_I8;
751 break;
752 case ISD::SHL:
753 CallCode = PIC16ISD::SLL_I8;
754 break;
755 case ISD::SRL:
756 CallCode = PIC16ISD::SRL_I8;
757 break;
758 default:
759 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000760 return SDValue();
761 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000762 SmallVector<SDValue, 2> Ops(2);
763 Ops[0] = Value;
764 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000765 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
766 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000767 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000768}
769
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000770void
771PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
772 SmallVectorImpl<SDValue>&Results,
773 SelectionDAG &DAG) {
774 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000775 SDValue Res;
776 unsigned i;
777 switch (Op.getOpcode()) {
778 case ISD::FORMAL_ARGUMENTS:
779 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
780 case ISD::LOAD:
781 Res = ExpandLoad(Op.getNode(), DAG); break;
782 case ISD::CALL:
783 Res = LowerCALL(Op, DAG); break;
784 default: {
785 // All other operations are handled in LowerOperation.
786 Res = LowerOperation(Op, DAG);
787 if (Res.getNode())
788 Results.push_back(Res);
789
790 return;
791 }
792 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000793
794 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000795 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000796 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000797 Results.push_back(SDValue(N, i));
798 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000799}
800
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000801SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
802 switch (Op.getOpcode()) {
803 case ISD::FORMAL_ARGUMENTS:
804 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000805 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000806 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000807 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000808 return LowerADD(Op, DAG);
809 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000810 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000811 case ISD::SUBE:
812 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000813 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000814 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000815 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000816 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000817 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000818 case ISD::SRA:
819 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000820 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000821 case ISD::OR:
822 case ISD::AND:
823 case ISD::XOR:
824 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000825 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000826 return LowerCALL(Op, DAG);
827 case ISD::RET:
828 return LowerRET(Op, DAG);
829 case ISD::BR_CC:
830 return LowerBR_CC(Op, DAG);
831 case ISD::SELECT_CC:
832 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000833 }
Dan Gohman475871a2008-07-27 21:46:04 +0000834 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000835}
836
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000837SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000838 SelectionDAG &DAG,
839 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000840 assert (Op.getValueType() == MVT::i8
841 && "illegal value type to store on stack.");
842
843 MachineFunction &MF = DAG.getMachineFunction();
844 const Function *Func = MF.getFunction();
845 const std::string FuncName = Func->getName();
846
847 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
848
849 // Put the value on stack.
850 // Get a stack slot index and convert to es.
851 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
852 sprintf(tmpName, "%s.tmp", FuncName.c_str());
853 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
854
855 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000856 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000857 DAG.getEntryNode(),
858 Op, ES,
859 DAG.getConstant (1, MVT::i8), // Banksel.
860 DAG.getConstant (FI, MVT::i8));
861
862 // Load the value from ES.
863 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000864 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000865 ES, DAG.getConstant (1, MVT::i8),
866 DAG.getConstant (FI, MVT::i8));
867
868 return Load.getValue(0);
869}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000870
871SDValue
872PIC16TargetLowering::LowerCallArguments(SDValue Op, SDValue Chain,
873 SDValue FrameAddress,
874 SDValue InFlag,
875 SelectionDAG &DAG) {
876 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
877 unsigned NumOps = TheCall->getNumArgs();
878 std::string Name;
879 SDValue Arg, StoreAt;
880 MVT ArgVT;
881 unsigned Size=0;
882 unsigned ArgCount=0;
883
884
885 // FIXME: This portion of code currently assumes only
886 // primitive types being passed as arguments.
887
888 // Legalize the address before use
889 SDValue PtrLo, PtrHi;
890 unsigned AddressOffset;
891 int StoreOffset = 0;
892 LegalizeAddress(FrameAddress, DAG, PtrLo, PtrHi, AddressOffset);
893 SDValue StoreRet;
894
895 std::vector<SDValue> Ops;
896 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
897 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
898 // Get the argument
899 Arg = TheCall->getArg(i);
900
901 StoreOffset = (Offset + AddressOffset);
902
903 // Store the argument on frame
904
905 Ops.clear();
906 Ops.push_back(Chain);
907 Ops.push_back(Arg.getValue(0));
908 Ops.push_back(PtrLo);
909 Ops.push_back(PtrHi);
910 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
911 Ops.push_back(InFlag);
912
913 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, Tys, &Ops[0], Ops.size());
914
915 Chain = getChain(StoreRet);
916 InFlag = getOutFlag(StoreRet);
917
918 // Update the frame offset to be used for next argument
919 ArgVT = Arg.getValueType();
920 Size = ArgVT.getSizeInBits();
921 Size = Size/8; // Calculate size in bytes
922 Offset += Size; // Increase the frame offset
923 }
924 return Chain;
925}
926
927SDValue
928PIC16TargetLowering::LowerCallReturn(SDValue Op, SDValue Chain,
929 SDValue FrameAddress,
930 SDValue InFlag,
931 SelectionDAG &DAG) {
932 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000933 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000934 // Currently handling primitive types only. They will come in
935 // i8 parts
936 unsigned RetVals = TheCall->getNumRetVals();
937
938 std::vector<SDValue> ResultVals;
939
940 // Return immediately if the return type is void
941 if (RetVals == 0)
942 return Chain;
943
944 // Call has something to return
945
946 // Legalize the address before use
947 SDValue LdLo, LdHi;
948 unsigned LdOffset;
949 LegalizeAddress(FrameAddress, DAG, LdLo, LdHi, LdOffset);
950
951 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
952 SDValue LoadRet;
953
954 for(unsigned i=0, Offset=0;i<RetVals;i++) {
955
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000956 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000957 DAG.getConstant(LdOffset + Offset, MVT::i8),
958 InFlag);
959
960 InFlag = getOutFlag(LoadRet);
961
962 Chain = getChain(LoadRet);
963 Offset++;
964 ResultVals.push_back(LoadRet);
965 }
966
967 // To return use MERGE_VALUES
968 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000969 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000970 return Res;
971}
972
973SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
974 //int NumOps = Op.getNode()->getNumOperands();
975
976 // For default cases LLVM returns the value on the function frame
977 // So let LLVM do this for all the cases other than character
978 return Op;
979}
980
981SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
982 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
983 SDValue Chain = TheCall->getChain();
984 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000985 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000986 unsigned i =0;
987 if (Callee.getValueType() == MVT::i16 &&
988 Callee.getOpcode() == ISD::BUILD_PAIR) {
989 // It has come from TypeLegalizer for lowering
990
991 Callee = Callee.getOperand(0).getOperand(0);
992
993 std::vector<SDValue> Ops;
994 Ops.push_back(Chain);
995 Ops.push_back(Callee);
996
997 // Add the call arguments and their flags
998 unsigned NumArgs = TheCall->getNumArgs();
999 for(i=0;i<NumArgs;i++) {
1000 Ops.push_back(TheCall->getArg(i));
1001 Ops.push_back(TheCall->getArgFlagsVal(i));
1002 }
1003
1004 std::vector<MVT> NodeTys;
1005 unsigned NumRets = TheCall->getNumRetVals();
1006 for(i=0;i<NumRets;i++)
1007 NodeTys.push_back(TheCall->getRetValType(i));
1008
1009 // Return a Chain as well
1010 NodeTys.push_back(MVT::Other);
1011
1012 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1013 SDValue NewCall =
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001014 DAG.getCall(TheCall->getCallingConv(), dl,
Dale Johannesen8ad9b432009-02-04 01:17:06 +00001015 TheCall->isVarArg(), TheCall->isTailCall(),
1016 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001017
1018 return NewCall;
1019 }
1020
1021 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1022
1023 // Start the call sequence.
1024 // Carring the Constant 0 along the CALLSEQSTART
1025 // because there is nothing else to carry.
1026 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1027 Chain = getChain(SeqStart);
1028
1029 // For any direct call - callee will be GlobalAddressNode or
1030 // ExternalSymbol
1031
1032 // Considering the GlobalAddressNode case here.
1033 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1034 GlobalValue *GV = G->getGlobal();
1035 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1036 }
1037
1038 // Considering the ExternalSymbol case here
1039 if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1040 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1041 }
1042
1043 SDValue OperFlag = getOutFlag(Chain); // To manage the data dependency
1044
1045 std::string Name;
1046
1047 // Considering GlobalAddress here
1048 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1049 Name = G->getGlobal()->getName();
1050
1051 // Considering ExternalSymbol here
1052 if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee))
1053 Name = ES->getSymbol();
1054
1055 char *argFrame = new char [strlen(Name.c_str()) + 8];
1056 sprintf(argFrame, "%s.args", Name.c_str());
1057 SDValue ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
1058
1059 char *retName = new char [strlen(Name.c_str()) + 8];
1060 sprintf(retName, "%s.retval", Name.c_str());
1061 SDValue RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1062
1063 // Pass the argument to function before making the call.
1064 SDValue CallArgs = LowerCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1065 Chain = getChain(CallArgs);
1066 OperFlag = getOutFlag(CallArgs);
1067
1068 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001069 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001070 OperFlag);
1071 Chain = getChain(PICCall);
1072 OperFlag = getOutFlag(PICCall);
1073
1074
1075 // Carrying the Constant 0 along the CALLSEQSTART
1076 // because there is nothing else to carry.
1077 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1078 OperFlag);
1079 Chain = getChain(SeqEnd);
1080 OperFlag = getOutFlag(SeqEnd);
1081
1082 // Lower the return value reading after the call.
1083 return LowerCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1084}
1085
1086bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1087 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1088 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1089 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1090 return true;
1091 return false;
1092}
1093
1094bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
1095 // Return false if one of the operands is already a direct
1096 // load and that operand has only one use.
1097 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1098 Op.getOperand(1).getOpcode() == ISD::Constant)
1099 return false;
1100 if (isDirectLoad(Op.getOperand(0))) {
1101 if (Op.getOperand(0).hasOneUse())
1102 return false;
1103 else
1104 MemOp = 0;
1105 }
1106 if (isDirectLoad(Op.getOperand(1))) {
1107 if (Op.getOperand(1).hasOneUse())
1108 return false;
1109 else
1110 MemOp = 1;
1111 }
1112 return true;
1113}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001114
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001115SDValue PIC16TargetLowering:: LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001116 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001117 // We should have handled larger operands in type legalizer itself.
1118 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001119 unsigned MemOp = 1;
1120 if (NeedToConvertToMemOp(Op, MemOp)) {
1121 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001122 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001123
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001124 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001125 NewVal);
1126 }
1127 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001128 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001129 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001130}
1131
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001132SDValue PIC16TargetLowering:: LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001133 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001134 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001135 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001136 unsigned MemOp = 1;
1137 if (NeedToConvertToMemOp(Op, MemOp)) {
1138 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001139 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001140
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001141 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001142
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001143 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001144 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1145 NewVal, Op.getOperand(2));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001146 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001147 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1148 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001149 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001150 else if (Op.getOpcode() == ISD::ADD) {
1151 return Op;
1152 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001153 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001154 return SDValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001155 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001156}
1157
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001158SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001159 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001160 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001161 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001162
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001163 // Nothing to do if the first operand is already a direct load and it has
1164 // only one use.
1165 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001166 return SDValue();
1167
1168 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001169 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001170
1171 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001172 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001173 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001174 Op.getOperand(2));
1175 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001176 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001177}
1178
1179// LowerFORMAL_ARGUMENTS - In Lowering FORMAL ARGUMENTS - MERGE_VALUES nodes
1180// is returned. MERGE_VALUES nodes number of operands and number of values are
1181// equal. Therefore to construct MERGE_VALUE node, UNDEF nodes equal to the
1182// number of arguments of function have been created.
1183
1184SDValue PIC16TargetLowering:: LowerFORMAL_ARGUMENTS(SDValue Op,
1185 SelectionDAG &DAG) {
1186 SmallVector<SDValue, 8> ArgValues;
1187 unsigned NumArgs = Op.getNumOperands() - 3;
1188
1189 // Creating UNDEF nodes to meet the requirement of MERGE_VALUES node.
1190 for(unsigned i = 0 ; i<NumArgs ; i++) {
1191 SDValue TempNode = DAG.getNode(ISD::UNDEF, Op.getNode()->getValueType(i));
1192 ArgValues.push_back(TempNode);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001193 }
1194
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001195 ArgValues.push_back(Op.getOperand(0));
1196 return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
1197 &ArgValues[0],
1198 ArgValues.size()).getValue(Op.getResNo());
1199}
1200
1201// Perform DAGCombine of PIC16Load
1202SDValue PIC16TargetLowering::
1203PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1204 SelectionDAG &DAG = DCI.DAG;
1205 SDValue Chain = N->getOperand(0);
1206 if (N->hasNUsesOfValue(0, 0)) {
1207 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1208 }
1209 return SDValue();
1210}
1211
1212
1213SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1214 DAGCombinerInfo &DCI) const {
1215 switch (N->getOpcode()) {
1216 case PIC16ISD::PIC16Load:
1217 return PerformPIC16LoadCombine(N, DCI);
1218 }
1219 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001220}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001221
1222static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1223 switch (CC) {
1224 default: assert(0 && "Unknown condition code!");
1225 case ISD::SETNE: return PIC16CC::NE;
1226 case ISD::SETEQ: return PIC16CC::EQ;
1227 case ISD::SETGT: return PIC16CC::GT;
1228 case ISD::SETGE: return PIC16CC::GE;
1229 case ISD::SETLT: return PIC16CC::LT;
1230 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001231 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001232 case ISD::SETULE: return PIC16CC::LE;
1233 case ISD::SETUGE: return PIC16CC::GE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001234 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001235 }
1236}
1237
1238// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1239// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1240static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1241 ISD::CondCode CC, unsigned &SPCC) {
1242 if (isa<ConstantSDNode>(RHS) &&
1243 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1244 CC == ISD::SETNE &&
1245 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1246 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1247 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1248 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1249 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1250 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1251 SDValue CMPCC = LHS.getOperand(3);
1252 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1253 LHS = CMPCC.getOperand(0);
1254 RHS = CMPCC.getOperand(1);
1255 }
1256}
1257
1258// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1259SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1260 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001261 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001262 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1263
1264 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1265 // i.e. a < 12 can be rewritten as 12 > a.
1266 if (RHS.getOpcode() == ISD::Constant) {
1267
1268 SDValue Tmp = LHS;
1269 LHS = RHS;
1270 RHS = Tmp;
1271
1272 switch (CondCode) {
1273 default: break;
1274 case PIC16CC::LT:
1275 CondCode = PIC16CC::GT;
1276 break;
1277 case PIC16CC::GT:
1278 CondCode = PIC16CC::LT;
1279 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001280 case PIC16CC::ULT:
1281 CondCode = PIC16CC::UGT;
1282 break;
1283 case PIC16CC::UGT:
1284 CondCode = PIC16CC::ULT;
1285 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001286 case PIC16CC::GE:
1287 CondCode = PIC16CC::LE;
1288 break;
1289 case PIC16CC::LE:
1290 CondCode = PIC16CC::GE;
1291 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001292 case PIC16CC::ULE:
1293 CondCode = PIC16CC::UGE;
1294 break;
1295 case PIC16CC::UGE:
1296 CondCode = PIC16CC::ULE;
1297 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001298 }
1299 }
1300
1301 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001302
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001303 // These are signed comparisons.
1304 SDValue Mask = DAG.getConstant(128, MVT::i8);
1305 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001306 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1307 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001308 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001309
1310 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001311 // We can use a subtract operation to set the condition codes. But
1312 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001313 // Nothing to do if the first operand is already a valid type (direct load
1314 // for subwf and literal for sublw) and it is used by this operation only.
1315 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1316 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001317 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001318
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001319 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001320 LHS = ConvertToMemOperand (LHS, DAG, dl);
1321 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001322}
1323
1324
1325SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1326 SDValue LHS = Op.getOperand(0);
1327 SDValue RHS = Op.getOperand(1);
1328 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1329 SDValue TrueVal = Op.getOperand(2);
1330 SDValue FalseVal = Op.getOperand(3);
1331 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001332 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001333
1334 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1335 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1336 // i.e.
1337 // A setcc: lhs, rhs, cc is expanded by llvm to
1338 // select_cc: result of setcc, 0, 1, 0, setne
1339 // We can think of it as:
1340 // select_cc: lhs, rhs, 1, 0, cc
1341 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1342 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1343
1344 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001345 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001346
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001347 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001348 FalseVal, PIC16CC, Cmp.getValue(1));
1349}
1350
1351MachineBasicBlock *
1352PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1353 MachineBasicBlock *BB) {
1354 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1355 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
1356
1357 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1358 // control-flow pattern. The incoming instruction knows the destination vreg
1359 // to set, the condition code register to branch on, the true/false values to
1360 // select between, and a branch opcode to use.
1361 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1362 MachineFunction::iterator It = BB;
1363 ++It;
1364
1365 // thisMBB:
1366 // ...
1367 // TrueVal = ...
1368 // [f]bCC copy1MBB
1369 // fallthrough --> copy0MBB
1370 MachineBasicBlock *thisMBB = BB;
1371 MachineFunction *F = BB->getParent();
1372 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1373 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1374 BuildMI(BB, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
1375 F->insert(It, copy0MBB);
1376 F->insert(It, sinkMBB);
1377
1378 // Update machine-CFG edges by transferring all successors of the current
1379 // block to the new block which will contain the Phi node for the select.
1380 sinkMBB->transferSuccessors(BB);
1381 // Next, add the true and fallthrough blocks as its successors.
1382 BB->addSuccessor(copy0MBB);
1383 BB->addSuccessor(sinkMBB);
1384
1385 // copy0MBB:
1386 // %FalseValue = ...
1387 // # fallthrough to sinkMBB
1388 BB = copy0MBB;
1389
1390 // Update machine-CFG edges
1391 BB->addSuccessor(sinkMBB);
1392
1393 // sinkMBB:
1394 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1395 // ...
1396 BB = sinkMBB;
1397 BuildMI(BB, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
1398 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1399 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1400
1401 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1402 return BB;
1403}
1404
1405
1406SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1407 SDValue Chain = Op.getOperand(0);
1408 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1409 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1410 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1411 SDValue Dest = Op.getOperand(4); // BB to jump to
1412 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001413 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001414
1415 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1416 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1417 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1418 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1419
1420 // Get the Compare insn and condition code.
1421 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001422 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001423
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001424 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001425 Cmp.getValue(1));
1426}
1427
1428