Make instcombine promote inline asm calls to 'nounwind'
[oota-llvm.git] / lib / Transforms / Scalar / InstructionCombining.cpp
index 2c88279afb092c3fb0fa629bd27a458bdf5707ea..aa9e932fc527379c732c9d203e59c280093e6799 100644 (file)
@@ -2622,6 +2622,7 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
   if (I.getType()->isInteger()) {
     APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
     if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
+      // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
       return BinaryOperator::createUDiv(Op0, Op1, I.getName());
     }
   }      
@@ -2659,7 +2660,7 @@ static Constant *GetFactor(Value *V) {
     if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
       // X & 0xFFF0 is known to be a multiple of 16.
       uint32_t Zeros = RHS->getValue().countTrailingZeros();
-      if (Zeros != V->getType()->getPrimitiveSizeInBits())
+      if (Zeros != V->getType()->getPrimitiveSizeInBits())// don't shift by "32"
         return ConstantExpr::getShl(Result, 
                                     ConstantInt::get(Result->getType(), Zeros));
     }
@@ -2811,6 +2812,7 @@ Instruction *InstCombiner::visitURem(BinaryOperator &I) {
 Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
 
+  // Handle the integer rem common cases
   if (Instruction *common = commonIRemTransforms(I))
     return common;
   
@@ -2823,12 +2825,14 @@ Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
       return &I;
     }
  
-  // If the top bits of both operands are zero (i.e. we can prove they are
+  // If the sign bits of both operands are zero (i.e. we can prove they are
   // unsigned inputs), turn this into a urem.
-  APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
-  if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
-    // X srem Y -> X urem Y, iff X and Y don't have sign bit set
-    return BinaryOperator::createURem(Op0, Op1, I.getName());
+  if (I.getType()->isInteger()) {
+    APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
+    if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
+      // X srem Y -> X urem Y, iff X and Y don't have sign bit set
+      return BinaryOperator::createURem(Op0, Op1, I.getName());
+    }
   }
 
   return 0;
@@ -3459,7 +3463,12 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
             LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE &&
             RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE &&
             LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE &&
-            RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) {
+            RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE &&
+            
+            // Don't try to fold ICMP_SLT + ICMP_ULT.
+            (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) ||
+             ICmpInst::isSignedPredicate(LHSCC) == 
+                 ICmpInst::isSignedPredicate(RHSCC))) {
           // Ensure that the larger constant is on the RHS.
           ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ? 
             ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
@@ -3573,8 +3582,7 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
           case ICmpInst::ICMP_SGT:
             switch (RHSCC) {
             default: assert(0 && "Unknown integer condition code!");
-            case ICmpInst::ICMP_EQ:         // (X s> 13 & X == 15) -> X s> 13
-              return ReplaceInstUsesWith(I, LHS);
+            case ICmpInst::ICMP_EQ:         // (X s> 13 & X == 15) -> X == 15
             case ICmpInst::ICMP_SGT:        // (X s> 13 & X s> 15) -> X s> 15
               return ReplaceInstUsesWith(I, RHS);
             case ICmpInst::ICMP_UGT:        // (X s> 13 & X u> 15) -> no change
@@ -3628,6 +3636,23 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
       }
   }
 
+  // (fcmp ord x, c) & (fcmp ord y, c)  -> (fcmp ord x, y)
+  if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
+    if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) {
+      if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
+          RHS->getPredicate() == FCmpInst::FCMP_ORD)
+        if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
+          if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
+            // If either of the constants are nans, then the whole thing returns
+            // false.
+            if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
+              return ReplaceInstUsesWith(I, ConstantInt::getFalse());
+            return new FCmpInst(FCmpInst::FCMP_ORD, LHS->getOperand(0),
+                                RHS->getOperand(0));
+          }
+    }
+  }
+      
   return Changed ? &I : 0;
 }
 
@@ -4011,6 +4036,10 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
             case ICmpInst::ICMP_EQ:         // (X u< 13 | X == 14) -> no change
               break;
             case ICmpInst::ICMP_UGT:        // (X u< 13 | X u> 15) ->(X-13) u> 2
