Reverting dtor devirtualization patch.
[oota-llvm.git] / lib / VMCore / Instructions.cpp
index 9eaa8f7e846bdb1b3fa37da3fdad012e78dce64e..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,24 +35,52 @@ 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();
+}
+
 
 
 //===----------------------------------------------------------------------===//
 //                            TerminatorInst Class
 //===----------------------------------------------------------------------===//
 
-TerminatorInst::TerminatorInst(Instruction::TermOps iType,
-                               Use *Ops, unsigned NumOps, Instruction *IB)
-  : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
+// Out of line virtual method, so the vtable, etc has a home.
+TerminatorInst::~TerminatorInst() {
 }
 
-TerminatorInst::TerminatorInst(Instruction::TermOps iType,
-                               Use *Ops, unsigned NumOps, BasicBlock *IAE)
-  : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
+// Out of line virtual method, so the vtable, etc has a home.
+UnaryInstruction::~UnaryInstruction() {
 }
 
 
-
 //===----------------------------------------------------------------------===//
 //                               PHINode Class
 //===----------------------------------------------------------------------===//
@@ -170,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.
 
@@ -185,24 +216,33 @@ Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
 
 CallInst::~CallInst() {
   delete [] OperandList;
+  if (ParamAttrs)
+    ParamAttrs->dropRef();
 }
 
-void CallInst::init(Value *Func, const std::vector<Value*> &Params) {
-  NumOperands = Params.size()+1;
-  Use *OL = OperandList = new Use[Params.size()+1];
+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);
 
   const FunctionType *FTy =
     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
-
-  assert((Params.size() == FTy->getNumParams() ||
-          (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
-         "Calling a function with bad signature");
-  for (unsigned i = 0, e = Params.size(); i != e; ++i)
+  FTy = FTy;  // silence warning.
+
+  assert((NumParams == FTy->getNumParams() ||
+          (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
+         "Calling a function with bad signature!");
+  for (unsigned i = 0; i != NumParams; ++i) {
+    assert((i >= FTy->getNumParams() || 
+            FTy->getParamType(i) == Params[i]->getType()) &&
+           "Calling a function with a bad signature!");
     OL[i+1].init(Params[i], this);
+  }
 }
 
 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
+  ParamAttrs = 0;
   NumOperands = 3;
   Use *OL = OperandList = new Use[3];
   OL[0].init(Func, this);
@@ -211,13 +251,21 @@ void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
 
   const FunctionType *FTy =
     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
+  FTy = FTy;  // silence warning.
 
   assert((FTy->getNumParams() == 2 ||
-          (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
+          (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
          "Calling a function with bad signature");
+  assert((0 >= FTy->getNumParams() || 
+          FTy->getParamType(0) == Actual1->getType()) &&
+         "Calling a function with a bad signature!");
+  assert((1 >= FTy->getNumParams() || 
+          FTy->getParamType(1) == Actual2->getType()) &&
+         "Calling a function with a bad signature!");
 }
 
 void CallInst::init(Value *Func, Value *Actual) {
+  ParamAttrs = 0;
   NumOperands = 2;
   Use *OL = OperandList = new Use[2];
   OL[0].init(Func, this);
@@ -225,90 +273,106 @@ void CallInst::init(Value *Func, Value *Actual) {
 
   const FunctionType *FTy =
     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
+  FTy = FTy;  // silence warning.
 
   assert((FTy->getNumParams() == 1 ||
           (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
          "Calling a function with bad signature");
+  assert((0 == FTy->getNumParams() || 
+          FTy->getParamType(0) == Actual->getType()) &&
+         "Calling a function with a bad signature!");
 }
 
 void CallInst::init(Value *Func) {
+  ParamAttrs = 0;
   NumOperands = 1;
   Use *OL = OperandList = new Use[1];
   OL[0].init(Func, this);
 
-  const FunctionType *MTy =
+  const FunctionType *FTy =
     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
+  FTy = FTy;  // silence warning.
 
-  assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
-}
-
-CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
-                   const std::string &Name, Instruction *InsertBefore)
-  : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
-                                 ->getElementType())->getReturnType(),
-                Instruction::Call, 0, 0, Name, InsertBefore) {
-  init(Func, Params);
+  assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
 }
 
-CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
+#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(),
-                Instruction::Call, 0, 0, Name, InsertAtEnd) {
-  init(Func, Params);
+                                     ->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) {
+  init(Func, Args, NumArgs);
+  setName(Name);
 }
 
 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
                    const std::string &Name, Instruction  *InsertBefore)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
                                    ->getElementType())->getReturnType(),
-                Instruction::Call, 0, 0, Name, InsertBefore) {
+                Instruction::Call, 0, 0, InsertBefore) {
   init(Func, Actual1, Actual2);
+  setName(Name);
 }
 
 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
                    const std::string &Name, BasicBlock  *InsertAtEnd)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
                                    ->getElementType())->getReturnType(),
-                Instruction::Call, 0, 0, Name, InsertAtEnd) {
+                Instruction::Call, 0, 0, InsertAtEnd) {
   init(Func, Actual1, Actual2);
+  setName(Name);
 }
-
+#endif
 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
-                   Instruction  *InsertBefore)
+                   Instruction *InsertBefore)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
                                    ->getElementType())->getReturnType(),
-                Instruction::Call, 0, 0, Name, InsertBefore) {
+                Instruction::Call, 0, 0, InsertBefore) {
   init(Func, Actual);
+  setName(Name);
 }
 
 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
                    BasicBlock  *InsertAtEnd)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
                                    ->getElementType())->getReturnType(),
-                Instruction::Call, 0, 0, Name, InsertAtEnd) {
+                Instruction::Call, 0, 0, InsertAtEnd) {
   init(Func, Actual);
+  setName(Name);
 }
-
 CallInst::CallInst(Value *Func, const std::string &Name,
                    Instruction *InsertBefore)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
                                    ->getElementType())->getReturnType(),
-                Instruction::Call, 0, 0, Name, InsertBefore) {
+                Instruction::Call, 0, 0, InsertBefore) {
   init(Func);
+  setName(Name);
 }
 
 CallInst::CallInst(Value *Func, const std::string &Name,
                    BasicBlock *InsertAtEnd)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
                                    ->getElementType())->getReturnType(),
-                Instruction::Call, 0, 0, Name, InsertAtEnd) {
+                Instruction::Call, 0, 0, InsertAtEnd) {
   init(Func);
+  setName(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;
@@ -316,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
@@ -323,49 +408,40 @@ CallInst::CallInst(const CallInst &CI)
 
 InvokeInst::~InvokeInst() {
   delete [] OperandList;
+  if (ParamAttrs)
+    ParamAttrs->dropRef();
 }
 
 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
-                      const std::vector<Value*> &Params) {
-  NumOperands = 3+Params.size();
-  Use *OL = OperandList = new Use[3+Params.size()];
+                      Value* const *Args, unsigned NumArgs) {
+  ParamAttrs = 0;
+  NumOperands = 3+NumArgs;
+  Use *OL = OperandList = new Use[3+NumArgs];
   OL[0].init(Fn, this);
   OL[1].init(IfNormal, this);
   OL[2].init(IfException, this);
   const FunctionType *FTy =
     cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
+  FTy = FTy;  // silence warning.
 
-  assert((Params.size() == FTy->getNumParams()) ||
-         (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
+  assert((NumArgs == FTy->getNumParams()) ||
+         (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
          "Calling a function with bad signature");
 
-  for (unsigned i = 0, e = Params.size(); i != e; i++)
-    OL[i+3].init(Params[i], this);
-}
-
-InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
-                       BasicBlock *IfException,
-                       const std::vector<Value*> &Params,
-                       const std::string &Name, Instruction *InsertBefore)
-  : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
-                                    ->getElementType())->getReturnType(),
-                   Instruction::Invoke, 0, 0, Name, InsertBefore) {
-  init(Fn, IfNormal, IfException, Params);
-}
-
-InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
-                       BasicBlock *IfException,
-                       const std::vector<Value*> &Params,
-                       const std::string &Name, BasicBlock *InsertAtEnd)
-  : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
-                                    ->getElementType())->getReturnType(),
-                   Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
-  init(Fn, IfNormal, IfException, Params);
+  for (unsigned i = 0, e = NumArgs; i != e; i++) {
+    assert((i >= FTy->getNumParams() || 
+            FTy->getParamType(i) == Args[i]->getType()) &&
+           "Invoking a function with a bad signature!");
+    
+    OL[i+3].init(Args[i], this);
+  }
 }
 
 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)
