Reverting dtor devirtualization patch.
[oota-llvm.git] / lib / VMCore / Instructions.cpp
index e1af531d649d6c2d42e54217a343cabfe669628c..0df0466112bc797aeeacb2ef1fcad8486687d905 100644 (file)
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
 #include "llvm/Instructions.h"
+#include "llvm/ParameterAttributes.h"
 #include "llvm/Support/CallSite.h"
+#include "llvm/Support/ConstantRange.h"
+#include "llvm/Support/MathExtras.h"
 using namespace llvm;
 
 unsigned CallSite::getCallingConv() const {
@@ -32,7 +35,36 @@ void CallSite::setCallingConv(unsigned CC) {
   else
     cast<InvokeInst>(I)->setCallingConv(CC);
 }
-
+const ParamAttrsList* CallSite::getParamAttrs() const {
+  if (CallInst *CI = dyn_cast<CallInst>(I))
+    return CI->getParamAttrs();
+  else
+    return cast<InvokeInst>(I)->getParamAttrs();
+}
+void CallSite::setParamAttrs(const ParamAttrsList *PAL) {
+  if (CallInst *CI = dyn_cast<CallInst>(I))
+    CI->setParamAttrs(PAL);
+  else
+    cast<InvokeInst>(I)->setParamAttrs(PAL);
+}
+bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
+  if (CallInst *CI = dyn_cast<CallInst>(I))
+    return CI->paramHasAttr(i, attr);
+  else
+    return cast<InvokeInst>(I)->paramHasAttr(i, attr);
+}
+bool CallSite::doesNotAccessMemory() const {
+  if (CallInst *CI = dyn_cast<CallInst>(I))
+    return CI->doesNotAccessMemory();
+  else
+    return cast<InvokeInst>(I)->doesNotAccessMemory();
+}
+bool CallSite::onlyReadsMemory() const {
+  if (CallInst *CI = dyn_cast<CallInst>(I))
+    return CI->onlyReadsMemory();
+  else
+    return cast<InvokeInst>(I)->onlyReadsMemory();
+}
 
 
 
@@ -169,7 +201,7 @@ Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
   if (HasUndefInput && !AllowNonDominatingInstruction)
     if (Instruction *IV = dyn_cast<Instruction>(InVal))
       // If it's in the entry block, it dominates everything.
-      if (IV->getParent() != &IV->getParent()->getParent()->front() ||
+      if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
           isa<InvokeInst>(IV))
         return 0;   // Cannot guarantee that InVal dominates this PHINode.
 
@@ -184,9 +216,12 @@ Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
 
 CallInst::~CallInst() {
   delete [] OperandList;
+  if (ParamAttrs)
+    ParamAttrs->dropRef();
 }
 
 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
+  ParamAttrs = 0;
   NumOperands = NumParams+1;
   Use *OL = OperandList = new Use[NumParams+1];
   OL[0].init(Func, this);
@@ -207,6 +242,7 @@ void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
 }
 
 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
+  ParamAttrs = 0;
   NumOperands = 3;
   Use *OL = OperandList = new Use[3];
   OL[0].init(Func, this);
@@ -229,6 +265,7 @@ void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
 }
 
 void CallInst::init(Value *Func, Value *Actual) {
+  ParamAttrs = 0;
   NumOperands = 2;
   Use *OL = OperandList = new Use[2];
   OL[0].init(Func, this);
@@ -247,6 +284,7 @@ void CallInst::init(Value *Func, Value *Actual) {
 }
 
 void CallInst::init(Value *Func) {
+  ParamAttrs = 0;
   NumOperands = 1;
   Use *OL = OperandList = new Use[1];
   OL[0].init(Func, this);
@@ -258,19 +296,21 @@ void CallInst::init(Value *Func) {
   assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
 }
 
+#if 0
+// Leave for llvm-gcc
 CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
                    const std::string &Name, BasicBlock *InsertAtEnd)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
-                                 ->getElementType())->getReturnType(),
+                                     ->getElementType())->getReturnType(),
                 Instruction::Call, 0, 0, InsertAtEnd) {
   init(Func, Args, NumArgs);
   setName(Name);
 }
 CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
                    const std::string &Name, Instruction *InsertBefore)
