+// 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::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(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.
+ Result = new ConstantExpr(Instruction::Cast, C, Ty);
+ ExprConstants.add(Ty, Key, Result);
+ return Result;
+}
+
+ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
+ // 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(C1->getType(), 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(C1->getType() == C2->getType() &&
+ "Operand types in binary constant expression should match");
+
+ Result = new ConstantExpr(Opcode, C1, C2);
+ ExprConstants.add(C1->getType(), Key, Result);
+ return Result;
+}
+
+ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
+ const std::vector<Constant*> &IdxList) {
+ const Type *Ty = C->getType();
+
+ // 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(Instruction::GetElementPtr, 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(isa<PointerType>(Ty) &&
+ "Non-pointer type for constant GelElementPtr expression");
+
+ // Check that the indices list is valid...
+ std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
+ const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
+ assert(DestTy && "Invalid index list for constant GelElementPtr expression");
+
+ Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
+ ExprConstants.add(Ty, Key, Result);
+ return Result;
+}