Several changes:
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAGISel.cpp
index 62bcc32bb4ef653b83894c5377986e1c2bc2caac..270c9a958f22bcca6b3f21e89cd8600f035fafe7 100644 (file)
@@ -212,7 +212,8 @@ namespace llvm {
 /// eh.selector intrinsic.
 static bool isSelector(Instruction *I) {
   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
-    return II->getIntrinsicID() == Intrinsic::eh_selector;
+    return (II->getIntrinsicID() == Intrinsic::eh_selector_i32 ||
+            II->getIntrinsicID() == Intrinsic::eh_selector_i64);
   return false;
 }
 
@@ -260,7 +261,7 @@ FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli,
     if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
       if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
         const Type *Ty = AI->getAllocatedType();
-        uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
+        uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
         unsigned Align = 
           std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
                    AI->getAlignment());
@@ -647,7 +648,15 @@ static SDOperand getCopyFromParts(SelectionDAG &DAG,
   
     if (MVT::isVector(PartVT)) {
       assert(MVT::isVector(ValueVT) && "Unknown vector conversion!");
-      return DAG.getNode(ISD::BIT_CONVERT, PartVT, Val);
+      return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val);
+    }
+  
+    if (MVT::isVector(ValueVT)) {
+      assert(NumParts == 1 &&
+             MVT::getVectorElementType(ValueVT) == PartVT &&
+             MVT::getVectorNumElements(ValueVT) == 1 &&
+             "Only trivial scalar-to-vector conversions should get here!");
+      return DAG.getNode(ISD::BUILD_VECTOR, ValueVT, Val);
     }
   
     if (MVT::isInteger(PartVT) &&
@@ -745,6 +754,13 @@ static void getCopyToParts(SelectionDAG &DAG,
         assert(MVT::isVector(ValueVT) &&
                "Not a vector-vector cast?");
         Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val);
+      } else if (MVT::isVector(ValueVT)) {
+        assert(NumParts == 1 &&
+               MVT::getVectorElementType(ValueVT) == PartVT &&
+               MVT::getVectorNumElements(ValueVT) == 1 &&
+               "Only trivial vector-to-scalar conversions should get here!");
+        Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, PartVT, Val,
+                          DAG.getConstant(0, PtrVT));
       } else if (MVT::isInteger(PartVT) && MVT::isInteger(ValueVT)) {
         if (PartVT < ValueVT)
           Val = DAG.getNode(ISD::TRUNCATE, PartVT, Val);
@@ -840,7 +856,7 @@ SDOperand SelectionDAGLowering::getValue(const Value *V) {
       return N = DAG.getNode(ISD::BUILD_VECTOR, VT,
                              &Ops[0], Ops.size());
     } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
-      return N = DAG.getConstantFP(CFP->getValue(), VT);
+      return N = DAG.getConstantFP(CFP->getValueAPF(), VT);
     } else if (const VectorType *PTy = dyn_cast<VectorType>(VTy)) {
       unsigned NumElements = PTy->getNumElements();
       MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
@@ -1899,7 +1915,7 @@ unsigned SelectionDAGLowering::Clusterify(CaseVector& Cases,
                          SI.getSuccessorValue(i),
                          SMBB));
   }
-  sort(Cases.begin(), Cases.end(), CaseCmp());
+  std::sort(Cases.begin(), Cases.end(), CaseCmp());
 
   // Merge case into clusters
   if (Cases.size()>=2)