@@ -382,11 +458,53 @@ 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
 //===----------------------------------------------------------------------===//
 
+ReturnInst::ReturnInst(const ReturnInst &RI)
+  : TerminatorInst(Type::VoidTy, Instruction::Ret,
+                   &RetVal, RI.getNumOperands()) {
+  if (RI.getNumOperands())
+    RetVal.init(RI.RetVal, this);
+}
+
+ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
+  : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
+  init(retVal);
+}
+ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
+  : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
+  init(retVal);
+}
+ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
+  : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
+}
+
+
+
 void ReturnInst::init(Value *retVal) {
   if (retVal && retVal->getType() != Type::VoidTy) {
     assert(!isa<BasicBlock>(retVal) &&
@@ -417,6 +535,14 @@ BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
 //                        UnwindInst Implementation
 //===----------------------------------------------------------------------===//
 
+UnwindInst::UnwindInst(Instruction *InsertBefore)
+  : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
+}
+UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
+  : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
+}
+
+
 unsigned UnwindInst::getNumSuccessorsV() const {
   return getNumSuccessors();
 }
@@ -435,6 +561,13 @@ BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
 //                      UnreachableInst Implementation
 //===----------------------------------------------------------------------===//
 
+UnreachableInst::UnreachableInst(Instruction *InsertBefore)
+  : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
+}
+UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
+  : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
+}
+
 unsigned UnreachableInst::getNumSuccessorsV() const {
   return getNumSuccessors();
 }
@@ -455,12 +588,46 @@ BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
 
 void BranchInst::AssertOK() {
   if (isConditional())
-    assert(getCondition()->getType() == Type::BoolTy &&
+    assert(getCondition()->getType() == Type::Int1Ty &&
            "May only branch on boolean predicates!");
 }
 
+BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
+  : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
+  assert(IfTrue != 0 && "Branch destination may not be null!");
+  Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
+}
+BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
+                       Instruction *InsertBefore)
+: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
+  Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
+  Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
+  Ops[2].init(Cond, this);
+#ifndef NDEBUG
+  AssertOK();
+#endif
+}
+
+BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
+  : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
+  assert(IfTrue != 0 && "Branch destination may not be null!");
+  Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
+}
+
+BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
+           BasicBlock *InsertAtEnd)
+  : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
+  Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
+  Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
+  Ops[2].init(Cond, this);
+#ifndef NDEBUG
+  AssertOK();
+#endif
+}
+
+
 BranchInst::BranchInst(const BranchInst &BI) :
-  TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
+  TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
   OperandList[0].init(BI.getOperand(0), this);
   if (BI.getNumOperands() != 1) {
     assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
@@ -486,10 +653,13 @@ void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
 
 static Value *getAISize(Value *Amt) {
   if (!Amt)
-    Amt = ConstantUInt::get(Type::UIntTy, 1);
-  else
-    assert(Amt->getType() == Type::UIntTy &&
-           "Malloc/Allocation array size != UIntTy!");
+    Amt = ConstantInt::get(Type::Int32Ty, 1);
+  else {
+    assert(!isa<BasicBlock>(Amt) &&
+           "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!");
+  }
   return Amt;
 }
 
@@ -497,23 +667,29 @@ AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
                                unsigned Align, const std::string &Name,
                                Instruction *InsertBefore)
   : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
-                     Name, InsertBefore), Alignment(Align) {
+                     InsertBefore), Alignment(Align) {
   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
   assert(Ty != Type::VoidTy && "Cannot allocate void!");
+  setName(Name);
 }
 
 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
                                unsigned Align, const std::string &Name,
                                BasicBlock *InsertAtEnd)
   : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
-                     Name, InsertAtEnd), Alignment(Align) {
+                     InsertAtEnd), Alignment(Align) {
   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
   assert(Ty != Type::VoidTy && "Cannot allocate void!");
+  setName(Name);
+}
+
+// Out of line virtual method, so the vtable, etc has a home.
+AllocationInst::~AllocationInst() {
 }
 
 bool AllocationInst::isArrayAllocation() const {
-  if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(getOperand(0)))
-    return CUI->getValue() != 1;
+  if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
+    return CI->getZExtValue() != 1;
   return true;
 }
 
@@ -541,12 +717,12 @@ void FreeInst::AssertOK() {
 }
 
 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
-  : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
+  : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
   AssertOK();
 }
 
 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
-  : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
+  : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
   AssertOK();
 }
 
@@ -562,35 +738,107 @@ void LoadInst::AssertOK() {
 
 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
-                     Load, Ptr, Name, InsertBef) {
+                     Load, Ptr, InsertBef) {
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
+  setName(Name);
 }
 
 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
-                     Load, Ptr, Name, InsertAE) {
+                     Load, Ptr, InsertAE) {
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
+  setName(Name);
 }
 
 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
                    Instruction *InsertBef)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
-                     Load, Ptr, Name, InsertBef) {
+                     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);
 }
 
 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
                    BasicBlock *InsertAE)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
-                     Load, Ptr, Name, InsertAE) {
+                     Load, Ptr, InsertAE) {
   setVolatile(isVolatile);
+  setAlignment(0);
   AssertOK();
+  setName(Name);
 }
 
 
+
+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);
+}
+
+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);
+}
+
+LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
+                   Instruction *InsertBef)
+: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
+                   Load, Ptr, InsertBef) {
+  setVolatile(isVolatile);
+  setAlignment(0);
+  AssertOK();
+  if (Name && Name[0]) setName(Name);
+}
+
+LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
+                   BasicBlock *InsertAE)
+  : 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
 //===----------------------------------------------------------------------===//
@@ -605,68 +853,81 @@ void StoreInst::AssertOK() {
 
 
 StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
-  : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
+  : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
   Ops[0].init(val, this);
   Ops[1].init(addr, this);
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
 }
 
 StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
-  : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
+  : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
   Ops[0].init(val, this);
   Ops[1].init(addr, this);
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
 }
 
 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
                      Instruction *InsertBefore)
-  : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
+  : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
   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();
 }
 
 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
                      BasicBlock *InsertAtEnd)
-  : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
+  : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
   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 indices for type!");
-  return Ty;
-}
-
-void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) {
-  NumOperands = 1+Idx.size();
+void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
+  NumOperands = 1+NumIdx;
   Use *OL = OperandList = new Use[NumOperands];
   OL[0].init(Ptr, this);
 
-  for (unsigned i = 0, e = Idx.size(); i != e; ++i)
+  for (unsigned i = 0; i != NumIdx; ++i)
     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];
@@ -674,50 +935,20 @@ void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
   OL[1].init(Idx, this);
 }
 
-GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
-                                     const std::string &Name, Instruction *InBe)
-  : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
-                                                          Idx, true))),
-                GetElementPtr, 0, 0, Name, InBe) {
-  init(Ptr, Idx);
-}
-
-GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
-                                     const std::string &Name, BasicBlock *IAE)
-  : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
-                                                          Idx, true))),
-                GetElementPtr, 0, 0, Name, IAE) {
-  init(Ptr, Idx);
-}
-
 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
                                      const std::string &Name, Instruction *InBe)
   : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
-                GetElementPtr, 0, 0, Name, InBe) {
+                GetElementPtr, 0, 0, InBe) {
   init(Ptr, Idx);
+  setName(Name);
 }
 
 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
                                      const std::string &Name, BasicBlock *IAE)
   : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
