LegalizeDAG.cpp revision c7af17923e3bb6053f529679ef0be5399d3519ed
13e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
23e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//
33e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//                     The LLVM Compiler Infrastructure
43e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//
53e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner// This file was developed by the LLVM research group and is distributed under
63e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner// the University of Illinois Open Source License. See LICENSE.TXT for details.
73e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//
83e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//===----------------------------------------------------------------------===//
93e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//
103e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner// This file implements the SelectionDAG::Legalize method.
113e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//
123e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//===----------------------------------------------------------------------===//
133e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
143e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner#include "llvm/CodeGen/SelectionDAG.h"
153e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner#include "llvm/CodeGen/MachineConstantPool.h"
163e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner#include "llvm/CodeGen/MachineFunction.h"
173e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner#include "llvm/Target/TargetLowering.h"
183e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner#include "llvm/Constants.h"
193e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner#include <iostream>
203e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattnerusing namespace llvm;
213e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
223e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//===----------------------------------------------------------------------===//
233e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
243e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// hacks on it until the target machine can handle it.  This involves
253e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// eliminating value sizes the machine cannot handle (promoting small sizes to
263e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// large sizes or splitting up large values into small values) as well as
273e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// eliminating operations the machine cannot handle.
283e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner///
293e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// This code also does a small amount of optimization and recognition of idioms
303e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// as part of its processing.  For example, if a target does not support a
313e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
323e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// will attempt merge setcc and brc instructions into brcc's.
333e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner///
343e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattnernamespace {
353e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattnerclass SelectionDAGLegalize {
363e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  TargetLowering &TLI;
373e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SelectionDAG &DAG;
383e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
393e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// LegalizeAction - This enum indicates what action we should take for each
403e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// value type the can occur in the program.
413e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  enum LegalizeAction {
423e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Legal,            // The target natively supports this value type.
433e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Promote,          // This should be promoted to the next larger type.
443e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Expand,           // This integer type should be broken into smaller pieces.
453e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  };
463e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
473e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// TransformToType - For any value types we are promoting or expanding, this
483e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// contains the value type that we are changing to.  For Expanded types, this
493e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// contains one step of the expand (e.g. i64 -> i32), even if there are
503e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// multiple steps required (e.g. i64 -> i16)
513e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
523e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
533e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// ValueTypeActions - This is a bitvector that contains two bits for each
543e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// value type, where the two bits correspond to the LegalizeAction enum.
553e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// This can be queried with "getTypeAction(VT)".
563e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  unsigned ValueTypeActions;
573e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
583e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// NeedsAnotherIteration - This is set when we expand a large integer
593e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// operation into smaller integer operations, but the smaller operations are
603e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// not set.  This occurs only rarely in practice, for targets that don't have
613e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// 32-bit or larger integer registers.
623e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  bool NeedsAnotherIteration;
633e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
643e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// LegalizedNodes - For nodes that are of legal width, and that have more
653e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// than one use, this map indicates what regularized operand to use.  This
663e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// allows us to avoid legalizing the same thing more than once.
673e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  std::map<SDOperand, SDOperand> LegalizedNodes;
683e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
693e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// ExpandedNodes - For nodes that need to be expanded, and which have more
703e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// than one use, this map indicates which which operands are the expanded
713e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// version of the input.  This allows us to avoid expanding the same node
723e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// more than once.
733e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
743e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
753e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// setValueTypeAction - Set the action for a particular value type.  This
763e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// assumes an action has not already been set for this value type.
773e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  void setValueTypeAction(MVT::ValueType VT, LegalizeAction A) {
783e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    ValueTypeActions |= A << (VT*2);
793e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (A == Promote) {
803e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      MVT::ValueType PromoteTo;
813e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      if (VT == MVT::f32)
823e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        PromoteTo = MVT::f64;
833e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      else {
843e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        unsigned LargerReg = VT+1;
853e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) {
863e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          ++LargerReg;
873e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
883e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                 "Nothing to promote to??");
893e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        }
903e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        PromoteTo = (MVT::ValueType)LargerReg;
913e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      }
923e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
933e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
943e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner             MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
953e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner             "Can only promote from int->int or fp->fp!");
963e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      assert(VT < PromoteTo && "Must promote to a larger type!");
973e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      TransformToType[VT] = PromoteTo;
983e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    } else if (A == Expand) {
993e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      assert(MVT::isInteger(VT) && VT > MVT::i8 &&
1003e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner             "Cannot expand this type: target must support SOME integer reg!");
1013e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      // Expand to the next smaller integer type!
1023e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      TransformToType[VT] = (MVT::ValueType)(VT-1);
1033e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
1043e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
1053e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1063e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattnerpublic:
1073e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1083e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SelectionDAGLegalize(TargetLowering &TLI, SelectionDAG &DAG);
1093e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1103e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// Run - While there is still lowering to do, perform a pass over the DAG.
1113e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// Most regularization can be done in a single pass, but targets that require
1123e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// large values to be split into registers multiple times (e.g. i64 -> 4x
1133e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// i16) require iteration for these values (the first iteration will demote
1143e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// to i32, the second will demote to i16).
1153e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  void Run() {
1163e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    do {
1173e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      NeedsAnotherIteration = false;
1183e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      LegalizeDAG();
1193e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    } while (NeedsAnotherIteration);
1203e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
1213e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1223e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// getTypeAction - Return how we should legalize values of this type, either
1233e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// it is already legal or we need to expand it into multiple registers of
1243e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// smaller integer type, or we need to promote it to a larger type.
1253e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  LegalizeAction getTypeAction(MVT::ValueType VT) const {
1263e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
1273e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
1283e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1293e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// isTypeLegal - Return true if this type is legal on this target.
1303e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  ///
1313e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  bool isTypeLegal(MVT::ValueType VT) const {
1323e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    return getTypeAction(VT) == Legal;
1333e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
1343e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1353e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattnerprivate:
1363e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  void LegalizeDAG();
1373e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1383e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SDOperand LegalizeOp(SDOperand O);
1393e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
1403e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1413e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SDOperand getIntPtrConstant(uint64_t Val) {
1423e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    return DAG.getConstant(Val, TLI.getPointerTy());
1433e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
1443e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner};
1453e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner}
1463e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1473e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1483e928bbd6153eb08641cbd0ad7d8487378a8b12aChris LattnerSelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli,
1493e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                                           SelectionDAG &dag)
1503e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  : TLI(tli), DAG(dag), ValueTypeActions(0) {
1513e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1523e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  assert(MVT::LAST_VALUETYPE <= 16 &&
1533e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner         "Too many value types for ValueTypeActions to hold!");
1543e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1553e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // Inspect all of the ValueType's possible, deciding how to process them.
1563e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
1573e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // If TLI says we are expanding this type, expand it!
1583e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (TLI.getNumElements((MVT::ValueType)IntReg) != 1)
1593e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      setValueTypeAction((MVT::ValueType)IntReg, Expand);
1603e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    else if (!TLI.hasNativeSupportFor((MVT::ValueType)IntReg))
1613e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      // Otherwise, if we don't have native support, we must promote to a
1623e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      // larger type.
1633e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      setValueTypeAction((MVT::ValueType)IntReg, Promote);
1643e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1653e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // If the target does not have native support for F32, promote it to F64.
1663e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  if (!TLI.hasNativeSupportFor(MVT::f32))
1673e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    setValueTypeAction(MVT::f32, Promote);
1683e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner}
1693e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1703e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattnervoid SelectionDAGLegalize::LegalizeDAG() {
1713e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SDOperand OldRoot = DAG.getRoot();
1723e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SDOperand NewRoot = LegalizeOp(OldRoot);
1733e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  DAG.setRoot(NewRoot);
1743e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1753e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  ExpandedNodes.clear();
1763e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  LegalizedNodes.clear();
1773e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1783e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // Remove dead nodes now.
17962fd269c146d9023efe32644e44cd97b88631d4fChris Lattner  DAG.RemoveDeadNodes(OldRoot.Val);
1803e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner}
1813e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
1823e928bbd6153eb08641cbd0ad7d8487378a8b12aChris LattnerSDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
1833e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // If this operation defines any values that cannot be represented in a
1843e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // register on this target, make sure to expand it.
1853e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  if (Op.Val->getNumValues() == 1) {// Fast path == assertion only
1863e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    assert(getTypeAction(Op.Val->getValueType(0)) == Legal &&
1873e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner           "For a single use value, caller should check for legality!");
1883e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  } else {
1893e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
1903e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      switch (getTypeAction(Op.Val->getValueType(i))) {
1913e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case Legal: break;  // Nothing to do.
1923e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case Expand: {
1933e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        SDOperand T1, T2;
1943e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        ExpandOp(Op.getValue(i), T1, T2);
1953e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        assert(LegalizedNodes.count(Op) &&
1963e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner               "Expansion didn't add legal operands!");
1973e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        return LegalizedNodes[Op];
1983e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      }
1993e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case Promote:
2003e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        // FIXME: Implement promotion!
2013e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        assert(0 && "Promotion not implemented at all yet!");
2023e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      }
2033e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
2043e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2053e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // If there is more than one use of this, see if we already legalized it.
2063e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // There is no use remembering values that only have a single use, as the map
2073e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // entries will never be reused.
2083e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  if (!Op.Val->hasOneUse()) {
2093e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
2103e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (I != LegalizedNodes.end()) return I->second;
2113e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
2123e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2133e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SDOperand Tmp1, Tmp2;
2143e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2153e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SDOperand Result = Op;
2163e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SDNode *Node = Op.Val;
2173e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  LegalizeAction Action;
2183e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2193e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  switch (Node->getOpcode()) {
2203e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  default:
2213e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2223e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    assert(0 && "Do not know how to legalize this operator!");
2233e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    abort();
2243e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::EntryToken:
2253e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::FrameIndex:
2263e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::GlobalAddress:
22703c0cf822e9a57109d1b4e6a2705d68852c93e1dChris Lattner  case ISD::ExternalSymbol:
2283e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::ConstantPool:
2293e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::CopyFromReg:            // Nothing to do.
2303e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    assert(getTypeAction(Node->getValueType(0)) == Legal &&
2313e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner           "This must be legal!");
2323e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
2333e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::Constant:
2343e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // We know we don't need to expand constants here, constants only have one
2353e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // value and we check that it is fine above.
2363e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2373e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // FIXME: Maybe we should handle things like targets that don't support full
2383e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // 32-bit immediates?
2393e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
2403e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::ConstantFP: {
2413e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // Spill FP immediates to the constant pool if the target cannot directly
2423e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // codegen them.  Targets often have some immediate values that can be
2433e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // efficiently generated into an FP register without a load.  We explicitly
2443e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // leave these constants as ConstantFP nodes for the target to deal with.
2453e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2463e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
2473e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2483e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // Check to see if this FP immediate is already legal.
2493e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    bool isLegal = false;
2503e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
2513e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner           E = TLI.legal_fpimm_end(); I != E; ++I)
2523e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      if (CFP->isExactlyValue(*I)) {
2533e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        isLegal = true;
2543e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        break;
2553e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      }
2563e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2573e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (!isLegal) {
2583e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      // Otherwise we need to spill the constant to memory.
2593e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2603e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2613e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      bool Extend = false;
2623e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2633e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      // If a FP immediate is precise when represented as a float, we put it
2643e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      // into the constant pool as a float, even if it's is statically typed
2653e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      // as a double.
2663e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      MVT::ValueType VT = CFP->getValueType(0);
2673e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      bool isDouble = VT == MVT::f64;
2683e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
2693e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                                             Type::FloatTy, CFP->getValue());
2703e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
2713e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
2723e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        VT = MVT::f32;
2733e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Extend = true;
2743e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      }
2753e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2763e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
2773e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                                            TLI.getPointerTy());
2783e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
2793e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
2803e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      if (Extend) Result = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Result);
2813e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
2823e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
2833e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
2843e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::ADJCALLSTACKDOWN:
2853e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::ADJCALLSTACKUP:
2863e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
2873e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // There is no need to legalize the size argument (Operand #1)
2883e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (Tmp1 != Node->getOperand(0))
2893e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
2903e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                           Node->getOperand(1));
2913e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
2923e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::CALL:
2933e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
2943e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the callee.
295fad71ebe1ebdc8d59c26e4e45a7c7579a2ca58b7Chris Lattner    if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
2963e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      std::vector<MVT::ValueType> RetTyVTs;
2973e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      RetTyVTs.reserve(Node->getNumValues());
2983e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
299ebda942efcb86634a6581aae76a0d0c92c4a232eChris Lattner        RetTyVTs.push_back(Node->getValueType(i));
3003e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), Op.ResNo);
3013e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
3023e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
3033e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
304c7af17923e3bb6053f529679ef0be5399d3519edChris Lattner  case ISD::BR:
305c7af17923e3bb6053f529679ef0be5399d3519edChris Lattner    Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
306c7af17923e3bb6053f529679ef0be5399d3519edChris Lattner    if (Tmp1 != Node->getOperand(0))
307c7af17923e3bb6053f529679ef0be5399d3519edChris Lattner      Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
308c7af17923e3bb6053f529679ef0be5399d3519edChris Lattner    break;
309c7af17923e3bb6053f529679ef0be5399d3519edChris Lattner
310c18ae4cb6a263cf31283c0ef51ace24350f8d72bChris Lattner  case ISD::BRCOND:
311c18ae4cb6a263cf31283c0ef51ace24350f8d72bChris Lattner    Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
312c18ae4cb6a263cf31283c0ef51ace24350f8d72bChris Lattner    // FIXME: booleans might not be legal!
313c18ae4cb6a263cf31283c0ef51ace24350f8d72bChris Lattner    Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the condition.
314c18ae4cb6a263cf31283c0ef51ace24350f8d72bChris Lattner    // Basic block destination (Op#2) is always legal.
315c18ae4cb6a263cf31283c0ef51ace24350f8d72bChris Lattner    if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
316c18ae4cb6a263cf31283c0ef51ace24350f8d72bChris Lattner      Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
317c18ae4cb6a263cf31283c0ef51ace24350f8d72bChris Lattner                           Node->getOperand(2));
318c18ae4cb6a263cf31283c0ef51ace24350f8d72bChris Lattner    break;
319c18ae4cb6a263cf31283c0ef51ace24350f8d72bChris Lattner
3203e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::LOAD:
3213e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
3223e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp2 = LegalizeOp(Node->getOperand(1));  // Legalize the pointer.
3233e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (Tmp1 != Node->getOperand(0) ||
3243e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Tmp2 != Node->getOperand(1))
3253e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
3263e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
3273e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
3283e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::EXTRACT_ELEMENT:
3293e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // Get both the low and high parts.
3303e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3313e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
3323e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = Tmp2;  // 1 -> Hi
3333e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    else
3343e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = Tmp1;  // 0 -> Lo
3353e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
3363e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
3373e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::CopyToReg:
3383e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
3393e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
3403e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    switch (getTypeAction(Node->getOperand(1).getValueType())) {
3413e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case Legal:
3423e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      // Legalize the incoming value (must be legal).
3433e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Tmp2 = LegalizeOp(Node->getOperand(1));
3443e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3453e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Result = DAG.getCopyToReg(Tmp1, Tmp2,
3463e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                                  cast<CopyRegSDNode>(Node)->getReg());
3473e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      break;
3483e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case Expand: {
3493e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      SDOperand Lo, Hi;
3503e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      ExpandOp(Node->getOperand(1), Lo, Hi);
3513e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      unsigned Reg = cast<CopyRegSDNode>(Node)->getReg();
3523e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = DAG.getCopyToReg(Tmp1, Lo, Reg);
3533e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = DAG.getCopyToReg(Result, Hi, Reg+1);
3543e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      assert(isTypeLegal(Result.getValueType()) &&
3553e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner             "Cannot expand multiple times yet (i64 -> i16)");
3563e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      break;
3573e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
3583e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case Promote:
3593e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      assert(0 && "Don't know what it means to promote this!");
3603e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      abort();
3613e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
3623e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
3633e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
3643e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::RET:
3653e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
3663e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    switch (Node->getNumOperands()) {
3673e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case 2:  // ret val
3683e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      switch (getTypeAction(Node->getOperand(1).getValueType())) {
3693e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case Legal:
3703e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Tmp2 = LegalizeOp(Node->getOperand(1));
3713e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        if (Tmp2 != Node->getOperand(1))
3723e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
3733e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        break;
3743e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case Expand: {
3753e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        SDOperand Lo, Hi;
3763e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        ExpandOp(Node->getOperand(1), Lo, Hi);
3773e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
3783e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        break;
3793e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      }
3803e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case Promote:
3813e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        assert(0 && "Can't promote return value!");
3823e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      }
3833e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      break;
3843e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case 1:  // ret void
3853e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      if (Tmp1 != Node->getOperand(0))
3863e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
3873e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      break;
3883e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    default: { // ret <values>
3893e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      std::vector<SDOperand> NewValues;
3903e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      NewValues.push_back(Tmp1);
3913e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
3923e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        switch (getTypeAction(Node->getOperand(i).getValueType())) {
3933e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case Legal:
3943e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          NewValues.push_back(LegalizeOp(Node->getOperand(1)));
3953e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          break;
3963e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case Expand: {
3973e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          SDOperand Lo, Hi;
3983e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          ExpandOp(Node->getOperand(i), Lo, Hi);
3993e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          NewValues.push_back(Lo);
4003e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          NewValues.push_back(Hi);
4013e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          break;
4023e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        }
4033e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case Promote:
4043e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          assert(0 && "Can't promote return value!");
4053e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        }
4063e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
4073e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      break;
4083e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
4093e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
4103e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
4113e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::STORE:
4123e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp1 = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
4133e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp2 = LegalizeOp(Node->getOperand(2));  // Legalize the pointer.
4143e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
4153e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    switch (getTypeAction(Node->getOperand(1).getValueType())) {
4163e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case Legal: {
4173e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      SDOperand Val = LegalizeOp(Node->getOperand(1));
4183e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
4193e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner          Tmp2 != Node->getOperand(2))
4203e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
4213e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      break;
4223e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
4233e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case Promote:
4243e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      assert(0 && "FIXME: promote for stores not implemented!");
4253e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case Expand:
4263e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      SDOperand Lo, Hi;
4273e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      ExpandOp(Node->getOperand(1), Lo, Hi);
4283e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
4293e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      if (!TLI.isLittleEndian())
4303e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        std::swap(Lo, Hi);
4313e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
4323e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      // FIXME: These two stores are independent of each other!
4333e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
4343e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
4353e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      unsigned IncrementSize;
4363e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      switch (Lo.getValueType()) {
4373e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      default: assert(0 && "Unknown ValueType to expand to!");
4383e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case MVT::i32: IncrementSize = 4; break;
4393e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case MVT::i16: IncrementSize = 2; break;
4403e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case MVT::i8:  IncrementSize = 1; break;
4413e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      }
4423e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
4433e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                         getIntPtrConstant(IncrementSize));
4443e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      assert(isTypeLegal(Tmp2.getValueType()) &&
4453e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner             "Pointers must be legal!");
4463e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = DAG.getNode(ISD::STORE, MVT::Other, Result, Hi, Tmp2);
4473e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
4483e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
4493e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::SELECT: {
4503e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // FIXME: BOOLS MAY REQUIRE PROMOTION!
4513e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp1 = LegalizeOp(Node->getOperand(0));   // Cond
4523e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp2 = LegalizeOp(Node->getOperand(1));   // TrueVal
4533e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    SDOperand Tmp3 = LegalizeOp(Node->getOperand(2));   // FalseVal
4543e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
4553e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (Tmp1 != Node->getOperand(0) ||
4563e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Tmp2 != Node->getOperand(1) ||
4573e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Tmp3 != Node->getOperand(2))
4583e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), Tmp1, Tmp2,Tmp3);
4593e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
4603e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
4613e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::SETCC:
4623e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    switch (getTypeAction(Node->getOperand(0).getValueType())) {
4633e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case Legal:
4643e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Tmp1 = LegalizeOp(Node->getOperand(0));   // LHS
4653e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Tmp2 = LegalizeOp(Node->getOperand(1));   // RHS
4663e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
4673e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
4683e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                              Tmp1, Tmp2);
4693e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      break;
4703e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case Promote:
4713e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      assert(0 && "Can't promote setcc operands yet!");
4723e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      break;
4733e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case Expand:
4743e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4753e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
4763e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
4773e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      switch (cast<SetCCSDNode>(Node)->getCondition()) {
4783e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case ISD::SETEQ:
4793e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      case ISD::SETNE:
4803e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4813e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4823e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4833e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), Tmp1,
4843e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                              DAG.getConstant(0, Tmp1.getValueType()));
4853e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        break;
4863e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      default:
4873e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        // FIXME: This generated code sucks.
4883e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        ISD::CondCode LowCC;
4893e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        switch (cast<SetCCSDNode>(Node)->getCondition()) {
4903e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        default: assert(0 && "Unknown integer setcc!");
4913e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case ISD::SETLT:
4923e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case ISD::SETULT: LowCC = ISD::SETULT; break;
4933e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case ISD::SETGT:
4943e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4953e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case ISD::SETLE:
4963e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case ISD::SETULE: LowCC = ISD::SETULE; break;
4973e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case ISD::SETGE:
4983e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4993e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        }
5003e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
5013e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison
5023e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands
5033e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5043e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
5053e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        // NOTE: on targets without efficient SELECT of bools, we can always use
5063e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
5073e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Tmp1 = DAG.getSetCC(LowCC, LHSLo, RHSLo);
5083e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
5093e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                            LHSHi, RHSHi);
5103e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Result = DAG.getSetCC(ISD::SETEQ, LHSHi, RHSHi);
5113e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Result = DAG.getNode(ISD::SELECT, MVT::i1, Result, Tmp1, Tmp2);
5123e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        break;
5133e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      }
5143e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
5153e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
5163e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
5173e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::ADD:
5183e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::SUB:
5193e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::MUL:
5203e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::UDIV:
5213e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::SDIV:
5223e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::UREM:
5233e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::SREM:
5243e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::AND:
5253e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::OR:
5263e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::XOR:
52703c0cf822e9a57109d1b4e6a2705d68852c93e1dChris Lattner  case ISD::SHL:
52803c0cf822e9a57109d1b4e6a2705d68852c93e1dChris Lattner  case ISD::SRL:
52903c0cf822e9a57109d1b4e6a2705d68852c93e1dChris Lattner  case ISD::SRA:
5303e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp1 = LegalizeOp(Node->getOperand(0));   // LHS
5313e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Tmp2 = LegalizeOp(Node->getOperand(1));   // RHS
5323e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (Tmp1 != Node->getOperand(0) ||
5333e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Tmp2 != Node->getOperand(1))
5343e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
5353e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
5363e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::ZERO_EXTEND:
5373e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::SIGN_EXTEND:
5387cc4777a263f6a52877d29201311fde5f6edb632Chris Lattner  case ISD::TRUNCATE:
53903c0cf822e9a57109d1b4e6a2705d68852c93e1dChris Lattner  case ISD::FP_EXTEND:
54003c0cf822e9a57109d1b4e6a2705d68852c93e1dChris Lattner  case ISD::FP_ROUND:
5413e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    switch (getTypeAction(Node->getOperand(0).getValueType())) {
5423e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case Legal:
5433e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Tmp1 = LegalizeOp(Node->getOperand(0));
5443e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      if (Tmp1 != Node->getOperand(0))
5453e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner        Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
5463e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      break;
5473e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    default:
5483e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      assert(0 && "Do not know how to expand or promote this yet!");
5493e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
5503e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
5513e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
5523e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
5533e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  if (!Op.Val->hasOneUse()) {
5543e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    bool isNew = LegalizedNodes.insert(std::make_pair(Op, Result)).second;
5553e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    assert(isNew && "Got into the map somehow?");
5563e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
5573e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
5583e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  return Result;
5593e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner}
5603e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
5613e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
5623e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// ExpandOp - Expand the specified SDOperand into its two component pieces
5633e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// Lo&Hi.  Note that the Op MUST be an expanded type.  As a result of this, the
5643e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// LegalizeNodes map is filled in for any results that are not expanded, the
5653e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// ExpandedNodes map is filled in for any results that are expanded, and the
5663e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner/// Lo/Hi values are returned.
5673e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattnervoid SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5683e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  MVT::ValueType VT = Op.getValueType();
5693e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  MVT::ValueType NVT = TransformToType[VT];
5703e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SDNode *Node = Op.Val;
5713e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  assert(getTypeAction(VT) == Expand && "Not an expanded type!");
5723e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  assert(MVT::isInteger(VT) && "Cannot expand FP values!");
5733e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  assert(MVT::isInteger(NVT) && NVT < VT &&
5743e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner         "Cannot expand to FP value or to larger int value!");
5753e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
5763e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // If there is more than one use of this, see if we already expanded it.
5773e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // There is no use remembering values that only have a single use, as the map
5783e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // entries will never be reused.
5793e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  if (!Node->hasOneUse()) {
5803e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5813e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      = ExpandedNodes.find(Op);
5823e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (I != ExpandedNodes.end()) {
5833e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Lo = I->second.first;
5843e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      Hi = I->second.second;
5853e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      return;
5863e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
5873e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
5883e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
5893e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // If we are lowering to a type that the target doesn't support, we will have
5903e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // to iterate lowering.
5913e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  if (!isTypeLegal(NVT))
5923e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    NeedsAnotherIteration = true;
5933e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
5943e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  LegalizeAction Action;
5953e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  switch (Node->getOpcode()) {
5963e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  default:
5973e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
5983e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    assert(0 && "Do not know how to expand this operator!");
5993e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    abort();
6003e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::Constant: {
6013e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
6023e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Lo = DAG.getConstant(Cst, NVT);
6033e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
6043e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
6053e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
6063e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
6073e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::CopyFromReg: {
6083e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    unsigned Reg = cast<CopyRegSDNode>(Node)->getReg();
6093e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // Aggregate register values are always in consequtive pairs.
6103e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Lo = DAG.getCopyFromReg(Reg, NVT);
6113e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Hi = DAG.getCopyFromReg(Reg+1, NVT);
6123e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
6133e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
6143e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
6153e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
6163e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::LOAD: {
6173e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    SDOperand Ch = LegalizeOp(Node->getOperand(0));   // Legalize the chain.
6183e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    SDOperand Ptr = LegalizeOp(Node->getOperand(1));  // Legalize the pointer.
6193e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Lo = DAG.getLoad(NVT, Ch, Ptr);
6203e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
6213e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // Increment the pointer to the other half.
6223e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    unsigned IncrementSize;
6233e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    switch (Lo.getValueType()) {
6243e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    default: assert(0 && "Unknown ValueType to expand to!");
6253e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case MVT::i32: IncrementSize = 4; break;
6263e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case MVT::i16: IncrementSize = 2; break;
6273e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    case MVT::i8:  IncrementSize = 1; break;
6283e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    }
6293e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6303e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                      getIntPtrConstant(IncrementSize));
6313e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // FIXME: This load is independent of the first one.
6323e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Hi = DAG.getLoad(NVT, Lo.getValue(1), Ptr);
6333e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
6343e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // Remember that we legalized the chain.
6353e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    bool isNew = LegalizedNodes.insert(std::make_pair(Op.getValue(1),
6363e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                                                      Hi.getValue(1))).second;
6373e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    assert(isNew && "This node was already legalized!");
6383e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    if (!TLI.isLittleEndian())
6393e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner      std::swap(Lo, Hi);
6403e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
6413e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
6423e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::CALL: {
6433e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    SDOperand Chain  = LegalizeOp(Node->getOperand(0));  // Legalize the chain.
6443e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    SDOperand Callee = LegalizeOp(Node->getOperand(1));  // Legalize the callee.
6453e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
6463e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
6473e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner           "Can only expand a call once so far, not i64 -> i16!");
6483e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
6493e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    std::vector<MVT::ValueType> RetTyVTs;
6503e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    RetTyVTs.reserve(3);
6513e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    RetTyVTs.push_back(NVT);
6523e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    RetTyVTs.push_back(NVT);
6533e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    RetTyVTs.push_back(MVT::Other);
6543e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee);
6553e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Lo = SDOperand(NC, 0);
6563e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Hi = SDOperand(NC, 1);
6573e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
6583e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // Insert the new chain mapping.
6593e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    bool isNew = LegalizedNodes.insert(std::make_pair(Op.getValue(1),
6603e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                                                      Hi.getValue(2))).second;
6613e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    assert(isNew && "This node was already legalized!");
6623e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
6633e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
6643e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::AND:
6653e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::OR:
6663e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::XOR: {   // Simple logical operators -> two trivial pieces.
6673e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    SDOperand LL, LH, RL, RH;
6683e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    ExpandOp(Node->getOperand(0), LL, LH);
6693e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    ExpandOp(Node->getOperand(1), RL, RH);
6703e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6713e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6723e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
6733e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
6743e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::SELECT: {
6753e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    SDOperand C, LL, LH, RL, RH;
6763e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // FIXME: BOOLS MAY REQUIRE PROMOTION!
6773e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    C = LegalizeOp(Node->getOperand(0));
6783e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    ExpandOp(Node->getOperand(1), LL, LH);
6793e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    ExpandOp(Node->getOperand(2), RL, RH);
6803e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
6813e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
6823e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
6833e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
6843e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::SIGN_EXTEND: {
6853e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // The low part is just a sign extension of the input (which degenerates to
6863e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // a copy).
6873e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
6883e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
6893e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // The high part is obtained by SRA'ing all but one of the bits of the lo
6903e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // part.
6913e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    unsigned SrcSize = MVT::getSizeInBits(Node->getOperand(0).getValueType());
6923e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(SrcSize-1, MVT::i8));
6933e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
6943e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
6953e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  case ISD::ZERO_EXTEND:
6963e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // The low part is just a zero extension of the input (which degenerates to
6973e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // a copy).
6983e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
6993e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
7003e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    // The high part is just a zero.
7013e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    Hi = DAG.getConstant(0, NVT);
7023e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    break;
7033e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
7043e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
7053e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  // Remember in a map if the values will be reused later.
7063e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  if (!Node->hasOneUse()) {
7073e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    bool isNew = ExpandedNodes.insert(std::make_pair(Op,
7083e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner                                            std::make_pair(Lo, Hi))).second;
7093e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner    assert(isNew && "Value already expanded?!?");
7103e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  }
7113e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner}
7123e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
7133e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
7143e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner// SelectionDAG::Legalize - This is the entry point for the file.
7153e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner//
7163e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattnervoid SelectionDAG::Legalize(TargetLowering &TLI) {
7173e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  /// run - This is the main entry point to this class.
7183e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  ///
7193e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner  SelectionDAGLegalize(TLI, *this).Run();
7203e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner}
7213e928bbd6153eb08641cbd0ad7d8487378a8b12aChris Lattner
722