+              // If RHSCst is [us]MAXINT, it is always false.  Not handling
+              // this can cause overflow.
+              if (RHSCst->isMaxValue(false))
+                return ReplaceInstUsesWith(I, LHS);
               return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, 
                                      false, I);
             case ICmpInst::ICMP_SGT:        // (X u< 13 | X s> 15) -> no change
@@ -4028,6 +4057,10 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
             case ICmpInst::ICMP_EQ:         // (X s< 13 | X == 14) -> no change
               break;
             case ICmpInst::ICMP_SGT:        // (X s< 13 | X s> 15) ->(X-13) s> 2
+              // If RHSCst is [us]MAXINT, it is always false.  Not handling
+              // this can cause overflow.
+              if (RHSCst->isMaxValue(true))
+                return ReplaceInstUsesWith(I, LHS);
               return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, 
                                      false, I);
             case ICmpInst::ICMP_UGT:        // (X s< 13 | X u> 15) -> no change
@@ -4074,7 +4107,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
   }
     
   // fold (or (cast A), (cast B)) -> (cast (or A, B))
-  if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
+  if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
       if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
         const Type *SrcTy = Op0C->getOperand(0)->getType();
@@ -4091,7 +4124,28 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
           return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
         }
       }
-      
+  }
+  
+    
+  // (fcmp uno x, c) | (fcmp uno y, c)  -> (fcmp uno x, y)
+  if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) {
+    if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) {
+      if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
+          RHS->getPredicate() == FCmpInst::FCMP_UNO)
+        if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
+          if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
+            // If either of the constants are nans, then the whole thing returns
+            // true.
+            if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
+              return ReplaceInstUsesWith(I, ConstantInt::getTrue());
+            
+            // Otherwise, no need to compare the two constants, compare the
+            // rest.
+            return new FCmpInst(FCmpInst::FCMP_UNO, LHS->getOperand(0),
+                                RHS->getOperand(0));
+          }
+    }
+  }
 
   return Changed ? &I : 0;
 }
@@ -4341,7 +4395,7 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) {
       return R;
 
   // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
-  if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) 
+  if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
     if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
       if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
         const Type *SrcTy = Op0C->getOperand(0)->getType();
@@ -4358,7 +4412,7 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) {
           return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
         }
       }
-
+  }
   return Changed ? &I : 0;
 }
 
@@ -4392,7 +4446,7 @@ static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) {
 
   for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
     Value *Op = GEP->getOperand(i);
-    uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask;
+    uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask;
     if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) {
       if (OpC->isZero()) continue;
       
@@ -4477,7 +4531,7 @@ Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS,
             return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
           if (C->isNullValue())
             EmitIt = false;
-          else if (TD->getTypeSize(GTI.getIndexedType()) == 0) {
+          else if (TD->getABITypeSize(GTI.getIndexedType()) == 0) {
             EmitIt = false;  // This is indexing into a zero sized array?
           } else if (isa<ConstantInt>(C))
             return ReplaceInstUsesWith(I, // No comparison is needed here.
@@ -5818,7 +5872,22 @@ Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
 }
 
 Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
-  return commonShiftTransforms(I);
+  if (Instruction *R = commonShiftTransforms(I))
+    return R;
+  
+  Value *Op0 = I.getOperand(0);
+  
+  // ashr int -1, X = -1   (for any arithmetic shift rights of ~0)
+  if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
+    if (CSI->isAllOnesValue())
+      return ReplaceInstUsesWith(I, CSI);
+  
+  // See if we can turn a signed shr into an unsigned shr.
+  if (MaskedValueIsZero(Op0, 
+                      APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())))
+    return BinaryOperator::createLShr(Op0, I.getOperand(1));
+  
+  return 0;
 }
 
 Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
@@ -5844,26 +5913,12 @@ Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
       return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
   }
 
-  // ashr int -1, X = -1   (for any arithmetic shift rights of ~0)
-  if (I.getOpcode() == Instruction::AShr)
-    if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
-      if (CSI->isAllOnesValue())
-        return ReplaceInstUsesWith(I, CSI);
-
   // Try to fold constant and into select arguments.
   if (isa<Constant>(Op0))
     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
       if (Instruction *R = FoldOpIntoSelect(I, SI, this))
         return R;
 
