80 columns.
[oota-llvm.git] / lib / Target / Mips / MipsSEISelLowering.cpp
1 //===-- MipsSEISelLowering.cpp - MipsSE DAG Lowering Interface --*- C++ -*-===//
2 //
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"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Target/TargetInstrInfo.h"
21
22 using namespace llvm;
23
24 static cl::opt<bool>
25 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
26                     cl::desc("MIPS: Enable tail calls."), cl::init(false));
27
28 MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
29   : MipsTargetLowering(TM) {
30   // Set up the register classes
31
32   clearRegisterClasses();
33
34   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
49       setOperationAction(ISD::ADD, VecTys[i], Legal);
50       setOperationAction(ISD::SUB, VecTys[i], Legal);
51       setOperationAction(ISD::LOAD, VecTys[i], Legal);
52       setOperationAction(ISD::STORE, VecTys[i], Legal);
53       setOperationAction(ISD::BITCAST, VecTys[i], Legal);
54     }
55
56     setTargetDAGCombine(ISD::SHL);
57     setTargetDAGCombine(ISD::SRA);
58     setTargetDAGCombine(ISD::SRL);
59   }
60
61   if (Subtarget->hasDSPR2())
62     setOperationAction(ISD::MUL, MVT::v2i16, Legal);
63
64   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
76   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
81   if (HasMips64) {
82     setOperationAction(ISD::MULHS,            MVT::i64, Custom);
83     setOperationAction(ISD::MULHU,            MVT::i64, Custom);
84     setOperationAction(ISD::MUL,              MVT::i64, Custom);
85   }
86
87   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
88   setOperationAction(ISD::INTRINSIC_W_CHAIN,  MVT::i64, Custom);
89
90   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);
94   setOperationAction(ISD::ATOMIC_FENCE,       MVT::Other, Custom);
95   setOperationAction(ISD::LOAD,               MVT::i32, Custom);
96   setOperationAction(ISD::STORE,              MVT::i32, Custom);
97
98   setTargetDAGCombine(ISD::ADDE);
99   setTargetDAGCombine(ISD::SUBE);
100
101   computeRegisterProperties();
102 }
103
104 const MipsTargetLowering *
105 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
106   return new MipsSETargetLowering(TM);
107 }
108
109
110 bool
111 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
112   MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
113
114   switch (SVT) {
115   case MVT::i64:
116   case MVT::i32:
117     if (Fast)
118       *Fast = true;
119     return true;
120   default:
121     return false;
122   }
123 }
124
125 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
126                                              SelectionDAG &DAG) const {
127   switch(Op.getOpcode()) {
128   case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
129   case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
130   case ISD::MULHS:     return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
131   case ISD::MULHU:     return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
132   case ISD::MUL:       return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
133   case ISD::SDIVREM:   return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
134   case ISD::UDIVREM:   return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
135                                           DAG);
136   case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
137   case ISD::INTRINSIC_W_CHAIN:  return lowerINTRINSIC_W_CHAIN(Op, DAG);
138   }
139
140   return MipsTargetLowering::LowerOperation(Op, DAG);
141 }
142
143 // 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.
151 static 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
187   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.
227 static 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
263   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
295 static 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
308 static 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
321 static 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 ||
332       !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
333                            EltSize,!Subtarget->isLittle()) ||
334       (SplatBitSize != EltSize) ||
335       !isUIntN(Log2_32(EltSize), SplatValue.getZExtValue()))
336     return SDValue();
337
338   return DAG.getNode(Opc, N->getDebugLoc(), Ty, N->getOperand(0),
339                      DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
340 }
341
342 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
343                                  TargetLowering::DAGCombinerInfo &DCI,
344                                  const MipsSubtarget *Subtarget) {
345   EVT Ty = N->getValueType(0);
346
347   if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
348     return SDValue();
349
350   return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
351 }
352
353 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
354                                  TargetLowering::DAGCombinerInfo &DCI,
355                                  const MipsSubtarget *Subtarget) {
356   EVT Ty = N->getValueType(0);
357
358   if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
359     return SDValue();
360
361   return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
362 }
363
364
365 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
366                                  TargetLowering::DAGCombinerInfo &DCI,
367                                  const MipsSubtarget *Subtarget) {
368   EVT Ty = N->getValueType(0);
369
370   if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
371     return SDValue();
372
373   return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
374 }
375
376 SDValue
377 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
378   SelectionDAG &DAG = DCI.DAG;
379
380   switch (N->getOpcode()) {
381   case ISD::ADDE:
382     return performADDECombine(N, DAG, DCI, Subtarget);
383   case ISD::SUBE:
384     return performSUBECombine(N, DAG, DCI, Subtarget);
385   case ISD::SHL:
386     return performSHLCombine(N, DAG, DCI, Subtarget);
387   case ISD::SRA:
388     return performSRACombine(N, DAG, DCI, Subtarget);
389   case ISD::SRL:
390     return performSRLCombine(N, DAG, DCI, Subtarget);
391   default:
392     return MipsTargetLowering::PerformDAGCombine(N, DCI);
393   }
394 }
395
396 MachineBasicBlock *
397 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
398                                                   MachineBasicBlock *BB) const {
399   switch (MI->getOpcode()) {
400   default:
401     return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
402   case Mips::BPOSGE32_PSEUDO:
403     return emitBPOSGE32(MI, BB);
404   }
405 }
406
407 bool MipsSETargetLowering::
408 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
409                                   unsigned NextStackOffset,
410                                   const MipsFunctionInfo& FI) const {
411   if (!EnableMipsTailCalls)
412     return false;
413
414   // Return false if either the callee or caller has a byval argument.
415   if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
416     return false;
417
418   // Return true if the callee's argument area is no larger than the
419   // caller's.
420   return NextStackOffset <= FI.getIncomingArgSize();
421 }
422
423 void MipsSETargetLowering::
424 getOpndList(SmallVectorImpl<SDValue> &Ops,
425             std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
426             bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
427             CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
428   // T9 should contain the address of the callee function if
429   // -reloction-model=pic or it is an indirect call.
430   if (IsPICCall || !GlobalOrExternal) {
431     unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
432     RegsToPass.push_front(std::make_pair(T9Reg, Callee));
433   } else
434     Ops.push_back(Callee);
435
436   MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
437                                   InternalLinkage, CLI, Callee, Chain);
438 }
439
440 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
441                                           bool HasLo, bool HasHi,
442                                           SelectionDAG &DAG) const {
443   EVT Ty = Op.getOperand(0).getValueType();
444   DebugLoc DL = Op.getDebugLoc();
445   SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
446                              Op.getOperand(0), Op.getOperand(1));
447   SDValue Lo, Hi;
448
449   if (HasLo)
450     Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
451                      DAG.getConstant(Mips::sub_lo, MVT::i32));
452   if (HasHi)
453     Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
454                      DAG.getConstant(Mips::sub_hi, MVT::i32));
455
456   if (!HasLo || !HasHi)
457     return HasLo ? Lo : Hi;
458
459   SDValue Vals[] = { Lo, Hi };
460   return DAG.getMergeValues(Vals, 2, DL);
461 }
462
463
464 static SDValue initAccumulator(SDValue In, DebugLoc DL, SelectionDAG &DAG) {
465   SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
466                              DAG.getConstant(0, MVT::i32));
467   SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
468                              DAG.getConstant(1, MVT::i32));
469   return DAG.getNode(MipsISD::InsertLOHI, DL, MVT::Untyped, InLo, InHi);
470 }
471
472 static SDValue extractLOHI(SDValue Op, DebugLoc DL, SelectionDAG &DAG) {
473   SDValue Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
474                            DAG.getConstant(Mips::sub_lo, MVT::i32));
475   SDValue Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
476                            DAG.getConstant(Mips::sub_hi, MVT::i32));
477   return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
478 }
479
480 // This function expands mips intrinsic nodes which have 64-bit input operands
481 // or output values.
482 //
483 // out64 = intrinsic-node in64
484 // =>
485 // lo = copy (extract-element (in64, 0))
486 // hi = copy (extract-element (in64, 1))
487 // mips-specific-node
488 // v0 = copy lo
489 // v1 = copy hi
490 // out64 = merge-values (v0, v1)
491 //
492 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
493   DebugLoc DL = Op.getDebugLoc();
494   bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
495   SmallVector<SDValue, 3> Ops;
496   unsigned OpNo = 0;
497
498   // See if Op has a chain input.
499   if (HasChainIn)
500     Ops.push_back(Op->getOperand(OpNo++));
501
502   // The next operand is the intrinsic opcode.
503   assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
504
505   // See if the next operand has type i64.
506   SDValue Opnd = Op->getOperand(++OpNo), In64;
507
508   if (Opnd.getValueType() == MVT::i64)
509     In64 = initAccumulator(Opnd, DL, DAG);
510   else
511     Ops.push_back(Opnd);
512
513   // Push the remaining operands.
514   for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
515     Ops.push_back(Op->getOperand(OpNo));
516
517   // Add In64 to the end of the list.
518   if (In64.getNode())
519     Ops.push_back(In64);
520
521   // Scan output.
522   SmallVector<EVT, 2> ResTys;
523
524   for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
525        I != E; ++I)
526     ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
527
528   // Create node.
529   SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
530   SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
531
532   if (!HasChainIn)
533     return Out;
534
535   assert(Val->getValueType(1) == MVT::Other);
536   SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
537   return DAG.getMergeValues(Vals, 2, DL);
538 }
539
540 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
541                                                       SelectionDAG &DAG) const {
542   switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
543   default:
544     return SDValue();
545   case Intrinsic::mips_shilo:
546     return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
547   case Intrinsic::mips_dpau_h_qbl:
548     return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
549   case Intrinsic::mips_dpau_h_qbr:
550     return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
551   case Intrinsic::mips_dpsu_h_qbl:
552     return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
553   case Intrinsic::mips_dpsu_h_qbr:
554     return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
555   case Intrinsic::mips_dpa_w_ph:
556     return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
557   case Intrinsic::mips_dps_w_ph:
558     return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
559   case Intrinsic::mips_dpax_w_ph:
560     return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
561   case Intrinsic::mips_dpsx_w_ph:
562     return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
563   case Intrinsic::mips_mulsa_w_ph:
564     return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
565   case Intrinsic::mips_mult:
566     return lowerDSPIntr(Op, DAG, MipsISD::Mult);
567   case Intrinsic::mips_multu:
568     return lowerDSPIntr(Op, DAG, MipsISD::Multu);
569   case Intrinsic::mips_madd:
570     return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
571   case Intrinsic::mips_maddu:
572     return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
573   case Intrinsic::mips_msub:
574     return lowerDSPIntr(Op, DAG, MipsISD::MSub);
575   case Intrinsic::mips_msubu:
576     return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
577   }
578 }
579
580 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
581                                                      SelectionDAG &DAG) const {
582   switch (cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue()) {
583   default:
584     return SDValue();
585   case Intrinsic::mips_extp:
586     return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
587   case Intrinsic::mips_extpdp:
588     return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
589   case Intrinsic::mips_extr_w:
590     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
591   case Intrinsic::mips_extr_r_w:
592     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
593   case Intrinsic::mips_extr_rs_w:
594     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
595   case Intrinsic::mips_extr_s_h:
596     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
597   case Intrinsic::mips_mthlip:
598     return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
599   case Intrinsic::mips_mulsaq_s_w_ph:
600     return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
601   case Intrinsic::mips_maq_s_w_phl:
602     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
603   case Intrinsic::mips_maq_s_w_phr:
604     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
605   case Intrinsic::mips_maq_sa_w_phl:
606     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
607   case Intrinsic::mips_maq_sa_w_phr:
608     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
609   case Intrinsic::mips_dpaq_s_w_ph:
610     return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
611   case Intrinsic::mips_dpsq_s_w_ph:
612     return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
613   case Intrinsic::mips_dpaq_sa_l_w:
614     return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
615   case Intrinsic::mips_dpsq_sa_l_w:
616     return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
617   case Intrinsic::mips_dpaqx_s_w_ph:
618     return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
619   case Intrinsic::mips_dpaqx_sa_w_ph:
620     return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
621   case Intrinsic::mips_dpsqx_s_w_ph:
622     return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
623   case Intrinsic::mips_dpsqx_sa_w_ph:
624     return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
625   }
626 }
627
628 MachineBasicBlock * MipsSETargetLowering::
629 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
630   // $bb:
631   //  bposge32_pseudo $vr0
632   //  =>
633   // $bb:
634   //  bposge32 $tbb
635   // $fbb:
636   //  li $vr2, 0
637   //  b $sink
638   // $tbb:
639   //  li $vr1, 1
640   // $sink:
641   //  $vr0 = phi($vr2, $fbb, $vr1, $tbb)
642
643   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
644   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
645   const TargetRegisterClass *RC = &Mips::CPURegsRegClass;
646   DebugLoc DL = MI->getDebugLoc();
647   const BasicBlock *LLVM_BB = BB->getBasicBlock();
648   MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
649   MachineFunction *F = BB->getParent();
650   MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
651   MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
652   MachineBasicBlock *Sink  = F->CreateMachineBasicBlock(LLVM_BB);
653   F->insert(It, FBB);
654   F->insert(It, TBB);
655   F->insert(It, Sink);
656
657   // Transfer the remainder of BB and its successor edges to Sink.
658   Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
659                BB->end());
660   Sink->transferSuccessorsAndUpdatePHIs(BB);
661
662   // Add successors.
663   BB->addSuccessor(FBB);
664   BB->addSuccessor(TBB);
665   FBB->addSuccessor(Sink);
666   TBB->addSuccessor(Sink);
667
668   // Insert the real bposge32 instruction to $BB.
669   BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
670
671   // Fill $FBB.
672   unsigned VR2 = RegInfo.createVirtualRegister(RC);
673   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
674     .addReg(Mips::ZERO).addImm(0);
675   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
676
677   // Fill $TBB.
678   unsigned VR1 = RegInfo.createVirtualRegister(RC);
679   BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
680     .addReg(Mips::ZERO).addImm(1);
681
682   // Insert phi function to $Sink.
683   BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
684           MI->getOperand(0).getReg())
685     .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
686
687   MI->eraseFromParent();   // The pseudo instruction is gone now.
688   return Sink;
689 }