* Make the ctor take a TargetData even though it's not using it yet
[oota-llvm.git] / lib / VMCore / Constants.cpp
index 192f5f7c65f3f4c290d78a731e5addfaade8a4c2..bc521d7686de23d3400e67a8784e68a6134bdfaa 100644 (file)
@@ -17,6 +17,7 @@
 using std::map;
 using std::pair;
 using std::make_pair;
+using std::vector;
 
 ConstantBool *ConstantBool::True  = new ConstantBool(true);
 ConstantBool *ConstantBool::False = new ConstantBool(false);
@@ -68,13 +69,10 @@ void Constant::destroyConstantImpl() {
   while (!use_empty()) {
     Value *V = use_back();
 #ifndef NDEBUG      // Only in -g mode...
-    if (!isa<Constant>(V)) {
-      std::cerr << "While deleting: ";
-      dump();
-      std::cerr << "\nUse still stuck around after Def is destroyed: ";
-      V->dump();
-      std::cerr << "\n";
-    }
+    if (!isa<Constant>(V))
+      std::cerr << "While deleting: " << *this
+                << "\n\nUse still stuck around after Def is destroyed: "
+                << *V << "\n\n";
 #endif
     assert(isa<Constant>(V) && "References remain to Constant being destroyed");
     Constant *CPV = cast<Constant>(V);
@@ -153,7 +151,7 @@ ConstantExpr::ConstantExpr(unsigned opCode, Constant* C1,
 }
 
 ConstantExpr::ConstantExpr(unsigned opCode, Constant* C,
-                           const std::vector<Value*>& IdxList, const Type *Ty)
+                          const std::vector<Constant*> &IdxList, const Type *Ty)
   : Constant(Ty), iType(opCode) {
   Operands.reserve(1+IdxList.size());
   Operands.push_back(Use(C, this));
@@ -167,27 +165,27 @@ ConstantExpr::ConstantExpr(unsigned opCode, Constant* C,
 //                           classof implementations
 
 bool ConstantInt::classof(const Constant *CPV) {
-  return CPV->getType()->isIntegral() && ! isa<ConstantExpr>(CPV);
+  return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
 }
 bool ConstantSInt::classof(const Constant *CPV) {
-  return CPV->getType()->isSigned() && ! isa<ConstantExpr>(CPV);
+  return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
 }
 bool ConstantUInt::classof(const Constant *CPV) {
-  return CPV->getType()->isUnsigned() && ! isa<ConstantExpr>(CPV);
+  return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
 }
 bool ConstantFP::classof(const Constant *CPV) {
   const Type *Ty = CPV->getType();
   return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
-          ! isa<ConstantExpr>(CPV));
+          !isa<ConstantExpr>(CPV));
 }
 bool ConstantArray::classof(const Constant *CPV) {
-  return isa<ArrayType>(CPV->getType()) && ! isa<ConstantExpr>(CPV);
+  return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
 }
 bool ConstantStruct::classof(const Constant *CPV) {
-  return isa<StructType>(CPV->getType()) && ! isa<ConstantExpr>(CPV);
+  return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
 }
 bool ConstantPointer::classof(const Constant *CPV) {
-  return (isa<PointerType>(CPV->getType()) && ! isa<ConstantExpr>(CPV));
+  return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
 }
 
 
@@ -390,81 +388,108 @@ ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
 //---- ConstantExpr::get() implementations...
 // Return NULL on invalid expressions.
 //
-ConstantExpr*
-ConstantExpr::get(unsigned opCode, Constant *C, const Type *Ty) {
-  if (opCode != Instruction::Cast &&
-      (opCode < Instruction::FirstUnaryOp ||
-       opCode >= Instruction::NumUnaryOps)) {
-    cerr << "Invalid opcode " << ConstantExpr::getOpcodeName(opCode)
-         << " in unary constant expression" << endl;
-    return NULL;       // Not Cast or other unary opcode
-  }
+typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
+static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
+
+ConstantExpr *ConstantExpr::get(unsigned Opcode, 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);
+  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
-  if (opCode != Instruction::Cast && Ty != C->getType()) {
-    cerr << "Type of operand in unary constant expression should match result" << endl;
-    return NULL;
-  }
-  return new ConstantExpr(opCode, C, Ty);
+  assert((Opcode == Instruction::Cast || Ty == C->getType()) &&
+         "Type of operand in unary constant expression should match result");
+  
+  Result = new ConstantExpr(Opcode, C, Ty);
+  ExprConstants.add(Ty, Key, Result);
+  return Result;
 }
 
-ConstantExpr*
-ConstantExpr::get(unsigned opCode, Constant *C1, Constant *C2,const Type *Ty) {
-  if (opCode < Instruction::FirstBinaryOp ||
-      opCode >= Instruction::NumBinaryOps) {
-    cerr << "Invalid opcode " << ConstantExpr::getOpcodeName(opCode)
-         << " in binary constant expression" << endl;
-    return NULL;
-  }
-  if (Ty != C1->getType() || Ty != C2->getType()) {
-    cerr << "Types of both operands in binary constant expression should match result" << endl;
-    return NULL;
-  }
-  return new ConstantExpr(opCode, C1, C2, Ty);
+ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
+                                const Type *Ty) {
+
+  // Look up the constant in the table first to ensure uniqueness
+  vector<Constant*> argVec(1, C1); argVec.push_back(C2);
+  const ExprMapKeyType &Key = make_pair(Opcode, 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::FirstBinaryOp &&
+          Opcode < Instruction::NumBinaryOps) &&
+         "Invalid opcode  in binary constant expression");
+
+  assert(Ty == C1->getType() && Ty == C2->getType() &&
+         "Operand types in binary constant expression should match result");
+  
+  Result = new ConstantExpr(Opcode, C1, C2, Ty);
+  ExprConstants.add(Ty, Key, Result);
+  return Result;
 }
 
