PHI nodes are not allowed to exist with zero incoming values, check that
[oota-llvm.git] / lib / VMCore / Constants.cpp
index eb68ef68095b65fd5faa7b16c370b012f9c0a557..5d7c35f232d0efd87df8c518875df1df3bd23452 100644 (file)
@@ -4,7 +4,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#define __STDC_LIMIT_MACROS           // Get defs for INT64_MAX and friends...
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iMemory.h"
@@ -106,9 +105,7 @@ ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
   case Type::UIntTyID:
   case Type::ULongTyID:  return getAllOnesValue(Ty);
 
-  default:
-    assert(0 && "Non-integral type specified!");
-    return 0;
+  default: return 0;
   }
 }
 
@@ -132,9 +129,7 @@ ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
   case Type::UIntTyID:
   case Type::ULongTyID:  return ConstantUInt::get(Ty, 0);
 
-  default:
-    assert(0 && "Non-integral type specified!");
-    return 0;
+  default: return 0;
   }
 }
 
@@ -157,9 +152,7 @@ ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
     Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
     return ConstantUInt::get(Ty, Val);
   }
-  default:
-    assert(0 && "Non-integral type specified!");
-    return 0;
+  default: return 0;
   }
 }
 
@@ -180,10 +173,14 @@ ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
 }
 
 ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
+  assert(Ty->isInteger() && Ty->isSigned() &&
+         "Illegal type for unsigned integer constant!");
   assert(isValueValidForType(Ty, V) && "Value too large for type!");
 }
 
 ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
+  assert(Ty->isInteger() && Ty->isUnsigned() &&
+         "Illegal type for unsigned integer constant!");
   assert(isValueValidForType(Ty, V) && "Value too large for type!");
 }
 
@@ -242,12 +239,11 @@ ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
 //                           classof implementations
 
 bool ConstantIntegral::classof(const Constant *CPV) {
-  return (CPV->getType()->isIntegral() || CPV->getType() == Type::BoolTy) &&
-          !isa<ConstantExpr>(CPV);
+  return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
 }
 
 bool ConstantInt::classof(const Constant *CPV) {
-  return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
+  return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
 }
 bool ConstantSInt::classof(const Constant *CPV) {
   return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
@@ -428,6 +424,24 @@ void ConstantArray::destroyConstant() {
   destroyConstantImpl();
 }
 
+// getAsString - If the sub-element type of this array is either sbyte or ubyte,
+// then this method converts the array to an std::string and returns it.
+// Otherwise, it asserts out.
+//
+std::string ConstantArray::getAsString() const {
+  std::string Result;
+  if (getType()->getElementType() == Type::SByteTy)
+    for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
+      Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
+  else {
+    assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
+    for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
+      Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
+  }
+  return Result;
+}
+
+
 //---- ConstantStruct::get() implementation...
 //
 static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
@@ -447,6 +461,7 @@ void ConstantStruct::destroyConstant() {
   destroyConstantImpl();
 }
 
+
 //---- ConstantPointerNull::get() implementation...
 //
 static ValueMap<char, ConstantPointerNull> NullPtrConstants;
@@ -458,6 +473,14 @@ ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
   return Result;
 }
 
+// destroyConstant - Remove the constant from the constant table...
+//
+void ConstantPointerNull::destroyConstant() {
+  NullPtrConstants.remove(this);
+  destroyConstantImpl();
+}
+
+
 //---- ConstantPointerRef::get() implementation...
 //
 ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
@@ -467,31 +490,29 @@ ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
   return GV->getParent()->getConstantPointerRef(GV);
 }
 
+// destroyConstant - Remove the constant from the constant table...
+//
+void ConstantPointerRef::destroyConstant() {
+  getValue()->getParent()->destroyConstantPointerRef(this);
+  destroyConstantImpl();
+}
+
+
 //---- ConstantExpr::get() implementations...
 //
 typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
 static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
 
-ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C, const Type *Ty) {
+ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
 
   // Look up the constant in the table first to ensure uniqueness
   vector<Constant*> argVec(1, C);
-  const ExprMapKeyType &Key = make_pair(Opcode, argVec);
+  const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
   ConstantExpr *Result = ExprConstants.get(Ty, Key);
   if (Result) return Result;
   
   // Its not in the table so create a new one and put it in the table.
-  // Check the operands for consistency first
-  assert(Opcode == Instruction::Cast ||
-         (Opcode >= Instruction::FirstUnaryOp &&
-          Opcode < Instruction::NumUnaryOps) &&
-         "Invalid opcode in unary ConstantExpr!");
-
-  // type of operand will not match result for Cast operation
-  assert((Opcode == Instruction::Cast || Ty == C->getType()) &&
-         "Type of operand in unary constant expression should match result");
-  
-  Result = new ConstantExpr(Opcode, C, Ty);
+  Result = new ConstantExpr(Instruction::Cast, C, Ty);
   ExprConstants.add(Ty, Key, Result);
   return Result;
 }