-  // See if we can turn a signed shr into an unsigned shr.
-  if (I.isArithmeticShift()) {
-    if (MaskedValueIsZero(Op0, 
-          APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) {
-      return BinaryOperator::createLShr(Op0, Op1, I.getName());
-    }
-  }
-
   if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1))
     if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
       return Res;
@@ -6027,9 +6082,8 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
         // the constant which would cause it to be modified for this
         // operation.
         //
-        if (isValid && !isLeftShift && I.getOpcode() == Instruction::AShr) {
+        if (isValid && I.getOpcode() == Instruction::AShr)
           isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
-        }
         
         if (isValid) {
           Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
@@ -6259,8 +6313,8 @@ Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
   // same, we open the door to infinite loops of various kinds.
   if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0;
 
-  uint64_t AllocElTySize = TD->getTypeSize(AllocElTy);
-  uint64_t CastElTySize = TD->getTypeSize(CastElTy);
+  uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy);
+  uint64_t CastElTySize = TD->getABITypeSize(CastElTy);
   if (CastElTySize == 0 || AllocElTySize == 0) return 0;
 
   // See if we can satisfy the modulus by pulling a scale out of the array
@@ -6527,7 +6581,7 @@ Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
           // is something like [0 x {int, int}]
           const Type *IntPtrTy = TD->getIntPtrType();
           int64_t FirstIdx = 0;
-          if (int64_t TySize = TD->getTypeSize(GEPIdxTy)) {
+          if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) {
             FirstIdx = Offset/TySize;
             Offset %= TySize;
           
@@ -6559,7 +6613,7 @@ Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) {
               }
             } else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) {
               const SequentialType *STy = cast<SequentialType>(GEPIdxTy);
-              if (uint64_t EltSize = TD->getTypeSize(STy->getElementType())) {
+              if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){
                 NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
                 Offset %= EltSize;
               } else {
@@ -7272,6 +7326,13 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
         return BinaryOperator::createOr(NotCond, TrueVal);
       }
     }
+    
+    // select a, b, a  -> a&b
+    // select a, a, b  -> a|b
+    if (CondVal == TrueVal)
+      return BinaryOperator::createOr(CondVal, FalseVal);
+    else if (CondVal == FalseVal)
+      return BinaryOperator::createAnd(CondVal, TrueVal);
   }
 
   // Selecting between two integer constants?
