Fix the verification for overloaded intrinsic types. Check that they are
[oota-llvm.git] / lib / VMCore / Instructions.cpp
index bfda46d983199935ea485a13c2a4d4973c95b371..5297374e251db649ba75deb6893a4ef3cffbf160 100644 (file)
@@ -20,6 +20,7 @@
 #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 {
@@ -186,7 +187,8 @@ Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
 
 CallInst::~CallInst() {
   delete [] OperandList;
-  delete ParamAttrs; // FIXME: ParamAttrsList should be uniqued!
+  if (ParamAttrs)
+    ParamAttrs->dropRef();
 }
 
 void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
@@ -265,19 +267,21 @@ void CallInst::init(Value *Func) {
   assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
 }
 
+#if 0
+// Leave for llvm-gcc
 CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
                    const std::string &Name, BasicBlock *InsertAtEnd)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
-                                 ->getElementType())->getReturnType(),
+                                     ->getElementType())->getReturnType(),
                 Instruction::Call, 0, 0, InsertAtEnd) {
   init(Func, Args, NumArgs);
   setName(Name);
 }
 CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
                    const std::string &Name, Instruction *InsertBefore)
-: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
-                                 ->getElementType())->getReturnType(),
-              Instruction::Call, 0, 0, InsertBefore) {
+    : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
+                                     ->getElementType())->getReturnType(),
+                  Instruction::Call, 0, 0, InsertBefore) {
   init(Func, Args, NumArgs);
   setName(Name);
 }
@@ -299,7 +303,7 @@ CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
   init(Func, Actual1, Actual2);
   setName(Name);
 }
-
+#endif
 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
                    Instruction *InsertBefore)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
@@ -317,7 +321,6 @@ CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
   init(Func, Actual);
   setName(Name);
 }
-
 CallInst::CallInst(Value *Func, const std::string &Name,
                    Instruction *InsertBefore)
   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
@@ -347,6 +350,15 @@ CallInst::CallInst(const CallInst &CI)
     OL[i].init(InOL[i], this);
 }
 
+void CallInst::setParamAttrs(ParamAttrsList *newAttrs) {
+  if (ParamAttrs)
+    ParamAttrs->dropRef();
+
+  if (newAttrs)
+    newAttrs->addRef();
+
+  ParamAttrs = newAttrs; 
+}
 
 //===----------------------------------------------------------------------===//
 //                        InvokeInst Implementation
@@ -354,7 +366,8 @@ CallInst::CallInst(const CallInst &CI)
 
 InvokeInst::~InvokeInst() {
   delete [] OperandList;
-  delete ParamAttrs; // FIXME: ParamAttrsList should be uniqued!
+  if (ParamAttrs)
+    ParamAttrs->dropRef();
 }
 
 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
@@ -424,6 +437,15 @@ void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
   return setSuccessor(idx, B);
 }
 
+void InvokeInst::setParamAttrs(ParamAttrsList *newAttrs) {
+  if (ParamAttrs)
+    ParamAttrs->dropRef();
+
+  if (newAttrs)
+    newAttrs->addRef();
+
+  ParamAttrs = newAttrs; 
+}
 
 //===----------------------------------------------------------------------===//
 //                        ReturnInst Implementation
@@ -685,6 +707,7 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertBef) {
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
   setName(Name);
 }
@@ -693,6 +716,7 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertAE) {
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
   setName(Name);
 }
@@ -702,6 +726,27 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertBef) {
   setVolatile(isVolatile);
+  setAlignment(0);
+  AssertOK();
+  setName(Name);
+}
+
+LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, 
+                   unsigned Align, Instruction *InsertBef)
+  : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
+                     Load, Ptr, InsertBef) {
+  setVolatile(isVolatile);
+  setAlignment(Align);
+  AssertOK();
+  setName(Name);
+}
+
+LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, 
+                   unsigned Align, BasicBlock *InsertAE)
+  : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
+                     Load, Ptr, InsertAE) {
+  setVolatile(isVolatile);
+  setAlignment(Align);
   AssertOK();
   setName(Name);
 }
@@ -711,6 +756,7 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertAE) {
   setVolatile(isVolatile);
+  setAlignment(0);
   AssertOK();
   setName(Name);
 }
@@ -721,6 +767,7 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertBef) {
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
   if (Name && Name[0]) setName(Name);
 }
@@ -729,6 +776,7 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertAE) {
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
   if (Name && Name[0]) setName(Name);
 }
@@ -738,6 +786,7 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                    Load, Ptr, InsertBef) {
   setVolatile(isVolatile);
+  setAlignment(0);
   AssertOK();
   if (Name && Name[0]) setName(Name);
 }
@@ -747,10 +796,15 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
                      Load, Ptr, InsertAE) {
   setVolatile(isVolatile);
+  setAlignment(0);
   AssertOK();
   if (Name && Name[0]) setName(Name);
 }
 