-                GetElementPtr, 0, 0, Name, IAE) {
+                GetElementPtr, 0, 0, IAE) {
   init(Ptr, Idx);
-}
-
-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, Name, InBe) {
-  init(Ptr, Idx0, Idx1);
-}
-
-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, Name, IAE) {
-  init(Ptr, Idx0, Idx1);
+  setName(Name);
 }
 
 GetElementPtrInst::~GetElementPtrInst() {
@@ -731,12 +962,13 @@ GetElementPtrInst::~GetElementPtrInst() {
 // pointer type.
 //
 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
-                                              const std::vector<Value*> &Idx,
+                                              Value* const *Idxs,
+                                              unsigned NumIdx,
                                               bool AllowCompositeLeaf) {
   if (!isa<PointerType>(Ptr)) return 0;   // Type isn't a pointer type!
 
   // Handle the special case of the empty set index set...
-  if (Idx.empty())
+  if (NumIdx == 0)
     if (AllowCompositeLeaf ||
         cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
       return cast<PointerType>(Ptr)->getElementType();
@@ -745,12 +977,12 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
 
   unsigned CurIdx = 0;
   while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
-    if (Idx.size() == CurIdx) {
+    if (NumIdx == CurIdx) {
       if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
       return 0;   // Can't load a whole structure or array!?!?
     }
 
-    Value *Index = Idx[CurIdx++];
+    Value *Index = Idxs[CurIdx++];
     if (isa<PointerType>(CT) && CurIdx != 1)
       return 0;  // Can only index into pointer types at the first index!
     if (!CT->indexValid(Index)) return 0;
@@ -764,37 +996,46 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
       Ptr = Ty;
     }
   }
-  return CurIdx == Idx.size() ? Ptr : 0;
+  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
 //===----------------------------------------------------------------------===//
@@ -802,28 +1043,59 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
                                        const std::string &Name,
                                        Instruction *InsertBef)
-  : Instruction(cast<PackedType>(Val->getType())->getElementType(),
-                ExtractElement, Ops, 2, Name, InsertBef) {
+  : Instruction(cast<VectorType>(Val->getType())->getElementType(),
+                ExtractElement, Ops, 2, InsertBef) {
+  assert(isValidOperands(Val, Index) &&
+         "Invalid extractelement instruction operands!");
+  Ops[0].init(Val, this);
+  Ops[1].init(Index, this);
+  setName(Name);
+}
+
+ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
+                                       const std::string &Name,
+                                       Instruction *InsertBef)
+  : Instruction(cast<VectorType>(Val->getType())->getElementType(),
+                ExtractElement, Ops, 2, InsertBef) {
+  Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
   assert(isValidOperands(Val, Index) &&
          "Invalid extractelement instruction operands!");
   Ops[0].init(Val, this);
   Ops[1].init(Index, this);
+  setName(Name);
 }
 
+
 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
                                        const std::string &Name,
                                        BasicBlock *InsertAE)
-  : Instruction(cast<PackedType>(Val->getType())->getElementType(),
-                ExtractElement, Ops, 2, Name, InsertAE) {
+  : Instruction(cast<VectorType>(Val->getType())->getElementType(),
+                ExtractElement, Ops, 2, InsertAE) {
   assert(isValidOperands(Val, Index) &&
          "Invalid extractelement instruction operands!");
 
   Ops[0].init(Val, this);
   Ops[1].init(Index, this);
+  setName(Name);
+}
+
+ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
+                                       const std::string &Name,
+                                       BasicBlock *InsertAE)
+  : Instruction(cast<VectorType>(Val->getType())->getElementType(),
+                ExtractElement, Ops, 2, InsertAE) {
+  Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
+  assert(isValidOperands(Val, Index) &&
+         "Invalid extractelement instruction operands!");
+  
+  Ops[0].init(Val, this);
+  Ops[1].init(Index, this);
+  setName(Name);
 }
 
+
 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
-  if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::UIntTy)
+  if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
     return false;
   return true;
 }
@@ -833,38 +1105,74 @@ bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
 //                           InsertElementInst Implementation
 //===----------------------------------------------------------------------===//
 
+InsertElementInst::InsertElementInst(const InsertElementInst &IE)
+    : Instruction(IE.getType(), InsertElement, Ops, 3) {
+  Ops[0].init(IE.Ops[0], this);
+  Ops[1].init(IE.Ops[1], this);
+  Ops[2].init(IE.Ops[2], this);
+}
 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
                                      const std::string &Name,
                                      Instruction *InsertBef)
-  : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
+  : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
   assert(isValidOperands(Vec, Elt, Index) &&
          "Invalid insertelement instruction operands!");
   Ops[0].init(Vec, this);
   Ops[1].init(Elt, this);
   Ops[2].init(Index, this);
+  setName(Name);
 }
 
+InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
+                                     const std::string &Name,
+                                     Instruction *InsertBef)
+  : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
+  Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
+  assert(isValidOperands(Vec, Elt, Index) &&
+         "Invalid insertelement instruction operands!");
+  Ops[0].init(Vec, this);
+  Ops[1].init(Elt, this);
+  Ops[2].init(Index, this);
+  setName(Name);
+}
+
+
 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
                                      const std::string &Name,
                                      BasicBlock *InsertAE)
-  : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
+  : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
   assert(isValidOperands(Vec, Elt, Index) &&
          "Invalid insertelement instruction operands!");
 
   Ops[0].init(Vec, this);
   Ops[1].init(Elt, this);
   Ops[2].init(Index, this);
+  setName(Name);
+}
+
+InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
+                                     const std::string &Name,
+                                     BasicBlock *InsertAE)
+: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
+  Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
+  assert(isValidOperands(Vec, Elt, Index) &&
+         "Invalid insertelement instruction operands!");
+  
+  Ops[0].init(Vec, this);
+  Ops[1].init(Elt, this);
+  Ops[2].init(Index, this);
+  setName(Name);
 }
 
 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, 
                                         const Value *Index) {
-  if (!isa<PackedType>(Vec->getType()))
-    return false;   // First operand of insertelement must be packed type.
+  if (!isa<VectorType>(Vec->getType()))
+    return false;   // First operand of insertelement must be vector type.
   
-  if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType())
-    return false;// Second operand of insertelement must be packed element type.
+  if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
+    return false;// Second operand of insertelement must be vector element type.
     
-  if (Index->getType() != Type::UIntTy)
+  if (Index->getType() != Type::Int32Ty)
     return false;  // Third operand of insertelement must be uint.
   return true;
 }
@@ -874,37 +1182,46 @@ bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
 //                      ShuffleVectorInst Implementation
 //===----------------------------------------------------------------------===//
 
+ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) 
+    : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
+  Ops[0].init(SV.Ops[0], this);
+  Ops[1].init(SV.Ops[1], this);
+  Ops[2].init(SV.Ops[2], this);
+}
+
 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
                                      const std::string &Name,
                                      Instruction *InsertBefore)
-  : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) {
+  : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
   assert(isValidOperands(V1, V2, Mask) &&
          "Invalid shuffle vector instruction operands!");
   Ops[0].init(V1, this);
   Ops[1].init(V2, this);
   Ops[2].init(Mask, this);
+  setName(Name);
 }
 
 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
                                      const std::string &Name, 
                                      BasicBlock *InsertAtEnd)
-  : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) {
+  : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
   assert(isValidOperands(V1, V2, Mask) &&
          "Invalid shuffle vector instruction operands!");
 
   Ops[0].init(V1, this);
   Ops[1].init(V2, this);
   Ops[2].init(Mask, this);
+  setName(Name);
 }
 
 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, 
                                         const Value *Mask) {
-  if (!isa<PackedType>(V1->getType())) return false;
+  if (!isa<VectorType>(V1->getType())) return false;
   if (V1->getType() != V2->getType()) return false;
-  if (!isa<PackedType>(Mask->getType()) ||
-         cast<PackedType>(Mask->getType())->getElementType() != Type::UIntTy ||
-         cast<PackedType>(Mask->getType())->getNumElements() !=
-         cast<PackedType>(V1->getType())->getNumElements())
+  if (!isa<VectorType>(Mask->getType()) ||
+         cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
+         cast<VectorType>(Mask->getType())->getNumElements() !=
+         cast<VectorType>(V1->getType())->getNumElements())
     return false;
   return true;
 }
@@ -914,34 +1231,89 @@ bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
 //                             BinaryOperator Class
 //===----------------------------------------------------------------------===//
 