@@ -2003,7 +2019,7 @@ void SelectionDAGLowering::visitSub(User &I) {
       const Type *ElTy = DestTy->getElementType();
       if (ElTy->isFloatingPoint()) {
         unsigned VL = DestTy->getNumElements();
-        std::vector<Constant*> NZ(VL, ConstantFP::get(ElTy, -0.0));
+        std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
         Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
         if (CV == CNZ) {
           SDOperand Op2 = getValue(I.getOperand(1));
@@ -2015,7 +2031,7 @@ void SelectionDAGLowering::visitSub(User &I) {
   }
   if (Ty->isFloatingPoint()) {
     if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
-      if (CFP->isExactlyValue(-0.0)) {
+      if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
         SDOperand Op2 = getValue(I.getOperand(1));
         setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
         return;
@@ -2278,13 +2294,13 @@ void SelectionDAGLowering::visitGetElementPtr(User &I) {
       if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
         if (CI->getZExtValue() == 0) continue;
         uint64_t Offs = 
-            TD->getTypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
+            TD->getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
         N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs));
         continue;
       }
       
       // N = N + Idx * ElementSize;
-      uint64_t ElementSize = TD->getTypeSize(Ty);
+      uint64_t ElementSize = TD->getABITypeSize(Ty);
       SDOperand IdxN = getValue(Idx);
 
       // If the index is smaller or larger than intptr_t, truncate or extend
@@ -2319,7 +2335,7 @@ void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
     return;   // getValue will auto-populate this.
 
   const Type *Ty = I.getAllocatedType();
-  uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
+  uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
   unsigned Align =
     std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
              I.getAlignment());
@@ -2687,9 +2703,12 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
     return 0;
   }
 
-  case Intrinsic::eh_selector:{
+  case Intrinsic::eh_selector_i32:
+  case Intrinsic::eh_selector_i64: {
     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
-
+    MVT::ValueType VT = (Intrinsic == Intrinsic::eh_selector_i32 ?
+                         MVT::i32 : MVT::i64);
+    
     if (ExceptionHandling && MMI) {
       if (CurMBB->isLandingPad())
         addCatchInfo(I, MMI, CurMBB);
@@ -2703,7 +2722,7 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
       }
 
       // Insert the EHSELECTION instruction.
-      SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
+      SDVTList VTs = DAG.getVTList(VT, MVT::Other);
       SDOperand Ops[2];
       Ops[0] = getValue(I.getOperand(1));
       Ops[1] = getRoot();
@@ -2711,24 +2730,27 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
       setValue(&I, Op);
       DAG.setRoot(Op.getValue(1));
     } else {
-      setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
+      setValue(&I, DAG.getConstant(0, VT));
     }
     
     return 0;
   }
-  
-  case Intrinsic::eh_typeid_for: {
+
+  case Intrinsic::eh_typeid_for_i32:
+  case Intrinsic::eh_typeid_for_i64: {
     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
+    MVT::ValueType VT = (Intrinsic == Intrinsic::eh_typeid_for_i32 ?
+                         MVT::i32 : MVT::i64);
     
     if (MMI) {
       // Find the type id for the given typeinfo.
       GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
 
       unsigned TypeID = MMI->getTypeIDFor(GV);
-      setValue(&I, DAG.getConstant(TypeID, MVT::i32));
+      setValue(&I, DAG.getConstant(TypeID, VT));
     } else {
       // Return something different to eh_selector.
-      setValue(&I, DAG.getConstant(1, MVT::i32));
+      setValue(&I, DAG.getConstant(1, VT));
     }
 
     return 0;
@@ -2789,19 +2811,33 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
      return 0;
   }
 
-  case Intrinsic::sqrt_f32:
-  case Intrinsic::sqrt_f64:
+  case Intrinsic::sqrt:
     setValue(&I, DAG.getNode(ISD::FSQRT,
                              getValue(I.getOperand(1)).getValueType(),
                              getValue(I.getOperand(1))));
     return 0;
-  case Intrinsic::powi_f32:
-  case Intrinsic::powi_f64:
+  case Intrinsic::powi:
     setValue(&I, DAG.getNode(ISD::FPOWI,
                              getValue(I.getOperand(1)).getValueType(),
                              getValue(I.getOperand(1)),
                              getValue(I.getOperand(2))));
     return 0;
+  case Intrinsic::sin:
+    setValue(&I, DAG.getNode(ISD::FSIN,
+                             getValue(I.getOperand(1)).getValueType(),
+                             getValue(I.getOperand(1))));
+    return 0;
+  case Intrinsic::cos:
+    setValue(&I, DAG.getNode(ISD::FCOS,
+                             getValue(I.getOperand(1)).getValueType(),
+                             getValue(I.getOperand(1))));
+    return 0;
+  case Intrinsic::pow:
+    setValue(&I, DAG.getNode(ISD::FPOW,
+                             getValue(I.getOperand(1)).getValueType(),
+                             getValue(I.getOperand(1)),
+                             getValue(I.getOperand(2))));
+    return 0;
   case Intrinsic::pcmarker: {
     SDOperand Tmp = getValue(I.getOperand(1));
     DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp));
@@ -2873,12 +2909,6 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
     // Discard annotate attributes
     return 0;
 
-  case Intrinsic::adjust_trampoline: {
-    SDOperand Arg = getValue(I.getOperand(1));
-    setValue(&I, DAG.getNode(ISD::ADJUST_TRAMP, TLI.getPointerTy(), Arg));
-    return 0;
-  }
-
   case Intrinsic::init_trampoline: {
     const Function *F =
       cast<Function>(IntrinsicInst::StripPointerCasts(I.getOperand(2)));
@@ -2891,7 +2921,17 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
     Ops[4] = DAG.getSrcValue(I.getOperand(1));
     Ops[5] = DAG.getSrcValue(F);
 
-    DAG.setRoot(DAG.getNode(ISD::TRAMPOLINE, MVT::Other, Ops, 6));
+    SDOperand Tmp = DAG.getNode(ISD::TRAMPOLINE,
+                                DAG.getNodeValueTypes(TLI.getPointerTy(),
+                                                      MVT::Other), 2,
+                                Ops, 6);
+
+    setValue(&I, Tmp);
+    DAG.setRoot(Tmp.getValue(1));
+    return 0;
+  }
+  case Intrinsic::flt_rounds: {
+    setValue(&I, DAG.getNode(ISD::FLT_ROUNDS, MVT::i32));
     return 0;
   }
   }