@@ -7532,7 +7593,7 @@ static unsigned GetOrEnforceKnownAlignment(Value *V, TargetData *TD,
                                            unsigned PrefAlign = 0) {
   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
     unsigned Align = GV->getAlignment();
-    if (Align == 0 && TD) 
+    if (Align == 0 && TD && GV->getType()->getElementType()->isSized()
       Align = TD->getPrefTypeAlignment(GV->getType()->getElementType());
 
     // If there is a large requested alignment and we can, bump up the alignment
@@ -7669,43 +7730,23 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
       // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
       // load/store.
       ConstantInt *MemOpLength = dyn_cast<ConstantInt>(CI.getOperand(3));
-      if (isa<MemCpyInst>(MI))
-        if (MemOpLength) {
+      if (MemOpLength) {
         unsigned Size = MemOpLength->getZExtValue();
         unsigned Align = cast<ConstantInt>(CI.getOperand(4))->getZExtValue();
-        const PointerType *PTy = cast<PointerType>(CI.getOperand(1)->getType());
-        const Type *MTy = PTy->getElementType();
         PointerType *NewPtrTy = NULL;
-        if (MTy == Type::Int8Ty) {
-          if (Size == 8)
-            NewPtrTy = PointerType::get(Type::Int64Ty);
-          else if (Size == 4)
-            NewPtrTy = PointerType::get(Type::Int32Ty);
-          else if (Size == 2)
-            NewPtrTy = PointerType::get(Type::Int16Ty);
-          else if (Size == 1)
-            NewPtrTy = PointerType::get(Type::Int8Ty);
-        } else if (MTy == Type::Int16Ty) {
-          if (Size == 4)
-            NewPtrTy = PointerType::get(Type::Int64Ty);
-          else if (Size == 2)
-            NewPtrTy = PointerType::get(Type::Int32Ty);
-          else if (Size == 1)
-            NewPtrTy = PointerType::get(Type::Int16Ty);
-        } else if (MTy == Type::Int32Ty) {
-          if (Size == 2)
-            NewPtrTy = PointerType::get(Type::Int64Ty);
-          else if (Size == 1)
-            NewPtrTy = PointerType::get(Type::Int32Ty);
-        } else if (MTy == Type::Int64Ty) {
-          if (Size == 1)
-            NewPtrTy = PointerType::get(Type::Int64Ty);
-        }
+        // Destination pointer type is always i8 *
+        // If Size is 8 then use Int64Ty
+        // If Size is 4 then use Int32Ty
+        // If Size is 2 then use Int16Ty
+        // If Size is 1 then use Int8Ty
+        if (Size && Size <=8 && !(Size&(Size-1)))
+          NewPtrTy = PointerType::get(IntegerType::get(Size<<3));
+
         if (NewPtrTy) {
-          Value *Src =
-            InsertCastBefore(Instruction::BitCast,CI.getOperand(2),NewPtrTy,CI);
-          Value *Dest = 
-            InsertCastBefore(Instruction::BitCast,CI.getOperand(1),NewPtrTy,CI);
+          Value *Src = InsertCastBefore(Instruction::BitCast, CI.getOperand(2),
+                                        NewPtrTy, CI);
+          Value *Dest = InsertCastBefore(Instruction::BitCast, CI.getOperand(1),
+                                         NewPtrTy, CI);
           Value *L = new LoadInst(Src, "tmp", false, Align, &CI);
           Value *NS = new StoreInst(L, Dest, false, Align, &CI);
           CI.replaceAllUsesWith(NS);
@@ -7931,6 +7972,19 @@ Instruction *InstCombiner::visitCallSite(CallSite CS) {
       }
   }
 
+  if (isa<InlineAsm>(Callee) && !CS.isNoUnwind()) {
+    // Inline asm calls cannot throw - mark them 'nounwind'.
+    const ParamAttrsList *PAL = CS.getParamAttrs();
+    uint16_t RAttributes = PAL ? PAL->getParamAttrs(0) : 0;
+    RAttributes |= ParamAttr::NoUnwind;
+
+    ParamAttrsVector modVec;
+    modVec.push_back(ParamAttrsWithIndex::get(0, RAttributes));
+    PAL = ParamAttrsList::getModified(PAL, modVec);
+    CS.setParamAttrs(PAL);
+    Changed = true;
+  }
+
   return Changed ? CS.getInstruction() : 0;
 }
 
@@ -7953,14 +8007,17 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) {
   const FunctionType *FT = Callee->getFunctionType();
   const Type *OldRetTy = Caller->getType();
 
-  const FunctionType *ActualFT =
-    cast<FunctionType>(cast<PointerType>(CE->getType())->getElementType());
-  
-  // If the parameter attributes don't match up, don't do the xform.  We don't
-  // want to lose an sret attribute or something.
-  if (FT->getParamAttrs() != ActualFT->getParamAttrs())
+  const ParamAttrsList* CallerPAL = 0;
+  if (CallInst *CallerCI = dyn_cast<CallInst>(Caller))
+    CallerPAL = CallerCI->getParamAttrs();
+  else if (InvokeInst *CallerII = dyn_cast<InvokeInst>(Caller))
+    CallerPAL = CallerII->getParamAttrs();
+
+  // If the parameter attributes are not compatible, don't do the xform.  We
+  // don't want to lose an sret attribute or something.
+  if (!ParamAttrsList::areCompatible(CallerPAL, Callee->getParamAttrs()))
     return false;
-  
+
   // Check to see if we are changing the return type...
   if (OldRetTy != FT->getReturnType()) {
     if (Callee->isDeclaration() && !Caller->use_empty() && 
@@ -8093,12 +8150,15 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) {
     NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(),
                         Args.begin(), Args.end(), Caller->getName(), Caller);
     cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
+    cast<InvokeInst>(NC)->setParamAttrs(CallerPAL);
   } else {
     NC = new CallInst(Callee, Args.begin(), Args.end(),
                       Caller->getName(), Caller);
-    if (cast<CallInst>(Caller)->isTailCall())
+    CallInst *CI = cast<CallInst>(Caller);
+    if (CI->isTailCall())
       cast<CallInst>(NC)->setTailCall();
-   cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv());
+    cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
+    cast<CallInst>(NC)->setParamAttrs(CallerPAL);
   }
 
   // Insert a cast of the return type as necessary.
@@ -8149,7 +8209,7 @@ Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
   const PointerType *NestFPTy = cast<PointerType>(NestF->getType());
   const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
 
-  if (const ParamAttrsList *NestAttrs = NestFTy->getParamAttrs()) {
+  if (const ParamAttrsList *NestAttrs = NestF->getParamAttrs()) {
     unsigned NestIdx = 1;
     const Type *NestTy = 0;
     uint16_t NestAttr = 0;
@@ -8197,7 +8257,7 @@ Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
       // Handle this by synthesizing a new function type, equal to FTy
       // with the chain parameter inserted.  Likewise for attributes.
 
-      const ParamAttrsList *Attrs = FTy->getParamAttrs();
+      const ParamAttrsList *Attrs = CS.getParamAttrs();
       std::vector<const Type*> NewTypes;
       ParamAttrsVector NewAttrs;
       NewTypes.reserve(FTy->getNumParams()+1);
@@ -8238,10 +8298,10 @@ Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
       // Replace the trampoline call with a direct call.  Let the generic
       // code sort out any function type mismatches.
       FunctionType *NewFTy =
-        FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg(),
-                          ParamAttrsList::get(NewAttrs));
+        FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg());
       Constant *NewCallee = NestF->getType() == PointerType::get(NewFTy) ?
         NestF : ConstantExpr::getBitCast(NestF, PointerType::get(NewFTy));
+      const ParamAttrsList *NewPAL = ParamAttrsList::get(NewAttrs);
 
       Instruction *NewCaller;
       if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
@@ -8250,6 +8310,7 @@ Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
                                    NewArgs.begin(), NewArgs.end(),
                                    Caller->getName(), Caller);
         cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
+        cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL);
       } else {
         NewCaller = new CallInst(NewCallee, NewArgs.begin(), NewArgs.end(),
                                  Caller->getName(), Caller);
@@ -8257,6 +8318,7 @@ Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
           cast<CallInst>(NewCaller)->setTailCall();
         cast<CallInst>(NewCaller)->
           setCallingConv(cast<CallInst>(Caller)->getCallingConv());
+        cast<CallInst>(NewCaller)->setParamAttrs(NewPAL);
       }
       if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
         Caller->replaceAllUsesWith(NewCaller);