-: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
-                                 ->getElementType())->getReturnType(),
-              Instruction::Call, 0, 0, InsertBefore) {
+    : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
+                                     ->getElementType())->getReturnType(),
+                  Instruction::Call, 0, 0, InsertBefore) {
   init(Func, Args, NumArgs);
   setName(Name);
 }
@@ -292,7 +332,7 @@ CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
   init(Func, Actual1, Actual2);
   setName(Name);
 }
-
+#endif
 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
                    Instruction *InsertBefore)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
@@ -310,7 +350,6 @@ CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
   init(Func, Actual);
   setName(Name);
 }
-
 CallInst::CallInst(Value *Func, const std::string &Name,
                    Instruction *InsertBefore)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
@@ -331,7 +370,9 @@ CallInst::CallInst(Value *Func, const std::string &Name,
 
 CallInst::CallInst(const CallInst &CI)
   : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
-                CI.getNumOperands()) {
+                CI.getNumOperands()),
+    ParamAttrs(0) {
+  setParamAttrs(CI.getParamAttrs());
   SubclassData = CI.SubclassData;
   Use *OL = OperandList;
   Use *InOL = CI.OperandList;
@@ -339,6 +380,27 @@ CallInst::CallInst(const CallInst &CI)
     OL[i].init(InOL[i], this);
 }
 
+void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
+  if (ParamAttrs == newAttrs)
+    return;
+
+  if (ParamAttrs)
+    ParamAttrs->dropRef();
+
+  if (newAttrs)
+    newAttrs->addRef();
+
+  ParamAttrs = newAttrs; 
+}
+
+bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
+  if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
+    return true;
+  if (const Function *F = getCalledFunction())
+    return F->paramHasAttr(i, attr);
+  return false;
+}
+
 
 //===----------------------------------------------------------------------===//
 //                        InvokeInst Implementation
@@ -346,10 +408,13 @@ CallInst::CallInst(const CallInst &CI)
 
 InvokeInst::~InvokeInst() {
   delete [] OperandList;
+  if (ParamAttrs)
+    ParamAttrs->dropRef();
 }
 
 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
                       Value* const *Args, unsigned NumArgs) {
+  ParamAttrs = 0;
   NumOperands = 3+NumArgs;
   Use *OL = OperandList = new Use[3+NumArgs];
   OL[0].init(Fn, this);
@@ -372,31 +437,11 @@ void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
   }
 }
 
-InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
-                       BasicBlock *IfException,
-                       Value* const *Args, unsigned NumArgs,
-                       const std::string &Name, Instruction *InsertBefore)
-  : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
-                                    ->getElementType())->getReturnType(),
-                   Instruction::Invoke, 0, 0, InsertBefore) {
-  init(Fn, IfNormal, IfException, Args, NumArgs);
-  setName(Name);
-}
-
-InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
-                       BasicBlock *IfException,
-                       Value* const *Args, unsigned NumArgs,
-                       const std::string &Name, BasicBlock *InsertAtEnd)
-  : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
-                                    ->getElementType())->getReturnType(),
-                   Instruction::Invoke, 0, 0, InsertAtEnd) {
-  init(Fn, IfNormal, IfException, Args, NumArgs);
-  setName(Name);
-}
-
 InvokeInst::InvokeInst(const InvokeInst &II)
   : TerminatorInst(II.getType(), Instruction::Invoke,
-                   new Use[II.getNumOperands()], II.getNumOperands()) {
+                   new Use[II.getNumOperands()], II.getNumOperands()),
+    ParamAttrs(0) {
+  setParamAttrs(II.getParamAttrs());
   SubclassData = II.SubclassData;
   Use *OL = OperandList, *InOL = II.OperandList;
   for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
@@ -413,6 +458,27 @@ void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
   return setSuccessor(idx, B);
 }
 
