blob: 28cc0825525749ebb45b508323be2b2329f1661c [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();
Dale Johannesende064702009-02-06 21:50:26 +0000308
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000309 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000310 // FIXME there isn't really debug info here
311 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000312 int Index = FR->getIndex();
313
314 SDValue FI[2];
315 FI[0] = DAG.getTargetFrameIndex(Index, MVT::i8);
316 FI[1] = DAG.getTargetFrameIndex(Index + 1, MVT::i8);
Dale Johannesende064702009-02-06 21:50:26 +0000317 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), FI[0], FI[1]);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000318}
319
320
321SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000322 StoreSDNode *St = cast<StoreSDNode>(N);
323 SDValue Chain = St->getChain();
324 SDValue Src = St->getValue();
325 SDValue Ptr = St->getBasePtr();
326 MVT ValueType = Src.getValueType();
327 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000328 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000329
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000330 SDValue PtrLo, PtrHi;
331 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset);
332
333 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000334 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000335 PtrLo, PtrHi,
336 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000337 }
338 else if (ValueType == MVT::i16) {
339 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
340 SDValue SrcLo, SrcHi;
341 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
342 SDValue ChainLo = Chain, ChainHi = Chain;
343 if (Chain.getOpcode() == ISD::TokenFactor) {
344 ChainLo = Chain.getOperand(0);
345 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000346 }
Dale Johannesende064702009-02-06 21:50:26 +0000347 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000348 ChainLo,
349 SrcLo, PtrLo, PtrHi,
350 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000351
Dale Johannesende064702009-02-06 21:50:26 +0000352 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000353 SrcHi, PtrLo, PtrHi,
354 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000355
Dale Johannesende064702009-02-06 21:50:26 +0000356 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000357 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000358 }
359 else if (ValueType == MVT::i32) {
360 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
361 SDValue SrcLo, SrcHi;
362 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000363
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000364 // Get the expanded parts of each of SrcLo and SrcHi.
365 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
366 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
367 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000368
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000369 SDValue ChainLo = Chain, ChainHi = Chain;
370 if (Chain.getOpcode() == ISD::TokenFactor) {
371 ChainLo = Chain.getOperand(0);
372 ChainHi = Chain.getOperand(1);
373 }
374 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
375 ChainHi2 = ChainHi;
376 if (ChainLo.getOpcode() == ISD::TokenFactor) {
377 ChainLo1 = ChainLo.getOperand(0);
378 ChainLo2 = ChainLo.getOperand(1);
379 }
380 if (ChainHi.getOpcode() == ISD::TokenFactor) {
381 ChainHi1 = ChainHi.getOperand(0);
382 ChainHi2 = ChainHi.getOperand(1);
383 }
Dale Johannesende064702009-02-06 21:50:26 +0000384 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000385 ChainLo1,
386 SrcLo1, PtrLo, PtrHi,
387 DAG.getConstant (0 + StoreOffset, MVT::i8));
388
Dale Johannesende064702009-02-06 21:50:26 +0000389 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000390 SrcLo2, PtrLo, PtrHi,
391 DAG.getConstant (1 + StoreOffset, MVT::i8));
392
Dale Johannesende064702009-02-06 21:50:26 +0000393 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000394 SrcHi1, PtrLo, PtrHi,
395 DAG.getConstant (2 + StoreOffset, MVT::i8));
396
Dale Johannesende064702009-02-06 21:50:26 +0000397 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000398 SrcHi2, PtrLo, PtrHi,
399 DAG.getConstant (3 + StoreOffset, MVT::i8));
400
Dale Johannesende064702009-02-06 21:50:26 +0000401 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
402 getChain(Store1), getChain(Store2));
403 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
404 getChain(Store3), getChain(Store4));
405 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000406
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000407 }
408 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000409 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000410 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000411 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000412}
413
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000414SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
415{
416 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000417 // FIXME there isn't really debug info here
418 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000419
420 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
421
Dale Johannesende064702009-02-06 21:50:26 +0000422 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES);
423 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000424
Dale Johannesende064702009-02-06 21:50:26 +0000425 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000426}
427
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000428// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000429SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000430 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000431 // FIXME there isn't really debug info here
432 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000433
434 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
435 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000436
Dale Johannesende064702009-02-06 21:50:26 +0000437 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA);
438 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000439
Dale Johannesende064702009-02-06 21:50:26 +0000440 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000441}
442
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000443bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
444 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000445
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000446 if (Op.getOpcode() == ISD::BUILD_PAIR) {
447 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
448 return true;
449 }
450 return false;
451}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000452
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000453// Return true if DirectAddress is in ROM_SPACE
454bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000455
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000456 // RomAddress is a GlobalAddress in ROM_SPACE_
457 // If the Op is not a GlobalAddress return NULL without checking
458 // anything further.
459 if (!isDirectAddress(Op))
460 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000461
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000462 // Its a GlobalAddress.
463 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
464 SDValue TGA = Op.getOperand(0).getOperand(0);
465 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
466 const Type *ValueType = GSDN->getGlobal()->getType();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000467
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000468 if (!isa<PointerType>(ValueType)) {
469 assert(0 && "TGA must be of a PointerType");
470 }
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000471
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000472 int AddrSpace = dyn_cast<PointerType>(ValueType)->getAddressSpace();
473 if (AddrSpace == PIC16ISD::ROM_SPACE)
474 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000475
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000476 // Any other address space return it false
477 return false;
478}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000479
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000480// Extract the out flag
481SDValue PIC16TargetLowering::getOutFlag(SDValue &Op) {
482 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
483
484 assert (Flag.getValueType() == MVT::Flag && "Node does not have an out Flag");
485
486 return Flag;
487}
488
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000489// To extract chain value from the SDValue Nodes
490// This function will help to maintain the chain extracting
491// code at one place. In case of any change in future it will
492// help maintain the code.
493SDValue PIC16TargetLowering::getChain(SDValue &Op) {
494 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000495
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000496 // If the last value returned in Flag then the chain is
497 // second last value returned.
498 if (Chain.getValueType() == MVT::Flag)
499 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
500
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000501 // All nodes may not produce a chain. Therefore following assert
502 // verifies that the node is returning a chain only.
503 assert (Chain.getValueType() == MVT::Other && "Node does not have a chain");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000504
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000505 return Chain;
506}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000507
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000508void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
509 SDValue &Lo, SDValue &Hi) {
510 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000511 DebugLoc dl = N->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000512 MVT NewVT;
513 std::vector<SDValue> Opers;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000514 NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000515
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000516 // extract the lo component
517 Opers.push_back(Op);
518 Opers.push_back(DAG.getConstant(0,MVT::i8));
Dale Johannesened2eee62009-02-06 01:31:28 +0000519 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT,dl,NewVT,&Opers[0],Opers.size());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000520 // extract the hi component
521 Opers.clear();
522 Opers.push_back(Op);
523 Opers.push_back(DAG.getConstant(1,MVT::i8));
Dale Johannesened2eee62009-02-06 01:31:28 +0000524 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT,dl,NewVT,&Opers[0],Opers.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000525}
526
527// Legalize FrameIndex into ExternalSymbol and offset.
528void
529PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
530 SDValue &ES, int &Offset) {
531
532 MachineFunction &MF = DAG.getMachineFunction();
533 const Function *Func = MF.getFunction();
534 const std::string Name = Func->getName();
535
536 char *tmpName = new char [strlen(Name.c_str()) + 8];
537 sprintf(tmpName, "%s.args", Name.c_str());
538 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
539 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
540 Offset = FR->getIndex();
541
542 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000543}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000544
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000545// This function legalizes the PIC16 Addresses. If the Pointer is
546// -- Direct address variable residing
547// --> then a Banksel for that variable will be created.
548// -- Rom variable
549// --> then it will be treated as an indirect address.
550// -- Indirect address
551// --> then the address will be loaded into FSR
552// -- ADD with constant operand
553// --> then constant operand of ADD will be returned as Offset
554// and non-constant operand of ADD will be treated as pointer.
555// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000556
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000557void PIC16TargetLowering:: LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
558 SDValue &Lo, SDValue &Hi,
559 unsigned &Offset) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000560
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000561 // Offset, by default, should be 0
562 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000563
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000564 // If the pointer is ADD with constant,
565 // return the constant value as the offset
566 if (Ptr.getOpcode() == ISD::ADD) {
567 SDValue OperLeft = Ptr.getOperand(0);
568 SDValue OperRight = Ptr.getOperand(1);
569 if (OperLeft.getOpcode() == ISD::Constant) {
570 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
571 Ptr = OperRight;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000572 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000573 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000574 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000575 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000576 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000577
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000578 // If the pointer is Type i8 and an external symbol
579 // then treat it as direct address.
580 // One example for such case is storing and loading
581 // from function frame during a call
582 if (Ptr.getValueType() == MVT::i8) {
583 switch (Ptr.getOpcode()) {
584 case ISD::TargetExternalSymbol:
585 Lo = Ptr;
586 Hi = DAG.getConstant(1, MVT::i8);
587 return;
588 }
589 }
590
591 if (Ptr.getOpcode() == ISD::BUILD_PAIR &&
592 Ptr.getOperand(0).getOpcode() == ISD::TargetFrameIndex) {
593
594 int FrameOffset;
595 LegalizeFrameIndex(Ptr.getOperand(0), DAG, Lo, FrameOffset);
596 Hi = DAG.getConstant(1, MVT::i8);
597 Offset += FrameOffset;
598 return;
599 }
600
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000601 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
602 // Direct addressing case for RAM variables. The Hi part is constant
603 // and the Lo part is the TGA itself.
604 Lo = Ptr.getOperand(0).getOperand(0);
605
606 // For direct addresses Hi is a constant. Value 1 for the constant
607 // signifies that banksel needs to generated for it. Value 0 for
608 // the constant signifies that banksel does not need to be generated
609 // for it. Mark it as 1 now and optimize later.
610 Hi = DAG.getConstant(1, MVT::i8);
611 return;
612 }
613
614 // Indirect addresses. Get the hi and lo parts of ptr.
615 GetExpandedParts(Ptr, DAG, Lo, Hi);
616
617 // Put the hi and lo parts into FSR.
618 Lo = DAG.getNode(PIC16ISD::MTLO, MVT::i8, Lo);
619 Hi = DAG.getNode(PIC16ISD::MTHI, MVT::i8, Hi);
620
621 return;
622}
623
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000624//SDValue PIC16TargetLowering::ExpandAdd(SDNode *N, SelectionDAG &DAG) {
625 //SDValue OperLeft = N->getOperand(0);
626 //SDValue OperRight = N->getOperand(1);
627
628 //if((OperLeft.getOpcode() == ISD::Constant) ||
629 //(OperRight.getOpcode() == ISD::Constant)) {
630 //return SDValue();
631 //}
632
633 // These case are yet to be handled
634 //return SDValue();
Duncan Sands1607f052008-12-01 11:39:25 +0000635//}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000636
Duncan Sands1607f052008-12-01 11:39:25 +0000637SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000638 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
639 SDValue Chain = LD->getChain();
640 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000641 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000642
643 SDValue Load, Offset;
644 SDVTList Tys;
645 MVT VT, NewVT;
646 SDValue PtrLo, PtrHi;
647 unsigned LoadOffset;
648
649 // Legalize direct/indirect addresses. This will give the lo and hi parts
650 // of the address and the offset.
651 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset);
652
653 // Load from the pointer (direct address or FSR)
654 VT = N->getValueType(0);
655 unsigned NumLoads = VT.getSizeInBits() / 8;
656 std::vector<SDValue> PICLoads;
657 unsigned iter;
658 MVT MemVT = LD->getMemoryVT();
659 if(ISD::isNON_EXTLoad(N)) {
660 for (iter=0; iter<NumLoads ; ++iter) {
661 // Add the pointer offset if any
662 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
663 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000664 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000665 Offset);
666 PICLoads.push_back(Load);
667 }
668 } else {
669 // If it is extended load then use PIC16Load for Memory Bytes
670 // and for all extended bytes perform action based on type of
671 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
672
673
674 // For extended loads this is the memory value type
675 // i.e. without any extension
676 MVT MemVT = LD->getMemoryVT();
677 unsigned MemBytes = MemVT.getSizeInBits() / 8;
678 unsigned ExtdBytes = VT.getSizeInBits() / 8;
679 Offset = DAG.getConstant(LoadOffset, MVT::i8);
680
681 Tys = DAG.getVTList(MVT::i8, MVT::Other);
682 // For MemBytes generate PIC16Load with proper offset
683 for (iter=0; iter<MemBytes; ++iter) {
684 // Add the pointer offset if any
685 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000686 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000687 Offset);
688 PICLoads.push_back(Load);
689 }
690
691 // For SignExtendedLoad
692 if (ISD::isSEXTLoad(N)) {
693 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
694 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000695 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000696 DAG.getConstant(7, MVT::i8));
697 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
698 PICLoads.push_back(SRA);
699 }
700 } else if (ISD::isZEXTLoad(N)) {
701 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
702 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
703 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
704 PICLoads.push_back(ConstZero);
705 }
706 }
707 }
708 SDValue BP;
709
710 if (VT == MVT::i8) {
711 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000712 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000713 }
714 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000715 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000716 if (MemVT == MVT::i8)
717 Chain = getChain(PICLoads[0]);
718 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000719 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
720 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000721 } else if (VT == MVT::i32) {
722 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000723 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
724 PICLoads[0], PICLoads[1]);
725 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
726 PICLoads[2], PICLoads[3]);
727 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000728 if (MemVT == MVT::i8)
729 Chain = getChain(PICLoads[0]);
730 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000731 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
732 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000733 else {
734 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000735 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000736 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000737 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000738 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000739 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
740 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000741 }
742 }
743 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000744 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000745}
746
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000747SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
748 // We should have handled larger operands in type legalizer itself.
749 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
750
751 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000752 SDValue Value = N->getOperand(0);
753 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000754 PIC16ISD::PIC16Libcall CallCode;
755 switch (N->getOpcode()) {
756 case ISD::SRA:
757 CallCode = PIC16ISD::SRA_I8;
758 break;
759 case ISD::SHL:
760 CallCode = PIC16ISD::SLL_I8;
761 break;
762 case ISD::SRL:
763 CallCode = PIC16ISD::SRL_I8;
764 break;
765 default:
766 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000767 return SDValue();
768 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000769 SmallVector<SDValue, 2> Ops(2);
770 Ops[0] = Value;
771 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000772 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
773 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000774 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000775}
776
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000777void
778PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
779 SmallVectorImpl<SDValue>&Results,
780 SelectionDAG &DAG) {
781 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000782 SDValue Res;
783 unsigned i;
784 switch (Op.getOpcode()) {
785 case ISD::FORMAL_ARGUMENTS:
786 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
787 case ISD::LOAD:
788 Res = ExpandLoad(Op.getNode(), DAG); break;
789 case ISD::CALL:
790 Res = LowerCALL(Op, DAG); break;
791 default: {
792 // All other operations are handled in LowerOperation.
793 Res = LowerOperation(Op, DAG);
794 if (Res.getNode())
795 Results.push_back(Res);
796
797 return;
798 }
799 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000800
801 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000802 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000803 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000804 Results.push_back(SDValue(N, i));
805 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000806}
807
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000808SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
809 switch (Op.getOpcode()) {
810 case ISD::FORMAL_ARGUMENTS:
811 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000812 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000813 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000814 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000815 return LowerADD(Op, DAG);
816 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000817 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000818 case ISD::SUBE:
819 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000820 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000821 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000822 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000823 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000824 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000825 case ISD::SRA:
826 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000827 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000828 case ISD::OR:
829 case ISD::AND:
830 case ISD::XOR:
831 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000832 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000833 return LowerCALL(Op, DAG);
834 case ISD::RET:
835 return LowerRET(Op, DAG);
836 case ISD::BR_CC:
837 return LowerBR_CC(Op, DAG);
838 case ISD::SELECT_CC:
839 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000840 }
Dan Gohman475871a2008-07-27 21:46:04 +0000841 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000842}
843
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000844SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000845 SelectionDAG &DAG,
846 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000847 assert (Op.getValueType() == MVT::i8
848 && "illegal value type to store on stack.");
849
850 MachineFunction &MF = DAG.getMachineFunction();
851 const Function *Func = MF.getFunction();
852 const std::string FuncName = Func->getName();
853
854 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
855
856 // Put the value on stack.
857 // Get a stack slot index and convert to es.
858 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
859 sprintf(tmpName, "%s.tmp", FuncName.c_str());
860 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
861
862 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000863 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000864 DAG.getEntryNode(),
865 Op, ES,
866 DAG.getConstant (1, MVT::i8), // Banksel.
867 DAG.getConstant (FI, MVT::i8));
868
869 // Load the value from ES.
870 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000871 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000872 ES, DAG.getConstant (1, MVT::i8),
873 DAG.getConstant (FI, MVT::i8));
874
875 return Load.getValue(0);
876}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000877
878SDValue
879PIC16TargetLowering::LowerCallArguments(SDValue Op, SDValue Chain,
880 SDValue FrameAddress,
881 SDValue InFlag,
882 SelectionDAG &DAG) {
883 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
884 unsigned NumOps = TheCall->getNumArgs();
885 std::string Name;
886 SDValue Arg, StoreAt;
887 MVT ArgVT;
888 unsigned Size=0;
889 unsigned ArgCount=0;
890
891
892 // FIXME: This portion of code currently assumes only
893 // primitive types being passed as arguments.
894
895 // Legalize the address before use
896 SDValue PtrLo, PtrHi;
897 unsigned AddressOffset;
898 int StoreOffset = 0;
899 LegalizeAddress(FrameAddress, DAG, PtrLo, PtrHi, AddressOffset);
900 SDValue StoreRet;
901
902 std::vector<SDValue> Ops;
903 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
904 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
905 // Get the argument
906 Arg = TheCall->getArg(i);
907
908 StoreOffset = (Offset + AddressOffset);
909
910 // Store the argument on frame
911
912 Ops.clear();
913 Ops.push_back(Chain);
914 Ops.push_back(Arg.getValue(0));
915 Ops.push_back(PtrLo);
916 Ops.push_back(PtrHi);
917 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
918 Ops.push_back(InFlag);
919
920 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, Tys, &Ops[0], Ops.size());
921
922 Chain = getChain(StoreRet);
923 InFlag = getOutFlag(StoreRet);
924
925 // Update the frame offset to be used for next argument
926 ArgVT = Arg.getValueType();
927 Size = ArgVT.getSizeInBits();
928 Size = Size/8; // Calculate size in bytes
929 Offset += Size; // Increase the frame offset
930 }
931 return Chain;
932}
933
934SDValue
935PIC16TargetLowering::LowerCallReturn(SDValue Op, SDValue Chain,
936 SDValue FrameAddress,
937 SDValue InFlag,
938 SelectionDAG &DAG) {
939 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000940 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000941 // Currently handling primitive types only. They will come in
942 // i8 parts
943 unsigned RetVals = TheCall->getNumRetVals();
944
945 std::vector<SDValue> ResultVals;
946
947 // Return immediately if the return type is void
948 if (RetVals == 0)
949 return Chain;
950
951 // Call has something to return
952
953 // Legalize the address before use
954 SDValue LdLo, LdHi;
955 unsigned LdOffset;
956 LegalizeAddress(FrameAddress, DAG, LdLo, LdHi, LdOffset);
957
958 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
959 SDValue LoadRet;
960
961 for(unsigned i=0, Offset=0;i<RetVals;i++) {
962
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000963 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000964 DAG.getConstant(LdOffset + Offset, MVT::i8),
965 InFlag);
966
967 InFlag = getOutFlag(LoadRet);
968
969 Chain = getChain(LoadRet);
970 Offset++;
971 ResultVals.push_back(LoadRet);
972 }
973
974 // To return use MERGE_VALUES
975 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000976 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000977 return Res;
978}
979
980SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
981 //int NumOps = Op.getNode()->getNumOperands();
982
983 // For default cases LLVM returns the value on the function frame
984 // So let LLVM do this for all the cases other than character
985 return Op;
986}
987
988SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
989 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
990 SDValue Chain = TheCall->getChain();
991 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000992 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000993 unsigned i =0;
994 if (Callee.getValueType() == MVT::i16 &&
995 Callee.getOpcode() == ISD::BUILD_PAIR) {
996 // It has come from TypeLegalizer for lowering
997
998 Callee = Callee.getOperand(0).getOperand(0);
999
1000 std::vector<SDValue> Ops;
1001 Ops.push_back(Chain);
1002 Ops.push_back(Callee);
1003
1004 // Add the call arguments and their flags
1005 unsigned NumArgs = TheCall->getNumArgs();
1006 for(i=0;i<NumArgs;i++) {
1007 Ops.push_back(TheCall->getArg(i));
1008 Ops.push_back(TheCall->getArgFlagsVal(i));
1009 }
1010
1011 std::vector<MVT> NodeTys;
1012 unsigned NumRets = TheCall->getNumRetVals();
1013 for(i=0;i<NumRets;i++)
1014 NodeTys.push_back(TheCall->getRetValType(i));
1015
1016 // Return a Chain as well
1017 NodeTys.push_back(MVT::Other);
1018
1019 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1020 SDValue NewCall =
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001021 DAG.getCall(TheCall->getCallingConv(), dl,
Dale Johannesen8ad9b432009-02-04 01:17:06 +00001022 TheCall->isVarArg(), TheCall->isTailCall(),
1023 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001024
1025 return NewCall;
1026 }
1027
1028 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1029
1030 // Start the call sequence.
1031 // Carring the Constant 0 along the CALLSEQSTART
1032 // because there is nothing else to carry.
1033 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1034 Chain = getChain(SeqStart);
1035
1036 // For any direct call - callee will be GlobalAddressNode or
1037 // ExternalSymbol
1038
1039 // Considering the GlobalAddressNode case here.
1040 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1041 GlobalValue *GV = G->getGlobal();
1042 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1043 }
1044
1045 // Considering the ExternalSymbol case here
1046 if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1047 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1048 }
1049
1050 SDValue OperFlag = getOutFlag(Chain); // To manage the data dependency
1051
1052 std::string Name;
1053
1054 // Considering GlobalAddress here
1055 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1056 Name = G->getGlobal()->getName();
1057
1058 // Considering ExternalSymbol here
1059 if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee))
1060 Name = ES->getSymbol();
1061
1062 char *argFrame = new char [strlen(Name.c_str()) + 8];
1063 sprintf(argFrame, "%s.args", Name.c_str());
1064 SDValue ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
1065
1066 char *retName = new char [strlen(Name.c_str()) + 8];
1067 sprintf(retName, "%s.retval", Name.c_str());
1068 SDValue RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1069
1070 // Pass the argument to function before making the call.
1071 SDValue CallArgs = LowerCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1072 Chain = getChain(CallArgs);
1073 OperFlag = getOutFlag(CallArgs);
1074
1075 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001076 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001077 OperFlag);
1078 Chain = getChain(PICCall);
1079 OperFlag = getOutFlag(PICCall);
1080
1081
1082 // Carrying the Constant 0 along the CALLSEQSTART
1083 // because there is nothing else to carry.
1084 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1085 OperFlag);
1086 Chain = getChain(SeqEnd);
1087 OperFlag = getOutFlag(SeqEnd);
1088
1089 // Lower the return value reading after the call.
1090 return LowerCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1091}
1092
1093bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1094 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1095 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1096 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1097 return true;
1098 return false;
1099}
1100
1101bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
1102 // Return false if one of the operands is already a direct
1103 // load and that operand has only one use.
1104 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1105 Op.getOperand(1).getOpcode() == ISD::Constant)
1106 return false;
1107 if (isDirectLoad(Op.getOperand(0))) {
1108 if (Op.getOperand(0).hasOneUse())
1109 return false;
1110 else
1111 MemOp = 0;
1112 }
1113 if (isDirectLoad(Op.getOperand(1))) {
1114 if (Op.getOperand(1).hasOneUse())
1115 return false;
1116 else
1117 MemOp = 1;
1118 }
1119 return true;
1120}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001121
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001122SDValue PIC16TargetLowering:: LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001123 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001124 // We should have handled larger operands in type legalizer itself.
1125 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001126 unsigned MemOp = 1;
1127 if (NeedToConvertToMemOp(Op, MemOp)) {
1128 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001129 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001130
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001131 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001132 NewVal);
1133 }
1134 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001135 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001136 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001137}
1138
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001139SDValue PIC16TargetLowering:: LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001140 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001141 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001142 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001143 unsigned MemOp = 1;
1144 if (NeedToConvertToMemOp(Op, MemOp)) {
1145 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001146 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001147
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001148 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001149
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001150 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001151 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1152 NewVal, Op.getOperand(2));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001153 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001154 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1155 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001156 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001157 else if (Op.getOpcode() == ISD::ADD) {
1158 return Op;
1159 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001160 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001161 return SDValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001162 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001163}
1164
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001165SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001166 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001167 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001168 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001169
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001170 // Nothing to do if the first operand is already a direct load and it has
1171 // only one use.
1172 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001173 return SDValue();
1174
1175 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001176 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001177
1178 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001179 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001180 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001181 Op.getOperand(2));
1182 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001183 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001184}
1185
1186// LowerFORMAL_ARGUMENTS - In Lowering FORMAL ARGUMENTS - MERGE_VALUES nodes
1187// is returned. MERGE_VALUES nodes number of operands and number of values are
1188// equal. Therefore to construct MERGE_VALUE node, UNDEF nodes equal to the
1189// number of arguments of function have been created.
1190
1191SDValue PIC16TargetLowering:: LowerFORMAL_ARGUMENTS(SDValue Op,
1192 SelectionDAG &DAG) {
1193 SmallVector<SDValue, 8> ArgValues;
1194 unsigned NumArgs = Op.getNumOperands() - 3;
1195
1196 // Creating UNDEF nodes to meet the requirement of MERGE_VALUES node.
1197 for(unsigned i = 0 ; i<NumArgs ; i++) {
1198 SDValue TempNode = DAG.getNode(ISD::UNDEF, Op.getNode()->getValueType(i));
1199 ArgValues.push_back(TempNode);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001200 }
1201
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001202 ArgValues.push_back(Op.getOperand(0));
1203 return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
1204 &ArgValues[0],
1205 ArgValues.size()).getValue(Op.getResNo());
1206}
1207
1208// Perform DAGCombine of PIC16Load
1209SDValue PIC16TargetLowering::
1210PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1211 SelectionDAG &DAG = DCI.DAG;
1212 SDValue Chain = N->getOperand(0);
1213 if (N->hasNUsesOfValue(0, 0)) {
1214 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1215 }
1216 return SDValue();
1217}
1218
1219
1220SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1221 DAGCombinerInfo &DCI) const {
1222 switch (N->getOpcode()) {
1223 case PIC16ISD::PIC16Load:
1224 return PerformPIC16LoadCombine(N, DCI);
1225 }
1226 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001227}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001228
1229static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1230 switch (CC) {
1231 default: assert(0 && "Unknown condition code!");
1232 case ISD::SETNE: return PIC16CC::NE;
1233 case ISD::SETEQ: return PIC16CC::EQ;
1234 case ISD::SETGT: return PIC16CC::GT;
1235 case ISD::SETGE: return PIC16CC::GE;
1236 case ISD::SETLT: return PIC16CC::LT;
1237 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001238 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001239 case ISD::SETULE: return PIC16CC::LE;
1240 case ISD::SETUGE: return PIC16CC::GE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001241 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001242 }
1243}
1244
1245// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1246// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1247static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1248 ISD::CondCode CC, unsigned &SPCC) {
1249 if (isa<ConstantSDNode>(RHS) &&
1250 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1251 CC == ISD::SETNE &&
1252 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1253 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1254 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1255 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1256 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1257 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1258 SDValue CMPCC = LHS.getOperand(3);
1259 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1260 LHS = CMPCC.getOperand(0);
1261 RHS = CMPCC.getOperand(1);
1262 }
1263}
1264
1265// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1266SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1267 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001268 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001269 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1270
1271 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1272 // i.e. a < 12 can be rewritten as 12 > a.
1273 if (RHS.getOpcode() == ISD::Constant) {
1274
1275 SDValue Tmp = LHS;
1276 LHS = RHS;
1277 RHS = Tmp;
1278
1279 switch (CondCode) {
1280 default: break;
1281 case PIC16CC::LT:
1282 CondCode = PIC16CC::GT;
1283 break;
1284 case PIC16CC::GT:
1285 CondCode = PIC16CC::LT;
1286 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001287 case PIC16CC::ULT:
1288 CondCode = PIC16CC::UGT;
1289 break;
1290 case PIC16CC::UGT:
1291 CondCode = PIC16CC::ULT;
1292 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001293 case PIC16CC::GE:
1294 CondCode = PIC16CC::LE;
1295 break;
1296 case PIC16CC::LE:
1297 CondCode = PIC16CC::GE;
1298 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001299 case PIC16CC::ULE:
1300 CondCode = PIC16CC::UGE;
1301 break;
1302 case PIC16CC::UGE:
1303 CondCode = PIC16CC::ULE;
1304 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001305 }
1306 }
1307
1308 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001309
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001310 // These are signed comparisons.
1311 SDValue Mask = DAG.getConstant(128, MVT::i8);
1312 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001313 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1314 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001315 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001316
1317 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001318 // We can use a subtract operation to set the condition codes. But
1319 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001320 // Nothing to do if the first operand is already a valid type (direct load
1321 // for subwf and literal for sublw) and it is used by this operation only.
1322 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1323 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001324 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001325
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001326 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001327 LHS = ConvertToMemOperand (LHS, DAG, dl);
1328 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001329}
1330
1331
1332SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1333 SDValue LHS = Op.getOperand(0);
1334 SDValue RHS = Op.getOperand(1);
1335 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1336 SDValue TrueVal = Op.getOperand(2);
1337 SDValue FalseVal = Op.getOperand(3);
1338 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001339 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001340
1341 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1342 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1343 // i.e.
1344 // A setcc: lhs, rhs, cc is expanded by llvm to
1345 // select_cc: result of setcc, 0, 1, 0, setne
1346 // We can think of it as:
1347 // select_cc: lhs, rhs, 1, 0, cc
1348 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1349 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1350
1351 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001352 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001353
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001354 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001355 FalseVal, PIC16CC, Cmp.getValue(1));
1356}
1357
1358MachineBasicBlock *
1359PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1360 MachineBasicBlock *BB) {
1361 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1362 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
1363
1364 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1365 // control-flow pattern. The incoming instruction knows the destination vreg
1366 // to set, the condition code register to branch on, the true/false values to
1367 // select between, and a branch opcode to use.
1368 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1369 MachineFunction::iterator It = BB;
1370 ++It;
1371
1372 // thisMBB:
1373 // ...
1374 // TrueVal = ...
1375 // [f]bCC copy1MBB
1376 // fallthrough --> copy0MBB
1377 MachineBasicBlock *thisMBB = BB;
1378 MachineFunction *F = BB->getParent();
1379 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1380 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1381 BuildMI(BB, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
1382 F->insert(It, copy0MBB);
1383 F->insert(It, sinkMBB);
1384
1385 // Update machine-CFG edges by transferring all successors of the current
1386 // block to the new block which will contain the Phi node for the select.
1387 sinkMBB->transferSuccessors(BB);
1388 // Next, add the true and fallthrough blocks as its successors.
1389 BB->addSuccessor(copy0MBB);
1390 BB->addSuccessor(sinkMBB);
1391
1392 // copy0MBB:
1393 // %FalseValue = ...
1394 // # fallthrough to sinkMBB
1395 BB = copy0MBB;
1396
1397 // Update machine-CFG edges
1398 BB->addSuccessor(sinkMBB);
1399
1400 // sinkMBB:
1401 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1402 // ...
1403 BB = sinkMBB;
1404 BuildMI(BB, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
1405 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1406 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1407
1408 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1409 return BB;
1410}
1411
1412
1413SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1414 SDValue Chain = Op.getOperand(0);
1415 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1416 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1417 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1418 SDValue Dest = Op.getOperand(4); // BB to jump to
1419 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001420 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001421
1422 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1423 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1424 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1425 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1426
1427 // Get the Compare insn and condition code.
1428 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001429 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001430
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001431 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001432 Cmp.getValue(1));
1433}
1434
1435