@@ -8521,6 +8583,34 @@ static bool DeadPHICycle(PHINode *PN,
   return false;
 }
 
+/// PHIsEqualValue - Return true if this phi node is always equal to
+/// NonPhiInVal.  This happens with mutually cyclic phi nodes like:
+///   z = some value; x = phi (y, z); y = phi (x, z)
+static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, 
+                           SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) {
+  // See if we already saw this PHI node.
+  if (!ValueEqualPHIs.insert(PN))
+    return true;
+  
+  // Don't scan crazily complex things.
+  if (ValueEqualPHIs.size() == 16)
+    return false;
+  // Scan the operands to see if they are either phi nodes or are equal to
+  // the value.
+  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
+    Value *Op = PN->getIncomingValue(i);
+    if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
+      if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs))
+        return false;
+    } else if (Op != NonPhiInVal)
+      return false;
+  }
+  
+  return true;
+}
+
+
 // PHINode simplification
 //
 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
@@ -8562,6 +8652,40 @@ Instruction *InstCombiner::visitPHINode(PHINode &PN) {
     }
   }
 
+  // We sometimes end up with phi cycles that non-obviously end up being the
+  // same value, for example:
+  //   z = some value; x = phi (y, z); y = phi (x, z)
+  // where the phi nodes don't necessarily need to be in the same block.  Do a
+  // quick check to see if the PHI node only contains a single non-phi value, if
+  // so, scan to see if the phi cycle is actually equal to that value.
+  {
+    unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues();
+    // Scan for the first non-phi operand.
+    while (InValNo != NumOperandVals && 
+           isa<PHINode>(PN.getIncomingValue(InValNo)))
+      ++InValNo;
+
+    if (InValNo != NumOperandVals) {
+      Value *NonPhiInVal = PN.getOperand(InValNo);
+      
+      // Scan the rest of the operands to see if there are any conflicts, if so
+      // there is no need to recursively scan other phis.
+      for (++InValNo; InValNo != NumOperandVals; ++InValNo) {
+        Value *OpVal = PN.getIncomingValue(InValNo);
+        if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
+          break;
+      }
+      
+      // If we scanned over all operands, then we have one unique value plus
+      // phi values.  Scan PHI nodes to see if they all merge in each other or
+      // the value.
+      if (InValNo == NumOperandVals) {
+        SmallPtrSet<PHINode*, 16> ValueEqualPHIs;
+        if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
+          return ReplaceInstUsesWith(PN, NonPhiInVal);
+      }
+    }
+  }
   return 0;
 }
 
