blob: d50f57f97dc26f18204578044e2cf0e4492f2821 [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
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000140 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Custom);
141
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000142 // Now deduce the information based on the above mentioned
143 // actions
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000144 computeRegisterProperties();
145}
146
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000147// getOutFlag - Extract the flag result if the Op has it.
148static SDValue getOutFlag(SDValue &Op) {
149 // Flag is the last value of the node.
150 SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
151
152 assert (Flag.getValueType() == MVT::Flag
153 && "Node does not have an out Flag");
154
155 return Flag;
156}
157
158// To extract chain value from the SDValue Nodes
159// This function will help to maintain the chain extracting
160// code at one place. In case of any change in future it will
161// help maintain the code.
162static SDValue getChain(SDValue &Op) {
163 SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
164
165 // If the last value returned in Flag then the chain is
166 // second last value returned.
167 if (Chain.getValueType() == MVT::Flag)
168 Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
169
170 // All nodes may not produce a chain. Therefore following assert
171 // verifies that the node is returning a chain only.
172 assert (Chain.getValueType() == MVT::Other
173 && "Node does not have a chain");
174
175 return Chain;
176}
177
178/// PopulateResults - Helper function to LowerOperation.
179/// If a node wants to return multiple results after lowering,
180/// it stuffs them into an array of SDValue called Results.
181
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000182static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
183 if (N.getOpcode() == ISD::MERGE_VALUES) {
184 int NumResults = N.getNumOperands();
185 for( int i = 0; i < NumResults; i++)
186 Results.push_back(N.getOperand(i));
187 }
188 else
189 Results.push_back(N);
190}
191
192MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
193 return MVT::i8;
194}
195
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000196/// The type legalizer framework of generating legalizer can generate libcalls
197/// only when the operand/result types are illegal.
198/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
199/// For example an arithmetic right shift. These functions are used to lower
200/// such operations that generate libcall for legal types.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000201
202void
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000203PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000204 const char *Name) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000205 PIC16LibcallNames[Call] = Name;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000206}
207
208const char *
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000209PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000210 return PIC16LibcallNames[Call];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000211}
212
213SDValue
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000214PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000215 MVT RetVT, const SDValue *Ops,
216 unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000217 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000218
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000219 TargetLowering::ArgListTy Args;
220 Args.reserve(NumOps);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000221
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000222 TargetLowering::ArgListEntry Entry;
223 for (unsigned i = 0; i != NumOps; ++i) {
224 Entry.Node = Ops[i];
225 Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
226 Entry.isSExt = isSigned;
227 Entry.isZExt = !isSigned;
228 Args.push_back(Entry);
229 }
230 SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000231
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000232 const Type *RetTy = RetVT.getTypeForMVT();
233 std::pair<SDValue,SDValue> CallInfo =
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000234 LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
Sanjiv Guptae9d81f02009-03-20 14:10:20 +0000235 false, CallingConv::C, false, Callee, Args, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000236
237 return CallInfo.first;
238}
239
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000240const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
241 switch (Opcode) {
242 default: return NULL;
243 case PIC16ISD::Lo: return "PIC16ISD::Lo";
244 case PIC16ISD::Hi: return "PIC16ISD::Hi";
245 case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
246 case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
247 case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
248 case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000249 case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000250 case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000251 case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000252 case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000253 case PIC16ISD::BCF: return "PIC16ISD::BCF";
254 case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
255 case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
256 case PIC16ISD::RLF: return "PIC16ISD::RLF";
257 case PIC16ISD::RRF: return "PIC16ISD::RRF";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000258 case PIC16ISD::CALL: return "PIC16ISD::CALL";
259 case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
260 case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
261 case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000262 case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000263 case PIC16ISD::PIC16StopPoint: return "PIC16ISD::PIC16StopPoint";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000264 }
265}
266
Duncan Sands1607f052008-12-01 11:39:25 +0000267void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
268 SmallVectorImpl<SDValue>&Results,
269 SelectionDAG &DAG) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000270
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000271 switch (N->getOpcode()) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000272 case ISD::GlobalAddress:
Duncan Sands1607f052008-12-01 11:39:25 +0000273 Results.push_back(ExpandGlobalAddress(N, DAG));
274 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000275 case ISD::ExternalSymbol:
276 Results.push_back(ExpandExternalSymbol(N, DAG));
277 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000278 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000279 Results.push_back(ExpandStore(N, DAG));
280 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000281 case ISD::LOAD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000282 PopulateResults(ExpandLoad(N, DAG), Results);
Duncan Sands1607f052008-12-01 11:39:25 +0000283 return;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000284 case ISD::ADD:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000285 // Results.push_back(ExpandAdd(N, DAG));
Duncan Sands1607f052008-12-01 11:39:25 +0000286 return;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000287 case ISD::FrameIndex:
288 Results.push_back(ExpandFrameIndex(N, DAG));
289 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000290 default:
291 assert (0 && "not implemented");
Duncan Sands1607f052008-12-01 11:39:25 +0000292 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000293 }
294}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000295
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000296SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
297
298 // Currently handling FrameIndex of size MVT::i16 only
299 // One example of this scenario is when return value is written on
300 // FrameIndex#0
301
302 if (N->getValueType(0) != MVT::i16)
303 return SDValue();
304
305 // Expand the FrameIndex into ExternalSymbol and a Constant node
306 // The constant will represent the frame index number
307 // Get the current function frame
308 MachineFunction &MF = DAG.getMachineFunction();
309 const Function *Func = MF.getFunction();
310 const std::string Name = Func->getName();
Dale Johannesende064702009-02-06 21:50:26 +0000311
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000312 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
Dale Johannesende064702009-02-06 21:50:26 +0000313 // FIXME there isn't really debug info here
314 DebugLoc dl = FR->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000315 int Index = FR->getIndex();
316
317 SDValue FI[2];
318 FI[0] = DAG.getTargetFrameIndex(Index, MVT::i8);
319 FI[1] = DAG.getTargetFrameIndex(Index + 1, MVT::i8);
Dale Johannesende064702009-02-06 21:50:26 +0000320 return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), FI[0], FI[1]);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000321}
322
323
324SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000325 StoreSDNode *St = cast<StoreSDNode>(N);
326 SDValue Chain = St->getChain();
327 SDValue Src = St->getValue();
328 SDValue Ptr = St->getBasePtr();
329 MVT ValueType = Src.getValueType();
330 unsigned StoreOffset = 0;
Dale Johannesende064702009-02-06 21:50:26 +0000331 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000332
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000333 SDValue PtrLo, PtrHi;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000334 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000335
336 if (ValueType == MVT::i8) {
Dale Johannesende064702009-02-06 21:50:26 +0000337 return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000338 PtrLo, PtrHi,
339 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000340 }
341 else if (ValueType == MVT::i16) {
342 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
343 SDValue SrcLo, SrcHi;
344 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
345 SDValue ChainLo = Chain, ChainHi = Chain;
346 if (Chain.getOpcode() == ISD::TokenFactor) {
347 ChainLo = Chain.getOperand(0);
348 ChainHi = Chain.getOperand(1);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000349 }
Dale Johannesende064702009-02-06 21:50:26 +0000350 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000351 ChainLo,
352 SrcLo, PtrLo, PtrHi,
353 DAG.getConstant (0 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000354
Dale Johannesende064702009-02-06 21:50:26 +0000355 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000356 SrcHi, PtrLo, PtrHi,
357 DAG.getConstant (1 + StoreOffset, MVT::i8));
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000358
Dale Johannesende064702009-02-06 21:50:26 +0000359 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
Duncan Sands1607f052008-12-01 11:39:25 +0000360 getChain(Store2));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000361 }
362 else if (ValueType == MVT::i32) {
363 // Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
364 SDValue SrcLo, SrcHi;
365 GetExpandedParts(Src, DAG, SrcLo, SrcHi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000366
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000367 // Get the expanded parts of each of SrcLo and SrcHi.
368 SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
369 GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
370 GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000371
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000372 SDValue ChainLo = Chain, ChainHi = Chain;
373 if (Chain.getOpcode() == ISD::TokenFactor) {
374 ChainLo = Chain.getOperand(0);
375 ChainHi = Chain.getOperand(1);
376 }
377 SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
378 ChainHi2 = ChainHi;
379 if (ChainLo.getOpcode() == ISD::TokenFactor) {
380 ChainLo1 = ChainLo.getOperand(0);
381 ChainLo2 = ChainLo.getOperand(1);
382 }
383 if (ChainHi.getOpcode() == ISD::TokenFactor) {
384 ChainHi1 = ChainHi.getOperand(0);
385 ChainHi2 = ChainHi.getOperand(1);
386 }
Dale Johannesende064702009-02-06 21:50:26 +0000387 SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000388 ChainLo1,
389 SrcLo1, PtrLo, PtrHi,
390 DAG.getConstant (0 + StoreOffset, MVT::i8));
391
Dale Johannesende064702009-02-06 21:50:26 +0000392 SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000393 SrcLo2, PtrLo, PtrHi,
394 DAG.getConstant (1 + StoreOffset, MVT::i8));
395
Dale Johannesende064702009-02-06 21:50:26 +0000396 SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000397 SrcHi1, PtrLo, PtrHi,
398 DAG.getConstant (2 + StoreOffset, MVT::i8));
399
Dale Johannesende064702009-02-06 21:50:26 +0000400 SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000401 SrcHi2, PtrLo, PtrHi,
402 DAG.getConstant (3 + StoreOffset, MVT::i8));
403
Dale Johannesende064702009-02-06 21:50:26 +0000404 SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
405 getChain(Store1), getChain(Store2));
406 SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
407 getChain(Store3), getChain(Store4));
408 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000409
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000410 }
411 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000412 assert (0 && "value type not supported");
Duncan Sands1607f052008-12-01 11:39:25 +0000413 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000414 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000415}
416
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000417SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
418{
419 ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000420 // FIXME there isn't really debug info here
421 DebugLoc dl = ES->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000422
423 SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
424
Dale Johannesende064702009-02-06 21:50:26 +0000425 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES);
426 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000427
Dale Johannesende064702009-02-06 21:50:26 +0000428 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000429}
430
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000431// ExpandGlobalAddress -
Duncan Sands1607f052008-12-01 11:39:25 +0000432SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000433 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
Dale Johannesende064702009-02-06 21:50:26 +0000434 // FIXME there isn't really debug info here
435 DebugLoc dl = G->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000436
437 SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
438 G->getOffset());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000439
Dale Johannesende064702009-02-06 21:50:26 +0000440 SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA);
441 SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000442
Dale Johannesende064702009-02-06 21:50:26 +0000443 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000444}
445
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000446bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
447 assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000448
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000449 if (Op.getOpcode() == ISD::BUILD_PAIR) {
450 if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
451 return true;
452 }
453 return false;
454}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000455
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000456// Return true if DirectAddress is in ROM_SPACE
457bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000458
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000459 // RomAddress is a GlobalAddress in ROM_SPACE_
460 // If the Op is not a GlobalAddress return NULL without checking
461 // anything further.
462 if (!isDirectAddress(Op))
463 return false;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000464
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000465 // Its a GlobalAddress.
466 // It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
467 SDValue TGA = Op.getOperand(0).getOperand(0);
468 GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
469 const Type *ValueType = GSDN->getGlobal()->getType();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000470
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000471 if (!isa<PointerType>(ValueType)) {
472 assert(0 && "TGA must be of a PointerType");
473 }
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000474
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000475 int AddrSpace = dyn_cast<PointerType>(ValueType)->getAddressSpace();
476 if (AddrSpace == PIC16ISD::ROM_SPACE)
477 return true;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000478
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000479 // Any other address space return it false
480 return false;
481}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000482
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000483
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000484// GetExpandedParts - This function is on the similiar lines as
485// the GetExpandedInteger in type legalizer is. This returns expanded
486// parts of Op in Lo and Hi.
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000487
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000488void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
489 SDValue &Lo, SDValue &Hi) {
490 SDNode *N = Op.getNode();
Dale Johannesened2eee62009-02-06 01:31:28 +0000491 DebugLoc dl = N->getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000492 MVT NewVT = getTypeToTransformTo(N->getValueType(0));
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000493
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000494 // Extract the lo component.
495 Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
496 DAG.getConstant(0, MVT::i8));
497
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000498 // extract the hi component
Sanjiv Gupta6b830e62009-03-20 13:42:20 +0000499 Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
500 DAG.getConstant(1, MVT::i8));
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000501}
502
503// Legalize FrameIndex into ExternalSymbol and offset.
504void
505PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
506 SDValue &ES, int &Offset) {
507
508 MachineFunction &MF = DAG.getMachineFunction();
509 const Function *Func = MF.getFunction();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000510 MachineFrameInfo *MFI = MF.getFrameInfo();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000511 const std::string Name = Func->getName();
512
513 char *tmpName = new char [strlen(Name.c_str()) + 8];
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000514 sprintf(tmpName, "%s.frame", Name.c_str());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000515 ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
516 FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000517
518 // FrameIndices are not stack offsets. But they represent the request
519 // for space on stack. That space requested may be more than one byte.
520 // Therefore, to calculate the stack offset that a FrameIndex aligns
521 // with, we need to traverse all the FrameIndices available earlier in
522 // the list and add their requested size.
523 unsigned FIndex = FR->getIndex();
524 Offset = 0;
525 for (unsigned i=0; i<FIndex ; ++i) {
526 Offset += MFI->getObjectSize(i);
527 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000528
529 return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000530}
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000531
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000532// This function legalizes the PIC16 Addresses. If the Pointer is
533// -- Direct address variable residing
534// --> then a Banksel for that variable will be created.
535// -- Rom variable
536// --> then it will be treated as an indirect address.
537// -- Indirect address
538// --> then the address will be loaded into FSR
539// -- ADD with constant operand
540// --> then constant operand of ADD will be returned as Offset
541// and non-constant operand of ADD will be treated as pointer.
542// Returns the high and lo part of the address, and the offset(in case of ADD).
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000543
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000544void PIC16TargetLowering:: LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
545 SDValue &Lo, SDValue &Hi,
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000546 unsigned &Offset, DebugLoc dl) {
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000547
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000548 // Offset, by default, should be 0
549 Offset = 0;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000550
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000551 // If the pointer is ADD with constant,
552 // return the constant value as the offset
553 if (Ptr.getOpcode() == ISD::ADD) {
554 SDValue OperLeft = Ptr.getOperand(0);
555 SDValue OperRight = Ptr.getOperand(1);
556 if (OperLeft.getOpcode() == ISD::Constant) {
557 Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
558 Ptr = OperRight;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000559 } else if (OperRight.getOpcode() == ISD::Constant) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000560 Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000561 Ptr = OperLeft;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000562 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000563 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000564
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000565 // If the pointer is Type i8 and an external symbol
566 // then treat it as direct address.
567 // One example for such case is storing and loading
568 // from function frame during a call
569 if (Ptr.getValueType() == MVT::i8) {
570 switch (Ptr.getOpcode()) {
571 case ISD::TargetExternalSymbol:
572 Lo = Ptr;
573 Hi = DAG.getConstant(1, MVT::i8);
574 return;
575 }
576 }
577
578 if (Ptr.getOpcode() == ISD::BUILD_PAIR &&
579 Ptr.getOperand(0).getOpcode() == ISD::TargetFrameIndex) {
580
581 int FrameOffset;
582 LegalizeFrameIndex(Ptr.getOperand(0), DAG, Lo, FrameOffset);
583 Hi = DAG.getConstant(1, MVT::i8);
584 Offset += FrameOffset;
585 return;
586 }
587
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000588 if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
589 // Direct addressing case for RAM variables. The Hi part is constant
590 // and the Lo part is the TGA itself.
591 Lo = Ptr.getOperand(0).getOperand(0);
592
593 // For direct addresses Hi is a constant. Value 1 for the constant
594 // signifies that banksel needs to generated for it. Value 0 for
595 // the constant signifies that banksel does not need to be generated
596 // for it. Mark it as 1 now and optimize later.
597 Hi = DAG.getConstant(1, MVT::i8);
598 return;
599 }
600
601 // Indirect addresses. Get the hi and lo parts of ptr.
602 GetExpandedParts(Ptr, DAG, Lo, Hi);
603
604 // Put the hi and lo parts into FSR.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000605 Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
606 Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000607
608 return;
609}
610
Duncan Sands1607f052008-12-01 11:39:25 +0000611SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000612 LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
613 SDValue Chain = LD->getChain();
614 SDValue Ptr = LD->getBasePtr();
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000615 DebugLoc dl = LD->getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000616
617 SDValue Load, Offset;
618 SDVTList Tys;
619 MVT VT, NewVT;
620 SDValue PtrLo, PtrHi;
621 unsigned LoadOffset;
622
623 // Legalize direct/indirect addresses. This will give the lo and hi parts
624 // of the address and the offset.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000625 LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000626
627 // Load from the pointer (direct address or FSR)
628 VT = N->getValueType(0);
629 unsigned NumLoads = VT.getSizeInBits() / 8;
630 std::vector<SDValue> PICLoads;
631 unsigned iter;
632 MVT MemVT = LD->getMemoryVT();
633 if(ISD::isNON_EXTLoad(N)) {
634 for (iter=0; iter<NumLoads ; ++iter) {
635 // Add the pointer offset if any
636 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
637 Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000638 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000639 Offset);
640 PICLoads.push_back(Load);
641 }
642 } else {
643 // If it is extended load then use PIC16Load for Memory Bytes
644 // and for all extended bytes perform action based on type of
645 // extention - i.e. SignExtendedLoad or ZeroExtendedLoad
646
647
648 // For extended loads this is the memory value type
649 // i.e. without any extension
650 MVT MemVT = LD->getMemoryVT();
651 unsigned MemBytes = MemVT.getSizeInBits() / 8;
652 unsigned ExtdBytes = VT.getSizeInBits() / 8;
653 Offset = DAG.getConstant(LoadOffset, MVT::i8);
654
655 Tys = DAG.getVTList(MVT::i8, MVT::Other);
656 // For MemBytes generate PIC16Load with proper offset
657 for (iter=0; iter<MemBytes; ++iter) {
658 // Add the pointer offset if any
659 Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000660 Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000661 Offset);
662 PICLoads.push_back(Load);
663 }
664
665 // For SignExtendedLoad
666 if (ISD::isSEXTLoad(N)) {
667 // For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
668 // highest MemByte
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000669 SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000670 DAG.getConstant(7, MVT::i8));
671 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
672 PICLoads.push_back(SRA);
673 }
674 } else if (ISD::isZEXTLoad(N)) {
675 // ZeroExtendedLoad -- For all ExtdBytes use constant 0
676 SDValue ConstZero = DAG.getConstant(0, MVT::i8);
677 for (iter=MemBytes; iter<ExtdBytes; ++iter) {
678 PICLoads.push_back(ConstZero);
679 }
680 }
681 }
682 SDValue BP;
683
684 if (VT == MVT::i8) {
685 // Operand of Load is illegal -- Load itself is legal
Duncan Sands1607f052008-12-01 11:39:25 +0000686 return PICLoads[0];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000687 }
688 else if (VT == MVT::i16) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000689 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000690 if (MemVT == MVT::i8)
691 Chain = getChain(PICLoads[0]);
692 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000693 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
694 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000695 } else if (VT == MVT::i32) {
696 SDValue BPs[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000697 BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
698 PICLoads[0], PICLoads[1]);
699 BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
700 PICLoads[2], PICLoads[3]);
701 BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000702 if (MemVT == MVT::i8)
703 Chain = getChain(PICLoads[0]);
704 else if (MemVT == MVT::i16)
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000705 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
706 getChain(PICLoads[0]), getChain(PICLoads[1]));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000707 else {
708 SDValue Chains[2];
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000709 Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000710 getChain(PICLoads[0]), getChain(PICLoads[1]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000711 Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000712 getChain(PICLoads[2]), getChain(PICLoads[3]));
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000713 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
714 Chains[0], Chains[1]);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000715 }
716 }
717 Tys = DAG.getVTList(VT, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000718 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000719}
720
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000721SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
722 // We should have handled larger operands in type legalizer itself.
723 assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
724
725 SDNode *N = Op.getNode();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000726 SDValue Value = N->getOperand(0);
727 SDValue Amt = N->getOperand(1);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000728 PIC16ISD::PIC16Libcall CallCode;
729 switch (N->getOpcode()) {
730 case ISD::SRA:
731 CallCode = PIC16ISD::SRA_I8;
732 break;
733 case ISD::SHL:
734 CallCode = PIC16ISD::SLL_I8;
735 break;
736 case ISD::SRL:
737 CallCode = PIC16ISD::SRL_I8;
738 break;
739 default:
740 assert ( 0 && "This shift is not implemented yet.");
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000741 return SDValue();
742 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000743 SmallVector<SDValue, 2> Ops(2);
744 Ops[0] = Value;
745 Ops[1] = Amt;
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000746 SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
747 true, DAG, N->getDebugLoc());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000748 return Call;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000749}
750
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000751void
752PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
753 SmallVectorImpl<SDValue>&Results,
754 SelectionDAG &DAG) {
755 SDValue Op = SDValue(N, 0);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000756 SDValue Res;
757 unsigned i;
758 switch (Op.getOpcode()) {
759 case ISD::FORMAL_ARGUMENTS:
760 Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
761 case ISD::LOAD:
762 Res = ExpandLoad(Op.getNode(), DAG); break;
763 case ISD::CALL:
764 Res = LowerCALL(Op, DAG); break;
765 default: {
766 // All other operations are handled in LowerOperation.
767 Res = LowerOperation(Op, DAG);
768 if (Res.getNode())
769 Results.push_back(Res);
770
771 return;
772 }
773 }
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000774
775 N = Res.getNode();
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000776 unsigned NumValues = N->getNumValues();
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000777 for (i = 0; i < NumValues ; i++) {
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000778 Results.push_back(SDValue(N, i));
779 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000780}
781
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000782SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
783 switch (Op.getOpcode()) {
784 case ISD::FORMAL_ARGUMENTS:
785 return LowerFORMAL_ARGUMENTS(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000786 case ISD::ADD:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000787 case ISD::ADDC:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000788 case ISD::ADDE:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000789 return LowerADD(Op, DAG);
790 case ISD::SUB:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000791 case ISD::SUBC:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000792 case ISD::SUBE:
793 return LowerSUB(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000794 case ISD::LOAD:
Duncan Sands1607f052008-12-01 11:39:25 +0000795 return ExpandLoad(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000796 case ISD::STORE:
Duncan Sands1607f052008-12-01 11:39:25 +0000797 return ExpandStore(Op.getNode(), DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000798 case ISD::SHL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000799 case ISD::SRA:
800 case ISD::SRL:
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000801 return LowerShift(Op, DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000802 case ISD::OR:
803 case ISD::AND:
804 case ISD::XOR:
805 return LowerBinOp(Op, DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000806 case ISD::CALL:
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000807 return LowerCALL(Op, DAG);
808 case ISD::RET:
809 return LowerRET(Op, DAG);
810 case ISD::BR_CC:
811 return LowerBR_CC(Op, DAG);
812 case ISD::SELECT_CC:
813 return LowerSELECT_CC(Op, DAG);
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000814 case ISD::DBG_STOPPOINT:
815 return LowerStopPoint(Op, DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000816 }
Dan Gohman475871a2008-07-27 21:46:04 +0000817 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000818}
819
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000820SDValue PIC16TargetLowering::LowerStopPoint(SDValue Op, SelectionDAG &DAG) {
821 DbgStopPointSDNode *SP = dyn_cast<DbgStopPointSDNode>(Op);
822 unsigned line = SP->getLine();
823 SDValue LineNode = DAG.getConstant(line, MVT::i8);
824 DebugLoc dl = Op.getDebugLoc();
825 return DAG.getNode(PIC16ISD::PIC16StopPoint, dl, MVT::Other,
826 Op.getOperand(0), LineNode);
827}
828
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000829SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000830 SelectionDAG &DAG,
831 DebugLoc dl) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000832 assert (Op.getValueType() == MVT::i8
833 && "illegal value type to store on stack.");
834
835 MachineFunction &MF = DAG.getMachineFunction();
836 const Function *Func = MF.getFunction();
837 const std::string FuncName = Func->getName();
838
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000839 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000840
841 // Put the value on stack.
842 // Get a stack slot index and convert to es.
843 int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
844 sprintf(tmpName, "%s.tmp", FuncName.c_str());
845 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
846
847 // Store the value to ES.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000848 SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000849 DAG.getEntryNode(),
850 Op, ES,
851 DAG.getConstant (1, MVT::i8), // Banksel.
852 DAG.getConstant (FI, MVT::i8));
853
854 // Load the value from ES.
855 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000856 SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000857 ES, DAG.getConstant (1, MVT::i8),
858 DAG.getConstant (FI, MVT::i8));
859
860 return Load.getValue(0);
861}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000862
863SDValue
864PIC16TargetLowering::LowerCallArguments(SDValue Op, SDValue Chain,
865 SDValue FrameAddress,
866 SDValue InFlag,
867 SelectionDAG &DAG) {
868 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
869 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesene8d72302009-02-06 23:05:02 +0000870 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000871 std::string Name;
872 SDValue Arg, StoreAt;
873 MVT ArgVT;
874 unsigned Size=0;
875 unsigned ArgCount=0;
876
877
878 // FIXME: This portion of code currently assumes only
879 // primitive types being passed as arguments.
880
881 // Legalize the address before use
882 SDValue PtrLo, PtrHi;
883 unsigned AddressOffset;
884 int StoreOffset = 0;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000885 LegalizeAddress(FrameAddress, DAG, PtrLo, PtrHi, AddressOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000886 SDValue StoreRet;
887
888 std::vector<SDValue> Ops;
889 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
890 for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
891 // Get the argument
892 Arg = TheCall->getArg(i);
893
894 StoreOffset = (Offset + AddressOffset);
895
896 // Store the argument on frame
897
898 Ops.clear();
899 Ops.push_back(Chain);
900 Ops.push_back(Arg.getValue(0));
901 Ops.push_back(PtrLo);
902 Ops.push_back(PtrHi);
903 Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
904 Ops.push_back(InFlag);
905
Dale Johannesene8d72302009-02-06 23:05:02 +0000906 StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000907
908 Chain = getChain(StoreRet);
909 InFlag = getOutFlag(StoreRet);
910
911 // Update the frame offset to be used for next argument
912 ArgVT = Arg.getValueType();
913 Size = ArgVT.getSizeInBits();
914 Size = Size/8; // Calculate size in bytes
915 Offset += Size; // Increase the frame offset
916 }
917 return Chain;
918}
919
920SDValue
921PIC16TargetLowering::LowerCallReturn(SDValue Op, SDValue Chain,
922 SDValue FrameAddress,
923 SDValue InFlag,
924 SelectionDAG &DAG) {
925 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000926 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000927 // Currently handling primitive types only. They will come in
928 // i8 parts
929 unsigned RetVals = TheCall->getNumRetVals();
930
931 std::vector<SDValue> ResultVals;
932
933 // Return immediately if the return type is void
934 if (RetVals == 0)
935 return Chain;
936
937 // Call has something to return
938
939 // Legalize the address before use
940 SDValue LdLo, LdHi;
941 unsigned LdOffset;
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000942 LegalizeAddress(FrameAddress, DAG, LdLo, LdHi, LdOffset, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000943
944 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
945 SDValue LoadRet;
946
947 for(unsigned i=0, Offset=0;i<RetVals;i++) {
948
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000949 LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000950 DAG.getConstant(LdOffset + Offset, MVT::i8),
951 InFlag);
952
953 InFlag = getOutFlag(LoadRet);
954
955 Chain = getChain(LoadRet);
956 Offset++;
957 ResultVals.push_back(LoadRet);
958 }
959
960 // To return use MERGE_VALUES
961 ResultVals.push_back(Chain);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000962 SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000963 return Res;
964}
965
966SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000967 SDValue Chain = Op.getOperand(0);
968 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000969
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000970 if (Op.getNumOperands() == 1) // return void
971 return Op;
972
973 // return should have odd number of operands
974 if ((Op.getNumOperands() % 2) == 0 ) {
975 assert(0 && "Do not know how to return this many arguments!");
976 abort();
977 }
978
979 // Number of values to return
980 unsigned NumRet = (Op.getNumOperands() / 2);
981
982 // Function returns value always on stack with the offset starting
983 // from 0
984 MachineFunction &MF = DAG.getMachineFunction();
985 const Function *F = MF.getFunction();
986 std::string FuncName = F->getName();
987
988 char *tmpName = new char [strlen(FuncName.c_str()) + 8];
989 sprintf(tmpName, "%s.frame", FuncName.c_str());
990 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
991 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
992 SDValue BS = DAG.getConstant(1, MVT::i8);
993 SDValue RetVal;
994 for(unsigned i=0;i<NumRet; ++i) {
995 RetVal = Op.getNode()->getOperand(2*i + 1);
996 Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
997 ES, BS,
998 DAG.getConstant (i, MVT::i8));
999
1000 }
1001 return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001002}
1003
1004SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
1005 CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
1006 SDValue Chain = TheCall->getChain();
1007 SDValue Callee = TheCall->getCallee();
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001008 DebugLoc dl = TheCall->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001009 unsigned i =0;
1010 if (Callee.getValueType() == MVT::i16 &&
1011 Callee.getOpcode() == ISD::BUILD_PAIR) {
1012 // It has come from TypeLegalizer for lowering
1013
1014 Callee = Callee.getOperand(0).getOperand(0);
1015
1016 std::vector<SDValue> Ops;
1017 Ops.push_back(Chain);
1018 Ops.push_back(Callee);
1019
1020 // Add the call arguments and their flags
1021 unsigned NumArgs = TheCall->getNumArgs();
1022 for(i=0;i<NumArgs;i++) {
1023 Ops.push_back(TheCall->getArg(i));
1024 Ops.push_back(TheCall->getArgFlagsVal(i));
1025 }
1026
1027 std::vector<MVT> NodeTys;
1028 unsigned NumRets = TheCall->getNumRetVals();
1029 for(i=0;i<NumRets;i++)
1030 NodeTys.push_back(TheCall->getRetValType(i));
1031
1032 // Return a Chain as well
1033 NodeTys.push_back(MVT::Other);
1034
1035 SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
1036 SDValue NewCall =
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001037 DAG.getCall(TheCall->getCallingConv(), dl,
Dale Johannesen8ad9b432009-02-04 01:17:06 +00001038 TheCall->isVarArg(), TheCall->isTailCall(),
1039 TheCall->isInreg(), VTs, &Ops[0], Ops.size());
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001040
1041 return NewCall;
1042 }
1043
1044 SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
1045
1046 // Start the call sequence.
1047 // Carring the Constant 0 along the CALLSEQSTART
1048 // because there is nothing else to carry.
1049 SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
1050 Chain = getChain(SeqStart);
1051
1052 // For any direct call - callee will be GlobalAddressNode or
1053 // ExternalSymbol
1054
1055 // Considering the GlobalAddressNode case here.
1056 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1057 GlobalValue *GV = G->getGlobal();
1058 Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
1059 }
1060
1061 // Considering the ExternalSymbol case here
1062 if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1063 Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
1064 }
1065
1066 SDValue OperFlag = getOutFlag(Chain); // To manage the data dependency
1067
1068 std::string Name;
1069
1070 // Considering GlobalAddress here
1071 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1072 Name = G->getGlobal()->getName();
1073
1074 // Considering ExternalSymbol here
1075 if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee))
1076 Name = ES->getSymbol();
1077
1078 char *argFrame = new char [strlen(Name.c_str()) + 8];
1079 sprintf(argFrame, "%s.args", Name.c_str());
1080 SDValue ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
1081
1082 char *retName = new char [strlen(Name.c_str()) + 8];
1083 sprintf(retName, "%s.retval", Name.c_str());
1084 SDValue RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
1085
1086 // Pass the argument to function before making the call.
1087 SDValue CallArgs = LowerCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
1088 Chain = getChain(CallArgs);
1089 OperFlag = getOutFlag(CallArgs);
1090
1091 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001092 SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001093 OperFlag);
1094 Chain = getChain(PICCall);
1095 OperFlag = getOutFlag(PICCall);
1096
1097
1098 // Carrying the Constant 0 along the CALLSEQSTART
1099 // because there is nothing else to carry.
1100 SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
1101 OperFlag);
1102 Chain = getChain(SeqEnd);
1103 OperFlag = getOutFlag(SeqEnd);
1104
1105 // Lower the return value reading after the call.
1106 return LowerCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
1107}
1108
1109bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
1110 if (Op.getOpcode() == PIC16ISD::PIC16Load)
1111 if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
1112 || Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
1113 return true;
1114 return false;
1115}
1116
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001117// NeedToConvertToMemOp - Returns true if one of the operands of the
1118// operation 'Op' needs to be put into memory. Also returns the
1119// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
1120// no instruction that can operation on two registers. Most insns take
1121// one register and one memory operand (addwf) / Constant (addlw).
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001122bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001123 // If one of the operand is a constant, return false.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001124 if (Op.getOperand(0).getOpcode() == ISD::Constant ||
1125 Op.getOperand(1).getOpcode() == ISD::Constant)
1126 return false;
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001127
1128 // Return false if one of the operands is already a direct
1129 // load and that operand has only one use.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001130 if (isDirectLoad(Op.getOperand(0))) {
1131 if (Op.getOperand(0).hasOneUse())
1132 return false;
1133 else
1134 MemOp = 0;
1135 }
1136 if (isDirectLoad(Op.getOperand(1))) {
1137 if (Op.getOperand(1).hasOneUse())
1138 return false;
1139 else
1140 MemOp = 1;
1141 }
1142 return true;
1143}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001144
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001145// LowerBinOp - Lower a commutative binary operation that does not
1146// affect status flag carry.
1147SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001148 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001149
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001150 // We should have handled larger operands in type legalizer itself.
1151 assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001152
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001153 unsigned MemOp = 1;
1154 if (NeedToConvertToMemOp(Op, MemOp)) {
1155 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001156 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001157
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001158 return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001159 NewVal);
1160 }
1161 else {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001162 return Op;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001163 }
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +00001164}
1165
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001166// LowerADD - Lower all types of ADD operations including the ones
1167// that affects carry.
1168SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001169 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001170 assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001171 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001172 unsigned MemOp = 1;
1173 if (NeedToConvertToMemOp(Op, MemOp)) {
1174 // Put one value on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001175 SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001176
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001177 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001178
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001179 if (Op.getOpcode() == ISD::ADDE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001180 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1181 NewVal, Op.getOperand(2));
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001182 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001183 return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
1184 NewVal);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001185 }
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001186 else if (Op.getOpcode() == ISD::ADD) {
1187 return Op;
1188 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001189 else {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001190 return SDValue();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001191 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001192}
1193
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001194SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001195 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001196 // We should have handled larger operands in type legalizer itself.
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001197 assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001198
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001199 // Nothing to do if the first operand is already a direct load and it has
1200 // only one use.
1201 if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001202 return SDValue();
1203
1204 // Put first operand on stack.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001205 SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001206
1207 SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001208 if (Op.getOpcode() == ISD::SUBE)
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001209 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001210 Op.getOperand(2));
1211 else
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001212 return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001213}
1214
1215// LowerFORMAL_ARGUMENTS - In Lowering FORMAL ARGUMENTS - MERGE_VALUES nodes
1216// is returned. MERGE_VALUES nodes number of operands and number of values are
1217// equal. Therefore to construct MERGE_VALUE node, UNDEF nodes equal to the
1218// number of arguments of function have been created.
1219
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001220SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001221 SelectionDAG &DAG) {
1222 SmallVector<SDValue, 8> ArgValues;
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001223 unsigned NumArgs = Op.getNode()->getNumValues()-1;
Dale Johannesene8d72302009-02-06 23:05:02 +00001224 DebugLoc dl = Op.getDebugLoc();
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001225 SDValue Chain = Op.getOperand(0); // Formal arguments' chain
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001226
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001227 MachineFunction &MF = DAG.getMachineFunction();
1228 //const TargetData *TD = getTargetData();
1229 const Function *F = MF.getFunction();
1230 std::string FuncName = F->getName();
1231
1232 char *tmpName = new char [strlen(FuncName.c_str()) + 6];
1233 sprintf(tmpName, "%s.args", FuncName.c_str());
1234 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
1235 SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
1236 SDValue BS = DAG.getConstant(1, MVT::i8);
1237 for (unsigned i=0; i<NumArgs ; ++i) {
1238 SDValue Offset = DAG.getConstant(i, MVT::i8);
1239 SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
1240 Offset);
1241 Chain = getChain(PICLoad);
1242 ArgValues.push_back(PICLoad);
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001243 }
1244
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001245 ArgValues.push_back(Op.getOperand(0));
Dale Johannesene8d72302009-02-06 23:05:02 +00001246 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001247 &ArgValues[0],
1248 ArgValues.size()).getValue(Op.getResNo());
1249}
1250
Sanjiv Gupta6b830e62009-03-20 13:42:20 +00001251// Perform DAGCombine of PIC16Load.
1252// FIXME - Need a more elaborate comment here.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001253SDValue PIC16TargetLowering::
1254PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1255 SelectionDAG &DAG = DCI.DAG;
1256 SDValue Chain = N->getOperand(0);
1257 if (N->hasNUsesOfValue(0, 0)) {
1258 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
1259 }
1260 return SDValue();
1261}
1262
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001263// For all the functions with arguments some STORE nodes are generated
1264// that store the argument on the frameindex. However in PIC16 the arguments
1265// are passed on stack only. Therefore these STORE nodes are redundant.
1266// To remove these STORE nodes will be removed in PerformStoreCombine
1267//
1268// Currently this function is doint nothing and will be updated for removing
1269// unwanted store operations
1270SDValue PIC16TargetLowering::
1271PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
1272 SelectionDAG &DAG = DCI.DAG;
1273 SDValue Chain;
1274 return SDValue(N, 0);
1275 /*
1276 // Storing an undef value is of no use, so remove it
1277 if (isStoringUndef(N, Chain, DAG)) {
1278 return Chain; // remove the store and return the chain
1279 }
1280 //else everything is ok.
1281 return SDValue(N, 0);
1282 */
1283}
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001284
1285SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
1286 DAGCombinerInfo &DCI) const {
1287 switch (N->getOpcode()) {
Sanjiv Guptab84d5a42009-04-02 17:42:00 +00001288 case ISD::STORE:
1289 return PerformStoreCombine(N, DCI);
1290 case PIC16ISD::PIC16Load:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +00001291 return PerformPIC16LoadCombine(N, DCI);
1292 }
1293 return SDValue();
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001294}
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001295
1296static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
1297 switch (CC) {
1298 default: assert(0 && "Unknown condition code!");
1299 case ISD::SETNE: return PIC16CC::NE;
1300 case ISD::SETEQ: return PIC16CC::EQ;
1301 case ISD::SETGT: return PIC16CC::GT;
1302 case ISD::SETGE: return PIC16CC::GE;
1303 case ISD::SETLT: return PIC16CC::LT;
1304 case ISD::SETLE: return PIC16CC::LE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001305 case ISD::SETULT: return PIC16CC::ULT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001306 case ISD::SETULE: return PIC16CC::LE;
1307 case ISD::SETUGE: return PIC16CC::GE;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001308 case ISD::SETUGT: return PIC16CC::UGT;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001309 }
1310}
1311
1312// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1313// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1314static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1315 ISD::CondCode CC, unsigned &SPCC) {
1316 if (isa<ConstantSDNode>(RHS) &&
1317 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1318 CC == ISD::SETNE &&
1319 (LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
1320 LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
1321 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1322 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1323 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
1324 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
1325 SDValue CMPCC = LHS.getOperand(3);
1326 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1327 LHS = CMPCC.getOperand(0);
1328 RHS = CMPCC.getOperand(1);
1329 }
1330}
1331
1332// Returns appropriate CMP insn and corresponding condition code in PIC16CC
1333SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
1334 unsigned CC, SDValue &PIC16CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001335 SelectionDAG &DAG, DebugLoc dl) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001336 PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
1337
1338 // PIC16 sub is literal - W. So Swap the operands and condition if needed.
1339 // i.e. a < 12 can be rewritten as 12 > a.
1340 if (RHS.getOpcode() == ISD::Constant) {
1341
1342 SDValue Tmp = LHS;
1343 LHS = RHS;
1344 RHS = Tmp;
1345
1346 switch (CondCode) {
1347 default: break;
1348 case PIC16CC::LT:
1349 CondCode = PIC16CC::GT;
1350 break;
1351 case PIC16CC::GT:
1352 CondCode = PIC16CC::LT;
1353 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001354 case PIC16CC::ULT:
1355 CondCode = PIC16CC::UGT;
1356 break;
1357 case PIC16CC::UGT:
1358 CondCode = PIC16CC::ULT;
1359 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001360 case PIC16CC::GE:
1361 CondCode = PIC16CC::LE;
1362 break;
1363 case PIC16CC::LE:
1364 CondCode = PIC16CC::GE;
1365 break;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001366 case PIC16CC::ULE:
1367 CondCode = PIC16CC::UGE;
1368 break;
1369 case PIC16CC::UGE:
1370 CondCode = PIC16CC::ULE;
1371 break;
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001372 }
1373 }
1374
1375 PIC16CC = DAG.getConstant(CondCode, MVT::i8);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001376
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001377 // These are signed comparisons.
1378 SDValue Mask = DAG.getConstant(128, MVT::i8);
1379 if (isSignedComparison(CondCode)) {
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001380 LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
1381 RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +00001382 }
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001383
1384 SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001385 // We can use a subtract operation to set the condition codes. But
1386 // we need to put one operand in memory if required.
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001387 // Nothing to do if the first operand is already a valid type (direct load
1388 // for subwf and literal for sublw) and it is used by this operation only.
1389 if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
1390 && LHS.hasOneUse())
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001391 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001392
Sanjiv Guptae0ffc922009-01-30 07:55:25 +00001393 // else convert the first operand to mem.
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001394 LHS = ConvertToMemOperand (LHS, DAG, dl);
1395 return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001396}
1397
1398
1399SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1400 SDValue LHS = Op.getOperand(0);
1401 SDValue RHS = Op.getOperand(1);
1402 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1403 SDValue TrueVal = Op.getOperand(2);
1404 SDValue FalseVal = Op.getOperand(3);
1405 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001406 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001407
1408 // If this is a select_cc of a "setcc", and if the setcc got lowered into
1409 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1410 // i.e.
1411 // A setcc: lhs, rhs, cc is expanded by llvm to
1412 // select_cc: result of setcc, 0, 1, 0, setne
1413 // We can think of it as:
1414 // select_cc: lhs, rhs, 1, 0, cc
1415 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1416 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1417
1418 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001419 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001420
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001421 return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001422 FalseVal, PIC16CC, Cmp.getValue(1));
1423}
1424
1425MachineBasicBlock *
1426PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00001427 MachineBasicBlock *BB) const {
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001428 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1429 unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001430 DebugLoc dl = MI->getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001431
1432 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1433 // control-flow pattern. The incoming instruction knows the destination vreg
1434 // to set, the condition code register to branch on, the true/false values to
1435 // select between, and a branch opcode to use.
1436 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1437 MachineFunction::iterator It = BB;
1438 ++It;
1439
1440 // thisMBB:
1441 // ...
1442 // TrueVal = ...
1443 // [f]bCC copy1MBB
1444 // fallthrough --> copy0MBB
1445 MachineBasicBlock *thisMBB = BB;
1446 MachineFunction *F = BB->getParent();
1447 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1448 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001449 BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001450 F->insert(It, copy0MBB);
1451 F->insert(It, sinkMBB);
1452
1453 // Update machine-CFG edges by transferring all successors of the current
1454 // block to the new block which will contain the Phi node for the select.
1455 sinkMBB->transferSuccessors(BB);
1456 // Next, add the true and fallthrough blocks as its successors.
1457 BB->addSuccessor(copy0MBB);
1458 BB->addSuccessor(sinkMBB);
1459
1460 // copy0MBB:
1461 // %FalseValue = ...
1462 // # fallthrough to sinkMBB
1463 BB = copy0MBB;
1464
1465 // Update machine-CFG edges
1466 BB->addSuccessor(sinkMBB);
1467
1468 // sinkMBB:
1469 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1470 // ...
1471 BB = sinkMBB;
Dale Johannesenbd9ef182009-02-13 02:29:03 +00001472 BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001473 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1474 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1475
1476 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
1477 return BB;
1478}
1479
1480
1481SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1482 SDValue Chain = Op.getOperand(0);
1483 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1484 SDValue LHS = Op.getOperand(2); // LHS of the condition.
1485 SDValue RHS = Op.getOperand(3); // RHS of the condition.
1486 SDValue Dest = Op.getOperand(4); // BB to jump to
1487 unsigned ORIGCC = ~0;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001488 DebugLoc dl = Op.getDebugLoc();
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001489
1490 // If this is a br_cc of a "setcc", and if the setcc got lowered into
1491 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
1492 LookThroughSetCC(LHS, RHS, CC, ORIGCC);
1493 if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
1494
1495 // Get the Compare insn and condition code.
1496 SDValue PIC16CC;
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001497 SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001498
Dale Johannesen2fabcb22009-02-05 01:01:16 +00001499 return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
Sanjiv Gupta1b046942009-01-13 19:18:47 +00001500 Cmp.getValue(1));
1501}
1502
1503