blob: efadaaa1f7296c4c0bf7b8494ed40d0dd81878c8 [file] [log] [blame]
Akira Hatanaka042b7962013-03-14 19:09:52 +00001//===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Subclass of MipsTargetLowering specialized for mips32/64.
11//
12//===----------------------------------------------------------------------===//
13#include "MipsSEISelLowering.h"
14#include "MipsRegisterInfo.h"
15#include "MipsTargetMachine.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
Akira Hatanaka4e0980a2013-04-13 02:13:30 +000018#include "llvm/IR/Intrinsics.h"
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000019#include "llvm/Support/CommandLine.h"
20#include "llvm/Target/TargetInstrInfo.h"
21
22using namespace llvm;
23
24static cl::opt<bool>
25EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
26 cl::desc("MIPS: Enable tail calls."), cl::init(false));
27
28MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
29 : MipsTargetLowering(TM) {
30 // Set up the register classes
Reed Kotlera430cb62013-04-09 19:46:01 +000031
32 clearRegisterClasses();
33
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000034 addRegisterClass(MVT::i32, &Mips::CPURegsRegClass);
35
36 if (HasMips64)
37 addRegisterClass(MVT::i64, &Mips::CPU64RegsRegClass);
38
39 if (Subtarget->hasDSP()) {
40 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
41
42 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
43 addRegisterClass(VecTys[i], &Mips::DSPRegsRegClass);
44
45 // Expand all builtin opcodes.
46 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
47 setOperationAction(Opc, VecTys[i], Expand);
48
Akira Hatanaka3d602412013-04-13 00:55:41 +000049 setOperationAction(ISD::ADD, VecTys[i], Legal);
50 setOperationAction(ISD::SUB, VecTys[i], Legal);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000051 setOperationAction(ISD::LOAD, VecTys[i], Legal);
52 setOperationAction(ISD::STORE, VecTys[i], Legal);
53 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
54 }
Akira Hatanaka97a62bf2013-04-19 23:21:32 +000055
56 setTargetDAGCombine(ISD::SHL);
57 setTargetDAGCombine(ISD::SRA);
58 setTargetDAGCombine(ISD::SRL);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000059 }
60
Akira Hatanaka3d602412013-04-13 00:55:41 +000061 if (Subtarget->hasDSPR2())
62 setOperationAction(ISD::MUL, MVT::v2i16, Legal);
63
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000064 if (!TM.Options.UseSoftFloat) {
65 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
66
67 // When dealing with single precision only, use libcalls
68 if (!Subtarget->isSingleFloat()) {
69 if (HasMips64)
70 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
71 else
72 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
73 }
74 }
75
Akira Hatanakaf5926fd2013-03-30 01:36:35 +000076 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
77 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
78 setOperationAction(ISD::MULHS, MVT::i32, Custom);
79 setOperationAction(ISD::MULHU, MVT::i32, Custom);
80
Akira Hatanakafc82e4d2013-04-11 19:29:26 +000081 if (HasMips64) {
82 setOperationAction(ISD::MULHS, MVT::i64, Custom);
83 setOperationAction(ISD::MULHU, MVT::i64, Custom);
Akira Hatanakaf5926fd2013-03-30 01:36:35 +000084 setOperationAction(ISD::MUL, MVT::i64, Custom);
Akira Hatanakafc82e4d2013-04-11 19:29:26 +000085 }
Akira Hatanakaf5926fd2013-03-30 01:36:35 +000086
Akira Hatanaka4e0980a2013-04-13 02:13:30 +000087 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
88 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
89
Akira Hatanakaf5926fd2013-03-30 01:36:35 +000090 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
91 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
92 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
93 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000094 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
95 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
96 setOperationAction(ISD::LOAD, MVT::i32, Custom);
97 setOperationAction(ISD::STORE, MVT::i32, Custom);
98
Akira Hatanakad593a772013-03-30 01:42:24 +000099 setTargetDAGCombine(ISD::ADDE);
100 setTargetDAGCombine(ISD::SUBE);
101
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000102 computeRegisterProperties();
103}
104
105const MipsTargetLowering *
106llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
107 return new MipsSETargetLowering(TM);
108}
109
110
111bool
112MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
113 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
114
115 switch (SVT) {
116 case MVT::i64:
117 case MVT::i32:
118 if (Fast)
119 *Fast = true;
120 return true;
121 default:
122 return false;
123 }
124}
125
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000126SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
127 SelectionDAG &DAG) const {
128 switch(Op.getOpcode()) {
129 case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
130 case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
131 case ISD::MULHS: return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
132 case ISD::MULHU: return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
133 case ISD::MUL: return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
134 case ISD::SDIVREM: return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
135 case ISD::UDIVREM: return lowerMulDiv(Op, MipsISD::DivRemU, true, true, DAG);
Akira Hatanaka4e0980a2013-04-13 02:13:30 +0000136 case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
137 case ISD::INTRINSIC_W_CHAIN: return lowerINTRINSIC_W_CHAIN(Op, DAG);
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000138 }
139
140 return MipsTargetLowering::LowerOperation(Op, DAG);
141}
142
Akira Hatanakad593a772013-03-30 01:42:24 +0000143// selectMADD -
144// Transforms a subgraph in CurDAG if the following pattern is found:
145// (addc multLo, Lo0), (adde multHi, Hi0),
146// where,
147// multHi/Lo: product of multiplication
148// Lo0: initial value of Lo register
149// Hi0: initial value of Hi register
150// Return true if pattern matching was successful.
151static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
152 // ADDENode's second operand must be a flag output of an ADDC node in order
153 // for the matching to be successful.
154 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
155
156 if (ADDCNode->getOpcode() != ISD::ADDC)
157 return false;
158
159 SDValue MultHi = ADDENode->getOperand(0);
160 SDValue MultLo = ADDCNode->getOperand(0);
161 SDNode *MultNode = MultHi.getNode();
162 unsigned MultOpc = MultHi.getOpcode();
163
164 // MultHi and MultLo must be generated by the same node,
165 if (MultLo.getNode() != MultNode)
166 return false;
167
168 // and it must be a multiplication.
169 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
170 return false;
171
172 // MultLo amd MultHi must be the first and second output of MultNode
173 // respectively.
174 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
175 return false;
176
177 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
178 // of the values of MultNode, in which case MultNode will be removed in later
179 // phases.
180 // If there exist users other than ADDENode or ADDCNode, this function returns
181 // here, which will result in MultNode being mapped to a single MULT
182 // instruction node rather than a pair of MULT and MADD instructions being
183 // produced.
184 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
185 return false;
186
Akira Hatanakad593a772013-03-30 01:42:24 +0000187 DebugLoc DL = ADDENode->getDebugLoc();
188
189 // Initialize accumulator.
190 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
191 ADDCNode->getOperand(1),
192 ADDENode->getOperand(1));
193
194 // create MipsMAdd(u) node
195 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
196
197 SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
198 MultNode->getOperand(0),// Factor 0
199 MultNode->getOperand(1),// Factor 1
200 ACCIn);
201
202 // replace uses of adde and addc here
203 if (!SDValue(ADDCNode, 0).use_empty()) {
204 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
205 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
206 LoIdx);
207 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
208 }
209 if (!SDValue(ADDENode, 0).use_empty()) {
210 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
211 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
212 HiIdx);
213 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
214 }
215
216 return true;
217}
218
219// selectMSUB -
220// Transforms a subgraph in CurDAG if the following pattern is found:
221// (addc Lo0, multLo), (sube Hi0, multHi),
222// where,
223// multHi/Lo: product of multiplication
224// Lo0: initial value of Lo register
225// Hi0: initial value of Hi register
226// Return true if pattern matching was successful.
227static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
228 // SUBENode's second operand must be a flag output of an SUBC node in order
229 // for the matching to be successful.
230 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
231
232 if (SUBCNode->getOpcode() != ISD::SUBC)
233 return false;
234
235 SDValue MultHi = SUBENode->getOperand(1);
236 SDValue MultLo = SUBCNode->getOperand(1);
237 SDNode *MultNode = MultHi.getNode();
238 unsigned MultOpc = MultHi.getOpcode();
239
240 // MultHi and MultLo must be generated by the same node,
241 if (MultLo.getNode() != MultNode)
242 return false;
243
244 // and it must be a multiplication.
245 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
246 return false;
247
248 // MultLo amd MultHi must be the first and second output of MultNode
249 // respectively.
250 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
251 return false;
252
253 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
254 // of the values of MultNode, in which case MultNode will be removed in later
255 // phases.
256 // If there exist users other than SUBENode or SUBCNode, this function returns
257 // here, which will result in MultNode being mapped to a single MULT
258 // instruction node rather than a pair of MULT and MSUB instructions being
259 // produced.
260 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
261 return false;
262
Akira Hatanakad593a772013-03-30 01:42:24 +0000263 DebugLoc DL = SUBENode->getDebugLoc();
264
265 // Initialize accumulator.
266 SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
267 SUBCNode->getOperand(0),
268 SUBENode->getOperand(0));
269
270 // create MipsSub(u) node
271 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
272
273 SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
274 MultNode->getOperand(0),// Factor 0
275 MultNode->getOperand(1),// Factor 1
276 ACCIn);
277
278 // replace uses of sube and subc here
279 if (!SDValue(SUBCNode, 0).use_empty()) {
280 SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
281 SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
282 LoIdx);
283 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
284 }
285 if (!SDValue(SUBENode, 0).use_empty()) {
286 SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
287 SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
288 HiIdx);
289 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
290 }
291
292 return true;
293}
294
295static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
296 TargetLowering::DAGCombinerInfo &DCI,
297 const MipsSubtarget *Subtarget) {
298 if (DCI.isBeforeLegalize())
299 return SDValue();
300
301 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
302 selectMADD(N, &DAG))
303 return SDValue(N, 0);
304
305 return SDValue();
306}
307
308static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
309 TargetLowering::DAGCombinerInfo &DCI,
310 const MipsSubtarget *Subtarget) {
311 if (DCI.isBeforeLegalize())
312 return SDValue();
313
314 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
315 selectMSUB(N, &DAG))
316 return SDValue(N, 0);
317
318 return SDValue();
319}
320
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000321static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
322 SelectionDAG &DAG,
323 const MipsSubtarget *Subtarget) {
324 // See if this is a vector splat immediate node.
325 APInt SplatValue, SplatUndef;
326 unsigned SplatBitSize;
327 bool HasAnyUndefs;
328 unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
329 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
330
331 if (!BV || !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
332 HasAnyUndefs, EltSize,
333 !Subtarget->isLittle()))
334 return SDValue();
335
336 return DAG.getNode(Opc, N->getDebugLoc(), Ty, N->getOperand(0),
337 DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
338}
339
340static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
341 TargetLowering::DAGCombinerInfo &DCI,
342 const MipsSubtarget *Subtarget) {
343 EVT Ty = N->getValueType(0);
344
345 if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
346 return SDValue();
347
348 return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
349}
350
351static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
352 TargetLowering::DAGCombinerInfo &DCI,
353 const MipsSubtarget *Subtarget) {
354 EVT Ty = N->getValueType(0);
355
356 if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
357 return SDValue();
358
359 return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
360}
361
362
363static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
364 TargetLowering::DAGCombinerInfo &DCI,
365 const MipsSubtarget *Subtarget) {
366 EVT Ty = N->getValueType(0);
367
368 if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
369 return SDValue();
370
371 return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
372}
373
Akira Hatanakad593a772013-03-30 01:42:24 +0000374SDValue
375MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
376 SelectionDAG &DAG = DCI.DAG;
377
378 switch (N->getOpcode()) {
379 case ISD::ADDE:
380 return performADDECombine(N, DAG, DCI, Subtarget);
381 case ISD::SUBE:
382 return performSUBECombine(N, DAG, DCI, Subtarget);
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000383 case ISD::SHL:
384 return performSHLCombine(N, DAG, DCI, Subtarget);
385 case ISD::SRA:
386 return performSRACombine(N, DAG, DCI, Subtarget);
387 case ISD::SRL:
388 return performSRLCombine(N, DAG, DCI, Subtarget);
Akira Hatanakad593a772013-03-30 01:42:24 +0000389 default:
390 return MipsTargetLowering::PerformDAGCombine(N, DCI);
391 }
392}
393
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000394MachineBasicBlock *
395MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
396 MachineBasicBlock *BB) const {
397 switch (MI->getOpcode()) {
398 default:
399 return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
400 case Mips::BPOSGE32_PSEUDO:
401 return emitBPOSGE32(MI, BB);
402 }
403}
404
405bool MipsSETargetLowering::
406isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
407 unsigned NextStackOffset,
408 const MipsFunctionInfo& FI) const {
409 if (!EnableMipsTailCalls)
410 return false;
411
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000412 // Return false if either the callee or caller has a byval argument.
413 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
414 return false;
415
416 // Return true if the callee's argument area is no larger than the
417 // caller's.
418 return NextStackOffset <= FI.getIncomingArgSize();
419}
420
421void MipsSETargetLowering::
422getOpndList(SmallVectorImpl<SDValue> &Ops,
423 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
424 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
425 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
426 // T9 should contain the address of the callee function if
427 // -reloction-model=pic or it is an indirect call.
428 if (IsPICCall || !GlobalOrExternal) {
429 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
430 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
431 } else
432 Ops.push_back(Callee);
433
434 MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
435 InternalLinkage, CLI, Callee, Chain);
436}
437
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000438SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
439 bool HasLo, bool HasHi,
440 SelectionDAG &DAG) const {
441 EVT Ty = Op.getOperand(0).getValueType();
442 DebugLoc DL = Op.getDebugLoc();
443 SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
444 Op.getOperand(0), Op.getOperand(1));
445 SDValue Lo, Hi;
446
447 if (HasLo)
448 Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
449 DAG.getConstant(Mips::sub_lo, MVT::i32));
450 if (HasHi)
451 Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
452 DAG.getConstant(Mips::sub_hi, MVT::i32));
453
454 if (!HasLo || !HasHi)
455 return HasLo ? Lo : Hi;
456
457 SDValue Vals[] = { Lo, Hi };
458 return DAG.getMergeValues(Vals, 2, DL);
459}
460
Akira Hatanaka4e0980a2013-04-13 02:13:30 +0000461
462static SDValue initAccumulator(SDValue In, DebugLoc DL, SelectionDAG &DAG) {
463 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
464 DAG.getConstant(0, MVT::i32));
465 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
466 DAG.getConstant(1, MVT::i32));
467 return DAG.getNode(MipsISD::InsertLOHI, DL, MVT::Untyped, InLo, InHi);
468}
469
470static SDValue extractLOHI(SDValue Op, DebugLoc DL, SelectionDAG &DAG) {
471 SDValue Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
472 DAG.getConstant(Mips::sub_lo, MVT::i32));
473 SDValue Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
474 DAG.getConstant(Mips::sub_hi, MVT::i32));
475 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
476}
477
478// This function expands mips intrinsic nodes which have 64-bit input operands
479// or output values.
480//
481// out64 = intrinsic-node in64
482// =>
483// lo = copy (extract-element (in64, 0))
484// hi = copy (extract-element (in64, 1))
485// mips-specific-node
486// v0 = copy lo
487// v1 = copy hi
488// out64 = merge-values (v0, v1)
489//
490static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
491 DebugLoc DL = Op.getDebugLoc();
492 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
493 SmallVector<SDValue, 3> Ops;
494 unsigned OpNo = 0;
495
496 // See if Op has a chain input.
497 if (HasChainIn)
498 Ops.push_back(Op->getOperand(OpNo++));
499
500 // The next operand is the intrinsic opcode.
501 assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
502
503 // See if the next operand has type i64.
504 SDValue Opnd = Op->getOperand(++OpNo), In64;
505
506 if (Opnd.getValueType() == MVT::i64)
507 In64 = initAccumulator(Opnd, DL, DAG);
508 else
509 Ops.push_back(Opnd);
510
511 // Push the remaining operands.
512 for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
513 Ops.push_back(Op->getOperand(OpNo));
514
515 // Add In64 to the end of the list.
516 if (In64.getNode())
517 Ops.push_back(In64);
518
519 // Scan output.
520 SmallVector<EVT, 2> ResTys;
521
522 for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
523 I != E; ++I)
524 ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
525
526 // Create node.
527 SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
528 SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
529
530 if (!HasChainIn)
531 return Out;
532
533 assert(Val->getValueType(1) == MVT::Other);
534 SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
535 return DAG.getMergeValues(Vals, 2, DL);
536}
537
538SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
539 SelectionDAG &DAG) const {
540 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
541 default:
542 return SDValue();
543 case Intrinsic::mips_shilo:
544 return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
545 case Intrinsic::mips_dpau_h_qbl:
546 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
547 case Intrinsic::mips_dpau_h_qbr:
548 return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
549 case Intrinsic::mips_dpsu_h_qbl:
550 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
551 case Intrinsic::mips_dpsu_h_qbr:
552 return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
553 case Intrinsic::mips_dpa_w_ph:
554 return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
555 case Intrinsic::mips_dps_w_ph:
556 return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
557 case Intrinsic::mips_dpax_w_ph:
558 return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
559 case Intrinsic::mips_dpsx_w_ph:
560 return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
561 case Intrinsic::mips_mulsa_w_ph:
562 return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
563 case Intrinsic::mips_mult:
564 return lowerDSPIntr(Op, DAG, MipsISD::Mult);
565 case Intrinsic::mips_multu:
566 return lowerDSPIntr(Op, DAG, MipsISD::Multu);
567 case Intrinsic::mips_madd:
568 return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
569 case Intrinsic::mips_maddu:
570 return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
571 case Intrinsic::mips_msub:
572 return lowerDSPIntr(Op, DAG, MipsISD::MSub);
573 case Intrinsic::mips_msubu:
574 return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
575 }
576}
577
578SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
579 SelectionDAG &DAG) const {
580 switch (cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue()) {
581 default:
582 return SDValue();
583 case Intrinsic::mips_extp:
584 return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
585 case Intrinsic::mips_extpdp:
586 return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
587 case Intrinsic::mips_extr_w:
588 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
589 case Intrinsic::mips_extr_r_w:
590 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
591 case Intrinsic::mips_extr_rs_w:
592 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
593 case Intrinsic::mips_extr_s_h:
594 return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
595 case Intrinsic::mips_mthlip:
596 return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
597 case Intrinsic::mips_mulsaq_s_w_ph:
598 return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
599 case Intrinsic::mips_maq_s_w_phl:
600 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
601 case Intrinsic::mips_maq_s_w_phr:
602 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
603 case Intrinsic::mips_maq_sa_w_phl:
604 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
605 case Intrinsic::mips_maq_sa_w_phr:
606 return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
607 case Intrinsic::mips_dpaq_s_w_ph:
608 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
609 case Intrinsic::mips_dpsq_s_w_ph:
610 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
611 case Intrinsic::mips_dpaq_sa_l_w:
612 return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
613 case Intrinsic::mips_dpsq_sa_l_w:
614 return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
615 case Intrinsic::mips_dpaqx_s_w_ph:
616 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
617 case Intrinsic::mips_dpaqx_sa_w_ph:
618 return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
619 case Intrinsic::mips_dpsqx_s_w_ph:
620 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
621 case Intrinsic::mips_dpsqx_sa_w_ph:
622 return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
623 }
624}
625
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000626MachineBasicBlock * MipsSETargetLowering::
627emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
628 // $bb:
629 // bposge32_pseudo $vr0
630 // =>
631 // $bb:
632 // bposge32 $tbb
633 // $fbb:
634 // li $vr2, 0
635 // b $sink
636 // $tbb:
637 // li $vr1, 1
638 // $sink:
639 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
640
641 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
642 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
643 const TargetRegisterClass *RC = &Mips::CPURegsRegClass;
644 DebugLoc DL = MI->getDebugLoc();
645 const BasicBlock *LLVM_BB = BB->getBasicBlock();
646 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
647 MachineFunction *F = BB->getParent();
648 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
649 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
650 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
651 F->insert(It, FBB);
652 F->insert(It, TBB);
653 F->insert(It, Sink);
654
655 // Transfer the remainder of BB and its successor edges to Sink.
656 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
657 BB->end());
658 Sink->transferSuccessorsAndUpdatePHIs(BB);
659
660 // Add successors.
661 BB->addSuccessor(FBB);
662 BB->addSuccessor(TBB);
663 FBB->addSuccessor(Sink);
664 TBB->addSuccessor(Sink);
665
666 // Insert the real bposge32 instruction to $BB.
667 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
668
669 // Fill $FBB.
670 unsigned VR2 = RegInfo.createVirtualRegister(RC);
671 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
672 .addReg(Mips::ZERO).addImm(0);
673 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
674
675 // Fill $TBB.
676 unsigned VR1 = RegInfo.createVirtualRegister(RC);
677 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
678 .addReg(Mips::ZERO).addImm(1);
679
680 // Insert phi function to $Sink.
681 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
682 MI->getOperand(0).getReg())
683 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
684
685 MI->eraseFromParent(); // The pseudo instruction is gone now.
686 return Sink;
687}