@@ -8620,7 +8744,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
       // insert it.  This explicit cast can make subsequent optimizations more
       // obvious.
       Value *Op = GEP.getOperand(i);
-      if (TD->getTypeSize(Op->getType()) > TD->getPointerSize())
+      if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits())
         if (Constant *C = dyn_cast<Constant>(Op)) {
           GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType()));
           MadeChange = true;
@@ -8643,8 +8767,14 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
       // converted to match the type of the cast, don't touch this.
       if (isa<AllocationInst>(BCI->getOperand(0))) {
         // See if the bitcast simplifies, if so, don't nuke this GEP yet.
-        if (visitBitCast(*BCI))
+        if (Instruction *I = visitBitCast(*BCI)) {
+          if (I != BCI) {
+            I->takeName(BCI);
+            BCI->getParent()->getInstList().insert(BCI, I);
+            ReplaceInstUsesWith(*BCI, I);
+          }
           return &GEP;
+        }
       }
       return new BitCastInst(BCI->getOperand(0), GEP.getType());
     }
@@ -8694,12 +8824,12 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
           } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) {
             GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true);
           } else {
-            unsigned PS = TD->getPointerSize();
-            if (TD->getTypeSize(SO1->getType()) == PS) {
+            unsigned PS = TD->getPointerSizeInBits();
+            if (TD->getTypeSizeInBits(SO1->getType()) == PS) {
               // Convert GO1 to SO1's type.
               GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this);
 
-            } else if (TD->getTypeSize(GO1->getType()) == PS) {
+            } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) {
               // Convert SO1 to GO1's type.
               SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this);
             } else {
@@ -8762,8 +8892,8 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
     if (!isa<PointerType>(X->getType())) {
       // Not interesting.  Source pointer must be a cast from pointer.
     } else if (HasZeroPointerIndex) {
-      // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ...
-      // into     : GEP [10 x ubyte]* X, long 0, ...
+      // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
+      // into     : GEP [10 x i8]* X, i32 0, ...
       //
       // This occurs when the program declares an array extern like "int X[];"
       //
@@ -8783,13 +8913,13 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
           }
     } else if (GEP.getNumOperands() == 2) {
       // Transform things like:
-      // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V
-      // into:  %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast
+      // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
+      // into:  %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
       const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
       const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
       if (isa<ArrayType>(SrcElTy) &&
-          TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
-          TD->getTypeSize(ResElTy)) {
+          TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
+          TD->getABITypeSize(ResElTy)) {
         Value *Idx[2];
         Idx[0] = Constant::getNullValue(Type::Int32Ty);
         Idx[1] = GEP.getOperand(1);
@@ -8800,14 +8930,13 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
       }
       
       // Transform things like:
-      // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp
+      // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
       //   (where tmp = 8*tmp2) into:
-      // getelementptr [100 x double]* %arr, int 0, int %tmp.2
+      // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
       
-      if (isa<ArrayType>(SrcElTy) &&
-          (ResElTy == Type::Int8Ty || ResElTy == Type::Int8Ty)) {
+      if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) {
         uint64_t ArrayEltSize =
-            TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType());
+            TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType());
         
         // Check to see if "tmp" is a scale by a multiple of ArrayEltSize.  We
         // allow either a mul, shift, or constant here.