@@ -2928,7 +2968,7 @@ void SelectionDAGLowering::LowerCallTo(Instruction &I,
     Args.push_back(Entry);
   }
 
-  if (ExceptionHandling && MMI) {
+  if (ExceptionHandling && MMI && LandingPad) {
     // Insert a label before the invoke call to mark the try range.  This can be
     // used to detect deletion of the invoke via the MachineModuleInfo.
     BeginLabel = MMI->NextLabelID();
@@ -2945,7 +2985,7 @@ void SelectionDAGLowering::LowerCallTo(Instruction &I,
     setValue(&I, Result.first);
   DAG.setRoot(Result.second);
 
-  if (ExceptionHandling && MMI) {
+  if (ExceptionHandling && MMI && LandingPad) {
     // Insert a label at the end of the invoke call to mark the try range.  This
     // can be used to detect deletion of the invoke via the MachineModuleInfo.
     EndLabel = MMI->NextLabelID();
@@ -2961,50 +3001,67 @@ void SelectionDAGLowering::LowerCallTo(Instruction &I,
 void SelectionDAGLowering::visitCall(CallInst &I) {
   const char *RenameFn = 0;
   if (Function *F = I.getCalledFunction()) {
-    if (F->isDeclaration())
+    if (F->isDeclaration()) {
       if (unsigned IID = F->getIntrinsicID()) {
         RenameFn = visitIntrinsicCall(I, IID);
         if (!RenameFn)
           return;
-      } else {    // Not an LLVM intrinsic.
-        const std::string &Name = F->getName();
-        if (Name[0] == 'c' && (Name == "copysign" || Name == "copysignf")) {
-          if (I.getNumOperands() == 3 &&   // Basic sanity checks.
-              I.getOperand(1)->getType()->isFloatingPoint() &&
-              I.getType() == I.getOperand(1)->getType() &&
-              I.getType() == I.getOperand(2)->getType()) {
-            SDOperand LHS = getValue(I.getOperand(1));
-            SDOperand RHS = getValue(I.getOperand(2));
-            setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(),
-                                     LHS, RHS));
-            return;
-          }
-        } else if (Name[0] == 'f' && (Name == "fabs" || Name == "fabsf")) {
-          if (I.getNumOperands() == 2 &&   // Basic sanity checks.
-              I.getOperand(1)->getType()->isFloatingPoint() &&
-              I.getType() == I.getOperand(1)->getType()) {
-            SDOperand Tmp = getValue(I.getOperand(1));
-            setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
-            return;
-          }
-        } else if (Name[0] == 's' && (Name == "sin" || Name == "sinf")) {
-          if (I.getNumOperands() == 2 &&   // Basic sanity checks.
-              I.getOperand(1)->getType()->isFloatingPoint() &&
-              I.getType() == I.getOperand(1)->getType()) {
-            SDOperand Tmp = getValue(I.getOperand(1));
-            setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
-            return;
-          }
-        } else if (Name[0] == 'c' && (Name == "cos" || Name == "cosf")) {
-          if (I.getNumOperands() == 2 &&   // Basic sanity checks.
-              I.getOperand(1)->getType()->isFloatingPoint() &&
-              I.getType() == I.getOperand(1)->getType()) {
-            SDOperand Tmp = getValue(I.getOperand(1));
-            setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
-            return;
-          }
+      }
+    }
+
+    // Check for well-known libc/libm calls.  If the function is internal, it
+    // can't be a library call.
+    unsigned NameLen = F->getNameLen();
+    if (!F->hasInternalLinkage() && NameLen) {
+      const char *NameStr = F->getNameStart();
+      if (NameStr[0] == 'c' &&
+          ((NameLen == 8 && !strcmp(NameStr, "copysign")) ||
+           (NameLen == 9 && !strcmp(NameStr, "copysignf")))) {
+        if (I.getNumOperands() == 3 &&   // Basic sanity checks.
+            I.getOperand(1)->getType()->isFloatingPoint() &&
+            I.getType() == I.getOperand(1)->getType() &&
+            I.getType() == I.getOperand(2)->getType()) {
+          SDOperand LHS = getValue(I.getOperand(1));
+          SDOperand RHS = getValue(I.getOperand(2));
+          setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(),
+                                   LHS, RHS));
+          return;
+        }
+      } else if (NameStr[0] == 'f' &&
+                 ((NameLen == 4 && !strcmp(NameStr, "fabs")) ||
+                  (NameLen == 5 && !strcmp(NameStr, "fabsf")) ||
+                  (NameLen == 5 && !strcmp(NameStr, "fabsl")))) {
+        if (I.getNumOperands() == 2 &&   // Basic sanity checks.
+            I.getOperand(1)->getType()->isFloatingPoint() &&
+            I.getType() == I.getOperand(1)->getType()) {
+          SDOperand Tmp = getValue(I.getOperand(1));
+          setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
+          return;
+        }
+      } else if (NameStr[0] == 's' && 
+                 ((NameLen == 3 && !strcmp(NameStr, "sin")) ||
+                  (NameLen == 4 && !strcmp(NameStr, "sinf")) ||
+                  (NameLen == 4 && !strcmp(NameStr, "sinl")))) {
+        if (I.getNumOperands() == 2 &&   // Basic sanity checks.
+            I.getOperand(1)->getType()->isFloatingPoint() &&
+            I.getType() == I.getOperand(1)->getType()) {
+          SDOperand Tmp = getValue(I.getOperand(1));
+          setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
+          return;
+        }
+      } else if (NameStr[0] == 'c' &&
+                 ((NameLen == 3 && !strcmp(NameStr, "cos")) ||
+                  (NameLen == 4 && !strcmp(NameStr, "cosf")) ||
+                  (NameLen == 4 && !strcmp(NameStr, "cosl")))) {
+        if (I.getNumOperands() == 2 &&   // Basic sanity checks.
+            I.getOperand(1)->getType()->isFloatingPoint() &&
+            I.getType() == I.getOperand(1)->getType()) {
+          SDOperand Tmp = getValue(I.getOperand(1));
+          setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
+          return;
         }
       }
+    }
   } else if (isa<InlineAsm>(I.getOperand(0))) {
     visitInlineAsm(I);
     return;
@@ -3436,31 +3493,37 @@ void SelectionDAGLowering::visitInlineAsm(CallInst &I) {
     }
 
     // If this is an input or an indirect output, process the call argument.
+    // BasicBlocks are labels, currently appearing only in asm's.
     if (OpInfo.CallOperandVal) {
-      OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
-      const Type *OpTy = OpInfo.CallOperandVal->getType();
-      // If this is an indirect operand, the operand is a pointer to the
-      // accessed type.
-      if (OpInfo.isIndirect)
-        OpTy = cast<PointerType>(OpTy)->getElementType();
-      
-      // If OpTy is not a first-class value, it may be a struct/union that we
-      // can tile with integers.
-      if (!OpTy->isFirstClassType() && OpTy->isSized()) {
-        unsigned BitSize = TD->getTypeSizeInBits(OpTy);
-        switch (BitSize) {
-        default: break;
-        case 1:
-        case 8:
-        case 16:
-        case 32:
-        case 64:
-          OpTy = IntegerType::get(BitSize);
-          break;
+      if (isa<BasicBlock>(OpInfo.CallOperandVal))
+        OpInfo.CallOperand = 
+          DAG.getBasicBlock(FuncInfo.MBBMap[cast<BasicBlock>(OpInfo.CallOperandVal)]);
+      else {
+        OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
+        const Type *OpTy = OpInfo.CallOperandVal->getType();
+        // If this is an indirect operand, the operand is a pointer to the
+        // accessed type.
+        if (OpInfo.isIndirect)
+          OpTy = cast<PointerType>(OpTy)->getElementType();
+
+        // If OpTy is not a first-class value, it may be a struct/union that we
+        // can tile with integers.
+        if (!OpTy->isFirstClassType() && OpTy->isSized()) {
+          unsigned BitSize = TD->getTypeSizeInBits(OpTy);
+          switch (BitSize) {
+          default: break;
+          case 1:
+          case 8:
+          case 16:
+          case 32:
+          case 64:
+            OpTy = IntegerType::get(BitSize);
+            break;
+          }
         }
+
+        OpVT = TLI.getValueType(OpTy, true);
       }
-      
-      OpVT = TLI.getValueType(OpTy, true);
     }
     
     OpInfo.ConstraintVT = OpVT;
@@ -3493,7 +3556,7 @@ void SelectionDAGLowering::visitInlineAsm(CallInst &I) {
         // Otherwise, create a stack slot and emit a store to it before the
         // asm.
         const Type *Ty = OpVal->getType();
-        uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
+        uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
         unsigned Align  = TLI.getTargetData()->getPrefTypeAlignment(Ty);
         MachineFunction &MF = DAG.getMachineFunction();
         int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align);
@@ -3751,7 +3814,7 @@ void SelectionDAGLowering::visitMalloc(MallocInst &I) {
     Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
 
   // Scale the source by the type size.
-  uint64_t ElementSize = TD->getTypeSize(I.getType()->getElementType());
+  uint64_t ElementSize = TD->getABITypeSize(I.getType()->getElementType());
   Src = DAG.getNode(ISD::MUL, Src.getValueType(),
                     Src, getIntPtrConstant(ElementSize));
 
@@ -3862,8 +3925,9 @@ TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
       Flags |= ISD::ParamFlags::ByVal;
       const PointerType *Ty = cast<PointerType>(I->getType());
       const StructType *STy = cast<StructType>(Ty->getElementType());
-      unsigned StructAlign = Log2_32(getTargetData()->getABITypeAlignment(STy));
-      unsigned StructSize  = getTargetData()->getTypeSize(STy);
+      unsigned StructAlign =
+          Log2_32(getTargetData()->getCallFrameTypeAlignment(STy));
+      unsigned StructSize  = getTargetData()->getABITypeSize(STy);
       Flags |= (StructAlign << ISD::ParamFlags::ByValAlignOffs);
       Flags |= (StructSize  << ISD::ParamFlags::ByValSizeOffs);
     }
@@ -3991,8 +4055,9 @@ TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
       Flags |= ISD::ParamFlags::ByVal;
       const PointerType *Ty = cast<PointerType>(Args[i].Ty);
       const StructType *STy = cast<StructType>(Ty->getElementType());
-      unsigned StructAlign = Log2_32(getTargetData()->getABITypeAlignment(STy));
-      unsigned StructSize  = getTargetData()->getTypeSize(STy);
+      unsigned StructAlign =
+          Log2_32(getTargetData()->getCallFrameTypeAlignment(STy));
+      unsigned StructSize  = getTargetData()->getABITypeSize(STy);
       Flags |= (StructAlign << ISD::ParamFlags::ByValAlignOffs);
       Flags |= (StructSize  << ISD::ParamFlags::ByValSizeOffs);
     }
@@ -4204,7 +4269,7 @@ void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) {
   // If the source and destination are known to not be aliases, we can
   // lower memmove as memcpy.
   if (Op == ISD::MEMMOVE) {
-    uint64_t Size = -1;
+    uint64_t Size = -1ULL;
     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
       Size = C->getValue();
     if (AA.alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
@@ -4280,13 +4345,13 @@ void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) {
                            I.getOperand(1), DstOff);
           } else {
             Value = DAG.getLoad(VT, getRoot(),
-                        getMemBasePlusOffset(Op2, SrcOff, DAG, TLI),
-                        I.getOperand(2), SrcOff);
+                                getMemBasePlusOffset(Op2, SrcOff, DAG, TLI),
+                                I.getOperand(2), SrcOff, false, Align);
             Chain = Value.getValue(1);
             Store =
               DAG.getStore(Chain, Value,
                            getMemBasePlusOffset(Op1, DstOff, DAG, TLI),
-                           I.getOperand(1), DstOff);
+                           I.getOperand(1), DstOff, false, Align);
           }
           OutChains.push_back(Store);
           SrcOff += VTSize;
@@ -4304,7 +4369,22 @@ void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) {
     }
   }
 
-  DAG.setRoot(DAG.getNode(Op, MVT::Other, getRoot(), Op1, Op2, Op3, Op4));
+  SDOperand AlwaysInline = DAG.getConstant(0, MVT::i1);
+  SDOperand Node;
+  switch(Op) {
+    default:
+      assert(0 && "Unknown Op");
+    case ISD::MEMCPY:
+      Node = DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Op4, AlwaysInline);
+      break;
+    case ISD::MEMMOVE:
+      Node = DAG.getMemmove(getRoot(), Op1, Op2, Op3, Op4, AlwaysInline);
+      break;
+    case ISD::MEMSET:
+      Node = DAG.getMemset(getRoot(), Op1, Op2, Op3, Op4, AlwaysInline);
+      break;
+  }
+  DAG.setRoot(Node);
 }
 
 //===----------------------------------------------------------------------===//
