LegalizeTypesGeneric.cpp revision 7fb085871857134f8cbeb17499d4ab771ba8da42
178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//===-------- LegalizeTypesGeneric.cpp - Generic type legalization --------===//
278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//
378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//                     The LLVM Compiler Infrastructure
478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//
578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// This file is distributed under the University of Illinois Open Source
678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// License. See LICENSE.TXT for details.
778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//
878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//===----------------------------------------------------------------------===//
978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//
1078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// This file implements generic type expansion and splitting for LegalizeTypes.
1178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// The routines here perform legalization when the details of the type (such as
1278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// whether it is an integer or a float) do not matter.
1378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// Expansion is the act of changing a computation in an illegal type to be a
1478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// computation in two identical registers of a smaller type.
1578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// Splitting is the act of changing a computation in an illegal type to be a
1678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// computation in two not necessarily identical registers of a smaller type.
1778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//
1878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//===----------------------------------------------------------------------===//
1978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
2078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands#include "LegalizeTypes.h"
2147d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands#include "llvm/Target/TargetData.h"
22905ff1ebc4f383088e6af6fc37504cd06ba62b57Dan Gohman#include "llvm/CodeGen/PseudoSourceValue.h"
2378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sandsusing namespace llvm;
2478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
2578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//===----------------------------------------------------------------------===//
2678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// Generic Result Expansion.
2778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//===----------------------------------------------------------------------===//
2878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
2978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// These routines assume that the Lo/Hi part is stored first in memory on
3078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// little/big-endian machines, followed by the Hi/Lo part.  This means that
3178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// they cannot be used as is on vectors, for which Lo is always stored first.
3278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
33475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::ExpandRes_BIT_CONVERT(SDNode *N, SDValue &Lo,
34475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                             SDValue &Hi) {
3547d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  MVT OutVT = N->getValueType(0);
3647d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  MVT NOutVT = TLI.getTypeToTransformTo(OutVT);
37475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue InOp = N->getOperand(0);
3878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  MVT InVT = InOp.getValueType();
3935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
4078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
4178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Handle some special cases efficiently.
4278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  switch (getTypeAction(InVT)) {
4378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    default:
4478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      assert(false && "Unknown type action!");
4578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    case Legal:
4678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    case PromoteInteger:
4778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      break;
4878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    case SoftenFloat:
4978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      // Convert the integer operand instead.
5078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      SplitInteger(GetSoftenedFloat(InOp), Lo, Hi);
5135ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen      Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo);
5235ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen      Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi);
5378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      return;
5478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    case ExpandInteger:
5578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    case ExpandFloat:
5678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      // Convert the expanded pieces of the input.
5778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      GetExpandedOp(InOp, Lo, Hi);
5835ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen      Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo);
5935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen      Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi);
6078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      return;
61f4e4629ee8c218f892ad8ae3e182fe40bc160895Duncan Sands    case SplitVector:
6278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      // Convert the split parts of the input if it was split in two.
6378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      GetSplitVector(InOp, Lo, Hi);
6478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      if (Lo.getValueType() == Hi.getValueType()) {
6578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands        if (TLI.isBigEndian())
6678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands          std::swap(Lo, Hi);
6735ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen        Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo);
6835ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen        Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi);
6978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands        return;
7078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      }
7178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      break;
72f4e4629ee8c218f892ad8ae3e182fe40bc160895Duncan Sands    case ScalarizeVector:
7378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      // Convert the element instead.
7478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      SplitInteger(BitConvertToInteger(GetScalarizedVector(InOp)), Lo, Hi);
7535ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen      Lo = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Lo);
7635ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen      Hi = DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, Hi);
7778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      return;
7878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  }
7978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
8047d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // Lower the bit-convert to a store/load from the stack.
8147d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  assert(NOutVT.isByteSized() && "Expanded type not byte sized!");
8247d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands
8347d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // Create the stack frame object.  Make sure it is aligned for both
8447d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // the source and expanded destination types.
8547d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  unsigned Alignment =
8647d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands    TLI.getTargetData()->getPrefTypeAlignment(NOutVT.getTypeForMVT());
8747d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  SDValue StackPtr = DAG.CreateStackTemporary(InVT, Alignment);
88905ff1ebc4f383088e6af6fc37504cd06ba62b57Dan Gohman  int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
89905ff1ebc4f383088e6af6fc37504cd06ba62b57Dan Gohman  const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
9047d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands
9147d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // Emit a store to the stack slot.
9235ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, InOp, StackPtr, SV, 0);
9347d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands
9447d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // Load the first half from the stack slot.
9535ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Lo = DAG.getLoad(NOutVT, dl, Store, StackPtr, SV, 0);
9647d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands
9747d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // Increment the pointer to the other half.
9847d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  unsigned IncrementSize = NOutVT.getSizeInBits() / 8;
9935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
10047d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands                         DAG.getIntPtrConstant(IncrementSize));
10147d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands
10247d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // Load the second half from the stack slot.
10335ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Hi = DAG.getLoad(NOutVT, dl, Store, StackPtr, SV, IncrementSize, false,
10447d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands                   MinAlign(Alignment, IncrementSize));
10547d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands
10647d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // Handle endianness of the load.
10747d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  if (TLI.isBigEndian())
10847d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands    std::swap(Lo, Hi);
10978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
11078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
111475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::ExpandRes_BUILD_PAIR(SDNode *N, SDValue &Lo,
112475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                            SDValue &Hi) {
11378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Return the operands.
11478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  Lo = N->getOperand(0);
11578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  Hi = N->getOperand(1);
11678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
11778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
118475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::ExpandRes_EXTRACT_ELEMENT(SDNode *N, SDValue &Lo,
119475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                 SDValue &Hi) {
1204a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands  GetExpandedOp(N->getOperand(0), Lo, Hi);
121f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman  SDValue Part = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ?
122f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman                   Hi : Lo;
12335ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
1244a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands
1254a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands  assert(Part.getValueType() == N->getValueType(0) &&
1264a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands         "Type twice as big as expanded type not itself expanded!");
1274a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1284a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands
12935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NVT, Part,
1304a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands                   DAG.getConstant(0, TLI.getPointerTy()));
13135ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NVT, Part,
1324a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands                   DAG.getConstant(1, TLI.getPointerTy()));
1334a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands}
1344a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands
135475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::ExpandRes_EXTRACT_VECTOR_ELT(SDNode *N, SDValue &Lo,
136475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                    SDValue &Hi) {
137475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue OldVec = N->getOperand(0);
13878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  unsigned OldElts = OldVec.getValueType().getVectorNumElements();
13935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
14078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
14178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Convert to a vector of the expanded element type, for example
14278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // <3 x i64> -> <6 x i32>.
14378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  MVT OldVT = N->getValueType(0);
14478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  MVT NewVT = TLI.getTypeToTransformTo(OldVT);
14578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
14635ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, dl,
14778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                                 MVT::getVectorVT(NewVT, 2*OldElts),
14878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                                 OldVec);
14978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
15078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Extract the elements at 2 * Idx and 2 * Idx + 1 from the new vector.
151475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Idx = N->getOperand(1);
15278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
15378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Make sure the type of Idx is big enough to hold the new values.
15478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  if (Idx.getValueType().bitsLT(TLI.getPointerTy()))
15535ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen    Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
15678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
15778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  Idx = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, Idx);
15835ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, NewVec, Idx);
15978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
16035ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Idx = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
16178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                    DAG.getConstant(1, Idx.getValueType()));
16235ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, NewVec, Idx);
16378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
16478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  if (TLI.isBigEndian())
16578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    std::swap(Lo, Hi);
16678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
16778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
168475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::ExpandRes_NormalLoad(SDNode *N, SDValue &Lo,
169475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                            SDValue &Hi) {
170ab09b7e8f34075c1759127a113f41bdf921f4034Duncan Sands  assert(ISD::isNormalLoad(N) && "This routine only for normal loads!");
17135ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
17278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
17378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  LoadSDNode *LD = cast<LoadSDNode>(N);
17478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
175475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Chain = LD->getChain();
176475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ptr = LD->getBasePtr();
17778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  int SVOffset = LD->getSrcValueOffset();
17878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  unsigned Alignment = LD->getAlignment();
17978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  bool isVolatile = LD->isVolatile();
18078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
18178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  assert(NVT.isByteSized() && "Expanded type not byte sized!");
18278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
18335ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Lo = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getSrcValue(), SVOffset,
18478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                   isVolatile, Alignment);
18578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
18678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Increment the pointer to the other half.
18778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  unsigned IncrementSize = NVT.getSizeInBits() / 8;
18835ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
18978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                    DAG.getIntPtrConstant(IncrementSize));
1907fb085871857134f8cbeb17499d4ab771ba8da42Duncan Sands  Hi = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getSrcValue(),
19135ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen                   SVOffset+IncrementSize,
19278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                   isVolatile, MinAlign(Alignment, IncrementSize));
19378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
19478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Build a factor node to remember that this load is independent of the
19578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // other one.
19635ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
19778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                      Hi.getValue(1));
19878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
19978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Handle endianness of the load.
20078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  if (TLI.isBigEndian())
20178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    std::swap(Lo, Hi);
20278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
20378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Modified the chain - switch anything that used the old chain to use
20478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // the new one.
205475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  ReplaceValueWith(SDValue(N, 1), Chain);
20678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
20778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
20821c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sandsvoid DAGTypeLegalizer::ExpandRes_VAARG(SDNode *N, SDValue &Lo, SDValue &Hi) {
20921c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
21021c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  SDValue Chain = N->getOperand(0);
21121c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  SDValue Ptr = N->getOperand(1);
21221c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands
21321c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  Lo = DAG.getVAArg(NVT, Chain, Ptr, N->getOperand(2));
21421c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, N->getOperand(2));
21521c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands
21621c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  // Handle endianness of the load.
21721c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  if (TLI.isBigEndian())
21821c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands    std::swap(Lo, Hi);
21921c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands
22021c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  // Modified the chain - switch anything that used the old chain to use
22121c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  // the new one.
22221c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
22321c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands}
22421c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands
225ae099d54428f4113f8a71c53314975fb8a8e8bbcDuncan Sands
22678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//===--------------------------------------------------------------------===//
22778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// Generic Operand Expansion.
22878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//===--------------------------------------------------------------------===//
22978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
230475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandOp_BIT_CONVERT(SDNode *N) {
23135ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
23278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  if (N->getValueType(0).isVector()) {
23378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    // An illegal expanding type is being converted to a legal vector type.
23478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    // Make a two element vector out of the expanded parts and convert that
23578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    // instead, but only if the new vector type is legal (otherwise there
23678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    // is no point, and it might create expansion loops).  For example, on
23778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    // x86 this turns v1i64 = BIT_CONVERT i64 into v1i64 = BIT_CONVERT v2i32.
23878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    MVT OVT = N->getOperand(0).getValueType();
23978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    MVT NVT = MVT::getVectorVT(TLI.getTypeToTransformTo(OVT), 2);
24078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
24178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    if (isTypeLegal(NVT)) {
242475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue Parts[2];
24378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      GetExpandedOp(N->getOperand(0), Parts[0], Parts[1]);
24478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
24578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      if (TLI.isBigEndian())
24678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands        std::swap(Parts[0], Parts[1]);
24778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
24835ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen      SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, Parts, 2);
24935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen      return DAG.getNode(ISD::BIT_CONVERT, dl, N->getValueType(0), Vec);
25078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    }
25178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  }
25278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
25378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Otherwise, store to a temporary and load out again as the new type.
25478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
25578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
25678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
257475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandOp_BUILD_VECTOR(SDNode *N) {
25878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // The vector type is legal but the element type needs expansion.
25978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  MVT VecVT = N->getValueType(0);
26078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  unsigned NumElts = VecVT.getVectorNumElements();
26178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  MVT OldVT = N->getOperand(0).getValueType();
26278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  MVT NewVT = TLI.getTypeToTransformTo(OldVT);
26335ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
26478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
26578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Build a vector of twice the length out of the expanded elements.
26678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // For example <3 x i64> -> <6 x i32>.
267475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  std::vector<SDValue> NewElts;
26878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  NewElts.reserve(NumElts*2);
26978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
27078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  for (unsigned i = 0; i < NumElts; ++i) {
271475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Lo, Hi;
27278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    GetExpandedOp(N->getOperand(i), Lo, Hi);
27378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    if (TLI.isBigEndian())
27478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands      std::swap(Lo, Hi);
27578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    NewElts.push_back(Lo);
27678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    NewElts.push_back(Hi);
27778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  }
27878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
27935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, dl,
28078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                                 MVT::getVectorVT(NewVT, NewElts.size()),
28178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                                 &NewElts[0], NewElts.size());
28278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
28378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Convert the new vector to the old vector type.
28435ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  return DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, NewVec);
28578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
28678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
287475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandOp_EXTRACT_ELEMENT(SDNode *N) {
288475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Lo, Hi;
28978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  GetExpandedOp(N->getOperand(0), Lo, Hi);
290f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman  return cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() ? Hi : Lo;
29178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
29278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
293d17c0302763cfd0b3f6657d2493147552762ac07Mon P WangSDValue DAGTypeLegalizer::ExpandOp_INSERT_VECTOR_ELT(SDNode *N) {
294d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  // The vector type is legal but the element type needs expansion.
295d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  MVT VecVT = N->getValueType(0);
296d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  unsigned NumElts = VecVT.getVectorNumElements();
29735ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
298d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang
299d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  SDValue Val = N->getOperand(1);
300d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  MVT OldEVT = Val.getValueType();
301d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  MVT NewEVT = TLI.getTypeToTransformTo(OldEVT);
302d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang
303d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  assert(OldEVT == VecVT.getVectorElementType() &&
304d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang         "Inserted element type doesn't match vector element type!");
305d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang
306d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  // Bitconvert to a vector of twice the length with elements of the expanded
307d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  // type, insert the expanded vector elements, and then convert back.
308d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  MVT NewVecVT = MVT::getVectorVT(NewEVT, NumElts*2);
3097fb085871857134f8cbeb17499d4ab771ba8da42Duncan Sands  SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, dl,
31035ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen                               NewVecVT, N->getOperand(0));
311d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang
312d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  SDValue Lo, Hi;
313d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  GetExpandedOp(Val, Lo, Hi);
314d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  if (TLI.isBigEndian())
315d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang    std::swap(Lo, Hi);
316d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang
317d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  SDValue Idx = N->getOperand(2);
31835ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Idx = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, Idx);
31935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, NewVec, Lo, Idx);
32035ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Idx = DAG.getNode(ISD::ADD, dl,
32135ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen                    Idx.getValueType(), Idx, DAG.getIntPtrConstant(1));
32235ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  NewVec =  DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NewVecVT, NewVec, Hi, Idx);
323d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang
324d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  // Convert the new vector to the old vector type.
32535ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  return DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, NewVec);
326d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang}
327d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang
328d17c0302763cfd0b3f6657d2493147552762ac07Mon P WangSDValue DAGTypeLegalizer::ExpandOp_SCALAR_TO_VECTOR(SDNode *N) {
32935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
330d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  MVT VT = N->getValueType(0);
331d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  unsigned NumElts = VT.getVectorNumElements();
332d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  SmallVector<SDValue, 16> Ops(NumElts);
333d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  Ops[0] = N->getOperand(0);
33435ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  SDValue UndefVal = DAG.getNode(ISD::UNDEF, dl, Ops[0].getValueType());
335d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang  for (unsigned i = 1; i < NumElts; ++i)
336d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang    Ops[i] = UndefVal;
33735ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElts);
338d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang}
339d17c0302763cfd0b3f6657d2493147552762ac07Mon P Wang
340475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandOp_NormalStore(SDNode *N, unsigned OpNo) {
341ab09b7e8f34075c1759127a113f41bdf921f4034Duncan Sands  assert(ISD::isNormalStore(N) && "This routine only for normal stores!");
34278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  assert(OpNo == 1 && "Can only expand the stored value so far");
34335ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
34478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
34578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  StoreSDNode *St = cast<StoreSDNode>(N);
34678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  MVT NVT = TLI.getTypeToTransformTo(St->getValue().getValueType());
347475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Chain = St->getChain();
348475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ptr = St->getBasePtr();
34978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  int SVOffset = St->getSrcValueOffset();
35078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  unsigned Alignment = St->getAlignment();
35178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  bool isVolatile = St->isVolatile();
35278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
35378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  assert(NVT.isByteSized() && "Expanded type not byte sized!");
35478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  unsigned IncrementSize = NVT.getSizeInBits() / 8;
35578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
356475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Lo, Hi;
35778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  GetExpandedOp(St->getValue(), Lo, Hi);
35878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
35978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  if (TLI.isBigEndian())
36078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    std::swap(Lo, Hi);
36178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
36235ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Lo = DAG.getStore(Chain, dl, Lo, Ptr, St->getSrcValue(), SVOffset,
36378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                    isVolatile, Alignment);
36478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
36535ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
36678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                    DAG.getIntPtrConstant(IncrementSize));
36778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
3687fb085871857134f8cbeb17499d4ab771ba8da42Duncan Sands  Hi = DAG.getStore(Chain, dl, Hi, Ptr, St->getSrcValue(),
36935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen                    SVOffset + IncrementSize,
37078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                    isVolatile, MinAlign(Alignment, IncrementSize));
37178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
37235ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
37378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
37478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
37578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
37678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//===--------------------------------------------------------------------===//
37778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// Generic Result Splitting.
37878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands//===--------------------------------------------------------------------===//
37978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
38078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// Be careful to make no assumptions about which of Lo/Hi is stored first in
38178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// memory (for vectors it is always Lo first followed by Hi in the following
38278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// bytes; for integers and floats it is Lo first if and only if the machine is
38378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// little-endian).
38478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
38578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sandsvoid DAGTypeLegalizer::SplitRes_MERGE_VALUES(SDNode *N,
386475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                             SDValue &Lo, SDValue &Hi) {
38778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // A MERGE_VALUES node can produce any number of values.  We know that the
38878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // first illegal one needs to be expanded into Lo/Hi.
38978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  unsigned i;
39078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
391cff50d9e20d7bbc3acf4845fe826bfb3095126c4Duncan Sands  // The string of legal results gets turned into input operands, which have
39278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // the same type.
39378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  for (i = 0; isTypeLegal(N->getValueType(i)); ++i)
394475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
39578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
39678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // The first illegal result must be the one that needs to be expanded.
39778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  GetSplitOp(N->getOperand(i), Lo, Hi);
39878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
39978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // Legalize the rest of the results into the input operands whether they are
40078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  // legal or not.
40178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  unsigned e = N->getNumValues();
40278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  for (++i; i != e; ++i)
403475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    ReplaceValueWith(SDValue(N, i), SDValue(N->getOperand(i)));
40478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
40578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
406475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::SplitRes_SELECT(SDNode *N, SDValue &Lo,
407475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                       SDValue &Hi) {
408475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LL, LH, RL, RH;
40935ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
41078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  GetSplitOp(N->getOperand(1), LL, LH);
41178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  GetSplitOp(N->getOperand(2), RL, RH);
41278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
413475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Cond = N->getOperand(0);
41435ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Lo = DAG.getNode(ISD::SELECT, dl, LL.getValueType(), Cond, LL, RL);
41535ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Hi = DAG.getNode(ISD::SELECT, dl, LH.getValueType(), Cond, LH, RH);
41678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
41778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
418475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::SplitRes_SELECT_CC(SDNode *N, SDValue &Lo,
419475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                          SDValue &Hi) {
420475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LL, LH, RL, RH;
42135ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
42278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  GetSplitOp(N->getOperand(2), LL, LH);
42378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  GetSplitOp(N->getOperand(3), RL, RH);
42478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
42535ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Lo = DAG.getNode(ISD::SELECT_CC, dl, LL.getValueType(), N->getOperand(0),
42678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                   N->getOperand(1), LL, RL, N->getOperand(4));
42735ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Hi = DAG.getNode(ISD::SELECT_CC, dl, LH.getValueType(), N->getOperand(0),
42878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands                   N->getOperand(1), LH, RH, N->getOperand(4));
42978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
43078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
431475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDValue &Lo, SDValue &Hi) {
43278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  MVT LoVT, HiVT;
43335ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  DebugLoc dl = N->getDebugLoc();
43478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
43535ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Lo = DAG.getNode(ISD::UNDEF, dl, LoVT);
43635ba3d463834f83e2bf8e8ad631ffc4d73a0203cDale Johannesen  Hi = DAG.getNode(ISD::UNDEF, dl, HiVT);
43778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands}
438