+void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
+  if (ParamAttrs == newAttrs)
+    return;
+
+  if (ParamAttrs)
+    ParamAttrs->dropRef();
+
+  if (newAttrs)
+    newAttrs->addRef();
+
+  ParamAttrs = newAttrs; 
+}
+
+bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
+  if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
+    return true;
+  if (const Function *F = getCalledFunction())
+    return F->paramHasAttr(i, attr);
+  return false;
+}
+
 
 //===----------------------------------------------------------------------===//
 //                        ReturnInst Implementation
@@ -590,7 +656,7 @@ static Value *getAISize(Value *Amt) {
     Amt = ConstantInt::get(Type::Int32Ty, 1);
   else {
     assert(!isa<BasicBlock>(Amt) &&
-           "Passed basic block into allocation size parameter!  Ue other ctor");
+           "Passed basic block into allocation size parameter! Use other ctor");
     assert(Amt->getType() == Type::Int32Ty &&
            "Malloc/Allocation array size is not a 32-bit integer!");
   }
@@ -622,8 +688,8 @@ AllocationInst::~AllocationInst() {
 }
 
 bool AllocationInst::isArrayAllocation() const {
-  if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0)))
-    return CUI->getZExtValue() != 1;
+  if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
+    return CI->getZExtValue() != 1;
   return true;
 }
 
@@ -674,6 +740,7 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertBef) {
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
   setName(Name);
 }
@@ -682,6 +749,7 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertAE) {
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
   setName(Name);
 }
@@ -691,6 +759,27 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertBef) {
   setVolatile(isVolatile);
+  setAlignment(0);
+  AssertOK();
+  setName(Name);
+}
+
+LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, 
+                   unsigned Align, Instruction *InsertBef)
+  : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
+                     Load, Ptr, InsertBef) {
+  setVolatile(isVolatile);
+  setAlignment(Align);
+  AssertOK();
+  setName(Name);
+}
+
+LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, 
+                   unsigned Align, BasicBlock *InsertAE)
+  : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
+                     Load, Ptr, InsertAE) {
+  setVolatile(isVolatile);
+  setAlignment(Align);
   AssertOK();
   setName(Name);
 }
@@ -700,6 +789,7 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertAE) {
   setVolatile(isVolatile);
+  setAlignment(0);
   AssertOK();
   setName(Name);
 }
@@ -710,6 +800,7 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertBef) {
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
   if (Name && Name[0]) setName(Name);
 }
@@ -718,6 +809,7 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertAE) {
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
   if (Name && Name[0]) setName(Name);
 }
@@ -727,6 +819,7 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                    Load, Ptr, InsertBef) {
   setVolatile(isVolatile);
+  setAlignment(0);
   AssertOK();
   if (Name && Name[0]) setName(Name);
 }
@@ -736,10 +829,15 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertAE) {
   setVolatile(isVolatile);
+  setAlignment(0);
   AssertOK();
   if (Name && Name[0]) setName(Name);
 }
 
+void LoadInst::setAlignment(unsigned Align) {
+  assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
+  SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
+}
 
 //===----------------------------------------------------------------------===//
 //                           StoreInst Implementation
@@ -759,6 +857,7 @@ StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
   Ops[0].init(val, this);
   Ops[1].init(addr, this);
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
 }
 
@@ -767,6 +866,7 @@ StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
   Ops[0].init(val, this);
   Ops[1].init(addr, this);
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
 }
 
@@ -776,6 +876,27 @@ StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
   Ops[0].init(val, this);
   Ops[1].init(addr, this);
   setVolatile(isVolatile);
+  setAlignment(0);
+  AssertOK();
+}
+
+StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
+                     unsigned Align, Instruction *InsertBefore)
+  : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
+  Ops[0].init(val, this);
+  Ops[1].init(addr, this);
+  setVolatile(isVolatile);
+  setAlignment(Align);
+  AssertOK();
+}
+
+StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
+                     unsigned Align, BasicBlock *InsertAtEnd)
+  : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
+  Ops[0].init(val, this);
+  Ops[1].init(addr, this);
+  setVolatile(isVolatile);
+  setAlignment(Align);
   AssertOK();
 }
 
