blob: 3ace4821d8d8a8dd61530f2f847f71afac3e492b [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001//
2// The LLVM Compiler Infrastructure
3//
4// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the interfaces that PIC16 uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "pic16-lower"
15
16#include "PIC16ISelLowering.h"
17#include "PIC16TargetMachine.h"
18#include "llvm/DerivedTypes.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000019#include "llvm/GlobalValue.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000020#include "llvm/Function.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000021#include "llvm/CallingConv.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
Duncan Sandsc6dbe7f2008-11-28 10:20:03 +000026#include <cstdio>
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000027
28
Sanjiv Gupta0e687712008-05-13 09:02:57 +000029using namespace llvm;
30
Sanjiv Gupta0e687712008-05-13 09:02:57 +000031
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000032// PIC16TargetLowering Constructor.
33PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
34 : TargetLowering(TM) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000035
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000036 Subtarget = &TM.getSubtarget<PIC16Subtarget>();
Sanjiv Gupta0e687712008-05-13 09:02:57 +000037
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000038 addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000039
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000040 setShiftAmountType(MVT::i8);
41 setShiftAmountFlavor(Extend);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000042
Sanjiv Gupta1b046942009-01-13 19:18:47 +000043 // SRA library call names
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000044 setPIC16LibcallName(PIC16ISD::SRA_I8, "__intrinsics.sra.i8");
45 setLibcallName(RTLIB::SRA_I16, "__intrinsics.sra.i16");
46 setLibcallName(RTLIB::SRA_I32, "__intrinsics.sra.i32");
Sanjiv Gupta1b046942009-01-13 19:18:47 +000047
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000048 // SHL library call names
49 setPIC16LibcallName(PIC16ISD::SLL_I8, "__intrinsics.sll.i8");
50 setLibcallName(RTLIB::SHL_I16, "__intrinsics.sll.i16");
51 setLibcallName(RTLIB::SHL_I32, "__intrinsics.sll.i32");
Sanjiv Gupta1b046942009-01-13 19:18:47 +000052
53 // SRL library call names
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000054 setPIC16LibcallName(PIC16ISD::SRL_I8, "__intrinsics.srl.i8");
55 setLibcallName(RTLIB::SRL_I16, "__intrinsics.srl.i16");
56 setLibcallName(RTLIB::SRL_I32, "__intrinsics.srl.i32");
57
58 // MUL Library call names
59 setPIC16LibcallName(PIC16ISD::MUL_I8, "__intrinsics.mul.i8");
60 setLibcallName(RTLIB::MUL_I16, "__intrinsics.mul.i16");
61 setLibcallName(RTLIB::MUL_I32, "__intrinsics.mul.i32");
Sanjiv Gupta0e687712008-05-13 09:02:57 +000062
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000063 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000064 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000065
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000066 setOperationAction(ISD::LOAD, MVT::i8, Legal);
67 setOperationAction(ISD::LOAD, MVT::i16, Custom);
68 setOperationAction(ISD::LOAD, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000069
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000070 setOperationAction(ISD::STORE, MVT::i8, Legal);
71 setOperationAction(ISD::STORE, MVT::i16, Custom);
72 setOperationAction(ISD::STORE, MVT::i32, Custom);
Sanjiv Gupta0e687712008-05-13 09:02:57 +000073
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000074 setOperationAction(ISD::ADDE, MVT::i8, Custom);
75 setOperationAction(ISD::ADDC, MVT::i8, Custom);
76 setOperationAction(ISD::SUBE, MVT::i8, Custom);
77 setOperationAction(ISD::SUBC, MVT::i8, Custom);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000078 setOperationAction(ISD::ADD, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000079 setOperationAction(ISD::ADD, MVT::i16, Custom);
80
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +000081 setOperationAction(ISD::OR, MVT::i8, Custom);
82 setOperationAction(ISD::AND, MVT::i8, Custom);
83 setOperationAction(ISD::XOR, MVT::i8, Custom);
84
Sanjiv Gupta1b046942009-01-13 19:18:47 +000085 setOperationAction(ISD::FrameIndex, MVT::i16, Custom);
86 setOperationAction(ISD::CALL, MVT::i16, Custom);
87 setOperationAction(ISD::RET, MVT::Other, Custom);
88
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000089 setOperationAction(ISD::MUL, MVT::i8, Custom);
90 setOperationAction(ISD::MUL, MVT::i16, Expand);
91 setOperationAction(ISD::MUL, MVT::i32, Expand);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000092
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000093 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
94 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
95 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
96 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
97 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
98 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
99 setOperationAction(ISD::MULHU, MVT::i8, Expand);
100 setOperationAction(ISD::MULHU, MVT::i16, Expand);
101 setOperationAction(ISD::MULHU, MVT::i32, Expand);
102 setOperationAction(ISD::MULHS, MVT::i8, Expand);
103 setOperationAction(ISD::MULHS, MVT::i16, Expand);
104 setOperationAction(ISD::MULHS, MVT::i32, Expand);
105
106 setOperationAction(ISD::SRA, MVT::i8, Custom);
107 setOperationAction(ISD::SRA, MVT::i16, Expand);
108 setOperationAction(ISD::SRA, MVT::i32, Expand);
109 setOperationAction(ISD::SHL, MVT::i8, Custom);
110 setOperationAction(ISD::SHL, MVT::i16, Expand);
111 setOperationAction(ISD::SHL, MVT::i32, Expand);
112 setOperationAction(ISD::SRL, MVT::i8, Custom);
113 setOperationAction(ISD::SRL, MVT::i16, Expand);
114 setOperationAction(ISD::SRL, MVT::i32, Expand);
115
116 // PIC16 does not support shift parts
117 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
118 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
119 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
120 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
121 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
122 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
123 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
124 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
125 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
126
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000127
128 // PIC16 does not have a SETCC, expand it to SELECT_CC.
129 setOperationAction(ISD::SETCC, MVT::i8, Expand);
130 setOperationAction(ISD::SELECT, MVT::i8, Expand);
131 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
132 setOperationAction(ISD::BRIND, MVT::Other, Expand);
133
134 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
135 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000136
137 //setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
138 setTruncStoreAction(MVT::i16, MVT::i8, Custom);
139
140 // Now deduce the information based on the above mentioned
141 // actions
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000142 computeRegisterProperties();
143}
144
Sanjiv 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}
155
156// To extract chain value from the SDValue Nodes
157// This function will help to maintain the chain extracting
158// code at one place. In case of any change in future it will
159// help maintain the code.
160static SDValue getChain(SDValue &Op) {
161 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
162
163 // If the last value returned in Flag then the chain is
164 // second last value returned.
165 if (Chain.getValueType() == MVT::Flag)
166 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
167
168 // All nodes may not produce a chain. Therefore following assert
169 // verifies that the node is returning a chain only.
170 assert (Chain.getValueType() == MVT::Other
171 && "Node does not have a chain");
172
173 return Chain;
174}
175
176/// PopulateResults - Helper function to LowerOperation.
177/// If a node wants to return multiple results after lowering,
178/// it stuffs them into an array of SDValue called Results.
179
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000180static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
181 if (N.getOpcode() == ISD::MERGE_VALUES) {
182 int NumResults = N.getNumOperands();
183 for( int i = 0; i < NumResults; i++)
184 Results.push_back(N.getOperand(i));
185 }
186 else
187 Results.push_back(N);
188}
189
190MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
191 return MVT::i8;
192}
193
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000194/// The type legalizer framework of generating legalizer can generate libcalls
195/// only when the operand/result types are illegal.
196/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
197/// For example an arithmetic right shift. These functions are used to lower
198/// such operations that generate libcall for legal types.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000199
200void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000201PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000202 const char *Name) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000203 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000204}
205
206const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000207PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000208 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000209}
210
211SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000212PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000213 MVT RetVT, const SDValue *Ops,
214 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000215 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000216
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000217 TargetLowering::ArgListTy Args;
218 Args.reserve(NumOps);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000219
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000220 TargetLowering::ArgListEntry Entry;
221 for (unsigned i = 0; i != NumOps; ++i) {
222 Entry.Node = Ops[i];
223 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
224 Entry.isSExt = isSigned;
225 Entry.isZExt = !isSigned;
226 Args.push_back(Entry);
227 }
228 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000229
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000230 const Type *RetTy = RetVT.getTypeForMVT();
231 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000232 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000233 false, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000234
235 return CallInfo.first;
236}
237
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000238const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
239 switch (Opcode) {
240 default: return NULL;
241 case PIC16ISD::Lo: return "PIC16ISD::Lo";
242 case PIC16ISD::Hi: return "PIC16ISD::Hi";
243 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
244 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
245 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
246 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000247 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000248 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000249 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000250 case PIC16ISD::BCF: return "PIC16ISD::BCF";
251 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
252 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
253 case PIC16ISD::RLF: return "PIC16ISD::RLF";
254 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000255 case PIC16ISD::CALL: return "PIC16ISD::CALL";
256 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
257 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
258 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000259 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000260 }
261}
262
Duncan Sands1607f052008-12-01 11:39:25 +0000263void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
264 SmallVectorImpl<SDValue>&Results,
265 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000266
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000267 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000268 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000269 Results.push_back(ExpandGlobalAddress(N, DAG));
270 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000271 case ISD::ExternalSymbol:
272 Results.push_back(ExpandExternalSymbol(N, DAG));
273 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000274 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000275 Results.push_back(ExpandStore(N, DAG));
276 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000277 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000278 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000279 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000280 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000281 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000282 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000283 case ISD::FrameIndex:
284 Results.push_back(ExpandFrameIndex(N, DAG));
285 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000286 default:
287 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000288 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000289 }
290}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000291
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000292SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
293
294 // Currently handling FrameIndex of size MVT::i16 only
295 // One example of this scenario is when return value is written on
296 // FrameIndex#0
297
298 if (N->getValueType(0) != MVT::i16)
299 return SDValue();
300
301 // Expand the FrameIndex into ExternalSymbol and a Constant node
302 // The constant will represent the frame index number
303 // Get the current function frame
304 MachineFunction &MF = DAG.getMachineFunction();
305 const Function *Func = MF.getFunction();
306 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000307
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000308 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000309 // FIXME there isn't really debug info here
310 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000311 int Index = FR->getIndex();
312
313 SDValue FI[2];
314 FI[0] = DAG.getTargetFrameIndex(Index, MVT::i8);
315 FI[1] = DAG.getTargetFrameIndex(Index + 1, MVT::i8);
Dale Johannesende064702009-02-06 21:50:26 +0000316 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), FI[0], FI[1]);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000317}
318
319
320SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000321 StoreSDNode *St = cast<StoreSDNode>(N);
322 SDValue Chain = St->getChain();
323 SDValue Src = St->getValue();
324 SDValue Ptr = St->getBasePtr();
325 MVT ValueType = Src.getValueType();
326 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000327 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000328
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000329 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000330 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000331
332 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000333 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000334 PtrLo, PtrHi,
335 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000336 }
337 else if (ValueType == MVT::i16) {
338 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
339 SDValue SrcLo, SrcHi;
340 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
341 SDValue ChainLo = Chain, ChainHi = Chain;
342 if (Chain.getOpcode() == ISD::TokenFactor) {
343 ChainLo = Chain.getOperand(0);
344 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000345 }
Dale Johannesende064702009-02-06 21:50:26 +0000346 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000347 ChainLo,
348 SrcLo, PtrLo, PtrHi,
349 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000350
Dale Johannesende064702009-02-06 21:50:26 +0000351 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000352 SrcHi, PtrLo, PtrHi,
353 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000354
Dale Johannesende064702009-02-06 21:50:26 +0000355 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000356 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000357 }
358 else if (ValueType == MVT::i32) {
359 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
360 SDValue SrcLo, SrcHi;
361 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000362
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000363 // Get the expanded parts of each of SrcLo and SrcHi.
364 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
365 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
366 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000367
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000368 SDValue ChainLo = Chain, ChainHi = Chain;
369 if (Chain.getOpcode() == ISD::TokenFactor) {
370 ChainLo = Chain.getOperand(0);
371 ChainHi = Chain.getOperand(1);
372 }
373 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
374 ChainHi2 = ChainHi;
375 if (ChainLo.getOpcode() == ISD::TokenFactor) {
376 ChainLo1 = ChainLo.getOperand(0);
377 ChainLo2 = ChainLo.getOperand(1);
378 }
379 if (ChainHi.getOpcode() == ISD::TokenFactor) {
380 ChainHi1 = ChainHi.getOperand(0);
381 ChainHi2 = ChainHi.getOperand(1);
382 }
Dale Johannesende064702009-02-06 21:50:26 +0000383 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000384 ChainLo1,
385 SrcLo1, PtrLo, PtrHi,
386 DAG.getConstant (0 + StoreOffset, MVT::i8));
387
Dale Johannesende064702009-02-06 21:50:26 +0000388 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000389 SrcLo2, PtrLo, PtrHi,
390 DAG.getConstant (1 + StoreOffset, MVT::i8));
391
Dale Johannesende064702009-02-06 21:50:26 +0000392 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000393 SrcHi1, PtrLo, PtrHi,
394 DAG.getConstant (2 + StoreOffset, MVT::i8));
395
Dale Johannesende064702009-02-06 21:50:26 +0000396 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000397 SrcHi2, PtrLo, PtrHi,
398 DAG.getConstant (3 + StoreOffset, MVT::i8));
399
Dale Johannesende064702009-02-06 21:50:26 +0000400 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
401 getChain(Store1), getChain(Store2));
402 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
403 getChain(Store3), getChain(Store4));
404 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000405
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000406 }
407 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000408 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000409 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000410 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000411}
412
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000413SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
414{
415 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000416 // FIXME there isn't really debug info here
417 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000418
419 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
420
Dale Johannesende064702009-02-06 21:50:26 +0000421 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES);
422 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000423
Dale Johannesende064702009-02-06 21:50:26 +0000424 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000425}
426
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000427// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000428SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000429 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000430 // FIXME there isn't really debug info here
431 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000432
433 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
434 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000435
Dale Johannesende064702009-02-06 21:50:26 +0000436 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA);
437 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000438
Dale Johannesende064702009-02-06 21:50:26 +0000439 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000440}
441
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000442bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
443 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000444
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000445 if (Op.getOpcode() == ISD::BUILD_PAIR) {
446 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
447 return true;
448 }
449 return false;
450}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000451
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000452// Return true if DirectAddress is in ROM_SPACE
453bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000454
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000455 // RomAddress is a GlobalAddress in ROM_SPACE_
456 // If the Op is not a GlobalAddress return NULL without checking
457 // anything further.
458 if (!isDirectAddress(Op))
459 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000460
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000461 // Its a GlobalAddress.
462 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
463 SDValue TGA = Op.getOperand(0).getOperand(0);
464 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
465 const Type *ValueType = GSDN->getGlobal()->getType();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000466
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000467 if (!isa<PointerType>(ValueType)) {
468 assert(0 && "TGA must be of a PointerType");
469 }
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000470
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000471 int AddrSpace = dyn_cast<PointerType>(ValueType)->getAddressSpace();
472 if (AddrSpace == PIC16ISD::ROM_SPACE)
473 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000474
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000475 // Any other address space return it false
476 return false;
477}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000478
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000479
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000480// GetExpandedParts - This function is on the similiar lines as
481// the GetExpandedInteger in type legalizer is. This returns expanded
482// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000483
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000484void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
485 SDValue &Lo, SDValue &Hi) {
486 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000487 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000488 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000489
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000490 // Extract the lo component.
491 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
492 DAG.getConstant(0, MVT::i8));
493
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000494 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000495 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
496 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000497}
498
499// Legalize FrameIndex into ExternalSymbol and offset.
500void
501PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
502 SDValue &ES, int &Offset) {
503
504 MachineFunction &MF = DAG.getMachineFunction();
505 const Function *Func = MF.getFunction();
506 const std::string Name = Func->getName();
507
508 char *tmpName = new char [strlen(Name.c_str()) + 8];
509 sprintf(tmpName, "%s.args", Name.c_str());
510 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
511 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
512 Offset = FR->getIndex();
513
514 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000515}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000516
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000517// This function legalizes the PIC16 Addresses. If the Pointer is
518// -- Direct address variable residing
519// --> then a Banksel for that variable will be created.
520// -- Rom variable
521// --> then it will be treated as an indirect address.
522// -- Indirect address
523// --> then the address will be loaded into FSR
524// -- ADD with constant operand
525// --> then constant operand of ADD will be returned as Offset
526// and non-constant operand of ADD will be treated as pointer.
527// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000528
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000529void PIC16TargetLowering:: LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
530 SDValue &Lo, SDValue &Hi,
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000531 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000532
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000533 // Offset, by default, should be 0
534 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000535
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000536 // If the pointer is ADD with constant,
537 // return the constant value as the offset
538 if (Ptr.getOpcode() == ISD::ADD) {
539 SDValue OperLeft = Ptr.getOperand(0);
540 SDValue OperRight = Ptr.getOperand(1);
541 if (OperLeft.getOpcode() == ISD::Constant) {
542 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
543 Ptr = OperRight;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000544 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000545 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000546 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000547 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000548 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000549
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000550 // If the pointer is Type i8 and an external symbol
551 // then treat it as direct address.
552 // One example for such case is storing and loading
553 // from function frame during a call
554 if (Ptr.getValueType() == MVT::i8) {
555 switch (Ptr.getOpcode()) {
556 case ISD::TargetExternalSymbol:
557 Lo = Ptr;
558 Hi = DAG.getConstant(1, MVT::i8);
559 return;
560 }
561 }
562
563 if (Ptr.getOpcode() == ISD::BUILD_PAIR &&
564 Ptr.getOperand(0).getOpcode() == ISD::TargetFrameIndex) {
565
566 int FrameOffset;
567 LegalizeFrameIndex(Ptr.getOperand(0), DAG, Lo, FrameOffset);
568 Hi = DAG.getConstant(1, MVT::i8);
569 Offset += FrameOffset;
570 return;
571 }
572
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000573 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
574 // Direct addressing case for RAM variables. The Hi part is constant
575 // and the Lo part is the TGA itself.
576 Lo = Ptr.getOperand(0).getOperand(0);
577
578 // For direct addresses Hi is a constant. Value 1 for the constant
579 // signifies that banksel needs to generated for it. Value 0 for
580 // the constant signifies that banksel does not need to be generated
581 // for it. Mark it as 1 now and optimize later.
582 Hi = DAG.getConstant(1, MVT::i8);
583 return;
584 }
585
586 // Indirect addresses. Get the hi and lo parts of ptr.
587 GetExpandedParts(Ptr, DAG, Lo, Hi);
588
589 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000590 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
591 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000592
593 return;
594}
595
Duncan Sands1607f052008-12-01 11:39:25 +0000596SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000597 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
598 SDValue Chain = LD->getChain();
599 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000600 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000601
602 SDValue Load, Offset;
603 SDVTList Tys;
604 MVT VT, NewVT;
605 SDValue PtrLo, PtrHi;
606 unsigned LoadOffset;
607
608 // Legalize direct/indirect addresses. This will give the lo and hi parts
609 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000610 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000611
612 // Load from the pointer (direct address or FSR)
613 VT = N->getValueType(0);
614 unsigned NumLoads = VT.getSizeInBits() / 8;
615 std::vector<SDValue> PICLoads;
616 unsigned iter;
617 MVT MemVT = LD->getMemoryVT();
618 if(ISD::isNON_EXTLoad(N)) {
619 for (iter=0; iter<NumLoads ; ++iter) {
620 // Add the pointer offset if any
621 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
622 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000623 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000624 Offset);
625 PICLoads.push_back(Load);
626 }
627 } else {
628 // If it is extended load then use PIC16Load for Memory Bytes
629 // and for all extended bytes perform action based on type of
630 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
631
632
633 // For extended loads this is the memory value type
634 // i.e. without any extension
635 MVT MemVT = LD->getMemoryVT();
636 unsigned MemBytes = MemVT.getSizeInBits() / 8;
637 unsigned ExtdBytes = VT.getSizeInBits() / 8;
638 Offset = DAG.getConstant(LoadOffset, MVT::i8);
639
640 Tys = DAG.getVTList(MVT::i8, MVT::Other);
641 // For MemBytes generate PIC16Load with proper offset
642 for (iter=0; iter<MemBytes; ++iter) {
643 // Add the pointer offset if any
644 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000645 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000646 Offset);
647 PICLoads.push_back(Load);
648 }
649
650 // For SignExtendedLoad
651 if (ISD::isSEXTLoad(N)) {
652 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
653 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000654 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000655 DAG.getConstant(7, MVT::i8));
656 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
657 PICLoads.push_back(SRA);
658 }
659 } else if (ISD::isZEXTLoad(N)) {
660 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
661 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
662 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
663 PICLoads.push_back(ConstZero);
664 }
665 }
666 }
667 SDValue BP;
668
669 if (VT == MVT::i8) {
670 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000671 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000672 }
673 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000674 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000675 if (MemVT == MVT::i8)
676 Chain = getChain(PICLoads[0]);
677 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000678 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
679 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000680 } else if (VT == MVT::i32) {
681 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000682 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
683 PICLoads[0], PICLoads[1]);
684 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
685 PICLoads[2], PICLoads[3]);
686 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000687 if (MemVT == MVT::i8)
688 Chain = getChain(PICLoads[0]);
689 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000690 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
691 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000692 else {
693 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000694 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000695 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000696 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000697 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000698 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
699 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000700 }
701 }
702 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000703 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000704}
705
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000706SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
707 // We should have handled larger operands in type legalizer itself.
708 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
709
710 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000711 SDValue Value = N->getOperand(0);
712 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000713 PIC16ISD::PIC16Libcall CallCode;
714 switch (N->getOpcode()) {
715 case ISD::SRA:
716 CallCode = PIC16ISD::SRA_I8;
717 break;
718 case ISD::SHL:
719 CallCode = PIC16ISD::SLL_I8;
720 break;
721 case ISD::SRL:
722 CallCode = PIC16ISD::SRL_I8;
723 break;
724 default:
725 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000726 return SDValue();
727 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000728 SmallVector<SDValue, 2> Ops(2);
729 Ops[0] = Value;
730 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000731 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
732 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000733 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000734}
735
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000736void
737PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
738 SmallVectorImpl<SDValue>&Results,
739 SelectionDAG &DAG) {
740 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000741 SDValue Res;
742 unsigned i;
743 switch (Op.getOpcode()) {
744 case ISD::FORMAL_ARGUMENTS:
745 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
746 case ISD::LOAD:
747 Res = ExpandLoad(Op.getNode(), DAG); break;
748 case ISD::CALL:
749 Res = LowerCALL(Op, DAG); break;
750 default: {
751 // All other operations are handled in LowerOperation.
752 Res = LowerOperation(Op, DAG);
753 if (Res.getNode())
754 Results.push_back(Res);
755
756 return;
757 }
758 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000759
760 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000761 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000762 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000763 Results.push_back(SDValue(N, i));
764 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000765}
766
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000767SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
768 switch (Op.getOpcode()) {
769 case ISD::FORMAL_ARGUMENTS:
770 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000771 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000772 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000773 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000774 return LowerADD(Op, DAG);
775 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000776 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000777 case ISD::SUBE:
778 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000779 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000780 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000781 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000782 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000783 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000784 case ISD::SRA:
785 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000786 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000787 case ISD::OR:
788 case ISD::AND:
789 case ISD::XOR:
790 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000791 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000792 return LowerCALL(Op, DAG);
793 case ISD::RET:
794 return LowerRET(Op, DAG);
795 case ISD::BR_CC:
796 return LowerBR_CC(Op, DAG);
797 case ISD::SELECT_CC:
798 return LowerSELECT_CC(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000799 }
Dan Gohman475871a2008-07-27 21:46:04 +0000800 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000801}
802
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000803SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000804 SelectionDAG &DAG,
805 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000806 assert (Op.getValueType() == MVT::i8
807 && "illegal value type to store on stack.");
808
809 MachineFunction &MF = DAG.getMachineFunction();
810 const Function *Func = MF.getFunction();
811 const std::string FuncName = Func->getName();
812
813 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
814
815 // Put the value on stack.
816 // Get a stack slot index and convert to es.
817 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
818 sprintf(tmpName, "%s.tmp", FuncName.c_str());
819 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
820
821 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000822 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000823 DAG.getEntryNode(),
824 Op, ES,
825 DAG.getConstant (1, MVT::i8), // Banksel.
826 DAG.getConstant (FI, MVT::i8));
827
828 // Load the value from ES.
829 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000830 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000831 ES, DAG.getConstant (1, MVT::i8),
832 DAG.getConstant (FI, MVT::i8));
833
834 return Load.getValue(0);
835}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000836
837SDValue
838PIC16TargetLowering::LowerCallArguments(SDValue Op, SDValue Chain,
839 SDValue FrameAddress,
840 SDValue InFlag,
841 SelectionDAG &DAG) {
842 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
843 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +0000844 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000845 std::string Name;
846 SDValue Arg, StoreAt;
847 MVT ArgVT;
848 unsigned Size=0;
849 unsigned ArgCount=0;
850
851
852 // FIXME: This portion of code currently assumes only
853 // primitive types being passed as arguments.
854
855 // Legalize the address before use
856 SDValue PtrLo, PtrHi;
857 unsigned AddressOffset;
858 int StoreOffset = 0;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000859 LegalizeAddress(FrameAddress, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000860 SDValue StoreRet;
861
862 std::vector<SDValue> Ops;
863 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
864 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
865 // Get the argument
866 Arg = TheCall->getArg(i);
867
868 StoreOffset = (Offset + AddressOffset);
869
870 // Store the argument on frame
871
872 Ops.clear();
873 Ops.push_back(Chain);
874 Ops.push_back(Arg.getValue(0));
875 Ops.push_back(PtrLo);
876 Ops.push_back(PtrHi);
877 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
878 Ops.push_back(InFlag);
879
Dale Johannesene8d72302009-02-06 23:05:02 +0000880 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000881
882 Chain = getChain(StoreRet);
883 InFlag = getOutFlag(StoreRet);
884
885 // Update the frame offset to be used for next argument
886 ArgVT = Arg.getValueType();
887 Size = ArgVT.getSizeInBits();
888 Size = Size/8; // Calculate size in bytes
889 Offset += Size; // Increase the frame offset
890 }
891 return Chain;
892}
893
894SDValue
895PIC16TargetLowering::LowerCallReturn(SDValue Op, SDValue Chain,
896 SDValue FrameAddress,
897 SDValue InFlag,
898 SelectionDAG &DAG) {
899 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000900 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000901 // Currently handling primitive types only. They will come in
902 // i8 parts
903 unsigned RetVals = TheCall->getNumRetVals();
904
905 std::vector<SDValue> ResultVals;
906
907 // Return immediately if the return type is void
908 if (RetVals == 0)
909 return Chain;
910
911 // Call has something to return
912
913 // Legalize the address before use
914 SDValue LdLo, LdHi;
915 unsigned LdOffset;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000916 LegalizeAddress(FrameAddress, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000917
918 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
919 SDValue LoadRet;
920
921 for(unsigned i=0, Offset=0;i<RetVals;i++) {
922
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000923 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000924 DAG.getConstant(LdOffset + Offset, MVT::i8),
925 InFlag);
926
927 InFlag = getOutFlag(LoadRet);
928
929 Chain = getChain(LoadRet);
930 Offset++;
931 ResultVals.push_back(LoadRet);
932 }
933
934 // To return use MERGE_VALUES
935 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000936 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000937 return Res;
938}
939
940SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
941 //int NumOps = Op.getNode()->getNumOperands();
942
943 // For default cases LLVM returns the value on the function frame
944 // So let LLVM do this for all the cases other than character
945 return Op;
946}
947
948SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
949 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
950 SDValue Chain = TheCall->getChain();
951 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000952 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000953 unsigned i =0;
954 if (Callee.getValueType() == MVT::i16 &&
955 Callee.getOpcode() == ISD::BUILD_PAIR) {
956 // It has come from TypeLegalizer for lowering
957
958 Callee = Callee.getOperand(0).getOperand(0);
959
960 std::vector<SDValue> Ops;
961 Ops.push_back(Chain);
962 Ops.push_back(Callee);
963
964 // Add the call arguments and their flags
965 unsigned NumArgs = TheCall->getNumArgs();
966 for(i=0;i<NumArgs;i++) {
967 Ops.push_back(TheCall->getArg(i));
968 Ops.push_back(TheCall->getArgFlagsVal(i));
969 }
970
971 std::vector<MVT> NodeTys;
972 unsigned NumRets = TheCall->getNumRetVals();
973 for(i=0;i<NumRets;i++)
974 NodeTys.push_back(TheCall->getRetValType(i));
975
976 // Return a Chain as well
977 NodeTys.push_back(MVT::Other);
978
979 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
980 SDValue NewCall =
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000981 DAG.getCall(TheCall->getCallingConv(), dl,
Dale Johannesen8ad9b432009-02-04 01:17:06 +0000982 TheCall->isVarArg(), TheCall->isTailCall(),
983 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000984
985 return NewCall;
986 }
987
988 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
989
990 // Start the call sequence.
991 // Carring the Constant 0 along the CALLSEQSTART
992 // because there is nothing else to carry.
993 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
994 Chain = getChain(SeqStart);
995
996 // For any direct call - callee will be GlobalAddressNode or
997 // ExternalSymbol
998
999 // Considering the GlobalAddressNode case here.
1000 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1001 GlobalValue *GV = G->getGlobal();
1002 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1003 }
1004
1005 // Considering the ExternalSymbol case here
1006 if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1007 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1008 }
1009
1010 SDValue OperFlag = getOutFlag(Chain); // To manage the data dependency
1011
1012 std::string Name;
1013
1014 // Considering GlobalAddress here
1015 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1016 Name = G->getGlobal()->getName();
1017
1018 // Considering ExternalSymbol here
1019 if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee))
1020 Name = ES->getSymbol();
1021
1022 char *argFrame = new char [strlen(Name.c_str()) + 8];
1023 sprintf(argFrame, "%s.args", Name.c_str());
1024 SDValue ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
1025
1026 char *retName = new char [strlen(Name.c_str()) + 8];
1027 sprintf(retName, "%s.retval", Name.c_str());
1028 SDValue RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1029
1030 // Pass the argument to function before making the call.
1031 SDValue CallArgs = LowerCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1032 Chain = getChain(CallArgs);
1033 OperFlag = getOutFlag(CallArgs);
1034
1035 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001036 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001037 OperFlag);
1038 Chain = getChain(PICCall);
1039 OperFlag = getOutFlag(PICCall);
1040
1041
1042 // Carrying the Constant 0 along the CALLSEQSTART
1043 // because there is nothing else to carry.
1044 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1045 OperFlag);
1046 Chain = getChain(SeqEnd);
1047 OperFlag = getOutFlag(SeqEnd);
1048
1049 // Lower the return value reading after the call.
1050 return LowerCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1051}
1052
1053bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1054 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1055 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1056 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1057 return true;
1058 return false;
1059}
1060
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001061// NeedToConvertToMemOp - Returns true if one of the operands of the
1062// operation 'Op' needs to be put into memory. Also returns the
1063// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1064// no instruction that can operation on two registers. Most insns take
1065// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001066bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001067 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001068 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1069 Op.getOperand(1).getOpcode() == ISD::Constant)
1070 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001071
1072 // Return false if one of the operands is already a direct
1073 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001074 if (isDirectLoad(Op.getOperand(0))) {
1075 if (Op.getOperand(0).hasOneUse())
1076 return false;
1077 else
1078 MemOp = 0;
1079 }
1080 if (isDirectLoad(Op.getOperand(1))) {
1081 if (Op.getOperand(1).hasOneUse())
1082 return false;
1083 else
1084 MemOp = 1;
1085 }
1086 return true;
1087}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001088
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001089// LowerBinOp - Lower a commutative binary operation that does not
1090// affect status flag carry.
1091SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001092 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001093
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001094 // We should have handled larger operands in type legalizer itself.
1095 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001096
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001097 unsigned MemOp = 1;
1098 if (NeedToConvertToMemOp(Op, MemOp)) {
1099 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001100 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001101
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001102 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001103 NewVal);
1104 }
1105 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001106 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001107 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001108}
1109
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001110// LowerADD - Lower all types of ADD operations including the ones
1111// that affects carry.
1112SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001113 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001114 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001115 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001116 unsigned MemOp = 1;
1117 if (NeedToConvertToMemOp(Op, MemOp)) {
1118 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001119 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001120
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001121 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001122
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001123 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001124 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1125 NewVal, Op.getOperand(2));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001126 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001127 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1128 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001129 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001130 else if (Op.getOpcode() == ISD::ADD) {
1131 return Op;
1132 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001133 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001134 return SDValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001135 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001136}
1137
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001138SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001139 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001140 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001141 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001142
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001143 // Nothing to do if the first operand is already a direct load and it has
1144 // only one use.
1145 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001146 return SDValue();
1147
1148 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001149 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001150
1151 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001152 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001153 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001154 Op.getOperand(2));
1155 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001156 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001157}
1158
1159// LowerFORMAL_ARGUMENTS - In Lowering FORMAL ARGUMENTS - MERGE_VALUES nodes
1160// is returned. MERGE_VALUES nodes number of operands and number of values are
1161// equal. Therefore to construct MERGE_VALUE node, UNDEF nodes equal to the
1162// number of arguments of function have been created.
1163
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001164SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001165 SelectionDAG &DAG) {
1166 SmallVector<SDValue, 8> ArgValues;
1167 unsigned NumArgs = Op.getNumOperands() - 3;
Dale Johannesene8d72302009-02-06 23:05:02 +00001168 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001169
1170 // Creating UNDEF nodes to meet the requirement of MERGE_VALUES node.
1171 for(unsigned i = 0 ; i<NumArgs ; i++) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001172 SDValue TempNode = DAG.getUNDEF(Op.getNode()->getValueType(i));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001173 ArgValues.push_back(TempNode);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001174 }
1175
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001176 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001177 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001178 &ArgValues[0],
1179 ArgValues.size()).getValue(Op.getResNo());
1180}
1181
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001182// Perform DAGCombine of PIC16Load.
1183// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001184SDValue PIC16TargetLowering::
1185PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1186 SelectionDAG &DAG = DCI.DAG;
1187 SDValue Chain = N->getOperand(0);
1188 if (N->hasNUsesOfValue(0, 0)) {
1189 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1190 }
1191 return SDValue();
1192}
1193
1194
1195SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1196 DAGCombinerInfo &DCI) const {
1197 switch (N->getOpcode()) {
1198 case PIC16ISD::PIC16Load:
1199 return PerformPIC16LoadCombine(N, DCI);
1200 }
1201 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001202}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001203
1204static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1205 switch (CC) {
1206 default: assert(0 && "Unknown condition code!");
1207 case ISD::SETNE: return PIC16CC::NE;
1208 case ISD::SETEQ: return PIC16CC::EQ;
1209 case ISD::SETGT: return PIC16CC::GT;
1210 case ISD::SETGE: return PIC16CC::GE;
1211 case ISD::SETLT: return PIC16CC::LT;
1212 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001213 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001214 case ISD::SETULE: return PIC16CC::LE;
1215 case ISD::SETUGE: return PIC16CC::GE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001216 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001217 }
1218}
1219
1220// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1221// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1222static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1223 ISD::CondCode CC, unsigned &SPCC) {
1224 if (isa<ConstantSDNode>(RHS) &&
1225 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1226 CC == ISD::SETNE &&
1227 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1228 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1229 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1230 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1231 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1232 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1233 SDValue CMPCC = LHS.getOperand(3);
1234 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1235 LHS = CMPCC.getOperand(0);
1236 RHS = CMPCC.getOperand(1);
1237 }
1238}
1239
1240// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1241SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1242 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001243 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001244 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1245
1246 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1247 // i.e. a < 12 can be rewritten as 12 > a.
1248 if (RHS.getOpcode() == ISD::Constant) {
1249
1250 SDValue Tmp = LHS;
1251 LHS = RHS;
1252 RHS = Tmp;
1253
1254 switch (CondCode) {
1255 default: break;
1256 case PIC16CC::LT:
1257 CondCode = PIC16CC::GT;
1258 break;
1259 case PIC16CC::GT:
1260 CondCode = PIC16CC::LT;
1261 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001262 case PIC16CC::ULT:
1263 CondCode = PIC16CC::UGT;
1264 break;
1265 case PIC16CC::UGT:
1266 CondCode = PIC16CC::ULT;
1267 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001268 case PIC16CC::GE:
1269 CondCode = PIC16CC::LE;
1270 break;
1271 case PIC16CC::LE:
1272 CondCode = PIC16CC::GE;
1273 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001274 case PIC16CC::ULE:
1275 CondCode = PIC16CC::UGE;
1276 break;
1277 case PIC16CC::UGE:
1278 CondCode = PIC16CC::ULE;
1279 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001280 }
1281 }
1282
1283 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001284
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001285 // These are signed comparisons.
1286 SDValue Mask = DAG.getConstant(128, MVT::i8);
1287 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001288 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1289 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001290 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001291
1292 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001293 // We can use a subtract operation to set the condition codes. But
1294 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001295 // Nothing to do if the first operand is already a valid type (direct load
1296 // for subwf and literal for sublw) and it is used by this operation only.
1297 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1298 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001299 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001300
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001301 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001302 LHS = ConvertToMemOperand (LHS, DAG, dl);
1303 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001304}
1305
1306
1307SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1308 SDValue LHS = Op.getOperand(0);
1309 SDValue RHS = Op.getOperand(1);
1310 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1311 SDValue TrueVal = Op.getOperand(2);
1312 SDValue FalseVal = Op.getOperand(3);
1313 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001314 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001315
1316 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1317 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1318 // i.e.
1319 // A setcc: lhs, rhs, cc is expanded by llvm to
1320 // select_cc: result of setcc, 0, 1, 0, setne
1321 // We can think of it as:
1322 // select_cc: lhs, rhs, 1, 0, cc
1323 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1324 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1325
1326 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001327 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001328
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001329 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001330 FalseVal, PIC16CC, Cmp.getValue(1));
1331}
1332
1333MachineBasicBlock *
1334PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001335 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001336 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1337 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001338 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001339
1340 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1341 // control-flow pattern. The incoming instruction knows the destination vreg
1342 // to set, the condition code register to branch on, the true/false values to
1343 // select between, and a branch opcode to use.
1344 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1345 MachineFunction::iterator It = BB;
1346 ++It;
1347
1348 // thisMBB:
1349 // ...
1350 // TrueVal = ...
1351 // [f]bCC copy1MBB
1352 // fallthrough --> copy0MBB
1353 MachineBasicBlock *thisMBB = BB;
1354 MachineFunction *F = BB->getParent();
1355 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1356 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001357 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001358 F->insert(It, copy0MBB);
1359 F->insert(It, sinkMBB);
1360
1361 // Update machine-CFG edges by transferring all successors of the current
1362 // block to the new block which will contain the Phi node for the select.
1363 sinkMBB->transferSuccessors(BB);
1364 // Next, add the true and fallthrough blocks as its successors.
1365 BB->addSuccessor(copy0MBB);
1366 BB->addSuccessor(sinkMBB);
1367
1368 // copy0MBB:
1369 // %FalseValue = ...
1370 // # fallthrough to sinkMBB
1371 BB = copy0MBB;
1372
1373 // Update machine-CFG edges
1374 BB->addSuccessor(sinkMBB);
1375
1376 // sinkMBB:
1377 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1378 // ...
1379 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001380 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001381 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1382 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1383
1384 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1385 return BB;
1386}
1387
1388
1389SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1390 SDValue Chain = Op.getOperand(0);
1391 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1392 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1393 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1394 SDValue Dest = Op.getOperand(4); // BB to jump to
1395 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001396 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001397
1398 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1399 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1400 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1401 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1402
1403 // Get the Compare insn and condition code.
1404 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001405 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001406
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001407 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001408 Cmp.getValue(1));
1409}
1410
1411