-void BinaryOperator::init(BinaryOps iType)
-{
+BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
+                               const Type *Ty, const std::string &Name,
+                               Instruction *InsertBefore)
+  : Instruction(Ty, iType, Ops, 2, InsertBefore) {
+  Ops[0].init(S1, this);
+  Ops[1].init(S2, this);
+  init(iType);
+  setName(Name);
+}
+
+BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 
+                               const Type *Ty, const std::string &Name,
+                               BasicBlock *InsertAtEnd)
+  : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
+  Ops[0].init(S1, this);
+  Ops[1].init(S2, this);
+  init(iType);
+  setName(Name);
+}
+
+
+void BinaryOperator::init(BinaryOps iType) {
   Value *LHS = getOperand(0), *RHS = getOperand(1);
+  LHS = LHS; RHS = RHS; // Silence warnings.
   assert(LHS->getType() == RHS->getType() &&
          "Binary operator operand types must match!");
 #ifndef NDEBUG
   switch (iType) {
   case Add: case Sub:
-  case Mul: case Div:
-  case Rem:
+  case Mul: 
     assert(getType() == LHS->getType() &&
            "Arithmetic operation should return same type as operands!");
     assert((getType()->isInteger() || getType()->isFloatingPoint() ||
-            isa<PackedType>(getType())) &&
+            isa<VectorType>(getType())) &&
           "Tried to create an arithmetic operation on a non-arithmetic type!");
     break;
+  case UDiv: 
+  case SDiv: 
+    assert(getType() == LHS->getType() &&
+           "Arithmetic operation should return same type as operands!");
+    assert((getType()->isInteger() || (isa<VectorType>(getType()) && 
+            cast<VectorType>(getType())->getElementType()->isInteger())) &&
+           "Incorrect operand type (not integer) for S/UDIV");
+    break;
+  case FDiv:
+    assert(getType() == LHS->getType() &&
+           "Arithmetic operation should return same type as operands!");
+    assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
+            cast<VectorType>(getType())->getElementType()->isFloatingPoint())) 
+            && "Incorrect operand type (not floating point) for FDIV");
+    break;
+  case URem: 
+  case SRem: 
+    assert(getType() == LHS->getType() &&
+           "Arithmetic operation should return same type as operands!");
+    assert((getType()->isInteger() || (isa<VectorType>(getType()) && 
+            cast<VectorType>(getType())->getElementType()->isInteger())) &&
+           "Incorrect operand type (not integer) for S/UREM");
+    break;
+  case FRem:
+    assert(getType() == LHS->getType() &&
+           "Arithmetic operation should return same type as operands!");
+    assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
+            cast<VectorType>(getType())->getElementType()->isFloatingPoint())) 
+            && "Incorrect operand type (not floating point) for FREM");
+    break;
+  case Shl:
+  case LShr:
+  case AShr:
+    assert(getType() == LHS->getType() &&
+           "Shift operation should return same type as operands!");
+    assert(getType()->isInteger() && 
+           "Shift operation requires integer operands");
+    break;
   case And: case Or:
   case Xor:
     assert(getType() == LHS->getType() &&
            "Logical operation should return same type as operands!");
-    assert((getType()->isIntegral() ||
-            (isa<PackedType>(getType()) && 
-             cast<PackedType>(getType())->getElementType()->isIntegral())) &&
+    assert((getType()->isInteger() ||
+            (isa<VectorType>(getType()) && 
+             cast<VectorType>(getType())->getElementType()->isInteger())) &&
            "Tried to create a logical operation on a non-integral type!");
     break;
-  case SetLT: case SetGT: case SetLE:
-  case SetGE: case SetEQ: case SetNE:
-    assert(getType() == Type::BoolTy && "Setcc must return bool!");
   default:
     break;
   }
@@ -953,15 +1325,7 @@ BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
                                        Instruction *InsertBefore) {
   assert(S1->getType() == S2->getType() &&
          "Cannot create binary operator with two operands of differing type!");
-  switch (Op) {
-  // Binary comparison operators...
-  case SetLT: case SetGT: case SetLE:
-  case SetGE: case SetEQ: case SetNE:
-    return new SetCondInst(Op, S1, S2, Name, InsertBefore);
-
-  default:
-    return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
-  }
+  return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
 }
 
 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