@@ -785,21 +906,19 @@ StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
   Ops[0].init(val, this);
   Ops[1].init(addr, this);
   setVolatile(isVolatile);
+  setAlignment(0);
   AssertOK();
 }
 
+void StoreInst::setAlignment(unsigned Align) {
+  assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
+  SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
+}
+
 //===----------------------------------------------------------------------===//
 //                       GetElementPtrInst Implementation
 //===----------------------------------------------------------------------===//
 
-// checkType - Simple wrapper function to give a better assertion failure
-// message on bad indexes for a gep instruction.
-//
-static inline const Type *checkType(const Type *Ty) {
-  assert(Ty && "Invalid GetElementPtrInst indices for type!");
-  return Ty;
-}
-
 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
   NumOperands = 1+NumIdx;
   Use *OL = OperandList = new Use[NumOperands];
@@ -809,14 +928,6 @@ void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
     OL[i+1].init(Idx[i], this);
 }
 
-void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
-  NumOperands = 3;
-  Use *OL = OperandList = new Use[3];
-  OL[0].init(Ptr, this);
-  OL[1].init(Idx0, this);
-  OL[2].init(Idx1, this);
-}
-
 void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
   NumOperands = 2;
   Use *OL = OperandList = new Use[2];
@@ -824,27 +935,6 @@ void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
   OL[1].init(Idx, this);
 }
 
-
-GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
-                                     unsigned NumIdx,
-                                     const std::string &Name, Instruction *InBe)
-: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
-                                                        Idx, NumIdx, true))),
-              GetElementPtr, 0, 0, InBe) {
-  init(Ptr, Idx, NumIdx);
-  setName(Name);
-}
-
-GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx, 
-                                     unsigned NumIdx,
-                                     const std::string &Name, BasicBlock *IAE)
-: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
-                                                        Idx, NumIdx, true))),
-              GetElementPtr, 0, 0, IAE) {
-  init(Ptr, Idx, NumIdx);
-  setName(Name);
-}
-
 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
                                      const std::string &Name, Instruction *InBe)
   : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
@@ -861,24 +951,6 @@ GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
   setName(Name);
 }
 
-GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
-                                     const std::string &Name, Instruction *InBe)
-  : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
-                                                          Idx0, Idx1, true))),
-                GetElementPtr, 0, 0, InBe) {
-  init(Ptr, Idx0, Idx1);
-  setName(Name);
-}
-
-GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
-                                     const std::string &Name, BasicBlock *IAE)
-  : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
-                                                          Idx0, Idx1, true))),
-                GetElementPtr, 0, 0, IAE) {
-  init(Ptr, Idx0, Idx1);
-  setName(Name);
-}
-
 GetElementPtrInst::~GetElementPtrInst() {
   delete[] OperandList;
 }
@@ -927,34 +999,43 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
   return CurIdx == NumIdx ? Ptr : 0;
 }
 
-const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
-                                              Value *Idx0, Value *Idx1,
-                                              bool AllowCompositeLeaf) {
+const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
   const PointerType *PTy = dyn_cast<PointerType>(Ptr);
   if (!PTy) return 0;   // Type isn't a pointer type!
 
   // Check the pointer index.
-  if (!PTy->indexValid(Idx0)) return 0;
-
-  const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
-  if (!CT || !CT->indexValid(Idx1)) return 0;
+  if (!PTy->indexValid(Idx)) return 0;
 
-  const Type *ElTy = CT->getTypeAtIndex(Idx1);
-  if (AllowCompositeLeaf || ElTy->isFirstClassType())
-    return ElTy;
-  return 0;
+  return PTy->getElementType();
 }
 
-const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
-  const PointerType *PTy = dyn_cast<PointerType>(Ptr);
-  if (!PTy) return 0;   // Type isn't a pointer type!
 
