LegalizeIntegerTypes.cpp revision 7d696d80409aad20bb5da0fc4eccab941dd371d4
169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands//===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===//
2cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//
3cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//                     The LLVM Compiler Infrastructure
4cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//
8cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//===----------------------------------------------------------------------===//
9cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//
1069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands// This file implements integer type expansion and promotion for LegalizeTypes.
1169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands// Promotion is the act of changing a computation in an illegal type into a
1269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands// computation in a larger type.  For example, implementing i8 arithmetic in an
1369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands// i32 register (often needed on powerpc).
1469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands// Expansion is the act of changing a computation in an illegal type into a
1578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands// computation in two identical registers of a smaller type.  For example,
1669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands// implementing i64 arithmetic in two i32 registers (often needed on 32-bit
1769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands// targets).
18cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//
19cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//===----------------------------------------------------------------------===//
20cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
21cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner#include "LegalizeTypes.h"
22c82bf9b268eb63f7cf6f435d9ea222ddb8e3c5a8Mon P Wang#include "llvm/CodeGen/PseudoSourceValue.h"
237d696d80409aad20bb5da0fc4eccab941dd371d4Torok Edwin#include "llvm/Support/ErrorHandling.h"
24cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattnerusing namespace llvm;
25cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
26cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//===----------------------------------------------------------------------===//
2769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands//  Integer Result Promotion
28cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//===----------------------------------------------------------------------===//
29cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
3069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands/// PromoteIntegerResult - This method is called when a result of a node is
3169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands/// found to be in need of promotion to a larger type.  At this point, the node
3269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands/// may also have invalid operands or may have other results that need
3369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands/// expansion, we just know that (at least) one result needs promotion.
3469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sandsvoid DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
3569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  DEBUG(cerr << "Promote integer result: "; N->dump(&DAG); cerr << "\n");
369fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  SDValue Res = SDValue();
3769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
38126d90770bdb17e6925b2fe26de99aa079b7b9b3Duncan Sands  // See if the target wants to custom expand this node.
39f43071beddb7ed5b2fd7d2f06c4130460616a13dDuncan Sands  if (CustomLowerNode(N, N->getValueType(ResNo), true))
401607f05cb7d77d01ce521a30232faa389dbed4e2Duncan Sands    return;
41126d90770bdb17e6925b2fe26de99aa079b7b9b3Duncan Sands
4269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  switch (N->getOpcode()) {
4369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  default:
4469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands#ifndef NDEBUG
4569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    cerr << "PromoteIntegerResult #" << ResNo << ": ";
4669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    N->dump(&DAG); cerr << "\n";
4769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands#endif
487d696d80409aad20bb5da0fc4eccab941dd371d4Torok Edwin    LLVM_UNREACHABLE("Do not know how to promote this operator!");
499fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::AssertSext:  Res = PromoteIntRes_AssertSext(N); break;
509fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::AssertZext:  Res = PromoteIntRes_AssertZext(N); break;
519fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::BIT_CONVERT: Res = PromoteIntRes_BIT_CONVERT(N); break;
529fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::BSWAP:       Res = PromoteIntRes_BSWAP(N); break;
539fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::BUILD_PAIR:  Res = PromoteIntRes_BUILD_PAIR(N); break;
549fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::Constant:    Res = PromoteIntRes_Constant(N); break;
5500ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang  case ISD::CONVERT_RNDSAT:
569fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands                         Res = PromoteIntRes_CONVERT_RNDSAT(N); break;
579fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::CTLZ:        Res = PromoteIntRes_CTLZ(N); break;
589fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::CTPOP:       Res = PromoteIntRes_CTPOP(N); break;
599fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::CTTZ:        Res = PromoteIntRes_CTTZ(N); break;
60bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  case ISD::EXTRACT_VECTOR_ELT:
619fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands                         Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
629fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::LOAD:        Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break;
639fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::SELECT:      Res = PromoteIntRes_SELECT(N); break;
649fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::SELECT_CC:   Res = PromoteIntRes_SELECT_CC(N); break;
659fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::SETCC:       Res = PromoteIntRes_SETCC(N); break;
669fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::SHL:         Res = PromoteIntRes_SHL(N); break;
678d56a6f4d8b010d4c582225a08ece971613f6fe3Duncan Sands  case ISD::SIGN_EXTEND_INREG:
689fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands                         Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
699fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::SRA:         Res = PromoteIntRes_SRA(N); break;
709fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::SRL:         Res = PromoteIntRes_SRL(N); break;
719fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::TRUNCATE:    Res = PromoteIntRes_TRUNCATE(N); break;
729fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::UNDEF:       Res = PromoteIntRes_UNDEF(N); break;
739fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::VAARG:       Res = PromoteIntRes_VAARG(N); break;
74bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
7569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SIGN_EXTEND:
7669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::ZERO_EXTEND:
779fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::ANY_EXTEND:  Res = PromoteIntRes_INT_EXTEND(N); break;
78bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
7969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::FP_TO_SINT:
809fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::FP_TO_UINT:  Res = PromoteIntRes_FP_TO_XINT(N); break;
8169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
8269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::AND:
8369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::OR:
8469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::XOR:
8569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::ADD:
8669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SUB:
879fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::MUL:         Res = PromoteIntRes_SimpleIntBinOp(N); break;
8869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
8969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SDIV:
909fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::SREM:        Res = PromoteIntRes_SDIV(N); break;
9169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
9269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::UDIV:
939fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::UREM:        Res = PromoteIntRes_UDIV(N); break;
948ac0d4b4fb10406278cd600214cd3ee6d76620cdBill Wendling
95253174bf50c932abaa680f465e2888c0e5272267Bill Wendling  case ISD::SADDO:
969fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::SSUBO:       Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
9774c376529101acbe141a256d0bf23a44eb454c84Bill Wendling  case ISD::UADDO:
989fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::USUBO:       Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
9974c376529101acbe141a256d0bf23a44eb454c84Bill Wendling  case ISD::SMULO:
1009fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  case ISD::UMULO:       Res = PromoteIntRes_XMULO(N, ResNo); break;
101b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands
1020b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_LOAD_ADD:
1030b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_LOAD_SUB:
1040b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_LOAD_AND:
1050b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_LOAD_OR:
1060b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_LOAD_XOR:
1070b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_LOAD_NAND:
1080b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_LOAD_MIN:
1090b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_LOAD_MAX:
1100b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_LOAD_UMIN:
1110b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_LOAD_UMAX:
1120b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_SWAP:
1139fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands    Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
114b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands
1150b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman  case ISD::ATOMIC_CMP_SWAP:
1169fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands    Res = PromoteIntRes_Atomic2(cast<AtomicSDNode>(N)); break;
11769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  }
11869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
1199fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  // If the result is null then the sub-method took care of registering it.
1209fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  if (Res.getNode())
1219fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands    SetPromotedInteger(SDValue(N, ResNo), Res);
12269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
12369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
124475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
12595db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  // Sign-extend the new bits, and continue the assertion.
126c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue Op = SExtPromotedInteger(N->getOperand(0));
127786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::AssertSext, N->getDebugLoc(),
128786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     Op.getValueType(), Op, N->getOperand(1));
12995db39a9de48f69f4d764335b492b83a698c7854Duncan Sands}
13095db39a9de48f69f4d764335b492b83a698c7854Duncan Sands
131475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
13295db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  // Zero the new bits, and continue the assertion.
133c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue Op = ZExtPromotedInteger(N->getOperand(0));
134786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::AssertZext, N->getDebugLoc(),
135786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     Op.getValueType(), Op, N->getOperand(1));
13695db39a9de48f69f4d764335b492b83a698c7854Duncan Sands}
13795db39a9de48f69f4d764335b492b83a698c7854Duncan Sands
138b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan SandsSDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
139b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  SDValue Op2 = GetPromotedInteger(N->getOperand(2));
1407fb085871857134f8cbeb17499d4ab771ba8da42Duncan Sands  SDValue Res = DAG.getAtomic(N->getOpcode(), N->getDebugLoc(),
141786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                              N->getMemoryVT(),
1420b1d4a798d1dd2f39521b6b381cd1c1911c9ab52Dan Gohman                              N->getChain(), N->getBasePtr(),
143b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands                              Op2, N->getSrcValue(), N->getAlignment());
144b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  // Legalized the chain result - switch anything that used the old chain to
145b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  // use the new one.
146b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
147b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  return Res;
148b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands}
149b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands
150b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan SandsSDValue DAGTypeLegalizer::PromoteIntRes_Atomic2(AtomicSDNode *N) {
151b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  SDValue Op2 = GetPromotedInteger(N->getOperand(2));
152b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  SDValue Op3 = GetPromotedInteger(N->getOperand(3));
153fdc40a0a696c658d550d894ea03772e5f8af2c94Scott Michel  SDValue Res = DAG.getAtomic(N->getOpcode(), N->getDebugLoc(),
154f8d3ec2c5725a2010f11de4ba78f6127712a5fe7Dale Johannesen                              N->getMemoryVT(), N->getChain(), N->getBasePtr(),
155b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands                              Op2, Op3, N->getSrcValue(), N->getAlignment());
156b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  // Legalized the chain result - switch anything that used the old chain to
157b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  // use the new one.
158b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
159b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands  return Res;
160b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands}
161b5f68e241f9eb19e5694131df830acbfce20a6ebDuncan Sands
162475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_BIT_CONVERT(SDNode *N) {
163475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue InOp = N->getOperand(0);
16469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  MVT InVT = InOp.getValueType();
16569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  MVT NInVT = TLI.getTypeToTransformTo(InVT);
16647d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  MVT OutVT = N->getValueType(0);
16747d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  MVT NOutVT = TLI.getTypeToTransformTo(OutVT);
168786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
16969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
17069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  switch (getTypeAction(InVT)) {
17169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  default:
17269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    assert(false && "Unknown type action!");
17369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    break;
17469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case Legal:
17569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    break;
17669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case PromoteInteger:
17747d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands    if (NOutVT.bitsEq(NInVT))
17869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands      // The input promotes to the same size.  Convert the promoted value.
1797fb085871857134f8cbeb17499d4ab771ba8da42Duncan Sands      return DAG.getNode(ISD::BIT_CONVERT, dl,
180786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                         NOutVT, GetPromotedInteger(InOp));
18169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    break;
1824fc4fd657d4266059dac3849133a3a351b03d99dDuncan Sands  case SoftenFloat:
18369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    // Promote the integer operand by hand.
184786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
18569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ExpandInteger:
18669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ExpandFloat:
18769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    break;
188f4e4629ee8c218f892ad8ae3e182fe40bc160895Duncan Sands  case ScalarizeVector:
18969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    // Convert the element to an integer and promote it by hand.
190786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
19169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands                       BitConvertToInteger(GetScalarizedVector(InOp)));
19287c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang  case SplitVector: {
19369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    // For example, i32 = BIT_CONVERT v2i16 on alpha.  Convert the split
19469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    // pieces of the input into integers and reassemble in the final type.
195475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Lo, Hi;
19669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    GetSplitVector(N->getOperand(0), Lo, Hi);
19769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    Lo = BitConvertToInteger(Lo);
19869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    Hi = BitConvertToInteger(Hi);
19969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
20069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    if (TLI.isBigEndian())
20169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands      std::swap(Lo, Hi);
20269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
203786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
20447d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands                       MVT::getIntegerVT(NOutVT.getSizeInBits()),
20569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands                       JoinIntegers(Lo, Hi));
206786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    return DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, InOp);
20769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  }
20887c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang  case WidenVector:
20987c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang    if (OutVT.bitsEq(NInVT))
21087c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang      // The input is widened to the same size.  Convert to the widened value.
211786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      return DAG.getNode(ISD::BIT_CONVERT, dl, OutVT, GetWidenedVector(InOp));
21287c8a8f304d1ee72829086ce2c41a8fa3813ba6aMon P Wang  }
21369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
214aad3460086a1b29c55f7490c6d8743ea4e53f07dEli Friedman  return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
215aad3460086a1b29c55f7490c6d8743ea4e53f07dEli Friedman                     CreateStackStoreLoad(InOp, OutVT));
21669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
21769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
218475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
219475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = GetPromotedInteger(N->getOperand(0));
220c07e6e53f757da1a7c79c66ed53f2844de85a77eDuncan Sands  MVT OVT = N->getValueType(0);
221c07e6e53f757da1a7c79c66ed53f2844de85a77eDuncan Sands  MVT NVT = Op.getValueType();
222786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
223c07e6e53f757da1a7c79c66ed53f2844de85a77eDuncan Sands
224c07e6e53f757da1a7c79c66ed53f2844de85a77eDuncan Sands  unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
225786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
22692abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                     DAG.getConstant(DiffBits, TLI.getPointerTy()));
227c07e6e53f757da1a7c79c66ed53f2844de85a77eDuncan Sands}
228c07e6e53f757da1a7c79c66ed53f2844de85a77eDuncan Sands
229475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
230bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // The pair element type may be legal, or may not promote to the same type as
231bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // the result, for example i14 = BUILD_PAIR (i7, i7).  Handle all cases.
232786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(),
233bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                     TLI.getTypeToTransformTo(N->getValueType(0)),
234bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                     JoinIntegers(N->getOperand(0), N->getOperand(1)));
23569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
23669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
237475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
23869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  MVT VT = N->getValueType(0);
239b300d2aa3ef08b5074449e2c05804717f488f4e4Dale Johannesen  // FIXME there is no actual debug info here
240b300d2aa3ef08b5074449e2c05804717f488f4e4Dale Johannesen  DebugLoc dl = N->getDebugLoc();
241bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // Zero extend things like i1, sign extend everything else.  It shouldn't
242bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // matter in theory which one we pick, but this tends to give better code?
243bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
244b300d2aa3ef08b5074449e2c05804717f488f4e4Dale Johannesen  SDValue Result = DAG.getNode(Opc, dl, TLI.getTypeToTransformTo(VT),
245a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands                               SDValue(N, 0));
246bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
247bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  return Result;
24869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
24969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
25000ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P WangSDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
25100ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang  ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
25200ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang  assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
25300ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang           CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
25400ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang           CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
25500ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang          "can only promote integers");
25600ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang  MVT OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
257c460ae90019ddb19d4c07b2cd2fbaecfa7adf67dDale Johannesen  return DAG.getConvertRndSat(OutVT, N->getDebugLoc(), N->getOperand(0),
25800ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang                              N->getOperand(1), N->getOperand(2),
25900ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang                              N->getOperand(3), N->getOperand(4), CvtCode);
26000ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang}
26100ec49b6bafc33ee17d97ec1c723e1edb41d4c97Mon P Wang
262475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
263c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  // Zero extend to the promoted type and do the count there.
264c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue Op = ZExtPromotedInteger(N->getOperand(0));
265b300d2aa3ef08b5074449e2c05804717f488f4e4Dale Johannesen  DebugLoc dl = N->getDebugLoc();
26669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  MVT OVT = N->getValueType(0);
26769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  MVT NVT = Op.getValueType();
268b300d2aa3ef08b5074449e2c05804717f488f4e4Dale Johannesen  Op = DAG.getNode(ISD::CTLZ, dl, NVT, Op);
26969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  // Subtract off the extra leading bits in the bigger type.
270b300d2aa3ef08b5074449e2c05804717f488f4e4Dale Johannesen  return DAG.getNode(ISD::SUB, dl, NVT, Op,
27169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands                     DAG.getConstant(NVT.getSizeInBits() -
27269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands                                     OVT.getSizeInBits(), NVT));
27369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
27469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
275475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
27669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  // Zero extend to the promoted type and do the count there.
277c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue Op = ZExtPromotedInteger(N->getOperand(0));
278786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), Op.getValueType(), Op);
27969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
28069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
281475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
282475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = GetPromotedInteger(N->getOperand(0));
28369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  MVT OVT = N->getValueType(0);
28469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  MVT NVT = Op.getValueType();
285786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
28669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  // The count is the same in the promoted type except if the original
28769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  // value was zero.  This can be handled by setting the bit just off
28869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  // the top of the original type.
28903dc093a2e63d20984c8fd67809fa762f1e31f1aDuncan Sands  APInt TopBit(NVT.getSizeInBits(), 0);
29003dc093a2e63d20984c8fd67809fa762f1e31f1aDuncan Sands  TopBit.set(OVT.getSizeInBits());
291786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, NVT));
292786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::CTTZ, dl, NVT, Op);
29369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
29469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
295475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
296786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
297c680ac90032bf455b2bba77de538fccea08eb267Eli Friedman  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
298c680ac90032bf455b2bba77de538fccea08eb267Eli Friedman  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, N->getOperand(0),
299c680ac90032bf455b2bba77de538fccea08eb267Eli Friedman                     N->getOperand(1));
30069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
30169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
302475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
303bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
304a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands  unsigned NewOpc = N->getOpcode();
305786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
306d3ca9fc9984d036146886a40ad3f73aaf7a424ebDuncan Sands
3074c9369df57a52cec5e1fc735e61a979766288074Dale Johannesen  // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
3084c9369df57a52cec5e1fc735e61a979766288074Dale Johannesen  // not Legal, check to see if we can use FP_TO_SINT instead.  (If both UINT
3094c9369df57a52cec5e1fc735e61a979766288074Dale Johannesen  // and SINT conversions are Custom, there is no way to tell which is preferable.
3108c899ee031481dbece5f111379a274c848cb5902Duncan Sands  // We choose SINT because that's the right thing on PPC.)
311a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands  if (N->getOpcode() == ISD::FP_TO_UINT &&
3124c9369df57a52cec5e1fc735e61a979766288074Dale Johannesen      !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
313f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman      TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
314a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands    NewOpc = ISD::FP_TO_SINT;
315a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands
316786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
317a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands
318a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands  // Assert that the converted value fits in the original type.  If it doesn't
319a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands  // (eg: because the value being converted is too big), then the result of the
320a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands  // original operation was undefined anyway, so the assert is still correct.
321a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands  return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
322786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     ISD::AssertZext : ISD::AssertSext, dl,
323a29c13086a3add78a3a79f744573fe09eaa9dc88Duncan Sands                     NVT, Res, DAG.getValueType(N->getValueType(0)));
324d3ca9fc9984d036146886a40ad3f73aaf7a424ebDuncan Sands}
325d3ca9fc9984d036146886a40ad3f73aaf7a424ebDuncan Sands
326475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
327bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
328786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
329be1ad4de2900451626c8d4ace07b9ea16099ea1dDuncan Sands
330bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  if (getTypeAction(N->getOperand(0).getValueType()) == PromoteInteger) {
331475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Res = GetPromotedInteger(N->getOperand(0));
3326959b2bb6521baca57e5507ca039e51002d4a971Duncan Sands    assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
333126d90770bdb17e6925b2fe26de99aa079b7b9b3Duncan Sands
334bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands    // If the result and operand types are the same after promotion, simplify
335bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands    // to an in-register extension.
336bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands    if (NVT == Res.getValueType()) {
337bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands      // The high bits are not guaranteed to be anything.  Insert an extend.
338bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands      if (N->getOpcode() == ISD::SIGN_EXTEND)
339786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen        return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
340bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                           DAG.getValueType(N->getOperand(0).getValueType()));
341bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands      if (N->getOpcode() == ISD::ZERO_EXTEND)
3424be0bdf7c1162824927dd3de89e016ae4934d0d6Dale Johannesen        return DAG.getZeroExtendInReg(Res, dl, N->getOperand(0).getValueType());
343bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands      assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
344bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands      return Res;
345126d90770bdb17e6925b2fe26de99aa079b7b9b3Duncan Sands    }
34669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  }
34769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
348bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // Otherwise, just extend the original operand all the way to the larger type.
349786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
350bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
35169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
352475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
353bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
354bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
355bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  ISD::LoadExtType ExtType =
356bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands    ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
357786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
358786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
35947d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands                               N->getSrcValue(), N->getSrcValueOffset(),
36047d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands                               N->getMemoryVT(), N->isVolatile(),
36147d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands                               N->getAlignment());
36269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
363bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // Legalized the chain result - switch anything that used the old chain to
364bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // use the new one.
365475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
366bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  return Res;
36769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
36869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
369874ae251c317788391f9c3f113957802d390a063Dale Johannesen/// Promote the overflow flag of an overflowing arithmetic node.
370ab0c578bfd1380326830180a9209df6c5be58887Duncan SandsSDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
371ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Simply change the return type of the boolean result.
372ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(1));
373ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  MVT ValueVTs[] = { N->getValueType(0), NVT };
374ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
375786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue Res = DAG.getNode(N->getOpcode(), N->getDebugLoc(),
376786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                            DAG.getVTList(ValueVTs, 2), Ops, 2);
377ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
378ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Modified the sum result - switch anything that used the old sum to use
379ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // the new one.
380ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  ReplaceValueWith(SDValue(N, 0), Res);
381ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
382ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  return SDValue(Res.getNode(), 1);
383ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands}
384ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
385ab0c578bfd1380326830180a9209df6c5be58887Duncan SandsSDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
386ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  if (ResNo == 1)
387ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands    return PromoteIntRes_Overflow(N);
388ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
389ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // The operation overflowed iff the result in the larger type is not the
390ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // sign extension of its truncation to the original type.
391ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  SDValue LHS = SExtPromotedInteger(N->getOperand(0));
392ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  SDValue RHS = SExtPromotedInteger(N->getOperand(1));
393ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  MVT OVT = N->getOperand(0).getValueType();
394ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  MVT NVT = LHS.getValueType();
395786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
396ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
397ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Do the arithmetic in the larger type.
398ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
399786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
400ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
401ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Calculate the overflow flag: sign extend the arithmetic result from
402ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // the original type.
403786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
404ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands                            DAG.getValueType(OVT));
405ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Overflowed if and only if this is not equal to Res.
406786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
407ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
408ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Use the calculated overflow everywhere.
409ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  ReplaceValueWith(SDValue(N, 1), Ofl);
410ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
411ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  return Res;
412ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands}
413ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
414475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
415bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // Sign extend the input.
416c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue LHS = SExtPromotedInteger(N->getOperand(0));
417c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue RHS = SExtPromotedInteger(N->getOperand(1));
418786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
419786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     LHS.getValueType(), LHS, RHS);
42069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
42169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
422475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
423475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LHS = GetPromotedInteger(N->getOperand(1));
424475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue RHS = GetPromotedInteger(N->getOperand(2));
425786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
426786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     LHS.getValueType(), N->getOperand(0),LHS,RHS);
42769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
42869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
429475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
430475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LHS = GetPromotedInteger(N->getOperand(2));
431475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue RHS = GetPromotedInteger(N->getOperand(3));
432786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
433786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     LHS.getValueType(), N->getOperand(0),
434bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                     N->getOperand(1), LHS, RHS, N->getOperand(4));
43569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
43669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
437475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
4385480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands  MVT SVT = TLI.getSetCCResultType(N->getOperand(0).getValueType());
4397e4982287591945c4e42ba8470a978e629789c76Duncan Sands  assert(isTypeLegal(SVT) && "Illegal SetCC type!");
440786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
4417e4982287591945c4e42ba8470a978e629789c76Duncan Sands
4427e4982287591945c4e42ba8470a978e629789c76Duncan Sands  // Get the SETCC result using the canonical SETCC type.
443786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue SetCC = DAG.getNode(ISD::SETCC, dl, SVT, N->getOperand(0),
4447e4982287591945c4e42ba8470a978e629789c76Duncan Sands                              N->getOperand(1), N->getOperand(2));
4457e4982287591945c4e42ba8470a978e629789c76Duncan Sands
4467e4982287591945c4e42ba8470a978e629789c76Duncan Sands  // Convert to the expected type.
4477e4982287591945c4e42ba8470a978e629789c76Duncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
4486959b2bb6521baca57e5507ca039e51002d4a971Duncan Sands  assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
449786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::TRUNCATE, dl, NVT, SetCC);
45069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
45169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
452475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
453786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::SHL, N->getDebugLoc(),
454786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     TLI.getTypeToTransformTo(N->getValueType(0)),
455bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                     GetPromotedInteger(N->getOperand(0)), N->getOperand(1));
45669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
45769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
458475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
459475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = GetPromotedInteger(N->getOperand(0));
460786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(),
461786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     Op.getValueType(), Op, N->getOperand(1));
4628d56a6f4d8b010d4c582225a08ece971613f6fe3Duncan Sands}
4638d56a6f4d8b010d4c582225a08ece971613f6fe3Duncan Sands
464475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
465bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // The input may have strange things in the top bits of the registers, but
466bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // these operations don't care.  They may have weird bits going out, but
467bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // that too is okay if they are integer operations.
468475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LHS = GetPromotedInteger(N->getOperand(0));
469475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue RHS = GetPromotedInteger(N->getOperand(1));
470786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
471786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                    LHS.getValueType(), LHS, RHS);
47269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
47369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
474475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
475bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // The input value must be properly sign extended.
476c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue Res = SExtPromotedInteger(N->getOperand(0));
477786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::SRA, N->getDebugLoc(),
478786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     Res.getValueType(), Res, N->getOperand(1));
479bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
48069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
481475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
482bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // The input value must be properly zero extended.
483bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  MVT VT = N->getValueType(0);
484bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(VT);
485475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Res = ZExtPromotedInteger(N->getOperand(0));
486786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::SRL, N->getDebugLoc(), NVT, Res, N->getOperand(1));
48769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
48869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
489475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
4906959b2bb6521baca57e5507ca039e51002d4a971Duncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
491475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Res;
49269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
493bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  switch (getTypeAction(N->getOperand(0).getValueType())) {
494bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  default: assert(0 && "Unknown type action!");
495bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  case Legal:
496bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  case ExpandInteger:
497bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands    Res = N->getOperand(0);
498bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands    break;
499bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  case PromoteInteger:
500bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands    Res = GetPromotedInteger(N->getOperand(0));
501bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands    break;
502bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  }
503bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
504bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // Truncate to NVT instead of VT
505786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Res);
50669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
50769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
508ab0c578bfd1380326830180a9209df6c5be58887Duncan SandsSDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
509ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  if (ResNo == 1)
510ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands    return PromoteIntRes_Overflow(N);
511ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
512ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // The operation overflowed iff the result in the larger type is not the
513ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // zero extension of its truncation to the original type.
514c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
515c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
516ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  MVT OVT = N->getOperand(0).getValueType();
517ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  MVT NVT = LHS.getValueType();
518786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
51969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
520ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Do the arithmetic in the larger type.
521ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
522786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
5238ac0d4b4fb10406278cd600214cd3ee6d76620cdBill Wendling
524ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Calculate the overflow flag: zero extend the arithmetic result from
525ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // the original type.
5264be0bdf7c1162824927dd3de89e016ae4934d0d6Dale Johannesen  SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
527ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Overflowed if and only if this is not equal to Res.
528786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
5298ac0d4b4fb10406278cd600214cd3ee6d76620cdBill Wendling
530ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Use the calculated overflow everywhere.
531ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  ReplaceValueWith(SDValue(N, 1), Ofl);
5328ac0d4b4fb10406278cd600214cd3ee6d76620cdBill Wendling
533ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  return Res;
534ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands}
535ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands
536ab0c578bfd1380326830180a9209df6c5be58887Duncan SandsSDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
537ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  // Zero extend the input.
538ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
539ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
540786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
541786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     LHS.getValueType(), LHS, RHS);
5428ac0d4b4fb10406278cd600214cd3ee6d76620cdBill Wendling}
5438ac0d4b4fb10406278cd600214cd3ee6d76620cdBill Wendling
544475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
545e8d7230f480654cdb8ff1c3d0a38e1e9ab0bd55fDale Johannesen  return DAG.getUNDEF(TLI.getTypeToTransformTo(N->getValueType(0)));
54669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
54769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
548475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
549475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Chain = N->getOperand(0); // Get the chain.
550475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ptr = N->getOperand(1); // Get the pointer.
551bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  MVT VT = N->getValueType(0);
552786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
55369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
554d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  MVT RegVT = TLI.getRegisterType(VT);
555d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  unsigned NumRegs = TLI.getNumRegisters(VT);
556d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  // The argument is passed as NumRegs registers of type RegVT.
557d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands
558d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  SmallVector<SDValue, 8> Parts(NumRegs);
559d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  for (unsigned i = 0; i < NumRegs; ++i) {
560c460ae90019ddb19d4c07b2cd2fbaecfa7adf67dDale Johannesen    Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2));
561d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands    Chain = Parts[i].getValue(1);
562d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  }
56369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
564d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  // Handle endianness of the load.
565d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  if (TLI.isBigEndian())
566d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands    std::reverse(Parts.begin(), Parts.end());
56769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
568d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  // Assemble the parts in the promoted type.
569d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
570786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
571d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  for (unsigned i = 1; i < NumRegs; ++i) {
572786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
573d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands    // Shift it to the right position and "or" it in.
574786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
575d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands                       DAG.getConstant(i * RegVT.getSizeInBits(),
57692abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                                       TLI.getPointerTy()));
577786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
578d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  }
57969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
580d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  // Modified the chain result - switch anything that used the old chain to
581bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // use the new one.
582d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  ReplaceValueWith(SDValue(N, 1), Chain);
583d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands
584d821726a91000c1b2de7ca8aafdc27b1a311741bDuncan Sands  return Res;
58569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
58669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
587ab0c578bfd1380326830180a9209df6c5be58887Duncan SandsSDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
588ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  assert(ResNo == 1 && "Only boolean result promotion currently supported!");
589ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands  return PromoteIntRes_Overflow(N);
590ab0c578bfd1380326830180a9209df6c5be58887Duncan Sands}
5917fc8ab81f5f46dcba0f76b1c546a1d11ccbebe26Duncan Sands
592bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands//===----------------------------------------------------------------------===//
593bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands//  Integer Operand Promotion
594bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands//===----------------------------------------------------------------------===//
5957fc8ab81f5f46dcba0f76b1c546a1d11ccbebe26Duncan Sands
596bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands/// PromoteIntegerOperand - This method is called when the specified operand of
597bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands/// the specified node is found to need promotion.  At this point, all of the
598bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands/// result types of the node are known to be legal, but other operands of the
599bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands/// node may need promotion or expansion as well as the specified one.
600bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sandsbool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
601bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  DEBUG(cerr << "Promote integer operand: "; N->dump(&DAG); cerr << "\n");
602475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Res = SDValue();
6037fc8ab81f5f46dcba0f76b1c546a1d11ccbebe26Duncan Sands
604f43071beddb7ed5b2fd7d2f06c4130460616a13dDuncan Sands  if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
605bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta    return false;
60669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
607bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  switch (N->getOpcode()) {
608bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta    default:
609bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  #ifndef NDEBUG
610bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta    cerr << "PromoteIntegerOperand Op #" << OpNo << ": ";
611bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta    N->dump(&DAG); cerr << "\n";
612bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  #endif
6137d696d80409aad20bb5da0fc4eccab941dd371d4Torok Edwin    LLVM_UNREACHABLE("Do not know how to promote this operator's operand!");
614bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
615bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::ANY_EXTEND:   Res = PromoteIntOp_ANY_EXTEND(N); break;
61627759f41ca1c930e2860275c9ba2567a5890d7d2Eli Friedman  case ISD::BIT_CONVERT:  Res = PromoteIntOp_BIT_CONVERT(N); break;
617bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::BR_CC:        Res = PromoteIntOp_BR_CC(N, OpNo); break;
618bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::BRCOND:       Res = PromoteIntOp_BRCOND(N, OpNo); break;
619bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::BUILD_PAIR:   Res = PromoteIntOp_BUILD_PAIR(N); break;
620bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
621bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::CONVERT_RNDSAT:
622bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta                          Res = PromoteIntOp_CONVERT_RNDSAT(N); break;
623bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::INSERT_VECTOR_ELT:
624bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta                          Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
625bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::MEMBARRIER:   Res = PromoteIntOp_MEMBARRIER(N); break;
626aa9df0b0c3cef33514095bde2eedead986677955Mon P Wang  case ISD::SCALAR_TO_VECTOR:
627aa9df0b0c3cef33514095bde2eedead986677955Mon P Wang                          Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
628bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::SELECT:       Res = PromoteIntOp_SELECT(N, OpNo); break;
629bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::SELECT_CC:    Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
630bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::SETCC:        Res = PromoteIntOp_SETCC(N, OpNo); break;
631bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::SIGN_EXTEND:  Res = PromoteIntOp_SIGN_EXTEND(N); break;
632bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::SINT_TO_FP:   Res = PromoteIntOp_SINT_TO_FP(N); break;
633bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::STORE:        Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
634bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta                                                   OpNo); break;
635bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::TRUNCATE:     Res = PromoteIntOp_TRUNCATE(N); break;
636bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::UINT_TO_FP:   Res = PromoteIntOp_UINT_TO_FP(N); break;
637bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::ZERO_EXTEND:  Res = PromoteIntOp_ZERO_EXTEND(N); break;
63855467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta
63955467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  case ISD::SHL:
64055467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  case ISD::SRA:
64155467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  case ISD::SRL:
64255467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  case ISD::ROTL:
64355467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
644bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  }
6459fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands
646bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // If the result is null, the sub-method took care of registering results etc.
6479fbc7e2e7a765298fb4326885b407e0962f7ab62Duncan Sands  if (!Res.getNode()) return false;
64847d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands
64947d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // If the result is N, the sub-method updated N in place.  Tell the legalizer
65047d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // core about this.
65147d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  if (Res.getNode() == N)
652bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands    return true;
653bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
654bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
655bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands         "Invalid operand expansion");
656bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
657475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  ReplaceValueWith(SDValue(N, 0), Res);
658bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  return false;
65969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
66069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
66169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands/// PromoteSetCCOperands - Promote the operands of a comparison.  This code is
66269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands/// shared among BR_CC, SELECT_CC, and SETCC handlers.
663475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
66469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands                                            ISD::CondCode CCCode) {
665cff50d9e20d7bbc3acf4845fe826bfb3095126c4Duncan Sands  // We have to insert explicit sign or zero extends.  Note that we could
666cff50d9e20d7bbc3acf4845fe826bfb3095126c4Duncan Sands  // insert sign extends for ALL conditions, but zero extend is cheaper on
667cff50d9e20d7bbc3acf4845fe826bfb3095126c4Duncan Sands  // many machines (an AND instead of two shifts), so prefer it.
66869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  switch (CCCode) {
66969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  default: assert(0 && "Unknown integer comparison!");
67069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SETEQ:
67169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SETNE:
67269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SETUGE:
67369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SETUGT:
67469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SETULE:
67569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SETULT:
67669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    // ALL of these operations will work if we either sign or zero extend
67769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    // the operands (including the unsigned comparisons!).  Zero extend is
67869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    // usually a simpler/cheaper operation, so prefer it.
679c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands    NewLHS = ZExtPromotedInteger(NewLHS);
680c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands    NewRHS = ZExtPromotedInteger(NewRHS);
68111ac797f5ed142f11aafde3dd76c28a73d84282eDuncan Sands    break;
68269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SETGE:
68369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SETGT:
68469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SETLT:
68569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::SETLE:
686c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands    NewLHS = SExtPromotedInteger(NewLHS);
687c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands    NewRHS = SExtPromotedInteger(NewRHS);
68811ac797f5ed142f11aafde3dd76c28a73d84282eDuncan Sands    break;
68969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  }
69069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
69169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
692475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
693475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = GetPromotedInteger(N->getOperand(0));
694786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), N->getValueType(0), Op);
695bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
69669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
69727759f41ca1c930e2860275c9ba2567a5890d7d2Eli FriedmanSDValue DAGTypeLegalizer::PromoteIntOp_BIT_CONVERT(SDNode *N) {
69827759f41ca1c930e2860275c9ba2567a5890d7d2Eli Friedman  // This should only occur in unusual situations like bitcasting to an
69927759f41ca1c930e2860275c9ba2567a5890d7d2Eli Friedman  // x86_fp80, so just turn it into a store+load
70027759f41ca1c930e2860275c9ba2567a5890d7d2Eli Friedman  return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
70127759f41ca1c930e2860275c9ba2567a5890d7d2Eli Friedman}
70227759f41ca1c930e2860275c9ba2567a5890d7d2Eli Friedman
703475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
704bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  assert(OpNo == 2 && "Don't know how to promote this operand!");
70569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
706475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LHS = N->getOperand(2);
707475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue RHS = N->getOperand(3);
708bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
70969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
710bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
711bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // legal types.
712475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
713bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                                N->getOperand(1), LHS, RHS, N->getOperand(4));
714bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
715bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
716475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
717bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  assert(OpNo == 1 && "only know how to promote condition");
718bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
719b6e223a9e806921183da972253c49082a2e07944Duncan Sands  // Promote all the way up to the canonical SetCC type.
7205480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands  MVT SVT = TLI.getSetCCResultType(MVT::Other);
721b6e223a9e806921183da972253c49082a2e07944Duncan Sands  SDValue Cond = PromoteTargetBoolean(N->getOperand(1), SVT);
722bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
723bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // The chain (Op#0) and basic block destination (Op#2) are always legal types.
724475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Cond,
725bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                                N->getOperand(2));
726bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
727bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
728475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
729bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // Since the result type is legal, the operands must promote to it.
730bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  MVT OVT = N->getOperand(0).getValueType();
731c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
732475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Hi = GetPromotedInteger(N->getOperand(1));
733bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
734786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
735bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
736786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
73792abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                   DAG.getConstant(OVT.getSizeInBits(), TLI.getPointerTy()));
738786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
73969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
74069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
741475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
74269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  // The vector type is legal but the element type is not.  This implies
74369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  // that the vector is a power-of-two in length and that the element
74469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  // type does not have a strange size (eg: it is not i1).
74569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  MVT VecVT = N->getValueType(0);
74669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  unsigned NumElts = VecVT.getVectorNumElements();
74769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  assert(!(NumElts & 1) && "Legal vector of one illegal element?");
74869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
749b1303d05a89972195de023fda432cc621375a27cBob Wilson  // Promote the inserted value.  The type does not need to match the
750b1303d05a89972195de023fda432cc621375a27cBob Wilson  // vector element type.  Check that any extra bits introduced will be
751b1303d05a89972195de023fda432cc621375a27cBob Wilson  // truncated away.
752b1303d05a89972195de023fda432cc621375a27cBob Wilson  assert(N->getOperand(0).getValueType().getSizeInBits() >=
753b1303d05a89972195de023fda432cc621375a27cBob Wilson         N->getValueType(0).getVectorElementType().getSizeInBits() &&
754b1303d05a89972195de023fda432cc621375a27cBob Wilson         "Type of inserted value narrower than vector element type!");
75569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
756b1303d05a89972195de023fda432cc621375a27cBob Wilson  SmallVector<SDValue, 16> NewOps;
757c885165e664f3b465403e1b6ce57ba63f57c5f0cBob Wilson  for (unsigned i = 0; i < NumElts; ++i)
758b1303d05a89972195de023fda432cc621375a27cBob Wilson    NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
75969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
760b1303d05a89972195de023fda432cc621375a27cBob Wilson  return DAG.UpdateNodeOperands(SDValue(N, 0), &NewOps[0], NumElts);
76169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
76269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
76328088d3c049017a131aa7b07201c6e19c0227cefMon P WangSDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
76428088d3c049017a131aa7b07201c6e19c0227cefMon P Wang  ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
76528088d3c049017a131aa7b07201c6e19c0227cefMon P Wang  assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
76628088d3c049017a131aa7b07201c6e19c0227cefMon P Wang           CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
76728088d3c049017a131aa7b07201c6e19c0227cefMon P Wang           CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
76828088d3c049017a131aa7b07201c6e19c0227cefMon P Wang           "can only promote integer arguments");
76928088d3c049017a131aa7b07201c6e19c0227cefMon P Wang  SDValue InOp = GetPromotedInteger(N->getOperand(0));
770c460ae90019ddb19d4c07b2cd2fbaecfa7adf67dDale Johannesen  return DAG.getConvertRndSat(N->getValueType(0), N->getDebugLoc(), InOp,
77128088d3c049017a131aa7b07201c6e19c0227cefMon P Wang                              N->getOperand(1), N->getOperand(2),
77228088d3c049017a131aa7b07201c6e19c0227cefMon P Wang                              N->getOperand(3), N->getOperand(4), CvtCode);
77328088d3c049017a131aa7b07201c6e19c0227cefMon P Wang}
77428088d3c049017a131aa7b07201c6e19c0227cefMon P Wang
775475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
776475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                         unsigned OpNo) {
77769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  if (OpNo == 1) {
77869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    // Promote the inserted value.  This is valid because the type does not
77969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    // have to match the vector element type.
78069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
78169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    // Check that any extra bits introduced will be truncated away.
78269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    assert(N->getOperand(1).getValueType().getSizeInBits() >=
78369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands           N->getValueType(0).getVectorElementType().getSizeInBits() &&
78469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands           "Type of inserted value narrower than vector element type!");
785475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
78669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands                                  GetPromotedInteger(N->getOperand(1)),
78769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands                                  N->getOperand(2));
78869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  }
78969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
79069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  assert(OpNo == 2 && "Different operand and result vector types?");
79169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
79269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  // Promote the index.
793c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  SDValue Idx = ZExtPromotedInteger(N->getOperand(2));
794475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
79569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands                                N->getOperand(1), Idx);
79669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
79769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
798475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_MEMBARRIER(SDNode *N) {
799475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue NewOps[6];
8004be0bdf7c1162824927dd3de89e016ae4934d0d6Dale Johannesen  DebugLoc dl = N->getDebugLoc();
80169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  NewOps[0] = N->getOperand(0);
80269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
803475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Flag = GetPromotedInteger(N->getOperand(i));
8044be0bdf7c1162824927dd3de89e016ae4934d0d6Dale Johannesen    NewOps[i] = DAG.getZeroExtendInReg(Flag, dl, MVT::i1);
80569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  }
806475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return DAG.UpdateNodeOperands(SDValue (N, 0), NewOps,
80769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands                                array_lengthof(NewOps));
80869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands}
80969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
810aa9df0b0c3cef33514095bde2eedead986677955Mon P WangSDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
811b10b5ac8d9da43ca2db61401a20af6b676c98438Duncan Sands  // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
812b10b5ac8d9da43ca2db61401a20af6b676c98438Duncan Sands  // the operand in place.
813b10b5ac8d9da43ca2db61401a20af6b676c98438Duncan Sands  return DAG.UpdateNodeOperands(SDValue(N, 0),
814b10b5ac8d9da43ca2db61401a20af6b676c98438Duncan Sands                                GetPromotedInteger(N->getOperand(0)));
815aa9df0b0c3cef33514095bde2eedead986677955Mon P Wang}
816aa9df0b0c3cef33514095bde2eedead986677955Mon P Wang
817475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
818bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  assert(OpNo == 0 && "Only know how to promote condition");
8197e4982287591945c4e42ba8470a978e629789c76Duncan Sands
820b6e223a9e806921183da972253c49082a2e07944Duncan Sands  // Promote all the way up to the canonical SetCC type.
8215480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands  MVT SVT = TLI.getSetCCResultType(N->getOperand(1).getValueType());
822b6e223a9e806921183da972253c49082a2e07944Duncan Sands  SDValue Cond = PromoteTargetBoolean(N->getOperand(0), SVT);
823bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
8247e4982287591945c4e42ba8470a978e629789c76Duncan Sands  return DAG.UpdateNodeOperands(SDValue(N, 0), Cond,
8257e4982287591945c4e42ba8470a978e629789c76Duncan Sands                                N->getOperand(1), N->getOperand(2));
826bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
827bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
828475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
829bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  assert(OpNo == 0 && "Don't know how to promote this operand!");
830bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
831475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LHS = N->getOperand(0);
832475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue RHS = N->getOperand(1);
833bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
834bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
835bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // The CC (#4) and the possible return values (#2 and #3) have legal types.
836475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2),
837bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                                N->getOperand(3), N->getOperand(4));
838bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
839bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
840475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
841bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  assert(OpNo == 0 && "Don't know how to promote this operand!");
842bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
843475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LHS = N->getOperand(0);
844475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue RHS = N->getOperand(1);
845bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
846bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
847bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // The CC (#2) is always legal.
848475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2));
849bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
850bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
85155467af31620c9d027e071ebcd9746b7593cff17Sanjiv GuptaSDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
85255467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
85355467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta                                ZExtPromotedInteger(N->getOperand(1)));
85455467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta}
85555467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta
856475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
857475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = GetPromotedInteger(N->getOperand(0));
858786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
859786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
860786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
861bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                     Op, DAG.getValueType(N->getOperand(0).getValueType()));
862bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
863bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
864c08468774b65dc288c44076d428f4beddabe58e2Duncan SandsSDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
865c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  return DAG.UpdateNodeOperands(SDValue(N, 0),
866c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands                                SExtPromotedInteger(N->getOperand(0)));
867c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands}
868c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands
869475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
870bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
871475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
872bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  int SVOffset = N->getSrcValueOffset();
873bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  unsigned Alignment = N->getAlignment();
874bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  bool isVolatile = N->isVolatile();
875786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
876bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
877475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Val = GetPromotedInteger(N->getValue());  // Get promoted value.
878bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
879bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands  // Truncate the value and store the result.
880786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getTruncStore(Ch, dl, Val, Ptr, N->getSrcValue(),
881bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                           SVOffset, N->getMemoryVT(),
882bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands                           isVolatile, Alignment);
883bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
884bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
885475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
886475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = GetPromotedInteger(N->getOperand(0));
887786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), N->getValueType(0), Op);
888bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
889bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
890c08468774b65dc288c44076d428f4beddabe58e2Duncan SandsSDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
891c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands  return DAG.UpdateNodeOperands(SDValue(N, 0),
892c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands                                ZExtPromotedInteger(N->getOperand(0)));
893c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands}
894c08468774b65dc288c44076d428f4beddabe58e2Duncan Sands
895475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
8964be0bdf7c1162824927dd3de89e016ae4934d0d6Dale Johannesen  DebugLoc dl = N->getDebugLoc();
897475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = GetPromotedInteger(N->getOperand(0));
8984be0bdf7c1162824927dd3de89e016ae4934d0d6Dale Johannesen  Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
8994be0bdf7c1162824927dd3de89e016ae4934d0d6Dale Johannesen  return DAG.getZeroExtendInReg(Op, dl, N->getOperand(0).getValueType());
900bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands}
901bf304c20651b80309af4c0fb3a14c0d73eaa984fDuncan Sands
90269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
90369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands//===----------------------------------------------------------------------===//
90469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands//  Integer Result Expansion
90569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands//===----------------------------------------------------------------------===//
90669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
90769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands/// ExpandIntegerResult - This method is called when the specified result of the
908cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner/// specified node is found to need expansion.  At this point, the node may also
909cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner/// have invalid operands or may have other results that need promotion, we just
910cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner/// know that (at least) one result needs expansion.
91169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sandsvoid DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
91269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  DEBUG(cerr << "Expand integer result: "; N->dump(&DAG); cerr << "\n");
913475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Lo, Hi;
914475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  Lo = Hi = SDValue();
915cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
916cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // See if the target wants to custom expand this node.
917f43071beddb7ed5b2fd7d2f06c4130460616a13dDuncan Sands  if (CustomLowerNode(N, N->getValueType(ResNo), true))
9181607f05cb7d77d01ce521a30232faa389dbed4e2Duncan Sands    return;
919cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
920cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  switch (N->getOpcode()) {
921cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  default:
922cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner#ifndef NDEBUG
92369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    cerr << "ExpandIntegerResult #" << ResNo << ": ";
924cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    N->dump(&DAG); cerr << "\n";
925cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner#endif
9267d696d80409aad20bb5da0fc4eccab941dd371d4Torok Edwin    LLVM_UNREACHABLE("Do not know how to expand the result of this operator!");
92769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
92878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
92978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
93078cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
93178cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
93278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
93378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  case ISD::BIT_CONVERT:        ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
93478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
9354a307ecce68f90e0eebf1ded52b947816cdc2304Duncan Sands  case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
93678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
93721c2972f7d24680f6475877a3398b7f8cf515b33Duncan Sands  case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
93878cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
93969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::ANY_EXTEND:  ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
94095db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  case ISD::AssertSext:  ExpandIntRes_AssertSext(N, Lo, Hi); break;
94169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::AssertZext:  ExpandIntRes_AssertZext(N, Lo, Hi); break;
94205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::BSWAP:       ExpandIntRes_BSWAP(N, Lo, Hi); break;
94305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::Constant:    ExpandIntRes_Constant(N, Lo, Hi); break;
94405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::CTLZ:        ExpandIntRes_CTLZ(N, Lo, Hi); break;
94505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::CTPOP:       ExpandIntRes_CTPOP(N, Lo, Hi); break;
94605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::CTTZ:        ExpandIntRes_CTTZ(N, Lo, Hi); break;
94769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::FP_TO_SINT:  ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
94869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::FP_TO_UINT:  ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
94969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  case ISD::LOAD:        ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
95005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::MUL:         ExpandIntRes_MUL(N, Lo, Hi); break;
95105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::SDIV:        ExpandIntRes_SDIV(N, Lo, Hi); break;
95205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
95305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
95405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::SREM:        ExpandIntRes_SREM(N, Lo, Hi); break;
95505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::TRUNCATE:    ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
95605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::UDIV:        ExpandIntRes_UDIV(N, Lo, Hi); break;
95705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::UREM:        ExpandIntRes_UREM(N, Lo, Hi); break;
95805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
95969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
960cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::AND:
961cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::OR:
96205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
96305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
964cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::ADD:
96505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
96605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
967cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::ADDC:
96805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
96905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
970cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::ADDE:
97105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
97205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
973cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::SHL:
974cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::SRA:
97505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
976cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
977d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands
978cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // If Lo/Hi is null, the sub-method took care of registering results etc.
979ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (Lo.getNode())
980475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
981cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner}
982cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
98305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands/// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
98405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands/// and the shift amount is a constant 'Amt'.  Expand the operation.
98505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
986475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                             SDValue &Lo, SDValue &Hi) {
987786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
98805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // Expand the incoming operand to be shifted, so that we have its parts
989475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue InL, InH;
99005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), InL, InH);
991cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
99205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT NVT = InL.getValueType();
99305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  unsigned VTBits = N->getValueType(0).getSizeInBits();
99405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  unsigned NVTBits = NVT.getSizeInBits();
99505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT ShTy = N->getOperand(1).getValueType();
996cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
99705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (N->getOpcode() == ISD::SHL) {
99805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    if (Amt > VTBits) {
99905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Lo = Hi = DAG.getConstant(0, NVT);
100005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    } else if (Amt > NVTBits) {
100105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Lo = DAG.getConstant(0, NVT);
10027fb085871857134f8cbeb17499d4ab771ba8da42Duncan Sands      Hi = DAG.getNode(ISD::SHL, dl,
1003786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                       NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
100405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    } else if (Amt == NVTBits) {
100505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Lo = DAG.getConstant(0, NVT);
100605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Hi = InL;
1007104de6cf7b80ec5e9beb502a069f376810a0a1e3Richard Osborne    } else if (Amt == 1 &&
1008f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman               TLI.isOperationLegalOrCustom(ISD::ADDC,
1009f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman                                            TLI.getTypeToExpandTo(NVT))) {
101005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      // Emit this X << 1 as X+X.
1011874ae251c317788391f9c3f113957802d390a063Dale Johannesen      SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1012475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue LoOps[2] = { InL, InL };
1013786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
1014475871a144eb604ddaf37503397ba0941442e5fbDan Gohman      SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
1015786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
101605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    } else {
1017786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, DAG.getConstant(Amt, ShTy));
1018786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::OR, dl, NVT,
1019786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                       DAG.getNode(ISD::SHL, dl, NVT, InH,
102005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                   DAG.getConstant(Amt, ShTy)),
1021786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                       DAG.getNode(ISD::SRL, dl, NVT, InL,
102205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                   DAG.getConstant(NVTBits-Amt, ShTy)));
102305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    }
102405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    return;
1025cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
1026cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
102705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (N->getOpcode() == ISD::SRL) {
102805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    if (Amt > VTBits) {
102905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Lo = DAG.getConstant(0, NVT);
103005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Hi = DAG.getConstant(0, NVT);
103105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    } else if (Amt > NVTBits) {
10327fb085871857134f8cbeb17499d4ab771ba8da42Duncan Sands      Lo = DAG.getNode(ISD::SRL, dl,
1033786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                       NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
103405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Hi = DAG.getConstant(0, NVT);
103505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    } else if (Amt == NVTBits) {
103605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Lo = InH;
103705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Hi = DAG.getConstant(0, NVT);
103805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    } else {
1039786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::OR, dl, NVT,
1040786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                       DAG.getNode(ISD::SRL, dl, NVT, InL,
104105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                   DAG.getConstant(Amt, ShTy)),
1042786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                       DAG.getNode(ISD::SHL, dl, NVT, InH,
104305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                   DAG.getConstant(NVTBits-Amt, ShTy)));
1044786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, DAG.getConstant(Amt, ShTy));
104505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    }
104605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    return;
1047cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
1048d885dbdf9eb7a51ebb9a15a85921f27d8219997cDuncan Sands
104905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
105005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (Amt > VTBits) {
1051786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
105205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                          DAG.getConstant(NVTBits-1, ShTy));
105305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  } else if (Amt > NVTBits) {
1054786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
105505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                     DAG.getConstant(Amt-NVTBits, ShTy));
1056786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
105705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                     DAG.getConstant(NVTBits-1, ShTy));
105805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  } else if (Amt == NVTBits) {
105905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    Lo = InH;
1060786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
106105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                     DAG.getConstant(NVTBits-1, ShTy));
1062d885dbdf9eb7a51ebb9a15a85921f27d8219997cDuncan Sands  } else {
1063de06470330260f5937e7ca558f5f5b3e171f2ee5Dale Johannesen    Lo = DAG.getNode(ISD::OR, dl, NVT,
1064786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     DAG.getNode(ISD::SRL, dl, NVT, InL,
106505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                 DAG.getConstant(Amt, ShTy)),
1066786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                     DAG.getNode(ISD::SHL, dl, NVT, InH,
106705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                 DAG.getConstant(NVTBits-Amt, ShTy)));
1068786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, DAG.getConstant(Amt, ShTy));
1069d885dbdf9eb7a51ebb9a15a85921f27d8219997cDuncan Sands  }
1070d885dbdf9eb7a51ebb9a15a85921f27d8219997cDuncan Sands}
1071d885dbdf9eb7a51ebb9a15a85921f27d8219997cDuncan Sands
107205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands/// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
107305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands/// this shift based on knowledge of the high bit of the shift amount.  If we
107405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands/// can tell this, we know that it is >= 32 or < 32, without knowing the actual
107505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands/// shift amount.
107605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsbool DAGTypeLegalizer::
1077475871a144eb604ddaf37503397ba0941442e5fbDan GohmanExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1078475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Amt = N->getOperand(1);
107983ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
108005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT ShTy = Amt.getValueType();
108105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  unsigned ShBits = ShTy.getSizeInBits();
108205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  unsigned NVTBits = NVT.getSizeInBits();
108305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  assert(isPowerOf2_32(NVTBits) &&
108405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands         "Expanded integer type size not a power of two!");
1085786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
1086cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
108705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
108805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  APInt KnownZero, KnownOne;
108905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1090cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
109105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // If we don't know anything about the high bits, exit.
109205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (((KnownZero|KnownOne) & HighBitMask) == 0)
109305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    return false;
1094cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
109505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // Get the incoming operand to be shifted.
1096475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue InL, InH;
109705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), InL, InH);
1098be1ad4de2900451626c8d4ace07b9ea16099ea1dDuncan Sands
109905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // If we know that any of the high bits of the shift amount are one, then we
110005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // can do this as a couple of simple shifts.
110105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (KnownOne.intersects(HighBitMask)) {
110205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // Mask out the high bit, which we know is set.
1103786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
110405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                      DAG.getConstant(~HighBitMask, ShTy));
110505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
110605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    switch (N->getOpcode()) {
110705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    default: assert(0 && "Unknown shift");
110805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    case ISD::SHL:
110905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Lo = DAG.getConstant(0, NVT);              // Low part is zero.
1110786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
111105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      return true;
111205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    case ISD::SRL:
111305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Hi = DAG.getConstant(0, NVT);              // Hi part is zero.
1114786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
111505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      return true;
111605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    case ISD::SRA:
1117786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,       // Sign extend high part.
111805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                       DAG.getConstant(NVTBits-1, ShTy));
1119786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
112005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      return true;
112105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    }
112205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
112305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
112428dc98f7521933872b93156e3ebf5c9f8327b2b3Eli Friedman#if 0
112528dc98f7521933872b93156e3ebf5c9f8327b2b3Eli Friedman  // FIXME: This code is broken for shifts with a zero amount!
112605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // If we know that all of the high bits of the shift amount are zero, then we
112705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // can do this as a couple of simple shifts.
112805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if ((KnownZero & HighBitMask) == HighBitMask) {
112905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // Compute 32-amt.
1130475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Amt2 = DAG.getNode(ISD::SUB, ShTy,
113105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                 DAG.getConstant(NVTBits, ShTy),
113205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                 Amt);
113305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    unsigned Op1, Op2;
113405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    switch (N->getOpcode()) {
113505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    default: assert(0 && "Unknown shift");
113605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    case ISD::SHL:  Op1 = ISD::SHL; Op2 = ISD::SRL; break;
113705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    case ISD::SRL:
113805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    case ISD::SRA:  Op1 = ISD::SRL; Op2 = ISD::SHL; break;
113905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    }
114005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
114105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
114205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    Hi = DAG.getNode(ISD::OR, NVT,
114305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                     DAG.getNode(Op1, NVT, InH, Amt),
114405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                     DAG.getNode(Op2, NVT, InL, Amt2));
114505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    return true;
114605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
114728dc98f7521933872b93156e3ebf5c9f8327b2b3Eli Friedman#endif
114805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
114905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  return false;
115005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
115105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
11526fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng/// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
11536fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng/// of any size.
11546fb2168aaed06e4685d064b6f63dc4c668b42728Evan Chengbool DAGTypeLegalizer::
11556fb2168aaed06e4685d064b6f63dc4c668b42728Evan ChengExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
11566fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  SDValue Amt = N->getOperand(1);
11576fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
11586fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  MVT ShTy = Amt.getValueType();
11596fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  unsigned NVTBits = NVT.getSizeInBits();
11606fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  assert(isPowerOf2_32(NVTBits) &&
11616fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng         "Expanded integer type size not a power of two!");
11626fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  DebugLoc dl = N->getDebugLoc();
11636fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
11646fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  // Get the incoming operand to be shifted.
11656fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  SDValue InL, InH;
11666fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  GetExpandedInteger(N->getOperand(0), InL, InH);
11676fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
11686fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  SDValue NVBitsNode = DAG.getConstant(NVTBits, ShTy);
11696fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  SDValue Amt2 = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
11706fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  SDValue Cmp = DAG.getSetCC(dl, TLI.getSetCCResultType(ShTy),
11716fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng                             Amt, NVBitsNode, ISD::SETULT);
11726fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
11736fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  SDValue Lo1, Hi1, Lo2, Hi2;
11746fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  switch (N->getOpcode()) {
11756fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  default: assert(0 && "Unknown shift");
11766fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  case ISD::SHL:
11776fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    // ShAmt < NVTBits
11786fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Lo1 = DAG.getConstant(0, NVT);                  // Low part is zero.
11796fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Hi1 = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
11806fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
11816fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    // ShAmt >= NVTBits
11826fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Lo2 = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
11836fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Hi2 = DAG.getNode(ISD::OR, dl, NVT,
11846fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng                      DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
11856fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng                      DAG.getNode(ISD::SRL, dl, NVT, InL, Amt2));
11866fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
11876fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Lo = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Lo1, Lo2);
11886fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Hi = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Hi1, Hi2);
11896fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    return true;
11906fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  case ISD::SRL:
11916fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    // ShAmt < NVTBits
11926fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Hi1 = DAG.getConstant(0, NVT);                  // Hi part is zero.
11936fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Lo1 = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
11946fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
11956fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    // ShAmt >= NVTBits
11966fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Hi2 = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
11976fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Lo2 = DAG.getNode(ISD::OR, dl, NVT,
11986fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng                     DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
11996fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng                     DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
12006fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
12016fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Lo = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Lo1, Lo2);
12026fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Hi = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Hi1, Hi2);
12036fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    return true;
12046fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  case ISD::SRA:
12056fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    // ShAmt < NVTBits
12066fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Hi1 = DAG.getNode(ISD::SRA, dl, NVT, InH,       // Sign extend high part.
12076fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng                       DAG.getConstant(NVTBits-1, ShTy));
12086fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Lo1 = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
12096fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
12106fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    // ShAmt >= NVTBits
12116fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Hi2 = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
12126fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Lo2 = DAG.getNode(ISD::OR, dl, NVT,
12136fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng                      DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
12146fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng                      DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
12156fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
12166fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Lo = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Lo1, Lo2);
12176fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    Hi = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Hi1, Hi2);
12186fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    return true;
12196fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  }
12206fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
12216fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  return false;
12226fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng}
12236fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng
122405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
1225475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                           SDValue &Lo, SDValue &Hi) {
1226786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
122705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // Expand the subcomponents.
1228475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LHSL, LHSH, RHSL, RHSH;
122905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
123005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
123129a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands
123229a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands  MVT NVT = LHSL.getValueType();
1233475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LoOps[2] = { LHSL, RHSL };
1234475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue HiOps[3] = { LHSH, RHSH };
123505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
123629a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands  // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
123729a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands  // them.  TODO: Teach operation legalization how to expand unsupported
123829a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands  // ADDC/ADDE/SUBC/SUBE.  The problem is that these operations generate
123929a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands  // a carry of type MVT::Flag, but there doesn't seem to be any way to
124029a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands  // generate a value of this type in the expanded code sequence.
124129a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands  bool hasCarry =
1242f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman    TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
1243f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman                                   ISD::ADDC : ISD::SUBC,
1244f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman                                 TLI.getTypeToExpandTo(NVT));
124529a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands
124629a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands  if (hasCarry) {
1247874ae251c317788391f9c3f113957802d390a063Dale Johannesen    SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
124829a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands    if (N->getOpcode() == ISD::ADD) {
1249786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
125029a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands      HiOps[2] = Lo.getValue(1);
1251786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
125229a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands    } else {
1253786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
125429a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands      HiOps[2] = Lo.getValue(1);
1255786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
125629a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands    }
125705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  } else {
125829a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands    if (N->getOpcode() == ISD::ADD) {
1259786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
1260786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
1261786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo, LoOps[0],
126229a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands                                  ISD::SETULT);
1263786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
126429a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands                                   DAG.getConstant(1, NVT),
126529a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands                                   DAG.getConstant(0, NVT));
1266786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo, LoOps[1],
126729a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands                                  ISD::SETULT);
1268786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
126929a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands                                   DAG.getConstant(1, NVT), Carry1);
1270786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
127129a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands    } else {
1272786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
1273786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
12745480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands      SDValue Cmp =
1275f8d3ec2c5725a2010f11de4ba78f6127712a5fe7Dale Johannesen        DAG.getSetCC(dl, TLI.getSetCCResultType(LoOps[0].getValueType()),
12765480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands                     LoOps[0], LoOps[1], ISD::SETULT);
1277786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
127829a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands                                   DAG.getConstant(1, NVT),
127929a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands                                   DAG.getConstant(0, NVT));
1280786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
128129a2822f287275a0d4df49f98104409d8f97c5dfDuncan Sands    }
128205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
128305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
128405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
128505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
1286475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                            SDValue &Lo, SDValue &Hi) {
128705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // Expand the subcomponents.
1288475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LHSL, LHSH, RHSL, RHSH;
1289786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
129005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
129105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1292874ae251c317788391f9c3f113957802d390a063Dale Johannesen  SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1293475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LoOps[2] = { LHSL, RHSL };
1294475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue HiOps[3] = { LHSH, RHSH };
129505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
129605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (N->getOpcode() == ISD::ADDC) {
1297786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
129805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    HiOps[2] = Lo.getValue(1);
1299786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
130005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  } else {
1301786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
130205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    HiOps[2] = Lo.getValue(1);
1303786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
130405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
130505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1306874ae251c317788391f9c3f113957802d390a063Dale Johannesen  // Legalized the flag result - switch anything that used the old flag to
1307874ae251c317788391f9c3f113957802d390a063Dale Johannesen  // use the new one.
1308475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
130905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
131005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
131105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
1312475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                            SDValue &Lo, SDValue &Hi) {
131305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // Expand the subcomponents.
1314475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LHSL, LHSH, RHSL, RHSH;
1315786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
131605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
131705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1318874ae251c317788391f9c3f113957802d390a063Dale Johannesen  SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1319475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1320475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue HiOps[3] = { LHSH, RHSH };
132105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1322786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps, 3);
132305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  HiOps[2] = Lo.getValue(1);
1324786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps, 3);
132505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1326874ae251c317788391f9c3f113957802d390a063Dale Johannesen  // Legalized the flag result - switch anything that used the old flag to
1327874ae251c317788391f9c3f113957802d390a063Dale Johannesen  // use the new one.
1328475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
132905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
133005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
133105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
1332475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                               SDValue &Lo, SDValue &Hi) {
133305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1334786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
1335475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = N->getOperand(0);
133605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (Op.getValueType().bitsLE(NVT)) {
133705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // The low part is any extension of the input (which degenerates to a copy).
1338786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
1339e8d7230f480654cdb8ff1c3d0a38e1e9ab0bd55fDale Johannesen    Hi = DAG.getUNDEF(NVT);   // The high part is undefined.
134005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  } else {
134105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // For example, extension of an i48 to an i64.  The operand type necessarily
134205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // promotes to the result type, so will end up being expanded too.
134305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
134405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands           "Only know how to promote this result!");
1345475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Res = GetPromotedInteger(Op);
134605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    assert(Res.getValueType() == N->getValueType(0) &&
134705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands           "Operand over promoted?");
134805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // Split the promoted operand.  This will simplify when it is expanded.
134905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    SplitInteger(Res, Lo, Hi);
135005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
135105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
135205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
135395db39a9de48f69f4d764335b492b83a698c7854Duncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
1354475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                               SDValue &Lo, SDValue &Hi) {
1355786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
135695db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  GetExpandedInteger(N->getOperand(0), Lo, Hi);
135795db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  MVT NVT = Lo.getValueType();
135895db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
135995db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  unsigned NVTBits = NVT.getSizeInBits();
136095db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  unsigned EVTBits = EVT.getSizeInBits();
136195db39a9de48f69f4d764335b492b83a698c7854Duncan Sands
136295db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  if (NVTBits < EVTBits) {
1363786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
136495db39a9de48f69f4d764335b492b83a698c7854Duncan Sands                     DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
136595db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  } else {
1366786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
136795db39a9de48f69f4d764335b492b83a698c7854Duncan Sands    // The high part replicates the sign bit of Lo, make it explicit.
1368786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
136992abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                     DAG.getConstant(NVTBits-1, TLI.getPointerTy()));
137095db39a9de48f69f4d764335b492b83a698c7854Duncan Sands  }
137195db39a9de48f69f4d764335b492b83a698c7854Duncan Sands}
137295db39a9de48f69f4d764335b492b83a698c7854Duncan Sands
137305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
1374475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                               SDValue &Lo, SDValue &Hi) {
1375786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
137605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), Lo, Hi);
137705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT NVT = Lo.getValueType();
137805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
137905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  unsigned NVTBits = NVT.getSizeInBits();
138005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  unsigned EVTBits = EVT.getSizeInBits();
138105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
138205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (NVTBits < EVTBits) {
1383786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
138405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                     DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
138505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  } else {
1386786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
138705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // The high part must be zero, make it explicit.
138805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    Hi = DAG.getConstant(0, NVT);
138905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
139005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
139105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
139205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
1393475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                          SDValue &Lo, SDValue &Hi) {
1394786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
139505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), Hi, Lo);  // Note swapped operands.
1396786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
1397786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
139805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
139905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
140005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
1401475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                             SDValue &Lo, SDValue &Hi) {
140205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
140305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  unsigned NBitWidth = NVT.getSizeInBits();
140405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
140505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  Lo = DAG.getConstant(APInt(Cst).trunc(NBitWidth), NVT);
140605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
140705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
140805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
140905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
1410475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                         SDValue &Lo, SDValue &Hi) {
1411786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
141205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
141305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), Lo, Hi);
141405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT NVT = Lo.getValueType();
141505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1416786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue HiNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Hi,
1417ef5b199905cee0b78eb30cd44836e5b6ca5cbd09Duncan Sands                                   DAG.getConstant(0, NVT), ISD::SETNE);
141805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1419786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue LoLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
1420786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue HiLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
142105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1422786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Lo = DAG.getNode(ISD::SELECT, dl, NVT, HiNotZero, HiLZ,
1423786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                   DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
142405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                               DAG.getConstant(NVT.getSizeInBits(), NVT)));
142505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  Hi = DAG.getConstant(0, NVT);
142605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
142705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
142805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
1429475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                          SDValue &Lo, SDValue &Hi) {
1430786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
143105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
143205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), Lo, Hi);
143305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT NVT = Lo.getValueType();
1434b300d2aa3ef08b5074449e2c05804717f488f4e4Dale Johannesen  Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
1435786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                   DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
143605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  Hi = DAG.getConstant(0, NVT);
143705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
143805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
143905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
1440475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                         SDValue &Lo, SDValue &Hi) {
1441786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
144205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
144305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), Lo, Hi);
144405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT NVT = Lo.getValueType();
144505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1446786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue LoNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo,
1447ef5b199905cee0b78eb30cd44836e5b6ca5cbd09Duncan Sands                                   DAG.getConstant(0, NVT), ISD::SETNE);
144805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1449786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue LoLZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
1450786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  SDValue HiLZ = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
145105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1452786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Lo = DAG.getNode(ISD::SELECT, dl, NVT, LoNotZero, LoLZ,
1453786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                   DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
145405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                               DAG.getConstant(NVT.getSizeInBits(), NVT)));
145505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  Hi = DAG.getConstant(0, NVT);
145605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
145705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1458475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
1459475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                               SDValue &Hi) {
1460c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  DebugLoc dl = N->getDebugLoc();
146105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT VT = N->getValueType(0);
1462475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = N->getOperand(0);
1463b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
1464be1ad4de2900451626c8d4ace07b9ea16099ea1dDuncan Sands  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
1465c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  SplitInteger(MakeLibCall(LC, VT, &Op, 1, true/*irrelevant*/, dl), Lo, Hi);
1466ddc016cc8592fe5c9379feb42a1fb4fb63164a91Duncan Sands}
1467ddc016cc8592fe5c9379feb42a1fb4fb63164a91Duncan Sands
1468475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
1469475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                               SDValue &Hi) {
1470c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  DebugLoc dl = N->getDebugLoc();
147183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  MVT VT = N->getValueType(0);
1472475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = N->getOperand(0);
1473b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
1474be1ad4de2900451626c8d4ace07b9ea16099ea1dDuncan Sands  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
1475c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  SplitInteger(MakeLibCall(LC, VT, &Op, 1, false/*irrelevant*/, dl), Lo, Hi);
1476ddc016cc8592fe5c9379feb42a1fb4fb63164a91Duncan Sands}
1477ddc016cc8592fe5c9379feb42a1fb4fb63164a91Duncan Sands
147869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
1479475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                         SDValue &Lo, SDValue &Hi) {
1480ab09b7e8f34075c1759127a113f41bdf921f4034Duncan Sands  if (ISD::isNormalLoad(N)) {
1481ab09b7e8f34075c1759127a113f41bdf921f4034Duncan Sands    ExpandRes_NormalLoad(N, Lo, Hi);
148278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands    return;
148378cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  }
148478cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
148578cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
148678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
148783ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  MVT VT = N->getValueType(0);
148883ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(VT);
1489475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ch  = N->getChain();
1490475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ptr = N->getBasePtr();
1491cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  ISD::LoadExtType ExtType = N->getExtensionType();
1492cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  int SVOffset = N->getSrcValueOffset();
1493cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  unsigned Alignment = N->getAlignment();
1494cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  bool isVolatile = N->isVolatile();
1495786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
1496cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
149778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  assert(NVT.isByteSized() && "Expanded type not byte sized!");
1498cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
149978cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  if (N->getMemoryVT().bitsLE(NVT)) {
150083ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    MVT EVT = N->getMemoryVT();
1501cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1502786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1503786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                        EVT, isVolatile, Alignment);
1504cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1505cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Remember the chain.
1506cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    Ch = Lo.getValue(1);
1507cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1508cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    if (ExtType == ISD::SEXTLOAD) {
1509cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      // The high part is obtained by SRA'ing all but one of the bits of the
1510cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      // lo part.
151183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      unsigned LoSize = Lo.getValueType().getSizeInBits();
1512786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
151392abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                       DAG.getConstant(LoSize-1, TLI.getPointerTy()));
1514cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    } else if (ExtType == ISD::ZEXTLOAD) {
1515cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      // The high part is just a zero.
1516cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      Hi = DAG.getConstant(0, NVT);
1517cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    } else {
1518cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
1519cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      // The high part is undefined.
1520e8d7230f480654cdb8ff1c3d0a38e1e9ab0bd55fDale Johannesen      Hi = DAG.getUNDEF(NVT);
1521cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    }
1522cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  } else if (TLI.isLittleEndian()) {
1523cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Little-endian - low bits are at low addresses.
1524786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getSrcValue(), SVOffset,
1525cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                     isVolatile, Alignment);
1526cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1527cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    unsigned ExcessBits =
152883ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
152983ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    MVT NEVT = MVT::getIntegerVT(ExcessBits);
1530cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1531cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Increment the pointer to the other half.
153283ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    unsigned IncrementSize = NVT.getSizeInBits()/8;
1533786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
15340bd4893a0726889b942405262e53d06cf3fe3be8Chris Lattner                      DAG.getIntPtrConstant(IncrementSize));
1535786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(),
1536cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                        SVOffset+IncrementSize, NEVT,
1537cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                        isVolatile, MinAlign(Alignment, IncrementSize));
1538cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1539cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Build a factor node to remember that this load is independent of the
1540cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // other one.
1541786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1542cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                     Hi.getValue(1));
1543cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  } else {
1544cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Big-endian - high bits are at low addresses.  Favor aligned loads at
1545cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // the cost of some bit-fiddling.
154683ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    MVT EVT = N->getMemoryVT();
154783ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    unsigned EBytes = EVT.getStoreSizeInBits()/8;
154883ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    unsigned IncrementSize = NVT.getSizeInBits()/8;
1549cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    unsigned ExcessBits = (EBytes - IncrementSize)*8;
1550cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1551cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Load both the high bits and maybe some of the low bits.
1552786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
155383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands                        MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits),
1554cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                        isVolatile, Alignment);
1555cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1556cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Increment the pointer to the other half.
1557786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
15580bd4893a0726889b942405262e53d06cf3fe3be8Chris Lattner                      DAG.getIntPtrConstant(IncrementSize));
1559cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Load the rest of the low bits.
1560786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr, N->getSrcValue(),
156183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands                        SVOffset+IncrementSize,
156283ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands                        MVT::getIntegerVT(ExcessBits),
1563cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                        isVolatile, MinAlign(Alignment, IncrementSize));
1564cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1565cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Build a factor node to remember that this load is independent of the
1566cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // other one.
1567786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1568cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                     Hi.getValue(1));
1569cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
157083ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    if (ExcessBits < NVT.getSizeInBits()) {
1571cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      // Transfer low bits from the bottom of Hi to the top of Lo.
1572786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::OR, dl, NVT, Lo,
1573786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                       DAG.getNode(ISD::SHL, dl, NVT, Hi,
1574cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                                   DAG.getConstant(ExcessBits,
157592abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                                                   TLI.getPointerTy())));
1576cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      // Move high bits to the right position in Hi.
15777fb085871857134f8cbeb17499d4ab771ba8da42Duncan Sands      Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl,
1578786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                       NVT, Hi,
157983ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands                       DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
158092abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                                       TLI.getPointerTy()));
1581cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    }
1582cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
1583cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1584cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // Legalized the chain result - switch anything that used the old chain to
1585cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // use the new one.
1586475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  ReplaceValueWith(SDValue(N, 1), Ch);
1587cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner}
1588cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
158969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
1590475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                            SDValue &Lo, SDValue &Hi) {
1591786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
1592475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LL, LH, RL, RH;
159369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  GetExpandedInteger(N->getOperand(0), LL, LH);
159469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  GetExpandedInteger(N->getOperand(1), RL, RH);
1595786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
1596786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
1597cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner}
1598cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
159905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
1600475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                        SDValue &Lo, SDValue &Hi) {
160105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT VT = N->getValueType(0);
160205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(VT);
1603c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  DebugLoc dl = N->getDebugLoc();
160469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
1605f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
1606f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
1607f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
1608f560ffae1f1f6591859c7b70636a3eca6c03f083Dan Gohman  bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
1609cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1610475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue LL, LH, RL, RH;
161169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    GetExpandedInteger(N->getOperand(0), LL, LH);
161269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    GetExpandedInteger(N->getOperand(1), RL, RH);
161383ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    unsigned OuterBitSize = VT.getSizeInBits();
1614e9c80f4d576bf1ff682958d447c1a60fa9348da3Duncan Sands    unsigned InnerBitSize = NVT.getSizeInBits();
1615cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1616cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
161769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
1618e9c80f4d576bf1ff682958d447c1a60fa9348da3Duncan Sands    APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
1619e9c80f4d576bf1ff682958d447c1a60fa9348da3Duncan Sands    if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
1620e9c80f4d576bf1ff682958d447c1a60fa9348da3Duncan Sands        DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
1621cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      // The inputs are both zero-extended.
1622cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      if (HasUMUL_LOHI) {
1623cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        // We can emit a umul_lohi.
1624786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen        Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
1625ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        Hi = SDValue(Lo.getNode(), 1);
1626cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        return;
1627cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      }
1628cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      if (HasMULHU) {
1629cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        // We can emit a mulhu+mul.
1630786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen        Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1631786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen        Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
1632cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        return;
1633cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      }
1634cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    }
1635e9c80f4d576bf1ff682958d447c1a60fa9348da3Duncan Sands    if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
1636cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      // The input values are both sign-extended.
1637cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      if (HasSMUL_LOHI) {
1638cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        // We can emit a smul_lohi.
1639786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen        Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
1640ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif        Hi = SDValue(Lo.getNode(), 1);
1641cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        return;
1642cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      }
1643cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      if (HasMULHS) {
1644cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        // We can emit a mulhs+mul.
1645786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen        Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1646786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen        Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
1647cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        return;
1648cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      }
1649cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    }
1650cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    if (HasUMUL_LOHI) {
1651cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      // Lo,Hi = umul LHS, RHS.
1652786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
1653cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                                       DAG.getVTList(NVT, NVT), LL, RL);
1654cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      Lo = UMulLOHI;
1655cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      Hi = UMulLOHI.getValue(1);
1656786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
1657786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
1658786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
1659786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
1660cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      return;
1661cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    }
1662e9c80f4d576bf1ff682958d447c1a60fa9348da3Duncan Sands    if (HasMULHU) {
1663786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1664786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
1665786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
1666786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
1667786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
1668786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
1669e9c80f4d576bf1ff682958d447c1a60fa9348da3Duncan Sands      return;
1670e9c80f4d576bf1ff682958d447c1a60fa9348da3Duncan Sands    }
1671cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
167241edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands
1673cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // If nothing else, we can make a libcall.
16745ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
167515c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta  if (VT == MVT::i16)
167615c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta    LC = RTLIB::MUL_I16;
167715c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta  else if (VT == MVT::i32)
16785ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands    LC = RTLIB::MUL_I32;
16795ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  else if (VT == MVT::i64)
1680e9c80f4d576bf1ff682958d447c1a60fa9348da3Duncan Sands    LC = RTLIB::MUL_I64;
16815ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  else if (VT == MVT::i128)
16825ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands    LC = RTLIB::MUL_I128;
16835ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
1684e9c80f4d576bf1ff682958d447c1a60fa9348da3Duncan Sands
1685475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1686c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  SplitInteger(MakeLibCall(LC, VT, Ops, 2, true/*irrelevant*/, dl), Lo, Hi);
168741edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands}
1688cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
168969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
1690475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                         SDValue &Lo, SDValue &Hi) {
16915ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  MVT VT = N->getValueType(0);
1692c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  DebugLoc dl = N->getDebugLoc();
16935ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands
16945ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1695a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta  if (VT == MVT::i16)
1696a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta    LC = RTLIB::SDIV_I16;
1697a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta  else if (VT == MVT::i32)
16985ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands    LC = RTLIB::SDIV_I32;
16995ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  else if (VT == MVT::i64)
17005ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands    LC = RTLIB::SDIV_I64;
17015ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  else if (VT == MVT::i128)
17025ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands    LC = RTLIB::SDIV_I128;
17035ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
17045ac319ac7125b009adddcc49294d2e040c4a91e5Duncan Sands
1705475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1706c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  SplitInteger(MakeLibCall(LC, VT, Ops, 2, true, dl), Lo, Hi);
1707ddc016cc8592fe5c9379feb42a1fb4fb63164a91Duncan Sands}
1708ddc016cc8592fe5c9379feb42a1fb4fb63164a91Duncan Sands
170969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
1710475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                          SDValue &Lo, SDValue &Hi) {
171183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  MVT VT = N->getValueType(0);
1712c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  DebugLoc dl = N->getDebugLoc();
171369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
171469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  // If we can emit an efficient shift operation, do so now.  Check to see if
1715cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // the RHS is a constant.
1716cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1717f5aeb1a8e4cf272c7348376d185ef8d8267653e0Dan Gohman    return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
1718cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1719cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // If we can determine that the high bit of the shift is zero or one, even if
1720cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // the low bits are variable, emit this shift in an optimized form.
1721cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1722cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    return;
172369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
1724cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // If this target supports shift_PARTS, use it.  First, map to the _PARTS opc.
1725cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  unsigned PartsOpc;
172641edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands  if (N->getOpcode() == ISD::SHL) {
1727cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    PartsOpc = ISD::SHL_PARTS;
172841edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands  } else if (N->getOpcode() == ISD::SRL) {
1729cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    PartsOpc = ISD::SRL_PARTS;
173041edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands  } else {
1731cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1732cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    PartsOpc = ISD::SRA_PARTS;
1733cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
173469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
1735cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // Next check to see if the target supports this SHL_PARTS operation or if it
1736cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // will custom expand it.
173783ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(VT);
1738cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1739cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1740cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      Action == TargetLowering::Custom) {
1741cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Expand the subcomponents.
1742475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue LHSL, LHSH;
174369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
174469b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
1745475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Ops[] = { LHSL, LHSH, N->getOperand(1) };
174683ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    MVT VT = LHSL.getValueType();
1747fc1665793e62eb4f26d24b8a19eecf59cd872e2aDan Gohman    Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops, 3);
1748cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    Hi = Lo.getValue(1);
1749cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    return;
1750cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
175141edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands
1752cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // Otherwise, emit a libcall.
1753dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
175441edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands  bool isSigned;
175541edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands  if (N->getOpcode() == ISD::SHL) {
175641edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands    isSigned = false; /*sign irrelevant*/
175715c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta    if (VT == MVT::i16)
175815c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta      LC = RTLIB::SHL_I16;
175915c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta    else if (VT == MVT::i32)
1760dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands      LC = RTLIB::SHL_I32;
1761dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands    else if (VT == MVT::i64)
1762dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands      LC = RTLIB::SHL_I64;
1763dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands    else if (VT == MVT::i128)
1764dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands      LC = RTLIB::SHL_I128;
176541edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands  } else if (N->getOpcode() == ISD::SRL) {
176641edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands    isSigned = false;
176715c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta    if (VT == MVT::i16)
176815c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta      LC = RTLIB::SRL_I16;
176915c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta    else if (VT == MVT::i32)
1770dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands      LC = RTLIB::SRL_I32;
1771dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands    else if (VT == MVT::i64)
1772dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands      LC = RTLIB::SRL_I64;
1773dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands    else if (VT == MVT::i128)
1774dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands      LC = RTLIB::SRL_I128;
177541edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands  } else {
177641edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands    assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
177741edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands    isSigned = true;
177815c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta    if (VT == MVT::i16)
177915c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta      LC = RTLIB::SRA_I16;
178015c94d08ab2be2e3d00de4edbfc7adde6545a7dbSanjiv Gupta    else if (VT == MVT::i32)
1781dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands      LC = RTLIB::SRA_I32;
1782dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands    else if (VT == MVT::i64)
1783dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands      LC = RTLIB::SRA_I64;
1784dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands    else if (VT == MVT::i128)
1785dddc6291fb5274282a20d5923b50535d456d34a4Duncan Sands      LC = RTLIB::SRA_I128;
178641edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands  }
17878c899ee031481dbece5f111379a274c848cb5902Duncan Sands
17886fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
17896fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
17906fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    SplitInteger(MakeLibCall(LC, VT, Ops, 2, isSigned, dl), Lo, Hi);
17916fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    return;
17926fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  }
179341edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands
17946fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng  if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
17956fb2168aaed06e4685d064b6f63dc4c668b42728Evan Cheng    assert(0 && "Unsupported shift!");
179641edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands}
1797cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
179805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
1799475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                SDValue &Lo, SDValue &Hi) {
180005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1801786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
1802475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = N->getOperand(0);
180305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (Op.getValueType().bitsLE(NVT)) {
1804b3bc6352defdf1a5c6b1b0770d0c4d603f6524a8Duncan Sands    // The low part is sign extension of the input (degenerates to a copy).
1805786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
180605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // The high part is obtained by SRA'ing all but one of the bits of low part.
180705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    unsigned LoSize = NVT.getSizeInBits();
1808786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
180992abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                     DAG.getConstant(LoSize-1, TLI.getPointerTy()));
181005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  } else {
181105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // For example, extension of an i48 to an i64.  The operand type necessarily
181205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // promotes to the result type, so will end up being expanded too.
181305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
181405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands           "Only know how to promote this result!");
1815475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Res = GetPromotedInteger(Op);
181605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    assert(Res.getValueType() == N->getValueType(0) &&
181705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands           "Operand over promoted?");
181805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // Split the promoted operand.  This will simplify when it is expanded.
181905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    SplitInteger(Res, Lo, Hi);
182005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    unsigned ExcessBits =
182105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1822786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
182305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                     DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
182405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
182505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
182605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
182705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::
1828475871a144eb604ddaf37503397ba0941442e5fbDan GohmanExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
1829786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
183069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  GetExpandedInteger(N->getOperand(0), Lo, Hi);
183105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1832d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands
183305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (EVT.bitsLE(Lo.getValueType())) {
183405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // sext_inreg the low part if needed.
1835786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
183605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                     N->getOperand(1));
1837d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands
183805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // The high part gets the sign extension from the lo-part.  This handles
183905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // things like sextinreg V:i64 from i8.
1840786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
184105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                     DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
184292abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                                     TLI.getPointerTy()));
184305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  } else {
184405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // For example, extension of an i48 to an i64.  Leave the low part alone,
184505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // sext_inreg the high part.
184605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    unsigned ExcessBits =
184705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
1848786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
184905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                     DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
185005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
185105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
1852d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands
185305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
1854475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                         SDValue &Lo, SDValue &Hi) {
185505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT VT = N->getValueType(0);
1856c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  DebugLoc dl = N->getDebugLoc();
185705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
185805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1859a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta  if (VT == MVT::i16)
1860a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta    LC = RTLIB::SREM_I16;
1861a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta  else if (VT == MVT::i32)
186205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    LC = RTLIB::SREM_I32;
186305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  else if (VT == MVT::i64)
186405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    LC = RTLIB::SREM_I64;
186505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  else if (VT == MVT::i128)
186605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    LC = RTLIB::SREM_I128;
186705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
186805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
1869475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1870c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  SplitInteger(MakeLibCall(LC, VT, Ops, 2, true, dl), Lo, Hi);
1871d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands}
1872d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands
187305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
1874475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                             SDValue &Lo, SDValue &Hi) {
187505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1876786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
1877786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
1878786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Hi = DAG.getNode(ISD::SRL, dl,
1879786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                   N->getOperand(0).getValueType(), N->getOperand(0),
188092abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                   DAG.getConstant(NVT.getSizeInBits(), TLI.getPointerTy()));
1881786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
1882d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands}
1883d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands
188405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
1885475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                         SDValue &Lo, SDValue &Hi) {
188605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT VT = N->getValueType(0);
1887c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  DebugLoc dl = N->getDebugLoc();
1888d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands
188905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1890a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta  if (VT == MVT::i16)
1891a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta    LC = RTLIB::UDIV_I16;
1892a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta  else if (VT == MVT::i32)
189305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    LC = RTLIB::UDIV_I32;
189405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  else if (VT == MVT::i64)
189505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    LC = RTLIB::UDIV_I64;
189605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  else if (VT == MVT::i128)
189705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    LC = RTLIB::UDIV_I128;
189805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
1899d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands
1900475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1901c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  SplitInteger(MakeLibCall(LC, VT, Ops, 2, false, dl), Lo, Hi);
1902d462ba853981d45bf9c777564e79dc9e1c850ca6Duncan Sands}
1903cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
190405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
1905475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                         SDValue &Lo, SDValue &Hi) {
190605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT VT = N->getValueType(0);
1907c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  DebugLoc dl = N->getDebugLoc();
190869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
190905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1910a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta  if (VT == MVT::i16)
1911a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta    LC = RTLIB::UREM_I16;
1912a43a7aefd753fe7d6005cbebc9619268db4ae139Sanjiv Gupta  else if (VT == MVT::i32)
191305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    LC = RTLIB::UREM_I32;
191405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  else if (VT == MVT::i64)
191505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    LC = RTLIB::UREM_I64;
191605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  else if (VT == MVT::i128)
191705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    LC = RTLIB::UREM_I128;
191805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
1919cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1920475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1921c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  SplitInteger(MakeLibCall(LC, VT, Ops, 2, false, dl), Lo, Hi);
1922cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner}
1923cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
192405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsvoid DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
1925475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                SDValue &Lo, SDValue &Hi) {
192683ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1927786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
1928475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = N->getOperand(0);
192905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (Op.getValueType().bitsLE(NVT)) {
1930b3bc6352defdf1a5c6b1b0770d0c4d603f6524a8Duncan Sands    // The low part is zero extension of the input (degenerates to a copy).
1931786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
193205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    Hi = DAG.getConstant(0, NVT);   // The high part is just a zero.
193305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  } else {
193405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // For example, extension of an i48 to an i64.  The operand type necessarily
193505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // promotes to the result type, so will end up being expanded too.
193605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
193705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands           "Only know how to promote this result!");
1938475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Res = GetPromotedInteger(Op);
193905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    assert(Res.getValueType() == N->getValueType(0) &&
194005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands           "Operand over promoted?");
194105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // Split the promoted operand.  This will simplify when it is expanded.
194205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    SplitInteger(Res, Lo, Hi);
194305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    unsigned ExcessBits =
194405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
19454be0bdf7c1162824927dd3de89e016ae4934d0d6Dale Johannesen    Hi = DAG.getZeroExtendInReg(Hi, dl, MVT::getIntegerVT(ExcessBits));
19469e255b7df5a0a629920706e086e78ef89bf2f183Dan Gohman  }
1947cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner}
1948cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1949cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
1950cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//===----------------------------------------------------------------------===//
195169b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands//  Integer Operand Expansion
1952cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner//===----------------------------------------------------------------------===//
1953cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
195405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands/// ExpandIntegerOperand - This method is called when the specified operand of
195505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands/// the specified node is found to need expansion.  At this point, all of the
195605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands/// result types of the node are known to be legal, but other operands of the
195705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands/// node may need promotion or expansion as well as the specified one.
195805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sandsbool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
195905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  DEBUG(cerr << "Expand integer operand: "; N->dump(&DAG); cerr << "\n");
1960475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Res = SDValue();
1961051bb7b07504be9f848f7cce802e62ed24980bc5Duncan Sands
1962f43071beddb7ed5b2fd7d2f06c4130460616a13dDuncan Sands  if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1963bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta    return false;
196411ac797f5ed142f11aafde3dd76c28a73d84282eDuncan Sands
1965bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  switch (N->getOpcode()) {
1966bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  default:
196705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  #ifndef NDEBUG
1968bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta    cerr << "ExpandIntegerOperand Op #" << OpNo << ": ";
1969bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta    N->dump(&DAG); cerr << "\n";
197005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  #endif
19717d696d80409aad20bb5da0fc4eccab941dd371d4Torok Edwin    LLVM_UNREACHABLE("Do not know how to expand this operator's operand!");
1972bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta
1973bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::BIT_CONVERT:       Res = ExpandOp_BIT_CONVERT(N); break;
197492abc62399881ba9c525be80362c134ad836e2d9Duncan Sands  case ISD::BR_CC:             Res = ExpandIntOp_BR_CC(N); break;
197592abc62399881ba9c525be80362c134ad836e2d9Duncan Sands  case ISD::BUILD_VECTOR:      Res = ExpandOp_BUILD_VECTOR(N); break;
1976bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::EXTRACT_ELEMENT:   Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1977bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
1978bb326bbe88d0b243d5d9d224308eb0c028d4d4afSanjiv Gupta  case ISD::SCALAR_TO_VECTOR:  Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
197992abc62399881ba9c525be80362c134ad836e2d9Duncan Sands  case ISD::SELECT_CC:         Res = ExpandIntOp_SELECT_CC(N); break;
198092abc62399881ba9c525be80362c134ad836e2d9Duncan Sands  case ISD::SETCC:             Res = ExpandIntOp_SETCC(N); break;
198192abc62399881ba9c525be80362c134ad836e2d9Duncan Sands  case ISD::SINT_TO_FP:        Res = ExpandIntOp_SINT_TO_FP(N); break;
198292abc62399881ba9c525be80362c134ad836e2d9Duncan Sands  case ISD::STORE:   Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
198392abc62399881ba9c525be80362c134ad836e2d9Duncan Sands  case ISD::TRUNCATE:          Res = ExpandIntOp_TRUNCATE(N); break;
198492abc62399881ba9c525be80362c134ad836e2d9Duncan Sands  case ISD::UINT_TO_FP:        Res = ExpandIntOp_UINT_TO_FP(N); break;
198555467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta
198655467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  case ISD::SHL:
198755467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  case ISD::SRA:
198855467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  case ISD::SRL:
198955467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  case ISD::ROTL:
199055467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
199105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
199269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
199305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // If the result is null, the sub-method took care of registering results etc.
1994ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (!Res.getNode()) return false;
199547d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands
199647d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // If the result is N, the sub-method updated N in place.  Tell the legalizer
199747d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  // core about this.
199847d9dcc584cdb7fd645ca1d5c2a0ce363570aeb7Duncan Sands  if (Res.getNode() == N)
199905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    return true;
2000cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
200105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
200205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands         "Invalid operand expansion");
200305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
2004475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  ReplaceValueWith(SDValue(N, 0), Res);
200505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  return false;
2006cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner}
2007cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
200811ac797f5ed142f11aafde3dd76c28a73d84282eDuncan Sands/// IntegerExpandSetCCOperands - Expand the operands of a comparison.  This code
200911ac797f5ed142f11aafde3dd76c28a73d84282eDuncan Sands/// is shared among BR_CC, SELECT_CC, and SETCC handlers.
2010475871a144eb604ddaf37503397ba0941442e5fbDan Gohmanvoid DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
2011475871a144eb604ddaf37503397ba0941442e5fbDan Gohman                                                  SDValue &NewRHS,
2012c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen                                                  ISD::CondCode &CCCode,
2013c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen                                                  DebugLoc dl) {
2014475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue LHSLo, LHSHi, RHSLo, RHSHi;
201569b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  GetExpandedInteger(NewLHS, LHSLo, LHSHi);
201669b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands  GetExpandedInteger(NewRHS, RHSLo, RHSHi);
201741edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands
201883ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  MVT VT = NewLHS.getValueType();
201941edfb8ae119d6cf6579046619dd2820ec876065Duncan Sands
2020cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
202111ac797f5ed142f11aafde3dd76c28a73d84282eDuncan Sands    if (RHSLo == RHSHi) {
202211ac797f5ed142f11aafde3dd76c28a73d84282eDuncan Sands      if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
2023cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        if (RHSCST->isAllOnesValue()) {
2024cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner          // Equality comparison to -1.
20257fb085871857134f8cbeb17499d4ab771ba8da42Duncan Sands          NewLHS = DAG.getNode(ISD::AND, dl,
2026786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                               LHSLo.getValueType(), LHSLo, LHSHi);
2027cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner          NewRHS = RHSLo;
2028cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner          return;
2029cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        }
203011ac797f5ed142f11aafde3dd76c28a73d84282eDuncan Sands      }
203111ac797f5ed142f11aafde3dd76c28a73d84282eDuncan Sands    }
203269b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
2033786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
2034786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
2035786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
2036cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2037cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    return;
2038cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
203969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
2040cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // If this is a comparison of the sign bit, just look at the top part.
2041cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // X > -1,  x < 0
2042cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
2043002e5d0a170dadd5c307e0b00d8c7970835837e6Dan Gohman    if ((CCCode == ISD::SETLT && CST->isNullValue()) ||     // X < 0
2044cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        (CCCode == ISD::SETGT && CST->isAllOnesValue())) {  // X > -1
2045cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      NewLHS = LHSHi;
2046cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      NewRHS = RHSHi;
2047cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      return;
2048cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    }
204969b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
2050cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // FIXME: This generated code sucks.
2051cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  ISD::CondCode LowCC;
2052cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  switch (CCCode) {
2053cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  default: assert(0 && "Unknown integer setcc!");
2054cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::SETLT:
2055cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::SETULT: LowCC = ISD::SETULT; break;
2056cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::SETGT:
2057cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2058cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::SETLE:
2059cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::SETULE: LowCC = ISD::SETULE; break;
2060cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::SETGE:
2061cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2062cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
206369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
2064cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison
2065cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands
2066cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
206769b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
2068cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // NOTE: on targets without efficient SELECT of bools, we can always use
2069cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2070cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
2071475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Tmp1, Tmp2;
20725480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands  Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
2073ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                           LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
2074ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (!Tmp1.getNode())
2075786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
20765480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands                        LHSLo, RHSLo, LowCC);
20775480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands  Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
2078ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                           LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
2079ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (!Tmp2.getNode())
2080786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Tmp2 = DAG.getNode(ISD::SETCC, dl,
2081786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                       TLI.getSetCCResultType(LHSHi.getValueType()),
20825480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands                       LHSHi, RHSHi, DAG.getCondCode(CCCode));
208369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
2084ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
2085ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
2086002e5d0a170dadd5c307e0b00d8c7970835837e6Dan Gohman  if ((Tmp1C && Tmp1C->isNullValue()) ||
2087002e5d0a170dadd5c307e0b00d8c7970835837e6Dan Gohman      (Tmp2C && Tmp2C->isNullValue() &&
2088cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner       (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
2089cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
2090002e5d0a170dadd5c307e0b00d8c7970835837e6Dan Gohman      (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
2091cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner       (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
2092cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner        CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
2093cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // low part is known false, returns high part.
2094cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // For LE / GE, if high part is known false, ignore the low part.
2095cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // For LT / GT, if high part is known true, ignore the low part.
2096cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    NewLHS = Tmp2;
2097475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    NewRHS = SDValue();
2098cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    return;
2099cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
210069b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands
21015480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands  NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
2102fdc40a0a696c658d550d894ea03772e5f8af2c94Scott Michel                             LHSHi, RHSHi, ISD::SETEQ, false,
2103ff97d4fe81ef0dcee9fe490bed8ab08e40251905Dale Johannesen                             DagCombineInfo, dl);
2104ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (!NewLHS.getNode())
2105786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    NewLHS = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
21065480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands                          LHSHi, RHSHi, ISD::SETEQ);
2107786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  NewLHS = DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
2108cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                       NewLHS, Tmp1, Tmp2);
2109475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  NewRHS = SDValue();
2110cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner}
2111cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
2112475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
2113475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
211405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
2115c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
211605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
211705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // If ExpandSetCCOperands returned a scalar, we need to compare the result
211805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // against zero to select between true and false values.
2119ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (NewRHS.getNode() == 0) {
212005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
212105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    CCCode = ISD::SETNE;
212205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
212305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
212405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // Update N to have the operands specified.
2125475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
212605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                DAG.getCondCode(CCCode), NewLHS, NewRHS,
212705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                N->getOperand(4));
212805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
212905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
2130475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
2131475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
213205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
2133c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
213405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
213505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // If ExpandSetCCOperands returned a scalar, we need to compare the result
213605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // against zero to select between true and false values.
2137ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (NewRHS.getNode() == 0) {
213805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
213905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    CCCode = ISD::SETNE;
214005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
214105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
214205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // Update N to have the operands specified.
2143475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
214405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                N->getOperand(2), N->getOperand(3),
214505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                DAG.getCondCode(CCCode));
214605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
214705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
2148475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
2149475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
215005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
2151c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
215205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
215305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // If ExpandSetCCOperands returned a scalar, use it.
2154ba36cb5242eb02b12b277f82b9efe497f7da4d7fGabor Greif  if (NewRHS.getNode() == 0) {
215505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    assert(NewLHS.getValueType() == N->getValueType(0) &&
215605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands           "Unexpected setcc expansion!");
215705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    return NewLHS;
215805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
215905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
216005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // Otherwise, update N to have the operands specified.
2161475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
216205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands                                DAG.getCondCode(CCCode));
216305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
216405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
216555467af31620c9d027e071ebcd9746b7593cff17Sanjiv GuptaSDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
216655467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  // The value being shifted is legal, but the shift amount is too big.
216755467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  // It follows that either the result of the shift is undefined, or the
216855467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  // upper half of the shift amount is zero.  Just use the lower half.
216955467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  SDValue Lo, Hi;
217055467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  GetExpandedInteger(N->getOperand(1), Lo, Hi);
217155467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta  return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Lo);
217255467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta}
217355467af31620c9d027e071ebcd9746b7593cff17Sanjiv Gupta
2174475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
2175475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = N->getOperand(0);
217605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT DstVT = N->getValueType(0);
2177b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
217805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  assert(LC != RTLIB::UNKNOWN_LIBCALL &&
217905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands         "Don't know how to expand this SINT_TO_FP!");
2180c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  return MakeLibCall(LC, DstVT, &Op, 1, true, N->getDebugLoc());
218105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
218205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
2183475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
2184ab09b7e8f34075c1759127a113f41bdf921f4034Duncan Sands  if (ISD::isNormalStore(N))
2185ab09b7e8f34075c1759127a113f41bdf921f4034Duncan Sands    return ExpandOp_NormalStore(N, OpNo);
218678cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands
218778cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2188cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  assert(OpNo == 1 && "Can only expand the stored value so far");
2189cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
219083ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  MVT VT = N->getOperand(1).getValueType();
219183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands  MVT NVT = TLI.getTypeToTransformTo(VT);
2192475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ch  = N->getChain();
2193475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Ptr = N->getBasePtr();
2194cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  int SVOffset = N->getSrcValueOffset();
2195cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  unsigned Alignment = N->getAlignment();
2196cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  bool isVolatile = N->isVolatile();
2197786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  DebugLoc dl = N->getDebugLoc();
2198475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Lo, Hi;
2199cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
2200a1ace76c70ae5332d6f33fce5c0c1e2fdb8cca11Duncan Sands  assert(NVT.isByteSized() && "Expanded type not byte sized!");
2201cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
220278cd649ad326f79a1f8424ca2b63cea3239a9a52Duncan Sands  if (N->getMemoryVT().bitsLE(NVT)) {
220369b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    GetExpandedInteger(N->getValue(), Lo, Hi);
2204786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getSrcValue(), SVOffset,
2205b625f2f8960de32bc973092aaee8ac62863006feDan Gohman                             N->getMemoryVT(), isVolatile, Alignment);
2206cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  } else if (TLI.isLittleEndian()) {
2207cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Little-endian - low bits are at low addresses.
220869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    GetExpandedInteger(N->getValue(), Lo, Hi);
2209cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
2210786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getSrcValue(), SVOffset,
2211cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                      isVolatile, Alignment);
2212cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
2213cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    unsigned ExcessBits =
221483ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands      N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
221583ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    MVT NEVT = MVT::getIntegerVT(ExcessBits);
2216cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
2217cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Increment the pointer to the other half.
221883ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    unsigned IncrementSize = NVT.getSizeInBits()/8;
2219786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
22200bd4893a0726889b942405262e53d06cf3fe3be8Chris Lattner                      DAG.getIntPtrConstant(IncrementSize));
2221786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getSrcValue(),
2222cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                           SVOffset+IncrementSize, NEVT,
2223cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                           isVolatile, MinAlign(Alignment, IncrementSize));
2224786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
2225cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  } else {
2226cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Big-endian - high bits are at low addresses.  Favor aligned stores at
2227cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // the cost of some bit-fiddling.
222869b01e92a29ce6d7e435171aeea3fbc987b81586Duncan Sands    GetExpandedInteger(N->getValue(), Lo, Hi);
2229cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
223083ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    MVT EVT = N->getMemoryVT();
223183ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    unsigned EBytes = EVT.getStoreSizeInBits()/8;
223283ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    unsigned IncrementSize = NVT.getSizeInBits()/8;
2233cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    unsigned ExcessBits = (EBytes - IncrementSize)*8;
223483ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    MVT HiVT = MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits);
2235cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
223683ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands    if (ExcessBits < NVT.getSizeInBits()) {
2237cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner      // Transfer high bits from the top of Lo to the bottom of Hi.
2238786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
223983ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands                       DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
224092abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                                       TLI.getPointerTy()));
2241786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen      Hi = DAG.getNode(ISD::OR, dl, NVT, Hi,
2242de06470330260f5937e7ca558f5f5b3e171f2ee5Dale Johannesen                       DAG.getNode(ISD::SRL, dl, NVT, Lo,
2243cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                                   DAG.getConstant(ExcessBits,
224492abc62399881ba9c525be80362c134ad836e2d9Duncan Sands                                                   TLI.getPointerTy())));
2245cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    }
2246cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
2247cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Store both the high bits and maybe some of the low bits.
2248786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getSrcValue(),
2249cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                           SVOffset, HiVT, isVolatile, Alignment);
2250cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner
2251cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Increment the pointer to the other half.
2252786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
22530bd4893a0726889b942405262e53d06cf3fe3be8Chris Lattner                      DAG.getIntPtrConstant(IncrementSize));
2254cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner    // Store the lowest ExcessBits bits in the second half.
2255786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getSrcValue(),
2256cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                           SVOffset+IncrementSize,
225783ec4b6711980242ef3c55a4fa36b2d7a39c1bfbDuncan Sands                           MVT::getIntegerVT(ExcessBits),
2258cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner                           isVolatile, MinAlign(Alignment, IncrementSize));
2259786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
2260cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner  }
2261cc663a8112017f06e0cd4b6fe1546ccfcd5d05c3Chris Lattner}
226205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
2263475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
2264475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue InL, InH;
226505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  GetExpandedInteger(N->getOperand(0), InL, InH);
226605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // Just truncate the low part of the source.
2267786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen  return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), N->getValueType(0), InL);
226805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
226905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
2270475871a144eb604ddaf37503397ba0941442e5fbDan GohmanSDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
2271475871a144eb604ddaf37503397ba0941442e5fbDan Gohman  SDValue Op = N->getOperand(0);
227205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT SrcVT = Op.getValueType();
227305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  MVT DstVT = N->getValueType(0);
2274c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  DebugLoc dl = N->getDebugLoc();
227505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
227605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  if (TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
227705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // Do a signed conversion then adjust the result.
2278786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
227905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    SignedConv = TLI.LowerOperation(SignedConv, DAG);
228005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
228105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // The result of the signed conversion needs adjusting if the 'sign bit' of
228205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // the incoming integer was set.  To handle this, we dynamically test to see
228305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // if it is set, and, if so, add a fudge factor.
228405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
228505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    const uint64_t F32TwoE32  = 0x4F800000ULL;
228605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    const uint64_t F32TwoE64  = 0x5F800000ULL;
228705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    const uint64_t F32TwoE128 = 0x7F800000ULL;
228805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
228905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    APInt FF(32, 0);
229005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    if (SrcVT == MVT::i32)
229105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      FF = APInt(32, F32TwoE32);
229205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    else if (SrcVT == MVT::i64)
229305c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      FF = APInt(32, F32TwoE64);
229405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    else if (SrcVT == MVT::i128)
229505c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      FF = APInt(32, F32TwoE128);
229605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    else
229705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands      assert(false && "Unsupported UINT_TO_FP!");
229805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
229905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // Check whether the sign bit is set.
2300475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Lo, Hi;
230105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    GetExpandedInteger(Op, Lo, Hi);
23027fb085871857134f8cbeb17499d4ab771ba8da42Duncan Sands    SDValue SignSet = DAG.getSetCC(dl,
2303786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen                                   TLI.getSetCCResultType(Hi.getValueType()),
23045480c0469e5c0323ffb12f1ead2abd169d6cc0e7Duncan Sands                                   Hi, DAG.getConstant(0, Hi.getValueType()),
2305ef5b199905cee0b78eb30cd44836e5b6ca5cbd09Duncan Sands                                   ISD::SETLT);
230605c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
230705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
2308475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue FudgePtr = DAG.getConstantPool(ConstantInt::get(FF.zext(64)),
230949c18cce976c158e86f54c681dff21bb81640fb8Duncan Sands                                           TLI.getPointerTy());
231005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
231105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
2312475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Zero = DAG.getIntPtrConstant(0);
2313475871a144eb604ddaf37503397ba0941442e5fbDan Gohman    SDValue Four = DAG.getIntPtrConstant(4);
231405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    if (TLI.isBigEndian()) std::swap(Zero, Four);
2315786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
2316ef5b199905cee0b78eb30cd44836e5b6ca5cbd09Duncan Sands                                 Zero, Four);
23171606e8e4cd937e6de6681f686c266cf61722d972Evan Cheng    unsigned Alignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
2318786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    FudgePtr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), FudgePtr, Offset);
231987a0f10dc7eff8cf5e83a754f75adf9cb3991435Dan Gohman    Alignment = std::min(Alignment, 4u);
232005c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
232105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // Load the value out, extending it from f32 to the destination float type.
232205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands    // FIXME: Avoid the extend by constructing the right constant pool?
2323786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(),
232487a0f10dc7eff8cf5e83a754f75adf9cb3991435Dan Gohman                                   FudgePtr, NULL, 0, MVT::f32,
232587a0f10dc7eff8cf5e83a754f75adf9cb3991435Dan Gohman                                   false, Alignment);
2326786fd4dded6a42561c0d82bbd9d13b9a4d8d9675Dale Johannesen    return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
232705c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  }
232805c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands
232905c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  // Otherwise, use a libcall.
2330b2ff885aaed8f9b033b16ca78d645650efc32433Duncan Sands  RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
233105c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands  assert(LC != RTLIB::UNKNOWN_LIBCALL &&
233205c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands         "Don't know how to expand this UINT_TO_FP!");
2333c8fc99d66a03dc603f49d653937ad1d94e833006Dale Johannesen  return MakeLibCall(LC, DstVT, &Op, 1, true, dl);
233405c397d52a145c8844790d6491c4c51d4bbfed7cDuncan Sands}
2335