-ConstantExpr*
-ConstantExpr::get(unsigned opCode, Constant*C,
-                   const std::vector<Value*>& idxList, const Type *Ty) {
+ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C,
+                                const std::vector<Constant*> &IdxList,
+                                const Type *Ty) {
+
+  // Look up the constant in the table first to ensure uniqueness
+  vector<Constant*> argVec(1, C);
+  argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
+  
+  const ExprMapKeyType &Key = make_pair(Opcode, 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
   // Must be a getElementPtr.  Check for valid getElementPtr expression.
   // 
-  if (opCode != Instruction::GetElementPtr) {
-    cerr << "operator other than GetElementPtr used with an index list" << endl;
-    return NULL;
-  }
-  if (!isa<ConstantPointer>(C)) {
-    cerr << "Constant GelElementPtr expression using something other than a constant pointer" << endl;
-    return NULL;
-  }
-  if (!isa<PointerType>(Ty)) {
-    cerr << "Non-pointer type for constant GelElementPtr expression" << endl;
-    return NULL;
-  }
-  const Type* fldType = GetElementPtrInst::getIndexedType(C->getType(),
-                                                          idxList, true);
-  if (!fldType) {
-    cerr << "Invalid index list for constant GelElementPtr expression" << endl;
-    return NULL;
-  }
-  if (cast<PointerType>(Ty)->getElementType() != fldType) {
-    cerr << "Type for constant GelElementPtr expression does not match field type" << endl;
-    return NULL;
-  }
+  assert(Opcode == Instruction::GetElementPtr &&
+         "Operator other than GetElementPtr used with an index list");
+
+  assert(isa<PointerType>(Ty) &&
+         "Non-pointer type for constant GelElementPtr expression");
+
+  std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
+  const Type *fldType = GetElementPtrInst::getIndexedType(C->getType(),
+                                                          ValIdxList, true);
+  assert(fldType && "Invalid index list for constant GelElementPtr expression");
+
+  assert(cast<PointerType>(Ty)->getElementType() == fldType &&
+         "Type for constant GelElementPtr expression doesn't match field type");
   
-  return new ConstantExpr(opCode, C, idxList, Ty);
+  Result = new ConstantExpr(Opcode, C, IdxList, Ty);
+  ExprConstants.add(Ty, Key, Result);
+  return Result;
+}
+
+// destroyConstant - Remove the constant from the constant table...
+//
+void ConstantExpr::destroyConstant() {
+  ExprConstants.remove(this);
+  destroyConstantImpl();
 }
 
-const char*
-ConstantExpr::getOpcodeName(unsigned opCode) {
-  return Instruction::getOpcodeName(opCode);
+const char *ConstantExpr::getOpcodeName(unsigned Opcode) {
+  return Instruction::getOpcodeName(Opcode);
 }
 
 
 //---- ConstantPointerRef::mutateReferences() implementation...
 //
-unsigned
-ConstantPointerRef::mutateReferences(Value* OldV, Value *NewV) {
+unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
   assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
-  GlobalValueNewGV = cast<GlobalValue>(NewV);
+  GlobalValue *NewGV = cast<GlobalValue>(NewV);
   getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
   Operands[0] = NewGV;
   return 1;
@@ -473,14 +498,13 @@ ConstantPointerRef::mutateReferences(Value* OldV, Value *NewV) {
 
 //---- ConstantPointerExpr::mutateReferences() implementation...
 //
-unsigned
-ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
-  unsigned numReplaced = 0;
-  Constant* NewC = cast<Constant>(NewV);
-  for (unsigned i=0, N = getNumOperands(); i < N; ++i)
+unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
+  unsigned NumReplaced = 0;
+  Constant *NewC = cast<Constant>(NewV);
+  for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
     if (Operands[i] == OldV) {
-      ++numReplaced;
+      ++NumReplaced;
       Operands[i] = NewC;
     }
-  return numReplaced;
+  return NumReplaced;
 }