@@ -974,36 +1338,28 @@ BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
 
 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
                                           Instruction *InsertBefore) {
-  if (!Op->getType()->isFloatingPoint())
-    return new BinaryOperator(Instruction::Sub,
-                              Constant::getNullValue(Op->getType()), Op,
-                              Op->getType(), Name, InsertBefore);
-  else
-    return new BinaryOperator(Instruction::Sub,
-                              ConstantFP::get(Op->getType(), -0.0), Op,
-                              Op->getType(), Name, InsertBefore);
+  Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
+  return new BinaryOperator(Instruction::Sub,
+                            zero, Op,
+                            Op->getType(), Name, InsertBefore);
 }
 
 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
                                           BasicBlock *InsertAtEnd) {
-  if (!Op->getType()->isFloatingPoint())
-    return new BinaryOperator(Instruction::Sub,
-                              Constant::getNullValue(Op->getType()), Op,
-                              Op->getType(), Name, InsertAtEnd);
-  else
-    return new BinaryOperator(Instruction::Sub,
-                              ConstantFP::get(Op->getType(), -0.0), Op,
-                              Op->getType(), Name, InsertAtEnd);
+  Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
+  return new BinaryOperator(Instruction::Sub,
+                            zero, Op,
+                            Op->getType(), Name, InsertAtEnd);
 }
 
 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
                                           Instruction *InsertBefore) {
   Constant *C;
-  if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
-    C = ConstantIntegral::getAllOnesValue(PTy->getElementType());
-    C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C));
+  if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
+    C = ConstantInt::getAllOnesValue(PTy->getElementType());
+    C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
   } else {
-    C = ConstantIntegral::getAllOnesValue(Op->getType());
+    C = ConstantInt::getAllOnesValue(Op->getType());
   }
   
   return new BinaryOperator(Instruction::Xor, Op, C,
@@ -1013,13 +1369,13 @@ BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
                                           BasicBlock *InsertAtEnd) {
   Constant *AllOnes;
-  if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
+  if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
     // Create a vector of all ones values.
-    Constant *Elt = ConstantIntegral::getAllOnesValue(PTy->getElementType());
+    Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
     AllOnes = 
-      ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
+      ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
   } else {
-    AllOnes = ConstantIntegral::getAllOnesValue(Op->getType());
+    AllOnes = ConstantInt::getAllOnesValue(Op->getType());
   }
   
   return new BinaryOperator(Instruction::Xor, Op, AllOnes,
@@ -1029,16 +1385,18 @@ 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<ConstantIntegral>(V) &&cast<ConstantIntegral>(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) {
   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
     if (Bop->getOpcode() == Instruction::Sub)
-      if (!V->getType()->isFloatingPoint())
-        return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
-      else
-        return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
+      return Bop->getOperand(0) ==
+             ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
   return false;
 }
 
@@ -1081,67 +1439,989 @@ const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
 // order dependent (SetLT f.e.) the opcode is changed.
 //
 bool BinaryOperator::swapOperands() {
-  if (isCommutative())
-    ;  // If the instruction is commutative, it is safe to swap the operands
-  else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
-    /// FIXME: SetCC instructions shouldn't all have different opcodes.
-    setOpcode(SCI->getSwappedCondition());
-  else
-    return true;   // Can't commute operands
-
+  if (!isCommutative())
+    return true; // Can't commute operands
   std::swap(Ops[0], Ops[1]);
   return false;
 }
 
+//===----------------------------------------------------------------------===//
+//                                CastInst Class
+//===----------------------------------------------------------------------===//
+
+// Just determine if this cast only deals with integral->integral conversion.
+bool CastInst::isIntegerCast() const {
+  switch (getOpcode()) {
+    default: return false;
+    case Instruction::ZExt:
+    case Instruction::SExt:
+    case Instruction::Trunc:
+      return true;
+    case Instruction::BitCast:
+      return getOperand(0)->getType()->isInteger() && getType()->isInteger();
+  }
+}
+
+bool CastInst::isLosslessCast() const {
+  // Only BitCast can be lossless, exit fast if we're not BitCast
+  if (getOpcode() != Instruction::BitCast)
+    return false;
+
+  // Identity cast is always lossless
+  const Type* SrcTy = getOperand(0)->getType();
+  const Type* DstTy = getType();
+  if (SrcTy == DstTy)
+    return true;
+  
+  // Pointer to pointer is always lossless.
+  if (isa<PointerType>(SrcTy))
+    return isa<PointerType>(DstTy);
+  return false;  // Other types have no identity values
+}
+
+/// This function determines if the CastInst does not require any bits to be
+/// changed in order to effect the cast. Essentially, it identifies cases where
+/// no code gen is necessary for the cast, hence the name no-op cast.  For 
+/// example, the following are all no-op casts:
+/// # bitcast uint %X, int
+/// # bitcast uint* %x, sbyte*
+/// # 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 {
+  switch (getOpcode()) {
+    default:
+      assert(!"Invalid CastOp");
+    case Instruction::Trunc:
+    case Instruction::ZExt:
+    case Instruction::SExt: 
+    case Instruction::FPTrunc:
+    case Instruction::FPExt:
+    case Instruction::UIToFP:
+    case Instruction::SIToFP:
+    case Instruction::FPToUI:
+    case Instruction::FPToSI:
+      return false; // These always modify bits
+    case Instruction::BitCast:
+      return true;  // BitCast never modifies bits.
+    case Instruction::PtrToInt:
+      return IntPtrTy->getPrimitiveSizeInBits() ==
+            getType()->getPrimitiveSizeInBits();
+    case Instruction::IntToPtr:
+      return IntPtrTy->getPrimitiveSizeInBits() ==
+             getOperand(0)->getType()->getPrimitiveSizeInBits();
+  }
+}
+
+/// This function determines if a pair of casts can be eliminated and what 
+/// opcode should be used in the elimination. This assumes that there are two 
+/// instructions like this:
+/// *  %F = firstOpcode SrcTy %x to MidTy
+/// *  %S = secondOpcode MidTy %F to DstTy
+/// The function returns a resultOpcode so these two casts can be replaced with:
+/// *  %Replacement = resultOpcode %SrcTy %x to DstTy
+/// If no such cast is permited, the function returns 0.
+unsigned CastInst::isEliminableCastPair(
+  Instruction::CastOps firstOp, Instruction::CastOps secondOp,
+  const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
+{
+  // Define the 144 possibilities for these two cast instructions. The values
+  // in this matrix determine what to do in a given situation and select the
+  // case in the switch below.  The rows correspond to firstOp, the columns 
+  // correspond to secondOp.  In looking at the table below, keep in  mind
+  // the following cast properties:
+  //
+  //          Size Compare       Source               Destination
+  // Operator  Src ? Size   Type       Sign         Type       Sign
+  // -------- ------------ -------------------   ---------------------
+  // TRUNC         >       Integer      Any        Integral     Any
+  // ZEXT          <       Integral   Unsigned     Integer      Any
+  // SEXT          <       Integral    Signed      Integer      Any
+  // FPTOUI       n/a      FloatPt      n/a        Integral   Unsigned
+  // FPTOSI       n/a      FloatPt      n/a        Integral    Signed 
+  // UITOFP       n/a      Integral   Unsigned     FloatPt      n/a   
+  // SITOFP       n/a      Integral    Signed      FloatPt      n/a   
+  // FPTRUNC       >       FloatPt      n/a        FloatPt      n/a   
+  // FPEXT         <       FloatPt      n/a        FloatPt      n/a   
+  // PTRTOINT     n/a      Pointer      n/a        Integral   Unsigned
+  // INTTOPTR     n/a      Integral   Unsigned     Pointer      n/a
+  // BITCONVERT    =       FirstClass   n/a       FirstClass    n/a   
+  //
+  // NOTE: some transforms are safe, but we consider them to be non-profitable.
+  // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
+  // into "fptoui double to ulong", but this loses information about the range
+  // of the produced value (we no longer know the top-part is all zeros). 
+  // Further this conversion is often much more expensive for typical hardware,
+  // and causes issues when building libgcc.  We disallow fptosi+sext for the 
+  // same reason.
+  const unsigned numCastOps = 
+    Instruction::CastOpsEnd - Instruction::CastOpsBegin;
+  static const uint8_t CastResults[numCastOps][numCastOps] = {
+    // T        F  F  U  S  F  F  P  I  B   -+
+    // R  Z  S  P  P  I  I  T  P  2  N  T    |
+    // U  E  E  2  2  2  2  R  E  I  T  C    +- secondOp
+    // N  X  X  U  S  F  F  N  X  N  2  V    |
+    // C  T  T  I  I  P  P  C  T  T  P  T   -+
+    {  1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc      -+
+    {  8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt        |
+    {  8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt        |
+    {  0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI      |
+    {  0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI      |
+    { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP      +- firstOp
+    { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP      |
+    { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc     |
+    { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt       |
+    {  1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt    |
+    { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr    |
+    {  5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast    -+
+  };
+
+  int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
+                            [secondOp-Instruction::CastOpsBegin];
+  switch (ElimCase) {
+    case 0: 
+      // categorically disallowed
+      return 0;
+    case 1: 
+      // allowed, use first cast's opcode
+      return firstOp;
+    case 2: 
+      // allowed, use second cast's opcode
+      return secondOp;
+    case 3: 
+      // no-op cast in second op implies firstOp as long as the DestTy 
+      // is integer
+      if (DstTy->isInteger())
+        return firstOp;
+      return 0;
+    case 4:
+      // no-op cast in second op implies firstOp as long as the DestTy
+      // is floating point
+      if (DstTy->isFloatingPoint())
+        return firstOp;
+      return 0;
+    case 5: 
+      // no-op cast in first op implies secondOp as long as the SrcTy
+      // is an integer
+      if (SrcTy->isInteger())
+        return secondOp;
+      return 0;
+    case 6:
+      // no-op cast in first op implies secondOp as long as the SrcTy
+      // is a floating point
+      if (SrcTy->isFloatingPoint())
+        return secondOp;
+      return 0;
+    case 7: { 
+      // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
+      unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
+      unsigned MidSize = MidTy->getPrimitiveSizeInBits();
+      if (MidSize >= PtrSize)
+        return Instruction::BitCast;
+      return 0;
+    }
+    case 8: {
+      // ext, trunc -> bitcast,    if the SrcTy and DstTy are same size
+      // ext, trunc -> ext,        if sizeof(SrcTy) < sizeof(DstTy)
+      // ext, trunc -> trunc,      if sizeof(SrcTy) > sizeof(DstTy)
+      unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
+      unsigned DstSize = DstTy->getPrimitiveSizeInBits();
+      if (SrcSize == DstSize)
+        return Instruction::BitCast;
+      else if (SrcSize < DstSize)
+        return firstOp;
+      return secondOp;
+    }
+    case 9: // zext, sext -> zext, because sext can't sign extend after zext
+      return Instruction::ZExt;
+    case 10:
+      // fpext followed by ftrunc is allowed if the bit size returned to is
+      // the same as the original, in which case its just a bitcast
+      if (SrcTy == DstTy)
+        return Instruction::BitCast;
+      return 0; // If the types are not the same we can't eliminate it.
+    case 11:
+      // bitcast followed by ptrtoint is allowed as long as the bitcast
+      // is a pointer to pointer cast.
+      if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
+        return secondOp;
+      return 0;
+    case 12:
+      // inttoptr, bitcast -> intptr  if bitcast is a ptr to ptr cast
+      if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
+        return firstOp;
+      return 0;
+    case 13: {
+      // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
+      unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
+      unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
+      unsigned DstSize = DstTy->getPrimitiveSizeInBits();
+      if (SrcSize <= PtrSize && SrcSize == DstSize)
+        return Instruction::BitCast;
+      return 0;
+    }
+    case 99: 
+      // cast combination can't happen (error in input). This is for all cases
+      // where the MidTy is not the same for the two cast instructions.
+      assert(!"Invalid Cast Combination");
+      return 0;
+    default:
+      assert(!"Error in CastResults table!!!");
+      return 0;
+  }
+  return 0;
+}
+
+CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, 
+  const std::string &Name, Instruction *InsertBefore) {
+  // Construct and return the appropriate CastInst subclass
+  switch (op) {
+    case Trunc:    return new TruncInst    (S, Ty, Name, InsertBefore);
+    case ZExt:     return new ZExtInst     (S, Ty, Name, InsertBefore);
+    case SExt:     return new SExtInst     (S, Ty, Name, InsertBefore);
+    case FPTrunc:  return new FPTruncInst  (S, Ty, Name, InsertBefore);
+    case FPExt:    return new FPExtInst    (S, Ty, Name, InsertBefore);
+    case UIToFP:   return new UIToFPInst   (S, Ty, Name, InsertBefore);
+    case SIToFP:   return new SIToFPInst   (S, Ty, Name, InsertBefore);
+    case FPToUI:   return new FPToUIInst   (S, Ty, Name, InsertBefore);
+    case FPToSI:   return new FPToSIInst   (S, Ty, Name, InsertBefore);
+    case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
+    case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
+    case BitCast:  return new BitCastInst  (S, Ty, Name, InsertBefore);
+    default:
+      assert(!"Invalid opcode provided");
+  }
+  return 0;
+}
+
+CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
+  const std::string &Name, BasicBlock *InsertAtEnd) {
+  // Construct and return the appropriate CastInst subclass
+  switch (op) {
+    case Trunc:    return new TruncInst    (S, Ty, Name, InsertAtEnd);
+    case ZExt:     return new ZExtInst     (S, Ty, Name, InsertAtEnd);
+    case SExt:     return new SExtInst     (S, Ty, Name, InsertAtEnd);
+    case FPTrunc:  return new FPTruncInst  (S, Ty, Name, InsertAtEnd);
+    case FPExt:    return new FPExtInst    (S, Ty, Name, InsertAtEnd);
+    case UIToFP:   return new UIToFPInst   (S, Ty, Name, InsertAtEnd);
+    case SIToFP:   return new SIToFPInst   (S, Ty, Name, InsertAtEnd);
+    case FPToUI:   return new FPToUIInst   (S, Ty, Name, InsertAtEnd);
+    case FPToSI:   return new FPToSIInst   (S, Ty, Name, InsertAtEnd);
+    case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
+    case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
+    case BitCast:  return new BitCastInst  (S, Ty, Name, InsertAtEnd);
+    default:
+      assert(!"Invalid opcode provided");
+  }
+  return 0;
+}
+
+CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, 
+                                        const std::string &Name,
+                                        Instruction *InsertBefore) {
+  if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
+    return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
+  return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
+}
+
+CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, 
+                                        const std::string &Name,
+                                        BasicBlock *InsertAtEnd) {
+  if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
+    return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
+  return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
+}
+
+CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, 
+                                        const std::string &Name,
+                                        Instruction *InsertBefore) {
+  if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
+    return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
+  return create(Instruction::SExt, S, Ty, Name, InsertBefore);
+}
+
+CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, 
+                                        const std::string &Name,
+                                        BasicBlock *InsertAtEnd) {
+  if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
+    return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
+  return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
+}
+
+CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
+                                         const std::string &Name,
+                                         Instruction *InsertBefore) {
+  if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
+    return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
+  return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
+}
+
+CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
+                                         const std::string &Name, 
+                                         BasicBlock *InsertAtEnd) {
+  if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
+    return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
+  return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
+}
+
+CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
+                                      const std::string &Name,
+                                      BasicBlock *InsertAtEnd) {
+  assert(isa<PointerType>(S->getType()) && "Invalid cast");
+  assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
+         "Invalid cast");
+
+  if (Ty->isInteger())
+    return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
+  return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
+}
+
+/// @brief Create a BitCast or a PtrToInt cast instruction
+CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, 
+                                      const std::string &Name, 
+                                      Instruction *InsertBefore) {
+  assert(isa<PointerType>(S->getType()) && "Invalid cast");
+  assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
+         "Invalid cast");
+
+  if (Ty->isInteger())
+    return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
+  return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
+}
+
+CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, 
+                                      bool isSigned, const std::string &Name,
+                                      Instruction *InsertBefore) {
+  assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
+  unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
+  unsigned DstBits = Ty->getPrimitiveSizeInBits();
+  Instruction::CastOps opcode =
+    (SrcBits == DstBits ? Instruction::BitCast :
+     (SrcBits > DstBits ? Instruction::Trunc :
+      (isSigned ? Instruction::SExt : Instruction::ZExt)));
+  return create(opcode, C, Ty, Name, InsertBefore);
+}
+
+CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, 
+                                      bool isSigned, const std::string &Name,
+                                      BasicBlock *InsertAtEnd) {
+  assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
+  unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
+  unsigned DstBits = Ty->getPrimitiveSizeInBits();
+  Instruction::CastOps opcode =
+    (SrcBits == DstBits ? Instruction::BitCast :
+     (SrcBits > DstBits ? Instruction::Trunc :
+      (isSigned ? Instruction::SExt : Instruction::ZExt)));
+  return create(opcode, C, Ty, Name, InsertAtEnd);
+}
+
+CastInst *CastInst::createFPCast(Value *C, const Type *Ty, 
+                                 const std::string &Name, 
+                                 Instruction *InsertBefore) {
+  assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && 
+         "Invalid cast");
+  unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
+  unsigned DstBits = Ty->getPrimitiveSizeInBits();
+  Instruction::CastOps opcode =
+    (SrcBits == DstBits ? Instruction::BitCast :
+     (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
+  return create(opcode, C, Ty, Name, InsertBefore);
+}
+
+CastInst *CastInst::createFPCast(Value *C, const Type *Ty, 
+                                 const std::string &Name, 
+                                 BasicBlock *InsertAtEnd) {
+  assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && 
+         "Invalid cast");
+  unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
+  unsigned DstBits = Ty->getPrimitiveSizeInBits();
+  Instruction::CastOps opcode =
+    (SrcBits == DstBits ? Instruction::BitCast :
+     (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
+  return create(opcode, C, Ty, Name, InsertAtEnd);
+}
+
+// Provide a way to get a "cast" where the cast opcode is inferred from the 
+// types and size of the operand. This, basically, is a parallel of the 
+// logic in the castIsValid function below.  This axiom should hold:
+//   castIsValid( getCastOpcode(Val, Ty), Val, Ty)
+// should not assert in castIsValid. In other words, this produces a "correct"
+// casting opcode for the arguments passed to it.
+Instruction::CastOps
+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/vector
+  unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
+
+  // Run through the possibilities ...
+  if (DestTy->isInteger()) {                       // Casting to integral
+    if (SrcTy->isInteger()) {                      // Casting from integral
+      if (DestBits < SrcBits)
+        return Trunc;                               // int -> smaller int
+      else if (DestBits > SrcBits) {                // its an extension
+        if (SrcIsSigned)
+          return SExt;                              // signed -> SEXT
+        else
+          return ZExt;                              // unsigned -> ZEXT
+      } else {
+        return BitCast;                             // Same size, No-op cast
+      }
+    } else if (SrcTy->isFloatingPoint()) {          // Casting from floating pt
+      if (DestIsSigned) 
+        return FPToSI;                              // FP -> sint
+      else
+        return FPToUI;                              // FP -> uint 
+    } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
+      assert(DestBits == PTy->getBitWidth() &&
+               "Casting vector to integer of different width");
+      return BitCast;                             // Same size, no-op cast
+    } else {
+      assert(isa<PointerType>(SrcTy) &&
+             "Casting from a value that is not first-class type");
+      return PtrToInt;                              // ptr -> int
+    }
+  } else if (DestTy->isFloatingPoint()) {           // Casting to floating pt
+    if (SrcTy->isInteger()) {                      // Casting from integral
+      if (SrcIsSigned)
+        return SIToFP;                              // sint -> FP
+      else
+        return UIToFP;                              // uint -> FP
+    } else if (SrcTy->isFloatingPoint()) {          // Casting from floating pt
+      if (DestBits < SrcBits) {
+        return FPTrunc;                             // FP -> smaller FP
+      } else if (DestBits > SrcBits) {
+        return FPExt;                               // FP -> larger FP
+      } else  {
+        return BitCast;                             // same size, no-op cast
+      }
+    } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
+      assert(DestBits == PTy->getBitWidth() &&
+             "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");
+    }
+  } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
+    if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
+      assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
+             "Casting vector to vector of different widths");
+      return BitCast;                             // vector -> vector
+    } else if (DestPTy->getBitWidth() == SrcBits) {
+      return BitCast;                               // float/int -> vector
+    } else {
+      assert(!"Illegal cast to vector (wrong type or size)");
+    }
+  } else if (isa<PointerType>(DestTy)) {
+    if (isa<PointerType>(SrcTy)) {
+      return BitCast;                               // ptr -> ptr
+    } else if (SrcTy->isInteger()) {
+      return IntToPtr;                              // int -> ptr
+    } else {
+      assert(!"Casting pointer to other than pointer or int");
+    }
+  } else {
+    assert(!"Casting to type that is not first-class");
+  }
+
+  // If we fall through to here we probably hit an assertion cast above
+  // and assertions are not turned on. Anything we return is an error, so
+  // BitCast is as good a choice as any.
+  return BitCast;
+}
 
 //===----------------------------------------------------------------------===//
