[mips][msa] Added support for matching addv from normal IR (i.e. not intrinsics)
[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 static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
29                                    cl::desc("Expand double precision loads and "
30                                             "stores to their single precision "
31                                             "counterparts"));
32
33 MipsSETargetLowering::MipsSETargetLowering(MipsTargetMachine &TM)
34   : MipsTargetLowering(TM) {
35   // Set up the register classes
36
37   clearRegisterClasses();
38
39   addRegisterClass(MVT::i32, &Mips::GPR32RegClass);
40
41   if (HasMips64)
42     addRegisterClass(MVT::i64, &Mips::GPR64RegClass);
43
44   if (Subtarget->hasDSP()) {
45     MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
46
47     for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
48       addRegisterClass(VecTys[i], &Mips::DSPRRegClass);
49
50       // Expand all builtin opcodes.
51       for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
52         setOperationAction(Opc, VecTys[i], Expand);
53
54       setOperationAction(ISD::ADD, VecTys[i], Legal);
55       setOperationAction(ISD::SUB, VecTys[i], Legal);
56       setOperationAction(ISD::LOAD, VecTys[i], Legal);
57       setOperationAction(ISD::STORE, VecTys[i], Legal);
58       setOperationAction(ISD::BITCAST, VecTys[i], Legal);
59     }
60
61     // Expand all truncating stores and extending loads.
62     unsigned FirstVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
63     unsigned LastVT = (unsigned)MVT::LAST_VECTOR_VALUETYPE;
64
65     for (unsigned VT0 = FirstVT; VT0 <= LastVT; ++VT0) {
66       for (unsigned VT1 = FirstVT; VT1 <= LastVT; ++VT1)
67         setTruncStoreAction((MVT::SimpleValueType)VT0,
68                             (MVT::SimpleValueType)VT1, Expand);
69
70       setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
71       setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT0, Expand);
72       setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT0, Expand);
73     }
74
75     setTargetDAGCombine(ISD::SHL);
76     setTargetDAGCombine(ISD::SRA);
77     setTargetDAGCombine(ISD::SRL);
78     setTargetDAGCombine(ISD::SETCC);
79     setTargetDAGCombine(ISD::VSELECT);
80   }
81
82   if (Subtarget->hasDSPR2())
83     setOperationAction(ISD::MUL, MVT::v2i16, Legal);
84
85   if (Subtarget->hasMSA()) {
86     addMSAIntType(MVT::v16i8, &Mips::MSA128BRegClass);
87     addMSAIntType(MVT::v8i16, &Mips::MSA128HRegClass);
88     addMSAIntType(MVT::v4i32, &Mips::MSA128WRegClass);
89     addMSAIntType(MVT::v2i64, &Mips::MSA128DRegClass);
90     addMSAFloatType(MVT::v8f16, &Mips::MSA128HRegClass);
91     addMSAFloatType(MVT::v4f32, &Mips::MSA128WRegClass);
92     addMSAFloatType(MVT::v2f64, &Mips::MSA128DRegClass);
93   }
94
95   if (!Subtarget->mipsSEUsesSoftFloat()) {
96     addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
97
98     // When dealing with single precision only, use libcalls
99     if (!Subtarget->isSingleFloat()) {
100       if (Subtarget->isFP64bit())
101         addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
102       else
103         addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
104     }
105   }
106
107   setOperationAction(ISD::SMUL_LOHI,          MVT::i32, Custom);
108   setOperationAction(ISD::UMUL_LOHI,          MVT::i32, Custom);
109   setOperationAction(ISD::MULHS,              MVT::i32, Custom);
110   setOperationAction(ISD::MULHU,              MVT::i32, Custom);
111
112   if (HasMips64) {
113     setOperationAction(ISD::MULHS,            MVT::i64, Custom);
114     setOperationAction(ISD::MULHU,            MVT::i64, Custom);
115     setOperationAction(ISD::MUL,              MVT::i64, Custom);
116   }
117
118   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
119   setOperationAction(ISD::INTRINSIC_W_CHAIN,  MVT::i64, Custom);
120
121   setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
122   setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
123   setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
124   setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
125   setOperationAction(ISD::ATOMIC_FENCE,       MVT::Other, Custom);
126   setOperationAction(ISD::LOAD,               MVT::i32, Custom);
127   setOperationAction(ISD::STORE,              MVT::i32, Custom);
128
129   setTargetDAGCombine(ISD::ADDE);
130   setTargetDAGCombine(ISD::SUBE);
131   setTargetDAGCombine(ISD::MUL);
132
133   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
134   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
135   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
136
137   if (NoDPLoadStore) {
138     setOperationAction(ISD::LOAD, MVT::f64, Custom);
139     setOperationAction(ISD::STORE, MVT::f64, Custom);
140   }
141
142   computeRegisterProperties();
143 }
144
145 const MipsTargetLowering *
146 llvm::createMipsSETargetLowering(MipsTargetMachine &TM) {
147   return new MipsSETargetLowering(TM);
148 }
149
150 void MipsSETargetLowering::
151 addMSAIntType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
152   addRegisterClass(Ty, RC);
153
154   // Expand all builtin opcodes.
155   for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
156     setOperationAction(Opc, Ty, Expand);
157
158   setOperationAction(ISD::BITCAST, Ty, Legal);
159   setOperationAction(ISD::LOAD, Ty, Legal);
160   setOperationAction(ISD::STORE, Ty, Legal);
161
162   setOperationAction(ISD::ADD, Ty, Legal);
163 }
164
165 void MipsSETargetLowering::
166 addMSAFloatType(MVT::SimpleValueType Ty, const TargetRegisterClass *RC) {
167   addRegisterClass(Ty, RC);
168
169   // Expand all builtin opcodes.
170   for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
171     setOperationAction(Opc, Ty, Expand);
172
173   setOperationAction(ISD::LOAD, Ty, Legal);
174   setOperationAction(ISD::STORE, Ty, Legal);
175   setOperationAction(ISD::BITCAST, Ty, Legal);
176 }
177
178 bool
179 MipsSETargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
180   MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
181
182   switch (SVT) {
183   case MVT::i64:
184   case MVT::i32:
185     if (Fast)
186       *Fast = true;
187     return true;
188   default:
189     return false;
190   }
191 }
192
193 SDValue MipsSETargetLowering::LowerOperation(SDValue Op,
194                                              SelectionDAG &DAG) const {
195   switch(Op.getOpcode()) {
196   case ISD::LOAD:  return lowerLOAD(Op, DAG);
197   case ISD::STORE: return lowerSTORE(Op, DAG);
198   case ISD::SMUL_LOHI: return lowerMulDiv(Op, MipsISD::Mult, true, true, DAG);
199   case ISD::UMUL_LOHI: return lowerMulDiv(Op, MipsISD::Multu, true, true, DAG);
200   case ISD::MULHS:     return lowerMulDiv(Op, MipsISD::Mult, false, true, DAG);
201   case ISD::MULHU:     return lowerMulDiv(Op, MipsISD::Multu, false, true, DAG);
202   case ISD::MUL:       return lowerMulDiv(Op, MipsISD::Mult, true, false, DAG);
203   case ISD::SDIVREM:   return lowerMulDiv(Op, MipsISD::DivRem, true, true, DAG);
204   case ISD::UDIVREM:   return lowerMulDiv(Op, MipsISD::DivRemU, true, true,
205                                           DAG);
206   case ISD::INTRINSIC_WO_CHAIN: return lowerINTRINSIC_WO_CHAIN(Op, DAG);
207   case ISD::INTRINSIC_W_CHAIN:  return lowerINTRINSIC_W_CHAIN(Op, DAG);
208   case ISD::INTRINSIC_VOID:     return lowerINTRINSIC_VOID(Op, DAG);
209   }
210
211   return MipsTargetLowering::LowerOperation(Op, DAG);
212 }
213
214 // selectMADD -
215 // Transforms a subgraph in CurDAG if the following pattern is found:
216 //  (addc multLo, Lo0), (adde multHi, Hi0),
217 // where,
218 //  multHi/Lo: product of multiplication
219 //  Lo0: initial value of Lo register
220 //  Hi0: initial value of Hi register
221 // Return true if pattern matching was successful.
222 static bool selectMADD(SDNode *ADDENode, SelectionDAG *CurDAG) {
223   // ADDENode's second operand must be a flag output of an ADDC node in order
224   // for the matching to be successful.
225   SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
226
227   if (ADDCNode->getOpcode() != ISD::ADDC)
228     return false;
229
230   SDValue MultHi = ADDENode->getOperand(0);
231   SDValue MultLo = ADDCNode->getOperand(0);
232   SDNode *MultNode = MultHi.getNode();
233   unsigned MultOpc = MultHi.getOpcode();
234
235   // MultHi and MultLo must be generated by the same node,
236   if (MultLo.getNode() != MultNode)
237     return false;
238
239   // and it must be a multiplication.
240   if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
241     return false;
242
243   // MultLo amd MultHi must be the first and second output of MultNode
244   // respectively.
245   if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
246     return false;
247
248   // Transform this to a MADD only if ADDENode and ADDCNode are the only users
249   // of the values of MultNode, in which case MultNode will be removed in later
250   // phases.
251   // If there exist users other than ADDENode or ADDCNode, this function returns
252   // here, which will result in MultNode being mapped to a single MULT
253   // instruction node rather than a pair of MULT and MADD instructions being
254   // produced.
255   if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
256     return false;
257
258   SDLoc DL(ADDENode);
259
260   // Initialize accumulator.
261   SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
262                                   ADDCNode->getOperand(1),
263                                   ADDENode->getOperand(1));
264
265   // create MipsMAdd(u) node
266   MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
267
268   SDValue MAdd = CurDAG->getNode(MultOpc, DL, MVT::Untyped,
269                                  MultNode->getOperand(0),// Factor 0
270                                  MultNode->getOperand(1),// Factor 1
271                                  ACCIn);
272
273   // replace uses of adde and addc here
274   if (!SDValue(ADDCNode, 0).use_empty()) {
275     SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
276     SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
277                                     LoIdx);
278     CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), LoOut);
279   }
280   if (!SDValue(ADDENode, 0).use_empty()) {
281     SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
282     SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MAdd,
283                                     HiIdx);
284     CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), HiOut);
285   }
286
287   return true;
288 }
289
290 // selectMSUB -
291 // Transforms a subgraph in CurDAG if the following pattern is found:
292 //  (addc Lo0, multLo), (sube Hi0, multHi),
293 // where,
294 //  multHi/Lo: product of multiplication
295 //  Lo0: initial value of Lo register
296 //  Hi0: initial value of Hi register
297 // Return true if pattern matching was successful.
298 static bool selectMSUB(SDNode *SUBENode, SelectionDAG *CurDAG) {
299   // SUBENode's second operand must be a flag output of an SUBC node in order
300   // for the matching to be successful.
301   SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
302
303   if (SUBCNode->getOpcode() != ISD::SUBC)
304     return false;
305
306   SDValue MultHi = SUBENode->getOperand(1);
307   SDValue MultLo = SUBCNode->getOperand(1);
308   SDNode *MultNode = MultHi.getNode();
309   unsigned MultOpc = MultHi.getOpcode();
310
311   // MultHi and MultLo must be generated by the same node,
312   if (MultLo.getNode() != MultNode)
313     return false;
314
315   // and it must be a multiplication.
316   if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
317     return false;
318
319   // MultLo amd MultHi must be the first and second output of MultNode
320   // respectively.
321   if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
322     return false;
323
324   // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
325   // of the values of MultNode, in which case MultNode will be removed in later
326   // phases.
327   // If there exist users other than SUBENode or SUBCNode, this function returns
328   // here, which will result in MultNode being mapped to a single MULT
329   // instruction node rather than a pair of MULT and MSUB instructions being
330   // produced.
331   if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
332     return false;
333
334   SDLoc DL(SUBENode);
335
336   // Initialize accumulator.
337   SDValue ACCIn = CurDAG->getNode(MipsISD::InsertLOHI, DL, MVT::Untyped,
338                                   SUBCNode->getOperand(0),
339                                   SUBENode->getOperand(0));
340
341   // create MipsSub(u) node
342   MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
343
344   SDValue MSub = CurDAG->getNode(MultOpc, DL, MVT::Glue,
345                                  MultNode->getOperand(0),// Factor 0
346                                  MultNode->getOperand(1),// Factor 1
347                                  ACCIn);
348
349   // replace uses of sube and subc here
350   if (!SDValue(SUBCNode, 0).use_empty()) {
351     SDValue LoIdx = CurDAG->getConstant(Mips::sub_lo, MVT::i32);
352     SDValue LoOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
353                                     LoIdx);
354     CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), LoOut);
355   }
356   if (!SDValue(SUBENode, 0).use_empty()) {
357     SDValue HiIdx = CurDAG->getConstant(Mips::sub_hi, MVT::i32);
358     SDValue HiOut = CurDAG->getNode(MipsISD::ExtractLOHI, DL, MVT::i32, MSub,
359                                     HiIdx);
360     CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), HiOut);
361   }
362
363   return true;
364 }
365
366 static SDValue performADDECombine(SDNode *N, SelectionDAG &DAG,
367                                   TargetLowering::DAGCombinerInfo &DCI,
368                                   const MipsSubtarget *Subtarget) {
369   if (DCI.isBeforeLegalize())
370     return SDValue();
371
372   if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
373       selectMADD(N, &DAG))
374     return SDValue(N, 0);
375
376   return SDValue();
377 }
378
379 static SDValue performSUBECombine(SDNode *N, SelectionDAG &DAG,
380                                   TargetLowering::DAGCombinerInfo &DCI,
381                                   const MipsSubtarget *Subtarget) {
382   if (DCI.isBeforeLegalize())
383     return SDValue();
384
385   if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
386       selectMSUB(N, &DAG))
387     return SDValue(N, 0);
388
389   return SDValue();
390 }
391
392 static SDValue genConstMult(SDValue X, uint64_t C, SDLoc DL, EVT VT,
393                             EVT ShiftTy, SelectionDAG &DAG) {
394   // Clear the upper (64 - VT.sizeInBits) bits.
395   C &= ((uint64_t)-1) >> (64 - VT.getSizeInBits());
396
397   // Return 0.
398   if (C == 0)
399     return DAG.getConstant(0, VT);
400
401   // Return x.
402   if (C == 1)
403     return X;
404
405   // If c is power of 2, return (shl x, log2(c)).
406   if (isPowerOf2_64(C))
407     return DAG.getNode(ISD::SHL, DL, VT, X,
408                        DAG.getConstant(Log2_64(C), ShiftTy));
409
410   unsigned Log2Ceil = Log2_64_Ceil(C);
411   uint64_t Floor = 1LL << Log2_64(C);
412   uint64_t Ceil = Log2Ceil == 64 ? 0LL : 1LL << Log2Ceil;
413
414   // If |c - floor_c| <= |c - ceil_c|,
415   // where floor_c = pow(2, floor(log2(c))) and ceil_c = pow(2, ceil(log2(c))),
416   // return (add constMult(x, floor_c), constMult(x, c - floor_c)).
417   if (C - Floor <= Ceil - C) {
418     SDValue Op0 = genConstMult(X, Floor, DL, VT, ShiftTy, DAG);
419     SDValue Op1 = genConstMult(X, C - Floor, DL, VT, ShiftTy, DAG);
420     return DAG.getNode(ISD::ADD, DL, VT, Op0, Op1);
421   }
422
423   // If |c - floor_c| > |c - ceil_c|,
424   // return (sub constMult(x, ceil_c), constMult(x, ceil_c - c)).
425   SDValue Op0 = genConstMult(X, Ceil, DL, VT, ShiftTy, DAG);
426   SDValue Op1 = genConstMult(X, Ceil - C, DL, VT, ShiftTy, DAG);
427   return DAG.getNode(ISD::SUB, DL, VT, Op0, Op1);
428 }
429
430 static SDValue performMULCombine(SDNode *N, SelectionDAG &DAG,
431                                  const TargetLowering::DAGCombinerInfo &DCI,
432                                  const MipsSETargetLowering *TL) {
433   EVT VT = N->getValueType(0);
434
435   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
436     if (!VT.isVector())
437       return genConstMult(N->getOperand(0), C->getZExtValue(), SDLoc(N),
438                           VT, TL->getScalarShiftAmountTy(VT), DAG);
439
440   return SDValue(N, 0);
441 }
442
443 static SDValue performDSPShiftCombine(unsigned Opc, SDNode *N, EVT Ty,
444                                       SelectionDAG &DAG,
445                                       const MipsSubtarget *Subtarget) {
446   // See if this is a vector splat immediate node.
447   APInt SplatValue, SplatUndef;
448   unsigned SplatBitSize;
449   bool HasAnyUndefs;
450   unsigned EltSize = Ty.getVectorElementType().getSizeInBits();
451   BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
452
453   if (!BV ||
454       !BV->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
455                            EltSize, !Subtarget->isLittle()) ||
456       (SplatBitSize != EltSize) ||
457       (SplatValue.getZExtValue() >= EltSize))
458     return SDValue();
459
460   return DAG.getNode(Opc, SDLoc(N), Ty, N->getOperand(0),
461                      DAG.getConstant(SplatValue.getZExtValue(), MVT::i32));
462 }
463
464 static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
465                                  TargetLowering::DAGCombinerInfo &DCI,
466                                  const MipsSubtarget *Subtarget) {
467   EVT Ty = N->getValueType(0);
468
469   if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
470     return SDValue();
471
472   return performDSPShiftCombine(MipsISD::SHLL_DSP, N, Ty, DAG, Subtarget);
473 }
474
475 static SDValue performSRACombine(SDNode *N, SelectionDAG &DAG,
476                                  TargetLowering::DAGCombinerInfo &DCI,
477                                  const MipsSubtarget *Subtarget) {
478   EVT Ty = N->getValueType(0);
479
480   if ((Ty != MVT::v2i16) && ((Ty != MVT::v4i8) || !Subtarget->hasDSPR2()))
481     return SDValue();
482
483   return performDSPShiftCombine(MipsISD::SHRA_DSP, N, Ty, DAG, Subtarget);
484 }
485
486
487 static SDValue performSRLCombine(SDNode *N, SelectionDAG &DAG,
488                                  TargetLowering::DAGCombinerInfo &DCI,
489                                  const MipsSubtarget *Subtarget) {
490   EVT Ty = N->getValueType(0);
491
492   if (((Ty != MVT::v2i16) || !Subtarget->hasDSPR2()) && (Ty != MVT::v4i8))
493     return SDValue();
494
495   return performDSPShiftCombine(MipsISD::SHRL_DSP, N, Ty, DAG, Subtarget);
496 }
497
498 static bool isLegalDSPCondCode(EVT Ty, ISD::CondCode CC) {
499   bool IsV216 = (Ty == MVT::v2i16);
500
501   switch (CC) {
502   case ISD::SETEQ:
503   case ISD::SETNE:  return true;
504   case ISD::SETLT:
505   case ISD::SETLE:
506   case ISD::SETGT:
507   case ISD::SETGE:  return IsV216;
508   case ISD::SETULT:
509   case ISD::SETULE:
510   case ISD::SETUGT:
511   case ISD::SETUGE: return !IsV216;
512   default:          return false;
513   }
514 }
515
516 static SDValue performSETCCCombine(SDNode *N, SelectionDAG &DAG) {
517   EVT Ty = N->getValueType(0);
518
519   if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
520     return SDValue();
521
522   if (!isLegalDSPCondCode(Ty, cast<CondCodeSDNode>(N->getOperand(2))->get()))
523     return SDValue();
524
525   return DAG.getNode(MipsISD::SETCC_DSP, SDLoc(N), Ty, N->getOperand(0),
526                      N->getOperand(1), N->getOperand(2));
527 }
528
529 static SDValue performVSELECTCombine(SDNode *N, SelectionDAG &DAG) {
530   EVT Ty = N->getValueType(0);
531
532   if ((Ty != MVT::v2i16) && (Ty != MVT::v4i8))
533     return SDValue();
534
535   SDValue SetCC = N->getOperand(0);
536
537   if (SetCC.getOpcode() != MipsISD::SETCC_DSP)
538     return SDValue();
539
540   return DAG.getNode(MipsISD::SELECT_CC_DSP, SDLoc(N), Ty,
541                      SetCC.getOperand(0), SetCC.getOperand(1), N->getOperand(1),
542                      N->getOperand(2), SetCC.getOperand(2));
543 }
544
545 SDValue
546 MipsSETargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
547   SelectionDAG &DAG = DCI.DAG;
548   SDValue Val;
549
550   switch (N->getOpcode()) {
551   case ISD::ADDE:
552     return performADDECombine(N, DAG, DCI, Subtarget);
553   case ISD::SUBE:
554     return performSUBECombine(N, DAG, DCI, Subtarget);
555   case ISD::MUL:
556     return performMULCombine(N, DAG, DCI, this);
557   case ISD::SHL:
558     return performSHLCombine(N, DAG, DCI, Subtarget);
559   case ISD::SRA:
560     return performSRACombine(N, DAG, DCI, Subtarget);
561   case ISD::SRL:
562     return performSRLCombine(N, DAG, DCI, Subtarget);
563   case ISD::VSELECT:
564     return performVSELECTCombine(N, DAG);
565   case ISD::SETCC: {
566     Val = performSETCCCombine(N, DAG);
567     break;
568   }
569   }
570
571   if (Val.getNode())
572     return Val;
573
574   return MipsTargetLowering::PerformDAGCombine(N, DCI);
575 }
576
577 MachineBasicBlock *
578 MipsSETargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
579                                                   MachineBasicBlock *BB) const {
580   switch (MI->getOpcode()) {
581   default:
582     return MipsTargetLowering::EmitInstrWithCustomInserter(MI, BB);
583   case Mips::BPOSGE32_PSEUDO:
584     return emitBPOSGE32(MI, BB);
585   case Mips::SNZ_B_PSEUDO:
586     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_B);
587   case Mips::SNZ_H_PSEUDO:
588     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_H);
589   case Mips::SNZ_W_PSEUDO:
590     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_W);
591   case Mips::SNZ_D_PSEUDO:
592     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_D);
593   case Mips::SNZ_V_PSEUDO:
594     return emitMSACBranchPseudo(MI, BB, Mips::BNZ_V);
595   case Mips::SZ_B_PSEUDO:
596     return emitMSACBranchPseudo(MI, BB, Mips::BZ_B);
597   case Mips::SZ_H_PSEUDO:
598     return emitMSACBranchPseudo(MI, BB, Mips::BZ_H);
599   case Mips::SZ_W_PSEUDO:
600     return emitMSACBranchPseudo(MI, BB, Mips::BZ_W);
601   case Mips::SZ_D_PSEUDO:
602     return emitMSACBranchPseudo(MI, BB, Mips::BZ_D);
603   case Mips::SZ_V_PSEUDO:
604     return emitMSACBranchPseudo(MI, BB, Mips::BZ_V);
605   }
606 }
607
608 bool MipsSETargetLowering::
609 isEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
610                                   unsigned NextStackOffset,
611                                   const MipsFunctionInfo& FI) const {
612   if (!EnableMipsTailCalls)
613     return false;
614
615   // Return false if either the callee or caller has a byval argument.
616   if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
617     return false;
618
619   // Return true if the callee's argument area is no larger than the
620   // caller's.
621   return NextStackOffset <= FI.getIncomingArgSize();
622 }
623
624 void MipsSETargetLowering::
625 getOpndList(SmallVectorImpl<SDValue> &Ops,
626             std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
627             bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
628             CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
629   // T9 should contain the address of the callee function if
630   // -reloction-model=pic or it is an indirect call.
631   if (IsPICCall || !GlobalOrExternal) {
632     unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
633     RegsToPass.push_front(std::make_pair(T9Reg, Callee));
634   } else
635     Ops.push_back(Callee);
636
637   MipsTargetLowering::getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal,
638                                   InternalLinkage, CLI, Callee, Chain);
639 }
640
641 SDValue MipsSETargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
642   LoadSDNode &Nd = *cast<LoadSDNode>(Op);
643
644   if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
645     return MipsTargetLowering::lowerLOAD(Op, DAG);
646
647   // Replace a double precision load with two i32 loads and a buildpair64.
648   SDLoc DL(Op);
649   SDValue Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
650   EVT PtrVT = Ptr.getValueType();
651
652   // i32 load from lower address.
653   SDValue Lo = DAG.getLoad(MVT::i32, DL, Chain, Ptr,
654                            MachinePointerInfo(), Nd.isVolatile(),
655                            Nd.isNonTemporal(), Nd.isInvariant(),
656                            Nd.getAlignment());
657
658   // i32 load from higher address.
659   Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
660   SDValue Hi = DAG.getLoad(MVT::i32, DL, Lo.getValue(1), Ptr,
661                            MachinePointerInfo(), Nd.isVolatile(),
662                            Nd.isNonTemporal(), Nd.isInvariant(),
663                            std::min(Nd.getAlignment(), 4U));
664
665   if (!Subtarget->isLittle())
666     std::swap(Lo, Hi);
667
668   SDValue BP = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
669   SDValue Ops[2] = {BP, Hi.getValue(1)};
670   return DAG.getMergeValues(Ops, 2, DL);
671 }
672
673 SDValue MipsSETargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
674   StoreSDNode &Nd = *cast<StoreSDNode>(Op);
675
676   if (Nd.getMemoryVT() != MVT::f64 || !NoDPLoadStore)
677     return MipsTargetLowering::lowerSTORE(Op, DAG);
678
679   // Replace a double precision store with two extractelement64s and i32 stores.
680   SDLoc DL(Op);
681   SDValue Val = Nd.getValue(), Ptr = Nd.getBasePtr(), Chain = Nd.getChain();
682   EVT PtrVT = Ptr.getValueType();
683   SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
684                            Val, DAG.getConstant(0, MVT::i32));
685   SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
686                            Val, DAG.getConstant(1, MVT::i32));
687
688   if (!Subtarget->isLittle())
689     std::swap(Lo, Hi);
690
691   // i32 store to lower address.
692   Chain = DAG.getStore(Chain, DL, Lo, Ptr, MachinePointerInfo(),
693                        Nd.isVolatile(), Nd.isNonTemporal(), Nd.getAlignment(),
694                        Nd.getTBAAInfo());
695
696   // i32 store to higher address.
697   Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Ptr, DAG.getConstant(4, PtrVT));
698   return DAG.getStore(Chain, DL, Hi, Ptr, MachinePointerInfo(),
699                       Nd.isVolatile(), Nd.isNonTemporal(),
700                       std::min(Nd.getAlignment(), 4U), Nd.getTBAAInfo());
701 }
702
703 SDValue MipsSETargetLowering::lowerMulDiv(SDValue Op, unsigned NewOpc,
704                                           bool HasLo, bool HasHi,
705                                           SelectionDAG &DAG) const {
706   EVT Ty = Op.getOperand(0).getValueType();
707   SDLoc DL(Op);
708   SDValue Mult = DAG.getNode(NewOpc, DL, MVT::Untyped,
709                              Op.getOperand(0), Op.getOperand(1));
710   SDValue Lo, Hi;
711
712   if (HasLo)
713     Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
714                      DAG.getConstant(Mips::sub_lo, MVT::i32));
715   if (HasHi)
716     Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, Ty, Mult,
717                      DAG.getConstant(Mips::sub_hi, MVT::i32));
718
719   if (!HasLo || !HasHi)
720     return HasLo ? Lo : Hi;
721
722   SDValue Vals[] = { Lo, Hi };
723   return DAG.getMergeValues(Vals, 2, DL);
724 }
725
726
727 static SDValue initAccumulator(SDValue In, SDLoc DL, SelectionDAG &DAG) {
728   SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
729                              DAG.getConstant(0, MVT::i32));
730   SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, In,
731                              DAG.getConstant(1, MVT::i32));
732   return DAG.getNode(MipsISD::InsertLOHI, DL, MVT::Untyped, InLo, InHi);
733 }
734
735 static SDValue extractLOHI(SDValue Op, SDLoc DL, SelectionDAG &DAG) {
736   SDValue Lo = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
737                            DAG.getConstant(Mips::sub_lo, MVT::i32));
738   SDValue Hi = DAG.getNode(MipsISD::ExtractLOHI, DL, MVT::i32, Op,
739                            DAG.getConstant(Mips::sub_hi, MVT::i32));
740   return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
741 }
742
743 // This function expands mips intrinsic nodes which have 64-bit input operands
744 // or output values.
745 //
746 // out64 = intrinsic-node in64
747 // =>
748 // lo = copy (extract-element (in64, 0))
749 // hi = copy (extract-element (in64, 1))
750 // mips-specific-node
751 // v0 = copy lo
752 // v1 = copy hi
753 // out64 = merge-values (v0, v1)
754 //
755 static SDValue lowerDSPIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
756   SDLoc DL(Op);
757   bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
758   SmallVector<SDValue, 3> Ops;
759   unsigned OpNo = 0;
760
761   // See if Op has a chain input.
762   if (HasChainIn)
763     Ops.push_back(Op->getOperand(OpNo++));
764
765   // The next operand is the intrinsic opcode.
766   assert(Op->getOperand(OpNo).getOpcode() == ISD::TargetConstant);
767
768   // See if the next operand has type i64.
769   SDValue Opnd = Op->getOperand(++OpNo), In64;
770
771   if (Opnd.getValueType() == MVT::i64)
772     In64 = initAccumulator(Opnd, DL, DAG);
773   else
774     Ops.push_back(Opnd);
775
776   // Push the remaining operands.
777   for (++OpNo ; OpNo < Op->getNumOperands(); ++OpNo)
778     Ops.push_back(Op->getOperand(OpNo));
779
780   // Add In64 to the end of the list.
781   if (In64.getNode())
782     Ops.push_back(In64);
783
784   // Scan output.
785   SmallVector<EVT, 2> ResTys;
786
787   for (SDNode::value_iterator I = Op->value_begin(), E = Op->value_end();
788        I != E; ++I)
789     ResTys.push_back((*I == MVT::i64) ? MVT::Untyped : *I);
790
791   // Create node.
792   SDValue Val = DAG.getNode(Opc, DL, ResTys, &Ops[0], Ops.size());
793   SDValue Out = (ResTys[0] == MVT::Untyped) ? extractLOHI(Val, DL, DAG) : Val;
794
795   if (!HasChainIn)
796     return Out;
797
798   assert(Val->getValueType(1) == MVT::Other);
799   SDValue Vals[] = { Out, SDValue(Val.getNode(), 1) };
800   return DAG.getMergeValues(Vals, 2, DL);
801 }
802
803 static SDValue lowerMSABinaryIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
804   SDLoc DL(Op);
805   SDValue LHS = Op->getOperand(1);
806   SDValue RHS = Op->getOperand(2);
807   EVT ResTy = Op->getValueType(0);
808
809   SDValue Result = DAG.getNode(Opc, DL, ResTy, LHS, RHS);
810
811   return Result;
812 }
813
814 static SDValue lowerMSABranchIntr(SDValue Op, SelectionDAG &DAG, unsigned Opc) {
815   SDLoc DL(Op);
816   SDValue Value = Op->getOperand(1);
817   EVT ResTy = Op->getValueType(0);
818
819   SDValue Result = DAG.getNode(Opc, DL, ResTy, Value);
820
821   return Result;
822 }
823
824 SDValue MipsSETargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
825                                                       SelectionDAG &DAG) const {
826   switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
827   default:
828     return SDValue();
829   case Intrinsic::mips_shilo:
830     return lowerDSPIntr(Op, DAG, MipsISD::SHILO);
831   case Intrinsic::mips_dpau_h_qbl:
832     return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL);
833   case Intrinsic::mips_dpau_h_qbr:
834     return lowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR);
835   case Intrinsic::mips_dpsu_h_qbl:
836     return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL);
837   case Intrinsic::mips_dpsu_h_qbr:
838     return lowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR);
839   case Intrinsic::mips_dpa_w_ph:
840     return lowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH);
841   case Intrinsic::mips_dps_w_ph:
842     return lowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH);
843   case Intrinsic::mips_dpax_w_ph:
844     return lowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH);
845   case Intrinsic::mips_dpsx_w_ph:
846     return lowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH);
847   case Intrinsic::mips_mulsa_w_ph:
848     return lowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH);
849   case Intrinsic::mips_mult:
850     return lowerDSPIntr(Op, DAG, MipsISD::Mult);
851   case Intrinsic::mips_multu:
852     return lowerDSPIntr(Op, DAG, MipsISD::Multu);
853   case Intrinsic::mips_madd:
854     return lowerDSPIntr(Op, DAG, MipsISD::MAdd);
855   case Intrinsic::mips_maddu:
856     return lowerDSPIntr(Op, DAG, MipsISD::MAddu);
857   case Intrinsic::mips_msub:
858     return lowerDSPIntr(Op, DAG, MipsISD::MSub);
859   case Intrinsic::mips_msubu:
860     return lowerDSPIntr(Op, DAG, MipsISD::MSubu);
861   case Intrinsic::mips_addv_b:
862   case Intrinsic::mips_addv_h:
863   case Intrinsic::mips_addv_w:
864   case Intrinsic::mips_addv_d:
865     return lowerMSABinaryIntr(Op, DAG, ISD::ADD);
866   case Intrinsic::mips_bnz_b:
867   case Intrinsic::mips_bnz_h:
868   case Intrinsic::mips_bnz_w:
869   case Intrinsic::mips_bnz_d:
870     return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_NONZERO);
871   case Intrinsic::mips_bnz_v:
872     return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_NONZERO);
873   case Intrinsic::mips_bz_b:
874   case Intrinsic::mips_bz_h:
875   case Intrinsic::mips_bz_w:
876   case Intrinsic::mips_bz_d:
877     return lowerMSABranchIntr(Op, DAG, MipsISD::VALL_ZERO);
878   case Intrinsic::mips_bz_v:
879     return lowerMSABranchIntr(Op, DAG, MipsISD::VANY_ZERO);
880   }
881 }
882
883 static SDValue lowerMSALoadIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
884   SDLoc DL(Op);
885   SDValue ChainIn = Op->getOperand(0);
886   SDValue Address = Op->getOperand(2);
887   SDValue Offset  = Op->getOperand(3);
888   EVT ResTy = Op->getValueType(0);
889   EVT PtrTy = Address->getValueType(0);
890
891   Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
892
893   return DAG.getLoad(ResTy, DL, ChainIn, Address, MachinePointerInfo(), false,
894                      false, false, 16);
895 }
896
897 SDValue MipsSETargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
898                                                      SelectionDAG &DAG) const {
899   unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
900   switch (Intr) {
901   default:
902     return SDValue();
903   case Intrinsic::mips_extp:
904     return lowerDSPIntr(Op, DAG, MipsISD::EXTP);
905   case Intrinsic::mips_extpdp:
906     return lowerDSPIntr(Op, DAG, MipsISD::EXTPDP);
907   case Intrinsic::mips_extr_w:
908     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_W);
909   case Intrinsic::mips_extr_r_w:
910     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W);
911   case Intrinsic::mips_extr_rs_w:
912     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W);
913   case Intrinsic::mips_extr_s_h:
914     return lowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H);
915   case Intrinsic::mips_mthlip:
916     return lowerDSPIntr(Op, DAG, MipsISD::MTHLIP);
917   case Intrinsic::mips_mulsaq_s_w_ph:
918     return lowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH);
919   case Intrinsic::mips_maq_s_w_phl:
920     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL);
921   case Intrinsic::mips_maq_s_w_phr:
922     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR);
923   case Intrinsic::mips_maq_sa_w_phl:
924     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL);
925   case Intrinsic::mips_maq_sa_w_phr:
926     return lowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR);
927   case Intrinsic::mips_dpaq_s_w_ph:
928     return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH);
929   case Intrinsic::mips_dpsq_s_w_ph:
930     return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH);
931   case Intrinsic::mips_dpaq_sa_l_w:
932     return lowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W);
933   case Intrinsic::mips_dpsq_sa_l_w:
934     return lowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W);
935   case Intrinsic::mips_dpaqx_s_w_ph:
936     return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH);
937   case Intrinsic::mips_dpaqx_sa_w_ph:
938     return lowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH);
939   case Intrinsic::mips_dpsqx_s_w_ph:
940     return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH);
941   case Intrinsic::mips_dpsqx_sa_w_ph:
942     return lowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH);
943   case Intrinsic::mips_ld_b:
944   case Intrinsic::mips_ld_h:
945   case Intrinsic::mips_ld_w:
946   case Intrinsic::mips_ld_d:
947   case Intrinsic::mips_ldx_b:
948   case Intrinsic::mips_ldx_h:
949   case Intrinsic::mips_ldx_w:
950   case Intrinsic::mips_ldx_d:
951    return lowerMSALoadIntr(Op, DAG, Intr);
952   }
953 }
954
955 static SDValue lowerMSAStoreIntr(SDValue Op, SelectionDAG &DAG, unsigned Intr) {
956   SDLoc DL(Op);
957   SDValue ChainIn = Op->getOperand(0);
958   SDValue Value   = Op->getOperand(2);
959   SDValue Address = Op->getOperand(3);
960   SDValue Offset  = Op->getOperand(4);
961   EVT PtrTy = Address->getValueType(0);
962
963   Address = DAG.getNode(ISD::ADD, DL, PtrTy, Address, Offset);
964
965   return DAG.getStore(ChainIn, DL, Value, Address, MachinePointerInfo(), false,
966                       false, 16);
967 }
968
969 SDValue MipsSETargetLowering::lowerINTRINSIC_VOID(SDValue Op,
970                                                   SelectionDAG &DAG) const {
971   unsigned Intr = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
972   switch (Intr) {
973   default:
974     return SDValue();
975   case Intrinsic::mips_st_b:
976   case Intrinsic::mips_st_h:
977   case Intrinsic::mips_st_w:
978   case Intrinsic::mips_st_d:
979   case Intrinsic::mips_stx_b:
980   case Intrinsic::mips_stx_h:
981   case Intrinsic::mips_stx_w:
982   case Intrinsic::mips_stx_d:
983     return lowerMSAStoreIntr(Op, DAG, Intr);
984   }
985 }
986
987 MachineBasicBlock * MipsSETargetLowering::
988 emitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
989   // $bb:
990   //  bposge32_pseudo $vr0
991   //  =>
992   // $bb:
993   //  bposge32 $tbb
994   // $fbb:
995   //  li $vr2, 0
996   //  b $sink
997   // $tbb:
998   //  li $vr1, 1
999   // $sink:
1000   //  $vr0 = phi($vr2, $fbb, $vr1, $tbb)
1001
1002   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1003   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1004   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1005   DebugLoc DL = MI->getDebugLoc();
1006   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1007   MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1008   MachineFunction *F = BB->getParent();
1009   MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1010   MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1011   MachineBasicBlock *Sink  = F->CreateMachineBasicBlock(LLVM_BB);
1012   F->insert(It, FBB);
1013   F->insert(It, TBB);
1014   F->insert(It, Sink);
1015
1016   // Transfer the remainder of BB and its successor edges to Sink.
1017   Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1018                BB->end());
1019   Sink->transferSuccessorsAndUpdatePHIs(BB);
1020
1021   // Add successors.
1022   BB->addSuccessor(FBB);
1023   BB->addSuccessor(TBB);
1024   FBB->addSuccessor(Sink);
1025   TBB->addSuccessor(Sink);
1026
1027   // Insert the real bposge32 instruction to $BB.
1028   BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1029
1030   // Fill $FBB.
1031   unsigned VR2 = RegInfo.createVirtualRegister(RC);
1032   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1033     .addReg(Mips::ZERO).addImm(0);
1034   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1035
1036   // Fill $TBB.
1037   unsigned VR1 = RegInfo.createVirtualRegister(RC);
1038   BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1039     .addReg(Mips::ZERO).addImm(1);
1040
1041   // Insert phi function to $Sink.
1042   BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1043           MI->getOperand(0).getReg())
1044     .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1045
1046   MI->eraseFromParent();   // The pseudo instruction is gone now.
1047   return Sink;
1048 }
1049
1050 MachineBasicBlock * MipsSETargetLowering::
1051 emitMSACBranchPseudo(MachineInstr *MI, MachineBasicBlock *BB,
1052                      unsigned BranchOp) const{
1053   // $bb:
1054   //  vany_nonzero $rd, $ws
1055   //  =>
1056   // $bb:
1057   //  bnz.b $ws, $tbb
1058   //  b $fbb
1059   // $fbb:
1060   //  li $rd1, 0
1061   //  b $sink
1062   // $tbb:
1063   //  li $rd2, 1
1064   // $sink:
1065   //  $rd = phi($rd1, $fbb, $rd2, $tbb)
1066
1067   MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1068   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1069   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
1070   DebugLoc DL = MI->getDebugLoc();
1071   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1072   MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1073   MachineFunction *F = BB->getParent();
1074   MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1075   MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1076   MachineBasicBlock *Sink  = F->CreateMachineBasicBlock(LLVM_BB);
1077   F->insert(It, FBB);
1078   F->insert(It, TBB);
1079   F->insert(It, Sink);
1080
1081   // Transfer the remainder of BB and its successor edges to Sink.
1082   Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1083                BB->end());
1084   Sink->transferSuccessorsAndUpdatePHIs(BB);
1085
1086   // Add successors.
1087   BB->addSuccessor(FBB);
1088   BB->addSuccessor(TBB);
1089   FBB->addSuccessor(Sink);
1090   TBB->addSuccessor(Sink);
1091
1092   // Insert the real bnz.b instruction to $BB.
1093   BuildMI(BB, DL, TII->get(BranchOp))
1094     .addReg(MI->getOperand(1).getReg())
1095     .addMBB(TBB);
1096
1097   // Fill $FBB.
1098   unsigned RD1 = RegInfo.createVirtualRegister(RC);
1099   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), RD1)
1100     .addReg(Mips::ZERO).addImm(0);
1101   BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1102
1103   // Fill $TBB.
1104   unsigned RD2 = RegInfo.createVirtualRegister(RC);
1105   BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), RD2)
1106     .addReg(Mips::ZERO).addImm(1);
1107
1108   // Insert phi function to $Sink.
1109   BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1110           MI->getOperand(0).getReg())
1111     .addReg(RD1).addMBB(FBB).addReg(RD2).addMBB(TBB);
1112
1113   MI->eraseFromParent();   // The pseudo instruction is gone now.
1114   return Sink;
1115 }