@@ -8832,16 +8961,18 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
             NewIdx = Inst->getOperand(0);
           }
         }
-
+        
         // If the index will be to exactly the right offset with the scale taken
-        // out, perform the transformation.
-        if (Scale && Scale->getZExtValue() % ArrayEltSize == 0) {
-          if (isa<ConstantInt>(Scale))
-            Scale = ConstantInt::get(Scale->getType(),
-                                      Scale->getZExtValue() / ArrayEltSize);
+        // out, perform the transformation. Note, we don't know whether Scale is
+        // signed or not. We'll use unsigned version of division/modulo
+        // operation after making sure Scale doesn't have the sign bit set.
+        if (Scale && Scale->getSExtValue() >= 0LL &&
+            Scale->getZExtValue() % ArrayEltSize == 0) {
+          Scale = ConstantInt::get(Scale->getType(),
+                                   Scale->getZExtValue() / ArrayEltSize);
           if (Scale->getZExtValue() != 1) {
             Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
-                                                       true /*SExt*/);
+                                                       false /*ZExt*/);
             Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale");
             NewIdx = InsertNewInstBefore(Sc, GEP);
           }
@@ -8908,7 +9039,7 @@ Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
   // Note that we only do this for alloca's, because malloc should allocate and
   // return a unique pointer, even for a zero byte allocation.
   if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() &&
-      TD->getTypeSize(AI.getAllocatedType()) == 0)
+      TD->getABITypeSize(AI.getAllocatedType()) == 0)
     return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
 
   return 0;
@@ -8957,10 +9088,43 @@ Instruction *InstCombiner::visitFreeInst(FreeInst &FI) {
 
 
 /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
-static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) {
+static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI,
+                                       const TargetData *TD) {
   User *CI = cast<User>(LI.getOperand(0));
   Value *CastOp = CI->getOperand(0);
 
+  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) {
+    // Instead of loading constant c string, use corresponding integer value
+    // directly if string length is small enough.
+    const std::string &Str = CE->getOperand(0)->getStringValue();
+    if (!Str.empty()) {
+      unsigned len = Str.length();
+      const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
+      unsigned numBits = Ty->getPrimitiveSizeInBits();
+      // Replace LI with immediate integer store.
+      if ((numBits >> 3) == len + 1) {
+       APInt StrVal(numBits, 0);
+       APInt SingleChar(numBits, 0);
+       if (TD->isLittleEndian()) {
+         for (signed i = len-1; i >= 0; i--) {
+           SingleChar = (uint64_t) Str[i];
+           StrVal = (StrVal << 8) | SingleChar;
+         }
+       } else {
+         for (unsigned i = 0; i < len; i++) {
+           SingleChar = (uint64_t) Str[i];
+               StrVal = (StrVal << 8) | SingleChar;
+         }
+         // Append NULL at the end.
+         SingleChar = 0;
+         StrVal = (StrVal << 8) | SingleChar;
+       }
+       Value *NL = ConstantInt::get(StrVal);
+       return IC.ReplaceInstUsesWith(LI, NL);
+      }
+    }
+  }
+
   const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
   if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
     const Type *SrcPTy = SrcTy->getElementType();
@@ -9066,7 +9230,7 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
 
   // load (cast X) --> cast (load X) iff safe
   if (isa<CastInst>(Op))
-    if (Instruction *Res = InstCombineLoadCast(*this, LI))
+    if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
       return Res;
 
   // None of the following transforms are legal for volatile loads.
@@ -9130,7 +9294,7 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
         }
 
       } else if (CE->isCast()) {
-        if (Instruction *Res = InstCombineLoadCast(*this, LI))
+        if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
           return Res;
       }
   }