+void LoadInst::setAlignment(unsigned Align) {
+  assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
+  SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
+}
 
 //===----------------------------------------------------------------------===//
 //                           StoreInst Implementation
@@ -770,6 +824,7 @@ StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
   Ops[0].init(val, this);
   Ops[1].init(addr, this);
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
 }
 
@@ -778,6 +833,7 @@ StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
   Ops[0].init(val, this);
   Ops[1].init(addr, this);
   setVolatile(false);
+  setAlignment(0);
   AssertOK();
 }
 
@@ -787,6 +843,27 @@ StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
   Ops[0].init(val, this);
   Ops[1].init(addr, this);
   setVolatile(isVolatile);
+  setAlignment(0);
+  AssertOK();
+}
+
+StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
+                     unsigned Align, Instruction *InsertBefore)
+  : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
+  Ops[0].init(val, this);
+  Ops[1].init(addr, this);
+  setVolatile(isVolatile);
+  setAlignment(Align);
+  AssertOK();
+}
+
+StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
+                     unsigned Align, BasicBlock *InsertAtEnd)
+  : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
+  Ops[0].init(val, this);
+  Ops[1].init(addr, this);
+  setVolatile(isVolatile);
+  setAlignment(Align);
   AssertOK();
 }
 
@@ -796,9 +873,15 @@ StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
   Ops[0].init(val, this);
   Ops[1].init(addr, this);
   setVolatile(isVolatile);
+  setAlignment(0);
   AssertOK();
 }
 
+void StoreInst::setAlignment(unsigned Align) {
+  assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
+  SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
+}
+
 //===----------------------------------------------------------------------===//
 //                       GetElementPtrInst Implementation
 //===----------------------------------------------------------------------===//
@@ -981,6 +1064,17 @@ bool GetElementPtrInst::hasAllZeroIndices() const {
   return true;
 }
 
+/// 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
@@ -1116,7 +1210,7 @@ bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
     return false;   // First operand of insertelement must be vector type.
   
   if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
-    return false;// Second operand of insertelement must be packed element type.
+    return false;// Second operand of insertelement must be vector element type.
     
   if (Index->getType() != Type::Int32Ty)
     return false;  // Third operand of insertelement must be uint.
@@ -1331,7 +1425,11 @@ BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
 
 // isConstantAllOnes - Helper function for several functions below
 static inline bool isConstantAllOnes(const Value *V) {
-  return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
+  if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
+    return CI->isAllOnesValue();
+  if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
+    return CV->isAllOnesValue();
+  return false;
 }
 
 bool BinaryOperator::isNeg(const Value *V) {
@@ -1427,7 +1525,7 @@ bool CastInst::isLosslessCast() const {
 /// example, the following are all no-op casts:
 /// # bitcast uint %X, int
 /// # bitcast uint* %x, sbyte*
-/// # bitcast packed< 2 x int > %x, packed< 4 x short> 
+/// # bitcast vector< 2 x int > %x, vector< 4 x short> 
 /// # ptrtoint uint* %x, uint     ; on 32-bit plaforms only
 /// @brief Determine if a cast is a no-op.
 bool CastInst::isNoopCast(const Type *IntPtrTy) const {
@@ -1794,8 +1892,8 @@ CastInst::getCastOpcode(
   const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
   // Get the bit sizes, we'll need these
   const Type *SrcTy = Src->getType();
-  unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();   // 0 for ptr/packed
-  unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
+  unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();   // 0 for ptr/vector
+  unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
 
   // Run through the possibilities ...
   if (DestTy->isInteger()) {                       // Casting to integral
@@ -1817,7 +1915,7 @@ CastInst::getCastOpcode(
         return FPToUI;                              // FP -> uint 
     } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
       assert(DestBits == PTy->getBitWidth() &&
-               "Casting packed to integer of different width");
+               "Casting vector to integer of different width");
       return BitCast;                             // Same size, no-op cast
     } else {
       assert(isa<PointerType>(SrcTy) &&
@@ -1840,7 +1938,7 @@ CastInst::getCastOpcode(
       }
     } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
       assert(DestBits == PTy->getBitWidth() &&
-             "Casting packed to floating point of different width");
+             "Casting vector to floating point of different width");
         return BitCast;                             // same size, no-op cast
     } else {
       assert(0 && "Casting pointer or non-first class to float");
@@ -1848,12 +1946,12 @@ CastInst::getCastOpcode(
   } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
     if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
       assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
-             "Casting packed to packed of different widths");
-      return BitCast;                             // packed -> packed
+             "Casting vector to vector of different widths");
+      return BitCast;                             // vector -> vector
     } else if (DestPTy->getBitWidth() == SrcBits) {
-      return BitCast;                               // float/int -> packed
+      return BitCast;                               // float/int -> vector
     } else {
-      assert(!"Illegal cast to packed (wrong type or size)");
+      assert(!"Illegal cast to vector (wrong type or size)");
     }
   } else if (isa<PointerType>(DestTy)) {
     if (isa<PointerType>(SrcTy)) {