-//                             SetCondInst Class
+//                    CastInst SubClass Constructors
 //===----------------------------------------------------------------------===//
 
-SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
-                         const std::string &Name, Instruction *InsertBefore)
-  : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
+/// Check that the construction parameters for a CastInst are correct. This
+/// could be broken out into the separate constructors but it is useful to have
+/// it in one place and to eliminate the redundant code for getting the sizes
+/// of the types involved.
+bool 
+CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
 
-  // Make sure it's a valid type... getInverseCondition will assert out if not.
-  assert(getInverseCondition(Opcode));
+  // Check for type sanity on the arguments
+  const Type *SrcTy = S->getType();
+  if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
+    return false;
+
+  // Get the size of the types in bits, we'll need this later
+  unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
+  unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
+
+  // Switch on the opcode provided
+  switch (op) {
+  default: return false; // This is an input error
+  case Instruction::Trunc:
+    return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
+  case Instruction::ZExt:
+    return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
+  case Instruction::SExt: 
+    return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
+  case Instruction::FPTrunc:
+    return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && 
+      SrcBitSize > DstBitSize;
+  case Instruction::FPExt:
+    return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && 
+      SrcBitSize < DstBitSize;
+  case Instruction::UIToFP:
+  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:
+  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();
+  case Instruction::IntToPtr:
+    return SrcTy->isInteger() && isa<PointerType>(DstTy);
+  case Instruction::BitCast:
+    // BitCast implies a no-op cast of type only. No bits change.
+    // However, you can't cast pointers to anything but pointers.
+    if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
+      return false;
+
+    // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
+    // these cases, the cast is okay if the source and destination bit widths
+    // are identical.
+    return SrcBitSize == DstBitSize;
+  }
 }
 
-SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
-                         const std::string &Name, BasicBlock *InsertAtEnd)
-  : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
+TruncInst::TruncInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
+}
 
-  // Make sure it's a valid type... getInverseCondition will assert out if not.
-  assert(getInverseCondition(Opcode));
+TruncInst::TruncInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
 }
 
-// getInverseCondition - Return the inverse of the current condition opcode.
-// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
-//
-Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
-  switch (Opcode) {
-  default:
-    assert(0 && "Unknown setcc opcode!");
-  case SetEQ: return SetNE;
-  case SetNE: return SetEQ;
-  case SetGT: return SetLE;
-  case SetLT: return SetGE;
-  case SetGE: return SetLT;
-  case SetLE: return SetGT;
+ZExtInst::ZExtInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+)  : CastInst(Ty, ZExt, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
+}
+
+ZExtInst::ZExtInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+)  : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
+}
+SExtInst::SExtInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, SExt, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
+}
+
+SExtInst::SExtInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+)  : CastInst(Ty, SExt, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
+}
+
+FPTruncInst::FPTruncInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
+}
+
+FPTruncInst::FPTruncInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
+}
+
+FPExtInst::FPExtInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, FPExt, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
+}
+
+FPExtInst::FPExtInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
+}
+
+UIToFPInst::UIToFPInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
+}
+
+UIToFPInst::UIToFPInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
+}
+
+SIToFPInst::SIToFPInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
+}
+
+SIToFPInst::SIToFPInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
+}
+
+FPToUIInst::FPToUIInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
+}
+
+FPToUIInst::FPToUIInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
+}
+
+FPToSIInst::FPToSIInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
+}
+
+FPToSIInst::FPToSIInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
+}
+
+PtrToIntInst::PtrToIntInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
+}
+
+PtrToIntInst::PtrToIntInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
+}
+
+IntToPtrInst::IntToPtrInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
+}
+
+IntToPtrInst::IntToPtrInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
+}
+
+BitCastInst::BitCastInst(
+  Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
+) : CastInst(Ty, BitCast, S, Name, InsertBefore) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
+}
+
+BitCastInst::BitCastInst(
+  Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
+) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { 
+  assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
+}
+
+//===----------------------------------------------------------------------===//
+//                               CmpInst Classes
+//===----------------------------------------------------------------------===//
+
+CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
+                 const std::string &Name, Instruction *InsertBefore)
+  : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
+    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 &&
+           "Invalid ICmp predicate value");
+    const Type* Op0Ty = getOperand(0)->getType();
+    const Type* Op1Ty = getOperand(1)->getType();
+    assert(Op0Ty == Op1Ty &&
+           "Both operands to ICmp instruction are not of the same type!");
+    // Check that the operands are the right type
+    assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
+           "Invalid operand types for ICmp instruction");
+    return;
+  }
+  assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
+  assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
+         "Invalid FCmp predicate value");
+  const Type* Op0Ty = getOperand(0)->getType();
+  const Type* Op1Ty = getOperand(1)->getType();
+  assert(Op0Ty == Op1Ty &&
+         "Both operands to FCmp instruction are not of the same type!");
+  // Check that the operands are the right type
+  assert(Op0Ty->isFloatingPoint() &&
+         "Invalid operand types for FCmp instruction");
+}
+  
+CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
+                 const std::string &Name, BasicBlock *InsertAtEnd)
+  : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
+  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 &&
+           "Invalid ICmp predicate value");
+
+    const Type* Op0Ty = getOperand(0)->getType();
+    const Type* Op1Ty = getOperand(1)->getType();
+    assert(Op0Ty == Op1Ty &&
+          "Both operands to ICmp instruction are not of the same type!");
+    // Check that the operands are the right type
+    assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
+           "Invalid operand types for ICmp instruction");
+    return;
+  }
+  assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
+  assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
+         "Invalid FCmp predicate value");
+  const Type* Op0Ty = getOperand(0)->getType();
+  const Type* Op1Ty = getOperand(1)->getType();
+  assert(Op0Ty == Op1Ty &&
+          "Both operands to FCmp instruction are not of the same type!");
+  // Check that the operands are the right type
+  assert(Op0Ty->isFloatingPoint() &&
+        "Invalid operand types for FCmp instruction");
+}
+
+CmpInst *
+CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, 
+                const std::string &Name, Instruction *InsertBefore) {
+  if (Op == Instruction::ICmp) {
+    return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, 
+                        InsertBefore);
   }
+  return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, 
+                      InsertBefore);
 }
 