-  // Check the pointer index.
-  if (!PTy->indexValid(Idx)) return 0;
+/// hasAllZeroIndices - Return true if all of the indices of this GEP are
+/// zeros.  If so, the result pointer and the first operand have the same
+/// value, just potentially different types.
+bool GetElementPtrInst::hasAllZeroIndices() const {
+  for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
+    if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
+      if (!CI->isZero()) return false;
+    } else {
+      return false;
+    }
+  }
+  return true;
+}
 
-  return PTy->getElementType();
+/// hasAllConstantIndices - Return true if all of the indices of this GEP are
+/// constant integers.  If so, the result pointer and the first operand have
+/// a constant offset between them.
+bool GetElementPtrInst::hasAllConstantIndices() const {
+  for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
+    if (!isa<ConstantInt>(getOperand(i)))
+      return false;
+  }
+  return true;
 }
 
+
 //===----------------------------------------------------------------------===//
 //                           ExtractElementInst Implementation
 //===----------------------------------------------------------------------===//
@@ -1089,7 +1170,7 @@ bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
     return false;   // First operand of insertelement must be vector type.
   
   if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
-    return false;// Second operand of insertelement must be packed element type.
+    return false;// Second operand of insertelement must be vector element type.
     
   if (Index->getType() != Type::Int32Ty)
     return false;  // Third operand of insertelement must be uint.
@@ -1304,7 +1385,11 @@ BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
 
 // isConstantAllOnes - Helper function for several functions below
 static inline bool isConstantAllOnes(const Value *V) {
-  return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
+  if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
+    return CI->isAllOnesValue();
+  if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
+    return CV->isAllOnesValue();
+  return false;
 }
 
 bool BinaryOperator::isNeg(const Value *V) {
@@ -1400,7 +1485,7 @@ bool CastInst::isLosslessCast() const {
 /// example, the following are all no-op casts:
 /// # bitcast uint %X, int
 /// # bitcast uint* %x, sbyte*
-/// # bitcast packed< 2 x int > %x, packed< 4 x short> 
+/// # bitcast vector< 2 x int > %x, vector< 4 x short> 
 /// # ptrtoint uint* %x, uint     ; on 32-bit plaforms only
 /// @brief Determine if a cast is a no-op.
 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
@@ -1767,8 +1852,8 @@ CastInst::getCastOpcode(
   const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
   // Get the bit sizes, we'll need these
   const Type *SrcTy = Src->getType();
-  unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();   // 0 for ptr/packed
-  unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
+  unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();   // 0 for ptr/vector
+  unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
 
   // Run through the possibilities ...
   if (DestTy->isInteger()) {                       // Casting to integral
@@ -1790,7 +1875,7 @@ CastInst::getCastOpcode(
         return FPToUI;                              // FP -> uint 
     } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
       assert(DestBits == PTy->getBitWidth() &&
-               "Casting packed to integer of different width");
+               "Casting vector to integer of different width");
       return BitCast;                             // Same size, no-op cast
     } else {
       assert(isa<PointerType>(SrcTy) &&
@@ -1813,7 +1898,7 @@ CastInst::getCastOpcode(
       }
     } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
       assert(DestBits == PTy->getBitWidth() &&
-             "Casting packed to floating point of different width");
+             "Casting vector to floating point of different width");
         return BitCast;                             // same size, no-op cast
     } else {
       assert(0 && "Casting pointer or non-first class to float");
@@ -1821,12 +1906,12 @@ CastInst::getCastOpcode(
   } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
     if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
       assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
-             "Casting packed to packed of different widths");
-      return BitCast;                             // packed -> packed
+             "Casting vector to vector of different widths");
+      return BitCast;                             // vector -> vector
     } else if (DestPTy->getBitWidth() == SrcBits) {
-      return BitCast;                               // float/int -> packed
+      return BitCast;                               // float/int -> vector
     } else {
-      assert(!"Illegal cast to packed (wrong type or size)");
+      assert(!"Illegal cast to vector (wrong type or size)");
     }
   } else if (isa<PointerType>(DestTy)) {
     if (isa<PointerType>(SrcTy)) {
@@ -1882,12 +1967,24 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
     return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && 
       SrcBitSize < DstBitSize;
   case Instruction::UIToFP:
-    return SrcTy->isInteger() && DstTy->isFloatingPoint();
   case Instruction::SIToFP:
+    if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
+      if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
+        return SVTy->getElementType()->isInteger() &&
+               DVTy->getElementType()->isFloatingPoint() &&
+               SVTy->getNumElements() == DVTy->getNumElements();
+      }
+    }
     return SrcTy->isInteger() && DstTy->isFloatingPoint();
   case Instruction::FPToUI:
