blob: 5996d886ad772bec3c3edb2dcb9b9c74530d3e57 [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
156unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI) {
157 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;
164 return TmpSize++;
165}
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000166
167// To extract chain value from the SDValue Nodes
168// This function will help to maintain the chain extracting
169// code at one place. In case of any change in future it will
170// help maintain the code.
171static SDValue getChain(SDValue &Op) {
172 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
173
174 // If the last value returned in Flag then the chain is
175 // second last value returned.
176 if (Chain.getValueType() == MVT::Flag)
177 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
178
179 // All nodes may not produce a chain. Therefore following assert
180 // verifies that the node is returning a chain only.
181 assert (Chain.getValueType() == MVT::Other
182 && "Node does not have a chain");
183
184 return Chain;
185}
186
187/// PopulateResults - Helper function to LowerOperation.
188/// If a node wants to return multiple results after lowering,
189/// it stuffs them into an array of SDValue called Results.
190
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000191static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
192 if (N.getOpcode() == ISD::MERGE_VALUES) {
193 int NumResults = N.getNumOperands();
194 for( int i = 0; i < NumResults; i++)
195 Results.push_back(N.getOperand(i));
196 }
197 else
198 Results.push_back(N);
199}
200
201MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
202 return MVT::i8;
203}
204
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000205/// The type legalizer framework of generating legalizer can generate libcalls
206/// only when the operand/result types are illegal.
207/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
208/// For example an arithmetic right shift. These functions are used to lower
209/// such operations that generate libcall for legal types.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000210
211void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000212PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000213 const char *Name) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000214 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000215}
216
217const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000218PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000219 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000220}
221
222SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000223PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000224 MVT RetVT, const SDValue *Ops,
225 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000226 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000227
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000228 TargetLowering::ArgListTy Args;
229 Args.reserve(NumOps);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000230
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000231 TargetLowering::ArgListEntry Entry;
232 for (unsigned i = 0; i != NumOps; ++i) {
233 Entry.Node = Ops[i];
234 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
235 Entry.isSExt = isSigned;
236 Entry.isZExt = !isSigned;
237 Args.push_back(Entry);
238 }
239 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000240
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000241 const Type *RetTy = RetVT.getTypeForMVT();
242 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000243 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000244 false, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000245
246 return CallInfo.first;
247}
248
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000249const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
250 switch (Opcode) {
251 default: return NULL;
252 case PIC16ISD::Lo: return "PIC16ISD::Lo";
253 case PIC16ISD::Hi: return "PIC16ISD::Hi";
254 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
255 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000256 case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
257 case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000258 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
259 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000260 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000261 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000262 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000263 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000264 case PIC16ISD::BCF: return "PIC16ISD::BCF";
265 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
266 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
267 case PIC16ISD::RLF: return "PIC16ISD::RLF";
268 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000269 case PIC16ISD::CALL: return "PIC16ISD::CALL";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000270 case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000271 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
272 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
273 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000274 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000275 }
276}
277
Duncan Sands1607f052008-12-01 11:39:25 +0000278void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
279 SmallVectorImpl<SDValue>&Results,
280 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000281
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000282 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000283 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000284 Results.push_back(ExpandGlobalAddress(N, DAG));
285 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000286 case ISD::ExternalSymbol:
287 Results.push_back(ExpandExternalSymbol(N, DAG));
288 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000289 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000290 Results.push_back(ExpandStore(N, DAG));
291 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000292 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000293 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000294 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000295 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000296 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000297 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000298 case ISD::FrameIndex:
299 Results.push_back(ExpandFrameIndex(N, DAG));
300 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000301 default:
302 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000303 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000304 }
305}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000306
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000307SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
308
309 // Currently handling FrameIndex of size MVT::i16 only
310 // One example of this scenario is when return value is written on
311 // FrameIndex#0
312
313 if (N->getValueType(0) != MVT::i16)
314 return SDValue();
315
316 // Expand the FrameIndex into ExternalSymbol and a Constant node
317 // The constant will represent the frame index number
318 // Get the current function frame
319 MachineFunction &MF = DAG.getMachineFunction();
320 const Function *Func = MF.getFunction();
321 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000322
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000323 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000324 // FIXME there isn't really debug info here
325 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000326 int Index = FR->getIndex();
327
328 SDValue FI[2];
329 FI[0] = DAG.getTargetFrameIndex(Index, MVT::i8);
330 FI[1] = DAG.getTargetFrameIndex(Index + 1, MVT::i8);
Dale Johannesende064702009-02-06 21:50:26 +0000331 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), FI[0], FI[1]);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000332}
333
334
335SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000336 StoreSDNode *St = cast<StoreSDNode>(N);
337 SDValue Chain = St->getChain();
338 SDValue Src = St->getValue();
339 SDValue Ptr = St->getBasePtr();
340 MVT ValueType = Src.getValueType();
341 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000342 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000343
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000344 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000345 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000346
347 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000348 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000349 PtrLo, PtrHi,
350 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000351 }
352 else if (ValueType == MVT::i16) {
353 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
354 SDValue SrcLo, SrcHi;
355 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
356 SDValue ChainLo = Chain, ChainHi = Chain;
357 if (Chain.getOpcode() == ISD::TokenFactor) {
358 ChainLo = Chain.getOperand(0);
359 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000360 }
Dale Johannesende064702009-02-06 21:50:26 +0000361 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000362 ChainLo,
363 SrcLo, PtrLo, PtrHi,
364 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000365
Dale Johannesende064702009-02-06 21:50:26 +0000366 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000367 SrcHi, PtrLo, PtrHi,
368 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000369
Dale Johannesende064702009-02-06 21:50:26 +0000370 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000371 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000372 }
373 else if (ValueType == MVT::i32) {
374 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
375 SDValue SrcLo, SrcHi;
376 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000377
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000378 // Get the expanded parts of each of SrcLo and SrcHi.
379 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
380 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
381 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000382
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000383 SDValue ChainLo = Chain, ChainHi = Chain;
384 if (Chain.getOpcode() == ISD::TokenFactor) {
385 ChainLo = Chain.getOperand(0);
386 ChainHi = Chain.getOperand(1);
387 }
388 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
389 ChainHi2 = ChainHi;
390 if (ChainLo.getOpcode() == ISD::TokenFactor) {
391 ChainLo1 = ChainLo.getOperand(0);
392 ChainLo2 = ChainLo.getOperand(1);
393 }
394 if (ChainHi.getOpcode() == ISD::TokenFactor) {
395 ChainHi1 = ChainHi.getOperand(0);
396 ChainHi2 = ChainHi.getOperand(1);
397 }
Dale Johannesende064702009-02-06 21:50:26 +0000398 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000399 ChainLo1,
400 SrcLo1, PtrLo, PtrHi,
401 DAG.getConstant (0 + StoreOffset, MVT::i8));
402
Dale Johannesende064702009-02-06 21:50:26 +0000403 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000404 SrcLo2, PtrLo, PtrHi,
405 DAG.getConstant (1 + StoreOffset, MVT::i8));
406
Dale Johannesende064702009-02-06 21:50:26 +0000407 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000408 SrcHi1, PtrLo, PtrHi,
409 DAG.getConstant (2 + StoreOffset, MVT::i8));
410
Dale Johannesende064702009-02-06 21:50:26 +0000411 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000412 SrcHi2, PtrLo, PtrHi,
413 DAG.getConstant (3 + StoreOffset, MVT::i8));
414
Dale Johannesende064702009-02-06 21:50:26 +0000415 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
416 getChain(Store1), getChain(Store2));
417 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
418 getChain(Store3), getChain(Store4));
419 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000420
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000421 }
422 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000423 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000424 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000425 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000426}
427
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000428SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
429{
430 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000431 // FIXME there isn't really debug info here
432 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000433
434 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
435
Dale Johannesende064702009-02-06 21:50:26 +0000436 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES);
437 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000438
Dale Johannesende064702009-02-06 21:50:26 +0000439 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000440}
441
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000442// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000443SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000444 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000445 // FIXME there isn't really debug info here
446 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000447
448 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
449 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000450
Dale Johannesende064702009-02-06 21:50:26 +0000451 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA);
452 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000453
Dale Johannesende064702009-02-06 21:50:26 +0000454 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000455}
456
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000457bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
458 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000459
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000460 if (Op.getOpcode() == ISD::BUILD_PAIR) {
461 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
462 return true;
463 }
464 return false;
465}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000466
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000467// Return true if DirectAddress is in ROM_SPACE
468bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000469
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000470 // RomAddress is a GlobalAddress in ROM_SPACE_
471 // If the Op is not a GlobalAddress return NULL without checking
472 // anything further.
473 if (!isDirectAddress(Op))
474 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000475
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000476 // Its a GlobalAddress.
477 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
478 SDValue TGA = Op.getOperand(0).getOperand(0);
479 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
480 const Type *ValueType = GSDN->getGlobal()->getType();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000481
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000482 if (!isa<PointerType>(ValueType)) {
483 assert(0 && "TGA must be of a PointerType");
484 }
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000485
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000486 int AddrSpace = dyn_cast<PointerType>(ValueType)->getAddressSpace();
487 if (AddrSpace == PIC16ISD::ROM_SPACE)
488 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000489
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000490 // Any other address space return it false
491 return false;
492}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000493
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000494
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000495// GetExpandedParts - This function is on the similiar lines as
496// the GetExpandedInteger in type legalizer is. This returns expanded
497// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000498
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000499void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
500 SDValue &Lo, SDValue &Hi) {
501 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000502 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000503 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000504
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000505 // Extract the lo component.
506 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
507 DAG.getConstant(0, MVT::i8));
508
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000509 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000510 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
511 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000512}
513
514// Legalize FrameIndex into ExternalSymbol and offset.
515void
516PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
517 SDValue &ES, int &Offset) {
518
519 MachineFunction &MF = DAG.getMachineFunction();
520 const Function *Func = MF.getFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000521 MachineFrameInfo *MFI = MF.getFrameInfo();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000522 const std::string Name = Func->getName();
523
524 char *tmpName = new char [strlen(Name.c_str()) + 8];
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000525 sprintf(tmpName, "%s.frame", Name.c_str());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000526 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
527 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000528
529 // FrameIndices are not stack offsets. But they represent the request
530 // for space on stack. That space requested may be more than one byte.
531 // Therefore, to calculate the stack offset that a FrameIndex aligns
532 // with, we need to traverse all the FrameIndices available earlier in
533 // the list and add their requested size.
534 unsigned FIndex = FR->getIndex();
535 Offset = 0;
536 for (unsigned i=0; i<FIndex ; ++i) {
537 Offset += MFI->getObjectSize(i);
538 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000539
540 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000541}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000542
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000543// This function legalizes the PIC16 Addresses. If the Pointer is
544// -- Direct address variable residing
545// --> then a Banksel for that variable will be created.
546// -- Rom variable
547// --> then it will be treated as an indirect address.
548// -- Indirect address
549// --> then the address will be loaded into FSR
550// -- ADD with constant operand
551// --> then constant operand of ADD will be returned as Offset
552// and non-constant operand of ADD will be treated as pointer.
553// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000554
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000555void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
556 SDValue &Lo, SDValue &Hi,
557 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000558
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000559 // Offset, by default, should be 0
560 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000561
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000562 // If the pointer is ADD with constant,
563 // return the constant value as the offset
564 if (Ptr.getOpcode() == ISD::ADD) {
565 SDValue OperLeft = Ptr.getOperand(0);
566 SDValue OperRight = Ptr.getOperand(1);
567 if (OperLeft.getOpcode() == ISD::Constant) {
568 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
569 Ptr = OperRight;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000570 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000571 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000572 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000573 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000574 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000575
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000576 // If the pointer is Type i8 and an external symbol
577 // then treat it as direct address.
578 // One example for such case is storing and loading
579 // from function frame during a call
580 if (Ptr.getValueType() == MVT::i8) {
581 switch (Ptr.getOpcode()) {
582 case ISD::TargetExternalSymbol:
583 Lo = Ptr;
584 Hi = DAG.getConstant(1, MVT::i8);
585 return;
586 }
587 }
588
589 if (Ptr.getOpcode() == ISD::BUILD_PAIR &&
590 Ptr.getOperand(0).getOpcode() == ISD::TargetFrameIndex) {
591
592 int FrameOffset;
593 LegalizeFrameIndex(Ptr.getOperand(0), DAG, Lo, FrameOffset);
594 Hi = DAG.getConstant(1, MVT::i8);
595 Offset += FrameOffset;
596 return;
597 }
598
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000599 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
600 // Direct addressing case for RAM variables. The Hi part is constant
601 // and the Lo part is the TGA itself.
602 Lo = Ptr.getOperand(0).getOperand(0);
603
604 // For direct addresses Hi is a constant. Value 1 for the constant
605 // signifies that banksel needs to generated for it. Value 0 for
606 // the constant signifies that banksel does not need to be generated
607 // for it. Mark it as 1 now and optimize later.
608 Hi = DAG.getConstant(1, MVT::i8);
609 return;
610 }
611
612 // Indirect addresses. Get the hi and lo parts of ptr.
613 GetExpandedParts(Ptr, DAG, Lo, Hi);
614
615 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000616 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
617 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000618
619 return;
620}
621
Duncan Sands1607f052008-12-01 11:39:25 +0000622SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000623 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
624 SDValue Chain = LD->getChain();
625 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000626 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000627
628 SDValue Load, Offset;
629 SDVTList Tys;
630 MVT VT, NewVT;
631 SDValue PtrLo, PtrHi;
632 unsigned LoadOffset;
633
634 // Legalize direct/indirect addresses. This will give the lo and hi parts
635 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000636 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000637
638 // Load from the pointer (direct address or FSR)
639 VT = N->getValueType(0);
640 unsigned NumLoads = VT.getSizeInBits() / 8;
641 std::vector<SDValue> PICLoads;
642 unsigned iter;
643 MVT MemVT = LD->getMemoryVT();
644 if(ISD::isNON_EXTLoad(N)) {
645 for (iter=0; iter<NumLoads ; ++iter) {
646 // Add the pointer offset if any
647 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
648 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000649 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000650 Offset);
651 PICLoads.push_back(Load);
652 }
653 } else {
654 // If it is extended load then use PIC16Load for Memory Bytes
655 // and for all extended bytes perform action based on type of
656 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
657
658
659 // For extended loads this is the memory value type
660 // i.e. without any extension
661 MVT MemVT = LD->getMemoryVT();
662 unsigned MemBytes = MemVT.getSizeInBits() / 8;
663 unsigned ExtdBytes = VT.getSizeInBits() / 8;
664 Offset = DAG.getConstant(LoadOffset, MVT::i8);
665
666 Tys = DAG.getVTList(MVT::i8, MVT::Other);
667 // For MemBytes generate PIC16Load with proper offset
668 for (iter=0; iter<MemBytes; ++iter) {
669 // Add the pointer offset if any
670 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000671 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000672 Offset);
673 PICLoads.push_back(Load);
674 }
675
676 // For SignExtendedLoad
677 if (ISD::isSEXTLoad(N)) {
678 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
679 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000680 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000681 DAG.getConstant(7, MVT::i8));
682 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
683 PICLoads.push_back(SRA);
684 }
685 } else if (ISD::isZEXTLoad(N)) {
686 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
687 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
688 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
689 PICLoads.push_back(ConstZero);
690 }
691 }
692 }
693 SDValue BP;
694
695 if (VT == MVT::i8) {
696 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000697 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000698 }
699 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000700 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000701 if (MemVT == MVT::i8)
702 Chain = getChain(PICLoads[0]);
703 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000704 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
705 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000706 } else if (VT == MVT::i32) {
707 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000708 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
709 PICLoads[0], PICLoads[1]);
710 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
711 PICLoads[2], PICLoads[3]);
712 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000713 if (MemVT == MVT::i8)
714 Chain = getChain(PICLoads[0]);
715 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000716 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
717 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000718 else {
719 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000720 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000721 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000722 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000723 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000724 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
725 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000726 }
727 }
728 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000729 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000730}
731
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000732SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
733 // We should have handled larger operands in type legalizer itself.
734 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
735
736 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000737 SDValue Value = N->getOperand(0);
738 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000739 PIC16ISD::PIC16Libcall CallCode;
740 switch (N->getOpcode()) {
741 case ISD::SRA:
742 CallCode = PIC16ISD::SRA_I8;
743 break;
744 case ISD::SHL:
745 CallCode = PIC16ISD::SLL_I8;
746 break;
747 case ISD::SRL:
748 CallCode = PIC16ISD::SRL_I8;
749 break;
750 default:
751 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000752 return SDValue();
753 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000754 SmallVector<SDValue, 2> Ops(2);
755 Ops[0] = Value;
756 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000757 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
758 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000759 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000760}
761
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000762void
763PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
764 SmallVectorImpl<SDValue>&Results,
765 SelectionDAG &DAG) {
766 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000767 SDValue Res;
768 unsigned i;
769 switch (Op.getOpcode()) {
770 case ISD::FORMAL_ARGUMENTS:
771 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
772 case ISD::LOAD:
773 Res = ExpandLoad(Op.getNode(), DAG); break;
774 case ISD::CALL:
775 Res = LowerCALL(Op, DAG); break;
776 default: {
777 // All other operations are handled in LowerOperation.
778 Res = LowerOperation(Op, DAG);
779 if (Res.getNode())
780 Results.push_back(Res);
781
782 return;
783 }
784 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000785
786 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000787 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000788 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000789 Results.push_back(SDValue(N, i));
790 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000791}
792
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000793SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
794 switch (Op.getOpcode()) {
795 case ISD::FORMAL_ARGUMENTS:
796 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000797 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000798 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000799 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000800 return LowerADD(Op, DAG);
801 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000802 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000803 case ISD::SUBE:
804 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000805 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000806 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000807 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000808 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000809 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000810 case ISD::SRA:
811 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000812 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000813 case ISD::OR:
814 case ISD::AND:
815 case ISD::XOR:
816 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000817 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000818 return LowerCALL(Op, DAG);
819 case ISD::RET:
820 return LowerRET(Op, DAG);
821 case ISD::BR_CC:
822 return LowerBR_CC(Op, DAG);
823 case ISD::SELECT_CC:
824 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000825 }
Dan Gohman475871a2008-07-27 21:46:04 +0000826 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000827}
828
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000829SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000830 SelectionDAG &DAG,
831 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000832 assert (Op.getValueType() == MVT::i8
833 && "illegal value type to store on stack.");
834
835 MachineFunction &MF = DAG.getMachineFunction();
836 const Function *Func = MF.getFunction();
837 const std::string FuncName = Func->getName();
838
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000839 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000840
841 // Put the value on stack.
842 // Get a stack slot index and convert to es.
843 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
844 sprintf(tmpName, "%s.tmp", FuncName.c_str());
845 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
846
847 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000848 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000849 DAG.getEntryNode(),
850 Op, ES,
851 DAG.getConstant (1, MVT::i8), // Banksel.
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000852 DAG.getConstant (GetTmpOffsetForFI(FI),
853 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000854
855 // Load the value from ES.
856 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000857 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000858 ES, DAG.getConstant (1, MVT::i8),
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000859 DAG.getConstant (GetTmpOffsetForFI(FI),
860 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000861
862 return Load.getValue(0);
863}
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000864
865SDValue PIC16TargetLowering::
866LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
867 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
868 SelectionDAG &DAG) {
869 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
870 unsigned NumOps = TheCall->getNumArgs();
871 DebugLoc dl = TheCall->getDebugLoc();
872
873 // If call has no arguments then do nothing and return.
874 if (NumOps == 0)
875 return Chain;
876
877 std::vector<SDValue> Ops;
878 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
879 SDValue Arg, StoreRet;
880 for (unsigned i=0; i<NumOps; i++) {
881 // Get the arguments
882 Arg = TheCall->getArg(i);
883 Ops.clear();
884 Ops.push_back(Chain);
885 Ops.push_back(Arg);
886 Ops.push_back(DataAddr_Lo);
887 Ops.push_back(DataAddr_Hi);
888 Ops.push_back(DAG.getConstant(i, MVT::i8));
889 Ops.push_back(InFlag);
890
891 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
892
893 Chain = getChain(StoreRet);
894 InFlag = getOutFlag(StoreRet);
895 }
896 return Chain;
897}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000898
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000899SDValue PIC16TargetLowering::
900LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress,
901 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000902 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
903 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +0000904 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000905 std::string Name;
906 SDValue Arg, StoreAt;
907 MVT ArgVT;
908 unsigned Size=0;
909 unsigned ArgCount=0;
910
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000911 // If call has no arguments then do nothing and return.
912 if (NumOps == 0)
913 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000914
915 // FIXME: This portion of code currently assumes only
916 // primitive types being passed as arguments.
917
918 // Legalize the address before use
919 SDValue PtrLo, PtrHi;
920 unsigned AddressOffset;
921 int StoreOffset = 0;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000922 LegalizeAddress(FrameAddress, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000923 SDValue StoreRet;
924
925 std::vector<SDValue> Ops;
926 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
927 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
928 // Get the argument
929 Arg = TheCall->getArg(i);
930
931 StoreOffset = (Offset + AddressOffset);
932
933 // Store the argument on frame
934
935 Ops.clear();
936 Ops.push_back(Chain);
937 Ops.push_back(Arg.getValue(0));
938 Ops.push_back(PtrLo);
939 Ops.push_back(PtrHi);
940 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
941 Ops.push_back(InFlag);
942
Dale Johannesene8d72302009-02-06 23:05:02 +0000943 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000944
945 Chain = getChain(StoreRet);
946 InFlag = getOutFlag(StoreRet);
947
948 // Update the frame offset to be used for next argument
949 ArgVT = Arg.getValueType();
950 Size = ArgVT.getSizeInBits();
951 Size = Size/8; // Calculate size in bytes
952 Offset += Size; // Increase the frame offset
953 }
954 return Chain;
955}
956
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000957SDValue PIC16TargetLowering::
958LowerIndirectCallReturn (SDValue Op, SDValue Chain, SDValue InFlag,
959 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
960 SelectionDAG &DAG) {
961 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
962 DebugLoc dl = TheCall->getDebugLoc();
963 unsigned RetVals = TheCall->getNumRetVals();
964
965 // If call does not have anything to return
966 // then do nothing and go back.
967 if (RetVals == 0)
968 return Chain;
969
970 // Call has something to return
971 std::vector<SDValue> ResultVals;
972 SDValue LoadRet;
973
974 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
975 for(unsigned i=0;i<RetVals;i++) {
976 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
977 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
978 InFlag);
979 InFlag = getOutFlag(LoadRet);
980 Chain = getChain(LoadRet);
981 ResultVals.push_back(LoadRet);
982 }
983 ResultVals.push_back(Chain);
984 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
985 return Res;
986}
987
988SDValue PIC16TargetLowering::
989LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress,
990 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000991 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000992 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000993 // Currently handling primitive types only. They will come in
994 // i8 parts
995 unsigned RetVals = TheCall->getNumRetVals();
996
997 std::vector<SDValue> ResultVals;
998
999 // Return immediately if the return type is void
1000 if (RetVals == 0)
1001 return Chain;
1002
1003 // Call has something to return
1004
1005 // Legalize the address before use
1006 SDValue LdLo, LdHi;
1007 unsigned LdOffset;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001008 LegalizeAddress(FrameAddress, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001009
1010 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1011 SDValue LoadRet;
1012
1013 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1014
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001015 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001016 DAG.getConstant(LdOffset + Offset, MVT::i8),
1017 InFlag);
1018
1019 InFlag = getOutFlag(LoadRet);
1020
1021 Chain = getChain(LoadRet);
1022 Offset++;
1023 ResultVals.push_back(LoadRet);
1024 }
1025
1026 // To return use MERGE_VALUES
1027 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001028 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001029 return Res;
1030}
1031
1032SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001033 SDValue Chain = Op.getOperand(0);
1034 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001035
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001036 if (Op.getNumOperands() == 1) // return void
1037 return Op;
1038
1039 // return should have odd number of operands
1040 if ((Op.getNumOperands() % 2) == 0 ) {
1041 assert(0 && "Do not know how to return this many arguments!");
1042 abort();
1043 }
1044
1045 // Number of values to return
1046 unsigned NumRet = (Op.getNumOperands() / 2);
1047
1048 // Function returns value always on stack with the offset starting
1049 // from 0
1050 MachineFunction &MF = DAG.getMachineFunction();
1051 const Function *F = MF.getFunction();
1052 std::string FuncName = F->getName();
1053
1054 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
1055 sprintf(tmpName, "%s.frame", FuncName.c_str());
1056 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1057 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1058 SDValue BS = DAG.getConstant(1, MVT::i8);
1059 SDValue RetVal;
1060 for(unsigned i=0;i<NumRet; ++i) {
1061 RetVal = Op.getNode()->getOperand(2*i + 1);
1062 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1063 ES, BS,
1064 DAG.getConstant (i, MVT::i8));
1065
1066 }
1067 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001068}
1069
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001070// CALL node may have some operands non-legal to PIC16. Generate new CALL
1071// node with all the operands legal.
1072// Currently only Callee operand of the CALL node is non-legal. This function
1073// legalizes the Callee operand and uses all other operands as are to generate
1074// new CALL node.
1075
1076SDValue PIC16TargetLowering::LegalizeCALL(SDValue Op, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001077 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1078 SDValue Chain = TheCall->getChain();
1079 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001080 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001081 unsigned i =0;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001082
1083 assert(Callee.getValueType() == MVT::i16 &&
1084 "Don't know how to legalize this call node!!!");
1085 assert(Callee.getOpcode() == ISD::BUILD_PAIR &&
1086 "Don't know how to legalize this call node!!!");
1087
1088 if (isDirectAddress(Callee)) {
1089 // Come here for direct calls
1090 Callee = Callee.getOperand(0).getOperand(0);
1091 } else {
1092 // Come here for indirect calls
1093 SDValue Lo, Hi;
1094 // Indirect addresses. Get the hi and lo parts of ptr.
1095 GetExpandedParts(Callee, DAG, Lo, Hi);
1096 // Connect Lo and Hi parts of the callee with the PIC16Connect
1097 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1098 }
1099 std::vector<SDValue> Ops;
1100 Ops.push_back(Chain);
1101 Ops.push_back(Callee);
1102
1103 // Add the call arguments and their flags
1104 unsigned NumArgs = TheCall->getNumArgs();
1105 for(i=0;i<NumArgs;i++) {
1106 Ops.push_back(TheCall->getArg(i));
1107 Ops.push_back(TheCall->getArgFlagsVal(i));
1108 }
1109 std::vector<MVT> NodeTys;
1110 unsigned NumRets = TheCall->getNumRetVals();
1111 for(i=0;i<NumRets;i++)
1112 NodeTys.push_back(TheCall->getRetValType(i));
1113
1114 // Return a Chain as well
1115 NodeTys.push_back(MVT::Other);
1116
1117 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1118 // Generate new call with all the operands legal
1119 return DAG.getCall(TheCall->getCallingConv(), dl,
1120 TheCall->isVarArg(), TheCall->isTailCall(),
1121 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
1122}
1123
1124void PIC16TargetLowering::
1125GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1126 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1127 SelectionDAG &DAG) {
1128 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1129 && "Don't know what to do of such callee!!");
1130 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1131 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1132 Chain = getChain(SeqStart);
1133 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1134
1135 // Get the Lo and Hi part of code address
1136 SDValue Lo = Callee.getOperand(0);
1137 SDValue Hi = Callee.getOperand(1);
1138
1139 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1140 Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Hi);
1141 // Use the Lo part as is and make CALLW
1142 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1143 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1144 OperFlag);
1145 Chain = getChain(Call);
1146 OperFlag = getOutFlag(Call);
1147 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1148 OperFlag);
1149 Chain = getChain(SeqEnd);
1150 OperFlag = getOutFlag(SeqEnd);
1151
1152 // Low part of Data Address
1153 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1154
1155 // Make the second call.
1156 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1157 Chain = getChain(SeqStart);
1158 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1159
1160 // Add 1 to Lo part for the second code word.
1161 Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, Lo, DAG.getConstant(1, MVT::i8));
1162 // Use new Lo to make another CALLW
1163 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1164 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1165 Chain = getChain(Call);
1166 OperFlag = getOutFlag(Call);
1167 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1168 OperFlag);
1169 Chain = getChain(SeqEnd);
1170 OperFlag = getOutFlag(SeqEnd);
1171 // Hi part of Data Address
1172 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1173}
1174
1175
1176SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
1177 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1178 SDValue Chain = TheCall->getChain();
1179 SDValue Callee = TheCall->getCallee();
1180 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001181 if (Callee.getValueType() == MVT::i16 &&
1182 Callee.getOpcode() == ISD::BUILD_PAIR) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001183 // Control should come here only from TypeLegalizer for lowering
1184
1185 // Legalize the non-legal arguments of call and return the
1186 // new call with legal arguments.
1187 return LegalizeCALL(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001188 }
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001189 // Control should come here from Legalize DAG.
1190 // Here all the operands of CALL node should be legal.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001191
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001192 // If this is an indirect call then to pass the arguments
1193 // and read the return value back, we need the data address
1194 // of the function being called.
1195 // To get the data address two more calls need to be made.
1196
1197 // The flag to track if this is a direct or indirect call.
1198 bool IsDirectCall = true;
1199 unsigned RetVals = TheCall->getNumRetVals();
1200 unsigned NumArgs = TheCall->getNumArgs();
1201
1202 SDValue DataAddr_Lo, DataAddr_Hi;
1203 if (Callee.getOpcode() == PIC16ISD::PIC16Connect) {
1204 IsDirectCall = false; // This is indirect call
1205 // Read DataAddress only if we have to pass arguments or
1206 // read return value.
1207 if ((RetVals > 0) || (NumArgs > 0))
1208 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1209 }
1210
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001211 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1212
1213 // Start the call sequence.
1214 // Carring the Constant 0 along the CALLSEQSTART
1215 // because there is nothing else to carry.
1216 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1217 Chain = getChain(SeqStart);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001218 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1219 std::string Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001220
1221 // For any direct call - callee will be GlobalAddressNode or
1222 // ExternalSymbol
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001223 SDValue ArgLabel, RetLabel;
1224 if (IsDirectCall) {
1225 // Considering the GlobalAddressNode case here.
1226 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1227 GlobalValue *GV = G->getGlobal();
1228 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1229 Name = G->getGlobal()->getName();
1230 } else {// Considering the ExternalSymbol case here
1231 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1232 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1233 Name = ES->getSymbol();
1234 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001235
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001236 // Label for argument passing
1237 char *argFrame = new char [strlen(Name.c_str()) + 8];
1238 sprintf(argFrame, "%s.args", Name.c_str());
1239 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001240
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001241 // Label for reading return value
1242 char *retName = new char [strlen(Name.c_str()) + 8];
1243 sprintf(retName, "%s.retval", Name.c_str());
1244 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1245 } else {
1246 // if indirect call
1247 SDValue CodeAddr_Lo = Callee.getOperand(0);
1248 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001249
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001250 CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1251 DAG.getConstant(2, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001252
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001253 // move Hi part in PCLATH
1254 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1255 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1256 CodeAddr_Hi);
1257 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001258
1259 // Pass the argument to function before making the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001260 SDValue CallArgs;
1261 if (IsDirectCall) {
1262 CallArgs = LowerDirectCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1263 Chain = getChain(CallArgs);
1264 OperFlag = getOutFlag(CallArgs);
1265 } else {
1266 CallArgs = LowerIndirectCallArguments(Op, Chain, OperFlag, DataAddr_Lo,
1267 DataAddr_Hi, DAG);
1268 Chain = getChain(CallArgs);
1269 OperFlag = getOutFlag(CallArgs);
1270 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001271
1272 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001273 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001274 OperFlag);
1275 Chain = getChain(PICCall);
1276 OperFlag = getOutFlag(PICCall);
1277
1278
1279 // Carrying the Constant 0 along the CALLSEQSTART
1280 // because there is nothing else to carry.
1281 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1282 OperFlag);
1283 Chain = getChain(SeqEnd);
1284 OperFlag = getOutFlag(SeqEnd);
1285
1286 // Lower the return value reading after the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001287 if (IsDirectCall)
1288 return LowerDirectCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1289 else
1290 return LowerIndirectCallReturn(Op, Chain, OperFlag, DataAddr_Lo,
1291 DataAddr_Hi, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001292}
1293
1294bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1295 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1296 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1297 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1298 return true;
1299 return false;
1300}
1301
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001302// NeedToConvertToMemOp - Returns true if one of the operands of the
1303// operation 'Op' needs to be put into memory. Also returns the
1304// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1305// no instruction that can operation on two registers. Most insns take
1306// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001307bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001308 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001309 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1310 Op.getOperand(1).getOpcode() == ISD::Constant)
1311 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001312
1313 // Return false if one of the operands is already a direct
1314 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001315 if (isDirectLoad(Op.getOperand(0))) {
1316 if (Op.getOperand(0).hasOneUse())
1317 return false;
1318 else
1319 MemOp = 0;
1320 }
1321 if (isDirectLoad(Op.getOperand(1))) {
1322 if (Op.getOperand(1).hasOneUse())
1323 return false;
1324 else
1325 MemOp = 1;
1326 }
1327 return true;
1328}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001329
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001330// LowerBinOp - Lower a commutative binary operation that does not
1331// affect status flag carry.
1332SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001333 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001334
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001335 // We should have handled larger operands in type legalizer itself.
1336 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001337
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001338 unsigned MemOp = 1;
1339 if (NeedToConvertToMemOp(Op, MemOp)) {
1340 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001341 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001342
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001343 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001344 NewVal);
1345 }
1346 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001347 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001348 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001349}
1350
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001351// LowerADD - Lower all types of ADD operations including the ones
1352// that affects carry.
1353SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001354 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001355 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001356 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001357 unsigned MemOp = 1;
1358 if (NeedToConvertToMemOp(Op, MemOp)) {
1359 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001360 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001361
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001362 // ADDC and ADDE produces two results.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001363 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001364
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001365 // ADDE has three operands, the last one is a flag.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001366 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001367 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1368 NewVal, Op.getOperand(2));
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001369 // ADDC has two operands.
1370 else if (Op.getOpcode() == ISD::ADDC)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001371 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1372 NewVal);
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001373 // ADD it is. It produces only one result.
1374 else
1375 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1376 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001377 }
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001378 else if (Op.getOpcode() == ISD::ADD)
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001379 return Op;
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001380 else
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001381 return SDValue();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001382}
1383
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001384SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001385 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001386 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001387 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001388
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001389 // Nothing to do if the first operand is already a direct load and it has
1390 // only one use.
1391 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001392 return SDValue();
1393
1394 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001395 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001396
1397 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001398 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001399 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001400 Op.getOperand(2));
1401 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001402 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001403}
1404
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001405// LowerFORMAL_ARGUMENTS - Argument values are loaded from the
1406// <fname>.args + offset. All arguments are already broken to leaglized
1407// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001408
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001409SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001410 SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001411 SmallVector<SDValue, 8> ArgValues;
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001412 unsigned NumArgVals = Op.getNode()->getNumValues() - 1;
Dale Johannesene8d72302009-02-06 23:05:02 +00001413 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001414 SDValue Chain = Op.getOperand(0); // Formal arguments' chain
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001415
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001416
1417 // Reset the map of FI and TmpOffset
1418 ResetTmpOffsetMap();
1419 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001420 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001421 const Function *F = MF.getFunction();
1422 std::string FuncName = F->getName();
1423
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001424 // Create the <fname>.args external symbol.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001425 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
1426 sprintf(tmpName, "%s.args", FuncName.c_str());
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001427 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001428
1429 // Load arg values from the label + offset.
1430 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001431 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001432 for (unsigned i = 0; i < NumArgVals ; ++i) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001433 SDValue Offset = DAG.getConstant(i, MVT::i8);
1434 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001435 Offset);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001436 Chain = getChain(PICLoad);
1437 ArgValues.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001438 }
1439
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001440 // Return a MERGE_VALUE node.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001441 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001442 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001443 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001444}
1445
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001446// Perform DAGCombine of PIC16Load.
1447// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001448SDValue PIC16TargetLowering::
1449PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1450 SelectionDAG &DAG = DCI.DAG;
1451 SDValue Chain = N->getOperand(0);
1452 if (N->hasNUsesOfValue(0, 0)) {
1453 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1454 }
1455 return SDValue();
1456}
1457
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001458// For all the functions with arguments some STORE nodes are generated
1459// that store the argument on the frameindex. However in PIC16 the arguments
1460// are passed on stack only. Therefore these STORE nodes are redundant.
1461// To remove these STORE nodes will be removed in PerformStoreCombine
1462//
1463// Currently this function is doint nothing and will be updated for removing
1464// unwanted store operations
1465SDValue PIC16TargetLowering::
1466PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001467 return SDValue(N, 0);
1468 /*
1469 // Storing an undef value is of no use, so remove it
1470 if (isStoringUndef(N, Chain, DAG)) {
1471 return Chain; // remove the store and return the chain
1472 }
1473 //else everything is ok.
1474 return SDValue(N, 0);
1475 */
1476}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001477
1478SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1479 DAGCombinerInfo &DCI) const {
1480 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001481 case ISD::STORE:
1482 return PerformStoreCombine(N, DCI);
1483 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001484 return PerformPIC16LoadCombine(N, DCI);
1485 }
1486 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001487}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001488
1489static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1490 switch (CC) {
1491 default: assert(0 && "Unknown condition code!");
1492 case ISD::SETNE: return PIC16CC::NE;
1493 case ISD::SETEQ: return PIC16CC::EQ;
1494 case ISD::SETGT: return PIC16CC::GT;
1495 case ISD::SETGE: return PIC16CC::GE;
1496 case ISD::SETLT: return PIC16CC::LT;
1497 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001498 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001499 case ISD::SETULE: return PIC16CC::LE;
1500 case ISD::SETUGE: return PIC16CC::GE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001501 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001502 }
1503}
1504
1505// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1506// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1507static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1508 ISD::CondCode CC, unsigned &SPCC) {
1509 if (isa<ConstantSDNode>(RHS) &&
1510 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1511 CC == ISD::SETNE &&
1512 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1513 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1514 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1515 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1516 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1517 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1518 SDValue CMPCC = LHS.getOperand(3);
1519 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1520 LHS = CMPCC.getOperand(0);
1521 RHS = CMPCC.getOperand(1);
1522 }
1523}
1524
1525// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1526SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1527 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001528 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001529 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1530
1531 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1532 // i.e. a < 12 can be rewritten as 12 > a.
1533 if (RHS.getOpcode() == ISD::Constant) {
1534
1535 SDValue Tmp = LHS;
1536 LHS = RHS;
1537 RHS = Tmp;
1538
1539 switch (CondCode) {
1540 default: break;
1541 case PIC16CC::LT:
1542 CondCode = PIC16CC::GT;
1543 break;
1544 case PIC16CC::GT:
1545 CondCode = PIC16CC::LT;
1546 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001547 case PIC16CC::ULT:
1548 CondCode = PIC16CC::UGT;
1549 break;
1550 case PIC16CC::UGT:
1551 CondCode = PIC16CC::ULT;
1552 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001553 case PIC16CC::GE:
1554 CondCode = PIC16CC::LE;
1555 break;
1556 case PIC16CC::LE:
1557 CondCode = PIC16CC::GE;
1558 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001559 case PIC16CC::ULE:
1560 CondCode = PIC16CC::UGE;
1561 break;
1562 case PIC16CC::UGE:
1563 CondCode = PIC16CC::ULE;
1564 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001565 }
1566 }
1567
1568 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001569
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001570 // These are signed comparisons.
1571 SDValue Mask = DAG.getConstant(128, MVT::i8);
1572 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001573 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1574 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001575 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001576
1577 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001578 // We can use a subtract operation to set the condition codes. But
1579 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001580 // Nothing to do if the first operand is already a valid type (direct load
1581 // for subwf and literal for sublw) and it is used by this operation only.
1582 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1583 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001584 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001585
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001586 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001587 LHS = ConvertToMemOperand (LHS, DAG, dl);
1588 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001589}
1590
1591
1592SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1593 SDValue LHS = Op.getOperand(0);
1594 SDValue RHS = Op.getOperand(1);
1595 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1596 SDValue TrueVal = Op.getOperand(2);
1597 SDValue FalseVal = Op.getOperand(3);
1598 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001599 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001600
1601 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1602 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1603 // i.e.
1604 // A setcc: lhs, rhs, cc is expanded by llvm to
1605 // select_cc: result of setcc, 0, 1, 0, setne
1606 // We can think of it as:
1607 // select_cc: lhs, rhs, 1, 0, cc
1608 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1609 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1610
1611 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001612 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001613
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001614 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001615 FalseVal, PIC16CC, Cmp.getValue(1));
1616}
1617
1618MachineBasicBlock *
1619PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001620 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001621 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1622 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001623 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001624
1625 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1626 // control-flow pattern. The incoming instruction knows the destination vreg
1627 // to set, the condition code register to branch on, the true/false values to
1628 // select between, and a branch opcode to use.
1629 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1630 MachineFunction::iterator It = BB;
1631 ++It;
1632
1633 // thisMBB:
1634 // ...
1635 // TrueVal = ...
1636 // [f]bCC copy1MBB
1637 // fallthrough --> copy0MBB
1638 MachineBasicBlock *thisMBB = BB;
1639 MachineFunction *F = BB->getParent();
1640 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1641 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001642 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001643 F->insert(It, copy0MBB);
1644 F->insert(It, sinkMBB);
1645
1646 // Update machine-CFG edges by transferring all successors of the current
1647 // block to the new block which will contain the Phi node for the select.
1648 sinkMBB->transferSuccessors(BB);
1649 // Next, add the true and fallthrough blocks as its successors.
1650 BB->addSuccessor(copy0MBB);
1651 BB->addSuccessor(sinkMBB);
1652
1653 // copy0MBB:
1654 // %FalseValue = ...
1655 // # fallthrough to sinkMBB
1656 BB = copy0MBB;
1657
1658 // Update machine-CFG edges
1659 BB->addSuccessor(sinkMBB);
1660
1661 // sinkMBB:
1662 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1663 // ...
1664 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001665 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001666 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1667 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1668
1669 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1670 return BB;
1671}
1672
1673
1674SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1675 SDValue Chain = Op.getOperand(0);
1676 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1677 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1678 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1679 SDValue Dest = Op.getOperand(4); // BB to jump to
1680 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001681 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001682
1683 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1684 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1685 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1686 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1687
1688 // Get the Compare insn and condition code.
1689 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001690 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001691
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001692 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001693 Cmp.getValue(1));
1694}
1695
1696