-// getSwappedCondition - Return the condition opcode that would be the result
-// of exchanging the two operands of the setcc instruction without changing
-// the result produced.  Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
-//
-Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
-  switch (Opcode) {
-  default: assert(0 && "Unknown setcc instruction!");
-  case SetEQ: case SetNE: return Opcode;
-  case SetGT: return SetLT;
-  case SetLT: return SetGT;
-  case SetGE: return SetLE;
-  case SetLE: return SetGE;
+CmpInst *
+CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, 
+                const std::string &Name, BasicBlock *InsertAtEnd) {
+  if (Op == Instruction::ICmp) {
+    return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, 
+                        InsertAtEnd);
+  }
+  return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, 
+                      InsertAtEnd);
+}
+
+void CmpInst::swapOperands() {
+  if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
+    IC->swapOperands();
+  else
+    cast<FCmpInst>(this)->swapOperands();
+}
+
+bool CmpInst::isCommutative() {
+  if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
+    return IC->isCommutative();
+  return cast<FCmpInst>(this)->isCommutative();
+}
+
+bool CmpInst::isEquality() {
+  if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
+    return IC->isEquality();
+  return cast<FCmpInst>(this)->isEquality();
+}
+
+
+ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
+  switch (pred) {
+    default:
+      assert(!"Unknown icmp predicate!");
+    case ICMP_EQ: return ICMP_NE;
+    case ICMP_NE: return ICMP_EQ;
+    case ICMP_UGT: return ICMP_ULE;
+    case ICMP_ULT: return ICMP_UGE;
+    case ICMP_UGE: return ICMP_ULT;
+    case ICMP_ULE: return ICMP_UGT;
+    case ICMP_SGT: return ICMP_SLE;
+    case ICMP_SLT: return ICMP_SGE;
+    case ICMP_SGE: return ICMP_SLT;
+    case ICMP_SLE: return ICMP_SGT;
+  }
+}
+
+ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
+  switch (pred) {
+    default: assert(! "Unknown icmp predicate!");
+    case ICMP_EQ: case ICMP_NE:
+      return pred;
+    case ICMP_SGT: return ICMP_SLT;
+    case ICMP_SLT: return ICMP_SGT;
+    case ICMP_SGE: return ICMP_SLE;
+    case ICMP_SLE: return ICMP_SGE;
+    case ICMP_UGT: return ICMP_ULT;
+    case ICMP_ULT: return ICMP_UGT;
+    case ICMP_UGE: return ICMP_ULE;
+    case ICMP_ULE: return ICMP_UGE;
+  }
+}
+
+ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
+  switch (pred) {
+    default: assert(! "Unknown icmp predicate!");
+    case ICMP_EQ: case ICMP_NE: 
+    case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: 
+       return pred;
+    case ICMP_UGT: return ICMP_SGT;
+    case ICMP_ULT: return ICMP_SLT;
+    case ICMP_UGE: return ICMP_SGE;
+    case ICMP_ULE: return ICMP_SLE;
+  }
+}
+
+bool ICmpInst::isSignedPredicate(Predicate pred) {
+  switch (pred) {
+    default: assert(! "Unknown icmp predicate!");
+    case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: 
+      return true;
+    case ICMP_EQ:  case ICMP_NE: case ICMP_UGT: case ICMP_ULT: 
+    case ICMP_UGE: case ICMP_ULE:
+      return false;
+  }
+}
+
+/// 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:
+      assert(!"Unknown icmp predicate!");
+    case FCMP_OEQ: return FCMP_UNE;
+    case FCMP_ONE: return FCMP_UEQ;
+    case FCMP_OGT: return FCMP_ULE;
+    case FCMP_OLT: return FCMP_UGE;
+    case FCMP_OGE: return FCMP_ULT;
+    case FCMP_OLE: return FCMP_UGT;
+    case FCMP_UEQ: return FCMP_ONE;
+    case FCMP_UNE: return FCMP_OEQ;
+    case FCMP_UGT: return FCMP_OLE;
+    case FCMP_ULT: return FCMP_OGE;
+    case FCMP_UGE: return FCMP_OLT;
+    case FCMP_ULE: return FCMP_OGT;
+    case FCMP_ORD: return FCMP_UNO;
+    case FCMP_UNO: return FCMP_ORD;
+    case FCMP_TRUE: return FCMP_FALSE;
+    case FCMP_FALSE: return FCMP_TRUE;
+  }
+}
+
+FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
+  switch (pred) {
+    default: assert(!"Unknown fcmp predicate!");
+    case FCMP_FALSE: case FCMP_TRUE:
+    case FCMP_OEQ: case FCMP_ONE:
+    case FCMP_UEQ: case FCMP_UNE:
+    case FCMP_ORD: case FCMP_UNO:
+      return pred;
+    case FCMP_OGT: return FCMP_OLT;
+    case FCMP_OLT: return FCMP_OGT;
+    case FCMP_OGE: return FCMP_OLE;
+    case FCMP_OLE: return FCMP_OGE;
+    case FCMP_UGT: return FCMP_ULT;
+    case FCMP_ULT: return FCMP_UGT;
+    case FCMP_UGE: return FCMP_ULE;
+    case FCMP_ULE: return FCMP_UGE;
+  }
+}
+
+bool CmpInst::isUnsigned(unsigned short predicate) {
+  switch (predicate) {
+    default: return false;
+    case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: 
+    case ICmpInst::ICMP_UGE: return true;
+  }
+}
+
+bool CmpInst::isSigned(unsigned short predicate){
+  switch (predicate) {
+    default: return false;
+    case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: 
+    case ICmpInst::ICMP_SGE: return true;
+  }
+}
+
+bool CmpInst::isOrdered(unsigned short predicate) {
+  switch (predicate) {
+    default: return false;
+    case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: 
+    case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: 
+    case FCmpInst::FCMP_ORD: return true;
+  }
+}
+      
+bool CmpInst::isUnordered(unsigned short predicate) {
+  switch (predicate) {
+    default: return false;
+    case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: 
+    case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: 
+    case FCmpInst::FCMP_UNO: return true;
   }
 }
 
@@ -1159,9 +2439,29 @@ void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
   OperandList[1].init(Default, this);
 }
 
+/// SwitchInst ctor - Create a new switch instruction, specifying a value to
+/// switch on and a default destination.  The number of additional cases can
+/// be specified here to make memory allocation more efficient.  This
+/// constructor can also autoinsert before another instruction.
+SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
+                       Instruction *InsertBefore)
+  : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
+  init(Value, Default, NumCases);
+}
+
+/// SwitchInst ctor - Create a new switch instruction, specifying a value to
+/// switch on and a default destination.  The number of additional cases can
+/// be specified here to make memory allocation more efficient.  This
+/// constructor also autoinserts at the end of the specified BasicBlock.
+SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
+                       BasicBlock *InsertAtEnd)
+  : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
+  init(Value, Default, NumCases);
+}
+
 SwitchInst::SwitchInst(const SwitchInst &SI)
-  : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
-                   SI.getNumOperands()) {
+  : TerminatorInst(Type::VoidTy, Instruction::Switch,
+                   new Use[SI.getNumOperands()], SI.getNumOperands()) {
   Use *OL = OperandList, *InOL = SI.OperandList;
   for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
     OL[i].init(InOL[i], this);
@@ -1267,16 +2567,34 @@ BinaryOperator *BinaryOperator::clone() const {
   return create(getOpcode(), Ops[0], Ops[1]);
 }
 
-MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
-AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
-FreeInst   *FreeInst::clone()   const { return new FreeInst(getOperand(0)); }
-LoadInst   *LoadInst::clone()   const { return new LoadInst(*this); }
-StoreInst  *StoreInst::clone()  const { return new StoreInst(*this); }
-CastInst   *CastInst::clone()   const { return new CastInst(*this); }
-CallInst   *CallInst::clone()   const { return new CallInst(*this); }
-ShiftInst  *ShiftInst::clone()  const { return new ShiftInst(*this); }
-SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
-VAArgInst  *VAArgInst::clone()  const { return new VAArgInst(*this); }
+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); }
+AllocaInst *AllocaInst::clone()   const { return new AllocaInst(*this); }
+FreeInst   *FreeInst::clone()     const { return new FreeInst(getOperand(0)); }
+LoadInst   *LoadInst::clone()     const { return new LoadInst(*this); }
+StoreInst  *StoreInst::clone()    const { return new StoreInst(*this); }
+CastInst   *TruncInst::clone()    const { return new TruncInst(*this); }
+CastInst   *ZExtInst::clone()     const { return new ZExtInst(*this); }
+CastInst   *SExtInst::clone()     const { return new SExtInst(*this); }
+CastInst   *FPTruncInst::clone()  const { return new FPTruncInst(*this); }
+CastInst   *FPExtInst::clone()    const { return new FPExtInst(*this); }
+CastInst   *UIToFPInst::clone()   const { return new UIToFPInst(*this); }
+CastInst   *SIToFPInst::clone()   const { return new SIToFPInst(*this); }
+CastInst   *FPToUIInst::clone()   const { return new FPToUIInst(*this); }
+CastInst   *FPToSIInst::clone()   const { return new FPToSIInst(*this); }
+CastInst   *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
+CastInst   *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
+CastInst   *BitCastInst::clone()  const { return new BitCastInst(*this); }
+CallInst   *CallInst::clone()     const { return new CallInst(*this); }
+SelectInst *SelectInst::clone()   const { return new SelectInst(*this); }
+VAArgInst  *VAArgInst::clone()    const { return new VAArgInst(*this); }
+
 ExtractElementInst *ExtractElementInst::clone() const {
   return new ExtractElementInst(*this);
 }