-    return SrcTy->isFloatingPoint() && DstTy->isInteger();
   case Instruction::FPToSI:
+    if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
+      if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
+        return SVTy->getElementType()->isFloatingPoint() &&
+               DVTy->getElementType()->isInteger() &&
+               SVTy->getNumElements() == DVTy->getNumElements();
+      }
+    }
     return SrcTy->isFloatingPoint() && DstTy->isInteger();
   case Instruction::PtrToInt:
     return isa<PointerType>(SrcTy) && DstTy->isInteger();
@@ -2059,6 +2156,7 @@ CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
     Ops[0].init(LHS, this);
     Ops[1].init(RHS, this);
   SubclassData = predicate;
+  setName(Name);
   if (op == Instruction::ICmp) {
     assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
            predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
@@ -2082,7 +2180,6 @@ CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
   // Check that the operands are the right type
   assert(Op0Ty->isFloatingPoint() &&
          "Invalid operand types for FCmp instruction");
-  setName(Name);
 }
   
 CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
@@ -2091,6 +2188,7 @@ CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
   Ops[0].init(LHS, this);
   Ops[1].init(RHS, this);
   SubclassData = predicate;
+  setName(Name);
   if (op == Instruction::ICmp) {
     assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
            predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
@@ -2115,7 +2213,6 @@ CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
   // Check that the operands are the right type
   assert(Op0Ty->isFloatingPoint() &&
         "Invalid operand types for FCmp instruction");
-  setName(Name);
 }
 
 CmpInst *
@@ -2217,6 +2314,41 @@ bool ICmpInst::isSignedPredicate(Predicate pred) {
   }
 }
 
+/// Initialize a set of values that all satisfy the condition with C.
+///
+ConstantRange 
+ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
+  APInt Lower(C);
+  APInt Upper(C);
+  uint32_t BitWidth = C.getBitWidth();
+  switch (pred) {
+  default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
+  case ICmpInst::ICMP_EQ: Upper++; break;
+  case ICmpInst::ICMP_NE: Lower++; break;
+  case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
+  case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
+  case ICmpInst::ICMP_UGT: 
+    Lower++; Upper = APInt::getMinValue(BitWidth);        // Min = Next(Max)
+    break;
+  case ICmpInst::ICMP_SGT:
+    Lower++; Upper = APInt::getSignedMinValue(BitWidth);  // Min = Next(Max)
+    break;
+  case ICmpInst::ICMP_ULE: 
+    Lower = APInt::getMinValue(BitWidth); Upper++; 
+    break;
+  case ICmpInst::ICMP_SLE: 
+    Lower = APInt::getSignedMinValue(BitWidth); Upper++; 
+    break;
+  case ICmpInst::ICMP_UGE:
+    Upper = APInt::getMinValue(BitWidth);        // Min = Next(Max)
+    break;
+  case ICmpInst::ICMP_SGE:
+    Upper = APInt::getSignedMinValue(BitWidth);  // Min = Next(Max)
+    break;
+  }
+  return ConstantRange(Lower, Upper);
+}
+
 FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
   switch (pred) {
     default:
@@ -2435,8 +2567,11 @@ BinaryOperator *BinaryOperator::clone() const {
   return create(getOpcode(), Ops[0], Ops[1]);
 }
 
-CmpInst* CmpInst::clone() const {
-  return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
+FCmpInst* FCmpInst::clone() const {
+  return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
+}
+ICmpInst* ICmpInst::clone() const {
+  return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
 }
 
 MallocInst *MallocInst::clone()   const { return new MallocInst(*this); }