* ConstExpr::getelementptr now takes a vector of Constants not Values

* Assert things instead of printing an error and returning null.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2949 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2002-07-18 00:14:50 +00:00
parent cc4b6ec2b9
commit 6183b92b2c

View File

@ -18,8 +18,6 @@ using std::map;
using std::pair; using std::pair;
using std::make_pair; using std::make_pair;
using std::vector; using std::vector;
using std::cerr;
using std::endl;
ConstantBool *ConstantBool::True = new ConstantBool(true); ConstantBool *ConstantBool::True = new ConstantBool(true);
ConstantBool *ConstantBool::False = new ConstantBool(false); ConstantBool *ConstantBool::False = new ConstantBool(false);
@ -71,13 +69,10 @@ void Constant::destroyConstantImpl() {
while (!use_empty()) { while (!use_empty()) {
Value *V = use_back(); Value *V = use_back();
#ifndef NDEBUG // Only in -g mode... #ifndef NDEBUG // Only in -g mode...
if (!isa<Constant>(V)) { if (!isa<Constant>(V))
std::cerr << "While deleting: "; std::cerr << "While deleting: " << *this
dump(); << "\n\nUse still stuck around after Def is destroyed: "
std::cerr << "\nUse still stuck around after Def is destroyed: "; << *V << "\n\n";
V->dump();
std::cerr << "\n";
}
#endif #endif
assert(isa<Constant>(V) && "References remain to Constant being destroyed"); assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Constant *CPV = cast<Constant>(V); Constant *CPV = cast<Constant>(V);
@ -156,7 +151,7 @@ ConstantExpr::ConstantExpr(unsigned opCode, Constant* C1,
} }
ConstantExpr::ConstantExpr(unsigned opCode, Constant* C, 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) { : Constant(Ty), iType(opCode) {
Operands.reserve(1+IdxList.size()); Operands.reserve(1+IdxList.size());
Operands.push_back(Use(C, this)); Operands.push_back(Use(C, this));
@ -170,27 +165,27 @@ ConstantExpr::ConstantExpr(unsigned opCode, Constant* C,
// classof implementations // classof implementations
bool ConstantInt::classof(const Constant *CPV) { 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) { 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) { 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) { bool ConstantFP::classof(const Constant *CPV) {
const Type *Ty = CPV->getType(); const Type *Ty = CPV->getType();
return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) && return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
! isa<ConstantExpr>(CPV)); !isa<ConstantExpr>(CPV));
} }
bool ConstantArray::classof(const Constant *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) { 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) { bool ConstantPointer::classof(const Constant *CPV) {
return (isa<PointerType>(CPV->getType()) && ! isa<ConstantExpr>(CPV)); return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
} }
@ -396,114 +391,86 @@ ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
typedef pair<unsigned, vector<Constant*> > ExprMapKeyType; typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants; static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
ConstantExpr* ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C, const Type *Ty) {
ConstantExpr::get(unsigned opCode, Constant *C, const Type *Ty) {
// Look up the constant in the table first to ensure uniqueness // Look up the constant in the table first to ensure uniqueness
vector<Constant*> argVec(1, C); vector<Constant*> argVec(1, C);
const ExprMapKeyType& key = make_pair(opCode, argVec); const ExprMapKeyType &Key = make_pair(Opcode, argVec);
ConstantExpr* result = ExprConstants.get(Ty, key); ConstantExpr *Result = ExprConstants.get(Ty, Key);
if (result) if (Result) return Result;
return result;
// Its not in the table so create a new one and put it in the table. // Its not in the table so create a new one and put it in the table.
// Check the operands for consistency first // Check the operands for consistency first
if (opCode != Instruction::Cast && assert(Opcode == Instruction::Cast ||
(opCode < Instruction::FirstUnaryOp || (Opcode >= Instruction::FirstUnaryOp &&
opCode >= Instruction::NumUnaryOps)) { Opcode < Instruction::NumUnaryOps) &&
std::cerr << "Invalid opcode " << ConstantExpr::getOpcodeName(opCode) "Invalid opcode in unary ConstantExpr!");
<< " in unary constant expression" << std::endl;
return NULL; // Not Cast or other unary opcode
}
// type of operand will not match result for Cast operation // type of operand will not match result for Cast operation
if (opCode != Instruction::Cast && Ty != C->getType()) { assert((Opcode == Instruction::Cast || Ty == C->getType()) &&
cerr << "Type of operand in unary constant expression should match result" << endl; "Type of operand in unary constant expression should match result");
return NULL;
}
result = new ConstantExpr(opCode, C, Ty); Result = new ConstantExpr(Opcode, C, Ty);
ExprConstants.add(Ty, key, result); ExprConstants.add(Ty, Key, Result);
return result; return Result;
} }
ConstantExpr* ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
ConstantExpr::get(unsigned opCode, Constant *C1, Constant *C2,const Type *Ty) { const Type *Ty) {
// Look up the constant in the table first to ensure uniqueness // Look up the constant in the table first to ensure uniqueness
vector<Constant*> argVec(1, C1); argVec.push_back(C2); vector<Constant*> argVec(1, C1); argVec.push_back(C2);
const ExprMapKeyType& key = make_pair(opCode, argVec); const ExprMapKeyType &Key = make_pair(Opcode, argVec);
ConstantExpr* result = ExprConstants.get(Ty, key); ConstantExpr *Result = ExprConstants.get(Ty, Key);
if (result) if (Result) return Result;
return result;
// Its not in the table so create a new one and put it in the table. // Its not in the table so create a new one and put it in the table.
// Check the operands for consistency first // Check the operands for consistency first
if (opCode < Instruction::FirstBinaryOp || assert((Opcode >= Instruction::FirstBinaryOp &&
opCode >= Instruction::NumBinaryOps) { Opcode < Instruction::NumBinaryOps) &&
cerr << "Invalid opcode " << ConstantExpr::getOpcodeName(opCode) "Invalid opcode in binary constant expression");
<< " in binary constant expression" << endl;
return NULL; assert(Ty == C1->getType() && Ty == C2->getType() &&
} "Operand types in binary constant expression should match result");
if (Ty != C1->getType() || Ty != C2->getType()) {
cerr << "Types of both operands in binary constant expression should match result" << endl;
return NULL;
}
result = new ConstantExpr(opCode, C1, C2, Ty); Result = new ConstantExpr(Opcode, C1, C2, Ty);
ExprConstants.add(Ty, key, result); ExprConstants.add(Ty, Key, Result);
return result; return Result;
} }
ConstantExpr* ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C,
ConstantExpr::get(unsigned opCode, Constant*C, const std::vector<Constant*> &IdxList,
const std::vector<Value*>& idxList, const Type *Ty) { const Type *Ty) {
// Look up the constant in the table first to ensure uniqueness // Look up the constant in the table first to ensure uniqueness
vector<Constant*> argVec(1, C); vector<Constant*> argVec(1, C);
for(vector<Value*>::const_iterator VI=idxList.begin(), VE=idxList.end(); argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
VI != VE; ++VI)
if (Constant *C = dyn_cast<Constant>(*VI))
argVec.push_back(C);
else {
cerr << "Non-constant index in constant GetElementPtr expr";
return NULL;
}
const ExprMapKeyType& key = make_pair(opCode, argVec); const ExprMapKeyType &Key = make_pair(Opcode, argVec);
ConstantExpr* result = ExprConstants.get(Ty, key); ConstantExpr *Result = ExprConstants.get(Ty, Key);
if (result) if (Result) return Result;
return result;
// Its not in the table so create a new one and put it in the table. // Its not in the table so create a new one and put it in the table.
// Check the operands for consistency first // Check the operands for consistency first
// Must be a getElementPtr. Check for valid getElementPtr expression. // Must be a getElementPtr. Check for valid getElementPtr expression.
// //
if (opCode != Instruction::GetElementPtr) { assert(Opcode == Instruction::GetElementPtr &&
cerr << "operator other than GetElementPtr used with an index list" << endl; "Operator other than GetElementPtr used with an index list");
return NULL;
} assert(isa<PointerType>(Ty) &&
if (!isa<ConstantPointer>(C)) { "Non-pointer type for constant GelElementPtr expression");
cerr << "Constant GelElementPtr expression using something other than a constant pointer" << endl;
return NULL; std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
} const Type *fldType = GetElementPtrInst::getIndexedType(C->getType(),
if (!isa<PointerType>(Ty)) { ValIdxList, true);
cerr << "Non-pointer type for constant GelElementPtr expression" << endl; assert(fldType && "Invalid index list for constant GelElementPtr expression");
return NULL;
} assert(cast<PointerType>(Ty)->getElementType() == fldType &&
const Type* fldType = GetElementPtrInst::getIndexedType(C->getType(), "Type for constant GelElementPtr expression doesn't match field type");
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;
}
result = new ConstantExpr(opCode, C, idxList, Ty); Result = new ConstantExpr(Opcode, C, IdxList, Ty);
ExprConstants.add(Ty, key, result); ExprConstants.add(Ty, Key, Result);
return result; return Result;
} }
// destroyConstant - Remove the constant from the constant table... // destroyConstant - Remove the constant from the constant table...
@ -513,18 +480,16 @@ void ConstantExpr::destroyConstant() {
destroyConstantImpl(); destroyConstantImpl();
} }
const char* const char *ConstantExpr::getOpcodeName(unsigned Opcode) {
ConstantExpr::getOpcodeName(unsigned opCode) { return Instruction::getOpcodeName(Opcode);
return Instruction::getOpcodeName(opCode);
} }
//---- ConstantPointerRef::mutateReferences() implementation... //---- ConstantPointerRef::mutateReferences() implementation...
// //
unsigned unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
ConstantPointerRef::mutateReferences(Value* OldV, Value *NewV) {
assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!"); assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
GlobalValue* NewGV = cast<GlobalValue>(NewV); GlobalValue *NewGV = cast<GlobalValue>(NewV);
getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV); getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
Operands[0] = NewGV; Operands[0] = NewGV;
return 1; return 1;
@ -533,14 +498,13 @@ ConstantPointerRef::mutateReferences(Value* OldV, Value *NewV) {
//---- ConstantPointerExpr::mutateReferences() implementation... //---- ConstantPointerExpr::mutateReferences() implementation...
// //
unsigned unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
ConstantExpr::mutateReferences(Value* OldV, Value *NewV) { unsigned NumReplaced = 0;
unsigned numReplaced = 0; Constant *NewC = cast<Constant>(NewV);
Constant* NewC = cast<Constant>(NewV); for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
for (unsigned i=0, N = getNumOperands(); i < N; ++i)
if (Operands[i] == OldV) { if (Operands[i] == OldV) {
++numReplaced; ++NumReplaced;
Operands[i] = NewC; Operands[i] = NewC;
} }
return numReplaced; return NumReplaced;
} }