For PR950:
[oota-llvm.git] / lib / VMCore / Instructions.cpp
index 9eaa8f7e846bdb1b3fa37da3fdad012e78dce64e..800eb9cd1d866850315993ded9d946c57f5c26b6 100644 (file)
@@ -34,6 +34,8 @@ void CallSite::setCallingConv(unsigned CC) {
 }
 
 
+
+
 //===----------------------------------------------------------------------===//
 //                            TerminatorInst Class
 //===----------------------------------------------------------------------===//
@@ -48,6 +50,13 @@ TerminatorInst::TerminatorInst(Instruction::TermOps iType,
   : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
 }
 
+// Out of line virtual method, so the vtable, etc has a home.
+TerminatorInst::~TerminatorInst() {
+}
+
+// Out of line virtual method, so the vtable, etc has a home.
+UnaryInstruction::~UnaryInstruction() {
+}
 
 
 //===----------------------------------------------------------------------===//
@@ -197,9 +206,13 @@ void CallInst::init(Value *Func, const std::vector<Value*> &Params) {
 
   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)
+         "Calling a function with bad signature!");
+  for (unsigned i = 0, e = Params.size(); i != e; ++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) {
@@ -213,8 +226,14 @@ void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
 
   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) {
@@ -229,6 +248,9 @@ void CallInst::init(Value *Func, Value *Actual) {
   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) {
@@ -339,8 +361,13 @@ void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
          (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
          "Calling a function with bad signature");
 
-  for (unsigned i = 0, e = Params.size(); i != e; i++)
+  for (unsigned i = 0, e = Params.size(); i != e; i++) {
+    assert((i >= FTy->getNumParams() || 
+            FTy->getParamType(i) == Params[i]->getType()) &&
+           "Invoking a function with a bad signature!");
+    
     OL[i+3].init(Params[i], this);
+  }
 }
 
 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
@@ -486,10 +513,13 @@ void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
 
 static Value *getAISize(Value *Amt) {
   if (!Amt)
-    Amt = ConstantUInt::get(Type::UIntTy, 1);
-  else
+    Amt = ConstantInt::get(Type::UIntTy, 1);
+  else {
+    assert(!isa<BasicBlock>(Amt) &&
+           "Passed basic block into allocation size parameter!  Ue other ctor");
     assert(Amt->getType() == Type::UIntTy &&
            "Malloc/Allocation array size != UIntTy!");
+  }
   return Amt;
 }
 
@@ -511,9 +541,13 @@ AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
   assert(Ty != Type::VoidTy && "Cannot allocate void!");
 }
 
+// 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 *CUI = dyn_cast<ConstantInt>(getOperand(0)))
+    return CUI->getZExtValue() != 1;
   return true;
 }
 
@@ -646,7 +680,7 @@ StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
 // message on bad indexes for a gep instruction.
 //
 static inline const Type *checkType(const Type *Ty) {
-  assert(Ty && "Invalid indices for type!");
+  assert(Ty && "Invalid GetElementPtrInst indices for type!");
   return Ty;
 }
 
@@ -810,6 +844,19 @@ ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
   Ops[1].init(Index, this);
 }
 
+ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
+                                       const std::string &Name,
+                                       Instruction *InsertBef)
+  : Instruction(cast<PackedType>(Val->getType())->getElementType(),
+                ExtractElement, Ops, 2, Name, InsertBef) {
+  Constant *Index = ConstantInt::get(Type::UIntTy, IndexV);
+  assert(isValidOperands(Val, Index) &&
+         "Invalid extractelement instruction operands!");
+  Ops[0].init(Val, this);
+  Ops[1].init(Index, this);
+}
+
+
 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
                                        const std::string &Name,
                                        BasicBlock *InsertAE)
@@ -822,6 +869,20 @@ ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
   Ops[1].init(Index, this);
 }
 
+ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
+                                       const std::string &Name,
+                                       BasicBlock *InsertAE)
+  : Instruction(cast<PackedType>(Val->getType())->getElementType(),
+                ExtractElement, Ops, 2, Name, InsertAE) {
+  Constant *Index = ConstantInt::get(Type::UIntTy, IndexV);
+  assert(isValidOperands(Val, Index) &&
+         "Invalid extractelement instruction operands!");
+  
+  Ops[0].init(Val, this);
+  Ops[1].init(Index, this);
+}
+
+
 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
   if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::UIntTy)
     return false;
@@ -833,6 +894,12 @@ 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)
@@ -844,6 +911,19 @@ InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
   Ops[2].init(Index, this);
 }
 
+InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
+                                     const std::string &Name,
+                                     Instruction *InsertBef)
+  : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
+  Constant *Index = ConstantInt::get(Type::UIntTy, 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);
+}
+
+
 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
                                      const std::string &Name,
                                      BasicBlock *InsertAE)
@@ -856,6 +936,19 @@ InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
   Ops[2].init(Index, this);
 }
 
+InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
+                                     const std::string &Name,
+                                     BasicBlock *InsertAE)
+: Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
+  Constant *Index = ConstantInt::get(Type::UIntTy, 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);
+}
+
 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, 
                                         const Value *Index) {
   if (!isa<PackedType>(Vec->getType()))
@@ -874,6 +967,13 @@ 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)
@@ -1094,6 +1194,33 @@ bool BinaryOperator::swapOperands() {
 }
 
 
+//===----------------------------------------------------------------------===//
+//                               ShiftInst Class
+//===----------------------------------------------------------------------===//
+
+/// isLogicalShift - Return true if this is a logical shift left or a logical
+/// shift right.
+bool ShiftInst::isLogicalShift() const {
+  return getOpcode() == Instruction::Shl || getType()->isUnsigned();
+}
+
+//===----------------------------------------------------------------------===//
+//                                CastInst Class
+//===----------------------------------------------------------------------===//
+
+/// isTruncIntCast - Return true if this is a truncating integer cast
+/// instruction, e.g. a cast from long to uint.
+bool CastInst::isTruncIntCast() const {
+  // The dest type has to be integral, the input has to be integer.
+  if (!getType()->isIntegral() || !getOperand(0)->getType()->isInteger())
+    return false;
+
+  // Has to be large to smaller.
+  return getOperand(0)->getType()->getPrimitiveSizeInBits() >
+         getType()->getPrimitiveSizeInBits();
+}
+
+
 //===----------------------------------------------------------------------===//
 //                             SetCondInst Class
 //===----------------------------------------------------------------------===//