blob: e0603bbbef26867d4a7da5fc8648fe00740d864b [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)
Sanjiv Guptacae1b622009-04-06 10:54:50 +000034 : TargetLowering(TM), TmpSize(0) {
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 Gupta6b830e62009-03-20 13:42:20 +0000145// getOutFlag - Extract the flag result if the Op has it.
146static SDValue getOutFlag(SDValue &Op) {
147 // Flag is the last value of the node.
148 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
149
150 assert (Flag.getValueType() == MVT::Flag
151 && "Node does not have an out Flag");
152
153 return Flag;
154}
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000155// Get the TmpOffset for FrameIndex
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000156unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI, unsigned size) {
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000157 std::map<unsigned, unsigned>::iterator
158 MapIt = FiTmpOffsetMap.find(FI);
159 if (MapIt != FiTmpOffsetMap.end())
160 return MapIt->second;
161
162 // This FI (FrameIndex) is not yet mapped, so map it
163 FiTmpOffsetMap[FI] = TmpSize;
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000164 TmpSize += size;
165 return FiTmpOffsetMap[FI];
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000166}
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000167
168// To extract chain value from the SDValue Nodes
169// This function will help to maintain the chain extracting
170// code at one place. In case of any change in future it will
171// help maintain the code.
172static SDValue getChain(SDValue &Op) {
173 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
174
175 // If the last value returned in Flag then the chain is
176 // second last value returned.
177 if (Chain.getValueType() == MVT::Flag)
178 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
179
180 // All nodes may not produce a chain. Therefore following assert
181 // verifies that the node is returning a chain only.
182 assert (Chain.getValueType() == MVT::Other
183 && "Node does not have a chain");
184
185 return Chain;
186}
187
188/// PopulateResults - Helper function to LowerOperation.
189/// If a node wants to return multiple results after lowering,
190/// it stuffs them into an array of SDValue called Results.
191
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000192static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
193 if (N.getOpcode() == ISD::MERGE_VALUES) {
194 int NumResults = N.getNumOperands();
195 for( int i = 0; i < NumResults; i++)
196 Results.push_back(N.getOperand(i));
197 }
198 else
199 Results.push_back(N);
200}
201
202MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
203 return MVT::i8;
204}
205
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000206/// The type legalizer framework of generating legalizer can generate libcalls
207/// only when the operand/result types are illegal.
208/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
209/// For example an arithmetic right shift. These functions are used to lower
210/// such operations that generate libcall for legal types.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000211
212void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000213PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000214 const char *Name) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000215 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000216}
217
218const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000219PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000220 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000221}
222
223SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000224PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000225 MVT RetVT, const SDValue *Ops,
226 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000227 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000228
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000229 TargetLowering::ArgListTy Args;
230 Args.reserve(NumOps);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000231
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000232 TargetLowering::ArgListEntry Entry;
233 for (unsigned i = 0; i != NumOps; ++i) {
234 Entry.Node = Ops[i];
235 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
236 Entry.isSExt = isSigned;
237 Entry.isZExt = !isSigned;
238 Args.push_back(Entry);
239 }
240 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000241
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000242 const Type *RetTy = RetVT.getTypeForMVT();
243 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000244 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000245 false, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000246
247 return CallInfo.first;
248}
249
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000250const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
251 switch (Opcode) {
252 default: return NULL;
253 case PIC16ISD::Lo: return "PIC16ISD::Lo";
254 case PIC16ISD::Hi: return "PIC16ISD::Hi";
255 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
256 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000257 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
258 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000259 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
260 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000261 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000262 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000263 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000264 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000265 case PIC16ISD::BCF: return "PIC16ISD::BCF";
266 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
267 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
268 case PIC16ISD::RLF: return "PIC16ISD::RLF";
269 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000270 case PIC16ISD::CALL: return "PIC16ISD::CALL";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000271 case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000272 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
273 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
274 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000275 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000276 }
277}
278
Duncan Sands1607f052008-12-01 11:39:25 +0000279void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
280 SmallVectorImpl<SDValue>&Results,
281 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000282
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000283 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000284 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000285 Results.push_back(ExpandGlobalAddress(N, DAG));
286 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000287 case ISD::ExternalSymbol:
288 Results.push_back(ExpandExternalSymbol(N, DAG));
289 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000290 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000291 Results.push_back(ExpandStore(N, DAG));
292 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000293 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000294 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000295 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000296 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000297 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000298 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000299 case ISD::FrameIndex:
300 Results.push_back(ExpandFrameIndex(N, DAG));
301 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000302 default:
303 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000304 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000305 }
306}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000307
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000308SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
309
310 // Currently handling FrameIndex of size MVT::i16 only
311 // One example of this scenario is when return value is written on
312 // FrameIndex#0
313
314 if (N->getValueType(0) != MVT::i16)
315 return SDValue();
316
317 // Expand the FrameIndex into ExternalSymbol and a Constant node
318 // The constant will represent the frame index number
319 // Get the current function frame
320 MachineFunction &MF = DAG.getMachineFunction();
321 const Function *Func = MF.getFunction();
322 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000323
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000324 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000325 // FIXME there isn't really debug info here
326 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000327 int Index = FR->getIndex();
328
329 SDValue FI[2];
330 FI[0] = DAG.getTargetFrameIndex(Index, MVT::i8);
331 FI[1] = DAG.getTargetFrameIndex(Index + 1, MVT::i8);
Dale Johannesende064702009-02-06 21:50:26 +0000332 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), FI[0], FI[1]);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000333}
334
335
336SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000337 StoreSDNode *St = cast<StoreSDNode>(N);
338 SDValue Chain = St->getChain();
339 SDValue Src = St->getValue();
340 SDValue Ptr = St->getBasePtr();
341 MVT ValueType = Src.getValueType();
342 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000343 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000344
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000345 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000346 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000347
348 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000349 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000350 PtrLo, PtrHi,
351 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000352 }
353 else if (ValueType == MVT::i16) {
354 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
355 SDValue SrcLo, SrcHi;
356 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
357 SDValue ChainLo = Chain, ChainHi = Chain;
358 if (Chain.getOpcode() == ISD::TokenFactor) {
359 ChainLo = Chain.getOperand(0);
360 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000361 }
Dale Johannesende064702009-02-06 21:50:26 +0000362 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000363 ChainLo,
364 SrcLo, PtrLo, PtrHi,
365 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000366
Dale Johannesende064702009-02-06 21:50:26 +0000367 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000368 SrcHi, PtrLo, PtrHi,
369 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000370
Dale Johannesende064702009-02-06 21:50:26 +0000371 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000372 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000373 }
374 else if (ValueType == MVT::i32) {
375 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
376 SDValue SrcLo, SrcHi;
377 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000378
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000379 // Get the expanded parts of each of SrcLo and SrcHi.
380 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
381 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
382 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000383
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000384 SDValue ChainLo = Chain, ChainHi = Chain;
385 if (Chain.getOpcode() == ISD::TokenFactor) {
386 ChainLo = Chain.getOperand(0);
387 ChainHi = Chain.getOperand(1);
388 }
389 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
390 ChainHi2 = ChainHi;
391 if (ChainLo.getOpcode() == ISD::TokenFactor) {
392 ChainLo1 = ChainLo.getOperand(0);
393 ChainLo2 = ChainLo.getOperand(1);
394 }
395 if (ChainHi.getOpcode() == ISD::TokenFactor) {
396 ChainHi1 = ChainHi.getOperand(0);
397 ChainHi2 = ChainHi.getOperand(1);
398 }
Dale Johannesende064702009-02-06 21:50:26 +0000399 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000400 ChainLo1,
401 SrcLo1, PtrLo, PtrHi,
402 DAG.getConstant (0 + StoreOffset, MVT::i8));
403
Dale Johannesende064702009-02-06 21:50:26 +0000404 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000405 SrcLo2, PtrLo, PtrHi,
406 DAG.getConstant (1 + StoreOffset, MVT::i8));
407
Dale Johannesende064702009-02-06 21:50:26 +0000408 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000409 SrcHi1, PtrLo, PtrHi,
410 DAG.getConstant (2 + StoreOffset, MVT::i8));
411
Dale Johannesende064702009-02-06 21:50:26 +0000412 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000413 SrcHi2, PtrLo, PtrHi,
414 DAG.getConstant (3 + StoreOffset, MVT::i8));
415
Dale Johannesende064702009-02-06 21:50:26 +0000416 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
417 getChain(Store1), getChain(Store2));
418 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
419 getChain(Store3), getChain(Store4));
420 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000421
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000422 }
423 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000424 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000425 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000426 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000427}
428
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000429SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
430{
431 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000432 // FIXME there isn't really debug info here
433 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000434
435 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
436
Dale Johannesende064702009-02-06 21:50:26 +0000437 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES);
438 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000439
Dale Johannesende064702009-02-06 21:50:26 +0000440 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000441}
442
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000443// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000444SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000445 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000446 // FIXME there isn't really debug info here
447 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000448
449 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
450 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000451
Dale Johannesende064702009-02-06 21:50:26 +0000452 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA);
453 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000454
Dale Johannesende064702009-02-06 21:50:26 +0000455 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000456}
457
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000458bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
459 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000460
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000461 if (Op.getOpcode() == ISD::BUILD_PAIR) {
462 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
463 return true;
464 }
465 return false;
466}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000467
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000468// Return true if DirectAddress is in ROM_SPACE
469bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000470
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000471 // RomAddress is a GlobalAddress in ROM_SPACE_
472 // If the Op is not a GlobalAddress return NULL without checking
473 // anything further.
474 if (!isDirectAddress(Op))
475 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000476
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000477 // Its a GlobalAddress.
478 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
479 SDValue TGA = Op.getOperand(0).getOperand(0);
480 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
481 const Type *ValueType = GSDN->getGlobal()->getType();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000482
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000483 if (!isa<PointerType>(ValueType)) {
484 assert(0 && "TGA must be of a PointerType");
485 }
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000486
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000487 int AddrSpace = dyn_cast<PointerType>(ValueType)->getAddressSpace();
488 if (AddrSpace == PIC16ISD::ROM_SPACE)
489 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000490
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000491 // Any other address space return it false
492 return false;
493}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000494
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000495
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000496// GetExpandedParts - This function is on the similiar lines as
497// the GetExpandedInteger in type legalizer is. This returns expanded
498// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000499
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000500void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
501 SDValue &Lo, SDValue &Hi) {
502 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000503 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000504 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000505
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000506 // Extract the lo component.
507 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
508 DAG.getConstant(0, MVT::i8));
509
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000510 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000511 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
512 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000513}
514
515// Legalize FrameIndex into ExternalSymbol and offset.
516void
517PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
518 SDValue &ES, int &Offset) {
519
520 MachineFunction &MF = DAG.getMachineFunction();
521 const Function *Func = MF.getFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000522 MachineFrameInfo *MFI = MF.getFrameInfo();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000523 const std::string Name = Func->getName();
524
525 char *tmpName = new char [strlen(Name.c_str()) + 8];
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000526 sprintf(tmpName, "%s.frame", Name.c_str());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000527 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
528 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000529
530 // FrameIndices are not stack offsets. But they represent the request
531 // for space on stack. That space requested may be more than one byte.
532 // Therefore, to calculate the stack offset that a FrameIndex aligns
533 // with, we need to traverse all the FrameIndices available earlier in
534 // the list and add their requested size.
535 unsigned FIndex = FR->getIndex();
536 Offset = 0;
537 for (unsigned i=0; i<FIndex ; ++i) {
538 Offset += MFI->getObjectSize(i);
539 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000540
541 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000542}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000543
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000544// This function legalizes the PIC16 Addresses. If the Pointer is
545// -- Direct address variable residing
546// --> then a Banksel for that variable will be created.
547// -- Rom variable
548// --> then it will be treated as an indirect address.
549// -- Indirect address
550// --> then the address will be loaded into FSR
551// -- ADD with constant operand
552// --> then constant operand of ADD will be returned as Offset
553// and non-constant operand of ADD will be treated as pointer.
554// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000555
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000556void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
557 SDValue &Lo, SDValue &Hi,
558 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000559
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000560 // Offset, by default, should be 0
561 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000562
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000563 // If the pointer is ADD with constant,
564 // return the constant value as the offset
565 if (Ptr.getOpcode() == ISD::ADD) {
566 SDValue OperLeft = Ptr.getOperand(0);
567 SDValue OperRight = Ptr.getOperand(1);
568 if (OperLeft.getOpcode() == ISD::Constant) {
569 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
570 Ptr = OperRight;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000571 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000572 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000573 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000574 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000575 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000576
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000577 // If the pointer is Type i8 and an external symbol
578 // then treat it as direct address.
579 // One example for such case is storing and loading
580 // from function frame during a call
581 if (Ptr.getValueType() == MVT::i8) {
582 switch (Ptr.getOpcode()) {
583 case ISD::TargetExternalSymbol:
584 Lo = Ptr;
585 Hi = DAG.getConstant(1, MVT::i8);
586 return;
587 }
588 }
589
590 if (Ptr.getOpcode() == ISD::BUILD_PAIR &&
591 Ptr.getOperand(0).getOpcode() == ISD::TargetFrameIndex) {
592
593 int FrameOffset;
594 LegalizeFrameIndex(Ptr.getOperand(0), DAG, Lo, FrameOffset);
595 Hi = DAG.getConstant(1, MVT::i8);
596 Offset += FrameOffset;
597 return;
598 }
599
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000600 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
601 // Direct addressing case for RAM variables. The Hi part is constant
602 // and the Lo part is the TGA itself.
603 Lo = Ptr.getOperand(0).getOperand(0);
604
605 // For direct addresses Hi is a constant. Value 1 for the constant
606 // signifies that banksel needs to generated for it. Value 0 for
607 // the constant signifies that banksel does not need to be generated
608 // for it. Mark it as 1 now and optimize later.
609 Hi = DAG.getConstant(1, MVT::i8);
610 return;
611 }
612
613 // Indirect addresses. Get the hi and lo parts of ptr.
614 GetExpandedParts(Ptr, DAG, Lo, Hi);
615
616 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000617 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
618 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000619
620 return;
621}
622
Duncan Sands1607f052008-12-01 11:39:25 +0000623SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000624 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
625 SDValue Chain = LD->getChain();
626 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000627 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000628
629 SDValue Load, Offset;
630 SDVTList Tys;
631 MVT VT, NewVT;
632 SDValue PtrLo, PtrHi;
633 unsigned LoadOffset;
634
635 // Legalize direct/indirect addresses. This will give the lo and hi parts
636 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000637 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000638
639 // Load from the pointer (direct address or FSR)
640 VT = N->getValueType(0);
641 unsigned NumLoads = VT.getSizeInBits() / 8;
642 std::vector<SDValue> PICLoads;
643 unsigned iter;
644 MVT MemVT = LD->getMemoryVT();
645 if(ISD::isNON_EXTLoad(N)) {
646 for (iter=0; iter<NumLoads ; ++iter) {
647 // Add the pointer offset if any
648 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
649 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000650 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000651 Offset);
652 PICLoads.push_back(Load);
653 }
654 } else {
655 // If it is extended load then use PIC16Load for Memory Bytes
656 // and for all extended bytes perform action based on type of
657 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
658
659
660 // For extended loads this is the memory value type
661 // i.e. without any extension
662 MVT MemVT = LD->getMemoryVT();
663 unsigned MemBytes = MemVT.getSizeInBits() / 8;
664 unsigned ExtdBytes = VT.getSizeInBits() / 8;
665 Offset = DAG.getConstant(LoadOffset, MVT::i8);
666
667 Tys = DAG.getVTList(MVT::i8, MVT::Other);
668 // For MemBytes generate PIC16Load with proper offset
669 for (iter=0; iter<MemBytes; ++iter) {
670 // Add the pointer offset if any
671 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000672 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000673 Offset);
674 PICLoads.push_back(Load);
675 }
676
677 // For SignExtendedLoad
678 if (ISD::isSEXTLoad(N)) {
679 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
680 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000681 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000682 DAG.getConstant(7, MVT::i8));
683 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
684 PICLoads.push_back(SRA);
685 }
686 } else if (ISD::isZEXTLoad(N)) {
687 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
688 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
689 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
690 PICLoads.push_back(ConstZero);
691 }
692 }
693 }
694 SDValue BP;
695
696 if (VT == MVT::i8) {
697 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000698 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000699 }
700 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000701 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000702 if (MemVT == MVT::i8)
703 Chain = getChain(PICLoads[0]);
704 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000705 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
706 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000707 } else if (VT == MVT::i32) {
708 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000709 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
710 PICLoads[0], PICLoads[1]);
711 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
712 PICLoads[2], PICLoads[3]);
713 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000714 if (MemVT == MVT::i8)
715 Chain = getChain(PICLoads[0]);
716 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000717 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
718 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000719 else {
720 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000721 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000722 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000723 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000724 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000725 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
726 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000727 }
728 }
729 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000730 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000731}
732
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000733SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
734 // We should have handled larger operands in type legalizer itself.
735 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
736
737 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000738 SDValue Value = N->getOperand(0);
739 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000740 PIC16ISD::PIC16Libcall CallCode;
741 switch (N->getOpcode()) {
742 case ISD::SRA:
743 CallCode = PIC16ISD::SRA_I8;
744 break;
745 case ISD::SHL:
746 CallCode = PIC16ISD::SLL_I8;
747 break;
748 case ISD::SRL:
749 CallCode = PIC16ISD::SRL_I8;
750 break;
751 default:
752 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000753 return SDValue();
754 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000755 SmallVector<SDValue, 2> Ops(2);
756 Ops[0] = Value;
757 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000758 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
759 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000760 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000761}
762
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000763void
764PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
765 SmallVectorImpl<SDValue>&Results,
766 SelectionDAG &DAG) {
767 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000768 SDValue Res;
769 unsigned i;
770 switch (Op.getOpcode()) {
771 case ISD::FORMAL_ARGUMENTS:
772 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
773 case ISD::LOAD:
774 Res = ExpandLoad(Op.getNode(), DAG); break;
775 case ISD::CALL:
776 Res = LowerCALL(Op, DAG); break;
777 default: {
778 // All other operations are handled in LowerOperation.
779 Res = LowerOperation(Op, DAG);
780 if (Res.getNode())
781 Results.push_back(Res);
782
783 return;
784 }
785 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000786
787 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000788 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000789 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000790 Results.push_back(SDValue(N, i));
791 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000792}
793
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000794SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
795 switch (Op.getOpcode()) {
796 case ISD::FORMAL_ARGUMENTS:
797 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000798 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000799 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000800 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000801 return LowerADD(Op, DAG);
802 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000803 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000804 case ISD::SUBE:
805 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000806 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000807 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000808 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000809 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000810 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000811 case ISD::SRA:
812 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000813 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000814 case ISD::OR:
815 case ISD::AND:
816 case ISD::XOR:
817 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000818 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000819 return LowerCALL(Op, DAG);
820 case ISD::RET:
821 return LowerRET(Op, DAG);
822 case ISD::BR_CC:
823 return LowerBR_CC(Op, DAG);
824 case ISD::SELECT_CC:
825 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000826 }
Dan Gohman475871a2008-07-27 21:46:04 +0000827 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000828}
829
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000830SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000831 SelectionDAG &DAG,
832 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000833 assert (Op.getValueType() == MVT::i8
834 && "illegal value type to store on stack.");
835
836 MachineFunction &MF = DAG.getMachineFunction();
837 const Function *Func = MF.getFunction();
838 const std::string FuncName = Func->getName();
839
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000840 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000841
842 // Put the value on stack.
843 // Get a stack slot index and convert to es.
844 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
845 sprintf(tmpName, "%s.tmp", FuncName.c_str());
846 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
847
848 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000849 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000850 DAG.getEntryNode(),
851 Op, ES,
852 DAG.getConstant (1, MVT::i8), // Banksel.
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000853 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000854 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000855
856 // Load the value from ES.
857 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000858 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000859 ES, DAG.getConstant (1, MVT::i8),
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000860 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000861 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000862
863 return Load.getValue(0);
864}
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000865
866SDValue PIC16TargetLowering::
867LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
868 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
869 SelectionDAG &DAG) {
870 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
871 unsigned NumOps = TheCall->getNumArgs();
872 DebugLoc dl = TheCall->getDebugLoc();
873
874 // If call has no arguments then do nothing and return.
875 if (NumOps == 0)
876 return Chain;
877
878 std::vector<SDValue> Ops;
879 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
880 SDValue Arg, StoreRet;
Sanjiv Guptae206b1d2009-04-09 10:29:32 +0000881
882 // For PIC16 ABI the arguments come after the return value.
883 unsigned RetVals = TheCall->getNumRetVals();
884 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000885 // Get the arguments
886 Arg = TheCall->getArg(i);
887 Ops.clear();
888 Ops.push_back(Chain);
889 Ops.push_back(Arg);
890 Ops.push_back(DataAddr_Lo);
891 Ops.push_back(DataAddr_Hi);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +0000892 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000893 Ops.push_back(InFlag);
894
895 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
896
897 Chain = getChain(StoreRet);
898 InFlag = getOutFlag(StoreRet);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +0000899 ArgOffset++;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000900 }
901 return Chain;
902}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000903
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000904SDValue PIC16TargetLowering::
905LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress,
906 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000907 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
908 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +0000909 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000910 std::string Name;
911 SDValue Arg, StoreAt;
912 MVT ArgVT;
913 unsigned Size=0;
914 unsigned ArgCount=0;
915
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000916 // If call has no arguments then do nothing and return.
917 if (NumOps == 0)
918 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000919
920 // FIXME: This portion of code currently assumes only
921 // primitive types being passed as arguments.
922
923 // Legalize the address before use
924 SDValue PtrLo, PtrHi;
925 unsigned AddressOffset;
926 int StoreOffset = 0;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000927 LegalizeAddress(FrameAddress, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000928 SDValue StoreRet;
929
930 std::vector<SDValue> Ops;
931 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
932 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
933 // Get the argument
934 Arg = TheCall->getArg(i);
935
936 StoreOffset = (Offset + AddressOffset);
937
938 // Store the argument on frame
939
940 Ops.clear();
941 Ops.push_back(Chain);
942 Ops.push_back(Arg.getValue(0));
943 Ops.push_back(PtrLo);
944 Ops.push_back(PtrHi);
945 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
946 Ops.push_back(InFlag);
947
Dale Johannesene8d72302009-02-06 23:05:02 +0000948 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000949
950 Chain = getChain(StoreRet);
951 InFlag = getOutFlag(StoreRet);
952
953 // Update the frame offset to be used for next argument
954 ArgVT = Arg.getValueType();
955 Size = ArgVT.getSizeInBits();
956 Size = Size/8; // Calculate size in bytes
957 Offset += Size; // Increase the frame offset
958 }
959 return Chain;
960}
961
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000962SDValue PIC16TargetLowering::
963LowerIndirectCallReturn (SDValue Op, SDValue Chain, SDValue InFlag,
964 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
965 SelectionDAG &DAG) {
966 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
967 DebugLoc dl = TheCall->getDebugLoc();
968 unsigned RetVals = TheCall->getNumRetVals();
969
970 // If call does not have anything to return
971 // then do nothing and go back.
972 if (RetVals == 0)
973 return Chain;
974
975 // Call has something to return
976 std::vector<SDValue> ResultVals;
977 SDValue LoadRet;
978
979 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
980 for(unsigned i=0;i<RetVals;i++) {
981 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
982 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
983 InFlag);
984 InFlag = getOutFlag(LoadRet);
985 Chain = getChain(LoadRet);
986 ResultVals.push_back(LoadRet);
987 }
988 ResultVals.push_back(Chain);
989 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
990 return Res;
991}
992
993SDValue PIC16TargetLowering::
994LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress,
995 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000996 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000997 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000998 // Currently handling primitive types only. They will come in
999 // i8 parts
1000 unsigned RetVals = TheCall->getNumRetVals();
1001
1002 std::vector<SDValue> ResultVals;
1003
1004 // Return immediately if the return type is void
1005 if (RetVals == 0)
1006 return Chain;
1007
1008 // Call has something to return
1009
1010 // Legalize the address before use
1011 SDValue LdLo, LdHi;
1012 unsigned LdOffset;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001013 LegalizeAddress(FrameAddress, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001014
1015 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1016 SDValue LoadRet;
1017
1018 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1019
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001020 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001021 DAG.getConstant(LdOffset + Offset, MVT::i8),
1022 InFlag);
1023
1024 InFlag = getOutFlag(LoadRet);
1025
1026 Chain = getChain(LoadRet);
1027 Offset++;
1028 ResultVals.push_back(LoadRet);
1029 }
1030
1031 // To return use MERGE_VALUES
1032 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001033 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001034 return Res;
1035}
1036
1037SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001038 SDValue Chain = Op.getOperand(0);
1039 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001040
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001041 if (Op.getNumOperands() == 1) // return void
1042 return Op;
1043
1044 // return should have odd number of operands
1045 if ((Op.getNumOperands() % 2) == 0 ) {
1046 assert(0 && "Do not know how to return this many arguments!");
1047 abort();
1048 }
1049
1050 // Number of values to return
1051 unsigned NumRet = (Op.getNumOperands() / 2);
1052
1053 // Function returns value always on stack with the offset starting
1054 // from 0
1055 MachineFunction &MF = DAG.getMachineFunction();
1056 const Function *F = MF.getFunction();
1057 std::string FuncName = F->getName();
1058
1059 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
1060 sprintf(tmpName, "%s.frame", FuncName.c_str());
1061 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1062 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1063 SDValue BS = DAG.getConstant(1, MVT::i8);
1064 SDValue RetVal;
1065 for(unsigned i=0;i<NumRet; ++i) {
1066 RetVal = Op.getNode()->getOperand(2*i + 1);
1067 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1068 ES, BS,
1069 DAG.getConstant (i, MVT::i8));
1070
1071 }
1072 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001073}
1074
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001075// CALL node may have some operands non-legal to PIC16. Generate new CALL
1076// node with all the operands legal.
1077// Currently only Callee operand of the CALL node is non-legal. This function
1078// legalizes the Callee operand and uses all other operands as are to generate
1079// new CALL node.
1080
1081SDValue PIC16TargetLowering::LegalizeCALL(SDValue Op, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001082 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1083 SDValue Chain = TheCall->getChain();
1084 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001085 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001086 unsigned i =0;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001087
1088 assert(Callee.getValueType() == MVT::i16 &&
1089 "Don't know how to legalize this call node!!!");
1090 assert(Callee.getOpcode() == ISD::BUILD_PAIR &&
1091 "Don't know how to legalize this call node!!!");
1092
1093 if (isDirectAddress(Callee)) {
1094 // Come here for direct calls
1095 Callee = Callee.getOperand(0).getOperand(0);
1096 } else {
1097 // Come here for indirect calls
1098 SDValue Lo, Hi;
1099 // Indirect addresses. Get the hi and lo parts of ptr.
1100 GetExpandedParts(Callee, DAG, Lo, Hi);
1101 // Connect Lo and Hi parts of the callee with the PIC16Connect
1102 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1103 }
1104 std::vector<SDValue> Ops;
1105 Ops.push_back(Chain);
1106 Ops.push_back(Callee);
1107
1108 // Add the call arguments and their flags
1109 unsigned NumArgs = TheCall->getNumArgs();
1110 for(i=0;i<NumArgs;i++) {
1111 Ops.push_back(TheCall->getArg(i));
1112 Ops.push_back(TheCall->getArgFlagsVal(i));
1113 }
1114 std::vector<MVT> NodeTys;
1115 unsigned NumRets = TheCall->getNumRetVals();
1116 for(i=0;i<NumRets;i++)
1117 NodeTys.push_back(TheCall->getRetValType(i));
1118
1119 // Return a Chain as well
1120 NodeTys.push_back(MVT::Other);
1121
1122 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1123 // Generate new call with all the operands legal
1124 return DAG.getCall(TheCall->getCallingConv(), dl,
1125 TheCall->isVarArg(), TheCall->isTailCall(),
1126 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
1127}
1128
1129void PIC16TargetLowering::
1130GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1131 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1132 SelectionDAG &DAG) {
1133 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1134 && "Don't know what to do of such callee!!");
1135 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1136 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1137 Chain = getChain(SeqStart);
1138 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1139
1140 // Get the Lo and Hi part of code address
1141 SDValue Lo = Callee.getOperand(0);
1142 SDValue Hi = Callee.getOperand(1);
1143
1144 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1145 Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Hi);
1146 // Use the Lo part as is and make CALLW
1147 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1148 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1149 OperFlag);
1150 Chain = getChain(Call);
1151 OperFlag = getOutFlag(Call);
1152 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1153 OperFlag);
1154 Chain = getChain(SeqEnd);
1155 OperFlag = getOutFlag(SeqEnd);
1156
1157 // Low part of Data Address
1158 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1159
1160 // Make the second call.
1161 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1162 Chain = getChain(SeqStart);
1163 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1164
1165 // Add 1 to Lo part for the second code word.
1166 Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, Lo, DAG.getConstant(1, MVT::i8));
1167 // Use new Lo to make another CALLW
1168 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1169 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1170 Chain = getChain(Call);
1171 OperFlag = getOutFlag(Call);
1172 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1173 OperFlag);
1174 Chain = getChain(SeqEnd);
1175 OperFlag = getOutFlag(SeqEnd);
1176 // Hi part of Data Address
1177 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1178}
1179
1180
1181SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
1182 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1183 SDValue Chain = TheCall->getChain();
1184 SDValue Callee = TheCall->getCallee();
1185 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001186 if (Callee.getValueType() == MVT::i16 &&
1187 Callee.getOpcode() == ISD::BUILD_PAIR) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001188 // Control should come here only from TypeLegalizer for lowering
1189
1190 // Legalize the non-legal arguments of call and return the
1191 // new call with legal arguments.
1192 return LegalizeCALL(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001193 }
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001194 // Control should come here from Legalize DAG.
1195 // Here all the operands of CALL node should be legal.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001196
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001197 // If this is an indirect call then to pass the arguments
1198 // and read the return value back, we need the data address
1199 // of the function being called.
1200 // To get the data address two more calls need to be made.
1201
1202 // The flag to track if this is a direct or indirect call.
1203 bool IsDirectCall = true;
1204 unsigned RetVals = TheCall->getNumRetVals();
1205 unsigned NumArgs = TheCall->getNumArgs();
1206
1207 SDValue DataAddr_Lo, DataAddr_Hi;
1208 if (Callee.getOpcode() == PIC16ISD::PIC16Connect) {
1209 IsDirectCall = false; // This is indirect call
1210 // Read DataAddress only if we have to pass arguments or
1211 // read return value.
1212 if ((RetVals > 0) || (NumArgs > 0))
1213 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1214 }
1215
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001216 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1217
1218 // Start the call sequence.
1219 // Carring the Constant 0 along the CALLSEQSTART
1220 // because there is nothing else to carry.
1221 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1222 Chain = getChain(SeqStart);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001223 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1224 std::string Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001225
1226 // For any direct call - callee will be GlobalAddressNode or
1227 // ExternalSymbol
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001228 SDValue ArgLabel, RetLabel;
1229 if (IsDirectCall) {
1230 // Considering the GlobalAddressNode case here.
1231 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1232 GlobalValue *GV = G->getGlobal();
1233 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1234 Name = G->getGlobal()->getName();
1235 } else {// Considering the ExternalSymbol case here
1236 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1237 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1238 Name = ES->getSymbol();
1239 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001240
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001241 // Label for argument passing
1242 char *argFrame = new char [strlen(Name.c_str()) + 8];
1243 sprintf(argFrame, "%s.args", Name.c_str());
1244 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001245
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001246 // Label for reading return value
1247 char *retName = new char [strlen(Name.c_str()) + 8];
1248 sprintf(retName, "%s.retval", Name.c_str());
1249 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1250 } else {
1251 // if indirect call
1252 SDValue CodeAddr_Lo = Callee.getOperand(0);
1253 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001254
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001255 CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1256 DAG.getConstant(2, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001257
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001258 // move Hi part in PCLATH
1259 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1260 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1261 CodeAddr_Hi);
1262 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001263
1264 // Pass the argument to function before making the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001265 SDValue CallArgs;
1266 if (IsDirectCall) {
1267 CallArgs = LowerDirectCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1268 Chain = getChain(CallArgs);
1269 OperFlag = getOutFlag(CallArgs);
1270 } else {
1271 CallArgs = LowerIndirectCallArguments(Op, Chain, OperFlag, DataAddr_Lo,
1272 DataAddr_Hi, DAG);
1273 Chain = getChain(CallArgs);
1274 OperFlag = getOutFlag(CallArgs);
1275 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001276
1277 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001278 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001279 OperFlag);
1280 Chain = getChain(PICCall);
1281 OperFlag = getOutFlag(PICCall);
1282
1283
1284 // Carrying the Constant 0 along the CALLSEQSTART
1285 // because there is nothing else to carry.
1286 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1287 OperFlag);
1288 Chain = getChain(SeqEnd);
1289 OperFlag = getOutFlag(SeqEnd);
1290
1291 // Lower the return value reading after the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001292 if (IsDirectCall)
1293 return LowerDirectCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1294 else
1295 return LowerIndirectCallReturn(Op, Chain, OperFlag, DataAddr_Lo,
1296 DataAddr_Hi, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001297}
1298
1299bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1300 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1301 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1302 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1303 return true;
1304 return false;
1305}
1306
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001307// NeedToConvertToMemOp - Returns true if one of the operands of the
1308// operation 'Op' needs to be put into memory. Also returns the
1309// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1310// no instruction that can operation on two registers. Most insns take
1311// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001312bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001313 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001314 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1315 Op.getOperand(1).getOpcode() == ISD::Constant)
1316 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001317
1318 // Return false if one of the operands is already a direct
1319 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001320 if (isDirectLoad(Op.getOperand(0))) {
1321 if (Op.getOperand(0).hasOneUse())
1322 return false;
1323 else
1324 MemOp = 0;
1325 }
1326 if (isDirectLoad(Op.getOperand(1))) {
1327 if (Op.getOperand(1).hasOneUse())
1328 return false;
1329 else
1330 MemOp = 1;
1331 }
1332 return true;
1333}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001334
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001335// LowerBinOp - Lower a commutative binary operation that does not
1336// affect status flag carry.
1337SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001338 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001339
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001340 // We should have handled larger operands in type legalizer itself.
1341 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001342
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001343 unsigned MemOp = 1;
1344 if (NeedToConvertToMemOp(Op, MemOp)) {
1345 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001346 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001347
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001348 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001349 NewVal);
1350 }
1351 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001352 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001353 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001354}
1355
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001356// LowerADD - Lower all types of ADD operations including the ones
1357// that affects carry.
1358SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001359 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001360 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001361 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001362 unsigned MemOp = 1;
1363 if (NeedToConvertToMemOp(Op, MemOp)) {
1364 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001365 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001366
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001367 // ADDC and ADDE produces two results.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001368 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001369
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001370 // ADDE has three operands, the last one is a flag.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001371 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001372 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1373 NewVal, Op.getOperand(2));
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001374 // ADDC has two operands.
1375 else if (Op.getOpcode() == ISD::ADDC)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001376 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1377 NewVal);
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001378 // ADD it is. It produces only one result.
1379 else
1380 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1381 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001382 }
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001383 else if (Op.getOpcode() == ISD::ADD)
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001384 return Op;
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001385 else
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001386 return SDValue();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001387}
1388
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001389SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001390 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001391 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001392 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001393
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001394 // Nothing to do if the first operand is already a direct load and it has
1395 // only one use.
1396 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001397 return SDValue();
1398
1399 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001400 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001401
1402 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001403 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001404 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001405 Op.getOperand(2));
1406 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001407 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001408}
1409
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001410// LowerFORMAL_ARGUMENTS - Argument values are loaded from the
1411// <fname>.args + offset. All arguments are already broken to leaglized
1412// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001413
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001414SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001415 SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001416 SmallVector<SDValue, 8> ArgValues;
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001417 unsigned NumArgVals = Op.getNode()->getNumValues() - 1;
Dale Johannesene8d72302009-02-06 23:05:02 +00001418 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001419 SDValue Chain = Op.getOperand(0); // Formal arguments' chain
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001420
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001421
1422 // Reset the map of FI and TmpOffset
1423 ResetTmpOffsetMap();
1424 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001425 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001426 const Function *F = MF.getFunction();
1427 std::string FuncName = F->getName();
1428
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001429 // Create the <fname>.args external symbol.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001430 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
1431 sprintf(tmpName, "%s.args", FuncName.c_str());
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001432 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001433
1434 // Load arg values from the label + offset.
1435 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001436 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001437 for (unsigned i = 0; i < NumArgVals ; ++i) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001438 SDValue Offset = DAG.getConstant(i, MVT::i8);
1439 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001440 Offset);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001441 Chain = getChain(PICLoad);
1442 ArgValues.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001443 }
1444
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001445 // Return a MERGE_VALUE node.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001446 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001447 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001448 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001449}
1450
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001451// Perform DAGCombine of PIC16Load.
1452// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001453SDValue PIC16TargetLowering::
1454PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1455 SelectionDAG &DAG = DCI.DAG;
1456 SDValue Chain = N->getOperand(0);
1457 if (N->hasNUsesOfValue(0, 0)) {
1458 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1459 }
1460 return SDValue();
1461}
1462
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001463// For all the functions with arguments some STORE nodes are generated
1464// that store the argument on the frameindex. However in PIC16 the arguments
1465// are passed on stack only. Therefore these STORE nodes are redundant.
1466// To remove these STORE nodes will be removed in PerformStoreCombine
1467//
1468// Currently this function is doint nothing and will be updated for removing
1469// unwanted store operations
1470SDValue PIC16TargetLowering::
1471PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001472 return SDValue(N, 0);
1473 /*
1474 // Storing an undef value is of no use, so remove it
1475 if (isStoringUndef(N, Chain, DAG)) {
1476 return Chain; // remove the store and return the chain
1477 }
1478 //else everything is ok.
1479 return SDValue(N, 0);
1480 */
1481}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001482
1483SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1484 DAGCombinerInfo &DCI) const {
1485 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001486 case ISD::STORE:
1487 return PerformStoreCombine(N, DCI);
1488 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001489 return PerformPIC16LoadCombine(N, DCI);
1490 }
1491 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001492}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001493
1494static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1495 switch (CC) {
1496 default: assert(0 && "Unknown condition code!");
1497 case ISD::SETNE: return PIC16CC::NE;
1498 case ISD::SETEQ: return PIC16CC::EQ;
1499 case ISD::SETGT: return PIC16CC::GT;
1500 case ISD::SETGE: return PIC16CC::GE;
1501 case ISD::SETLT: return PIC16CC::LT;
1502 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001503 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001504 case ISD::SETULE: return PIC16CC::LE;
1505 case ISD::SETUGE: return PIC16CC::GE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001506 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001507 }
1508}
1509
1510// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1511// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1512static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1513 ISD::CondCode CC, unsigned &SPCC) {
1514 if (isa<ConstantSDNode>(RHS) &&
1515 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1516 CC == ISD::SETNE &&
1517 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1518 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1519 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1520 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1521 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1522 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1523 SDValue CMPCC = LHS.getOperand(3);
1524 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1525 LHS = CMPCC.getOperand(0);
1526 RHS = CMPCC.getOperand(1);
1527 }
1528}
1529
1530// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1531SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1532 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001533 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001534 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1535
1536 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1537 // i.e. a < 12 can be rewritten as 12 > a.
1538 if (RHS.getOpcode() == ISD::Constant) {
1539
1540 SDValue Tmp = LHS;
1541 LHS = RHS;
1542 RHS = Tmp;
1543
1544 switch (CondCode) {
1545 default: break;
1546 case PIC16CC::LT:
1547 CondCode = PIC16CC::GT;
1548 break;
1549 case PIC16CC::GT:
1550 CondCode = PIC16CC::LT;
1551 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001552 case PIC16CC::ULT:
1553 CondCode = PIC16CC::UGT;
1554 break;
1555 case PIC16CC::UGT:
1556 CondCode = PIC16CC::ULT;
1557 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001558 case PIC16CC::GE:
1559 CondCode = PIC16CC::LE;
1560 break;
1561 case PIC16CC::LE:
1562 CondCode = PIC16CC::GE;
1563 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001564 case PIC16CC::ULE:
1565 CondCode = PIC16CC::UGE;
1566 break;
1567 case PIC16CC::UGE:
1568 CondCode = PIC16CC::ULE;
1569 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001570 }
1571 }
1572
1573 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001574
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001575 // These are signed comparisons.
1576 SDValue Mask = DAG.getConstant(128, MVT::i8);
1577 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001578 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1579 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001580 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001581
1582 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001583 // We can use a subtract operation to set the condition codes. But
1584 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001585 // Nothing to do if the first operand is already a valid type (direct load
1586 // for subwf and literal for sublw) and it is used by this operation only.
1587 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1588 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001589 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001590
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001591 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001592 LHS = ConvertToMemOperand (LHS, DAG, dl);
1593 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001594}
1595
1596
1597SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1598 SDValue LHS = Op.getOperand(0);
1599 SDValue RHS = Op.getOperand(1);
1600 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1601 SDValue TrueVal = Op.getOperand(2);
1602 SDValue FalseVal = Op.getOperand(3);
1603 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001604 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001605
1606 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1607 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1608 // i.e.
1609 // A setcc: lhs, rhs, cc is expanded by llvm to
1610 // select_cc: result of setcc, 0, 1, 0, setne
1611 // We can think of it as:
1612 // select_cc: lhs, rhs, 1, 0, cc
1613 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1614 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1615
1616 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001617 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001618
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001619 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001620 FalseVal, PIC16CC, Cmp.getValue(1));
1621}
1622
1623MachineBasicBlock *
1624PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001625 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001626 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1627 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001628 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001629
1630 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1631 // control-flow pattern. The incoming instruction knows the destination vreg
1632 // to set, the condition code register to branch on, the true/false values to
1633 // select between, and a branch opcode to use.
1634 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1635 MachineFunction::iterator It = BB;
1636 ++It;
1637
1638 // thisMBB:
1639 // ...
1640 // TrueVal = ...
1641 // [f]bCC copy1MBB
1642 // fallthrough --> copy0MBB
1643 MachineBasicBlock *thisMBB = BB;
1644 MachineFunction *F = BB->getParent();
1645 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1646 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001647 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001648 F->insert(It, copy0MBB);
1649 F->insert(It, sinkMBB);
1650
1651 // Update machine-CFG edges by transferring all successors of the current
1652 // block to the new block which will contain the Phi node for the select.
1653 sinkMBB->transferSuccessors(BB);
1654 // Next, add the true and fallthrough blocks as its successors.
1655 BB->addSuccessor(copy0MBB);
1656 BB->addSuccessor(sinkMBB);
1657
1658 // copy0MBB:
1659 // %FalseValue = ...
1660 // # fallthrough to sinkMBB
1661 BB = copy0MBB;
1662
1663 // Update machine-CFG edges
1664 BB->addSuccessor(sinkMBB);
1665
1666 // sinkMBB:
1667 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1668 // ...
1669 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001670 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001671 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1672 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1673
1674 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1675 return BB;
1676}
1677
1678
1679SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1680 SDValue Chain = Op.getOperand(0);
1681 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1682 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1683 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1684 SDValue Dest = Op.getOperand(4); // BB to jump to
1685 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001686 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001687
1688 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1689 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1690 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1691 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1692
1693 // Get the Compare insn and condition code.
1694 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001695 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001696
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001697 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001698 Cmp.getValue(1));
1699}
1700
1701