@@ -4408,18 +4488,59 @@ LowerArguments(BasicBlock *LLVMBB, SelectionDAGLowering &SDL,
 
 static void copyCatchInfo(BasicBlock *SrcBB, BasicBlock *DestBB,
                           MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) {
-  assert(!FLI.MBBMap[SrcBB]->isLandingPad() &&
-         "Copying catch info out of a landing pad!");
   for (BasicBlock::iterator I = SrcBB->begin(), E = --SrcBB->end(); I != E; ++I)
     if (isSelector(I)) {
       // Apply the catch info to DestBB.
       addCatchInfo(cast<CallInst>(*I), MMI, FLI.MBBMap[DestBB]);
 #ifndef NDEBUG
-      FLI.CatchInfoFound.insert(I);
+      if (!FLI.MBBMap[SrcBB]->isLandingPad())
+        FLI.CatchInfoFound.insert(I);
 #endif
     }
 }
 
+/// CheckDAGForTailCallsAndFixThem - This Function looks for CALL nodes in the
+/// DAG and fixes their tailcall attribute operand.
+static void CheckDAGForTailCallsAndFixThem(SelectionDAG &DAG, 
+                                           TargetLowering& TLI) {
+  SDNode * Ret = NULL;
+  SDOperand Terminator = DAG.getRoot();
+
+  // Find RET node.
+  if (Terminator.getOpcode() == ISD::RET) {
+    Ret = Terminator.Val;
+  }
+  // Fix tail call attribute of CALL nodes.
+  for (SelectionDAG::allnodes_iterator BE = DAG.allnodes_begin(),
+         BI = prior(DAG.allnodes_end()); BI != BE; --BI) {
+    if (BI->getOpcode() == ISD::CALL) {
+      SDOperand OpRet(Ret, 0);
+      SDOperand OpCall(static_cast<SDNode*>(BI), 0);
+      bool isMarkedTailCall = 
+        cast<ConstantSDNode>(OpCall.getOperand(3))->getValue() != 0;
+      // If CALL node has tail call attribute set to true and the call is not
+      // eligible (no RET or the target rejects) the attribute is fixed to
+      // false. The TargetLowering::IsEligibleForTailCallOptimization function
+      // must correctly identify tail call optimizable calls.
+      if (isMarkedTailCall && 
+          (Ret==NULL || 
+           !TLI.IsEligibleForTailCallOptimization(OpCall, OpRet, DAG))) {
+        SmallVector<SDOperand, 32> Ops;
+        unsigned idx=0;
+        for(SDNode::op_iterator I =OpCall.Val->op_begin(), 
+              E=OpCall.Val->op_end(); I!=E; I++, idx++) {
+          if (idx!=3)
+            Ops.push_back(*I);
+          else 
+            Ops.push_back(DAG.getConstant(false, TLI.getPointerTy()));
+        }
+        DAG.UpdateNodeOperands(OpCall, Ops.begin(), Ops.size());
+      }
+    }
+  }
+}
+
 void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
        std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
                                          FunctionLoweringInfo &FuncInfo) {
@@ -4597,17 +4718,30 @@ void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
     
   // Make sure the root of the DAG is up-to-date.
   DAG.setRoot(SDL.getRoot());
+
+  // Check whether calls in this block are real tail calls. Fix up CALL nodes
+  // with correct tailcall attribute so that the target can rely on the tailcall
+  // attribute indicating whether the call is really eligible for tail call
+  // optimization.
+  CheckDAGForTailCallsAndFixThem(DAG, TLI);
 }
 
 void SelectionDAGISel::CodeGenAndEmitDAG(SelectionDAG &DAG) {
+  DOUT << "Lowered selection DAG:\n";
+  DEBUG(DAG.dump());
+
   // Run the DAG combiner in pre-legalize mode.
   DAG.Combine(false, *AA);
   
-  DOUT << "Lowered selection DAG:\n";
+  DOUT << "Optimized lowered selection DAG:\n";
   DEBUG(DAG.dump());
   
   // Second step, hack on the DAG until it only uses operations and types that
   // the target supports.
+#if 0  // Enable this some day.
+  DAG.LegalizeTypes();
+  // Someday even later, enable a dag combine pass here.
+#endif
   DAG.Legalize();
   
   DOUT << "Legalized selection DAG:\n";
@@ -4616,6 +4750,9 @@ void SelectionDAGISel::CodeGenAndEmitDAG(SelectionDAG &DAG) {
   // Run the DAG combiner in post-legalize mode.
   DAG.Combine(true, *AA);
   
+  DOUT << "Optimized legalized selection DAG:\n";
+  DEBUG(DAG.dump());
+
   if (ViewISelDAGs) DAG.viewGraph();
 
   // Third, instruction select all of the operations to machine code, adding the