blob: cc7ce5557fe4ac9d9b9da369f64e6b291d730451 [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
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000329 // Expand FrameIndex like GlobalAddress and ExternalSymbol
330 // Also use Offset field for lo and hi parts. The default
331 // offset is zero.
332 SDValue Offset = DAG.getConstant(0, MVT::i8);
333 SDValue FI = DAG.getTargetFrameIndex(Index, MVT::i8);
334 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, FI, Offset);
335 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, FI, Offset);
336 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000337}
338
339
340SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000341 StoreSDNode *St = cast<StoreSDNode>(N);
342 SDValue Chain = St->getChain();
343 SDValue Src = St->getValue();
344 SDValue Ptr = St->getBasePtr();
345 MVT ValueType = Src.getValueType();
346 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000347 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000348
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000349 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000350 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000351
352 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000353 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000354 PtrLo, PtrHi,
355 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000356 }
357 else if (ValueType == MVT::i16) {
358 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
359 SDValue SrcLo, SrcHi;
360 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
361 SDValue ChainLo = Chain, ChainHi = Chain;
362 if (Chain.getOpcode() == ISD::TokenFactor) {
363 ChainLo = Chain.getOperand(0);
364 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000365 }
Dale Johannesende064702009-02-06 21:50:26 +0000366 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000367 ChainLo,
368 SrcLo, PtrLo, PtrHi,
369 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000370
Dale Johannesende064702009-02-06 21:50:26 +0000371 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000372 SrcHi, PtrLo, PtrHi,
373 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000374
Dale Johannesende064702009-02-06 21:50:26 +0000375 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000376 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000377 }
378 else if (ValueType == MVT::i32) {
379 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
380 SDValue SrcLo, SrcHi;
381 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000382
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000383 // Get the expanded parts of each of SrcLo and SrcHi.
384 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
385 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
386 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000387
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000388 SDValue ChainLo = Chain, ChainHi = Chain;
389 if (Chain.getOpcode() == ISD::TokenFactor) {
390 ChainLo = Chain.getOperand(0);
391 ChainHi = Chain.getOperand(1);
392 }
393 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
394 ChainHi2 = ChainHi;
395 if (ChainLo.getOpcode() == ISD::TokenFactor) {
396 ChainLo1 = ChainLo.getOperand(0);
397 ChainLo2 = ChainLo.getOperand(1);
398 }
399 if (ChainHi.getOpcode() == ISD::TokenFactor) {
400 ChainHi1 = ChainHi.getOperand(0);
401 ChainHi2 = ChainHi.getOperand(1);
402 }
Dale Johannesende064702009-02-06 21:50:26 +0000403 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000404 ChainLo1,
405 SrcLo1, PtrLo, PtrHi,
406 DAG.getConstant (0 + StoreOffset, MVT::i8));
407
Dale Johannesende064702009-02-06 21:50:26 +0000408 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000409 SrcLo2, PtrLo, PtrHi,
410 DAG.getConstant (1 + StoreOffset, MVT::i8));
411
Dale Johannesende064702009-02-06 21:50:26 +0000412 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000413 SrcHi1, PtrLo, PtrHi,
414 DAG.getConstant (2 + StoreOffset, MVT::i8));
415
Dale Johannesende064702009-02-06 21:50:26 +0000416 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000417 SrcHi2, PtrLo, PtrHi,
418 DAG.getConstant (3 + StoreOffset, MVT::i8));
419
Dale Johannesende064702009-02-06 21:50:26 +0000420 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
421 getChain(Store1), getChain(Store2));
422 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
423 getChain(Store3), getChain(Store4));
424 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000425
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000426 }
427 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000428 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000429 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000430 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000431}
432
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000433SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
434{
435 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000436 // FIXME there isn't really debug info here
437 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000438
439 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000440 SDValue Offset = DAG.getConstant(0, MVT::i8);
441 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES, Offset);
442 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES, Offset);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000443
Dale Johannesende064702009-02-06 21:50:26 +0000444 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000445}
446
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000447// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000448SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000449 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000450 // FIXME there isn't really debug info here
451 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000452
453 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
454 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000455
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000456 SDValue Offset = DAG.getConstant(0, MVT::i8);
457 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA, Offset);
458 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA, Offset);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000459
Dale Johannesende064702009-02-06 21:50:26 +0000460 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000461}
462
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000463bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
464 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000465
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000466 if (Op.getOpcode() == ISD::BUILD_PAIR) {
467 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
468 return true;
469 }
470 return false;
471}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000472
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000473// Return true if DirectAddress is in ROM_SPACE
474bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000475
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000476 // RomAddress is a GlobalAddress in ROM_SPACE_
477 // If the Op is not a GlobalAddress return NULL without checking
478 // anything further.
479 if (!isDirectAddress(Op))
480 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000481
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000482 // Its a GlobalAddress.
483 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
484 SDValue TGA = Op.getOperand(0).getOperand(0);
485 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
486 const Type *ValueType = GSDN->getGlobal()->getType();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000487
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000488 if (!isa<PointerType>(ValueType)) {
489 assert(0 && "TGA must be of a PointerType");
490 }
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000491
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000492 int AddrSpace = dyn_cast<PointerType>(ValueType)->getAddressSpace();
493 if (AddrSpace == PIC16ISD::ROM_SPACE)
494 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000495
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000496 // Any other address space return it false
497 return false;
498}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000499
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000500
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000501// GetExpandedParts - This function is on the similiar lines as
502// the GetExpandedInteger in type legalizer is. This returns expanded
503// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000504
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000505void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
506 SDValue &Lo, SDValue &Hi) {
507 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000508 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000509 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000510
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000511 // Extract the lo component.
512 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
513 DAG.getConstant(0, MVT::i8));
514
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000515 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000516 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
517 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000518}
519
520// Legalize FrameIndex into ExternalSymbol and offset.
521void
522PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
523 SDValue &ES, int &Offset) {
524
525 MachineFunction &MF = DAG.getMachineFunction();
526 const Function *Func = MF.getFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000527 MachineFrameInfo *MFI = MF.getFrameInfo();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000528 const std::string Name = Func->getName();
529
530 char *tmpName = new char [strlen(Name.c_str()) + 8];
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000531 sprintf(tmpName, "%s.frame", Name.c_str());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000532 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
533 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000534
535 // FrameIndices are not stack offsets. But they represent the request
536 // for space on stack. That space requested may be more than one byte.
537 // Therefore, to calculate the stack offset that a FrameIndex aligns
538 // with, we need to traverse all the FrameIndices available earlier in
539 // the list and add their requested size.
540 unsigned FIndex = FR->getIndex();
541 Offset = 0;
542 for (unsigned i=0; i<FIndex ; ++i) {
543 Offset += MFI->getObjectSize(i);
544 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000545
546 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000547}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000548
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000549// This function legalizes the PIC16 Addresses. If the Pointer is
550// -- Direct address variable residing
551// --> then a Banksel for that variable will be created.
552// -- Rom variable
553// --> then it will be treated as an indirect address.
554// -- Indirect address
555// --> then the address will be loaded into FSR
556// -- ADD with constant operand
557// --> then constant operand of ADD will be returned as Offset
558// and non-constant operand of ADD will be treated as pointer.
559// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000560
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000561void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
562 SDValue &Lo, SDValue &Hi,
563 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000564
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000565 // Offset, by default, should be 0
566 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000567
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000568 // If the pointer is ADD with constant,
569 // return the constant value as the offset
570 if (Ptr.getOpcode() == ISD::ADD) {
571 SDValue OperLeft = Ptr.getOperand(0);
572 SDValue OperRight = Ptr.getOperand(1);
573 if (OperLeft.getOpcode() == ISD::Constant) {
574 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
575 Ptr = OperRight;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000576 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000577 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000578 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000579 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000580 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000581
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000582 // If the pointer is Type i8 and an external symbol
583 // then treat it as direct address.
584 // One example for such case is storing and loading
585 // from function frame during a call
586 if (Ptr.getValueType() == MVT::i8) {
587 switch (Ptr.getOpcode()) {
588 case ISD::TargetExternalSymbol:
589 Lo = Ptr;
590 Hi = DAG.getConstant(1, MVT::i8);
591 return;
592 }
593 }
594
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000595 // Expansion of FrameIndex has Lo/Hi parts
596 if (isDirectAddress(Ptr)) {
597 SDValue TFI = Ptr.getOperand(0).getOperand(0);
598 if (TFI.getOpcode() == ISD::TargetFrameIndex) {
599 int FrameOffset;
600 LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset);
601 Hi = DAG.getConstant(1, MVT::i8);
602 Offset += FrameOffset;
603 return;
604 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000605 }
606
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000607 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
608 // Direct addressing case for RAM variables. The Hi part is constant
609 // and the Lo part is the TGA itself.
610 Lo = Ptr.getOperand(0).getOperand(0);
611
612 // For direct addresses Hi is a constant. Value 1 for the constant
613 // signifies that banksel needs to generated for it. Value 0 for
614 // the constant signifies that banksel does not need to be generated
615 // for it. Mark it as 1 now and optimize later.
616 Hi = DAG.getConstant(1, MVT::i8);
617 return;
618 }
619
620 // Indirect addresses. Get the hi and lo parts of ptr.
621 GetExpandedParts(Ptr, DAG, Lo, Hi);
622
623 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000624 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
625 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000626
627 return;
628}
629
Duncan Sands1607f052008-12-01 11:39:25 +0000630SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000631 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
632 SDValue Chain = LD->getChain();
633 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000634 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000635
636 SDValue Load, Offset;
637 SDVTList Tys;
638 MVT VT, NewVT;
639 SDValue PtrLo, PtrHi;
640 unsigned LoadOffset;
641
642 // Legalize direct/indirect addresses. This will give the lo and hi parts
643 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000644 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000645
646 // Load from the pointer (direct address or FSR)
647 VT = N->getValueType(0);
648 unsigned NumLoads = VT.getSizeInBits() / 8;
649 std::vector<SDValue> PICLoads;
650 unsigned iter;
651 MVT MemVT = LD->getMemoryVT();
652 if(ISD::isNON_EXTLoad(N)) {
653 for (iter=0; iter<NumLoads ; ++iter) {
654 // Add the pointer offset if any
655 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
656 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000657 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000658 Offset);
659 PICLoads.push_back(Load);
660 }
661 } else {
662 // If it is extended load then use PIC16Load for Memory Bytes
663 // and for all extended bytes perform action based on type of
664 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
665
666
667 // For extended loads this is the memory value type
668 // i.e. without any extension
669 MVT MemVT = LD->getMemoryVT();
670 unsigned MemBytes = MemVT.getSizeInBits() / 8;
671 unsigned ExtdBytes = VT.getSizeInBits() / 8;
672 Offset = DAG.getConstant(LoadOffset, MVT::i8);
673
674 Tys = DAG.getVTList(MVT::i8, MVT::Other);
675 // For MemBytes generate PIC16Load with proper offset
676 for (iter=0; iter<MemBytes; ++iter) {
677 // Add the pointer offset if any
678 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000679 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000680 Offset);
681 PICLoads.push_back(Load);
682 }
683
684 // For SignExtendedLoad
685 if (ISD::isSEXTLoad(N)) {
686 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
687 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000688 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000689 DAG.getConstant(7, MVT::i8));
690 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
691 PICLoads.push_back(SRA);
692 }
693 } else if (ISD::isZEXTLoad(N)) {
694 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
695 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
696 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
697 PICLoads.push_back(ConstZero);
698 }
699 }
700 }
701 SDValue BP;
702
703 if (VT == MVT::i8) {
704 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000705 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000706 }
707 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000708 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000709 if (MemVT == MVT::i8)
710 Chain = getChain(PICLoads[0]);
711 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000712 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
713 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000714 } else if (VT == MVT::i32) {
715 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000716 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
717 PICLoads[0], PICLoads[1]);
718 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
719 PICLoads[2], PICLoads[3]);
720 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000721 if (MemVT == MVT::i8)
722 Chain = getChain(PICLoads[0]);
723 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000724 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
725 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000726 else {
727 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000728 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000729 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000730 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000731 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000732 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
733 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000734 }
735 }
736 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000737 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000738}
739
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000740SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
741 // We should have handled larger operands in type legalizer itself.
742 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
743
744 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000745 SDValue Value = N->getOperand(0);
746 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000747 PIC16ISD::PIC16Libcall CallCode;
748 switch (N->getOpcode()) {
749 case ISD::SRA:
750 CallCode = PIC16ISD::SRA_I8;
751 break;
752 case ISD::SHL:
753 CallCode = PIC16ISD::SLL_I8;
754 break;
755 case ISD::SRL:
756 CallCode = PIC16ISD::SRL_I8;
757 break;
758 default:
759 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000760 return SDValue();
761 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000762 SmallVector<SDValue, 2> Ops(2);
763 Ops[0] = Value;
764 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000765 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
766 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000767 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000768}
769
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000770void
771PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
772 SmallVectorImpl<SDValue>&Results,
773 SelectionDAG &DAG) {
774 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000775 SDValue Res;
776 unsigned i;
777 switch (Op.getOpcode()) {
778 case ISD::FORMAL_ARGUMENTS:
779 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
780 case ISD::LOAD:
781 Res = ExpandLoad(Op.getNode(), DAG); break;
782 case ISD::CALL:
783 Res = LowerCALL(Op, DAG); break;
784 default: {
785 // All other operations are handled in LowerOperation.
786 Res = LowerOperation(Op, DAG);
787 if (Res.getNode())
788 Results.push_back(Res);
789
790 return;
791 }
792 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000793
794 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000795 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000796 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000797 Results.push_back(SDValue(N, i));
798 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000799}
800
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000801SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
802 switch (Op.getOpcode()) {
803 case ISD::FORMAL_ARGUMENTS:
804 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000805 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000806 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000807 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000808 return LowerADD(Op, DAG);
809 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000810 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000811 case ISD::SUBE:
812 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000813 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000814 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000815 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000816 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000817 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000818 case ISD::SRA:
819 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000820 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000821 case ISD::OR:
822 case ISD::AND:
823 case ISD::XOR:
824 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000825 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000826 return LowerCALL(Op, DAG);
827 case ISD::RET:
828 return LowerRET(Op, DAG);
829 case ISD::BR_CC:
830 return LowerBR_CC(Op, DAG);
831 case ISD::SELECT_CC:
832 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000833 }
Dan Gohman475871a2008-07-27 21:46:04 +0000834 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000835}
836
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000837SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000838 SelectionDAG &DAG,
839 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000840 assert (Op.getValueType() == MVT::i8
841 && "illegal value type to store on stack.");
842
843 MachineFunction &MF = DAG.getMachineFunction();
844 const Function *Func = MF.getFunction();
845 const std::string FuncName = Func->getName();
846
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000847 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000848
849 // Put the value on stack.
850 // Get a stack slot index and convert to es.
851 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
852 sprintf(tmpName, "%s.tmp", FuncName.c_str());
853 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
854
855 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000856 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000857 DAG.getEntryNode(),
858 Op, ES,
859 DAG.getConstant (1, MVT::i8), // Banksel.
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 // Load the value from ES.
864 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000865 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000866 ES, DAG.getConstant (1, MVT::i8),
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000867 DAG.getConstant (GetTmpOffsetForFI(FI, 1),
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000868 MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000869
870 return Load.getValue(0);
871}
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000872
873SDValue PIC16TargetLowering::
874LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
875 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
876 SelectionDAG &DAG) {
877 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
878 unsigned NumOps = TheCall->getNumArgs();
879 DebugLoc dl = TheCall->getDebugLoc();
880
881 // If call has no arguments then do nothing and return.
882 if (NumOps == 0)
883 return Chain;
884
885 std::vector<SDValue> Ops;
886 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
887 SDValue Arg, StoreRet;
Sanjiv Guptae206b1d2009-04-09 10:29:32 +0000888
889 // For PIC16 ABI the arguments come after the return value.
890 unsigned RetVals = TheCall->getNumRetVals();
891 for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000892 // Get the arguments
893 Arg = TheCall->getArg(i);
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000894 // If argument is FrameIndex then map it with temporary
895 if (Arg.getOpcode() == PIC16ISD::Lo || Arg.getOpcode() == PIC16ISD::Hi) {
896 if (Arg.getOperand(0).getOpcode() == ISD::TargetFrameIndex) {
897 Arg = LegalizeFrameArgument(Arg, dl, DAG);
898 }
899 }
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000900 Ops.clear();
901 Ops.push_back(Chain);
902 Ops.push_back(Arg);
903 Ops.push_back(DataAddr_Lo);
904 Ops.push_back(DataAddr_Hi);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +0000905 Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000906 Ops.push_back(InFlag);
907
908 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
909
910 Chain = getChain(StoreRet);
911 InFlag = getOutFlag(StoreRet);
Sanjiv Guptae206b1d2009-04-09 10:29:32 +0000912 ArgOffset++;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000913 }
914 return Chain;
915}
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000916
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000917SDValue PIC16TargetLowering::
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000918LegalizeFrameArgument(SDValue Arg, DebugLoc dl, SelectionDAG &DAG) {
919 MachineFunction &MF = DAG.getMachineFunction();
920 const Function *Func = MF.getFunction();
921 MachineFrameInfo *MFI = MF.getFrameInfo();
922 const std::string Name = Func->getName();
923
924 // Caller creates the stack storage to pass the aggregate type
925 // as argument. So it should be relative to tmp variable.
926 SDValue FI = Arg.getOperand(0);
927 char *tmpName = new char [strlen(Name.c_str()) + 8];
928 sprintf(tmpName, "%s.tmp", Name.c_str());
929 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
930 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(FI);
931
932 unsigned FIndex = FR->getIndex();
933 // Reserve space in tmp variable for the aggregate type
934 int FrameOffset = GetTmpOffsetForFI(FIndex, MFI->getObjectSize(FIndex));
935
936 if (Arg.getOpcode() == PIC16ISD::Lo) {
937 // Lo part of frame index
938 SDValue FrameConst = DAG.getConstant(FrameOffset, MVT::i8);
939 return DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, ES, FrameConst);
940 } else {
941 // Hi part of frame index
942 SDValue FrameConst = DAG.getConstant(FrameOffset + 1, MVT::i8);
943 return DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, ES, FrameConst);
944 }
945}
946
947SDValue PIC16TargetLowering::
948LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue ArgLabel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000949 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000950 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
951 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +0000952 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000953 std::string Name;
954 SDValue Arg, StoreAt;
955 MVT ArgVT;
956 unsigned Size=0;
957 unsigned ArgCount=0;
958
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000959 // If call has no arguments then do nothing and return.
960 if (NumOps == 0)
961 return Chain;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000962
963 // FIXME: This portion of code currently assumes only
964 // primitive types being passed as arguments.
965
966 // Legalize the address before use
967 SDValue PtrLo, PtrHi;
968 unsigned AddressOffset;
969 int StoreOffset = 0;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000970 LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000971 SDValue StoreRet;
972
973 std::vector<SDValue> Ops;
974 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
975 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
976 // Get the argument
977 Arg = TheCall->getArg(i);
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000978 // If argument is FrameIndex then map it with temporary
979 if (Arg.getOpcode() == PIC16ISD::Lo || Arg.getOpcode() == PIC16ISD::Hi) {
980 if (Arg.getOperand(0).getOpcode() == ISD::TargetFrameIndex) {
981 Arg = LegalizeFrameArgument(Arg, dl, DAG);
982 }
983 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000984 StoreOffset = (Offset + AddressOffset);
985
986 // Store the argument on frame
987
988 Ops.clear();
989 Ops.push_back(Chain);
Sanjiv Gupta12f23a82009-04-13 09:38:38 +0000990 Ops.push_back(Arg);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000991 Ops.push_back(PtrLo);
992 Ops.push_back(PtrHi);
993 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
994 Ops.push_back(InFlag);
995
Dale Johannesene8d72302009-02-06 23:05:02 +0000996 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000997
998 Chain = getChain(StoreRet);
999 InFlag = getOutFlag(StoreRet);
1000
1001 // Update the frame offset to be used for next argument
1002 ArgVT = Arg.getValueType();
1003 Size = ArgVT.getSizeInBits();
1004 Size = Size/8; // Calculate size in bytes
1005 Offset += Size; // Increase the frame offset
1006 }
1007 return Chain;
1008}
1009
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001010SDValue PIC16TargetLowering::
1011LowerIndirectCallReturn (SDValue Op, SDValue Chain, SDValue InFlag,
1012 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
1013 SelectionDAG &DAG) {
1014 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1015 DebugLoc dl = TheCall->getDebugLoc();
1016 unsigned RetVals = TheCall->getNumRetVals();
1017
1018 // If call does not have anything to return
1019 // then do nothing and go back.
1020 if (RetVals == 0)
1021 return Chain;
1022
1023 // Call has something to return
1024 std::vector<SDValue> ResultVals;
1025 SDValue LoadRet;
1026
1027 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1028 for(unsigned i=0;i<RetVals;i++) {
1029 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
1030 DataAddr_Hi, DAG.getConstant(i, MVT::i8),
1031 InFlag);
1032 InFlag = getOutFlag(LoadRet);
1033 Chain = getChain(LoadRet);
1034 ResultVals.push_back(LoadRet);
1035 }
1036 ResultVals.push_back(Chain);
1037 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
1038 return Res;
1039}
1040
1041SDValue PIC16TargetLowering::
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001042LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue RetLabel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001043 SDValue InFlag, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001044 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001045 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001046 // Currently handling primitive types only. They will come in
1047 // i8 parts
1048 unsigned RetVals = TheCall->getNumRetVals();
1049
1050 std::vector<SDValue> ResultVals;
1051
1052 // Return immediately if the return type is void
1053 if (RetVals == 0)
1054 return Chain;
1055
1056 // Call has something to return
1057
1058 // Legalize the address before use
1059 SDValue LdLo, LdHi;
1060 unsigned LdOffset;
Sanjiv Gupta85be4082009-04-14 02:49:52 +00001061 LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001062
1063 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1064 SDValue LoadRet;
1065
1066 for(unsigned i=0, Offset=0;i<RetVals;i++) {
1067
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001068 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001069 DAG.getConstant(LdOffset + Offset, MVT::i8),
1070 InFlag);
1071
1072 InFlag = getOutFlag(LoadRet);
1073
1074 Chain = getChain(LoadRet);
1075 Offset++;
1076 ResultVals.push_back(LoadRet);
1077 }
1078
1079 // To return use MERGE_VALUES
1080 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001081 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001082 return Res;
1083}
1084
1085SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001086 SDValue Chain = Op.getOperand(0);
1087 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001088
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001089 if (Op.getNumOperands() == 1) // return void
1090 return Op;
1091
1092 // return should have odd number of operands
1093 if ((Op.getNumOperands() % 2) == 0 ) {
1094 assert(0 && "Do not know how to return this many arguments!");
1095 abort();
1096 }
1097
1098 // Number of values to return
1099 unsigned NumRet = (Op.getNumOperands() / 2);
1100
1101 // Function returns value always on stack with the offset starting
1102 // from 0
1103 MachineFunction &MF = DAG.getMachineFunction();
1104 const Function *F = MF.getFunction();
1105 std::string FuncName = F->getName();
1106
1107 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
1108 sprintf(tmpName, "%s.frame", FuncName.c_str());
1109 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1110 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1111 SDValue BS = DAG.getConstant(1, MVT::i8);
1112 SDValue RetVal;
1113 for(unsigned i=0;i<NumRet; ++i) {
1114 RetVal = Op.getNode()->getOperand(2*i + 1);
1115 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
1116 ES, BS,
1117 DAG.getConstant (i, MVT::i8));
1118
1119 }
1120 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001121}
1122
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001123// CALL node may have some operands non-legal to PIC16. Generate new CALL
1124// node with all the operands legal.
1125// Currently only Callee operand of the CALL node is non-legal. This function
1126// legalizes the Callee operand and uses all other operands as are to generate
1127// new CALL node.
1128
1129SDValue PIC16TargetLowering::LegalizeCALL(SDValue Op, SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001130 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1131 SDValue Chain = TheCall->getChain();
1132 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001133 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001134 unsigned i =0;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001135
1136 assert(Callee.getValueType() == MVT::i16 &&
1137 "Don't know how to legalize this call node!!!");
1138 assert(Callee.getOpcode() == ISD::BUILD_PAIR &&
1139 "Don't know how to legalize this call node!!!");
1140
1141 if (isDirectAddress(Callee)) {
1142 // Come here for direct calls
1143 Callee = Callee.getOperand(0).getOperand(0);
1144 } else {
1145 // Come here for indirect calls
1146 SDValue Lo, Hi;
1147 // Indirect addresses. Get the hi and lo parts of ptr.
1148 GetExpandedParts(Callee, DAG, Lo, Hi);
1149 // Connect Lo and Hi parts of the callee with the PIC16Connect
1150 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1151 }
1152 std::vector<SDValue> Ops;
1153 Ops.push_back(Chain);
1154 Ops.push_back(Callee);
1155
1156 // Add the call arguments and their flags
1157 unsigned NumArgs = TheCall->getNumArgs();
1158 for(i=0;i<NumArgs;i++) {
1159 Ops.push_back(TheCall->getArg(i));
1160 Ops.push_back(TheCall->getArgFlagsVal(i));
1161 }
1162 std::vector<MVT> NodeTys;
1163 unsigned NumRets = TheCall->getNumRetVals();
1164 for(i=0;i<NumRets;i++)
1165 NodeTys.push_back(TheCall->getRetValType(i));
1166
1167 // Return a Chain as well
1168 NodeTys.push_back(MVT::Other);
1169
1170 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1171 // Generate new call with all the operands legal
1172 return DAG.getCall(TheCall->getCallingConv(), dl,
1173 TheCall->isVarArg(), TheCall->isTailCall(),
1174 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
1175}
1176
1177void PIC16TargetLowering::
1178GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
1179 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
1180 SelectionDAG &DAG) {
1181 assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
1182 && "Don't know what to do of such callee!!");
1183 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1184 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1185 Chain = getChain(SeqStart);
1186 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1187
1188 // Get the Lo and Hi part of code address
1189 SDValue Lo = Callee.getOperand(0);
1190 SDValue Hi = Callee.getOperand(1);
1191
1192 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
1193 Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Hi);
1194 // Use the Lo part as is and make CALLW
1195 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1196 SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
1197 OperFlag);
1198 Chain = getChain(Call);
1199 OperFlag = getOutFlag(Call);
1200 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1201 OperFlag);
1202 Chain = getChain(SeqEnd);
1203 OperFlag = getOutFlag(SeqEnd);
1204
1205 // Low part of Data Address
1206 DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
1207
1208 // Make the second call.
1209 SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1210 Chain = getChain(SeqStart);
1211 OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1212
1213 // Add 1 to Lo part for the second code word.
1214 Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, Lo, DAG.getConstant(1, MVT::i8));
1215 // Use new Lo to make another CALLW
1216 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
1217 Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
1218 Chain = getChain(Call);
1219 OperFlag = getOutFlag(Call);
1220 SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1221 OperFlag);
1222 Chain = getChain(SeqEnd);
1223 OperFlag = getOutFlag(SeqEnd);
1224 // Hi part of Data Address
1225 DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
1226}
1227
1228
1229SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
1230 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1231 SDValue Chain = TheCall->getChain();
1232 SDValue Callee = TheCall->getCallee();
1233 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001234 if (Callee.getValueType() == MVT::i16 &&
1235 Callee.getOpcode() == ISD::BUILD_PAIR) {
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001236 // Control should come here only from TypeLegalizer for lowering
1237
1238 // Legalize the non-legal arguments of call and return the
1239 // new call with legal arguments.
1240 return LegalizeCALL(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001241 }
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001242 // Control should come here from Legalize DAG.
1243 // Here all the operands of CALL node should be legal.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001244
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001245 // If this is an indirect call then to pass the arguments
1246 // and read the return value back, we need the data address
1247 // of the function being called.
1248 // To get the data address two more calls need to be made.
1249
1250 // The flag to track if this is a direct or indirect call.
1251 bool IsDirectCall = true;
1252 unsigned RetVals = TheCall->getNumRetVals();
1253 unsigned NumArgs = TheCall->getNumArgs();
1254
1255 SDValue DataAddr_Lo, DataAddr_Hi;
1256 if (Callee.getOpcode() == PIC16ISD::PIC16Connect) {
1257 IsDirectCall = false; // This is indirect call
1258 // Read DataAddress only if we have to pass arguments or
1259 // read return value.
1260 if ((RetVals > 0) || (NumArgs > 0))
1261 GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
1262 }
1263
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001264 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1265
1266 // Start the call sequence.
1267 // Carring the Constant 0 along the CALLSEQSTART
1268 // because there is nothing else to carry.
1269 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1270 Chain = getChain(SeqStart);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001271 SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
1272 std::string Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001273
1274 // For any direct call - callee will be GlobalAddressNode or
1275 // ExternalSymbol
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001276 SDValue ArgLabel, RetLabel;
1277 if (IsDirectCall) {
1278 // Considering the GlobalAddressNode case here.
1279 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1280 GlobalValue *GV = G->getGlobal();
1281 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1282 Name = G->getGlobal()->getName();
1283 } else {// Considering the ExternalSymbol case here
1284 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
1285 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1286 Name = ES->getSymbol();
1287 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001288
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001289 // Label for argument passing
1290 char *argFrame = new char [strlen(Name.c_str()) + 8];
1291 sprintf(argFrame, "%s.args", Name.c_str());
1292 ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001293
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001294 // Label for reading return value
1295 char *retName = new char [strlen(Name.c_str()) + 8];
1296 sprintf(retName, "%s.retval", Name.c_str());
1297 RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1298 } else {
1299 // if indirect call
1300 SDValue CodeAddr_Lo = Callee.getOperand(0);
1301 SDValue CodeAddr_Hi = Callee.getOperand(1);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001302
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001303 CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
1304 DAG.getConstant(2, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001305
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001306 // move Hi part in PCLATH
1307 CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
1308 Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
1309 CodeAddr_Hi);
1310 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001311
1312 // Pass the argument to function before making the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001313 SDValue CallArgs;
1314 if (IsDirectCall) {
1315 CallArgs = LowerDirectCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1316 Chain = getChain(CallArgs);
1317 OperFlag = getOutFlag(CallArgs);
1318 } else {
1319 CallArgs = LowerIndirectCallArguments(Op, Chain, OperFlag, DataAddr_Lo,
1320 DataAddr_Hi, DAG);
1321 Chain = getChain(CallArgs);
1322 OperFlag = getOutFlag(CallArgs);
1323 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001324
1325 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001326 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001327 OperFlag);
1328 Chain = getChain(PICCall);
1329 OperFlag = getOutFlag(PICCall);
1330
1331
1332 // Carrying the Constant 0 along the CALLSEQSTART
1333 // because there is nothing else to carry.
1334 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1335 OperFlag);
1336 Chain = getChain(SeqEnd);
1337 OperFlag = getOutFlag(SeqEnd);
1338
1339 // Lower the return value reading after the call.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +00001340 if (IsDirectCall)
1341 return LowerDirectCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1342 else
1343 return LowerIndirectCallReturn(Op, Chain, OperFlag, DataAddr_Lo,
1344 DataAddr_Hi, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001345}
1346
1347bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1348 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1349 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1350 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1351 return true;
1352 return false;
1353}
1354
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001355// NeedToConvertToMemOp - Returns true if one of the operands of the
1356// operation 'Op' needs to be put into memory. Also returns the
1357// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1358// no instruction that can operation on two registers. Most insns take
1359// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001360bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001361 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001362 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1363 Op.getOperand(1).getOpcode() == ISD::Constant)
1364 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001365
1366 // Return false if one of the operands is already a direct
1367 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001368 if (isDirectLoad(Op.getOperand(0))) {
1369 if (Op.getOperand(0).hasOneUse())
1370 return false;
1371 else
1372 MemOp = 0;
1373 }
1374 if (isDirectLoad(Op.getOperand(1))) {
1375 if (Op.getOperand(1).hasOneUse())
1376 return false;
1377 else
1378 MemOp = 1;
1379 }
1380 return true;
1381}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001382
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001383// LowerBinOp - Lower a commutative binary operation that does not
1384// affect status flag carry.
1385SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001386 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001387
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001388 // We should have handled larger operands in type legalizer itself.
1389 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001390
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001391 unsigned MemOp = 1;
1392 if (NeedToConvertToMemOp(Op, MemOp)) {
1393 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001394 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001395
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001396 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001397 NewVal);
1398 }
1399 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001400 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001401 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001402}
1403
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001404// LowerADD - Lower all types of ADD operations including the ones
1405// that affects carry.
1406SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001407 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001408 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001409 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001410 unsigned MemOp = 1;
1411 if (NeedToConvertToMemOp(Op, MemOp)) {
1412 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001413 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001414
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001415 // ADDC and ADDE produces two results.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001416 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001417
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001418 // ADDE has three operands, the last one is a flag.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001419 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001420 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1421 NewVal, Op.getOperand(2));
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001422 // ADDC has two operands.
1423 else if (Op.getOpcode() == ISD::ADDC)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001424 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1425 NewVal);
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001426 // ADD it is. It produces only one result.
1427 else
1428 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
1429 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001430 }
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001431 else if (Op.getOpcode() == ISD::ADD)
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001432 return Op;
Sanjiv Gupta6e2a46a2009-04-09 04:03:43 +00001433 else
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001434 return SDValue();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001435}
1436
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001437SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001438 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001439 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001440 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001441
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001442 // Nothing to do if the first operand is already a direct load and it has
1443 // only one use.
1444 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001445 return SDValue();
1446
1447 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001448 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001449
1450 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001451 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001452 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001453 Op.getOperand(2));
1454 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001455 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001456}
1457
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001458// LowerFORMAL_ARGUMENTS - Argument values are loaded from the
1459// <fname>.args + offset. All arguments are already broken to leaglized
1460// types, so the offset just runs from 0 to NumArgVals - 1.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001461
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001462SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001463 SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001464 SmallVector<SDValue, 8> ArgValues;
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001465 unsigned NumArgVals = Op.getNode()->getNumValues() - 1;
Dale Johannesene8d72302009-02-06 23:05:02 +00001466 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001467 SDValue Chain = Op.getOperand(0); // Formal arguments' chain
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001468
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001469
1470 // Reset the map of FI and TmpOffset
1471 ResetTmpOffsetMap();
1472 // Get the callee's name to create the <fname>.args label to pass args.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001473 MachineFunction &MF = DAG.getMachineFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001474 const Function *F = MF.getFunction();
1475 std::string FuncName = F->getName();
1476
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001477 // Create the <fname>.args external symbol.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001478 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
1479 sprintf(tmpName, "%s.args", FuncName.c_str());
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001480 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001481
1482 // Load arg values from the label + offset.
1483 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001484 SDValue BS = DAG.getConstant(1, MVT::i8);
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001485 for (unsigned i = 0; i < NumArgVals ; ++i) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001486 SDValue Offset = DAG.getConstant(i, MVT::i8);
1487 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001488 Offset);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001489 Chain = getChain(PICLoad);
1490 ArgValues.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001491 }
1492
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001493 // Return a MERGE_VALUE node.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001494 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001495 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptacae1b622009-04-06 10:54:50 +00001496 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001497}
1498
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001499// Perform DAGCombine of PIC16Load.
1500// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001501SDValue PIC16TargetLowering::
1502PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1503 SelectionDAG &DAG = DCI.DAG;
1504 SDValue Chain = N->getOperand(0);
1505 if (N->hasNUsesOfValue(0, 0)) {
1506 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1507 }
1508 return SDValue();
1509}
1510
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001511// For all the functions with arguments some STORE nodes are generated
1512// that store the argument on the frameindex. However in PIC16 the arguments
1513// are passed on stack only. Therefore these STORE nodes are redundant.
1514// To remove these STORE nodes will be removed in PerformStoreCombine
1515//
1516// Currently this function is doint nothing and will be updated for removing
1517// unwanted store operations
1518SDValue PIC16TargetLowering::
1519PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001520 return SDValue(N, 0);
1521 /*
1522 // Storing an undef value is of no use, so remove it
1523 if (isStoringUndef(N, Chain, DAG)) {
1524 return Chain; // remove the store and return the chain
1525 }
1526 //else everything is ok.
1527 return SDValue(N, 0);
1528 */
1529}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001530
1531SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1532 DAGCombinerInfo &DCI) const {
1533 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001534 case ISD::STORE:
1535 return PerformStoreCombine(N, DCI);
1536 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001537 return PerformPIC16LoadCombine(N, DCI);
1538 }
1539 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001540}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001541
1542static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1543 switch (CC) {
1544 default: assert(0 && "Unknown condition code!");
1545 case ISD::SETNE: return PIC16CC::NE;
1546 case ISD::SETEQ: return PIC16CC::EQ;
1547 case ISD::SETGT: return PIC16CC::GT;
1548 case ISD::SETGE: return PIC16CC::GE;
1549 case ISD::SETLT: return PIC16CC::LT;
1550 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001551 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001552 case ISD::SETULE: return PIC16CC::LE;
1553 case ISD::SETUGE: return PIC16CC::GE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001554 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001555 }
1556}
1557
1558// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1559// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1560static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1561 ISD::CondCode CC, unsigned &SPCC) {
1562 if (isa<ConstantSDNode>(RHS) &&
1563 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1564 CC == ISD::SETNE &&
1565 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1566 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1567 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1568 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1569 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1570 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1571 SDValue CMPCC = LHS.getOperand(3);
1572 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1573 LHS = CMPCC.getOperand(0);
1574 RHS = CMPCC.getOperand(1);
1575 }
1576}
1577
1578// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1579SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1580 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001581 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001582 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1583
1584 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1585 // i.e. a < 12 can be rewritten as 12 > a.
1586 if (RHS.getOpcode() == ISD::Constant) {
1587
1588 SDValue Tmp = LHS;
1589 LHS = RHS;
1590 RHS = Tmp;
1591
1592 switch (CondCode) {
1593 default: break;
1594 case PIC16CC::LT:
1595 CondCode = PIC16CC::GT;
1596 break;
1597 case PIC16CC::GT:
1598 CondCode = PIC16CC::LT;
1599 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001600 case PIC16CC::ULT:
1601 CondCode = PIC16CC::UGT;
1602 break;
1603 case PIC16CC::UGT:
1604 CondCode = PIC16CC::ULT;
1605 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001606 case PIC16CC::GE:
1607 CondCode = PIC16CC::LE;
1608 break;
1609 case PIC16CC::LE:
1610 CondCode = PIC16CC::GE;
1611 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001612 case PIC16CC::ULE:
1613 CondCode = PIC16CC::UGE;
1614 break;
1615 case PIC16CC::UGE:
1616 CondCode = PIC16CC::ULE;
1617 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001618 }
1619 }
1620
1621 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001622
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001623 // These are signed comparisons.
1624 SDValue Mask = DAG.getConstant(128, MVT::i8);
1625 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001626 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1627 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001628 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001629
1630 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001631 // We can use a subtract operation to set the condition codes. But
1632 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001633 // Nothing to do if the first operand is already a valid type (direct load
1634 // for subwf and literal for sublw) and it is used by this operation only.
1635 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1636 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001637 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001638
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001639 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001640 LHS = ConvertToMemOperand (LHS, DAG, dl);
1641 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001642}
1643
1644
1645SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1646 SDValue LHS = Op.getOperand(0);
1647 SDValue RHS = Op.getOperand(1);
1648 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1649 SDValue TrueVal = Op.getOperand(2);
1650 SDValue FalseVal = Op.getOperand(3);
1651 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001652 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001653
1654 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1655 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1656 // i.e.
1657 // A setcc: lhs, rhs, cc is expanded by llvm to
1658 // select_cc: result of setcc, 0, 1, 0, setne
1659 // We can think of it as:
1660 // select_cc: lhs, rhs, 1, 0, cc
1661 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1662 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1663
1664 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001665 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001666
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001667 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001668 FalseVal, PIC16CC, Cmp.getValue(1));
1669}
1670
1671MachineBasicBlock *
1672PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001673 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001674 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1675 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001676 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001677
1678 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1679 // control-flow pattern. The incoming instruction knows the destination vreg
1680 // to set, the condition code register to branch on, the true/false values to
1681 // select between, and a branch opcode to use.
1682 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1683 MachineFunction::iterator It = BB;
1684 ++It;
1685
1686 // thisMBB:
1687 // ...
1688 // TrueVal = ...
1689 // [f]bCC copy1MBB
1690 // fallthrough --> copy0MBB
1691 MachineBasicBlock *thisMBB = BB;
1692 MachineFunction *F = BB->getParent();
1693 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1694 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001695 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001696 F->insert(It, copy0MBB);
1697 F->insert(It, sinkMBB);
1698
1699 // Update machine-CFG edges by transferring all successors of the current
1700 // block to the new block which will contain the Phi node for the select.
1701 sinkMBB->transferSuccessors(BB);
1702 // Next, add the true and fallthrough blocks as its successors.
1703 BB->addSuccessor(copy0MBB);
1704 BB->addSuccessor(sinkMBB);
1705
1706 // copy0MBB:
1707 // %FalseValue = ...
1708 // # fallthrough to sinkMBB
1709 BB = copy0MBB;
1710
1711 // Update machine-CFG edges
1712 BB->addSuccessor(sinkMBB);
1713
1714 // sinkMBB:
1715 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1716 // ...
1717 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001718 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001719 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1720 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1721
1722 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1723 return BB;
1724}
1725
1726
1727SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1728 SDValue Chain = Op.getOperand(0);
1729 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1730 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1731 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1732 SDValue Dest = Op.getOperand(4); // BB to jump to
1733 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001734 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001735
1736 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1737 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1738 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1739 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1740
1741 // Get the Compare insn and condition code.
1742 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001743 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001744
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001745 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001746 Cmp.getValue(1));
1747}
1748